hgrepo.py

Sun, 16 Aug 2015 23:30:11 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 16 Aug 2015 23:30:11 +0300
changeset 154
df862cca1773
parent 152
1b734faab67a
child 162
d24fe5e3e420
permissions
-rw-r--r--

Added ability to define an IRC command with a function decorator instead of a manifest entry

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

mercurial