| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 | """Adds xref targets to the top of files."""import sysimport ostesting = FalseDONT_TOUCH = (        './index.txt',        )def target_name(fn):    if fn.endswith('.txt'):        fn = fn[:-4]    return '_' + fn.lstrip('./').replace('/', '-')def process_file(fn, lines):    lines.insert(0, '\n')    lines.insert(0, '.. %s:\n' % target_name(fn))    try:        f = open(fn, 'w')    except IOError:        print("Can't open %s for writing. Not touching it." % fn)        return    try:        f.writelines(lines)    except IOError:        print("Can't write to %s. Not touching it." % fn)    finally:        f.close()def has_target(fn):    try:        f = open(fn, 'r')    except IOError:        print("Can't open %s. Not touching it." % fn)        return (True, None)    readok = True    try:        lines = f.readlines()    except IOError:        print("Can't read %s. Not touching it." % fn)        readok = False    finally:        f.close()        if not readok:            return (True, None)    #print fn, len(lines)    if len(lines) < 1:        print("Not touching empty file %s." % fn)        return (True, None)    if lines[0].startswith('.. _'):        return (True, None)    return (False, lines)def main(argv=None):    if argv is None:        argv = sys.argv    if len(argv) == 1:        argv.extend('.')    files = []    for root in argv[1:]:        for (dirpath, dirnames, filenames) in os.walk(root):            files.extend([(dirpath, f) for f in filenames])    files.sort()    files = [os.path.join(p, fn) for p, fn in files if fn.endswith('.txt')]    #print files    for fn in files:        if fn in DONT_TOUCH:            print("Skipping blacklisted file %s." % fn)            continue        target_found, lines = has_target(fn)        if not target_found:            if testing:                print '%s: %s' % (fn, lines[0]),            else:                print "Adding xref to %s" % fn                process_file(fn, lines)        else:            print "Skipping %s: already has a xref" % fnif __name__ == '__main__':    sys.exit(main())
 |