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.
This commit is contained in:
Harshit Bansal 2017-08-19 14:23:30 +00:00 committed by Tim Abbott
parent 948cf54ee3
commit facb5dbe85
2 changed files with 28 additions and 20 deletions

View File

@ -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()

View File

@ -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