2017-01-24 19:38:02 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2017-09-14 20:18:24 +00:00
|
|
|
from __future__ import print_function
|
2017-01-24 19:38:02 +00:00
|
|
|
import argparse
|
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
from os import path
|
2017-05-16 20:04:53 +00:00
|
|
|
import shutil
|
2017-01-24 19:38:02 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
"""Create aliases in target directory.
|
|
|
|
|
|
|
|
The target files should not contain the emoji variation selector
|
|
|
|
codepoint in their names."""
|
|
|
|
|
|
|
|
DATA_ROOT = path.dirname(path.abspath(__file__))
|
|
|
|
|
|
|
|
def str_to_seq(seq_str):
|
2017-05-16 20:04:53 +00:00
|
|
|
res = [int(s, 16) for s in seq_str.split('_')]
|
|
|
|
if 0xfe0f in res:
|
2017-09-14 20:18:24 +00:00
|
|
|
print('0xfe0f in file name: %s' % seq_str)
|
2017-05-16 20:04:53 +00:00
|
|
|
res = [x for x in res if x != 0xfe0f]
|
|
|
|
return tuple(res)
|
2017-01-24 19:38:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def seq_to_str(seq):
|
|
|
|
return '_'.join('%04x' % cp for cp in seq)
|
|
|
|
|
|
|
|
|
2017-04-13 21:34:20 +00:00
|
|
|
def read_default_unknown_flag_aliases():
|
|
|
|
unknown_flag_path = path.join(DATA_ROOT, 'unknown_flag_aliases.txt')
|
|
|
|
return read_emoji_aliases(unknown_flag_path)
|
|
|
|
|
|
|
|
|
2017-03-07 22:28:21 +00:00
|
|
|
def read_default_emoji_aliases():
|
2017-02-18 02:59:05 +00:00
|
|
|
alias_path = path.join(DATA_ROOT, 'emoji_aliases.txt')
|
|
|
|
return read_emoji_aliases(alias_path)
|
|
|
|
|
|
|
|
|
|
|
|
def read_emoji_aliases(filename):
|
2017-01-24 19:38:02 +00:00
|
|
|
result = {}
|
|
|
|
|
2017-02-18 02:59:05 +00:00
|
|
|
with open(filename, 'r') as f:
|
2017-01-24 19:38:02 +00:00
|
|
|
for line in f:
|
|
|
|
ix = line.find('#')
|
|
|
|
if (ix > -1):
|
|
|
|
line = line[:ix]
|
|
|
|
line = line.strip()
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
als, trg = (s.strip() for s in line.split(';'))
|
|
|
|
try:
|
2017-02-18 02:59:05 +00:00
|
|
|
als_seq = tuple([int(x, 16) for x in als.split('_')])
|
2017-01-24 19:38:02 +00:00
|
|
|
trg_seq = tuple([int(x, 16) for x in trg.split('_')])
|
|
|
|
except:
|
2017-09-14 20:18:24 +00:00
|
|
|
print('cannot process alias %s -> %s' % (als, trg))
|
2017-01-24 19:38:02 +00:00
|
|
|
continue
|
|
|
|
result[als_seq] = trg_seq
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2017-05-16 20:04:53 +00:00
|
|
|
def add_aliases(
|
|
|
|
srcdir, dstdir, aliasfile, prefix, ext, replace=False, copy=False,
|
|
|
|
dry_run=False):
|
|
|
|
"""Use aliasfile to create aliases of files in srcdir matching prefix/ext in
|
|
|
|
dstdir. If dstdir is null, use srcdir as dstdir. If replace is false
|
|
|
|
and a file already exists in dstdir, report and do nothing. If copy is false
|
|
|
|
create a symlink, else create a copy. If dry_run is true, report what would
|
|
|
|
be done. Dstdir will be created if necessary, even if dry_run is true."""
|
|
|
|
|
|
|
|
if not path.isdir(srcdir):
|
2017-09-14 20:18:24 +00:00
|
|
|
print('%s is not a directory' % srcdir, file=sys.stderr)
|
2017-01-24 19:38:02 +00:00
|
|
|
return
|
|
|
|
|
2017-05-16 20:04:53 +00:00
|
|
|
if not dstdir:
|
|
|
|
dstdir = srcdir
|
|
|
|
elif not path.isdir(dstdir):
|
|
|
|
os.makedirs(dstdir)
|
|
|
|
|
2017-01-24 19:38:02 +00:00
|
|
|
prefix_len = len(prefix)
|
|
|
|
suffix_len = len(ext) + 1
|
|
|
|
filenames = [path.basename(f)
|
2017-05-16 20:04:53 +00:00
|
|
|
for f in glob.glob(path.join(srcdir, '%s*.%s' % (prefix, ext)))]
|
2017-01-24 19:38:02 +00:00
|
|
|
seq_to_file = {
|
|
|
|
str_to_seq(name[prefix_len:-suffix_len]) : name
|
|
|
|
for name in filenames}
|
|
|
|
|
2017-05-16 20:04:53 +00:00
|
|
|
aliases = read_emoji_aliases(aliasfile)
|
2017-01-24 19:38:02 +00:00
|
|
|
aliases_to_create = {}
|
|
|
|
aliases_to_replace = []
|
2017-05-16 20:04:53 +00:00
|
|
|
alias_exists = False
|
|
|
|
for als, trg in sorted(aliases.items()):
|
2017-01-24 19:38:02 +00:00
|
|
|
if trg not in seq_to_file:
|
2017-09-14 20:18:24 +00:00
|
|
|
print('target %s for %s does not exist' % (
|
|
|
|
seq_to_str(trg), seq_to_str(als)), file=sys.stderr)
|
2017-01-24 19:38:02 +00:00
|
|
|
continue
|
2017-05-16 20:04:53 +00:00
|
|
|
alias_name = '%s%s.%s' % (prefix, seq_to_str(als), ext)
|
|
|
|
alias_path = path.join(dstdir, alias_name)
|
|
|
|
if path.exists(alias_path):
|
2017-01-24 19:38:02 +00:00
|
|
|
if replace:
|
2017-05-16 20:04:53 +00:00
|
|
|
aliases_to_replace.append(alias_name)
|
2017-01-24 19:38:02 +00:00
|
|
|
else:
|
2017-09-14 20:18:24 +00:00
|
|
|
print('alias %s exists' % seq_to_str(als), file=sys.stderr)
|
2017-05-16 20:04:53 +00:00
|
|
|
alias_exists = True
|
2017-01-24 19:38:02 +00:00
|
|
|
continue
|
|
|
|
target_file = seq_to_file[trg]
|
|
|
|
aliases_to_create[alias_name] = target_file
|
|
|
|
|
|
|
|
if replace:
|
|
|
|
if not dry_run:
|
|
|
|
for k in sorted(aliases_to_replace):
|
2017-05-16 20:04:53 +00:00
|
|
|
os.remove(path.join(dstdir, k))
|
2017-09-14 20:18:24 +00:00
|
|
|
print('replacing %d files' % len(aliases_to_replace))
|
2017-05-16 20:04:53 +00:00
|
|
|
elif alias_exists:
|
2017-09-14 20:18:24 +00:00
|
|
|
print('aborting, aliases exist.', file=sys.stderr)
|
2017-05-16 20:04:53 +00:00
|
|
|
return
|
2017-01-24 19:38:02 +00:00
|
|
|
|
|
|
|
for k, v in sorted(aliases_to_create.items()):
|
|
|
|
if dry_run:
|
|
|
|
msg = 'replace ' if k in aliases_to_replace else ''
|
2017-09-14 20:18:24 +00:00
|
|
|
print('%s%s -> %s' % (msg, k, v))
|
2017-01-24 19:38:02 +00:00
|
|
|
else:
|
|
|
|
try:
|
2017-05-16 20:04:53 +00:00
|
|
|
if copy:
|
|
|
|
shutil.copy2(path.join(srcdir, v), path.join(dstdir, k))
|
|
|
|
else:
|
|
|
|
# fix this to create relative symlinks
|
|
|
|
if srcdir == dstdir:
|
|
|
|
os.symlink(v, path.join(dstdir, k))
|
|
|
|
else:
|
|
|
|
raise Exception('can\'t create cross-directory symlinks yet')
|
|
|
|
except Exception as e:
|
2017-09-14 20:18:24 +00:00
|
|
|
print('failed to create %s -> %s' % (k, v), file=sys.stderr)
|
2017-05-16 20:04:53 +00:00
|
|
|
raise Exception('oops, ' + str(e))
|
2017-09-14 20:18:24 +00:00
|
|
|
print('created %d %s' % (
|
|
|
|
len(aliases_to_create), 'copies' if copy else 'symlinks'))
|
2017-01-24 19:38:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument(
|
2017-05-16 20:04:53 +00:00
|
|
|
'-s', '--srcdir', help='directory containing files to alias',
|
2017-01-24 19:38:02 +00:00
|
|
|
required=True, metavar='dir')
|
2017-05-16 20:04:53 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'-d', '--dstdir', help='directory to write aliases, default srcdir',
|
|
|
|
metavar='dir')
|
|
|
|
parser.add_argument(
|
|
|
|
'-a', '--aliasfile', help='alias file (default emoji_aliases.txt)',
|
|
|
|
metavar='file', default='emoji_aliases.txt')
|
2017-01-24 19:38:02 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'-p', '--prefix', help='file name prefix (default emoji_u)',
|
|
|
|
metavar='pfx', default='emoji_u')
|
|
|
|
parser.add_argument(
|
|
|
|
'-e', '--ext', help='file name extension (default png)',
|
2017-09-15 18:25:19 +00:00
|
|
|
choices=['ai', 'png', 'svg'], default='png')
|
2017-01-24 19:38:02 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'-r', '--replace', help='replace existing files/aliases',
|
|
|
|
action='store_true')
|
2017-05-16 20:04:53 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'-c', '--copy', help='create a copy of the file, not a symlink',
|
|
|
|
action='store_true')
|
2017-01-24 19:38:02 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'-n', '--dry_run', help='print out aliases to create only',
|
|
|
|
action='store_true')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2017-05-16 20:04:53 +00:00
|
|
|
add_aliases(
|
|
|
|
args.srcdir, args.dstdir, args.aliasfile, args.prefix, args.ext,
|
|
|
|
args.replace, args.copy, args.dry_run)
|
2017-01-24 19:38:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|