~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to bzrtools.py

  • Committer: Aaron Bentley
  • Date: 2006-02-01 03:37:33 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20060201033733-df5441ce73162009
Got selftest passing with storage API

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
#    You should have received a copy of the GNU General Public License
15
15
#    along with this program; if not, write to the Free Software
16
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
import codecs
18
 
import errno
 
17
import bzrlib
 
18
import bzrlib.errors
19
19
import os
20
 
import re
 
20
import os.path
 
21
import sys
21
22
import tempfile
22
23
import shutil
 
24
import errno
23
25
from subprocess import Popen, PIPE
24
 
import sys
25
 
 
26
 
import bzrlib
27
 
import bzrlib.errors
28
 
from bzrlib.errors import BzrCommandError
29
 
from bzrlib.bzrdir import BzrDir
30
 
 
31
 
def temp_tree():
 
26
import codecs
 
27
import re
 
28
 
 
29
def temp_branch():
32
30
    dirname = tempfile.mkdtemp("temp-branch")
33
 
    return BzrDir.create_standalone_workingtree(dirname)
34
 
 
35
 
def rm_tree(tree):
36
 
    shutil.rmtree(tree.basedir)
37
 
 
38
 
def is_clean(cur_tree):
 
31
    return bzrlib.branch.Branch.initialize(dirname)
 
32
 
 
33
def rm_branch(br):
 
34
    shutil.rmtree(br.base)
 
35
 
 
36
def is_clean(cur_branch):
39
37
    """
40
38
    Return true if no files are modifed or unknown
41
39
    >>> import bzrlib.add
42
 
    >>> tree = temp_tree()
43
 
    >>> is_clean(tree)
 
40
    >>> br = temp_branch()
 
41
    >>> is_clean(br)
44
42
    (True, [])
45
 
    >>> fooname = os.path.join(tree.basedir, "foo")
 
43
    >>> fooname = os.path.join(br.base, "foo")
46
44
    >>> file(fooname, "wb").write("bar")
47
 
    >>> is_clean(tree)
 
45
    >>> is_clean(br)
48
46
    (True, [u'foo'])
49
 
    >>> bzrlib.add.smart_add_tree(tree, [tree.basedir])
 
47
    >>> bzrlib.add.smart_add_tree(br.working_tree(), [br.base])
50
48
    ([u'foo'], {})
51
 
    >>> is_clean(tree)
 
49
    >>> is_clean(br)
52
50
    (False, [])
53
 
    >>> tree.commit("added file")
54
 
    >>> is_clean(tree)
 
51
    >>> br.working_tree().commit("added file")
 
52
    >>> is_clean(br)
55
53
    (True, [])
56
 
    >>> rm_tree(tree)
 
54
    >>> rm_branch(br)
57
55
    """
58
56
    from bzrlib.diff import compare_trees
59
 
    old_tree = cur_tree.basis_tree()
60
 
    new_tree = cur_tree
 
57
    old_tree = cur_branch.basis_tree()
 
58
    new_tree = cur_branch.working_tree()
61
59
    non_source = []
62
60
    for path, file_class, kind, file_id, entry in new_tree.list_files():
63
61
        if file_class in ('?', 'I'):
65
63
    delta = compare_trees(old_tree, new_tree, want_unchanged=False)
66
64
    return not delta.has_changed(), non_source
67
65
 
68
 
def set_push_data(tree, location):
69
 
    push_file = file (tree._control_files.controlfilename("x-push-data"), "wb")
 
66
def set_push_data(br, location):
 
67
    push_file = file (br.control_files.controlfilename("x-push-data"), "wb")
70
68
    push_file.write("%s\n" % location)
71
69
 
72
 
def get_push_data(tree):
 
70
def get_push_data(br):
73
71
    """
74
 
    >>> tree = temp_tree()
75
 
    >>> get_push_data(tree) is None
 
72
    >>> br = temp_branch()
 
73
    >>> get_push_data(br) is None
76
74
    True
77
 
    >>> set_push_data(tree, 'http://somewhere')
78
 
    >>> get_push_data(tree)
 
75
    >>> set_push_data(br, 'http://somewhere')
 
76
    >>> get_push_data(br)
79
77
    'http://somewhere'
80
 
    >>> rm_tree(tree)
 
78
    >>> rm_branch(br)
81
79
    """
82
 
    filename = tree._control_files.controlfilename("x-push-data")
 
80
    filename = br.control_files.controlfilename("x-push-data")
83
81
    if not os.path.exists(filename):
84
82
        return None
85
83
    push_file = file (filename, "rb")
