Mercurial > hg > hq
annotate hq/main.py @ 14:46a68cd554b5
remove extraneous self
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 10 May 2013 09:23:40 -0700 |
parents | 42a1de8a0bd5 |
children | 6c4f258fae85 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
2 |
0 | 3 """ |
14 | 4 mercurial queue extension front-end |
0 | 5 """ |
6 | |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
7 # TODO: migrate to http://k0s.org/hg/CommandParser/ |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
8 |
0 | 9 import os |
10 import subprocess | |
11 import sys | |
12 | |
13 from command import CommandParser | |
14 | |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
15 call = subprocess.check_output |
0 | 16 |
17 class HQ(object): | |
18 """ | |
19 mercurial queue extension front-end policy manager | |
20 """ | |
21 | |
11 | 22 def __init__(self, network=True, root=None, binary='hg'): |
0 | 23 """initialize global options""" |
24 # TODO: look at hgrc file | |
1 | 25 # for [defaults] repository_host |
5
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
26 # XXX ??? |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
27 |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
28 # check for network |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
29 self.network = network |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
30 |
12 | 31 # repository root |
32 self.root = root or call(['hg', 'root']).strip() | |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
33 |
12 | 34 # hg binary |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
35 self.binary = binary |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
36 |
13 | 37 # patch repo; not guaranteed to exit |
14 | 38 self._patch_repo = os.path.join(self.root, '.hg', 'patches') |
13 | 39 |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
40 ### subcommands |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
41 |
3 | 42 def clone(self, repo, patch=None, queue=None): |
43 """ | |
44 clone the repository and begin a patch queue | |
45 - path: name of a new patch to initiate | |
46 - queue: name of the remote queue | |
47 """ | |
0 | 48 directory = repo.rsplit('/', 1) |
49 call(['hg', 'clone', repo, directory]) | |
50 os.chdir(directory) | |
51 call(['hg', 'qinit', '-c']) | |
3 | 52 if queue: |
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
53 # pull from the given repository |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
54 self._patch_command(*['hg', 'pull', '--update', queue]) |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
55 else: |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
56 # (optionally) setup a new repo |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
57 pass # TODO |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
58 |
0 | 59 if patch: |
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
60 # create a new patch |
0 | 61 call(['hg', 'qnew', patch]) |
62 | |
63 def commit(self, message): | |
64 """ | |
65 commit a patch and push it to the master repository | |
5
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
66 - message : commit message |
0 | 67 """ |
68 call(['hg', 'qrefresh']) | |
69 call(['hg', 'qcommit', '-m', message]) | |
5
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
70 if self.network: |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
71 self._patch_command(*['hg', 'push']) |
0 | 72 |
73 def pull(self, repo=None): | |
12 | 74 """pull from the root repository""" |
0 | 75 # TODO: commit prior to realignment |
76 call(['hg', 'qpop', '--all']) | |
12 | 77 call(['hg', 'pull'] + (repo and [repo] or [])) |
0 | 78 call(['hg', 'qpush', '--all']) |
2
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
79 |
3 | 80 def goto(self, patch): |
81 """ | |
5
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
82 go to a specific patch and apply it |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
83 - patch: name of patch to go to |
3 | 84 """ |
85 # TODO | |
86 process = subprocess.Popen(['hg', 'qapplied'], stdout=subprocess.PIPE) | |
87 stdout, stderr = process.communicate() | |
88 applied = [ i.strip() for i in stdout.splitlines() | |
89 if i ] | |
90 raise NotImplementedError | |
91 | |
2
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
92 def files(self): |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
93 """ |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
94 list the files added by the top patch |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
95 """ |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
96 # TODO: should only list top-level directories, otherwise it's silly |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
97 _oldcwd = os.getcwd() |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
98 process = subprocess.Popen("hg qdiff | grep '^+++ ' | sed 's/+++ b\///'", stdout=subprocess.PIPE) |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
99 stdout, stderr = process.communicate() |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
100 return stdout |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
101 |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
102 def status(self): |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
103 """ |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
104 display status |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
105 """ |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
106 |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
107 # TODO: once this is on CommandParser, there should be a utility in |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
108 # command parser to allow aliases; e.g. if you do ``st = status`` at |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
109 # the class scope, you can run through the methods and only display |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
110 # one as canon but allow aliases as such. |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
111 # Alternatively, you can allow any short non-ambiguous abbreviation. |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
112 |
8 | 113 return '\n'.join([self._call(i).strip() for i in ('root', 'status', 'qseries')]) |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
114 |
9 | 115 def directory(self): |
13 | 116 """patch queue directory""" |
117 if os.path.isdir(self._patch_repo): | |
118 return self._patch_repo | |
9 | 119 |
7
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
120 ### internals |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
121 |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
122 def _call(self, *args): |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
123 command = [self.binary] + list(args) |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
124 return call(command, cwd=self.root) |
2e77fc6a36e8
add a status command; so much shit to clean
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
125 |
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
126 def _patch_command(self, *command): |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
127 """perform a command in the patch repository""" |
10 | 128 call(command, cwd=self.directory()) |
0 | 129 |
11 | 130 |
0 | 131 def main(args=sys.argv[1:]): |
132 parser = CommandParser(HQ) | |
133 options, args = parser.parse_args(args) | |
134 parser.invoke(args) | |
135 | |
136 if __name__ == '__main__': | |
137 main() |