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
|
2024-08-22 18:08:03 +02:00
|
|
|
|
import subprocess
|
2016-10-20 20:41:39 +02:00
|
|
|
|
import sys
|
2024-07-12 02:30:25 +02:00
|
|
|
|
from collections.abc import Iterator, Sequence
|
|
|
|
|
from typing import Any
|
2015-09-25 08:56:36 +02:00
|
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
|
import orjson
|
2020-06-11 00:54:34 +02:00
|
|
|
|
|
2022-12-04 10:59:47 +01:00
|
|
|
|
ZULIP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../../")
|
|
|
|
|
sys.path.append(ZULIP_PATH)
|
|
|
|
|
|
|
|
|
|
from scripts.lib.zulip_tools import generate_sha1sum_emoji, run_as_root
|
|
|
|
|
from tools.setup.emoji.emoji_names import EMOJI_NAME_MAPS
|
|
|
|
|
from tools.setup.emoji.emoji_setup_utils import (
|
2020-06-11 00:54:34 +02:00
|
|
|
|
EMOTICON_CONVERSIONS,
|
|
|
|
|
REMAPPED_EMOJIS,
|
2023-11-29 06:47:59 +01:00
|
|
|
|
emoji_is_supported,
|
2020-06-11 00:54:34 +02:00
|
|
|
|
emoji_names_for_picker,
|
|
|
|
|
generate_codepoint_to_name_map,
|
2022-06-21 23:56:52 +02:00
|
|
|
|
generate_codepoint_to_names_map,
|
2020-06-11 00:54:34 +02:00
|
|
|
|
generate_emoji_catalog,
|
|
|
|
|
generate_name_to_codepoint_map,
|
|
|
|
|
get_emoji_code,
|
|
|
|
|
)
|
2017-01-26 08:35:23 +01:00
|
|
|
|
|
2024-08-26 21:30:43 +02:00
|
|
|
|
EMOJI_CACHE_BASE_PATH = "/srv/zulip-emoji-cache"
|
2021-02-12 08:20:45 +01:00
|
|
|
|
EMOJI_SCRIPT_DIR_PATH = os.path.join(ZULIP_PATH, "tools", "setup", "emoji")
|
|
|
|
|
NODE_MODULES_PATH = os.path.join(ZULIP_PATH, "node_modules")
|
2016-10-20 20:41:39 +02:00
|
|
|
|
|
2020-02-21 19:26:35 +01:00
|
|
|
|
|
|
|
|
|
# The CSS for emoji spritesheet has somewhat tricky requirements. One
|
|
|
|
|
# is that we want to be able to use the same emoji CSS classes for
|
|
|
|
|
# different display sizes of our emoji (e.g. reactions are smaller
|
|
|
|
|
# than inline message emoji, which are smaller than those in the emoji
|
|
|
|
|
# picker) while only downloading 1 copy of the spritesheet, having
|
|
|
|
|
# good browser rendering performance, and reusing as much common CSS
|
|
|
|
|
# as is possible.
|
|
|
|
|
|
|
|
|
|
# Our solution to those problem is to use the `background-size` (Which
|
|
|
|
|
# is e.g. 5700%) and background-position attributes to select the
|
|
|
|
|
# region of the spritesheet corresponding to the target sprite and
|
|
|
|
|
# display it properly scaled in an emoji span.
|
2017-03-20 15:09:26 +01:00
|
|
|
|
SPRITE_CSS_FILE_TEMPLATE = """\
|
|
|
|
|
div.emoji,
|
|
|
|
|
span.emoji
|
2020-07-10 01:57:43 +02:00
|
|
|
|
{{
|
2017-03-20 15:09:26 +01:00
|
|
|
|
display: inline-block;
|
2024-08-22 18:08:03 +02:00
|
|
|
|
background-image: url(../emoji/{emojiset}.webp);
|
2020-07-10 01:57:43 +02:00
|
|
|
|
background-size: {background_size};
|
2017-03-20 15:09:26 +01:00
|
|
|
|
background-repeat: no-repeat;
|
|
|
|
|
|
|
|
|
|
/* Hide the text. */
|
2020-07-10 01:57:43 +02:00
|
|
|
|
text-indent: 100%;
|
2017-03-20 15:09:26 +01:00
|
|
|
|
white-space: nowrap;
|
|
|
|
|
overflow: hidden;
|
2020-07-10 01:57:43 +02:00
|
|
|
|
}}
|
2017-03-20 15:09:26 +01:00
|
|
|
|
|
2018-07-10 08:06:41 +02:00
|
|
|
|
.emoji-1f419
|
2020-07-10 01:57:43 +02:00
|
|
|
|
{{
|
2023-02-22 23:03:47 +01:00
|
|
|
|
background-image: url(../../../static/generated/emoji/images-google-64/1f419.png) !important;
|
2020-07-10 01:57:43 +02:00
|
|
|
|
background-position: 0% 0% !important;
|
2018-07-10 08:06:41 +02:00
|
|
|
|
background-size: contain !important;
|
2020-07-10 01:57:43 +02:00
|
|
|
|
}}
|
2018-07-10 08:06:41 +02:00
|
|
|
|
|
2020-07-10 01:57:43 +02:00
|
|
|
|
{emoji_positions}
|
2017-03-20 15:09:26 +01:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
EMOJI_POS_INFO_TEMPLATE = """\
|
2020-07-10 01:57:43 +02:00
|
|
|
|
.emoji-{codepoint} {{
|
|
|
|
|
background-position: {pos_x} {pos_y};
|
|
|
|
|
}}
|
2017-03-20 15:09:26 +01:00
|
|
|
|
"""
|
|
|
|
|
|
2023-11-29 23:33:54 +01:00
|
|
|
|
EMOJI_OVERRIDE_TEMPLATE = """\
|
2022-05-17 00:59:28 +02:00
|
|
|
|
.emoji-{codepoint} {{
|
2023-11-29 23:33:54 +01:00
|
|
|
|
background-image: url(../../../static/generated/emoji/images-google-64/{codepoint}.png) !important;
|
|
|
|
|
background-position: 0% 0% !important;
|
|
|
|
|
background-size: contain !important;
|
2022-05-17 00:59:28 +02:00
|
|
|
|
}}
|
|
|
|
|
"""
|
|
|
|
|
|
2017-05-24 19:35:00 +02:00
|
|
|
|
# change directory
|
2016-10-20 20:41:39 +02:00
|
|
|
|
os.chdir(EMOJI_SCRIPT_DIR_PATH)
|
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
2017-11-22 21:59:08 +01:00
|
|
|
|
def main() -> None:
|
2024-08-26 21:30:43 +02:00
|
|
|
|
if not os.access(EMOJI_CACHE_BASE_PATH, os.W_OK):
|
provision: Let build_emoji build its own cache.
We no longer need to maintain duplicate code
related to where we set up the emoji
cache directory.
And we no longer need two extra steps for
people doing advanced (i.e. manual) setup.
There was no clear benefit to having provision
build the cache directory for `build_emoji`,
when it was easy to make `build_emoji` more
self-sufficient. The `build_emoji` tool
was already importing the library that has
`run_as_root`, and it was already responsible
for 99% of the create-directory kind of tasks.
(We always call `build_emoji` unconditionally from
`provision`, so there's no rationale in terms
of avoiding startup time or something.)
ASIDE:
Its not completely clear to me why we need
to put this directory in "/srv", instead of
somewhere more local (like we already do for
Travis), but maybe it's just to be like
its siblings in "/srv":
node_modules
yarn.lock
zulip-emoji-cache
zulip-npm-cache
zulip-py3-venv
zulip-thumbor-venv
zulip-venv-cache
zulip-yarn
I guess the caches that we keep in var are
dev-only, although I think some of what's under
`zulip-emoji-cache` is also dev-only in nature?
./var/webpack-cache
./var/mypy-cache
In `docs/subsystems/emoji.md` we say this:
```
The `build_emoji` tool generates the set of files under
`static/generated/emoji` (or really, it generates the
`/srv/zulip-emoji-cache/<sha1>/emoji` tree, and
`static/generated/emoji` is a symlink to that tree;we do this in
order to cache old versions to make provisioning and production
deployments super fast in the common case that we haven't changed the
emoji tooling). [...]
```
I don't really understand that rationale for the development
case, since `static/generated` is as much ignored by `git` as
'/srv' is, without the complications of needing `sudo` to create it.
And in production, I'm not sure how much time we're really saving,
as it takes me about 1.4s to fully rebuild the cache in dev, not to
mention we're taking on upgrade risk by sharing files between versions.
2020-04-17 14:08:55 +02:00
|
|
|
|
# Note: In production, this block will fail, since we don't
|
|
|
|
|
# assume sudo access; but it should never run in production
|
2024-08-26 21:30:43 +02:00
|
|
|
|
# anyway, because EMOJI_CACHE_BASE_PATH is created by Puppet before
|
provision: Let build_emoji build its own cache.
We no longer need to maintain duplicate code
related to where we set up the emoji
cache directory.
And we no longer need two extra steps for
people doing advanced (i.e. manual) setup.
There was no clear benefit to having provision
build the cache directory for `build_emoji`,
when it was easy to make `build_emoji` more
self-sufficient. The `build_emoji` tool
was already importing the library that has
`run_as_root`, and it was already responsible
for 99% of the create-directory kind of tasks.
(We always call `build_emoji` unconditionally from
`provision`, so there's no rationale in terms
of avoiding startup time or something.)
ASIDE:
Its not completely clear to me why we need
to put this directory in "/srv", instead of
somewhere more local (like we already do for
Travis), but maybe it's just to be like
its siblings in "/srv":
node_modules
yarn.lock
zulip-emoji-cache
zulip-npm-cache
zulip-py3-venv
zulip-thumbor-venv
zulip-venv-cache
zulip-yarn
I guess the caches that we keep in var are
dev-only, although I think some of what's under
`zulip-emoji-cache` is also dev-only in nature?
./var/webpack-cache
./var/mypy-cache
In `docs/subsystems/emoji.md` we say this:
```
The `build_emoji` tool generates the set of files under
`static/generated/emoji` (or really, it generates the
`/srv/zulip-emoji-cache/<sha1>/emoji` tree, and
`static/generated/emoji` is a symlink to that tree;we do this in
order to cache old versions to make provisioning and production
deployments super fast in the common case that we haven't changed the
emoji tooling). [...]
```
I don't really understand that rationale for the development
case, since `static/generated` is as much ignored by `git` as
'/srv' is, without the complications of needing `sudo` to create it.
And in production, I'm not sure how much time we're really saving,
as it takes me about 1.4s to fully rebuild the cache in dev, not to
mention we're taking on upgrade risk by sharing files between versions.
2020-04-17 14:08:55 +02:00
|
|
|
|
# build_emoji would be run.
|
2024-08-26 21:30:43 +02:00
|
|
|
|
run_as_root(["mkdir", "-p", EMOJI_CACHE_BASE_PATH])
|
|
|
|
|
run_as_root(["chown", f"{os.getuid()}:{os.getgid()}", EMOJI_CACHE_BASE_PATH])
|
provision: Let build_emoji build its own cache.
We no longer need to maintain duplicate code
related to where we set up the emoji
cache directory.
And we no longer need two extra steps for
people doing advanced (i.e. manual) setup.
There was no clear benefit to having provision
build the cache directory for `build_emoji`,
when it was easy to make `build_emoji` more
self-sufficient. The `build_emoji` tool
was already importing the library that has
`run_as_root`, and it was already responsible
for 99% of the create-directory kind of tasks.
(We always call `build_emoji` unconditionally from
`provision`, so there's no rationale in terms
of avoiding startup time or something.)
ASIDE:
Its not completely clear to me why we need
to put this directory in "/srv", instead of
somewhere more local (like we already do for
Travis), but maybe it's just to be like
its siblings in "/srv":
node_modules
yarn.lock
zulip-emoji-cache
zulip-npm-cache
zulip-py3-venv
zulip-thumbor-venv
zulip-venv-cache
zulip-yarn
I guess the caches that we keep in var are
dev-only, although I think some of what's under
`zulip-emoji-cache` is also dev-only in nature?
./var/webpack-cache
./var/mypy-cache
In `docs/subsystems/emoji.md` we say this:
```
The `build_emoji` tool generates the set of files under
`static/generated/emoji` (or really, it generates the
`/srv/zulip-emoji-cache/<sha1>/emoji` tree, and
`static/generated/emoji` is a symlink to that tree;we do this in
order to cache old versions to make provisioning and production
deployments super fast in the common case that we haven't changed the
emoji tooling). [...]
```
I don't really understand that rationale for the development
case, since `static/generated` is as much ignored by `git` as
'/srv' is, without the complications of needing `sudo` to create it.
And in production, I'm not sure how much time we're really saving,
as it takes me about 1.4s to fully rebuild the cache in dev, not to
mention we're taking on upgrade risk by sharing files between versions.
2020-04-17 14:08:55 +02:00
|
|
|
|
|
2020-04-17 14:00:18 +02:00
|
|
|
|
sha1_hexdigest = generate_sha1sum_emoji(ZULIP_PATH)
|
2024-08-26 21:30:43 +02:00
|
|
|
|
emoji_cache_path = os.path.join(EMOJI_CACHE_BASE_PATH, sha1_hexdigest)
|
|
|
|
|
success_stamp = os.path.join(emoji_cache_path, ".success-stamp")
|
2016-10-20 20:41:39 +02:00
|
|
|
|
|
|
|
|
|
if not os.path.exists(success_stamp):
|
|
|
|
|
print("Dumping emojis ...")
|
2024-08-26 21:30:43 +02:00
|
|
|
|
dump_emojis(emoji_cache_path)
|
2021-02-12 08:20:45 +01:00
|
|
|
|
with open(success_stamp, "w") as f:
|
2019-07-14 21:37:08 +02:00
|
|
|
|
f.close()
|
2016-10-20 20:41:39 +02:00
|
|
|
|
|
2024-08-26 21:30:43 +02:00
|
|
|
|
print(f"build_emoji: Using cached emojis from {emoji_cache_path}")
|
2016-10-20 20:41:39 +02:00
|
|
|
|
|
2024-08-26 21:30:43 +02:00
|
|
|
|
# /srv/zulip-emoji-cache/*/static gets symlinked to ZULIP_PATH/static/generated/emoji
|
|
|
|
|
TARGET_STATIC_EMOJI = os.path.join(ZULIP_PATH, "static", "generated", "emoji")
|
|
|
|
|
if os.path.lexists(TARGET_STATIC_EMOJI):
|
|
|
|
|
os.remove(TARGET_STATIC_EMOJI)
|
|
|
|
|
os.symlink(os.path.join(emoji_cache_path, "static"), TARGET_STATIC_EMOJI)
|
|
|
|
|
|
|
|
|
|
# /srv/zulip-emoji-cache/*/web gets copied to ZULIP_PATH/web/generated
|
2020-02-21 00:20:39 +01:00
|
|
|
|
# These must not be symlinked so webpack can resolve module references.
|
2024-08-22 18:08:03 +02:00
|
|
|
|
for subdir in ("emoji", "emoji-styles"):
|
|
|
|
|
target_dir = os.path.join(ZULIP_PATH, "web", "generated", subdir)
|
|
|
|
|
os.makedirs(target_dir, exist_ok=True)
|
|
|
|
|
to_remove = set(os.listdir(target_dir))
|
|
|
|
|
source_emoji_dump = os.path.join(emoji_cache_path, "web", subdir)
|
|
|
|
|
for filename in os.listdir(source_emoji_dump):
|
|
|
|
|
shutil.copy2(os.path.join(source_emoji_dump, filename), target_dir)
|
|
|
|
|
to_remove.discard(filename)
|
|
|
|
|
for filename in to_remove:
|
|
|
|
|
os.remove(os.path.join(target_dir, filename))
|
2020-02-21 00:20:39 +01:00
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
2020-02-20 21:35:27 +01:00
|
|
|
|
def percent(f: float) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
|
return f"{f * 100:0.3f}%"
|
2020-02-20 21:35:27 +01:00
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
|
def get_square_size(emoji_data: Sequence[dict[str, Any]]) -> int:
|
2020-02-21 13:32:01 +01:00
|
|
|
|
"""
|
|
|
|
|
Spritesheets are usually NxN squares, and we have to
|
|
|
|
|
infer N from the sheet_x/sheet_y values of emojis.
|
|
|
|
|
"""
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
|
def get_offsets(emoji_data: Sequence[dict[str, Any]]) -> Iterator[int]:
|
2018-08-26 15:01:53 +02:00
|
|
|
|
for emoji_dict in emoji_data:
|
2021-02-12 08:20:45 +01:00
|
|
|
|
yield emoji_dict["sheet_x"]
|
|
|
|
|
yield emoji_dict["sheet_y"]
|
|
|
|
|
if "skin_variations" in emoji_dict:
|
|
|
|
|
for img_info in emoji_dict["skin_variations"].values():
|
|
|
|
|
yield img_info["sheet_x"]
|
|
|
|
|
yield img_info["sheet_y"]
|
2018-08-26 15:01:53 +02:00
|
|
|
|
|
2020-02-21 13:32:01 +01:00
|
|
|
|
n = max(get_offsets(emoji_data)) + 1
|
|
|
|
|
return n
|
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
|
|
def generate_sprite_css_files(
|
2022-05-17 00:59:28 +02:00
|
|
|
|
cache_path: str,
|
2024-07-12 02:30:17 +02:00
|
|
|
|
emoji_data: list[dict[str, Any]],
|
2022-05-17 00:59:28 +02:00
|
|
|
|
emojiset: str,
|
|
|
|
|
alt_name: str,
|
2024-07-12 02:30:17 +02:00
|
|
|
|
fallback_emoji_data: Sequence[dict[str, Any]],
|
2021-02-12 08:19:30 +01:00
|
|
|
|
) -> None:
|
2020-02-20 21:35:27 +01:00
|
|
|
|
"""
|
2020-02-21 13:32:01 +01:00
|
|
|
|
Spritesheets are usually NxN squares.
|
2020-02-20 21:35:27 +01:00
|
|
|
|
"""
|
2020-02-21 13:32:01 +01:00
|
|
|
|
n = get_square_size(emoji_data)
|
2020-02-20 21:35:27 +01:00
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Each single emoji is 64x64, with 1px gutters on every border.
|
|
|
|
|
We just consider the gutters to be part of the image for
|
|
|
|
|
simplicity reasons, so you can think of the spritesheet as
|
|
|
|
|
an NxN square of 66x66 pre-padded emojis. The CSS
|
|
|
|
|
background-size parameter below says to size the background
|
|
|
|
|
element as N times the size of the element that you're drawing.
|
|
|
|
|
|
|
|
|
|
Note that we use percentages here, instead of absolute
|
|
|
|
|
pixel values, because when we render emojis as actual elements,
|
|
|
|
|
their size will vary depending on which part of the UI we're
|
|
|
|
|
in (message emojis, emoji reactions, emoji popup, emoji
|
|
|
|
|
popup showcase, etc.).
|
|
|
|
|
|
|
|
|
|
(The next step is to offset the image; that will be in the
|
|
|
|
|
upcoming loop.)
|
|
|
|
|
"""
|
|
|
|
|
background_size = percent(n)
|
|
|
|
|
|
2017-09-23 14:23:06 +02:00
|
|
|
|
emoji_positions = ""
|
|
|
|
|
for emoji in emoji_data:
|
2023-11-29 06:47:59 +01:00
|
|
|
|
if emoji_is_supported(emoji):
|
2020-02-20 21:35:27 +01:00
|
|
|
|
"""
|
|
|
|
|
For background-position we need to use percentages.
|
|
|
|
|
Absolute pixel values won't work, because the size
|
|
|
|
|
of the background sprite image is proportional to
|
|
|
|
|
the size of the element we're rendering, and we render
|
|
|
|
|
elements in multiple sizes.
|
|
|
|
|
|
|
|
|
|
The way that CSS background-position works is linear
|
|
|
|
|
interpolation. When you tell CSS background-position
|
|
|
|
|
is "42% 37%", then in the `x` dimension it will align
|
|
|
|
|
the image such that 42% of the background image is to
|
|
|
|
|
the left of the 42% mark in the element itself.
|
|
|
|
|
|
|
|
|
|
For simplicity assume we render the emoji as 66px
|
|
|
|
|
(and everything will scale appropriately for other
|
|
|
|
|
size images as long as we use percentages).
|
|
|
|
|
|
|
|
|
|
The image size will be 66n.
|
|
|
|
|
The left offset of the x-th emoji (including its
|
|
|
|
|
padding) will be 66x. And the element's width
|
|
|
|
|
will be 66.
|
|
|
|
|
|
|
|
|
|
So, solve this equation for `p`, where p is
|
|
|
|
|
the ratio that we'll later express as a
|
|
|
|
|
percentage:
|
|
|
|
|
|
|
|
|
|
<image offset> = <offset of p% mark of element>
|
|
|
|
|
(p * 66n) = 66x + p66
|
|
|
|
|
p * n = x + p
|
|
|
|
|
p * n - p = x
|
|
|
|
|
p * (n - 1) = x
|
|
|
|
|
p = x / (n - 1)
|
|
|
|
|
|
|
|
|
|
If you ever want to change the code so that the
|
|
|
|
|
gutters don't show up in the element, the algebra
|
|
|
|
|
will get more complicated.
|
|
|
|
|
"""
|
2020-07-10 01:57:43 +02:00
|
|
|
|
emoji_positions += EMOJI_POS_INFO_TEMPLATE.format(
|
|
|
|
|
codepoint=get_emoji_code(emoji),
|
|
|
|
|
pos_x=percent(emoji["sheet_x"] / (n - 1)),
|
|
|
|
|
pos_y=percent(emoji["sheet_y"] / (n - 1)),
|
|
|
|
|
)
|
2017-09-23 14:23:06 +02:00
|
|
|
|
|
2024-08-26 21:30:43 +02:00
|
|
|
|
SPRITE_STYLES_DIRECTORY = os.path.join(cache_path, "web", "emoji-styles")
|
|
|
|
|
os.makedirs(SPRITE_STYLES_DIRECTORY, exist_ok=True)
|
|
|
|
|
SPRITE_CSS_PATH = os.path.join(SPRITE_STYLES_DIRECTORY, f"{emojiset}-sprite.css")
|
2021-02-12 08:20:45 +01:00
|
|
|
|
with open(SPRITE_CSS_PATH, "w") as f:
|
2020-07-10 01:57:43 +02:00
|
|
|
|
f.write(
|
|
|
|
|
SPRITE_CSS_FILE_TEMPLATE.format(
|
|
|
|
|
emojiset=emojiset,
|
|
|
|
|
alt_name=alt_name,
|
|
|
|
|
emoji_positions=emoji_positions,
|
|
|
|
|
background_size=background_size,
|
|
|
|
|
),
|
|
|
|
|
)
|
2017-09-23 14:23:06 +02:00
|
|
|
|
|
2022-05-17 00:59:28 +02:00
|
|
|
|
# Google Classic stopped being supported in 2017. To be able to use other emoji, we
|
|
|
|
|
# fallback to Google Modern for any emoji not covered by Google Classic.
|
|
|
|
|
if emojiset == "google-blob":
|
|
|
|
|
extra_emoji_positions = ""
|
|
|
|
|
covered_emoji_codes = [
|
|
|
|
|
get_emoji_code(emoji) for emoji in emoji_data if emoji["has_img_google"]
|
|
|
|
|
]
|
|
|
|
|
for emoji in fallback_emoji_data:
|
|
|
|
|
code = get_emoji_code(emoji)
|
|
|
|
|
if emoji["has_img_google"] and code not in covered_emoji_codes:
|
2023-11-29 23:33:54 +01:00
|
|
|
|
extra_emoji_positions += EMOJI_OVERRIDE_TEMPLATE.format(
|
2022-05-17 00:59:28 +02:00
|
|
|
|
codepoint=code,
|
|
|
|
|
)
|
2023-11-29 06:47:59 +01:00
|
|
|
|
with open(SPRITE_CSS_PATH, "a") as f:
|
|
|
|
|
f.write(extra_emoji_positions)
|
|
|
|
|
|
|
|
|
|
# The Twitter emoji team was laid off in 2022, so new emoji aren't supported.
|
|
|
|
|
# https://github.com/twitter/twemoji/issues/570#issuecomment-1303422143.
|
|
|
|
|
# The "twitter" sprite sheet we’re using does have images in those locations,
|
|
|
|
|
# but they’re fallback images that emoji-datasource fills in from the Apple
|
|
|
|
|
# sprite sheet, which has unclear licensing implications.
|
|
|
|
|
# To be able to support newer emoji, we fallback to Google Modern for any emoji
|
|
|
|
|
# not covered by Twemoji.
|
|
|
|
|
if emojiset == "twitter":
|
|
|
|
|
extra_emoji_positions = ""
|
|
|
|
|
twitter_covered_emoji_codes = {
|
|
|
|
|
get_emoji_code(emoji) for emoji in emoji_data if emoji["has_img_twitter"]
|
|
|
|
|
}
|
|
|
|
|
for emoji in emoji_data:
|
|
|
|
|
code = get_emoji_code(emoji)
|
|
|
|
|
if emoji["has_img_google"] and code not in twitter_covered_emoji_codes:
|
2023-11-29 23:33:54 +01:00
|
|
|
|
extra_emoji_positions += EMOJI_OVERRIDE_TEMPLATE.format(
|
2023-11-29 06:47:59 +01:00
|
|
|
|
codepoint=code,
|
|
|
|
|
)
|
2022-05-17 00:59:28 +02:00
|
|
|
|
with open(SPRITE_CSS_PATH, "a") as f:
|
|
|
|
|
f.write(extra_emoji_positions)
|
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
|
def setup_emoji_farms(cache_path: str, emoji_data: list[dict[str, Any]]) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
|
def ensure_emoji_image(
|
2024-07-12 02:30:17 +02:00
|
|
|
|
emoji_dict: dict[str, Any], src_emoji_farm: str, target_emoji_farm: str
|
2021-02-12 08:19:30 +01:00
|
|
|
|
) -> 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)
|
2021-02-12 08:20:45 +01:00
|
|
|
|
img_file_name = emoji_code + ".png"
|
|
|
|
|
src_file = os.path.join(src_emoji_farm, emoji_dict["image"])
|
2018-08-09 21:06:21 +02:00
|
|
|
|
dst_file = os.path.join(target_emoji_farm, img_file_name)
|
|
|
|
|
shutil.copy2(src_file, dst_file)
|
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
def setup_emoji_farm(
|
2022-06-10 22:43:59 +02:00
|
|
|
|
emojiset: str,
|
2024-07-12 02:30:17 +02:00
|
|
|
|
emoji_data: list[dict[str, Any]],
|
2024-07-12 02:30:23 +02:00
|
|
|
|
alt_name: str | None = None,
|
2024-07-12 02:30:17 +02:00
|
|
|
|
fallback_emoji_data: Sequence[dict[str, Any]] = [],
|
2021-02-12 08:19:30 +01:00
|
|
|
|
) -> None:
|
2018-08-25 20:47:56 +02:00
|
|
|
|
# `alt_name` is an optional parameter that we use to avoid duplicating below
|
2021-09-08 21:21:51 +02:00
|
|
|
|
# code. It is only used while setting up google-blob emoji set as it is just
|
2018-08-25 20:47:56 +02:00
|
|
|
|
# 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
|
2021-09-08 21:21:51 +02:00
|
|
|
|
# emoji sets and is just equivalent to `emojiset`.
|
2018-08-25 20:47:56 +02:00
|
|
|
|
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(
|
2021-02-12 08:20:45 +01:00
|
|
|
|
NODE_MODULES_PATH, "emoji-datasource-" + emojiset, "img", alt_name, "64"
|
2021-02-12 08:19:30 +01:00
|
|
|
|
)
|
2024-08-26 21:30:43 +02:00
|
|
|
|
target_emoji_farm = os.path.join(cache_path, "static", "images-" + emojiset + "-64")
|
2018-07-18 23:50:16 +02:00
|
|
|
|
os.makedirs(target_emoji_farm, exist_ok=True)
|
2024-08-26 21:30:43 +02:00
|
|
|
|
print(f"Copying individual {emojiset} image files...")
|
2018-08-09 21:06:21 +02:00
|
|
|
|
for emoji_dict in emoji_data:
|
2021-02-12 08:20:45 +01: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)
|
2021-02-12 08:20:45 +01:00
|
|
|
|
skin_variations = emoji_dict.get("skin_variations", {})
|
2021-08-14 01:01:37 +02:00
|
|
|
|
for img_info in skin_variations.values():
|
2021-02-12 08:20:45 +01: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.
|
2023-02-22 23:03:47 +01:00
|
|
|
|
zulip_image = os.path.join(ZULIP_PATH, "web", "images", "zulip-emoji")
|
2018-07-18 23:50:16 +02:00
|
|
|
|
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
|
|
|
|
|
2021-09-08 21:21:51 +02:00
|
|
|
|
# We hardcode octopus emoji image to Google emoji set's old
|
2018-08-09 21:41:56 +02:00
|
|
|
|
# "cute octopus" image. Copy it to the emoji farms.
|
2021-02-12 08:20:45 +01:00
|
|
|
|
input_img_file = os.path.join(EMOJI_SCRIPT_DIR_PATH, "1f419.png")
|
2024-08-26 21:30:43 +02:00
|
|
|
|
output_img_file = os.path.join(target_emoji_farm, "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
|
|
|
|
|
2022-05-17 00:59:28 +02:00
|
|
|
|
generate_sprite_css_files(cache_path, emoji_data, emojiset, alt_name, fallback_emoji_data)
|
2017-09-22 01:26:46 +02:00
|
|
|
|
|
2024-08-22 18:08:03 +02:00
|
|
|
|
print(f"Converting {emojiset} sheet to webp...")
|
|
|
|
|
TARGET_EMOJI_SHEETS = os.path.join(cache_path, "web", "emoji")
|
|
|
|
|
os.makedirs(TARGET_EMOJI_SHEETS, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
sheet_src = os.path.join(
|
|
|
|
|
NODE_MODULES_PATH,
|
|
|
|
|
f"emoji-datasource-{emojiset}",
|
|
|
|
|
"img",
|
|
|
|
|
alt_name,
|
|
|
|
|
"sheets-256",
|
|
|
|
|
"64.png",
|
|
|
|
|
)
|
|
|
|
|
sheet_dst = os.path.join(TARGET_EMOJI_SHEETS, f"{emojiset}.webp")
|
|
|
|
|
# From libwebp: [Q is] between 0 and 100. For lossy, 0 gives
|
|
|
|
|
# the smallest size and 100 the largest. For lossless, this
|
|
|
|
|
# parameter is the amount of effort put into the
|
|
|
|
|
# compression: 0 is the fastest but gives larger files
|
|
|
|
|
# compared to the slowest, but best, 100.
|
|
|
|
|
subprocess.check_call(["vips", "copy", sheet_src, f"{sheet_dst}[lossless=true,Q=100]"])
|
|
|
|
|
|
2021-09-08 21:21:51 +02:00
|
|
|
|
# Set up standard emoji sets.
|
2021-02-12 08:20:45 +01:00
|
|
|
|
for emojiset in ["google", "twitter"]:
|
2018-08-25 10:17:29 +02:00
|
|
|
|
setup_emoji_farm(emojiset, emoji_data)
|
|
|
|
|
|
2021-09-08 21:21:51 +02:00
|
|
|
|
# Set up old Google "blobs" emoji set.
|
2021-02-12 08:19:30 +01:00
|
|
|
|
GOOGLE_BLOB_EMOJI_DATA_PATH = os.path.join(
|
2021-02-12 08:20:45 +01:00
|
|
|
|
NODE_MODULES_PATH, "emoji-datasource-google-blob", "emoji.json"
|
2021-02-12 08:19:30 +01:00
|
|
|
|
)
|
2020-08-07 01:09:47 +02:00
|
|
|
|
with open(GOOGLE_BLOB_EMOJI_DATA_PATH, "rb") as fp:
|
|
|
|
|
blob_emoji_data = orjson.loads(fp.read())
|
2022-05-17 00:59:28 +02:00
|
|
|
|
setup_emoji_farm("google-blob", blob_emoji_data, "google", emoji_data)
|
2018-08-25 20:47:56 +02:00
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
|
|
def setup_old_emoji_farm(
|
2024-07-12 02:30:17 +02:00
|
|
|
|
cache_path: str, emoji_map: dict[str, str], emoji_data: list[dict[str, Any]]
|
2021-02-12 08:19:30 +01:00
|
|
|
|
) -> None:
|
2017-09-22 00:42:49 +02:00
|
|
|
|
# Code for setting up old emoji farm.
|
2024-08-26 21:30:43 +02:00
|
|
|
|
emoji_cache_path = os.path.join(cache_path, "static", "images", "emoji")
|
|
|
|
|
unicode_emoji_cache_path = os.path.join(cache_path, "static", "images", "emoji", "unicode")
|
|
|
|
|
google_emoji_cache_path = os.path.join(cache_path, "static", "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.
|
2021-02-12 08:20:45 +01:00
|
|
|
|
image_file_path = os.path.join(google_emoji_cache_path, "zulip.png")
|
|
|
|
|
symlink_path = os.path.join(emoji_cache_path, "zulip.png")
|
2017-09-22 00:42:49 +02:00
|
|
|
|
os.symlink(image_file_path, symlink_path)
|
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
|
unicode_symlink_path = os.path.join(unicode_emoji_cache_path, "zulip.png")
|
2017-09-22 00:42:49 +02:00
|
|
|
|
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)
|
2021-02-12 08:20:45 +01:00
|
|
|
|
image_file_path = os.path.join(google_emoji_cache_path, f"{mapped_codepoint}.png")
|
|
|
|
|
symlink_path = os.path.join(emoji_cache_path, f"{name}.png")
|
2017-09-22 00:42:49 +02:00
|
|
|
|
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`.
|
2021-02-12 08:20:45 +01:00
|
|
|
|
unicode_symlink_path = os.path.join(unicode_emoji_cache_path, f"{codepoint}.png")
|
2017-09-22 00:42:49 +02:00
|
|
|
|
os.symlink(image_file_path, unicode_symlink_path)
|
|
|
|
|
except FileExistsError:
|
|
|
|
|
pass
|
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
|
def generate_map_files(cache_path: str, emoji_catalog: dict[str, list[str]]) -> None:
|
2022-06-21 23:56:52 +02:00
|
|
|
|
# This function generates the main data files about emoji that are
|
2021-05-14 00:16:30 +02:00
|
|
|
|
# consumed by the web app, mobile apps, Markdown processor, 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
|
|
|
|
|
2024-08-26 21:30:43 +02:00
|
|
|
|
EMOJI_CODES_FILE_PATH = os.path.join(cache_path, "static", "emoji_codes.json")
|
2021-02-12 08:20:45 +01:00
|
|
|
|
with open(EMOJI_CODES_FILE_PATH, "wb") as emoji_codes_file:
|
2021-02-12 08:19:30 +01:00
|
|
|
|
emoji_codes_file.write(
|
|
|
|
|
orjson.dumps(
|
|
|
|
|
{
|
2021-02-12 08:20:45 +01:00
|
|
|
|
"names": names,
|
|
|
|
|
"name_to_codepoint": name_to_codepoint,
|
|
|
|
|
"codepoint_to_name": codepoint_to_name,
|
|
|
|
|
"emoji_catalog": emoji_catalog,
|
|
|
|
|
"emoticon_conversions": EMOTICON_CONVERSIONS,
|
2021-02-12 08:19:30 +01:00
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2022-06-21 23:56:52 +02:00
|
|
|
|
# This is the more official API for mobile to fetch data about emoji.
|
|
|
|
|
# emoji_codes.json has a lot of goo, and we're creating this new file
|
|
|
|
|
# as a cleaner data format to move towards. We could add the rest of
|
|
|
|
|
# the data into this API-described data, and then the web client could
|
|
|
|
|
# switch to that which would allow us to drop the existing file. But
|
|
|
|
|
# we'll probably instead do #18121 which will make this file obsolete.
|
|
|
|
|
# So this is a temporary solution. CZO discussion:
|
2024-06-06 21:48:31 +02:00
|
|
|
|
# https://chat.zulip.org/#narrow/channel/378-api-design/topic/currently.20supported.20emoji/near/1394598
|
2024-08-26 21:30:43 +02:00
|
|
|
|
EMOJI_API_FILE_PATH = os.path.join(cache_path, "static", "emoji_api.json")
|
2022-06-21 23:56:52 +02:00
|
|
|
|
with open(EMOJI_API_FILE_PATH, "wb") as emoji_api_file:
|
|
|
|
|
emoji_api_file.write(
|
|
|
|
|
orjson.dumps(
|
|
|
|
|
{
|
|
|
|
|
"code_to_names": generate_codepoint_to_names_map(EMOJI_NAME_MAPS),
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2018-07-20 11:37:39 +02:00
|
|
|
|
|
2017-11-22 21:59:08 +01:00
|
|
|
|
def dump_emojis(cache_path: str) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
|
with open("emoji_map.json", "rb") as emoji_map_file:
|
2020-08-07 01:09:47 +02:00
|
|
|
|
emoji_map = orjson.loads(emoji_map_file.read())
|
2017-09-22 13:47:50 +02:00
|
|
|
|
|
2018-03-13 20:34:31 +01:00
|
|
|
|
# `emoji.json` or any other data file can be sourced from any of the supported
|
2021-09-08 21:21:51 +02:00
|
|
|
|
# emoji set packages, they all contain the same data files.
|
2021-02-12 08:20:45 +01:00
|
|
|
|
EMOJI_DATA_FILE_PATH = os.path.join(NODE_MODULES_PATH, "emoji-datasource-google", "emoji.json")
|
2020-08-07 01:09:47 +02:00
|
|
|
|
with open(EMOJI_DATA_FILE_PATH, "rb") as emoji_data_file:
|
|
|
|
|
emoji_data = orjson.loads(emoji_data_file.read())
|
2024-04-12 00:36:20 +02:00
|
|
|
|
|
|
|
|
|
# These are the codes that we'll be able to show Google Modern
|
|
|
|
|
# emoji for. (For other emoji sets, we fall back to Google Modern.)
|
|
|
|
|
supported_codes = {get_emoji_code(emoji) for emoji in emoji_data if emoji_is_supported(emoji)}
|
|
|
|
|
# These are in the emoji dropdown so we should make sure they're supported.
|
|
|
|
|
for code in EMOJI_NAME_MAPS:
|
|
|
|
|
# If an assertion here fails, we either need to find an image for Google Modern
|
|
|
|
|
# to display for that emoji, or we need to remove that emoji as an option for users,
|
|
|
|
|
# by removing it from emoji_names.py through a change to `generate_emoji_names`.
|
|
|
|
|
assert code in supported_codes
|
|
|
|
|
|
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
|
|
|
|
|
2020-10-13 23:50:18 +02:00
|
|
|
|
# Set up 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
|
|
|
|
|
2022-11-15 07:19:59 +01:00
|
|
|
|
# This file is needed to translate emoji when importing data from Slack.
|
|
|
|
|
shutil.copyfile(
|
2024-08-26 21:30:43 +02:00
|
|
|
|
EMOJI_DATA_FILE_PATH,
|
|
|
|
|
os.path.join(cache_path, "static", "emoji-datasource-google-emoji.json"),
|
2022-11-15 07:19:59 +01:00
|
|
|
|
)
|
|
|
|
|
|
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
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
2016-10-20 20:41:39 +02:00
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|