hgbook-ru / fr / fblinks

#!/usr/bin/python

import errno
import os
import re
import sys

hg_id = sys.argv[1]

dest_dir = sys.argv[2]

empty_re = re.compile(r'^\s*$')
line_re = re.compile(r'^(\w+)(.*)')

try:
    os.makedirs(dest_dir)
except OSError, err:
    if err.errno != errno.EEXIST:
        raise

def feedback(name, text, ctx_id):
    return r'\marginpar{\scriptsize \href{http://demesne:8000/book/feedback/submit/%s/%s/%d/}{Feedback}}' % (hg_id, name, ctx_id)

ctxs = {}
try:
    cfp = open(os.path.join(dest_dir, 'rev-' + hg_id + '.ctx'), 'r+')
    for line in cfp:
        f, l, c = line.split(':', 2)
        ctxs[(f, int(l))] = c.strip()
except IOError, err:
    if err.errno != errno.ENOENT: raise
    cfp = open(os.path.join(dest_dir, 'rev-' + hg_id + '.ctx'), 'w+')

changes = 0

for name in sys.argv[3:]:
    if not name.endswith('.tex'):
        continue
    dest_name = os.path.join(dest_dir, name)
    ifp = open(name)
    ofp = open(dest_name, 'w')
    new_par = True
    line_num = 0
    par_num = 0
    for line in ifp:
        line_num += 1
        if new_par:
            m = line_re.match(line)
            if m:
                par_num += 1
                ls = line.strip()
                if ctxs.get((name, par_num)) != ls:
                    ctxs[(name, par_num)] = ls
                    changes += 1
                line = m.group(1) + feedback(name, line, par_num) + m.group(2)
                new_par = False
        elif not line.strip():
            new_par = True
        ofp.write(line)

if changes:
    cfp.seek(0)
    print '%s: %d changes' % (cfp.name, changes)
    ctxs = ctxs.items()
    ctxs.sort()
    for ((file, line), content) in ctxs:
        cfp.write('%s:%d: %s\n' % (file, line, content))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.