30 import sys |
30 import sys |
31 import os |
31 import os |
32 from configfile import Config |
32 from configfile import Config |
33 |
33 |
34 class HgRepository (object): |
34 class HgRepository (object): |
35 def __init__ (self, reponame): |
35 def __init__ (self, name): |
36 reponame = reponame.lower() |
36 name = name.lower() |
37 repoconfig = Config.get_node ('hg').get_node ('repos') |
37 repoconfig = Config.get_node ('hg').get_node ('repos') |
38 repoinfo = repoconfig.get_node (reponame) |
38 repoinfo = repoconfig.get_node (name) |
39 |
39 |
40 if not repoinfo: |
40 if not repoinfo: |
41 raise ValueError ('Unknown repository "%s"' % reponame) |
41 raise ValueError ('Unknown repository "%s"' % name) |
42 |
42 |
43 self.name = reponame |
43 self.name = name |
44 self.published = not bool (repoinfo.get_value ('extrarepo', default=False)) |
44 self.published = not bool (repoinfo.get_value ('extrarepo', default=False)) |
45 self.url = repoinfo.get_value ('url') |
45 self.url = repoinfo.get_value ('url') |
46 self.version = repoinfo.get_value ('version', default='') |
46 self.version = repoinfo.get_value ('version', default='') |
47 self.color = int (repoinfo.get_value ('colorcode', default=0)) |
47 self.color = int (repoinfo.get_value ('colorcode', default=0)) |
48 |
48 |
49 if not self.url: |
49 if not self.url: |
50 raise ValueError ('Repository %s has no url!' % reponame) |
50 raise ValueError ('Repository %s has no url!' % name) |
51 |
51 |
52 def hg (self, *args): |
52 def hg (self, *args): |
53 output = subprocess.check_output (['hg', '--cwd', self.name] + list (args)) |
53 output = subprocess.check_output (['hg', '--cwd', self.name] + list (args)) |
54 |
54 return output.decode ('utf-8', 'ignore') |
55 if sys.version_info >= (3, 0): |
|
56 output = output.decode ('utf-8', 'ignore') |
|
57 |
|
58 return output |
|
59 |
55 |
60 def is_valid (self): |
56 def is_valid (self): |
61 return os.path.isdir (os.path.join (self.name, '.hg')) |
57 return os.path.isdir (os.path.join (self.name, '.hg')) |
62 |
58 |
63 def split_template (self, kvargs, separator): |
59 def split_template (self, kvargs, separator): |
72 result[items[i][0]] = data[i] |
68 result[items[i][0]] = data[i] |
73 |
69 |
74 return result |
70 return result |
75 |
71 |
76 def get_commit_data (self, rev, **kvargs): |
72 def get_commit_data (self, rev, **kvargs): |
77 if not kvargs: |
73 try: |
78 raise ValueError ('template arguments must be provided') |
74 if not kvargs: |
|
75 raise ValueError ('template arguments must be provided') |
79 |
76 |
80 separator = '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^' |
77 separator = '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^' |
81 template = self.split_template (kvargs, separator) |
78 template = self.split_template (kvargs, separator) |
82 data = self.hg ('log', '--limit', '1', '--rev', rev, '--template', template) |
79 data = self.hg ('log', '--limit', '1', '--rev', rev, '--template', template) |
83 data = data.split (separator) |
80 data = data.split (separator) |
84 return self.merge_template (data, kvargs) |
81 return self.merge_template (data, kvargs) |
|
82 except subprocess.CalledProcessError: |
|
83 raise ValueError ('''couldn't find changeset %s in %s''' % (node, repo.name)) |
85 |
84 |
86 def incoming (self, maxcommits=0, **kvargs): |
85 def incoming (self, maxcommits=0, **kvargs): |
87 if not kvargs: |
86 if not kvargs: |
88 raise ValueError ('template arguments must be provided') |
87 raise ValueError ('template arguments must be provided') |
89 |
88 |
103 |
102 |
104 return [self.merge_template (data=x.split (separator), args=kvargs) for x in data] |
103 return [self.merge_template (data=x.split (separator), args=kvargs) for x in data] |
105 except subprocess.CalledProcessError: |
104 except subprocess.CalledProcessError: |
106 return [] |
105 return [] |
107 |
106 |
108 def clone(): |
107 def clone (self): |
109 print ('Cloning %s...' % repo.name) |
108 print ('Cloning %s from %s...' % (self.name, self.url)) |
110 subprocess.call (['hg', 'clone', repo.url, repo.name]) |
109 subprocess.call (['hg', 'clone', self.url, self.name]) |