~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to bzrtools.py

  • Committer: Aaron Bentley
  • Date: 2006-03-24 19:01:30 UTC
  • Revision ID: abentley@panoramicfeedback.com-20060324190130-2208c693486a8b33
Added apache index scraping to the branches command

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import bzrlib.errors
28
28
from bzrlib.errors import (BzrCommandError, NotBranchError, NoSuchFile,
29
29
                           UnsupportedFormatError, TransportError, 
30
 
                           NoWorkingTree, PermissionDenied)
 
30
                           NoWorkingTree)
31
31
from bzrlib.bzrdir import BzrDir, BzrDirFormat
32
32
 
33
33
def temp_tree():
52
52
    ([u'foo'], {})
53
53
    >>> is_clean(tree)
54
54
    (False, [])
55
 
    >>> tree.commit("added file", rev_id='commit-id')
56
 
    'commit-id'
 
55
    >>> tree.commit("added file")
57
56
    >>> is_clean(tree)
58
57
    (True, [])
59
58
    >>> rm_tree(tree)
60
59
    """
 
60
    from bzrlib.diff import compare_trees
61
61
    old_tree = cur_tree.basis_tree()
62
62
    new_tree = cur_tree
63
63
    non_source = []
64
64
    for path, file_class, kind, file_id, entry in new_tree.list_files():
65
65
        if file_class in ('?', 'I'):
66
66
            non_source.append(path)
67
 
    delta = new_tree.changes_from(old_tree, want_unchanged=False)
 
67
    delta = compare_trees(old_tree, new_tree, want_unchanged=False)
68
68
    return not delta.has_changed(), non_source
69
69
 
70
70
def set_push_data(tree, location):
71
 
    tree.branch.control_files.put_utf8("x-push-data", "%s\n" % location)
 
71
    push_file = file (tree.branch.control_files.controlfilename("x-push-data"), "wb")
 
72
    push_file.write("%s\n" % location)
72
73
 
73
74
def get_push_data(tree):
74
75
    """
77
78
    True
78
79
    >>> set_push_data(tree, 'http://somewhere')
79
80
    >>> get_push_data(tree)
80
 
    u'http://somewhere'
 
81
    'http://somewhere'
81
82
    >>> rm_tree(tree)
82
83
    """
83
 
    try:
84
 
        location = tree.branch.control_files.get_utf8('x-push-data').read()
85
 
    except NoSuchFile:
 
84
    filename = tree.branch.control_files.controlfilename("x-push-data")
 
85
    if not os.path.exists(filename):
86
86
        return None
87
 
    return location.rstrip('\n')
 
87
    push_file = file (filename, "rb")
 
88
    (location,) = [f.rstrip('\n') for f in push_file]
 
89
    return location
88
90
 
89
91
"""
90
92
>>> shell_escape('hello')
239
241
    except RsyncNoFile:
240
242
        return True
241
243
 
242
 
def rspush(tree, location=None, overwrite=False, working_tree=True):
 
244
def push(tree, location=None, overwrite=False, working_tree=True):
243
245
    push_location = get_push_data(tree)
244
246
    if location is not None:
245
247
        if not location.endswith('/'):
247
249
        push_location = location
248
250
    
249
251
    if push_location is None:
250
 
        raise BzrCommandError("No rspush location known or specified.")
251
 
 
252
 
    if (push_location.find('::') != -1):
253
 
        usessh=False
254
 
    else:
255
 
        usessh=True
256
 
 
257
 
    if (push_location.find('://') != -1 or
258
 
        push_location.find(':') == -1):
259
 
        raise BzrCommandError("Invalid rsync path %r." % push_location)
 
252
        if tree.branch.get_push_location() is None:
 
253
            raise BzrCommandError("No push location known or specified.")
 
254
        else:
 
255
            raise bzrlib.errors.MustUseDecorated
 
256
 
 
257
    if push_location.find('://') != -1:
 
258
        raise bzrlib.errors.MustUseDecorated
 
259
 
 
260
    if push_location.find(':') == -1:
 
261
        raise bzrlib.errors.MustUseDecorated
260
262
 
261
263
    if working_tree:
262
264
        clean, non_source = is_clean(tree)
288
290
                " specified location.  Please ensure that"
289
291
                ' "%s" is of the form "machine:/path".' % push_location)
290
292
    print "Pushing to %s" % push_location
291
 
    rsync(tree.basedir+'/', push_location, ssh=usessh, 
 
293
    rsync(tree.basedir+'/', push_location, ssh=True, 
292
294
          excludes=final_exclusions)
293
295
 
294
296
    set_push_data(tree, push_location)
303
305
 
304
306
def apache_ls(t):
305
307
    """Screen-scrape Apache listings"""
306
 
    apache_dir = '<img border="0" src="/icons/folder.gif" alt="[dir]">'\
307
 
        ' <a href="'
 
308
    apache_dir = '<IMG border="0" src="/icons/folder.gif" ALT="[DIR]">'\
 
309
        ' <A HREF="'
308
310
    lines = t.get('.')
309
 
    expr = re.compile('<a[^>]*href="([^>]*)"[^>]*>', flags=re.I)
310
311
    for line in lines:
311
 
        match = expr.search(line)
312
 
        if match is None:
313
 
            continue
314
 
        url = match.group(1)
315
 
        if url.startswith('http://') or url.startswith('/') or '../' in url:
316
 
            continue
317
 
        if '?' in url:
318
 
            continue
319
 
        yield url.rstrip('/')
 
312
        if line.startswith(apache_dir):
 
313
            url = line[len(apache_dir):].split('"')[0]
 
314
            yield url.rstrip('/')
320
315
 
321
316
 
322
317
def iter_branches(t, lister=None):
343
338
            except (NotBranchError, UnsupportedFormatError):
344
339
                continue
345
340
 
346
 
 
347
341
def iter_bzrdirs(t, lister=None):
348
342
    if lister is None:
349
343
        def lister(t):
351
345
    try:
352
346
        bzrdir = bzrdir_from_transport(t)
353
347
        yield bzrdir
354
 
    except (NotBranchError, UnsupportedFormatError, TransportError,
355
 
            PermissionDenied):
 
348
    except (NotBranchError, UnsupportedFormatError, TransportError):
356
349
        pass
357
350
    try:
358
351
        for directory in lister(t):
359
352
            if directory == ".bzr":
360
353
                continue
361
 
            try:
362
 
                subt = t.clone(directory)
363
 
            except UnicodeDecodeError:
364
 
                continue
365
 
            for bzrdir in iter_bzrdirs(subt, lister):
 
354
            subt = t.clone(directory)
 
355
            for bzrdir in iter_bzrdirs(subt):
366
356
                yield bzrdir
367
 
    except (NoSuchFile, PermissionDenied, TransportError):
 
357
    except NoSuchFile:
 
358
        pass
 
359
    except TransportError, e:
368
360
        pass
369
361
 
370
362