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
23
print "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
tempdir = tempfile.mkdtemp(prefix="baz2bzr")
190
for result in iter_import_version(output_dir, version, tempdir):
194
sys.stdout.write('.')
199
sys.stdout.write('\n')
200
shutil.rmtree(tempdir)
201
print "Import complete."
203
class UserError(Exception):
204
def __init__(self, message):
205
"""Exception to throw when a user makes an impossible request
206
:param message: The message to emit when printing this exception
207
:type message: string
209
Exception.__init__(self, message)
211
def revision_id(arch_revision):
213
Generate a Bzr revision id from an Arch revision id. 'x' in the id
214
designates a revision imported with an experimental algorithm. A number
215
would indicate a particular standardized version.
217
:param arch_revision: The Arch revision to generate an ID for.
219
>>> revision_id(pybaz.Revision("you@example.com/cat--br--0--base-0"))
220
'Arch-x:you@example.com%cat--br--0--base-0'
222
return "Arch-x:%s" % str(arch_revision).replace('/', '%')
224
def iter_import_version(output_dir, version, tempdir):
226
ancestors = version_ancestry(version)
227
for i in range(len(ancestors)):
228
revision = ancestors[i]
229
yield Progress("revisions", i, len(ancestors))
231
revdir = os.path.join(tempdir, "rd")
232
baz_inv, log = get_revision(revdir, revision)
233
branch = bzrlib.Branch(revdir, init=True)
235
old = os.path.join(revdir, ".bzr")
236
new = os.path.join(tempdir, ".bzr")
238
baz_inv, log = apply_revision(revdir, revision)
240
branch = bzrlib.Branch(revdir)
241
branch.set_inventory(baz_inv)
242
timestamp = email.Utils.mktime_tz(log.date + (0,))
243
rev_id = revision_id(revision)
244
bzrlib.trace.silent = True
246
branch.commit(log.summary, verbose=False, committer=log.creator,
247
timestamp=timestamp, timezone=0, rev_id=rev_id)
249
bzrlib.trace.silent = False
250
yield Progress("revisions", len(ancestors), len(ancestors))
251
unlink_unversioned(branch, revdir)
252
os.rename(revdir, output_dir)
254
def unlink_unversioned(branch, revdir):
255
for unversioned in branch.working_tree().extras():
256
path = os.path.join(revdir, unversioned)
257
if os.path.isdir(path):
262
def get_log(tree, revision):
263
log = tree.iter_logs(version=revision.version, reverse=True).next()
264
assert log.revision == revision
267
def get_revision(revdir, revision):
269
tree = pybaz.tree_root(revdir)
270
log = get_log(tree, revision)
272
return bzr_inventory_data(tree), log
273
except BadFileKind, e:
274
raise UserError("Cannot convert %s because %s is a %s" % (revision,e.path, e.kind) )
277
def apply_revision(revdir, revision):
278
tree = pybaz.tree_root(revdir)
280
log = get_log(tree, revision)
282
return bzr_inventory_data(tree), log
283
except BadFileKind, e:
284
raise UserError("Cannot convert %s because %s is a %s" % (revision,e.path, e.kind) )
289
class BadFileKind(Exception):
290
"""The file kind is not permitted in bzr inventories"""
291
def __init__(self, tree_root, path, kind):
292
self.tree_root = tree_root
295
Exception.__init__(self, "File %s is of forbidden type %s" %
296
(os.path.join(tree_root, path), kind))
298
def bzr_inventory_data(tree):
299
inv_iter = tree.iter_inventory_ids(source=True, both=True)
301
for file_id, path in inv_iter:
302
inv_map[path] = file_id
305
for path, file_id in inv_map.iteritems():
306
full_path = os.path.join(tree, path)
307
kind = bzrlib.osutils.file_kind(full_path)
308
if kind not in ("file", "directory"):
309
raise BadFileKind(tree, path, kind)
310
parent_dir = os.path.dirname(path)
312
parent_id = inv_map[parent_dir]
314
parent_id = bzrlib.inventory.ROOT_ID
315
bzr_inv.append((path, file_id, parent_id, kind))
319
if len(sys.argv) == 2 and sys.argv[1] == "test":
320
print "Running tests"
323
elif len(sys.argv) == 3:
325
output_dir = sys.argv[2]
326
if os.path.exists(output_dir):
327
raise UserError("Directory \"%s\" already exists" % output_dir)
328
import_version(output_dir, pybaz.Version(sys.argv[1]))
331
except KeyboardInterrupt:
334
print "usage: %s VERSION OUTDIR" % os.path.basename(sys.argv[0])