Mon, 05 Oct 2015 23:35:44 +0300
Calculator now supports variables
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 | 35 | def __init__ (self, name): |
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 | 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 | 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 | 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 | 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 | 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 | 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 | 73 | try: |
74 | if not kvargs: | |
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 | 77 | separator = '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^' |
78 | template = self.split_template (kvargs, separator) | |
79 | data = self.hg ('log', '--limit', '1', '--rev', rev, '--template', template) | |
80 | data = data.split (separator) | |
81 | return self.merge_template (data, kvargs) | |
82 | except subprocess.CalledProcessError: | |
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 | 107 | def clone (self): |
108 | print ('Cloning %s from %s...' % (self.name, self.url)) | |
109 | subprocess.call (['hg', 'clone', self.url, self.name]) |