py3: Switch almost all shebang lines to use `python3`.
This causes `upgrade-zulip-from-git`, as well as a no-option run of
`tools/build-release-tarball`, to produce a Zulip install running
Python 3, rather than Python 2. In particular this means that the
virtualenv we create, in which all application code runs, is Python 3.
One shebang line, on `zulip-ec2-configure-interfaces`, explicitly
keeps Python 2, and at least one external ops script, `wal-e`, also
still runs on Python 2. See discussion on the respective previous
commits that made those explicit. There may also be some other
third-party scripts we use, outside of this source tree and running
outside our virtualenv, that still run on Python 2.
2017-08-02 23:15:16 +02:00
|
|
|
#!/usr/bin/env python3
|
2017-01-29 21:33:44 +01:00
|
|
|
#
|
|
|
|
# See docs/emoji.md for a high-level explanation of how this system
|
|
|
|
# works.
|
2016-10-20 20:41:39 +02:00
|
|
|
import os
|
|
|
|
import sys
|
2017-09-22 20:59:21 +02:00
|
|
|
import ujson
|
|
|
|
|
2017-09-23 14:23:06 +02:00
|
|
|
from typing import Any, Dict, List, Text, Union
|
2015-09-25 08:56:36 +02:00
|
|
|
|
2017-05-23 17:15:26 +02:00
|
|
|
from emoji_setup_utils import generate_emoji_catalog, generate_codepoint_to_name_map, \
|
|
|
|
emoji_names_for_picker, EMOJISETS
|
2017-01-26 08:35:23 +01:00
|
|
|
|
2016-10-20 20:41:39 +02:00
|
|
|
ZULIP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../')
|
|
|
|
sys.path.append(ZULIP_PATH)
|
|
|
|
|
2017-08-19 16:23:30 +02:00
|
|
|
from scripts.lib.zulip_tools import generate_sha1sum_emoji, run
|
2016-10-20 20:41:39 +02:00
|
|
|
|
2016-12-28 05:07:10 +01:00
|
|
|
TARGET_EMOJI_DUMP = os.path.join(ZULIP_PATH, 'static', 'generated', 'emoji')
|
2016-10-20 20:41:39 +02:00
|
|
|
EMOJI_CACHE_PATH = "/srv/zulip-emoji-cache"
|
2016-12-28 04:58:41 +01:00
|
|
|
EMOJI_SCRIPT_DIR_PATH = os.path.join(ZULIP_PATH, 'tools', 'setup', 'emoji')
|
2017-09-19 11:03:56 +02:00
|
|
|
NODE_MODULES_PATH = os.path.join(ZULIP_PATH, 'node_modules')
|
|
|
|
EMOJI_DATA_PATH = os.path.join(NODE_MODULES_PATH, 'emoji-datasource')
|
2016-10-20 20:41:39 +02:00
|
|
|
|
2017-01-26 00:41:53 +01:00
|
|
|
EMOJI_CODES_FILE_TEMPLATE = """\
|
|
|
|
var emoji_codes = (function () {
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
exports.names = %(names)s;
|
|
|
|
|
|
|
|
exports.codepoints = %(codepoints)s;
|
|
|
|
|
2017-02-10 21:23:58 +01:00
|
|
|
exports.name_to_codepoint = %(name_to_codepoint)s;
|
|
|
|
|
2017-06-24 20:01:38 +02:00
|
|
|
exports.codepoint_to_name = %(codepoint_to_name)s;
|
|
|
|
|
2017-03-19 09:41:24 +01:00
|
|
|
exports.emoji_catalog = %(emoji_catalog)s;
|
|
|
|
|
2017-05-17 11:03:03 +02:00
|
|
|
exports.patched_css_classes = %(patched_css_classes)s;
|
|
|
|
|
2017-01-26 00:41:53 +01:00
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = emoji_codes;
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2017-03-20 15:09:26 +01:00
|
|
|
SPRITE_CSS_FILE_TEMPLATE = """\
|
|
|
|
div.emoji,
|
|
|
|
span.emoji
|
|
|
|
{
|
|
|
|
display: inline-block;
|
2017-04-01 17:20:32 +02:00
|
|
|
background-image: url('sheet_%(emojiset)s_32.png');
|
2017-05-28 13:11:57 +02:00
|
|
|
-webkit-background-size: 4900%%;
|
|
|
|
-moz-background-size: 4900%%;
|
|
|
|
background-size: 4900%%;
|
2017-03-20 15:09:26 +01:00
|
|
|
background-repeat: no-repeat;
|
|
|
|
|
|
|
|
/* Hide the text. */
|
|
|
|
text-indent: 100%%;
|
|
|
|
white-space: nowrap;
|
|
|
|
overflow: hidden;
|
|
|
|
}
|
|
|
|
|
|
|
|
%(emoji_positions)s
|
|
|
|
"""
|
|
|
|
|
|
|
|
EMOJI_POS_INFO_TEMPLATE = """\
|
|
|
|
.emoji-%(codepoint)s {
|
|
|
|
background-position: %(pos_x)s%% %(pos_y)s%%;
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2017-05-24 19:35:00 +02:00
|
|
|
# change directory
|
2016-10-20 20:41:39 +02:00
|
|
|
os.chdir(EMOJI_SCRIPT_DIR_PATH)
|
|
|
|
|
|
|
|
if 'TRAVIS' in os.environ:
|
|
|
|
# In Travis CI, we don't have root access
|
|
|
|
EMOJI_CACHE_PATH = "/home/travis/zulip-emoji-cache"
|
|
|
|
|
|
|
|
def main():
|
|
|
|
# type: () -> None
|
|
|
|
success_stamp = get_success_stamp()
|
2017-09-22 08:15:01 +02:00
|
|
|
source_emoji_dump = os.path.dirname(success_stamp)
|
2016-10-20 20:41:39 +02:00
|
|
|
|
|
|
|
if not os.path.exists(success_stamp):
|
|
|
|
print("Dumping emojis ...")
|
|
|
|
dump_emojis(source_emoji_dump)
|
|
|
|
run(['touch', success_stamp])
|
|
|
|
|
|
|
|
print("Using cached emojis from {}".format(source_emoji_dump))
|
|
|
|
run(['rm', '-rf', TARGET_EMOJI_DUMP])
|
2017-09-23 15:52:56 +02:00
|
|
|
os.symlink(source_emoji_dump, TARGET_EMOJI_DUMP)
|
2016-10-20 20:41:39 +02:00
|
|
|
|
|
|
|
def get_success_stamp():
|
|
|
|
# type: () -> str
|
2017-08-19 16:23:30 +02:00
|
|
|
sha1_hexdigest = generate_sha1sum_emoji(ZULIP_PATH)
|
|
|
|
return os.path.join(EMOJI_CACHE_PATH, sha1_hexdigest, 'emoji', '.success-stamp')
|
2016-10-20 20:41:39 +02:00
|
|
|
|
2017-09-23 14:23:06 +02:00
|
|
|
def generate_sprite_css_files(cache_path, emoji_map, emoji_data):
|
|
|
|
# type: (Text, Dict[Text, Text], List[Dict[Text, Any]]) -> None
|
|
|
|
# Spritesheet CSS generation code.
|
|
|
|
emoji_positions = ""
|
|
|
|
for emoji in emoji_data:
|
|
|
|
if emoji["has_img_google"]:
|
|
|
|
emoji_positions += EMOJI_POS_INFO_TEMPLATE % {
|
|
|
|
'codepoint': emoji['unified'].lower(),
|
|
|
|
'pos_x': (emoji["sheet_x"] * 100) / 48,
|
|
|
|
'pos_y': (emoji["sheet_y"] * 100) / 48,
|
|
|
|
}
|
|
|
|
# Remove the code below once the migration to iamcal's dataset is complete.
|
|
|
|
emoji_name = emoji['short_name']
|
|
|
|
codepoint = emoji['unified'].lower()
|
|
|
|
if emoji_name in emoji_map and codepoint != emoji_map[emoji_name]:
|
|
|
|
emoji_positions += EMOJI_POS_INFO_TEMPLATE % {
|
|
|
|
'codepoint': emoji_map[emoji_name],
|
|
|
|
'pos_x': (emoji["sheet_x"] * 100) / 48,
|
|
|
|
'pos_y': (emoji["sheet_y"] * 100) / 48,
|
|
|
|
}
|
|
|
|
|
|
|
|
for emojiset in EMOJISETS:
|
|
|
|
SPRITE_CSS_PATH = os.path.join(cache_path, '%s_sprite.css' % (emojiset,))
|
|
|
|
sprite_css_file = open(SPRITE_CSS_PATH, 'w')
|
|
|
|
sprite_css_file.write(SPRITE_CSS_FILE_TEMPLATE % {'emojiset': emojiset,
|
|
|
|
'emoji_positions': emoji_positions,
|
|
|
|
})
|
|
|
|
sprite_css_file.close()
|
|
|
|
|
2017-09-22 01:26:46 +02:00
|
|
|
def setup_emoji_farm(cache_path, emoji_map, emoji_data):
|
|
|
|
# type: (Text, Dict[Text, Text], List[Dict[Text, Any]]) -> None
|
2017-09-19 11:03:56 +02:00
|
|
|
# Copy individual emoji images from npm package.
|
|
|
|
src_emoji_farm = os.path.join(NODE_MODULES_PATH, 'emoji-datasource-google', 'img', 'google', '64', '*')
|
|
|
|
target_emoji_farm = os.path.join(cache_path, 'images-google-64')
|
|
|
|
run(['mkdir', '-p', target_emoji_farm])
|
|
|
|
run(['cp', '-RPp', src_emoji_farm, target_emoji_farm], shell=True)
|
|
|
|
|
|
|
|
# Copy zulip.png to the emoji farm.
|
|
|
|
zulip_image = "{}/static/assets/zulip-emoji/*".format(ZULIP_PATH)
|
|
|
|
run(['cp', '-RPp', zulip_image, target_emoji_farm], shell=True)
|
|
|
|
|
2017-09-22 01:26:46 +02:00
|
|
|
# Copy spritesheets.
|
|
|
|
for emojiset in EMOJISETS:
|
|
|
|
input_sprite_sheet = os.path.join(EMOJI_DATA_PATH, 'img', emojiset, 'sheets', '32.png')
|
|
|
|
output_sprite_sheet = os.path.join(cache_path, 'sheet_%s_32.png' % (emojiset,))
|
|
|
|
run(['cp', input_sprite_sheet, output_sprite_sheet])
|
|
|
|
|
|
|
|
generate_sprite_css_files(cache_path, emoji_map, emoji_data)
|
|
|
|
|
2017-09-22 00:42:49 +02:00
|
|
|
def setup_old_emoji_farm(cache_path, emoji_map):
|
|
|
|
# type: (Text, Dict[Text, Text]) -> None
|
|
|
|
# Code for setting up old emoji farm.
|
|
|
|
# Some image files in the old emoji farm had a different name than in the new emoji
|
|
|
|
# farm. `remapped_emojis` is a map that contains a mapping of their name in the old
|
|
|
|
# emoji farm to their name in the new emoji farm.
|
|
|
|
remapped_emojis = {
|
|
|
|
"0023": "0023-20e3", # Hash
|
|
|
|
"0030": "0030-20e3", # Zero
|
|
|
|
"0031": "0031-20e3", # One
|
|
|
|
"0032": "0032-20e3", # Two
|
|
|
|
"0033": "0033-20e3", # Three
|
|
|
|
"0034": "0034-20e3", # Four
|
|
|
|
"0035": "0035-20e3", # Five
|
|
|
|
"0036": "0036-20e3", # Six
|
|
|
|
"0037": "0037-20e3", # Seven
|
|
|
|
"0038": "0038-20e3", # Eight
|
|
|
|
"0039": "0039-20e3", # Nine
|
|
|
|
"1f48f": "1f469-200d-2764-200d-1f48b-200d-1f468", # Couple kiss
|
|
|
|
"1f491": "1f469-200d-2764-200d-1f468", # Couple with heart
|
|
|
|
}
|
|
|
|
|
|
|
|
os.chdir(cache_path)
|
|
|
|
emoji_cache_path = os.path.join(cache_path, 'images', 'emoji')
|
|
|
|
unicode_emoji_cache_path = os.path.join(cache_path, 'images', 'emoji', 'unicode')
|
|
|
|
google_emoji_cache_path = os.path.join(cache_path, 'images-google-64')
|
2017-09-22 01:02:11 +02:00
|
|
|
run(['mkdir', '-p', emoji_cache_path])
|
|
|
|
run(['mkdir', '-p', unicode_emoji_cache_path])
|
2017-09-22 00:42:49 +02:00
|
|
|
|
|
|
|
# Symlink zulip.png image file.
|
|
|
|
image_file_path = os.path.join(google_emoji_cache_path, 'zulip.png')
|
|
|
|
symlink_path = os.path.join(emoji_cache_path, 'zulip.png')
|
|
|
|
os.symlink(image_file_path, symlink_path)
|
|
|
|
|
|
|
|
unicode_symlink_path = os.path.join(unicode_emoji_cache_path, 'zulip.png')
|
|
|
|
os.symlink(image_file_path, unicode_symlink_path)
|
|
|
|
|
|
|
|
for name, codepoint in emoji_map.items():
|
|
|
|
mapped_codepoint = remapped_emojis.get(codepoint, codepoint)
|
|
|
|
image_file_path = os.path.join(google_emoji_cache_path, '{}.png'.format(mapped_codepoint))
|
|
|
|
symlink_path = os.path.join(emoji_cache_path, '{}.png'.format(name))
|
|
|
|
os.symlink(image_file_path, symlink_path)
|
|
|
|
try:
|
|
|
|
# `emoji_map` contains duplicate entries for the same codepoint with different
|
|
|
|
# names. So creation of symlink for <codepoint>.png may throw `FileExistsError`.
|
|
|
|
unicode_symlink_path = os.path.join(unicode_emoji_cache_path, '{}.png'.format(codepoint))
|
|
|
|
os.symlink(image_file_path, unicode_symlink_path)
|
|
|
|
except FileExistsError:
|
|
|
|
pass
|
|
|
|
|
2017-09-22 13:47:50 +02:00
|
|
|
def generate_map_files(cache_path, emoji_map, emoji_data, emoji_catalog, unified_reactions_data):
|
|
|
|
# type: (Text, Dict[Text, Text], List[Dict[Text, Any]], Dict[Text, List[Text]], Dict[Text, Text]) -> None
|
|
|
|
# This function generates the various data files consumed by webapp, mobile apps, bugdown etc.
|
2017-01-26 00:41:53 +01:00
|
|
|
EMOJI_CODES_PATH = os.path.join(cache_path, 'emoji_codes.js')
|
|
|
|
emoji_codes_file = open(EMOJI_CODES_PATH, 'w')
|
2017-01-29 07:54:42 +01:00
|
|
|
|
|
|
|
# put thumbs_up before thumbs_down
|
|
|
|
names = emoji_names_for_picker(emoji_map)
|
|
|
|
down_index = names.index('thumbs_down')
|
|
|
|
up_index = names.index('thumbs_up')
|
|
|
|
names[down_index], names[up_index] = ('thumbs_up', 'thumbs_down')
|
|
|
|
|
2017-05-17 11:03:03 +02:00
|
|
|
# Patch CSS classes of flag emojis.
|
|
|
|
patched_css_classes = {}
|
|
|
|
for emoji in emoji_data:
|
|
|
|
if emoji['category'] == 'Flags':
|
|
|
|
for name in emoji['short_names']:
|
|
|
|
if name in emoji_map:
|
|
|
|
patched_css_classes[str(name)] = str(emoji['unified'].lower())
|
|
|
|
|
2017-06-24 20:01:38 +02:00
|
|
|
codepoint_to_name = generate_codepoint_to_name_map(names, unified_reactions_data)
|
|
|
|
|
2017-01-26 00:41:53 +01:00
|
|
|
emoji_codes_file.write(EMOJI_CODES_FILE_TEMPLATE % {
|
2017-01-29 07:54:42 +01:00
|
|
|
'names': names,
|
2017-02-10 21:23:58 +01:00
|
|
|
'codepoints': sorted([str(code_point) for code_point in set(emoji_map.values())]),
|
2017-03-19 09:41:24 +01:00
|
|
|
'name_to_codepoint': {str(key): str(emoji_map[key]) for key in emoji_map},
|
2017-06-24 20:01:38 +02:00
|
|
|
'codepoint_to_name': codepoint_to_name,
|
2017-05-17 11:03:03 +02:00
|
|
|
'emoji_catalog': emoji_catalog,
|
|
|
|
'patched_css_classes': patched_css_classes
|
2017-01-26 00:41:53 +01:00
|
|
|
})
|
|
|
|
emoji_codes_file.close()
|
|
|
|
|
2017-02-04 22:44:32 +01:00
|
|
|
NAME_TO_CODEPOINT_PATH = os.path.join(cache_path, 'name_to_codepoint.json')
|
|
|
|
name_to_codepoint_file = open(NAME_TO_CODEPOINT_PATH, 'w')
|
|
|
|
|
2017-03-24 19:38:15 +01:00
|
|
|
name_to_codepoint_file.write(ujson.dumps(emoji_map))
|
2017-02-04 22:44:32 +01:00
|
|
|
name_to_codepoint_file.close()
|
|
|
|
|
2017-05-23 17:15:26 +02:00
|
|
|
CODEPOINT_TO_NAME_PATH = os.path.join(cache_path, 'codepoint_to_name.json')
|
|
|
|
codepoint_to_name_file = open(CODEPOINT_TO_NAME_PATH, 'w')
|
|
|
|
|
|
|
|
codepoint_to_name_file.write(ujson.dumps(codepoint_to_name))
|
|
|
|
codepoint_to_name_file.close()
|
|
|
|
|
2017-09-22 13:47:50 +02:00
|
|
|
def dump_emojis(cache_path):
|
|
|
|
# type: (str) -> None
|
|
|
|
with open('emoji_map.json') as emoji_map_file:
|
|
|
|
emoji_map = ujson.load(emoji_map_file)
|
|
|
|
|
|
|
|
EMOJI_DATA_FILE_PATH = os.path.join(EMOJI_DATA_PATH, 'emoji.json')
|
|
|
|
with open(EMOJI_DATA_FILE_PATH) as emoji_data_file:
|
|
|
|
emoji_data = ujson.load(emoji_data_file)
|
|
|
|
emoji_catalog = generate_emoji_catalog(emoji_data)
|
|
|
|
|
|
|
|
UNIFIED_REACTIONS_PATH = os.path.join(ZULIP_PATH, 'zerver', 'management', 'data', 'unified_reactions.json')
|
|
|
|
with open(UNIFIED_REACTIONS_PATH) as unified_reactions_file:
|
|
|
|
unified_reactions_data = ujson.load(unified_reactions_file)
|
|
|
|
|
|
|
|
# Setup emoji farms.
|
|
|
|
run(['rm', '-rf', cache_path])
|
|
|
|
setup_emoji_farm(cache_path, emoji_map, emoji_data)
|
|
|
|
setup_old_emoji_farm(cache_path, emoji_map)
|
|
|
|
|
|
|
|
# Generate various map files.
|
|
|
|
generate_map_files(cache_path, emoji_map, emoji_data, emoji_catalog, unified_reactions_data)
|
|
|
|
|
2016-10-20 20:41:39 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|