115
113
def rsync(source, target, ssh=False, excludes=(), silent=False, 
116
114
          rsync_name="rsync"):
117
115
    """
118
 
    >>> new_dir = tempfile.mkdtemp()
119
 
    >>> old_dir = os.getcwd()
120
 
    >>> os.chdir(new_dir)
121
116
    >>> rsync("a", "b", silent=True)
122
117
    Traceback (most recent call last):
123
 
    RsyncNoFile: No such file...
124
 
    >>> rsync(new_dir + "/a", new_dir + "/b", excludes=("*.py",), silent=True)
 
118
    RsyncNoFile: No such file a
 
119
    >>> rsync("a", "b", excludes=("*.py",), silent=True)
125
120
    Traceback (most recent call last):
126
 
    RsyncNoFile: No such file...
127
 
    >>> rsync(new_dir + "/a", new_dir + "/b", excludes=("*.py",), silent=True, rsync_name="rsyncc")
 
121
    RsyncNoFile: No such file a
 
122
    >>> rsync("a", "b", excludes=("*.py",), silent=True, rsync_name="rsyncc")
128
123
    Traceback (most recent call last):
129
124
    NoRsync: rsyncc not found.
130
 
    >>> os.chdir(old_dir)
131
 
    >>> os.rmdir(new_dir)
132
125
    """
133
126
    cmd = [rsync_name, "-av", "--delete"]
134
127
    if ssh:
191
184
 
192
185
exclusions = ('.bzr/x-push-data', '.bzr/parent', '.bzr/x-pull-data', 
193
186
              '.bzr/x-pull', '.bzr/pull', '.bzr/stat-cache',
194
 
              '.bzr/x-rsync-data', '.bzr/basis-inventory', 
195
 
              '.bzr/inventory.backup.weave')
 
187
              '.bzr/x-rsync-data')
196
188
 
197
189
 
198
190
def read_revision_history(fname):
235
227
    except RsyncNoFile:
236
228
        return True
237
229
 
238
 
def push(tree, location=None, overwrite=False, working_tree=True):
239
 
    push_location = get_push_data(tree)
 
230
def push(cur_branch, location=None, overwrite=False, working_tree=True):
 
231
    push_location = get_push_data(cur_branch)
240
232
    if location is not None:
241
233
        if not location.endswith('/'):
242
234
            location += '/'
243
235
        push_location = location
244
236
    
245
237
    if push_location is None:
246
 
        if tree.branch.get_push_location() is None:
247
 
            raise BzrCommandError("No push location known or specified.")
248
 
        else:
249
 
            raise bzrlib.errors.MustUseDecorated
 
238
        raise bzrlib.errors.MustUseDecorated
250
239
 
251
240
    if push_location.find('://') != -1:
252
241
        raise bzrlib.errors.MustUseDecorated
254
243
    if push_location.find(':') == -1:
255
244
        raise bzrlib.errors.MustUseDecorated
256
245
 
257
 
    clean, non_source = is_clean(tree)
 
246
    clean, non_source = is_clean(cur_branch)
258
247
    if not clean:
259
248
        print """Error: This tree has uncommitted changes or unknown (?) files.
260
249
Use "bzr status" to list them."""
262
251
    if working_tree:
263
252
        final_exclusions = non_source[:]
264
253
    else:
265
 
        wt = tree
 
254
        wt = cur_branch.working_tree()
266
255
        final_exclusions = []
267
256
        for path, status, kind, file_id, entry in wt.list_files():
268
257
            final_exclusions.append(path)
270
259
    final_exclusions.extend(exclusions)
271
260
    if not overwrite:
272
261
        try:
273
 
            if not history_subset(push_location, tree.branch):
 
262
            if not history_subset(push_location, cur_branch):
274
263
                raise bzrlib.errors.BzrCommandError("Local branch is not a"
275
264
                                                    " newer version of remote"
276
265
                                                    " branch.")
284
273
                " specified location.  Please ensure that"
285
274
                ' "%s" is of the form "machine:/path".' % push_location)
286
275
    print "Pushing to %s" % push_location
287
 
    rsync(tree.basedir+'/', push_location, ssh=True, 
 
276
    rsync(cur_branch.base+'/', push_location, ssh=True, 
288
277
          excludes=final_exclusions)
289
278
 
290
 
    set_push_data(tree, push_location)
291
 
 
 
279
    set_push_data(cur_branch, push_location)
292
280
 
293
281
def short_committer(committer):
294
282
    new_committer = re.sub('<.*>', '', committer).strip(' ')