tests/generate-working-copy-states.py
author Martin von Zweigbergk <martinvonz@google.com>
Sat, 29 Nov 2014 14:39:33 -0800
changeset 23445 390a2610eaef
parent 23195 29977b315be1
child 23446 e51027c85dcd
permissions -rw-r--r--
generate-working-copy-states: make 'tracked' a string The only places the 'tracked' variable is only used is where it's converted to a string, so let's simplify by using the string value directly.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23195
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
     1
# generate proper file state to test working copy behavior
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
     2
import sys
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
     3
import os
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
     4
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
     5
# build the combination of possible states
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
     6
combination = []
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
     7
for base in [None, 'content1']:
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
     8
    for parent in set([None, 'content2']) | set([base]):
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
     9
        for wcc in set([None, 'content3']) | set([base, parent]):
23445
390a2610eaef generate-working-copy-states: make 'tracked' a string
Martin von Zweigbergk <martinvonz@google.com>
parents: 23195
diff changeset
    10
            for tracked in ('untracked', 'tracked'):
23195
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    11
                def statestring(content):
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    12
                    return content is None and 'missing' or content
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    13
                filename = "%s_%s_%s-%s" % (statestring(base),
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    14
                                            statestring(parent),
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    15
                                            statestring(wcc),
23445
390a2610eaef generate-working-copy-states: make 'tracked' a string
Martin von Zweigbergk <martinvonz@google.com>
parents: 23195
diff changeset
    16
                                            tracked)
23195
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    17
                combination.append((filename, base, parent, wcc))
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    18
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    19
# make sure we have stable output
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    20
combination.sort()
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    21
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    22
# retrieve the state we must generate
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    23
target = sys.argv[1]
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    24
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    25
# compute file content
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    26
content = []
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    27
for filename, base, parent, wcc in combination:
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    28
    if target == 'filelist':
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    29
        print filename
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    30
    elif target == 'base':
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    31
        content.append((filename, base))
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    32
    elif target == 'parent':
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    33
        content.append((filename, parent))
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    34
    elif target == 'wc':
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    35
        # Make sure there is content so the file gets written and can be
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    36
        # tracked. It will be deleted outside of this script.
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    37
        content.append((filename, wcc or 'TOBEDELETED'))
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    38
    else:
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    39
        print >> sys.stderr, "unknown target:", target
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    40
        sys.exit(1)
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    41
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    42
# write actual content
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    43
for filename, data in content:
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    44
    if data is not None:
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    45
        f = open(filename, 'w')
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    46
        f.write(data + '\n')
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    47
        f.close()
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    48
    elif os.path.exists(filename):
29977b315be1 test-revert: move embedded script to its own file
Martin von Zweigbergk <martinvonz@google.com>
parents:
diff changeset
    49
        os.remove(filename)