~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to bzrtools.py

  • Committer: Aaron Bentley
  • Date: 2006-03-14 18:10:04 UTC
  • mto: This revision was merged to the branch mainline in revision 329.
  • Revision ID: abentley@panoramicfeedback.com-20060314181004-ea3edbc59ddc8ae3
Handle aliases in bzr shell

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
import bzrlib
27
27
import bzrlib.errors
28
 
from bzrlib.errors import (BzrCommandError, NotBranchError, NoSuchFile,
29
 
                           UnsupportedFormatError, TransportError, 
30
 
                           NoWorkingTree, PermissionDenied)
31
 
from bzrlib.bzrdir import BzrDir, BzrDirFormat
 
28
from bzrlib.errors import BzrCommandError
 
29
from bzrlib.bzrdir import BzrDir
32
30
 
33
31
def temp_tree():
34
32
    dirname = tempfile.mkdtemp("temp-branch")
52
50
    ([u'foo'], {})
53
51
    >>> is_clean(tree)
54
52
    (False, [])
55
 
    >>> tree.commit("added file", rev_id='commit-id')
56
 
    'commit-id'
 
53
    >>> tree.commit("added file")
57
54
    >>> is_clean(tree)
58
55
    (True, [])
59
56
    >>> rm_tree(tree)
69
66
    return not delta.has_changed(), non_source
70
67
 
71
68
def set_push_data(tree, location):
72
 
    tree.branch.control_files.put_utf8("x-push-data", "%s\n" % location)
 
69
    push_file = file (tree._control_files.controlfilename("x-push-data"), "wb")
 
70
    push_file.write("%s\n" % location)
73
71
 
74
72
def get_push_data(tree):
75
73
    """
78
76
    True
79
77
    >>> set_push_data(tree, 'http://somewhere')
80
78
    >>> get_push_data(tree)
81
 
    u'http://somewhere'
 
79
    'http://somewhere'
82
80
    >>> rm_tree(tree)
83
81
    """
84
 
    try:
85
 
        location = tree.branch.control_files.get_utf8('x-push-data').read()
86
 
    except NoSuchFile:
 
82
    filename = tree._control_files.controlfilename("x-push-data")
 
83
    if not os.path.exists(filename):
87
84
        return None
88
 
    return location.rstrip('\n')
 
85
    push_file = file (filename, "rb")
 
86
    (location,) = [f.rstrip('\n') for f in push_file]
 
87
    return location
89
88
 
90
89
"""
91
90
>>> shell_escape('hello')
190
189
        raise RsyncUnknownStatus(proc.returncode)
191
190
    return [l.split(' ')[-1].rstrip('\n') for l in result.splitlines(True)]
192
191
 
193
 
exclusions = ('.bzr/x-push-data', '.bzr/branch/x-push/data', '.bzr/parent', 
194
 
              '.bzr/branch/parent', '.bzr/x-pull-data', '.bzr/x-pull',
195
 
              '.bzr/pull', '.bzr/stat-cache', '.bzr/x-rsync-data',
196
 
              '.bzr/basis-inventory', '.bzr/inventory.backup.weave')
 
192
exclusions = ('.bzr/x-push-data', '.bzr/parent', '.bzr/x-pull-data', 
 
193
              '.bzr/x-pull', '.bzr/pull', '.bzr/stat-cache',
 
194
              '.bzr/x-rsync-data', '.bzr/basis-inventory', 
 
195
              '.bzr/inventory.backup.weave')
197
196
 
198
197
 
199
198
def read_revision_history(fname):
212
211
    tempdir = tempfile.mkdtemp('push')
213
212
    try:
214
213
        history_fname = os.path.join(tempdir, 'revision-history')
215
 
        try:
216
 
            cmd = rsync(location+'.bzr/revision-history', history_fname,
217
 
                        silent=True)
218
 
        except RsyncNoFile:
219
 
            cmd = rsync(location+'.bzr/branch/revision-history', history_fname,
220
 
                        silent=True)
 
214
        cmd = rsync(location+'.bzr/revision-history', history_fname,
 
215
                    silent=True)
221
216
        history = read_revision_history(history_fname)
222
217
    finally:
223
218
        shutil.rmtree(tempdir)
240
235
    except RsyncNoFile:
241
236
        return True
242
237
 
243
 
def rspush(tree, location=None, overwrite=False, working_tree=True):
 
238
def push(tree, location=None, overwrite=False, working_tree=True):
244
239
    push_location = get_push_data(tree)
245
240
    if location is not None:
246
241
        if not location.endswith('/'):
248
243
        push_location = location
249
244
    
250
245
    if push_location is None:
251
 
        raise BzrCommandError("No rspush location known or specified.")
252
 
 
253
 
    if (push_location.find('://') != -1 or
254
 
        push_location.find(':') == -1):
255
 
        raise BzrCommandError("Invalid rsync path %r." % push_location)
256
 
 
 
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
 
250
 
 
251
    if push_location.find('://') != -1:
 
252
        raise bzrlib.errors.MustUseDecorated
 
253
 
 
254
    if push_location.find(':') == -1:
 
255
        raise bzrlib.errors.MustUseDecorated
 
256
 
 
257
    clean, non_source = is_clean(tree)
 
258
    if not clean:
 
259
        print """Error: This tree has uncommitted changes or unknown (?) files.
 
