bzr-discover-dir.py

Second revision of script. - Michael Trausch, 2011-01-16 02:25

Download (2.12 KB)

 
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
'''Discover the type of directory given on the command line.
4
Copyright © 2011 Michael B. Trausch <mike@trausch.us>
5

6
-----------
7
This program is free software; you can redistribute it and/or
8
modify it under the terms of the GNU General Public License
9
as published by the Free Software Foundation; either version 2
10
of the License, or (at your option) any later version.
11

12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16

17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software Foundation, Inc.,
19
51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
-----------
21

22
This module's purpose is to look at a directory and determine if it
23
is:
24

25
 * A plain directory (e.g., nothing to do with bzr at all),
26
 * A branch, or
27
 * A shared repository.
28

29
The return value will be 0 if it finds a branch or a shared
30
repository, or 1 if it is neither.  To learn more, the standard output
31
stream must be read and parsed.  For a branch it will appear thus:
32

33
path: /path/to/dirname
34
type: branch
35

36
For a shared repository it will appear thus:
37

38
path: /path/to/dirname
39
type: shared-repository
40
branches:
41
  /path/to/branch1
42
  /path/to/branch2
43
  /path/to/branch3
44

45
(That is, with two spaces before the path to the branch.)
46
'''
47
import sys
48

    
49
import bzrlib
50
import bzrlib.branch as bz_b
51
import bzrlib.repository as bz_r
52

    
53
def bzr_dirtype(path):
54
    try:
55
        r = bz_r.Repository.open(path)
56
        return ('shared-repository', r)
57
    except:
58
        pass
59

    
60
    try:
61
        b = bz_b.Branch.open(path)
62
        return ('branch', b)
63
    except:
64
        pass
65

    
66
    return (None, None)
67

    
68
p = sys.argv[1]
69
dtype, dobj = bzr_dirtype(p)
70

    
71
if dtype is None:
72
    sys.exit(1)
73
else:
74
    print 'path: {0}'.format(p)
75
    print 'type: {0}'.format(dtype)
76

    
77
    if dtype == 'shared-repository':
78
        print 'branches:'
79
        for branch in dobj.find_branches(True):
80
            print '  {0}'.format(branch.user_url)