Mercurial > hg > silvermirror
comparison silvermirror/hg.py @ 27:a648f57b1921
STUB: silvermirror/hg.py
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 31 Jan 2014 19:26:34 -0800 |
parents | 16a12d660609 |
children | 03911cb46f53 |
comparison
equal
deleted
inserted
replaced
26:16a12d660609 | 27:a648f57b1921 |
---|---|
2 | 2 |
3 """ | 3 """ |
4 stub for the hg backend of silvermirror | 4 stub for the hg backend of silvermirror |
5 """ | 5 """ |
6 | 6 |
7 import os | |
8 import sys | |
9 import argparse | |
10 | |
11 _import_error = None | |
7 try: | 12 try: |
8 import lxml.html | 13 import lxml.html |
9 import mercurial | 14 except ImportError as _import_error: |
10 from mercurial import commands, hg, ui | 15 pass |
11 requirements_fulfilled = True | |
12 except ImportError as e: | |
13 _import_error = e | |
14 requirements_fulfilled = False | |
15 | 16 |
16 import os | |
17 import sys | |
18 from optparse import OptionParser | |
19 | 17 |
20 def update(host, path): | 18 def update(host, path): |
21 """ | 19 """ |
22 get changes from host on path | 20 get changes from host on path |
23 """ | 21 """ |
25 url = '%s/%s' % (host, path) | 23 url = '%s/%s' % (host, path) |
26 try: | 24 try: |
27 repo = hg.repository(_ui, path) | 25 repo = hg.repository(_ui, path) |
28 print 'Updating %s:' % path | 26 print 'Updating %s:' % path |
29 except Exception, e: | 27 except Exception, e: |
28 import pdb;pdb.set_trace() | |
30 repo = hg.repository(_ui, url) | 29 repo = hg.repository(_ui, url) |
31 print 'Cloning %s:' % path | 30 print 'Cloning %s:' % path |
32 commands.clone(_ui, repo, pull=False, uncompressed=False, rev=None, noupdate=False) | 31 commands.clone(_ui, repo, pull=False, uncompressed=False, rev=None, noupdate=False) |
33 return | 32 return |
34 | 33 |
62 repos = [i.text_content() for i in tds] | 61 repos = [i.text_content() for i in tds] |
63 return repos | 62 return repos |
64 | 63 |
65 | 64 |
66 def main(args=sys.argv[1:]): | 65 def main(args=sys.argv[1:]): |
67 parser = OptionParser() | 66 """CLI""" |
68 parser.add_option('-H', '--host', dest='host') | 67 |
69 parser.add_option('-d', '--directory', dest='directory', | 68 # parse command line |
69 parser = argparse.ArgumentParser(description=__doc__) | |
70 parser.add_argument('host', dest='host', | |
71 help="URL of mercurial repository index page") | |
72 parser.add_argument('-d', '--directory', dest='directory', | |
70 default=os.path.join(os.environ['HOME'], 'hg'), | 73 default=os.path.join(os.environ['HOME'], 'hg'), |
71 help="directory to clone/update to") | 74 help="base directory to clone/update to [DEFAULT: %default]") |
72 parser.add_option('--list', dest='list', | 75 parser.add_argument('--list', dest='list', |
73 action='store_true', default=False) | 76 action='store_true', default=False, |
74 options, args = parser.parse_args(args) | 77 help="list repositories and exit") |
75 if not requirements_fulfilled: | 78 options = parser.parse_args(args) |
76 # Hack; this should all be better o_O | 79 if _import_error is not None: |
77 parser.error("Must have mercurial and lxml packages to use, sorry: {}".format(e)) | 80 # XXX better error handling |
78 if not options.host: | 81 parser.error("Must have hglib and lxml package to use, sorry: {}".format(_import_error)) |
79 parser.print_usage() | |
80 parser.exit() | |
81 | 82 |
82 # kill trailing slash | 83 # kill trailing slash |
83 options.host = options.host.rstrip('/') | 84 options.host = options.host.rstrip('/') |
84 | 85 |
86 # get repositories | |
85 repos = repositories(options.host) | 87 repos = repositories(options.host) |
86 if options.list: | 88 if options.list: |
87 for repo in repos: | 89 for repo in repos: |
88 print repo | 90 print repo |
89 sys.exit(0) | 91 sys.exit(0) |
90 | 92 |
93 # clone/update repos to directory | |
91 if not os.path.exists(options.directory): | 94 if not os.path.exists(options.directory): |
92 os.mkdir(options.directory) | 95 os.mkdir(options.directory) |
93 | |
94 os.chdir(options.directory) | |
95 for repo in repos: | 96 for repo in repos: |
96 update(options.host, repo) | 97 update(options.host, repo) |
97 | 98 |
98 if __name__ == '__main__': | 99 if __name__ == '__main__': |
99 main() | 100 main() |