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