~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to baz2bzr

  • Committer: Aaron Bentley
  • Date: 2005-06-02 18:16:32 UTC
  • Revision ID: abentley@panoramicfeedback.com-20050602181632-c8a739077a85046b
Skip symlinks while importing

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
#    along with this program; if not, write to the Free Software
18
18
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
19
 
20
 
import sys
21
 
from errors import NoPyBaz
22
20
try:
23
 
    from baz_import import import_version, UserError
24
 
except NoPyBaz:
25
 
    print >> sys.stderr, "This command requires PyBaz.  Please ensure that it is installed."
 
21
    import pybaz
 
22
except ImportError:
 
23
    print "This command requires PyBaz.  Please ensure that it is installed."
 
24
    import sys
26
25
    sys.exit(1)
27
 
 
28
 
import pybaz
 
26
from pybaz.backends.baz import null_cmd
 
27
import tempfile
 
28
import os
29
29
import os.path
30
 
 
31
 
def main(args):
32
 
    """Just the main() function for this script.
33
 
 
34
 
    By separating it into a function, this can be called as a child from some other
35
 
    script.
36
 
 
37
 
    :param args: The arguments to this script. Essentially sys.argv[1:]
38
 
    """
39
 
    import optparse
40
 
    parser = optparse.OptionParser(usage='%prog [options] [VERSION] OUTDIR'
41
 
        '\n  VERSION is the arch version to import.'
42
 
        '\n  OUTDIR can be an existing directory to be updated'
43
 
        '\n         or a new directory which will be created from scratch.')
44
 
    parser.add_option('--verbose', action='store_true'
45
 
        , help='Get chatty')
46
 
 
47
 
    parser.add_option('--skip-symlinks', action="store_true", 
48
 
                      dest="skip_symlinks", 
49
 
                      help="Ignore any symlinks present in the Arch tree.")
50
 
 
51
 
    g = optparse.OptionGroup(parser, 'Test options', 'Options useful while testing process.')
52
 
    g.add_option('--test', action='store_true'
53
 
        , help='Run the self-tests and exit.')
54
 
    g.add_option('--dry-run', action='store_true'
55
 
        , help='Do the update, but don\'t copy the result to OUTDIR')
56
 
    g.add_option('--max-count', type='int', metavar='COUNT', default=None
57
 
        , help='At most, add COUNT patches.')
58
 
    g.add_option('--safe', action='store_false', dest='fast')
59
 
    g.add_option('--fast', action='store_true', default=False
60
 
        , help='By default the .bzr control directory will be copied, so that an error'
61
 
        ' does not modify the original. --fast allows the directory to be renamed instead.')
62
 
    parser.add_option_group(g)
63
 
 
64
 
    (opts, args) = parser.parse_args(args)
65
 
 
66
 
    if opts.test:
67
 
        print "Running tests"
68
 
        import doctest, baz_import
69
 
        nfail, ntests = doctest.testmod(baz_import, verbose=opts.verbose)
70
 
        if nfail > 0:
71
 
            return 1
 
30
import shutil
 
31
import bzrlib
 
32
import bzrlib.trace
 
33
import sys
 
34
import email.Utils
 
35
from progress import *
 
36
 
 
37
def add_id(files, id=None):
 
38
    """Adds an explicit id to a list of files.
 
39
 
 
40
    :param files: the name of the file to add an id to
 
41
    :type files: list of str
 
42
    :param id: tag one file using the specified id, instead of generating id
 
43
    :type id: str
 
44
    """
 
45
    args = ["add-id"]
 
46
    if id is not None:
 
47
        args.extend(["--id", id])
 
48
    args.extend(files)
 
49
    return null_cmd(args)
 
50
 
 
51
def test_environ():
 
52
    """
 
53
    >>> q = test_environ()
 
54
    >>> os.path.exists(q)
 
55
    True
 
56
    >>> os.path.exists(os.path.join(q, "home", ".arch-params"))
 
57
    True
 
58
    >>> teardown_environ(q)
 
59
    >>> os.path.exists(q)
 
60
    False
 
61
    """
 
62
    tdir = tempfile.mkdtemp(prefix="baz2bzr-")
 
63
    os.environ["HOME"] = os.path.join(tdir, "home")
 
64
    os.mkdir(os.environ["HOME"])
 
65
    arch_dir = os.path.join(tdir, "archive_dir")
 
66
    pybaz.make_archive("test@example.com", arch_dir)
 