260
Use "bzr status" to list them."""
 
261
        sys.exit(1)
257
262
    if working_tree:
258
 
        clean, non_source = is_clean(tree)
259
 
        if not clean:
260
 
            print """Error: This tree has uncommitted changes or unknown (?) files.
261
 
    Use "bzr status" to list them."""
262
 
            sys.exit(1)
263
263
        final_exclusions = non_source[:]
264
264
    else:
265
265
        wt = tree
297
297
    return new_committer
298
298
 
299
299
 
300
 
def apache_ls(t):
301
 
    """Screen-scrape Apache listings"""
302
 
    apache_dir = '<img border="0" src="/icons/folder.gif" alt="[dir]">'\
303
 
        ' <a href="'
304
 
    lines = t.get('.')
305
 
    expr = re.compile('<a[^>]*href="([^>]*)"[^>]*>', flags=re.I)
306
 
    for line in lines:
307
 
        match = expr.search(line)
308
 
        if match is None:
309
 
            continue
310
 
        url = match.group(1)
311
 
        if url.startswith('http://') or url.startswith('/') or '../' in url:
312
 
            continue
313
 
        if '?' in url:
314
 
            continue
315
 
        yield url.rstrip('/')
316
 
 
317
 
 
318
 
def iter_branches(t, lister=None):
319
 
    """Iterate through all the branches under a transport"""
320
 
    for bzrdir in iter_bzrdirs(t, lister):
321
 
        try:
322
 
            branch = bzrdir.open_branch()
323
 
            if branch.bzrdir is bzrdir:
324
 
                yield branch
325
 
        except (NotBranchError, UnsupportedFormatError):
326
 
            pass
327
 
 
328
 
 
329
 
def iter_branch_tree(t, lister=None):
330
 
    for bzrdir in iter_bzrdirs(t, lister):
331
 
        try:
332
 
            wt = bzrdir.open_workingtree()
333
 
            yield wt.branch, wt
334
 
        except NoWorkingTree, UnsupportedFormatError:
335
 
            try:
336
 
                branch = bzrdir.open_branch()
337
 
                if branch.bzrdir is bzrdir:
338
 
                    yield branch, None
339
 
            except (NotBranchError, UnsupportedFormatError):
340
 
                continue
341
 
 
342
 
 
343
 
def iter_bzrdirs(t, lister=None):
344
 
    if lister is None:
345
 
        def lister(t):
346
 
            return t.list_dir('.')
347
 
    try:
348
 
        bzrdir = bzrdir_from_transport(t)
349
 
        yield bzrdir
350
 
    except (NotBranchError, UnsupportedFormatError, TransportError,
351
 
            PermissionDenied):
352
 
        pass
353
 
    try:
354
 
        for directory in lister(t):
355
 
            if directory == ".bzr":
356
 
                continue
357
 
            try:
358
 
                subt = t.clone(directory)
359
 
            except UnicodeDecodeError:
360
 
                continue
361
 
            for bzrdir in iter_bzrdirs(subt, lister):
362
 
                yield bzrdir
363
 
    except (NoSuchFile, PermissionDenied, TransportError):
364
 
        pass
365
 
 
366
 
    
367
 
def bzrdir_from_transport(t):
368
 
    """Open a bzrdir from a transport (not a location)"""
369
 
    format = BzrDirFormat.find_format(t)
370
 
    BzrDir._check_supported(format, False)
371
 
    return format.open(t)
372
 
 
373
 
 
374
300
def run_tests():
375
301
    import doctest
376
302
    result = doctest.testmod()