| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 
 | #!/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
 |