diff --git a/tools/release/c/bomfix.py b/tools/release/c/bomfix.py index c6cd79a0b78..018d4eba20e 100644 --- a/tools/release/c/bomfix.py +++ b/tools/release/c/bomfix.py @@ -1,5 +1,8 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright © 2016 and later: Unicode, Inc. and others. +# License & terms of use: http://www.unicode.org/copyright.html # Copyright (C) 2011 IBM Corporation and Others. All Rights Reserved. # # Run this like so: @@ -9,13 +12,12 @@ # it will fixup any files that have a mime-type of "utf-8" but no BOM. import os -import svn.core, svn.client, svn.wc import codecs - +import subprocess print "Fixing bom in .\n" -ctx = svn.client.svn_client_ctx_t() +ctx = None tree = os.walk(".") @@ -28,61 +30,78 @@ tfiles=0 bom=codecs.BOM_UTF8 +# my own rewrite +def my_propget(prop, path, ignored_rev, ignored_recurs, ignored_ctx): + "function_docstring" + try: + return subprocess.check_output(["svn", "pg", prop, path], stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as cpe: + # now decode the error + if "E200005" in cpe.output: + # not under version control + return None + if "W200017" in cpe.output: + # property not found + return None + else: + print "On " + fp + ":\n" + cpe.output + "\n" + print "This error wasn't recognized by bomfix, sorry." + raise cpe + for ent in tree: (path,dirs,files) = ent if(path.find("/.svn") != -1): continue for file in files: tfiles=tfiles+1 - revision = svn.core.svn_opt_revision_t() - fp = os.path.abspath(path + "/" + file) + revision = None + # use relative path + fp = path + "/" + file #print "testing " + fp - try: - props = svn.client.propget("svn:mime-type", fp, revision, 0, ctx) - if (not props or not props[fp]): - noprops = noprops + 1 - continue + props = my_propget("svn:mime-type", fp, revision, 0, ctx) + if not props: + noprops = noprops + 1 + continue + + type = props + + # ends with \n because of process + if (not type == "text/plain;charset=utf-8\n"): + #print fp + ": delta " + type + notutf8 = notutf8 + 1 + continue + + # fp is utf-8 + utf8=utf8+1 + + f = open(fp, 'rb') + bytes=f.read(3) + if not bytes: + print fp + ": could not read 3 bytes" + continue + elif (bytes == bom): + #print fp + ": OK" + continue + + f.seek(0) + + os.rename(fp,fp+".tmp") + o=open(fp,'wb') + o.write(bom) + while(1): + bytes = f.read(2048) + if bytes: + o.write(bytes) + else: + break + o.close() + f.close() + os.remove(fp+".tmp") + fixed=fixed+1 + + + print fp - type = props[fp] - - if (not type == "text/plain;charset=utf-8"): - notutf8 = notutf8 + 1 - continue - - # fp is utf-8 - utf8=utf8+1 - - f = open(fp, 'rb') - bytes=f.read(3) - if not bytes: - print fp + ": could not read 3 bytes" - continue - elif (bytes == bom): - #print fp + ": OK" - continue - - f.seek(0) - - os.rename(fp,fp+".tmp") - o=open(fp,'wb') - o.write(bom) - while(1): - bytes = f.read(2048) - if bytes: - o.write(bytes) - else: - break - o.close() - f.close() - os.remove(fp+".tmp") - fixed=fixed+1 - - - print fp - - except svn.core.SubversionException: - nots = nots + 1 - #print "Not under vc: " + fp print "%d files, %d not under svn, %d with no props, %d not utf8: %d utf8, %d fixed\n" % (tfiles,nots,noprops,notutf8,utf8,fixed)