~abentley/bzrtools/bzrtools.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python

# Copyright (C) 2005 Aaron Bentley
# <aaron.bentley@utoronto.ca>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from baz_import import import_version, UserError
import pybaz
import sys
import os.path

def main(args):
    """Just the main() function for this script.

    By separating it into a function, this can be called as a child from some other
    script.

    :param args: The arguments to this script. Essentially sys.argv[1:]
    """
    import optparse
    parser = optparse.OptionParser(usage='%prog [options] [VERSION] OUTDIR'
        '\n  VERSION is the arch version to import.'
        '\n  OUTDIR can be an existing directory to be updated'
        '\n         or a new directory which will be created from scratch.')
    parser.add_option('--verbose', action='store_true'
        , help='Get chatty')

    parser.add_option('--skip-symlinks', action="store_true", 
                      dest="skip_symlinks", 
                      help="Ignore any symlinks present in the Arch tree.")

    g = optparse.OptionGroup(parser, 'Test options', 'Options useful while testing process.')
    g.add_option('--test', action='store_true'
        , help='Run the self-tests and exit.')
    g.add_option('--dry-run', action='store_true'
        , help='Do the update, but don\'t copy the result to OUTDIR')
    g.add_option('--max-count', type='int', metavar='COUNT', default=None
        , help='At most, add COUNT patches.')
    g.add_option('--safe', action='store_false', dest='fast')
    g.add_option('--fast', action='store_true', default=False
        , help='By default the .bzr control directory will be copied, so that an error'
        ' does not modify the original. --fast allows the directory to be renamed instead.')
    parser.add_option_group(g)

    (opts, args) = parser.parse_args(args)

    if opts.test:
        print "Running tests"
        import doctest, baz_import
        nfail, ntests = doctest.testmod(baz_import, verbose=opts.verbose)
        if nfail > 0:
            return 1
        else:
            return 0
    if len(args) == 2:
        version,output_dir = args[1]
            
    elif len(args) == 1:
        output_dir = args[0]
        version = None
    else:
        print 'Invalid number of arguments, try --help for more info'
        return 1

    output_dir = os.path.realpath(output_dir)
    if version is not None:
        try:
            version = pybaz.Version(version)
        except pybaz.errors.NamespaceError:
            print "%s is not a valid Arch branch." % version
            return 1
        
    try:
        import_version(output_dir, version,
            verbose=opts.verbose, fast=opts.fast,
            dry_run=opts.dry_run, max_count=opts.max_count,
            skip_symlinks=opts.skip_symlinks)
        return 0
    except UserError, e:
        print e
        return 1
    except KeyboardInterrupt:
        print "Aborted."
        return 1

        

if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))