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
33
from progress import *
34
bzrlib.trace.create_tracefile([])
36
def add_id(files, id=None):
37
"""Adds an explicit id to a list of files.
39
:param files: the name of the file to add an id to
40
:type files: list of str
41
:param id: tag one file using the specified id, instead of generating id
46
args.extend(["--id", id])
52
>>> q = test_environ()
55
>>> os.path.exists(os.path.join(q, "home", ".arch-params"))
57
>>> teardown_environ(q)
61
tdir = tempfile.mkdtemp(prefix="baz2bzr-")
62
os.environ["HOME"] = os.path.join(tdir, "home")
63
os.mkdir(os.environ["HOME"])
64
arch_dir = os.path.join(tdir, "archive_dir")
65
pybaz.make_archive("test@example.com", arch_dir)
66
work_dir = os.path.join(tdir, "work_dir")
69
pybaz.init_tree(work_dir, "test@example.com/test--test--0")
70
lib_dir = os.path.join(tdir, "lib_dir")
72
pybaz.register_revision_library(lib_dir)
73
pybaz.set_my_id("Test User<test@example.org>")
76
def add_file(path, text, id):
78
>>> q = test_environ()
79
>>> add_file("path with space", "text", "lalala")
80
>>> tree = pybaz.tree_root(".")
81
>>> inv = list(tree.iter_inventory_ids(source=True, both=True))
82
>>> ("x_lalala", "path with space") in inv
84
>>> teardown_environ(q)
86
file(path, "wb").write(text)
90
def add_dir(path, id):
92
>>> q = test_environ()
93
>>> add_dir("path with\(sp) space", "lalala")
94
>>> tree = pybaz.tree_root(".")
95
>>> inv = list(tree.iter_inventory_ids(source=True, both=True))
96
>>> ("x_lalala", "path with\(sp) space") in inv
98
>>> teardown_environ(q)
103
def teardown_environ(tdir):
107
def timport(tree, summary):
108
msg = tree.log_message()
109
msg["summary"] = summary
112
def commit(tree, summary):
114
>>> q = test_environ()
115
>>> tree = pybaz.tree_root(".")
116
>>> timport(tree, "import")
117
>>> commit(tree, "commit")
118
>>> logs = [str(l.revision) for l in tree.iter_logs()]
122
'test@example.com/test--test--0--base-0'
124
'test@example.com/test--test--0--patch-1'
125
>>> teardown_environ(q)
127
msg = tree.log_message()
128
msg["summary"] = summary
131
def commit_test_revisions():
133
>>> q = test_environ()
134
>>> commit_test_revisions()
135
>>> a = pybaz.Archive("test@example.com")
136
>>> revisions = list(a.iter_revisions("test--test--0"))
139
>>> str(revisions[2])
140
'test@example.com/test--test--0--base-0'
141
>>> str(revisions[1])
142
'test@example.com/test--test--0--patch-1'
143
>>> str(revisions[0])
144
'test@example.com/test--test--0--patch-2'
145
>>> teardown_environ(q)
147
tree = pybaz.tree_root(".")
148
add_file("mainfile", "void main(void){}", "mainfile by aaron")
149
timport(tree, "Created mainfile")
150
file("mainfile", "wb").write("or something like that")
151
commit(tree, "altered mainfile")
152
add_file("ofile", "this is another file", "ofile by aaron")
153
commit(tree, "altered mainfile")
155
def version_ancestry(version):
157
>>> q = test_environ()
158
>>> commit_test_revisions()
159
>>> version = pybaz.Version("test@example.com/test--test--0")
160
>>> ancestors = version_ancestry(version)
161
>>> str(ancestors[0])
162
'test@example.com/test--test--0--base-0'
163
>>> str(ancestors[1])
164
'test@example.com/test--test--0--patch-1'
165
>>> teardown_environ(q)
167
revision = version.archive.iter_revisions(version.nonarch).next()
168
ancestors = list(revision.iter_ancestors())
170
ancestors.append(revision)
174
def import_version(output_dir, version):
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)
181
Importing 3 revisions
183
>>> teardown_environ(q)
185
tempdir = tempfile.mkdtemp(prefix="baz2bzr")
187
for result in iter_import_version(output_dir, version, tempdir):
190
print "Import complete."
192
shutil.rmtree(tempdir)
194
class UserError(Exception):
195
def __init__(self, message):
196
"""Exception to throw when a user makes an impossible request
197
:param message: The message to emit when printing this exception
198
:type message: string
200
Exception.__init__(self, message)
202
def iter_import_version(output_dir, version, tempdir):
204
ancestors = version_ancestry(version)
205
for i in range(len(ancestors)):
206
revision = ancestors[i]
207
yield Progress("revisions", i, len(ancestors))
209
revdir = os.path.join(tempdir, "rd")
210
baz_inv, log = get_revision(revdir, revision)
211
branch = bzrlib.Branch(revdir, init=True)
213
old = os.path.join(revdir, ".bzr")
214
new = os.path.join(tempdir, ".bzr")
216
baz_inv, log = apply_revision(revdir, revision)
218
branch = bzrlib.Branch(revdir)
219
branch.set_inventory(baz_inv)
220
branch.commit(log.summary)
221
yield Progress("revisions", len(ancestors), len(ancestors))
222
unlink_unversioned(branch, revdir)
223
os.rename(revdir, output_dir)
225
def unlink_unversioned(branch, revdir):
226
for unversioned in branch.working_tree().extras():
227
path = os.path.join(revdir, unversioned)
228
if os.path.isdir(path):
233
def get_revision(revdir, revision):
235
tree = pybaz.tree_root(revdir)
236
log = tree.iter_logs(reverse=True).next()
237
return bzr_inventory_data(tree), log
239
def apply_revision(revdir, revision):
240
tree = pybaz.tree_root(revdir)
242
log = tree.iter_logs(reverse=True).next()
243
return bzr_inventory_data(tree), log
246
def bzr_inventory_data(tree):
247
inv_iter = tree.iter_inventory_ids(source=True, both=True)
249
for file_id, path in inv_iter:
250
inv_map[path] = file_id
253
for path, file_id in inv_map.iteritems():
254
kind = bzrlib.osutils.file_kind(os.path.join(tree, path))
255
assert kind in ("file", "directory")
256
parent_dir = os.path.dirname(path)
258
parent_id = inv_map[parent_dir]
260
parent_id = bzrlib.inventory.ROOT_ID
261
bzr_inv.append((path, file_id, parent_id, kind))
265
if len(sys.argv) == 2 and sys.argv[1] == "test":
266
print "Running tests"
269
elif len(sys.argv) == 3:
271
output_dir = sys.argv[2]
272
if os.path.exists(output_dir):
273
raise UserError("Directory \"%s\" already exists" % output_dir)
274
import_version(output_dir, pybaz.Version(sys.argv[1]))
278
print "usage: %s VERSION OUTDIR" % os.path.basename(sys.argv[0])