def update_samples( sample_dir, udhr_dir, bcp_to_code_attrib_sample, in_repo, no_stage): """Create samples in sample_dir based on the bcp to c_a_s map. Stage if sample_dir is in the repo. If sample_dir is in the repo, don't overwrite samples whose most recent log entry does not start with 'Updated by tool'.""" tool_utils.check_dir_exists(udhr_dir) if (in_repo and not no_stage and os.path.isdir(sample_dir) and not tool_utils.git_is_clean(sample_dir)): raise ValueError('Please clean %s.' % sample_dir) if in_repo: repo, subdir = os.path.split(sample_dir) tool_samples = frozenset(tool_utils.get_tool_generated(repo, subdir)) print 'allowing overwrite of %d files:\n %s' % ( len(tool_samples), ', '.join(sorted(tool_samples))) comments = [ '# Attributions for sample excerpts:', '# original - in the public domain, no attribution', '# UN - UN, OHCHR, or affiliate, attribute to UN', '# other - not a UN translation', '# none - not on ohchr, not a UN translation' ] sample_attrib_list = [] sample_dir = tool_utils.ensure_dir_exists(sample_dir) count = 0 for bcp, (code, attrib, sample) in bcp_to_code_attrib_sample.iteritems(): dst_file = '%s_udhr.txt' % bcp dst_path = os.path.join(sample_dir, dst_file) if in_repo and os.path.isfile(dst_path) and dst_file not in tool_samples: print 'Not overwriting modified file %s' % dst_file else: with codecs.open(dst_path, 'w', 'utf8') as f: f.write(sample) count += 1 sample_attrib_list.append('%s: %s' % (dst_file, attrib)) print 'Created %d samples' % count # Some existing samples that we don't overwrite are not in # bcp_to_code_attrib_sample, so they're not listed. Readers of the # attributions.txt file will need to default these to 'none'. attrib_data = '\n'.join(comments + sorted(sample_attrib_list)) + '\n' with open(os.path.join(sample_dir, 'attributions.txt'), 'w') as f: f.write(attrib_data) if in_repo and not no_stage: tool_utils.git_add_all(sample_dir) date = datetime.datetime.now().strftime('%Y-%m-%d') dst = 'in %s ' % sample_dir if not in_repo else '' noto_ix = udhr_dir.find('nototools') src = udhr_dir if noto_ix == -1 else udhr_dir[noto_ix:] # prefix of this sample commit message indicates that these were # tool-generated print 'Updated by tool - sample files %sfrom %s as of %s.' % (dst, src, date)
def _copy_files(src, dst): """Copies files named 'emoji_u*.png' from dst to src, and return a set of the names with 'emoji_u' and the extension stripped.""" code_strings = set() tool_utils.check_dir_exists(src) dst = tool_utils.ensure_dir_exists(dst, clean=True) for f in glob.glob(path.join(src, 'emoji_u*.png')): shutil.copy(f, dst) code_strings.add(path.splitext(path.basename(f))[0][7:]) return code_strings
def update_cldr(noto_repo, cldr_repo, update=False, cldr_tag=""): """Copy needed directories/files from cldr_repo to noto_repo/third_party/cldr.""" noto_repo = os.path.abspath(noto_repo) cldr_repo = os.path.abspath(cldr_repo) noto_cldr = os.path.join(noto_repo, "third_party/cldr") tool_utils.check_dir_exists(noto_cldr) tool_utils.check_dir_exists(cldr_repo) if not tool_utils.git_is_clean(noto_repo): print("git in %s is not clean: please fix" % noto_repo) return if update: tool_utils.svn_update(cldr_repo) # get version of cldr. Unfortunately, this doesn't know about tags. cldr_version = tool_utils.svn_get_version(cldr_repo) # prepare and create README.third_party readme_text = string.Template(README_TEMPLATE).substitute( version=cldr_version, tag=cldr_tag) with open(os.path.join(noto_cldr, "README.third_party"), "w") as f: f.write(readme_text) # remove/replace directories for subdir in CLDR_SUBDIRS: src = os.path.join(cldr_repo, subdir) dst = os.path.join(noto_cldr, subdir) print("replacing directory %s..." % subdir) print("...by copying from %s to %s" % (src, dst)) if os.path.exists(dst): shutil.rmtree(dst) shutil.copytree(src, dst) # replace files for f in CLDR_FILES: print("replacing file %s..." % f) src = os.path.join(cldr_repo, f) dst = os.path.join(noto_cldr, f) shutil.copy(src, dst) # stage changes in cldr dir tool_utils.git_add_all(noto_cldr) # print commit message tag_string = (" tag %s" % cldr_tag) if cldr_tag else "" print("Update CLDR data to SVN r%s%s." % (cldr_version, tag_string))
def update_samples(sample_dir, udhr_dir, bcp_to_code_attrib_sample, in_repo, no_stage): """Create samples in sample_dir based on the bcp to c_a_s map. Stage if sample_dir is in the repo. If sample_dir is in the repo, don't overwrite samples whose most recent log entry does not start with 'Updated by tool'.""" tool_utils.check_dir_exists(udhr_dir) if ( in_repo and not no_stage and os.path.isdir(sample_dir) and not tool_utils.git_is_clean(sample_dir) ): raise ValueError("Please clean %s." % sample_dir) if in_repo: repo, subdir = os.path.split(sample_dir) tool_samples = frozenset(tool_utils.get_tool_generated(repo, subdir)) print( "allowing overwrite of %d files:\n %s" % (len(tool_samples), ", ".join(sorted(tool_samples))) ) comments = [ "# Attributions for sample excerpts:", "# original - in the public domain, no attribution", "# UN - UN, OHCHR, or affiliate, attribute to UN", "# other - not a UN translation", "# none - not on ohchr, not a UN translation", ] sample_attrib_list = [] sample_dir = tool_utils.ensure_dir_exists(sample_dir) count = 0 for bcp, (code, attrib, sample) in bcp_to_code_attrib_sample.items(): dst_file = "%s_udhr.txt" % bcp dst_path = os.path.join(sample_dir, dst_file) if in_repo and os.path.isfile(dst_path) and dst_file not in tool_samples: print("Not overwriting modified file %s" % dst_file) else: with codecs.open(dst_path, "w", "utf8") as f: f.write(sample) count += 1 sample_attrib_list.append("%s: %s" % (dst_file, attrib)) print("Created %d samples" % count) # Some existing samples that we don't overwrite are not in # bcp_to_code_attrib_sample, so they're not listed. Readers of the # attributions.txt file will need to default these to 'none'. attrib_data = "\n".join(comments + sorted(sample_attrib_list)) + "\n" with open(os.path.join(sample_dir, "attributions.txt"), "w") as f: f.write(attrib_data) if in_repo and not no_stage: tool_utils.git_add_all(sample_dir) date = datetime.datetime.now().strftime("%Y-%m-%d") dst = "in %s " % sample_dir if not in_repo else "" noto_ix = udhr_dir.find("nototools") src = udhr_dir if noto_ix == -1 else udhr_dir[noto_ix:] # prefix of this sample commit message indicates that these were # tool-generated print("Updated by tool - sample files %sfrom %s as of %s." % (dst, src, date))