Mercurial > hg > silvermirror
comparison silvermirror/hg.py @ 6:c999b2f16159
adding functionality to update/clone a bunch of hg repos
author | k0s <k0scist@gmail.com> |
---|---|
date | Thu, 15 Oct 2009 15:27:05 -0400 |
parents | 660c74f33ed2 |
children | 0da780dd81d0 |
comparison
equal
deleted
inserted
replaced
5:660c74f33ed2 | 6:c999b2f16159 |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 """ | 2 """ |
3 stub for the hg backend of silvermirror | 3 stub for the hg backend of silvermirror |
4 """ | 4 """ |
5 | 5 |
6 import lxml.html | |
7 import mercurial | |
6 import os | 8 import os |
7 import sys | 9 import sys |
8 from mercurial import commands, hg, ui | 10 from mercurial import commands, hg, ui |
9 from optparse import OptionParser | 11 from optparse import OptionParser |
10 | 12 |
11 def update(host, path): | 13 def update(host, path): |
12 """ | 14 """ |
13 get changes from host on path | 15 get changes from host on path |
14 """ | 16 """ |
15 ui = ui.ui() | 17 _ui = ui.ui() |
18 url = '%s/%s' % (host, path) | |
16 try: | 19 try: |
17 repo = hg.repository(ui, path) | 20 repo = hg.repository(_ui, path) |
18 command = commands.pull | 21 print 'Updating %s:' % path |
19 except mercurial.repo.RepoError: | 22 except mercurial.repo.RepoError: |
20 repo = hg.repository(ui, 'ssh://%s/%s' % (host, path)) | 23 repo = hg.repository(_ui, url) |
21 command = commands.clone | 24 print 'Cloning %s:' % path |
25 commands.clone(_ui, repo, pull=False, uncompressed=False, rev=None, noupdate=False) | |
26 return | |
22 | 27 |
23 def repositories(path): | 28 commands.pull(_ui, repo, url, rev=None, force=False, update=True) |
29 | |
30 # def repositories(path): | |
31 # """ | |
32 # return all hg repositories in a path | |
33 # """ | |
34 | |
35 # ui = ui.ui() | |
36 # files = [ os.path.join(path, f) for f in os.listdir(path) ] | |
37 # directories = [ f for f in files if os.path.isdir(f) ] | |
38 | |
39 # repos = [] | |
40 # for d in directories: | |
41 # try: | |
42 # repo = hg.repository(ui, d) | |
43 # repos.append(os.path.basename(d)) | |
44 # except mercurial.repo.RepoError: | |
45 # pass | |
46 # return repos | |
47 | |
48 def repositories(url): | |
24 """ | 49 """ |
25 return all hg repositories in a path | 50 returns the list of reposotories under a URL of an hg server |
26 """ | 51 """ |
52 element = lxml.html.parse(url) | |
53 tds = element.xpath('//tr[position() > 1]/td[1]') | |
54 repos = [ i.text_content() for i in tds ] | |
55 return repos | |
27 | 56 |
28 ui = ui.ui() | |
29 files = [ os.path.join(path, f) for f in os.listdir(path) ] | |
30 directories = [ f for f in files if os.path.isdir(f) ] | |
31 | |
32 repos = [] | |
33 for d in directories: | |
34 try: | |
35 repo = hg.repository(ui, d) | |
36 repos.append(os.path.basename(d)) | |
37 except mercurial.repo.RepoError: | |
38 pass | |
39 return repos | |
40 | 57 |
41 def main(args=sys.argv[1:]): | 58 def main(args=sys.argv[1:]): |
42 parser = OptionParser() | 59 parser = OptionParser() |
43 parser.add_option('-H', '--host', dest='host') | 60 parser.add_option('-H', '--host', dest='host') |
61 parser.add_option('-d', '--directory', dest='directory', | |
62 default=os.path.join(os.environ['HOME'], 'hg')) | |
63 parser.add_option('--list', dest='list', | |
64 action='store_true', default=False) | |
44 options, args = parser.parse_args(args) | 65 options, args = parser.parse_args(args) |
45 if len(args) != 1: | 66 if not options.host: |
46 parser.print_usage() | 67 parser.print_usage() |
47 parser.exit() | 68 parser.exit() |
48 | 69 |
70 # kill trailing slash | |
71 options.host = options.host.rstrip('/') | |
72 | |
73 repos = repositories(options.host) | |
74 if options.list: | |
75 for repo in repos: | |
76 print repo | |
77 sys.exit(0) | |
78 | |
79 os.chdir(options.directory) | |
80 for repo in repos: | |
81 update(options.host, repo) | |
82 | |
83 | |
49 if __name__ == '__main__': | 84 if __name__ == '__main__': |
50 main() | 85 main() |