~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to scriptlib.py

  • Committer: abentley
  • Date: 2005-05-02 06:40:51 UTC
  • Revision ID: abentley@lappy-20050502064051-6ad6780f042823ef
librified most of the pull script

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import bzrlib
 
2
import os
2
3
import os.path
 
4
import sys
3
5
import tempfile
4
6
import shutil
5
7
 
37
39
    return True
38
40
 
39
41
def set_pull_data(br, location, rev_id):
40
 
    pull_file = file (br.controlfilename("pull-data"), "wb")
 
42
    pull_file = file (br.controlfilename("x-pull-data"), "wb")
41
43
    pull_file.write("%s\n%s\n" % (location, rev_id))
42
44
 
43
45
def get_pull_data(br):
50
52
    ('http://somewhere', '888-777')
51
53
    >>> rm_branch(br)
52
54
    """
53
 
    filename = br.controlfilename("pull-data")
 
55
    filename = br.controlfilename("x-pull-data")
54
56
    if not os.path.exists(filename):
55
57
        return (None, None)
56
58
    pull_file = file (filename, "rb")
57
59
    location, rev_id = [f.rstrip('\n') for f in pull_file]
58
60
    return location, rev_id
59
61
 
60
 
if __name__ == "__main__":
 
62
"""
 
63
>>> shell_escape('hello')
 
64
'\h\e\l\l\o'
 
65
"""
 
66
def shell_escape(arg):
 
67
    return "".join(['\\'+c for c in arg])
 
68
 
 
69
def safe_system(args):
 
70
    """
 
71
    >>> real_system = os.system
 
72
    >>> os.system = sys.stdout.write
 
73
    >>> safe_system(['a', 'b', 'cd'])
 
74
    \\a \\b \\c\\d
 
75
    >>> os.system = real_system
 
76
    """
 
77
    arg_str = " ".join([shell_escape(a) for a in args])
 
78
    return os.system(arg_str)
 
79
 
 
80
def rsync(source, target):
 
81
    """
 
82
    >>> real_system = os.system
 
83
    >>> os.system = sys.stdout.write
 
84
    >>> rsync("a", "b")
 
85
    \\r\\s\\y\\n\\c \\-\\a\\v \\-\\-\\d\\e\\l\\e\\t\\e \\a \\b
 
86
    >>> os.system = real_system
 
87
    """
 
88
    safe_system(("rsync", "-av", "--delete", source, target))
 
89
 
 
90
def pull(cur_branch, location=None):
 
91
    pull_location, pull_revision = get_pull_data(cur_branch)
 
92
    if pull_location is not None:
 
93
        if cur_branch.last_patch() != pull_revision:
 
94
            print "Aborting: This branch has had commits, so pull would lose data."
 
95
            sys.exit(1)
 
96
    if len(sys.argv) > 1:
 
97
        pull_location = sys.argv[1] 
 
98
        if not pull_location.endswith('/'):
 
99
            pull_location.append('/')
 
100
 
 
101
    if pull_location is None:
 
102
        print "No pull location saved.  Please specify one on the command line."
 
103
        sys.exit(1)
 
104
 
 
105
    if not is_clean(cur_branch):
 
106
        print "Error: This tree has uncommitted changes or unknown (?) files."
 
107
        sys.exit(1)
 
108
 
 
109
    print "Synchronizing with %s" % pull_location
 
110
    rsync (pull_location, cur_branch.base)
 
111
 
 
112
    set_pull_data(cur_branch, pull_location, cur_branch.last_patch())
 
113
 
 
114
def run_tests():
61
115
    import doctest
62
116
    bzrlib.trace.create_tracefile([])
63
117
    result = doctest.testmod()
64
 
    if result[0] == 0 and result[1] > 0:
65
 
        print "All tests passed"
 
118
    if result[1] > 0:
 
119
        if result[0] == 0:
 
120
            print "All tests passed"
 
121
    else:
 
122
        print "No tests to run"
 
123
if __name__ == "__main__":
 
124
    run_tests()