#!/usr/bin/env python3 # Runs under both Python 2 and Python 3: preserve this property! # SPDX-License-Identifier: GPL-2.0+ """ cvsstrip - skeletonize CVS master files Called as a filter, skeletonizes a CVS master presented on standard input and write it to standard output. If an argument is specified, it must be the name of a directory containing CVS master files; in that case a corresponding directory of stripped files is created. Options: -o dir Set name of output directory. Defaults to the input dirname with the suffix '-reduced'. -t Suppress stripping of (non-sticky) tags. Sticky tags are always preserved. -l Suppress replacement of log content with a hash. -c Suppress replacement of revision content. -v Enable progress messages. Default behavior is to strip non-sticky tags, replace each version of content with a unique string including the revision ID, and replace log text with its MD5 hash in hex. The only identifying information left in the tree is filenames and CVS user IDs. The intent is to discard bulky content but preserve all metadata relevant to changeset collation. A collection of stripped files should imply the same changeset DAG as the unstripped originals, but be easier to pass around, faster to process, and not reveal potentially sensitive data. """ import os, sys, getopt, hashlib, io, shutil strip_tags = True strip_logs = True strip_content = True verbose = 0 # Any encoding that preserves 0x80...0x8f through round-tripping from byte # streams to Unicode and back would do, latin-1 is the best known of these. binary_encoding = 'latin-1' if str is bytes: # Python 2 polystr = str polybytes = bytes polyord = ord polychr = str else: # Python 3 def polystr(o): if isinstance(o, str): return o if isinstance(o, bytes): return str(o, encoding=binary_encoding) raise ValueError def polybytes(o): if isinstance(o, bytes): return o if isinstance(o, str): return bytes(o, encoding=binary_encoding) raise ValueError def polyord(c): "Polymorphic ord() function" if isinstance(c, str): return ord(c) return c def polychr(c): "Polymorphic chr() function" if isinstance(c, int): return chr(c) return c def make_std_wrapper(stream): "Standard input/output wrapper factory function" # This ensures that the encoding of standard output and standard # error on Python 3 matches the binary encoding we use to turn # bytes to Unicode in polystr above # newline="\n" ensures that Python 3 won't mangle line breaks # line_buffering=True ensures that interactive command sessions work as expected return io.TextIOWrapper(stream.buffer, encoding=binary_encoding, newline="\n", line_buffering=True) sys.stdin = make_std_wrapper(sys.stdin) sys.stdout = make_std_wrapper(sys.stdout) sys.stderr = make_std_wrapper(sys.stderr) def replace_escaped_text(inputf, replacement, outputf): "Replace text between @ delimiters with a specified string." leader = polystr(inputf.read(1)) if leader != '@': sys.stderr.write("cvsstrip: fatal error, @ leader not where expected.\n") sys.exit(1) else: outputf.write('@' + replacement.replace("@", r'@@')) while True: nxt = inputf.read(1) if nxt == '@': nxt2 = inputf.read(1) if nxt2 == '@': continue else: break if nxt2 == '\n': outputf.write("@\n") else: sys.stderr.write("cvsstrip: fatal error, @ trailer not followed by newline (%s).\n" % nxt2) sys.exit(1) def hash_escaped_text(inputf, outputf): "Replace text between @ delimiters with its MD5 hash." leader = polystr(inputf.read(1)) if leader != '@': sys.stderr.write("cvsstrip: fatal error, @ leader not where expected.\n") sys.exit(1) txt = "" while True: nxt = polystr(inputf.read(1)) if nxt == '@': nxt2 = inputf.read(1) if nxt2 == '@': txt += "@" continue else: break txt += nxt if nxt2 == '\n': m = hashlib.md5() m.update(polybytes(txt)) outputf.write("@%s\n@\n" % m.hexdigest()) else: sys.stderr.write("cvsstrip: fatal error, @ trailer not followed by newline (%s).\n" % nxt2) sys.exit(1) def skeletonize(inputf, outputf): "Skeletonize a CVS master, discarding content but leaving metadata." state = "ini" last_version = None deltacount = 0 lineno = 0 while True: lineno += 1 line = polystr(inputf.readline()) if not line: break if verbose > 1: sys.stderr.write(b"%s: %s\n" % (state, line.strip())) if state == 'ini': if line.startswith("symbols"): state = "sym" elif line[0].isdigit(): last_version = line.strip() elif line.startswith("log"): if strip_logs: outputf.write(polystr(line)) hash_escaped_text(inputf, outputf) continue elif line.startswith("text"): if strip_content: outputf.write(polystr(line)) txt = "%s content for %s\n" % (inputf.name, last_version) if deltacount > 0: txt = "d1 1\na1 1\n" + txt deltacount += 1 replace_escaped_text(inputf, txt, outputf) continue elif state == "sym": if not line[0] in (' ', '\t') or line.strip() == ';': state = "ini" elif strip_tags and '0' not in line.split(":")[1]: if line.endswith(";\n"): outputf.write("\t;\n") continue outputf.write(polystr(line)) if __name__ == '__main__': (opts, arguments) = getopt.getopt(sys.argv[1:], "ctlo:v") outdir = None for (opt, arg) in opts: if opt == '-t': strip_tags = False elif opt == '-l': strip_logs = False elif opt == '-c': strip_content = False elif opt == '-o': outdir = arg elif opt == '-v': verbose += 1 if not arguments: skeletonize(sys.stdin, sys.stdout) sys.exit(0) elif not os.path.isdir(arguments[0]): sys.stderr.write("cvsstrip: argument must be a directory.\n") sys.exit(1) originals = arguments[0] if not outdir: outdir = originals + "-reduced" if os.path.exists(outdir): sys.stderr.write("cvsstrip: refusing to step on %s.\n" % outdir) sys.exit(1) # Directory traversal for dirName, subdirList, fileList in os.walk(originals): path_parts = list(dirName.split(os.sep)) path_parts.pop(0) newparts = [outdir] + path_parts for i in range(len(newparts)): newdir = os.path.join(*newparts[:i+1]) if not os.path.exists(newdir): if verbose: print("Directory creation: %s" % newdir) os.mkdir(newdir) for fname in fileList: oldname = os.path.join(dirName, fname) newpath = newparts + [fname] newname = os.path.join(*newpath) if verbose > 0: print('%s -> %s' % (oldname, newname)) if oldname.endswith(',v'): old = open(oldname, "rb") new = open(newname, "wb") skeletonize(old, new) old.close() new.close() else: sys.stderr.write("cvsstrip: %s isn't a CVS master.\n" % oldname) shutil.copyfile(oldname, newname) # end