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
|
|
|
#
|
2017-11-08 17:55:36 +01:00
|
|
|
# See docs/subsystems/emoji.md for a high-level explanation of how this system
|
2017-01-29 21:33:44 +01:00
|
|
|
# works.
|
2016-10-20 20:41:39 +02:00
|
|
|
import os
|
2018-08-09 21:06:21 +02:00
|
|
|
import shutil
|
2016-10-20 20:41:39 +02:00
|
|
|
import sys
|
2017-09-22 20:59:21 +02:00
|
|
|
import ujson
|
|
|
|
|
2018-08-25 20:47:56 +02:00
|
|
|
from typing import Any, Dict, List, Optional
|
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, \
|
2018-08-09 21:27:23 +02:00
|
|
|
get_emoji_code, generate_name_to_codepoint_map, emoji_names_for_picker, \
|
2019-02-02 23:53:29 +01:00
|
|
|
EMOTICON_CONVERSIONS, REMAPPED_EMOJIS
|
2017-11-08 19:40:43 +01:00
|
|
|
from emoji_names import EMOJI_NAME_MAPS
|
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)
|
|
|
|
|
2018-07-18 23:50:16 +02:00
|
|
|
from scripts.lib.zulip_tools import generate_sha1sum_emoji
|
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')
|
2016-10-20 20:41:39 +02:00
|
|
|
|
2017-03-20 15:09:26 +01:00
|
|
|
SPRITE_CSS_FILE_TEMPLATE = """\
|
|
|
|
div.emoji,
|
|
|
|
span.emoji
|
|
|
|
{
|
|
|
|
display: inline-block;
|
2018-08-25 20:02:38 +02:00
|
|
|
background-image: url('sheet-%(emojiset)s-64.png');
|
2018-04-23 07:48:19 +02:00
|
|
|
-webkit-background-size: 5200%%;
|
|
|
|
-moz-background-size: 5200%%;
|
|
|
|
background-size: 5200%%;
|
2017-03-20 15:09:26 +01:00
|
|
|
background-repeat: no-repeat;
|
|
|
|
|
|
|
|
/* Hide the text. */
|
|
|
|
text-indent: 100%%;
|
|
|
|
white-space: nowrap;
|
|
|
|
overflow: hidden;
|
|
|
|
}
|
|
|
|
|
2018-07-10 08:06:41 +02:00
|
|
|
.emoji-1f419
|
|
|
|
{
|
|
|
|
background-image: url('images-google-64/1f419.png') !important;
|
|
|
|
background-position: 0%% 0%% !important;
|
|
|
|
background-size: contain !important;
|
|
|
|
}
|
|
|
|
|
2017-03-20 15:09:26 +01:00
|
|
|
%(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"
|
|
|
|
|
2017-11-22 21:59:08 +01:00
|
|
|
def main() -> None:
|
2016-10-20 20:41:39 +02:00
|
|
|
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)
|
2019-07-14 21:37:08 +02:00
|
|
|
with open(success_stamp, 'w') as f:
|
|
|
|
f.close()
|
2016-10-20 20:41:39 +02:00
|
|
|
|
|
|
|
print("Using cached emojis from {}".format(source_emoji_dump))
|
2018-07-18 23:50:16 +02:00
|
|
|
if os.path.lexists(TARGET_EMOJI_DUMP):
|
|
|
|
os.remove(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
|
|
|
|
2017-11-22 21:59:08 +01:00
|
|
|
def get_success_stamp() -> 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
|
|
|
|
2018-08-09 21:52:15 +02:00
|
|
|
def generate_sprite_css_files(cache_path: str,
|
|
|
|
emoji_data: List[Dict[str, Any]],
|
|
|
|
emojiset: str) -> None:
|
2018-08-26 15:01:53 +02:00
|
|
|
def get_max_val(field: str, emoji_data: List[Dict[str, Any]]) -> int:
|
|
|
|
max_val = 0
|
|
|
|
for emoji_dict in emoji_data:
|
|
|
|
max_val = max(max_val, emoji_dict[field])
|
|
|
|
if 'skin_variations' in emoji_dict:
|
|
|
|
for skin_tone, img_info in emoji_dict['skin_variations'].items():
|
|
|
|
max_val = max(max_val, img_info[field])
|
|
|
|
return max_val
|
|
|
|
|
|
|
|
# Spritesheet CSS generation code. Spritesheets are squared using
|
|
|
|
# padding, so we have to take only the maximum of two dimensions.
|
|
|
|
nrows = get_max_val('sheet_x', emoji_data)
|
|
|
|
ncols = get_max_val('sheet_y', emoji_data)
|
|
|
|
max_dim = max(nrows, ncols)
|
2017-09-23 14:23:06 +02:00
|
|
|
emoji_positions = ""
|
|
|
|
for emoji in emoji_data:
|
|
|
|
if emoji["has_img_google"]:
|
2018-07-13 17:47:29 +02:00
|
|
|
# Why is the test here has_img_google and not
|
|
|
|
# emoji_is_universal? Because we briefly supported all
|
|
|
|
# Google emoji (not just the universal ones), we need to
|
|
|
|
# ensure the spritesheet is setup to correctly display
|
|
|
|
# those google emoji (in case anyone used them).
|
2017-09-23 14:23:06 +02:00
|
|
|
emoji_positions += EMOJI_POS_INFO_TEMPLATE % {
|
2018-04-23 15:03:38 +02:00
|
|
|
'codepoint': get_emoji_code(emoji),
|
2018-08-26 15:01:53 +02:00
|
|
|
'pos_x': (emoji["sheet_x"] * 100) / max_dim,
|
|
|
|
'pos_y': (emoji["sheet_y"] * 100) / max_dim,
|
2017-09-23 14:23:06 +02:00
|
|
|
}
|
|
|
|
|
2018-08-25 20:16:23 +02:00
|
|
|
SPRITE_CSS_PATH = os.path.join(cache_path, '%s-sprite.css' % (emojiset,))
|
2019-07-14 21:37:08 +02:00
|
|
|
with open(SPRITE_CSS_PATH, 'w') as f:
|
|
|
|
f.write(SPRITE_CSS_FILE_TEMPLATE % {'emojiset': emojiset,
|
|
|
|
'emoji_positions': emoji_positions,
|
|
|
|
})
|
2017-09-23 14:23:06 +02:00
|
|
|
|
2018-08-25 10:18:22 +02:00
|
|
|
def setup_emoji_farms(cache_path: str, emoji_data: List[Dict[str, Any]]) -> None:
|
2018-08-26 14:32:58 +02:00
|
|
|
def ensure_emoji_image(emoji_dict: Dict[str, Any],
|
|
|
|
src_emoji_farm: str,
|
|
|
|
target_emoji_farm: str) -> None:
|
2018-08-09 21:06:21 +02:00
|
|
|
# We use individual images from emoji farm for rendering emojis
|
|
|
|
# in notification messages. We have a custom emoji formatter in
|
|
|
|
# notifications processing code that converts `span` tags to
|
|
|
|
# `img` and that logic requires us to have non-qualified
|
|
|
|
# `emoji_code` as file name for emoji.
|
|
|
|
emoji_code = get_emoji_code(emoji_dict)
|
|
|
|
img_file_name = emoji_code + '.png'
|
|
|
|
src_file = os.path.join(src_emoji_farm, emoji_dict['image'])
|
|
|
|
dst_file = os.path.join(target_emoji_farm, img_file_name)
|
|
|
|
shutil.copy2(src_file, dst_file)
|
|
|
|
|
2018-08-25 20:47:56 +02:00
|
|
|
def setup_emoji_farm(emojiset: str,
|
|
|
|
emoji_data: List[Dict[str, Any]],
|
|
|
|
alt_name: Optional[str]=None) -> None:
|
|
|
|
# `alt_name` is an optional parameter that we use to avoid duplicating below
|
|
|
|
# code. It is only used while setting up google-blob emojiset as it is just
|
|
|
|
# a wrapper for an older version of emoji-datasource package due to which we
|
|
|
|
# need to use 'google' at some places in this code. It has no meaning for other
|
|
|
|
# emojisets and is just equivalent to `emojiset`.
|
|
|
|
alt_name = alt_name or emojiset
|
|
|
|
|
2017-10-27 21:44:12 +02:00
|
|
|
# Copy individual emoji images from npm packages.
|
|
|
|
src_emoji_farm = os.path.join(
|
2018-08-25 20:47:56 +02:00
|
|
|
NODE_MODULES_PATH, 'emoji-datasource-' + emojiset, 'img', alt_name, '64')
|
2017-10-27 21:44:12 +02:00
|
|
|
target_emoji_farm = os.path.join(cache_path, 'images-' + emojiset + '-64')
|
2018-07-18 23:50:16 +02:00
|
|
|
os.makedirs(target_emoji_farm, exist_ok=True)
|
2018-08-09 21:06:21 +02:00
|
|
|
print("Copying individual image files...")
|
|
|
|
for emoji_dict in emoji_data:
|
2018-08-25 20:47:56 +02:00
|
|
|
if emoji_dict['has_img_' + alt_name]:
|
2018-08-26 14:32:58 +02:00
|
|
|
ensure_emoji_image(emoji_dict, src_emoji_farm, target_emoji_farm)
|
2018-08-09 21:06:21 +02:00
|
|
|
skin_variations = emoji_dict.get('skin_variations', {})
|
2018-08-26 17:46:45 +02:00
|
|
|
for skin_tone, img_info in skin_variations.items():
|
2018-08-25 20:47:56 +02:00
|
|
|
if img_info['has_img_' + alt_name]:
|
2018-08-26 17:46:45 +02:00
|
|
|
ensure_emoji_image(img_info, src_emoji_farm, target_emoji_farm)
|
2017-10-27 21:44:12 +02:00
|
|
|
|
|
|
|
# Copy zulip.png to the emoji farm.
|
2018-07-18 23:50:16 +02:00
|
|
|
zulip_image = os.path.join(ZULIP_PATH, 'static', 'assets', 'zulip-emoji')
|
|
|
|
for f in os.listdir(zulip_image):
|
|
|
|
shutil.copy2(os.path.join(zulip_image, f), target_emoji_farm, follow_symlinks=False)
|
2017-10-27 21:44:12 +02:00
|
|
|
|
|
|
|
# Copy spritesheets.
|
2018-03-13 20:34:31 +01:00
|
|
|
emoji_data_path = os.path.join(NODE_MODULES_PATH, 'emoji-datasource-' + emojiset)
|
2018-08-25 20:47:56 +02:00
|
|
|
input_sprite_sheet = os.path.join(emoji_data_path, 'img', alt_name, 'sheets-256', '64.png')
|
2018-08-25 20:02:38 +02:00
|
|
|
output_sprite_sheet = os.path.join(cache_path, 'sheet-%s-64.png' % (emojiset,))
|
2018-07-18 23:50:16 +02:00
|
|
|
shutil.copyfile(input_sprite_sheet, output_sprite_sheet)
|
2017-09-22 01:26:46 +02:00
|
|
|
|
2018-08-09 21:41:56 +02:00
|
|
|
# We hardcode octopus emoji image to Google emojiset's old
|
|
|
|
# "cute octopus" image. Copy it to the emoji farms.
|
|
|
|
input_img_file = os.path.join(EMOJI_SCRIPT_DIR_PATH, '1f419.png')
|
|
|
|
output_img_file = os.path.join(cache_path, 'images-' + emojiset + '-64', '1f419.png')
|
2018-07-18 23:50:16 +02:00
|
|
|
shutil.copyfile(input_img_file, output_img_file)
|
2018-08-09 21:41:56 +02:00
|
|
|
|
2018-08-09 21:52:15 +02:00
|
|
|
generate_sprite_css_files(cache_path, emoji_data, emojiset)
|
2017-09-22 01:26:46 +02:00
|
|
|
|
2018-08-25 20:47:56 +02:00
|
|
|
# Setup standard emojisets.
|
|
|
|
for emojiset in ['google', 'twitter']:
|
2018-08-25 10:17:29 +02:00
|
|
|
setup_emoji_farm(emojiset, emoji_data)
|
|
|
|
|
2018-08-25 20:47:56 +02:00
|
|
|
# Setup old google "blobs" emojiset.
|
|
|
|
GOOGLE_BLOB_EMOJI_DATA_PATH = os.path.join(NODE_MODULES_PATH,
|
|
|
|
'emoji-datasource-google-blob',
|
|
|
|
'emoji.json')
|
|
|
|
with open(GOOGLE_BLOB_EMOJI_DATA_PATH) as fp:
|
|
|
|
blob_emoji_data = ujson.load(fp)
|
|
|
|
setup_emoji_farm('google-blob', blob_emoji_data, 'google')
|
|
|
|
|
2018-04-23 07:48:19 +02:00
|
|
|
def setup_old_emoji_farm(cache_path: str,
|
|
|
|
emoji_map: Dict[str, str],
|
|
|
|
emoji_data: List[Dict[str, Any]]) -> None:
|
2017-09-22 00:42:49 +02:00
|
|
|
# Code for setting up old emoji farm.
|
|
|
|
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')
|
2018-07-18 23:50:16 +02:00
|
|
|
os.makedirs(emoji_cache_path, exist_ok=True)
|
|
|
|
os.makedirs(unicode_emoji_cache_path, exist_ok=True)
|
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():
|
2018-08-09 21:27:23 +02:00
|
|
|
mapped_codepoint = REMAPPED_EMOJIS.get(codepoint, codepoint)
|
2017-09-22 00:42:49 +02:00
|
|
|
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-11-22 21:59:08 +01:00
|
|
|
def generate_map_files(cache_path: str, emoji_catalog: Dict[str, List[str]]) -> None:
|
2020-02-06 07:07:10 +01:00
|
|
|
# This function generates the data file consumed by webapp, mobile apps, bugdown etc.
|
2017-11-08 19:40:43 +01:00
|
|
|
names = emoji_names_for_picker(EMOJI_NAME_MAPS)
|
|
|
|
codepoint_to_name = generate_codepoint_to_name_map(EMOJI_NAME_MAPS)
|
|
|
|
name_to_codepoint = generate_name_to_codepoint_map(EMOJI_NAME_MAPS)
|
2017-01-29 07:54:42 +01:00
|
|
|
|
2020-02-06 07:07:10 +01:00
|
|
|
EMOJI_CODES_FILE_PATH = os.path.join(cache_path, 'emoji_codes.json')
|
2017-11-08 19:40:43 +01:00
|
|
|
with open(EMOJI_CODES_FILE_PATH, 'w') as emoji_codes_file:
|
2020-02-06 07:07:10 +01:00
|
|
|
ujson.dump({
|
2018-07-13 17:37:08 +02:00
|
|
|
'names': names,
|
|
|
|
'name_to_codepoint': name_to_codepoint,
|
|
|
|
'codepoint_to_name': codepoint_to_name,
|
|
|
|
'emoji_catalog': emoji_catalog,
|
2018-07-20 11:37:39 +02:00
|
|
|
'emoticon_conversions': EMOTICON_CONVERSIONS,
|
2020-02-06 07:07:10 +01:00
|
|
|
}, emoji_codes_file)
|
2018-07-20 11:37:39 +02:00
|
|
|
|
2017-11-22 21:59:08 +01:00
|
|
|
def dump_emojis(cache_path: str) -> None:
|
2017-09-22 13:47:50 +02:00
|
|
|
with open('emoji_map.json') as emoji_map_file:
|
|
|
|
emoji_map = ujson.load(emoji_map_file)
|
|
|
|
|
2018-03-13 20:34:31 +01:00
|
|
|
# `emoji.json` or any other data file can be sourced from any of the supported
|
|
|
|
# emojiset packages, they all contain the same data files.
|
|
|
|
EMOJI_DATA_FILE_PATH = os.path.join(NODE_MODULES_PATH, 'emoji-datasource-google', 'emoji.json')
|
2017-09-22 13:47:50 +02:00
|
|
|
with open(EMOJI_DATA_FILE_PATH) as emoji_data_file:
|
|
|
|
emoji_data = ujson.load(emoji_data_file)
|
2017-11-08 19:40:43 +01:00
|
|
|
emoji_catalog = generate_emoji_catalog(emoji_data, EMOJI_NAME_MAPS)
|
2017-09-22 13:47:50 +02:00
|
|
|
|
|
|
|
# Setup emoji farms.
|
2018-07-18 23:50:16 +02:00
|
|
|
if os.path.exists(cache_path):
|
|
|
|
shutil.rmtree(cache_path)
|
2018-08-25 10:18:22 +02:00
|
|
|
setup_emoji_farms(cache_path, emoji_data)
|
2018-04-23 07:48:19 +02:00
|
|
|
setup_old_emoji_farm(cache_path, emoji_map, emoji_data)
|
2017-09-22 13:47:50 +02:00
|
|
|
|
|
|
|
# Generate various map files.
|
2017-11-08 19:40:43 +01:00
|
|
|
generate_map_files(cache_path, emoji_catalog)
|
2017-09-22 13:47:50 +02:00
|
|
|
|
2016-10-20 20:41:39 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|