67
    work_dir = os.path.join(tdir, "work_dir")
 
68
    os.mkdir(work_dir)
 
69
    os.chdir(work_dir)
 
70
    pybaz.init_tree(work_dir, "test@example.com/test--test--0")
 
71
    lib_dir = os.path.join(tdir, "lib_dir")
 
72
    os.mkdir(lib_dir)
 
73
    pybaz.register_revision_library(lib_dir)
 
74
    pybaz.set_my_id("Test User<test@example.org>")
 
75
    return tdir
 
76
 
 
77
def add_file(path, text, id):
 
78
    """
 
79
    >>> q = test_environ()
 
80
    >>> add_file("path with space", "text", "lalala")
 
81
    >>> tree = pybaz.tree_root(".")
 
82
    >>> inv = list(tree.iter_inventory_ids(source=True, both=True))
 
83
    >>> ("x_lalala", "path with space") in inv
 
84
    True
 
85
    >>> teardown_environ(q)
 
86
    """
 
87
    file(path, "wb").write(text)
 
88
    add_id([path], id)
 
89
 
 
90
 
 
91
def add_dir(path, id):
 
92
    """
 
93
    >>> q = test_environ()
 
94
    >>> add_dir("path with\(sp) space", "lalala")
 
95
    >>> tree = pybaz.tree_root(".")
 
96
    >>> inv = list(tree.iter_inventory_ids(source=True, both=True))
 
97
    >>> ("x_lalala", "path with\(sp) space") in inv
 
98
    True
 
99
    >>> teardown_environ(q)
 
100
    """
 
101
    os.mkdir(path)
 
102
    add_id([path], id)
 
103
 
 
104
def teardown_environ(tdir):
 
105
    os.chdir("/")
 
106
    shutil.rmtree(tdir)
 
107
 
 
108
def timport(tree, summary):
 
109
    msg = tree.log_message()
 
110
    msg["summary"] = summary
 
111
    tree.import_(msg)
 
112
 
 
113
def commit(tree, summary):
 
114
    """
 
115
    >>> q = test_environ()
 
116
    >>> tree = pybaz.tree_root(".")
 
117
    >>> timport(tree, "import")
 
118
    >>> commit(tree, "commit")
 
119
    >>> logs = [str(l.revision) for l in tree.iter_logs()]
 
120
    >>> len(logs)
 
121
    2
 
122
    >>> logs[0]
 
123
    'test@example.com/test--test--0--base-0'
 
124
    >>> logs[1]
 
125
    'test@example.com/test--test--0--patch-1'
 
126
    >>> teardown_environ(q)
 
127
    """
 
128
    msg = tree.log_message()
 
129
    msg["summary"] = summary
 
130
    tree.commit(msg)
 
131
 
 
132
def commit_test_revisions():
 
133
    """
 
134
    >>> q = test_environ()
 
135
    >>> commit_test_revisions()
 
136
    >>> a = pybaz.Archive("test@example.com")
 
137
    >>> revisions = list(a.iter_revisions("test--test--0"))
 
138
    >>> len(revisions)
 
139
    3
 
140
    >>> str(revisions[2])
 
141
    'test@example.com/test--test--0--base-0'
 
142
    >>> str(revisions[1])
 
143
    'test@example.com/test--test--0--patch-1'
 
144
    >>> str(revisions[0])
 
145
    'test@example.com/test--test--0--patch-2'
 
146
    >>> teardown_environ(q)
 
147
    """
 
148
    tree = pybaz.tree_root(".")
 
149
    add_file("mainfile", "void main(void){}", "mainfile by aaron")
 
150
    timport(tree, "Created mainfile")
 
151
    file("mainfile", "wb").write("or something like that")
 
152
    commit(tree, "altered mainfile")
 
153
    add_file("ofile", "this is another file", "ofile by aaron")
 
154
    commit(tree, "altered mainfile")
 
155
 
 
156
def version_ancestry(version):
 
157
    """
 
158
    >>> q = test_environ()
 
159
    >>> commit_test_revisions()
 
160
    >>> version = pybaz.Version("test@example.com/test--test--0")
 
161
    >>> ancestors = version_ancestry(version)
 
162
    >>> str(ancestors[0])
 
163
    'test@example.com/test--test--0--base-0'
 
164
    >>> str(ancestors[1])
 
165
    'test@example.com/test--test--0--patch-1'
 
166
    >>> teardown_environ(q)
 
167
    """
 
168
    revision = version.iter_revisions(reverse=True).next()
 
