From facb5dbe851478a005fb636755317107f4c95d94 Mon Sep 17 00:00:00 2001 From: Harshit Bansal Date: Sat, 19 Aug 2017 14:23:30 +0000 Subject: [PATCH] zulip_tools.py: Extract `generate_sha1sum_emoji()`. Given the path of a zulip installation, it returns a hash corresponding to the emoji infrastructure of that installation. --- scripts/lib/zulip_tools.py | 25 +++++++++++++++++++++++++ tools/setup/emoji/build_emoji | 23 +++-------------------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/scripts/lib/zulip_tools.py b/scripts/lib/zulip_tools.py index 423bc885a5..0be9eae71d 100755 --- a/scripts/lib/zulip_tools.py +++ b/scripts/lib/zulip_tools.py @@ -3,6 +3,7 @@ from __future__ import print_function import argparse import datetime import errno +import hashlib import logging import os import pwd @@ -11,6 +12,7 @@ import shutil import subprocess import sys import time +import json if False: from typing import Sequence, Set, Text, Any @@ -236,3 +238,26 @@ def purge_unused_caches(caches_dir, caches_in_use, threshold_days, dry_run, cach print("Keeping used %s cache: %s" % (cache_type, cache_dir)) print("Done!\n") + +def generate_sha1sum_emoji(zulip_path): + # type: (Text) -> Text + ZULIP_EMOJI_DIR = os.path.join(zulip_path, 'tools', 'setup', 'emoji') + sha = hashlib.sha1() + + filenames = ['NotoColorEmoji.ttf', 'emoji_map.json', 'AndroidEmoji.ttf', + 'build_emoji', 'emoji_setup_utils.py'] + + for filename in filenames: + file_path = os.path.join(ZULIP_EMOJI_DIR, filename) + with open(file_path, 'rb') as reader: + sha.update(reader.read()) + + # Take into account the version of `emoji-datasource` package while generating success stamp. + PACKAGE_FILE_PATH = os.path.join(zulip_path, 'package.json') + with open(PACKAGE_FILE_PATH, 'r') as fp: + parsed_package_file = json.load(fp) + dependency_data = parsed_package_file['dependencies'] + emoji_datasource_version = dependency_data['emoji-datasource'].encode('utf-8') + sha.update(emoji_datasource_version) + + return sha.hexdigest() diff --git a/tools/setup/emoji/build_emoji b/tools/setup/emoji/build_emoji index ae98a9b417..baba9bb7ac 100755 --- a/tools/setup/emoji/build_emoji +++ b/tools/setup/emoji/build_emoji @@ -9,7 +9,6 @@ import shutil import subprocess import ujson import sys -import hashlib import xml.etree.ElementTree as ET from six import unichr from typing import Dict, Text, Union @@ -22,7 +21,7 @@ from emoji_setup_utils import generate_emoji_catalog, generate_codepoint_to_name ZULIP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../') sys.path.append(ZULIP_PATH) -from scripts.lib.zulip_tools import run +from scripts.lib.zulip_tools import generate_sha1sum_emoji, run AA_SCALE = 8 SIZE = (136, 136) @@ -189,24 +188,8 @@ def main(): def get_success_stamp(): # type: () -> str - sha = hashlib.sha1() - - filenames = ['NotoColorEmoji.ttf', 'emoji_map.json', 'AndroidEmoji.ttf', - 'build_emoji', 'emoji_setup_utils.py'] - - for filename in filenames: - with open(filename, 'rb') as reader: - sha.update(reader.read()) - - # Take into account the version of `emoji-datasource` package while generating success stamp. - PACKAGE_FILE_PATH = os.path.join(ZULIP_PATH, 'package.json') - with open(PACKAGE_FILE_PATH, 'r') as fp: - parsed_package_file = ujson.load(fp) - dependency_data = parsed_package_file['dependencies'] - emoji_datasource_version = dependency_data['emoji-datasource'].encode('utf-8') - sha.update(emoji_datasource_version) - - return os.path.join(EMOJI_CACHE_PATH, sha.hexdigest(), 'emoji', '.success-stamp') + sha1_hexdigest = generate_sha1sum_emoji(ZULIP_PATH) + return os.path.join(EMOJI_CACHE_PATH, sha1_hexdigest, 'emoji', '.success-stamp') def dump_emojis(cache_path): # type: (str) -> None