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
21
from errors import NoPyBaz
23
print "This command requires PyBaz. Please ensure that it is installed."
23
from baz_import import import_version, UserError
25
print >> sys.stderr, "This command requires PyBaz. Please ensure that it is installed."
26
from pybaz.backends.baz import null_cmd
35
from progress import *
37
def add_id(files, id=None):
38
"""Adds an explicit id to a list of files.
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
47
args.extend(["--id", id])
53
>>> q = test_environ()
56
>>> os.path.exists(os.path.join(q, "home", ".arch-params"))
58
>>> teardown_environ(q)
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")
70
pybaz.init_tree(work_dir, "test@example.com/test--test--0")
71
lib_dir = os.path.join(tdir, "lib_dir")
73
pybaz.register_revision_library(lib_dir)
74
pybaz.set_my_id("Test User<test@example.org>")
77
def add_file(path, text, id):
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
85
>>> teardown_environ(q)
87
file(path, "wb").write(text)
91
def add_dir(path, id):
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
99
>>> teardown_environ(q)
104
def teardown_environ(tdir):
108
def timport(tree, summary):
109
msg = tree.log_message()
110
msg["summary"] = summary
113
def commit(tree, summary):
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()]
123
'test@example.com/test--test--0--base-0'
125
'test@example.com/test--test--0--patch-1'
126
>>> teardown_environ(q)
128
msg = tree.log_message()
129
msg["summary"] = summary
132
def commit_test_revisions():
134
>>> q = test_environ()
135
>>> commit_test_revisions()
136
>>> a = pybaz.Archive("test@example.com")
137
>>> revisions = list(a.iter_revisions("test--test--0"))
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)
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")
156
def version_ancestry(version):
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)
168
revision = version.iter_revisions(reverse=True).next()
169
ancestors = list(revision.iter_ancestors(metoo=True))
174
def import_version(output_dir, version, fancy=True):
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)
184
>>> teardown_environ(q)
186
progress_bar = ProgressBar()
187
tempdir = tempfile.mkdtemp(prefix="baz2bzr")
191
for result in iter_import_version(output_dir, version, tempdir):
195
sys.stdout.write('.')
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)
200
sys.stdout.write('\n')
201
shutil.rmtree(tempdir)
202
print "Import complete."
75
version,output_dir = args
204
class UserError(Exception):
205
def __init__(self, message):
206
"""Exception to throw when a user makes an impossible request
207
:param message: The message to emit when printing this exception
208
:type message: string
210
Exception.__init__(self, message)
212
def revision_id(arch_revision):
214
Generate a Bzr revision id from an Arch revision id. 'x' in the id
215
designates a revision imported with an experimental algorithm. A number
216
would indicate a particular standardized version.
218
:param arch_revision: The Arch revision to generate an ID for.
220
>>> revision_id(pybaz.Revision("you@example.com/cat--br--0--base-0"))
221
'Arch-x:you@example.com%cat--br--0--base-0'
223
return "Arch-x:%s" % str(arch_revision).replace('/', '%')
225
def iter_import_version(output_dir, version, tempdir):
227
ancestors = version_ancestry(version)
228
for i in range(len(ancestors)):
229
revision = ancestors[i]
230
yield Progress("revisions", i, len(ancestors))
232
revdir = os.path.join(tempdir, "rd")
233
baz_inv, log = get_revision(revdir, revision)
234
branch = bzrlib.Branch(revdir, init=True)
236
old = os.path.join(revdir, ".bzr")
237
new = os.path.join(tempdir, ".bzr")
239
baz_inv, log = apply_revision(revdir, revision)
241
branch = bzrlib.Branch(revdir)
242
branch.set_inventory(baz_inv)
243
timestamp = email.Utils.mktime_tz(log.date + (0,))
244
rev_id = revision_id(revision)
245
bzrlib.trace.silent = True
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:
247
branch.commit(log.summary, verbose=False, committer=log.creator,
248
timestamp=timestamp, timezone=0, rev_id=rev_id)
250
bzrlib.trace.silent = False
251
yield Progress("revisions", len(ancestors), len(ancestors))
252
unlink_unversioned(branch, revdir)
253
os.rename(revdir, output_dir)
255
def unlink_unversioned(branch, revdir):
256
for unversioned in branch.working_tree().extras():
257
path = os.path.join(revdir, unversioned)
258
if os.path.isdir(path):
263
def get_log(tree, revision):
264
log = tree.iter_logs(version=revision.version, reverse=True).next()
265
assert log.revision == revision
268
def get_revision(revdir, revision):
270
tree = pybaz.tree_root(revdir)
271
log = get_log(tree, revision)
273
return bzr_inventory_data(tree), log
274
except BadFileKind, e:
275
raise UserError("Cannot convert %s because %s is a %s" % (revision,e.path, e.kind) )
278
def apply_revision(revdir, revision):
279
tree = pybaz.tree_root(revdir)
281
log = get_log(tree, revision)
283
return bzr_inventory_data(tree), log
284
except BadFileKind, e:
285
raise UserError("Cannot convert %s because %s is a %s" % (revision,e.path, e.kind) )
290
class BadFileKind(Exception):
291
"""The file kind is not permitted in bzr inventories"""
292
def __init__(self, tree_root, path, kind):
293
self.tree_root = tree_root
296
Exception.__init__(self, "File %s is of forbidden type %s" %
297
(os.path.join(tree_root, path), kind))
299
def bzr_inventory_data(tree):
300
inv_iter = tree.iter_inventory_ids(source=True, both=True)
302
for file_id, path in inv_iter:
303
inv_map[path] = file_id
306
for path, file_id in inv_map.iteritems():
307
full_path = os.path.join(tree, path)
308
kind = bzrlib.osutils.file_kind(full_path)
309
if kind not in ("file", "directory"):
310
raise BadFileKind(tree, path, kind)
311
parent_dir = os.path.dirname(path)
313
parent_id = inv_map[parent_dir]
315
parent_id = bzrlib.inventory.ROOT_ID
316
bzr_inv.append((path, file_id, parent_id, kind))
320
if len(sys.argv) == 2 and sys.argv[1] == "test":
321
print "Running tests"
324
elif len(sys.argv) == 3:
326
output_dir = sys.argv[2]
327
if os.path.exists(output_dir):
328
raise UserError("Directory \"%s\" already exists" % output_dir)
329
import_version(output_dir, pybaz.Version(sys.argv[1]))
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)
330
98
except UserError, e:
332
101
except KeyboardInterrupt:
335
print "usage: %s VERSION OUTDIR" % os.path.basename(sys.argv[0])
107
if __name__ == '__main__':
108
sys.exit(main(sys.argv[1:]))