169
    ancestors = list(revision.iter_ancestors(metoo=True))
 
170
    ancestors.reverse()
 
171
    return ancestors
 
172
 
 
173
 
 
174
def import_version(output_dir, version, fancy=True):
 
175
    """
 
176
    >>> q = test_environ()
 
177
    >>> result_path = os.path.join(q, "result")
 
178
    >>> commit_test_revisions()
 
179
    >>> version = pybaz.Version("test@example.com/test--test--0")
 
180
    >>> import_version(result_path, version, fancy=False)
 
181
    not fancy
 
182
    ....
 
183
    Import complete.
 
184
    >>> teardown_environ(q)
 
185
    """
 
186
    progress_bar = ProgressBar()
 
187
    tempdir = tempfile.mkdtemp(prefix="baz2bzr-",
 
188
                               dir=os.path.dirname(output_dir))
 
189
    try:
 
190
        if not fancy:
 
191
            print "not fancy"
 
192
        for result in iter_import_version(output_dir, version, tempdir):
 
193
            if fancy:
 
194
                progress_bar(result)
 
195
            else:
 
196
                sys.stdout.write('.')
 
197
    finally:
 
198
        if fancy:
 
199
            clear_progress_bar()
72
200
        else:
73
 
            return 0
74
 
    if len(args) == 2:
75
 
        version,output_dir = args
 
201
            sys.stdout.write('\n')
 
202
        shutil.rmtree(tempdir)
 
203
    print "Import complete."
76
204
            
77
 
    elif len(args) == 1:
78
 
        output_dir = args[0]
79
 
        version = None
80
 
    else:
81
 
        print 'Invalid number of arguments, try --help for more info'
82
 
        return 1
83
 
 
84
 
    output_dir = os.path.realpath(output_dir)
85
 
    if version is not None:
 
205
class UserError(Exception):
 
206
    def __init__(self, message):
 
207
        """Exception to throw when a user makes an impossible request
 
208
        :param message: The message to emit when printing this exception
 
209
        :type message: string
 
210
        """
 
211
        Exception.__init__(self, message)
 
212
 
 
213
def revision_id(arch_revision):
 
214
    """
 
215
    Generate a Bzr revision id from an Arch revision id.  'x' in the id
 
216
    designates a revision imported with an experimental algorithm.  A number
 
217
    would indicate a particular standardized version.
 
218
 
 
219
    :param arch_revision: The Arch revision to generate an ID for.
 
220
 
 
221
    >>> revision_id(pybaz.Revision("you@example.com/cat--br--0--base-0"))
 
222
    'Arch-x:you@example.com%cat--br--0--base-0'
 
223
    """
 
224
    return "Arch-x:%s" % str(arch_revision).replace('/', '%')
 
225
 
 
226
def iter_import_version(output_dir, version, tempdir):
 
227
    revdir = None
 
228
    ancestors = version_ancestry(version)
 
229
    for i in range(len(ancestors)):
 
230
        revision = ancestors[i]
 
231
        yield Progress("revisions", i, len(ancestors))
 
232
        if revdir is None:
 
233
            revdir = os.path.join(tempdir, "rd")
 
234
            baz_inv, log = get_revision(revdir, revision, skip_symlink=True)
 
235
            branch = bzrlib.Branch(revdir, init=True)
 
236
        else:
 
237
            old = os.path.join(revdir, ".bzr")
 
238
            new = os.path.join(tempdir, ".bzr")
 
239
            os.rename(old, new)
 
240
            baz_inv, log = apply_revision(revdir, revision, skip_symlink=True)
 
241
            os.rename(new, old)
 
242
            branch = bzrlib.Branch(revdir)
 
243
        timestamp = email.Utils.mktime_tz(log.date + (0,))
 
244
        rev_id = revision_id(revision)
 
245
        branch.lock_write()
86
246
        try:
87
 
            version = pybaz.Version(version)
88
 
        except pybaz.errors.NamespaceError:
89
 
            print "%s is not a valid Arch branch." % version
90
 
            return 1
91
 
        
92
 
    try:
93
 
        import_version(output_dir, version,
94
 
            verbose=opts.verbose, fast=opts.fast,
95
 
            dry_run=opts.dry_run, max_count=opts.max_count,
96
 
            skip_symlinks=opts.skip_symlinks)
97
 
        return 0
 
247
            branch.set_inventory(baz_inv)
 
248
            bzrlib.trace.silent = True
 
