1
1
#!/usr/bin/env python
3
# Copyright (C) 2005 Aaron Bentley
4
# <aaron.bentley@utoronto.ca>
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
from errors import NoPyBaz
23
from baz_import import import_version, UserError
25
print >> sys.stderr, "This command requires PyBaz. Please ensure that it is installed."
5
print "This command requires PyBaz. Please ensure that it is installed."
8
from pybaz.backends.baz import null_cmd
32
"""Just the main() function for this script.
34
By separating it into a function, this can be called as a child from some other
37
:param args: The arguments to this script. Essentially sys.argv[1:]
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'
47
parser.add_option('--skip-symlinks', action="store_true",
49
help="Ignore any symlinks present in the Arch tree.")
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)
64
(opts, args) = parser.parse_args(args)
68
import doctest, baz_import
69
nfail, ntests = doctest.testmod(baz_import, verbose=opts.verbose)
75
version,output_dir = args
15
from progress import *
16
bzrlib.trace.create_tracefile([])
18
def add_id(files, id=None):
19
"""Adds an explicit id to a list of files.
21
:param files: the name of the file to add an id to
22
:type files: list of str
23
:param id: tag one file using the specified id, instead of generating id
28
args.extend(["--id", id])
34
>>> q = test_environ()
37
>>> os.path.exists(os.path.join(q, "home", ".arch-params"))
39
>>> teardown_environ(q)
43
tdir = tempfile.mkdtemp(prefix="baz2bzr-")
44
os.environ["HOME"] = os.path.join(tdir, "home")
45
os.mkdir(os.environ["HOME"])
46
arch_dir = os.path.join(tdir, "archive_dir")
47
pybaz.make_archive("test@example.com", arch_dir)
48
work_dir = os.path.join(tdir, "work_dir")
51
pybaz.init_tree(work_dir, "test@example.com/test--test--0")
52
lib_dir = os.path.join(tdir, "lib_dir")
54
pybaz.register_revision_library(lib_dir)
55
pybaz.set_my_id("Test User<test@example.org>")
58
def add_file(path, text, id):
60
>>> q = test_environ()
61
>>> add_file("path with space", "text", "lalala")
62
>>> tree = pybaz.tree_root(".")
63
>>> inv = list(tree.iter_inventory_ids(source=True, both=True))
64
>>> ("x_lalala", "path with space") in inv
66
>>> teardown_environ(q)
68
file(path, "wb").write(text)
72
def add_dir(path, id):
74
>>> q = test_environ()
75
>>> add_dir("path with\(sp) space", "lalala")
76
>>> tree = pybaz.tree_root(".")
77
>>> inv = list(tree.iter_inventory_ids(source=True, both=True))
78
>>> ("x_lalala", "path with\(sp) space") in inv
80
>>> teardown_environ(q)
85
def teardown_environ(tdir):
89
def timport(tree, summary):
90
msg = tree.log_message()
91
msg["summary"] = summary
94
def commit(tree, summary):
96
>>> q = test_environ()
97
>>> tree = pybaz.tree_root(".")
98
>>> timport(tree, "import")
99
>>> commit(tree, "commit")
100
>>> logs = [str(l.revision) for l in tree.iter_logs()]
104
'test@example.com/test--test--0--base-0'
106
'test@example.com/test--test--0--patch-1'
107
>>> teardown_environ(q)
109
msg = tree.log_message()
110
msg["summary"] = summary
113
def commit_test_revisions():
115
>>> q = test_environ()
116
>>> commit_test_revisions()
117
>>> a = pybaz.Archive("test@example.com")
118
>>> revisions = list(a.iter_revisions("test--test--0"))
121
>>> str(revisions[2])
122
'test@example.com/test--test--0--base-0'
123
>>> str(revisions[1])
124
'test@example.com/test--test--0--patch-1'
125
>>> str(revisions[0])
126
'test@example.com/test--test--0--patch-2'
127
>>> teardown_environ(q)
129
tree = pybaz.tree_root(".")
130
add_file("mainfile", "void main(void){}", "mainfile by aaron")
131
timport(tree, "Created mainfile")
132
file("mainfile", "wb").write("or something like that")
133
commit(tree, "altered mainfile")
134
add_file("ofile", "this is another file", "ofile by aaron")
135
commit(tree, "altered mainfile")
137
def version_ancestry(version):
139
>>> q = test_environ()
140
>>> commit_test_revisions()
141
>>> version = pybaz.Version("test@example.com/test--test--0")
142
>>> ancestors = version_ancestry(version)
143
>>> str(ancestors[0])
144
'test@example.com/test--test--0--base-0'
145
>>> str(ancestors[1])
146
'test@example.com/test--test--0--patch-1'
147
>>> teardown_environ(q)
149
revision = version.archive.iter_revisions(version.nonarch).next()
150
ancestors = list(revision.iter_ancestors())
152
ancestors.append(revision)
156
def import_version(output_dir, version):
158
>>> q = test_environ()
159
>>> result_path = os.path.join(q, "result")
160
>>> commit_test_revisions()
161
>>> version = pybaz.Version("test@example.com/test--test--0")
162
>>> import_version(result_path, version)
163
Importing 3 revisions
165
>>> teardown_environ(q)
167
tempdir = tempfile.mkdtemp(prefix="baz2bzr")
169
for result in iter_import_version(output_dir, version, tempdir):
172
print "Import complete."
174
shutil.rmtree(tempdir)
81
print 'Invalid number of arguments, try --help for more info'
84
output_dir = os.path.realpath(output_dir)
85
if version is not None:
87
version = pybaz.Version(version)
88
except pybaz.errors.NamespaceError:
89
print "%s is not a valid Arch branch." % version
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)
101
except KeyboardInterrupt:
107
if __name__ == '__main__':
108
sys.exit(main(sys.argv[1:]))
176
def iter_import_version(output_dir, version, tempdir):
178
ancestors = version_ancestry(version)
179
for i in range(len(ancestors)):
180
revision = ancestors[i]
181
yield Progress("revisions", i, len(ancestors))
183
revdir = os.path.join(tempdir, "rd")
184
baz_inv, log = get_revision(revdir, revision)
185
branch = bzrlib.Branch(revdir, init=True)
187
old = os.path.join(revdir, ".bzr")
188
new = os.path.join(tempdir, ".bzr")
190
shutil.rmtree(revdir)
191
baz_inv, log = get_revision(revdir, revision)
193
branch = bzrlib.Branch(revdir)
194
branch.set_inventory(baz_inv)
195
branch.commit(log.summary)
196
yield Progress("revisions", len(ancestors), len(ancestors))
197
unlink_unversioned(branch, revdir)
198
os.rename(revdir, output_dir)
200
def unlink_unversioned(branch, revdir):
201
for unversioned in branch.working_tree().extras():
202
path = os.path.join(revdir, unversioned)
203
if os.path.isdir(path):
208
def get_revision(revdir, revision):
210
tree = pybaz.tree_root(revdir)
211
log = tree.iter_logs(reverse=True).next()
212
return bzr_inventory_data(tree), log
214
def bzr_inventory_data(tree):
215
inv_iter = tree.iter_inventory_ids(source=True, both=True)
217
for file_id, path in inv_iter:
218
inv_map[path] = file_id
221
for path, file_id in inv_map.iteritems():
222
kind = bzrlib.osutils.file_kind(os.path.join(tree, path))
223
assert kind in ("file", "directory")
224
parent_dir = os.path.dirname(path)
226
parent_id = inv_map[parent_dir]
228
parent_id = bzrlib.inventory.ROOT_ID
229
bzr_inv.append((path, file_id, parent_id, kind))
233
if len(sys.argv) == 2 and sys.argv[1] == "test":
234
print "Running tests"
237
elif len(sys.argv) == 3:
238
import_version(sys.argv[2], pybaz.Version(sys.argv[1]))
240
print "usage: %s VERSION OUTDIR" % os.path.basename(sys.argv[0])