2020-04-20 15:27:36 +02:00
|
|
|
#!/usr/bin/env python3
|
2020-06-19 00:32:55 +02:00
|
|
|
import argparse
|
2020-04-20 15:27:36 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
if ZULIP_PATH not in sys.path:
|
|
|
|
sys.path.append(ZULIP_PATH)
|
|
|
|
from scripts.lib.setup_path import setup_path
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2020-04-20 15:27:36 +02:00
|
|
|
setup_path()
|
|
|
|
|
2020-04-20 20:06:52 +02:00
|
|
|
os.environ["DJANGO_SETTINGS_MODULE"] = "zproject.settings"
|
|
|
|
import django
|
|
|
|
|
|
|
|
django.setup()
|
|
|
|
|
2024-06-13 05:23:00 +02:00
|
|
|
import pyvips
|
2020-04-20 20:06:52 +02:00
|
|
|
|
2021-03-29 21:06:17 +02:00
|
|
|
from zerver.lib.integrations import INTEGRATIONS
|
2020-04-20 20:06:52 +02:00
|
|
|
from zerver.lib.storage import static_path
|
2024-06-13 05:23:00 +02:00
|
|
|
from zerver.lib.thumbnail import DEFAULT_AVATAR_SIZE
|
2020-04-20 20:06:52 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-05-15 09:45:06 +02:00
|
|
|
def create_integration_bot_avatar(logo_path: str, bot_avatar_path: str) -> None:
|
2020-04-20 20:06:52 +02:00
|
|
|
os.makedirs(os.path.dirname(bot_avatar_path), exist_ok=True)
|
2024-06-13 05:23:00 +02:00
|
|
|
avatar = pyvips.Image.thumbnail(logo_path, DEFAULT_AVATAR_SIZE, height=DEFAULT_AVATAR_SIZE)
|
|
|
|
if avatar.height != avatar.width:
|
|
|
|
avatar = avatar.gravity(
|
|
|
|
pyvips.CompassDirection.CENTRE, DEFAULT_AVATAR_SIZE, DEFAULT_AVATAR_SIZE
|
|
|
|
)
|
|
|
|
avatar.write_to_file(bot_avatar_path)
|
2020-04-20 20:06:52 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def generate_integration_bots_avatars(check_missing: bool = False) -> None:
|
2020-04-21 19:01:54 +02:00
|
|
|
missing = set()
|
2021-03-29 21:06:17 +02:00
|
|
|
for integration in INTEGRATIONS.values():
|
|
|
|
if not integration.logo_path:
|
2020-04-20 20:06:52 +02:00
|
|
|
continue
|
2020-05-15 09:45:06 +02:00
|
|
|
|
2021-03-29 21:06:17 +02:00
|
|
|
bot_avatar_path = integration.get_bot_avatar_path()
|
2020-05-15 09:45:06 +02:00
|
|
|
if bot_avatar_path is None:
|
|
|
|
continue
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
bot_avatar_path = os.path.join(ZULIP_PATH, "static", bot_avatar_path)
|
2020-04-21 19:01:54 +02:00
|
|
|
if check_missing:
|
|
|
|
if not os.path.isfile(bot_avatar_path):
|
2021-03-29 21:06:17 +02:00
|
|
|
missing.add(integration.name)
|
2020-04-21 19:01:54 +02:00
|
|
|
else:
|
2021-03-29 21:06:17 +02:00
|
|
|
create_integration_bot_avatar(static_path(integration.logo_path), bot_avatar_path)
|
2020-04-21 19:01:54 +02:00
|
|
|
|
|
|
|
if missing:
|
2021-02-12 08:19:30 +01:00
|
|
|
print(
|
2021-02-12 08:20:45 +01:00
|
|
|
"ERROR: Bot avatars are missing for these webhooks: {}.\n"
|
|
|
|
"ERROR: Run ./tools/setup/generate_integration_bots_avatars.py "
|
|
|
|
"to generate them.\nERROR: Commit the newly generated avatars to "
|
|
|
|
"the repository.".format(", ".join(missing))
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2020-04-21 19:01:54 +02:00
|
|
|
sys.exit(1)
|
2020-04-20 20:06:52 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if __name__ == "__main__":
|
2020-04-21 19:01:54 +02:00
|
|
|
parser = argparse.ArgumentParser()
|
2021-02-12 08:20:45 +01:00
|
|
|
parser.add_argument("--check-missing", action="store_true")
|
2020-04-21 19:01:54 +02:00
|
|
|
options = parser.parse_args()
|
|
|
|
generate_integration_bots_avatars(options.check_missing)
|