1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
# Copyright (C) 2004 Aaron Bentley
# <aaron.bentley@utoronto.ca>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import pybaz as arch
from pybaz import iter_revision_libraries
from pybaz.backends.baz import sequence_cmd, null_cmd, one_cmd, status_cmd
import re
from native import DiffFilenames, DiffHunk, DiffLine, DiffAddLine
from native import DiffRemoveLine
__docformat__ = "restructuredtext"
__doc__ = "Functionality provided by tla"
def chattermatch (line, matchtext):
"""
Detects whether line is Chatter that begins with matchtext.
:param line: The line to test
:type line: `arch.Chatter`, other
:param matchtext: The chatter text to look for
:type matchtext: string
"""
return isinstance(line, arch.Chatter) and line.text.startswith(matchtext)
def diff_iter(changeset):
"""
Generate an iterator of diff output for a given changeset
:param changeset: The location of the changeset
:type changeset: string
:rtype: iterator of diff output
"""
printline=False
iterator = sequence_cmd(['show-changeset', '--diffs', changeset])
for line in arch.classify_chatter(iterator):
if chattermatch(line, "file metadata changed") or \
chattermatch(line, "modified files"):
printline=True
if not printline:
continue
if isinstance(line, arch.Chatter):
yield line
elif line.startswith("---"):
origname=line
elif line.startswith("+++"):
yield DiffFilenames(origname, line)
elif line.startswith("@"):
yield DiffHunk(line)
elif line.startswith("+"):
yield DiffAddLine(line)
elif line.startswith("-"):
yield DiffRemoveLine(line)
elif line.startswith(" "):
yield DiffLine(line)
else:
yield line
def iter_mirror_archive(from_arch, to_arch, limit):
iter = mirror_archive(from_arch, to_arch, limit)
return arch.classify_chatter(iter)
def mirror_archive(from_arch, to_arch, limit):
"""
Mirrors an archive to another archive.
:param from_arch: The name of the source archive
:param to_arch: The name of the target archive
:param limit: The specific thing to mirror from the archive. May be a\
branch, package or package-version. Must not include an archive.
:rtype: arch.util.exec_safe_iter_stdout
"""
command=['archive-mirror', str(from_arch), str(to_arch)]
if limit != None:
command.append(str(limit))
for line in arch.util.exec_safe_iter_stdout('tla', command, expected=(0)):
yield line.rstrip('\n')
def iter_get(revision, directory=None, link=False, no_pristine=False,
no_greedy_add=False):
"""Creates a new dir containing the given revision.
:param revision: The revision to get
:type revision: `arch.Revision`
:param directory: The name of the dir to create
:type directory: str
:rtype: Iterator of `arch.Chatter`
"""
if directory is None:
directory = revision.fullname
return arch.classify_chatter(_iter_get(revision.fullname, directory,
link, no_pristine, no_greedy_add))
def _iter_get(revision, directory, link, no_pristine, no_greedy_add):
"""Creates a new dir containing the given revision.
:param revision: The revision to get
:type revision: `arch.Revision`
:param directory: The name of the dir to create
:type directory: str
:rtype: Iterator of str
"""
args = ["get"]
if link:
args.append("--link")
if no_pristine:
args.append("--no-pristine")
if no_greedy_add:
args.append("--no-greedy-add")
args.extend([revision, directory])
return sequence_cmd(args)
def iter_library_add(revision, directory=None):
"""Adds a revision to a library.
:param revision: The revision to add
:type revision: `arch.Revision`
:param directory: The directory of the library to add to
:type directory: str
:rtype: Iterator of `arch.Chatter`
"""
return arch.ChangesetApplication(_iter_library_add(revision.fullname,
directory))
def _iter_library_add(revision, directory):
"""Creates a new dir containing the given revision.
:param revision: The revision to get
:type revision: str
:param directory: The name of the dir to create
:type directory: str
:rtype: Iterator of str
"""
args = ["library-add"]
if directory is not None:
args.extend(["-L", directory])
args.append(revision)
return arch.util.exec_safe_iter_stdout('tla', args)
def iter_missing(tree, version, reverse=False, skip_present=False):
assert (isinstance(version, arch.Version))
for line in _iter_missing(str(tree), version.fullname, reverse,
skip_present):
yield arch.Revision(line)
def iter_new_merges(tree, version, reverse=False):
"""List patchlogs that are new in this tree since the last commit.
:param tree: The working tree to calculate new revisions in
:type tree: str
:param version: The version to use when determining new logs
:type version: str
:param reverse: If true, list backwards, from newest to oldest
:type reverse: bool
:return: An iterator for new revision logs in this tree
:rtype: Iterator of `arch.Patchlog`
"""
assert (isinstance(version, arch.Version))
for line in _iter_new_merges(tree, version.fullname, reverse):
yield arch.Patchlog(line, tree)
def _iter_missing(directory, version, reverse, skip_present):
args = [ 'missing', '--full', '--dir', directory ]
if reverse: args.append('--reverse')
if skip_present:
args.append('--skip-present')
args.append(version)
return sequence_cmd(args)
def _iter_new_merges(directory, version, reverse):
"""List patchlogs that are new in this tree since the last commit.
:param directory: The working tree to calculate new revisions in
:type directory: str
:param version: The version to use when determining new logs
:type version: str
:param reverse: If true, list backwards, from newest to oldest
:type reverse: bool
:return: An iterator for names of revisions new in this tree
:rtype: Iterator of str
"""
args = [ 'new-merges', '--dir', directory ]
if reverse:
args.append('--reverse')
args.append(version)
return sequence_cmd(args)
def apply_changeset(directory, changeset, reverse=False):
application = changeset.iter_apply(directory, reverse=reverse)
for line in application:
pass
return application.conflicting
def iter_tla_commands(command_aliases = True):
"""Generator for tla command name iterator.
:rtype: iterator of str
"""
for line in sequence_cmd(["help"]):
pieces=line.split(':')
if len(pieces) == 2:
if command_aliases or (re.search('\(alias for', pieces[1]) is None):
yield pieces[0].lstrip(' ').rstrip(' ')
def iter_tla_cmd_help(name, detailed=False):
if detailed:
arg = "-H"
else:
arg = "-h"
return sequence_cmd((name, arg))
def _iter_browse(search):
if search is None:
return []
splurch=search.split("/")
args=["rbrowse", "-A", splurch[0]]
if len(splurch) > 1:
args.append(splurch[1])
return sequence_cmd(args)
def iter_browse(search):
archive = None
for entry in _iter_browse(search):
if entry.startswith(' ') or entry == "":
pass
elif entry.startswith(' '):
assert (archive is not None)
yield arch.Version("%s/%s" % (archive.fullname, entry[9:]))
elif entry.startswith(' '):
assert (archive is not None)
yield arch.Branch("%s/%s" % (archive.fullname, entry[6:]))
elif entry.startswith(' '):
assert (archive is not None)
yield arch.Category("%s/%s" % (archive.fullname, entry[3:]))
else:
assert (archive is not None)
archive = arch.Archive(entry)
yield archive
def _iter_log_for_merge(directory, version, reverse):
"""Produces log-for-merge output
:param directory: The tree to produce output for
:type directory: str
:param version: The version to produce output for
:type version: str
:param reverse: List the logs in reverse order
:type reverse: bool
:return: An iterator that iterates through lines of output
:rtype: iterator of str
"""
args = ["log-for-merge"]
if reverse:
args.append("--reverse")
if version is not None:
args.append(version)
return sequence_cmd(args)
def log_for_merge(tree, version=None, reverse=False):
"""Produces log-for-merge output
:param tree: The tree to produce output for
:type tree: `arch.WorkingTree`
:param version: The version to produce output for
:type version: `arch.Version`
:param reverse: List the logs in reverse order
:type reverse: bool
:return: log-for-merge output or None
:rtype: str or NoneType
"""
merge_log = ""
if version is not None:
version = str(version)
for line in _iter_log_for_merge(tree, version, reverse):
merge_log += line + "\n"
if len(merge_log) == 1 and merge_log[0]=="\n":
return None
return merge_log
def add_id(files, id=None):
"""Adds an explicit id to a list of files.
:param files: the name of the file to add an id to
:type files: list of str
:param id: tag one file using the specified id, instead of generating id
:type id: str
"""
args = ["add-id"]
if id is not None:
args.extend(["--id", id])
args.extend(files)
return null_cmd(args)
def file_id(filename):
return one_cmd(("id", filename)).split()[1].rstrip("\n")
def has_changed(version):
return status_cmd(['changes', str(version)], (0, 1)) == 1
def iter_all_inventory(tree, dirs, control_files=False):
args = ['inventory', '--both', '--ids']
if control_files:
args.append('--all')
return sequence_cmd(args, chdir=str(tree))
class TreeFile:
def __init__(self, name, tagged, letter):
self.letter = letter
self.name = name
self.tagged = tagged
def __str__(self):
line = self.letter
if not self.tagged:
line += "? "
else:
line += " "
line += self.name
return line
def get_id(self):
return None
class SourceFile(TreeFile):
def __init__(self, name, tagged, id):
TreeFile.__init__(self, name, tagged, "S")
self.id = id
def get_id(self):
return self.id
class PreciousFile(TreeFile):
def __init__(self, name, tagged):
TreeFile.__init__(self, name, tagged, "P")
class BackupFile(TreeFile):
def __init__(self, name, tagged):
TreeFile.__init__(self, name, tagged, "B")
class JunkFile(TreeFile):
def __init__(self, name, tagged):
TreeFile.__init__(self, name, tagged, "J")
class UnrecognizedFile(TreeFile):
def __init__(self, name, tagged):
TreeFile.__init__(self, name, tagged, "U")
class TreeRootFile(TreeFile):
def __init__(self, name, tagged):
TreeFile.__init__(self, name, tagged, "T")
category_letters = {
"S" : SourceFile,
"P" : PreciousFile,
"B" : BackupFile,
"J" : JunkFile,
"U" : UnrecognizedFile,
"T" : TreeRootFile
}
non_root = (SourceFile, PreciousFile, BackupFile, JunkFile, UnrecognizedFile)
def file_classifier(iter):
for line in iter:
segment = line.split()
tagged = len(segment[0]) == 1 or segment[0][1] !="?"
name = segment[1]
if segment[0][0] == "S":
file_id = segment[2]
yield SourceFile(name, tagged, file_id)
else:
yield category_letters[segment[0][0]](name, tagged)
def iter_inventory_filter(tree, dirs, tagged=None, categories=None,
control_files=False):
for my_file in file_classifier(iter_all_inventory(tree, dirs,
control_files)):
if tagged is not None:
if tagged != my_file.tagged:
continue
if categories is None:
yield my_file
else:
for category in categories:
if isinstance(my_file, category):
yield my_file
break
def file_line(my_file, category=True, untagged=True, filename=True, id=False):
line = ""
if category:
line += my_file.letter
if untagged:
if my_file.tagged:
line +=" "
else:
line +="?"
if filename:
if len(line) > 0:
line +=" "
line += my_file.name
if id:
if len(line) > 0:
line +="\t"
if my_file.get_id() is not None:
line += my_file.get_id()
else:
line += "???"
return line
def get_id_filename_map(tree):
map = {}
for listing in iter_inventory_filter(tree, (tree,), categories=(SourceFile,),
control_files=True):
map[listing.id] = listing.name
return map
def file_find(my_file, revision=None):
"""Returns the path to the file in the revision.
:param my_file: the file in the working tree to find in the revision
:type my_file: str
:param revision: The revision to look in
:type revision: `arch.Revision`, str, or NoneTye
"""
args=["file-find", my_file]
if revision is not None:
args.append(str(revision))
for line in arch.classify_chatter(sequence_cmd(args)):
if isinstance (line, arch.Chatter):
continue
return line
class LintProblem:
def __init__(self, filename):
self.filename = filename
def __str__(self):
return filename
class BrokenSymlink(LintProblem):
def __init__(self, filename):
LintProblem.__init__(self, filename)
class UnrecognizedLintFile(LintProblem):
def __init__(self, filename):
LintProblem.__init__(self, filename)
class UnusedID(LintProblem):
def __init__(self, filename):
LintProblem.__init__(self, filename)
class DuplicateID(LintProblem):
def __init__(self, id, files):
self.id = id
self.files = files
def __str__(self):
output = ""
did_id = False
for file in self.files:
output += file
if not did_id:
output +="\t%s" % self.id
did_id = True
output += "\n"
return output
# arch-tag: 07e2fed5-d68a-4272-95d9-65450a4f9774
|