249
            branch.commit(log.summary, verbose=False, committer=log.creator,
 
250
                          timestamp=timestamp, timezone=0, rev_id=rev_id)
 
251
        finally:
 
252
            bzrlib.trace.silent = False   
 
253
            branch.unlock()
 
254
    yield Progress("revisions", len(ancestors), len(ancestors))
 
255
    unlink_unversioned(branch, revdir)
 
256
    os.rename(revdir, output_dir)   
 
257
 
 
258
def unlink_unversioned(branch, revdir):
 
259
    for unversioned in branch.working_tree().extras():
 
260
        path = os.path.join(revdir, unversioned)
 
261
        if os.path.isdir(path):
 
262
            shutil.rmtree(path)
 
263
        else:
 
264
            os.unlink(path)
 
265
 
 
266
def get_log(tree, revision):
 
267
    log = tree.iter_logs(version=revision.version, reverse=True).next()
 
268
    assert log.revision == revision
 
269
    return log
 
270
 
 
271
def get_revision(revdir, revision, skip_symlink=False):
 
272
    revision.get(revdir)
 
273
    tree = pybaz.tree_root(revdir)
 
274
    log = get_log(tree, revision)
 
275
    try:
 
276
        return bzr_inventory_data(tree, skip_symlink=skip_symlink), log 
 
277
    except BadFileKind, e:
 
278
        raise UserError("Cannot convert %s because %s is a %s" % (revision,e.path, e.kind) )
 
279
 
 
280
 
 
281
def apply_revision(revdir, revision, skip_symlink=False):
 
282
    tree = pybaz.tree_root(revdir)
 
283
    revision.apply(tree)
 
284
    log = get_log(tree, revision)
 
285
    try:
 
286
        return bzr_inventory_data(tree, skip_symlink=skip_symlink), log
 
287
    except BadFileKind, e:
 
288
        raise UserError("Cannot convert %s because %s is a %s" % (revision,e.path, e.kind) )
 
289
 
 
290
 
 
291
 
 
292
 
 
293
class BadFileKind(Exception):
 
294
    """The file kind is not permitted in bzr inventories"""
 
295
    def __init__(self, tree_root, path, kind):
 
296
        self.tree_root = tree_root
 
297
        self.path = path
 
298
        self.kind = kind
 
299
        Exception.__init__(self, "File %s is of forbidden type %s" %
 
300
                           (os.path.join(tree_root, path), kind))
 
301
 
 
302
def bzr_inventory_data(tree, skip_symlink=False):
 
303
    inv_iter = tree.iter_inventory_ids(source=True, both=True)
 
304
    inv_map = {}
 
305
    for file_id, path in inv_iter:
 
306
        inv_map[path] = file_id 
 
307
 
 
308
    bzr_inv = []
 
309
    for path, file_id in inv_map.iteritems():
 
310
        full_path = os.path.join(tree, path)
 
311
        kind = bzrlib.osutils.file_kind(full_path)
 
312
        if skip_symlink and kind == "symlink":
 
313
            continue
 
314
        if kind not in ("file", "directory"):
 
315
            raise BadFileKind(tree, path, kind)
 
316
        parent_dir = os.path.dirname(path)
 
317
        if parent_dir != "":
 
318
            parent_id = inv_map[parent_dir]
 
319
        else:
 
320
            parent_id = bzrlib.inventory.ROOT_ID
 
321
        bzr_inv.append((path, file_id, parent_id, kind))
 
322
    bzr_inv.sort()
 
323
    return bzr_inv
 
324
 
 
325
if len(sys.argv) == 2 and sys.argv[1] == "test":
 
326
    print "Running tests"
 
327
    import doctest
 
328
    doctest.testmod()
 
329
elif len(sys.argv) == 3:
 
330
    try:
 
331
        output_dir = os.path.realpath(sys.argv[2])
 
332
        if os.path.exists(output_dir):
 
333
            raise UserError("Directory \"%s\" already exists" % output_dir)
 
334
        import_version(output_dir, pybaz.Version(sys.argv[1]))
98
335
    except UserError, e:
99
336
        print e
100
 
        return 1
101
337
    except KeyboardInterrupt:
102
338
        print "Aborted."
103
 
        return 1
104
 
 
105
 
        
106
 
 
107
 
if __name__ == '__main__':
108
 
    sys.exit(main(sys.argv[1:]))
109
 
 
 
339
else:
 
340
    print "usage: %s VERSION OUTDIR" % os.path.basename(sys.argv[0])