Mercurial > hg > hq
annotate hq/main.py @ 5:448c248b3738
start the path to using the __init__ function for realz
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 26 May 2010 08:18:36 -0700 |
parents | 44ea39c3e98f |
children | 321721b581f1 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 """ | |
3 mercurial queue extension front-end policy manager | |
4 """ | |
5 | |
6 import os | |
7 import subprocess | |
8 import sys | |
9 | |
10 from command import CommandParser | |
11 | |
12 def call(command, *args, **kw): | |
13 code = subprocess.call(command, *args, **kw) | |
14 if code: | |
15 if isinstance(command, basestring): | |
16 cmdstr = command | |
17 else: | |
18 cmdstr = ' '.join(command) | |
19 raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code)) | |
20 | |
21 class HQ(object): | |
22 """ | |
23 mercurial queue extension front-end policy manager | |
24 """ | |
25 | |
5
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
26 def __init__(self, queue_host=None, network=True) |
0 | 27 """initialize global options""" |
28 # TODO: look at hgrc file | |
1 | 29 # 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
|
30 # XXX ??? |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
31 |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
32 # check for network |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
33 self.network = network |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
34 |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
35 self.queue_host = queue_host |
0 | 36 |
3 | 37 def clone(self, repo, patch=None, queue=None): |
38 """ | |
39 clone the repository and begin a patch queue | |
40 - path: name of a new patch to initiate | |
41 - queue: name of the remote queue | |
42 """ | |
0 | 43 directory = repo.rsplit('/', 1) |
44 call(['hg', 'clone', repo, directory]) | |
45 os.chdir(directory) | |
46 call(['hg', 'qinit', '-c']) | |
3 | 47 if queue: |
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
48 # pull from the given repository |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
49 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
|
50 else: |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
51 # (optionally) setup a new repo |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
52 pass # TODO |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
53 |
0 | 54 if patch: |
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
55 # create a new patch |
0 | 56 call(['hg', 'qnew', patch]) |
57 | |
58 def edit(self, patch=None): | |
59 """ | |
60 edit a patch | |
61 - patch: the patch to edit | |
62 """ | |
63 | |
64 def commit(self, message): | |
65 """ | |
66 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
|
67 - message : commit message |
0 | 68 """ |
69 call(['hg', 'qrefresh']) | |
70 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
|
71 if self.network: |
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
72 self._patch_command(*['hg', 'push']) |
0 | 73 |
74 def pull(self, repo=None): | |
75 """ | |
76 pull from the root repository | |
77 """ | |
78 # TODO: commit prior to realignment | |
79 call(['hg', 'qpop', '--all']) | |
80 call(['hg', 'pull'] + repo and [repo] or []) | |
81 call(['hg', 'qpush', '--all']) | |
2
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
82 |
3 | 83 def goto(self, patch): |
84 """ | |
5
448c248b3738
start the path to using the __init__ function for realz
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
85 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
|
86 - patch: name of patch to go to |
3 | 87 """ |
88 # TODO | |
89 process = subprocess.Popen(['hg', 'qapplied'], stdout=subprocess.PIPE) | |
90 stdout, stderr = process.communicate() | |
91 applied = [ i.strip() for i in stdout.splitlines() | |
92 if i ] | |
93 raise NotImplementedError | |
94 | |
2
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
95 def files(self): |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
96 """ |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
97 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
|
98 """ |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
99 # 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
|
100 _oldcwd = os.getcwd() |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
101 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
|
102 stdout, stderr = process.communicate() |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
103 return stdout |
dedf4c4c2ba2
add a function to list files (incomplete)
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
104 |
4
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
105 def _patch_repo(self): |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
106 """the location of the patch repository""" |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
107 root = subprocess.Popen(['hg', 'root'], stdout=subprocess.PIPE).communicate()[0] |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
108 return os.path.join(root, '.hg', 'patches') |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
109 |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
110 def _patch_command(self, *command): |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
111 """perform a command in the patch repository""" |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
112 _oldpwd = os.getcwd() |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
113 os.chdir(self._patch_repo()) |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
114 call(command) |
44ea39c3e98f
add methods for dealing with the patch repositories
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
115 os.chdir(_oldpwd) |
0 | 116 |
117 def main(args=sys.argv[1:]): | |
118 parser = CommandParser(HQ) | |
119 options, args = parser.parse_args(args) | |
120 parser.invoke(args) | |
121 | |
122 if __name__ == '__main__': | |
123 main() |