forked from duplys/git-issues
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-issues
executable file
·1206 lines (982 loc) · 39.2 KB
/
git-issues
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
# git-issue, version 0.3
#
# by John Wiegley <johnw@newartisans.com>
# TODO: (until I can add these bugs to the repo itself!)
#
# 1. use utf-8 throughout
# 2. use -z flag for ls-tree
# 3. use UTC throughout
import platform
from os.path import split, join, exists, dirname
from os import getcwd, makedirs, execv
from shutil import copy
from sys import argv
if platform.system() == "Windows":
resolvedLink = None
else:
from os import readlink
try:
resolvedLink = readlink(__file__)
except:
resolvedLink = None
if resolvedLink and resolvedLink[0] != "/":
resolvedLink = join(dirname(__file__), resolvedLink)
if resolvedLink:
#print "Symlink found, using %s instead" % resolvedLink
execv(resolvedLink, [resolvedLink] + argv[1:])
path = getcwd()
if ".gitissues" not in __file__:
while not exists(join(path,".gitissues")):
path,extra = split(path)
if not extra:
break
issuesExec = join(path,".gitissues/git-issues")
if exists(issuesExec):
#print "git-issues found in %s. Using it in place of the one in %s" % (issuesExec, __file__)
execv(issuesExec, [issuesExec]+ argv[1:])
assert ("This should never be called" and False)
import sys
import os
import re
import optparse
import gitshelve
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
import cPickle
from datetime import datetime
from subprocess import Popen, PIPE
from os.path import isdir, isfile, join, basename
from tempfile import mkstemp
######################################################################
iso_fmt = "%Y%m%dT%H%M%S"
options = None
cache_version = 11
######################################################################
# You may wonder what dirtiness means below. Here's the deal:
#
# An object is "self-dirty" if it itself has been changed, but possibly none
# of its children. An object is "dirty" if its children have been changed,
# but possibly not itself. Any dirtiness is cause for rewriting the object
# cache; only self-dirtiness of specific objects will cause the repository to
# be updated.
class Person:
def __init__(self, name, email):
self.name = name
self.email = email
def __str__(self):
return "%s <%s>" % (self.name, self.email)
class Comment:
def __init__(self, issue, author, comment):
self.name = None
self.issue = issue
self.author = author
self.comment = comment
self.created = datetime.now()
self.modified = None
self.self_dirty = True
self.attachments = [] # records filename and blob
self.issue.comments[self.get_name()] = self # register into issue
def mark_dirty(self):
self.modified = datetime.now()
self.self_dirty = True
self.issue.mark_dirty(self_dirty = False)
def __getstate__(self):
odict = self.__dict__.copy() # copy the dict since we change it
del odict['self_dirty'] # remove self dirty flag
return odict
def __setstate__(self, dict):
self.__dict__.update(dict) # update attributes
self.self_dirty = False
class Issue:
def __init__(self, issueSet, author, title,
summary = None,
description = None,
reporters = [],
owners = [],
assigned = None,
carbons = [],
status = "new",
resolution = None,
issue_type = "defect",
components = [],
version = None,
milestone = None,
severity = "major",
priority = "medium",
tags = []):
self.issueSet = issueSet
self.name = None
self.author = author
self.title = title
self.summary = summary
self.description = description
self.reporters = reporters
self.owners = owners
self.assigned = assigned
self.carbons = carbons
self.status = status
self.resolution = resolution
self.issue_type = issue_type
self.components = components
self.version = version
self.milestone = milestone
self.severity = severity
self.priority = priority
self.tags = tags
self.created = datetime.now()
self.modified = None
self.changes = {}
self.dirty = False
self.self_dirty = True
self.comments = {}
def mark_dirty(self, self_dirty):
self.dirty = True
if self_dirty:
self.self_dirty = True
self.modified = datetime.now()
self.issueSet.mark_dirty(self_dirty = False)
def get_name(self):
assert False
def note_change(self, field, before, after):
if self.changes.has_key(field):
data = self.changes[field]
data[1] = after
else:
data = [before, after]
self.changes[field] = data
def set_author(self, author):
self.note_change('author', self.author, author)
self.author = author
def set_title(self, title):
self.note_change('title', self.title, title)
self.title = title
def set_summary(self, summary):
self.note_change('summary', self.summary, summary)
self.summary = summary
def set_description(self, description):
self.note_change('description', self.description, description)
self.description = description
def set_reporters(self, reporters):
self.note_change('reporters', self.reporters, reporters)
self.reporters = reporters
def set_owners(self, owners):
self.note_change('owners', self.owners, owners)
self.owners = owners
def set_assigned(self, assigned):
self.note_change('assigned', self.assigned, assigned)
self.assigned = assigned
def set_carbons(self, carbons):
self.note_change('carbons', self.carbons, carbons)
self.carbons = carbons
def set_status(self, status):
self.note_change('status', self.status, status)
self.status = status
def set_resolution(self, resolution):
self.note_change('resolution', self.resolution, resolution)
self.resolution = resolution
def set_issue_type(self, issue_type):
self.note_change('type', self.issue_type, issue_type)
self.issue_type = issue_type
def set_components(self, components):
self.note_change('components', self.components, components)
self.components = components
def set_version(self, version):
self.note_change('version', self.version, version)
self.version = version
def set_milestone(self, milestone):
self.note_change('milestone', self.milestone, milestone)
self.milestone = milestone
def set_severity(self, severity):
self.note_change('severity', self.severity, severity)
self.severity = severity
def set_priority(self, priority):
self.note_change('priority', self.priority, priority)
self.priority = priority
def set_tags(self, tags):
self.note_change('tags', self.tags, tags)
self.tags = tags
def __getstate__(self):
odict = self.__dict__.copy() # copy the dict since we change it
del odict['changes'] # remove change log
del odict['dirty'] # remove dirty flag
del odict['self_dirty'] # remove self dirty flag
return odict
def __setstate__(self,dict):
self.__dict__.update(dict) # update attributes
self.changes = {}
self.dirty = False
self.self_dirty = False
class IssueSet:
"""An IssueSet refers to a group of issues. There is always at least one
IssueSet that refers to all of the issues which exist in a repository.
Other IssueSet's can be generated from that one as "views" or queries into
that data.
In essence, it contains both a set of Issue's which can be looked up by
their unique identifier, and also certain global definition, like the
allowable components, etc."""
def __init__(self, shelf):
self.shelf = shelf
self.statuses = []
self.resolutions = []
self.issue_types = []
self.components = []
self.versions = []
self.milestones = []
self.severities = []
self.priorities = []
self.dirty = False
self.self_dirty = True
self.cache_version = cache_version
self.created = datetime.now()
self.modified = None
def mark_dirty(self, self_dirty):
self.dirty = True
if self_dirty:
self.modified = datetime.now()
self.self_dirty = True
def current_author(self):
assert False
def allocate_issue(self, title):
assert False
def new_issue(self, title):
issue = self.allocate_issue(title)
self.add_issue(issue)
return issue
def new_comment(self, issue, text):
comment = self.allocate_comment(issue, text)
self.add_comment(comment)
return comment
def comment_path(self, comment):
name = comment.issue.get_name()
return "%s/%s/comment_%s_%s_%s.xml" %(name[:2],
name[2:],
comment.name,
datetime.now().isoformat(),
comment.comment)
def issue_path(self, issue):
name = issue.get_name()
return '%s/%s/issue.xml' % (name[:2], name[2:])
def add_issue(self, issue):
self.shelf[self.issue_path(issue)] = issue
self.mark_dirty(self_dirty = False)
def add_comment(self, comment):
self.shelf[self.comment_path(comment)] = comment
self.mark_dirty(self_dirty = False)
def get_comment(self, idx_or_partial_hash):
comment = None
try:
idx = int(idx_or_partial_hash) - 1
comment = self.shelf[self.shelf.keys()[idx]]
except:
print [key for key in self.shelf.iterkeys()]
def getCommentId(x):
if not 'comment_' in x:
return ""
x = x.split('comment_')[1]
x = x.split('_')[0]
return x
clean = lambda x: getCommentId(x)
matching = [(clean(key), key) for key in self.shelf.iterkeys()
if clean(key).startswith(idx_or_partial_hash) and not "issue.xml" in clean(key)]
if len(matching) == 0:
pass
elif len(matching) == 1:
comment = self.shelf[matching[0][1]]
else:
print ("Ambiguous hash matches:\n" +
'\t\n'.join(a[0] for a in matching))
if not comment:
raise Exception("There is no issue matching the identifier '%s'.\n" %
idx_or_partial_hash)
return comment
def __getitem__(self, idx_or_partial_hash):
issue = None
try:
idx = int(idx_or_partial_hash) - 1
issue = self.shelf[self.shelf.keys()[idx]]
except:
clean = lambda x: x.replace('issue.xml', '').replace('/','')
matching = [(clean(key), key) for key in self.shelf.iterkeys()
if clean(key).startswith(idx_or_partial_hash) and not "comment" in clean(key)]
if len(matching) == 0:
pass
elif len(matching) == 1:
issue = self.shelf[matching[0][1]]
else:
print ("Ambiguous hash matches:\n" +
'\t\n'.join(a[0] for a in matching))
if not issue:
raise Exception("There is no issue matching the identifier '%s'.\n" %
idx_or_partial_hash)
return issue
def __delitem__(self, idx_or_partial_hash):
del self.shelf[None] # jww (2008-05-14): NYI
assert False
def issues_cache_file(self):
assert False
def load_state(self):
"""Given a newly created IssueSet object as a template, see if we can
restore the cached version of the data from disk, and then check whether
it's still valid. This can _greatly_ speed up subsequent list and show
operations.
The reason why a newly created template exists is to abstract
DVCS-specific behavior, such as the location of the cache file.
Thus, a typical session looks like this:
issueSet = GitIssueSet()
if ... looking at issues list is required ...:
issueSet = issueSet.load_state()
... use the issue data ..."""
cache_file = self.issues_cache_file()
if isfile(cache_file):
fd = open(cache_file, 'rb')
if options.verbose:
print "Cache: Loading saved issues data"
try:
cachedIssueSet = cPickle.load(fd)
finally:
fd.close()
if cachedIssueSet.cache_version == self.cache_version:
if options.verbose:
print "Cache: It is valid and usable"
return cachedIssueSet
if options.verbose:
print "Cache: No longer valid, throwing it away"
# We can't use or rely on the cache, so read all details from disk and
# then mark the IssueSet dirty so that it gets saved back again when
# we exit.
try:
return object_from_string(self.shelf['project.xml'])
except:
return self
def save_state(self):
"""Write an IssueSet to disk in object form, for fast loading on the next
iteration. This is only done if there are actual changes to write."""
if not self.dirty:
return
self.shelf.sync()
cache_file = self.issues_cache_file()
cache_file_dir = os.path.dirname(cache_file)
if not isdir(cache_file_dir):
os.makedirs(cache_file_dir)
fd = open(cache_file, 'wb')
try:
cPickle.dump(issueSet, fd)
finally:
fd.close()
self.dirty = False
######################################################################
import xml.dom.minidom
def read_object(obj, file_descriptor):
return XmlReader.read(file_descriptor)
def object_from_string(str):
return XmlReader.readString(str)
class XmlReader:
def read(cls, fd):
doc = xml.dom.minidom.parse(fd)
data = XmlRipper.rip(doc.firstChild)
doc.unlink()
return data
read = classmethod(read)
def readString(cls, data):
doc = xml.dom.minidom.parseString(data)
data = XmlRipper.rip(doc.firstChild)
doc.unlink()
return data
readString = classmethod(readString)
class XmlStringRipper:
def rip(cls, node):
return node.data[1:-1]
rip = classmethod(rip)
class XmlListRipper:
def rip(cls, node):
assert False
rip = classmethod(rip)
class XmlDateTimeRipper:
def rip(cls, node):
return datetime.strptime(node.childNodes[0].data[1:-1], iso_fmt)
rip = classmethod(rip)
class XmlPersonRipper:
def rip(cls, node):
person = Person(node.childNodes[1].childNodes[0].data[1:-1],
node.childNodes[3].childNodes[0].data[1:-1])
return person
rip = classmethod(rip)
class XmlIssueRipper:
def rip(cls, node):
created = XmlRipper.rip(node.childNodes[1].childNodes[1])
author = XmlRipper.rip(node.childNodes[3].childNodes[1])
title = XmlRipper.rip(node.childNodes[5].firstChild)
issue = Issue(None, author, title)
issue.created = created
issue.dirty = False
return issue
rip = classmethod(rip)
class XmlIssueSetRipper:
pass
class XmlRipper:
def rip(cls, node):
if node.nodeType == xml.dom.minidom.Node.TEXT_NODE:
return XmlStringRipper.rip(node)
elif node.nodeName == 'datetime':
return XmlDateTimeRipper.rip(node)
elif node.nodeName == 'person':
return XmlPersonRipper.rip(node)
elif node.nodeName == 'list':
return XmlListRipper.rip(node)
elif node.nodeName == 'issue':
return XmlIssueRipper.rip(node)
elif node.nodeName == 'issue-set':
return XmlIssueSetRipper.rip(node)
else:
print node.nodeType
print node.nodeName
assert False
rip = classmethod(rip)
######################################################################
def write_object(obj, file_descriptor = sys.stdout):
XmlWriter.write(XmlBuilder.build(obj), fd = file_descriptor)
def object_to_string(obj):
buffer = StringIO()
XmlWriter.write(XmlBuilder.build(obj), fd = buffer)
return buffer.getvalue()
class XmlWriter:
def write(cls, doc, no_header = False, fd = sys.stdout):
if no_header:
buffer = StringIO()
buffer.write(doc.toprettyxml(indent = "", encoding = "utf-8"))
fd.write(re.sub('^.+\n', '', buffer.getvalue()))
else:
fd.write(doc.toprettyxml(indent = "", encoding = "utf-8"))
doc.unlink()
write = classmethod(write)
class XmlStringBuilder:
def build(cls, data, node, doc):
node.appendChild(doc.createTextNode(data))
build = classmethod(build)
class XmlListBuilder:
def build(cls, data, node, doc):
element = doc.createElement("list")
for child in data:
XmlBuilder.build(doc, element, child)
node.appendChild(element)
build = classmethod(build)
class XmlDateTimeBuilder:
def build(cls, data, node, doc):
element = doc.createElement("datetime")
element.appendChild(doc.createTextNode(data.strftime(iso_fmt)))
node.appendChild(element)
build = classmethod(build)
class XmlPersonBuilder:
def build(cls, data, node, doc):
person = doc.createElement("person")
name = doc.createElement("name")
name.appendChild(doc.createTextNode(data.name))
person.appendChild(name)
email = doc.createElement("email")
email.appendChild(doc.createTextNode(data.email))
person.appendChild(email)
node.appendChild(person)
build = classmethod(build)
class XmlIssueBuilder:
def build(cls, issue, node, doc):
issueNode = doc.createElement("issue")
created = doc.createElement("created")
XmlBuilder.build(issue.created, created, doc)
issueNode.appendChild(created)
author = doc.createElement("author")
XmlBuilder.build(issue.author, author, doc)
issueNode.appendChild(author)
title = doc.createElement("title")
XmlBuilder.build(issue.title, title, doc)
issueNode.appendChild(title)
summary = doc.createElement("summary")
XmlBuilder.build(issue.summary, summary, doc)
issueNode.appendChild(summary)
description = doc.createElement("description")
XmlBuilder.build(issue.description, description, doc)
issueNode.appendChild(description)
reporters = doc.createElement("reporters")
XmlBuilder.build(issue.reporters, reporters, doc)
issueNode.appendChild(reporters)
owners = doc.createElement("owners")
XmlBuilder.build(issue.owners, owners, doc)
issueNode.appendChild(owners)
assigned = doc.createElement("assigned")
XmlBuilder.build(issue.assigned, assigned, doc)
issueNode.appendChild(assigned)
carbons = doc.createElement("carbons")
XmlBuilder.build(issue.carbons, carbons, doc)
issueNode.appendChild(carbons)
status = doc.createElement("status")
XmlBuilder.build(issue.status, status, doc)
issueNode.appendChild(status)
resolution = doc.createElement("resolution")
XmlBuilder.build(issue.resolution, resolution, doc)
issueNode.appendChild(resolution)
issue_type = doc.createElement("type")
XmlBuilder.build(issue.issue_type, issue_type, doc)
issueNode.appendChild(issue_type)
components = doc.createElement("components")
XmlBuilder.build(issue.components, components, doc)
issueNode.appendChild(components)
version = doc.createElement("version")
XmlBuilder.build(issue.version, version, doc)
issueNode.appendChild(version)
milestone = doc.createElement("milestone")
XmlBuilder.build(issue.milestone, milestone, doc)
issueNode.appendChild(milestone)
severity = doc.createElement("severity")
XmlBuilder.build(issue.severity, severity, doc)
issueNode.appendChild(severity)
priority = doc.createElement("priority")
XmlBuilder.build(issue.priority, priority, doc)
issueNode.appendChild(priority)
tags = doc.createElement("tags")
XmlBuilder.build(issue.tags, tags, doc)
issueNode.appendChild(tags)
modified = doc.createElement("modified")
XmlBuilder.build(issue.modified, modified, doc)
issueNode.appendChild(modified)
node.appendChild(issueNode)
build = classmethod(build)
class XmlCommentBuilder:
def build(cls, comment, node, doc):
commentNode = doc.createElement("comment")
created = doc.createElement("created")
XmlBuilder.build(comment.created, created, doc)
commentNode.appendChild(created)
author = doc.createElement("author")
XmlBuilder.build(comment.author, author, doc)
commentNode.appendChild(author)
commentText = doc.createElement("comment")
XmlBuilder.build(comment.comment, commentText, doc)
commentNode.appendChild(commentText)
node.appendChild(commentNode)
build = classmethod(build)
#class XmlIssueChangesBuilder:
# def build(cls, data, node, doc):
# changes = doc.createElement("changes")
# doc.appendChild(changes)
#
# for field_name in self.changes.keys():
# field = doc.createElement("field")
# field.setAttribute("name", field_name)
#
# data = self.changes[field_name]
#
# before = doc.createElement("before")
# XmlBuilder.build(data[0], before, doc)
# field.appendChild(before)
#
# after = doc.createElement("after")
# XmlBuilder.build(data[1], after, doc)
# field.appendChild(after)
#
# changes.appendChild(field)
#
# node.appendChild(changes)
#
# build = classmethod(build)
class XmlIssueSetBuilder:
def build(cls, issueSet, node, doc):
set = doc.createElement("issue-set")
created = doc.createElement("created")
XmlBuilder.build(issueSet.created, created, doc)
set.appendChild(created)
statuses = doc.createElement("statuses")
XmlBuilder.build(issueSet.statuses, statuses, doc)
set.appendChild(statuses)
resolutions = doc.createElement("resolutions")
XmlBuilder.build(issueSet.resolutions, resolutions, doc)
set.appendChild(resolutions)
issue_types = doc.createElement("types")
XmlBuilder.build(issueSet.issue_types, issue_types, doc)
set.appendChild(issue_types)
components = doc.createElement("components")
XmlBuilder.build(issueSet.components, components, doc)
set.appendChild(components)
versions = doc.createElement("versions")
XmlBuilder.build(issueSet.versions, versions, doc)
set.appendChild(versions)
milestones = doc.createElement("milestones")
XmlBuilder.build(issueSet.milestones, milestones, doc)
set.appendChild(milestones)
severities = doc.createElement("severities")
XmlBuilder.build(issueSet.severities, severities, doc)
set.appendChild(severities)
priorities = doc.createElement("priorities")
XmlBuilder.build(issueSet.priorities, priorities, doc)
set.appendChild(priorities)
modified = doc.createElement("modified")
XmlBuilder.build(issueSet.modified, modified, doc)
set.appendChild(modified)
node.appendChild(set)
build = classmethod(build)
class XmlBuilder:
def build(cls, data, node = None, doc = None):
if data is None:
pass
elif isinstance(data, datetime):
assert doc
XmlDateTimeBuilder.build(data, node, doc)
elif isinstance(data, Person):
assert doc
XmlPersonBuilder.build(data, node, doc)
elif isinstance(data, list):
assert doc
XmlListBuilder.build(data, node, doc)
elif isinstance(data, str):
assert doc
XmlStringBuilder.build(data, node, doc)
elif isinstance(data, Issue):
assert not doc
doc = xml.dom.minidom.Document()
XmlIssueBuilder.build(data, doc, doc)
elif isinstance(data, IssueSet):
assert not doc
doc = xml.dom.minidom.Document()
XmlIssueSetBuilder.build(data, doc, doc)
elif isinstance(data, Comment):
assert not doc
doc = xml.dom.minidom.Document()
XmlCommentBuilder.build(data, doc, doc)
else:
print "Unknown type %s" % data
assert False
return doc
build = classmethod(build)
######################################################################
class GitIssue(Issue):
def get_name(self):
if not self.name:
hash_func = self.issueSet.shelf.hash_blob
name = hash_func(str(self.created) + str(self.author) +
self.title)
self.name = name
return self.name
class GitComment(Comment):
def get_name(self):
if not self.name:
hash_func = self.issue.issueSet.shelf.hash_blob
name = hash_func(str(self.created)
+ str(self.author)
+ self.comment)
self.name = name
return self.name
class xml_gitbook(gitshelve.gitbook):
def serialize_data(self, data):
return object_to_string(data)
def deserialize_data(self, data):
return object_from_string(data)
class GitIssueSet(IssueSet):
"""This object implements all the command necessary to interact with Git
for the purpose of storing and distributing issues."""
def __init__(self):
self.GIT_DIR = None
self.GIT_AUTHOR = None
IssueSet.__init__(self, gitshelve.open('issues',
book_type = xml_gitbook))
def git_directory(self):
if self.GIT_DIR is None:
self.GIT_DIR = gitshelve.git('rev-parse', '--git-dir')
return self.GIT_DIR
def issues_cache_file(self):
return join(self.git_directory(), "issues")
def current_author(self):
if self.GIT_AUTHOR is None:
self.GIT_AUTHOR = Person(gitshelve.git('config', 'user.name'),
gitshelve.git('config', 'user.email'))
return self.GIT_AUTHOR
def allocate_issue(self, title):
return GitIssue(self, self.current_author(), title)
def allocate_comment(self, issue, commentText):
return GitComment(issue, self.current_author(), commentText)
######################################################################
def format_long_text(text, indent = 13):
if not text:
return "<none>"
lines = text.split('\n')
buffer = StringIO()
first = True
for line in lines:
if not first:
buffer.write("\n%s" % (" " * indent))
else:
first = False
buffer.write(line)
return buffer.getvalue()
def format_people_list(people, indent = 13):
if not people:
return "<no one yet>"
buffer = StringIO()
first = True
for person in people:
if not first:
buffer.write(",\n%s" % (" " * indent))
else:
first = False
buffer.write(person)
return buffer.getvalue()
def terminal_width():
"""Return terminal width."""
width = 0
try:
import struct, fcntl, termios
s = struct.pack('HHHH', 0, 0, 0, 0)
x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
width = struct.unpack('HHHH', x)[1]
except:
pass
if width <= 0:
if os.environ.has_key("COLUMNS"):
width = int(os.getenv("COLUMNS"))
if width <= 0:
width = 80
return width
######################################################################
parser = optparse.OptionParser(usage="""Usage: git-issues [options] <command> [command-options]
Commands:
init Creates a copy of git-issues repository in .gitissues in the
current git repository.
list Lists tickets for this repository
new Creates a new ticket for this repository
show/dump Shows the given ticket
change Change options for the given ticket
edit edit options for the given ticket in text editor
comment Add a comment to the given ticket
close Close the given ticket""")
parser.add_option("-v", "--verbose",
action = "store_true",
dest = "verbose",
default = False,
help = "report activity options.verbosely")
parser.add_option("--print-new-bugs",
action = "store_true",
dest = "printNewBugs",
default = False,
help = "prints out a formatted string with the bug summary and id. Usuful for in editor usage.")
parser.add_option("--filter-status",
dest="filterStatus",
default="closed",
help = """do not print a issue if it is in one of
the stati specified (column separated) by this option.""".replace("\n",""))
parser.add_option("--filter-tags",
dest="filterTags",
default="",
help = """Prints only the issues with one of the following
tags (column separated) associated to it.""")
parser.add_option("--screen-width",
dest="screenWidth",
default=terminal_width(),
help = "Width of the terminal we are printing to.")
parser.add_option("--status",
dest="status",
default=None,
metavar="STATUS",
help="Set the status of the issue to STATUS when creating it.")
(options, args) = parser.parse_args()
gitshelve.verbose = options.verbose
######################################################################
def inputFromEditor(originalText):
fd, tempFile = mkstemp()
f = open(tempFile,"w")
f.write(originalText or "")
f.close()
defaultEditor = "vi"
if platform.system() == "Windows":
defaultEditor = "notepad"
if os.environ.has_key("VISUAL"):
defaultEditor = os.getenv("VISUAL")
elif os.environ.has_key("EDITOR"):
defualtEditor = os.getenv("EDITOR")
editCommand = "%s %s" % (defaultEditor, tempFile)
if os.system(editCommand) != 0:
os.unlink(tempFile)
print "Error while executing %s" % editCommand
sys.exit(1)
contents = open(tempFile).read()
os.unlink(tempFile)
return contents
if __name__ == '__main__':
if len(args) == 0:
parser.print_help()