|
1 ''' |
|
2 Copyright 2015 Teemu Piippo |
|
3 All rights reserved. |
|
4 |
|
5 Redistribution and use in source and binary forms, with or without |
|
6 modification, are permitted provided that the following conditions |
|
7 are met: |
|
8 |
|
9 1. Redistributions of source code must retain the above copyright |
|
10 notice, this list of conditions and the following disclaimer. |
|
11 2. Redistributions in binary form must reproduce the above copyright |
|
12 notice, this list of conditions and the following disclaimer in the |
|
13 documentation and/or other materials provided with the distribution. |
|
14 3. The name of the author may not be used to endorse or promote products |
|
15 derived from this software without specific prior written permission. |
|
16 |
|
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
27 ''' |
|
28 |
|
29 import subprocess |
|
30 import sys |
|
31 import os |
|
32 from configfile import Config |
|
33 |
|
34 class HgRepository (object): |
|
35 def __init__ (self, reponame): |
|
36 reponame = reponame.lower() |
|
37 repoconfig = Config.get_node ('hg').get_node ('repos') |
|
38 repoinfo = repoconfig.get_node (reponame) |
|
39 |
|
40 if not repoinfo: |
|
41 raise ValueError ('Unknown repository "%s"' % reponame) |
|
42 |
|
43 self.name = reponame |
|
44 self.published = not bool (repoinfo.get_value ('extrarepo', default=False)) |
|
45 self.url = repoinfo.get_value ('url') |
|
46 self.color = int (repoinfo.get_value ('colorcode', default=0)) |
|
47 |
|
48 if not self.url: |
|
49 raise ValueError ('Repository %s has no url!' % reponame) |
|
50 |
|
51 def hg (self, *args): |
|
52 output = subprocess.check_output (['hg', '--cwd', self.name] + list (args)) |
|
53 |
|
54 if sys.version_info >= (3, 0): |
|
55 output = output.decode ('utf-8', 'ignore') |
|
56 |
|
57 return output |
|
58 |
|
59 def is_valid (self): |
|
60 return os.path.isdir (os.path.join (self.name, '.hg')) |
|
61 |
|
62 def split_template (self, kvargs, separator): |
|
63 return separator.join (['{%s}' % x[1] for x in kvargs.items()]) |
|
64 |
|
65 def merge_template (self, data, args): |
|
66 result = {} |
|
67 items = list (args.items()) |
|
68 assert (len (items) == len (data)), '''Bad amount of items from hg log''' |
|
69 |
|
70 for i in range (0, len (items)): |
|
71 result[items[i][0]] = data[i] |
|
72 |
|
73 return result |
|
74 |
|
75 def get_commit_data (self, rev, **kvargs): |
|
76 if not kvargs: |
|
77 raise ValueError ('template arguments must be provided') |
|
78 |
|
79 separator = '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^' |
|
80 template = self.split_template (kvargs, separator) |
|
81 data = self.hg ('log', '--limit', '1', '--rev', rev, '--template', template) |
|
82 data = data.split (separator) |
|
83 return self.merge_template (data, kvargs) |
|
84 |
|
85 def incoming (self, maxcommits=0, **kvargs): |
|
86 if not kvargs: |
|
87 raise ValueError ('template arguments must be provided') |
|
88 |
|
89 commit_data = [] |
|
90 separator = '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^' |
|
91 separator2 = '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@' |
|
92 template = self.split_template (kvargs, separator) |
|
93 limit = ['--limit', str(maxcommits)] if maxcommits else [] |
|
94 |
|
95 try: |
|
96 args = ['incoming'] + limit + ['--quiet', '--template', template + separator2] |
|
97 data = self.hg (*args) |
|
98 data = data.split (separator2) |
|
99 |
|
100 if not data[-1]: |
|
101 data = data[:-1] |
|
102 |
|
103 return [self.merge_template (data=x.split (separator), args=kvargs) for x in data] |
|
104 except subprocess.CalledProcessError: |
|
105 return [] |
|
106 |
|
107 def clone(): |
|
108 print ('Cloning %s...' % repo.name) |
|
109 subprocess.call (['hg', 'clone', repo.url, repo.name]) |