Skip to content

Instantly share code, notes, and snippets.

@jun66j5
Created November 25, 2021 23:37
Show Gist options
  • Save jun66j5/e6d2ababd27ac05f4ddaf959dcdea0ac to your computer and use it in GitHub Desktop.
Save jun66j5/e6d2ababd27ac05f4ddaf959dcdea0ac to your computer and use it in GitHub Desktop.
Replace private data in RCS file
#! /usr/bin/python
import os
import re
import sys
_rcsdiff_re = re.compile(r"""
\n
([^\n]+)\n
log\n
@(?:[^@]+|@@)*@\n
text\n
@((?:[^@]+|@@)*)@\n
""".encode('ascii'), re.VERBOSE)
_line_re = re.compile(b'[^\n]*(?:\n|[^\n]\Z)')
_diff_re = re.compile(b'[ad][0-9]+ ([0-9]+)')
_author_re = re.compile(b';\tauthor [^;]+')
def _replace_rcsfile(f):
state = {'first': True, 'count': 0}
def content_repl(tag, count, match):
line = b'<%s/%d/%d>' % count
if match.endswith(b'\n'):
line += b'\n'
return line
def body_repl(match):
tag = match.group(1)
log = b'(Log %s)\n' % tag
text = match.group(2)
if state['first']:
state['first'] = False
state['count'] += 1
count = state['count']
text = b''.join(b'<%s/%d/%d>' % (tag, count, idx) +
(b'\n' if m.group(0).endswith(b'\n') else b'')
for idx, m in enumerate(_line_re.finditer(text)))
else:
lines = [m.group(0) for m in _line_re.finditer(text)]
new_lines = []
iter_lines = iter(lines)
readline = lambda: next(iter_lines)
while 1:
try:
line = readline()
except StopIteration:
line = None
break
m = _diff_re.match(line)
assert m, repr(line)
new_lines.append(line)
if line.startswith(b'd'):
continue
state['count'] += 1
count = state['count']
n = int(m.group(1))
while n > 0:
line = readline()
new_line = b'<%s/%d/%d>' % (tag, count, n)
if line.endswith(b'\n'):
new_line += b'\n'
new_lines.append(new_line)
n -= 1
text = b''.join(new_lines)
return b'\n%s\nlog\n@%s@\ntext\n@%s@\n' % (tag, log, text)
def author_repl(match):
return b';\tauthor anonymous'
content = f.read()
header, body = content.split(b'\ndesc\n', 1)
header = re.sub(b'\nsymbols[^;]*', b'\nsymbols', header, re.M)
header = _author_re.sub(author_repl, header)
os.write(1, header)
os.write(1, b'\ndesc\n')
os.write(1, _rcsdiff_re.sub(body_repl, body))
def main(args):
for arg in args:
with open(arg, 'rb') as f:
_replace_rcsfile(f)
if __name__ == '__main__':
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment