Mercurial > hg > silvermirror
annotate silvermirror/hg.py @ 39:9e21b4fbd8b3
whitespace
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 12 May 2015 18:33:30 -0700 |
parents | d17978d9697b |
children |
rev | line source |
---|---|
1
9b139702a8f9
use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
17
f8edfc9c28ba
a really hacky way to disable...for temporary sanity and long term backing down from insanity
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
2 |
1
9b139702a8f9
use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
3 """ |
9b139702a8f9
use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
4 stub for the hg backend of silvermirror |
9b139702a8f9
use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
5 """ |
9b139702a8f9
use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
6 |
36 | 7 # imports |
8 import argparse | |
28
03911cb46f53
STUB: setup.py silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents:
27
diff
changeset
|
9 import hglib # http://mercurial.selenic.com/wiki/PythonHglib |
27 | 10 import os |
11 import sys | |
32 | 12 from hglib.error import ServerError |
27 | 13 |
14 _import_error = None | |
17
f8edfc9c28ba
a really hacky way to disable...for temporary sanity and long term backing down from insanity
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
15 try: |
f8edfc9c28ba
a really hacky way to disable...for temporary sanity and long term backing down from insanity
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
16 import lxml.html |
27 | 17 except ImportError as _import_error: |
18 pass | |
17
f8edfc9c28ba
a really hacky way to disable...for temporary sanity and long term backing down from insanity
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
19 |
1
9b139702a8f9
use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
20 |
38 | 21 def clone(source, path): |
22 print ('Cloning {} -> {}'.format(source, path)) | |
23 return hglib.clone(source, path) | |
24 | |
32 | 25 def update(source, path): |
1
9b139702a8f9
use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
26 """ |
9b139702a8f9
use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
27 get changes from host on path |
9b139702a8f9
use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
28 """ |
37
1877e2cb1118
remove commented out function; we dont use it and it probably needs to be rewritten anyway
Jeff Hammel <k0scist@gmail.com>
parents:
36
diff
changeset
|
29 |
38 | 30 if not os.path.exists(path): |
31 return clone(source, path) | |
37
1877e2cb1118
remove commented out function; we dont use it and it probably needs to be rewritten anyway
Jeff Hammel <k0scist@gmail.com>
parents:
36
diff
changeset
|
32 |
17
f8edfc9c28ba
a really hacky way to disable...for temporary sanity and long term backing down from insanity
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
33 try: |
32 | 34 repo = hglib.open(path) |
38 | 35 print ('Updating {}'.format(path)) |
34 | 36 repo.pull(source, update=True, insecure=True) |
32 | 37 except ServerError: |
38 repo = hglib.clone(source, path) | |
38 | 39 return repo |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
40 |
39 | 41 |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
42 def repositories(url): |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
43 """ |
17
f8edfc9c28ba
a really hacky way to disable...for temporary sanity and long term backing down from insanity
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
44 returns the list of repositories under a URL of an hg server |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
45 """ |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
46 element = lxml.html.parse(url) |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
47 tds = element.xpath('//tr[position() > 1]/td[1]') |
24 | 48 repos = [i.text_content() for i in tds] |
2
0d9094bb98b0
adding function to return all hg repos on a path
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
49 return repos |
3 | 50 |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
51 |
3 | 52 def main(args=sys.argv[1:]): |
27 | 53 """CLI""" |
54 | |
55 # parse command line | |
56 parser = argparse.ArgumentParser(description=__doc__) | |
29 | 57 parser.add_argument('host', |
27 | 58 help="URL of mercurial repository index page") |
59 parser.add_argument('-d', '--directory', dest='directory', | |
29 | 60 default=os.path.join(os.environ['HOME'], 'hg'), |
61 help="base directory to clone/update to [DEFAULT: %(default)s]") | |
36 | 62 parser.add_argument('-r', '--repo', dest='repositories', nargs='+', |
63 help="repositories") # TODO: stub | |
27 | 64 parser.add_argument('--list', dest='list', |
65 action='store_true', default=False, | |
66 help="list repositories and exit") | |
67 options = parser.parse_args(args) | |
36 | 68 |
27 | 69 if _import_error is not None: |
36 | 70 # TODO: better error handling |
71 parser.error("Must have hglib and lxml package to use, sorry:\n{}".format(_import_error)) | |
3 | 72 |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
73 # kill trailing slash |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
74 options.host = options.host.rstrip('/') |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
75 |
27 | 76 # get repositories |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
77 repos = repositories(options.host) |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
78 if options.list: |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
79 for repo in repos: |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
80 print repo |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
81 sys.exit(0) |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
82 |
27 | 83 # clone/update repos to directory |
9
5a909090fb24
make the destination directory for mirroring if it doesnt exist
k0s <k0scist@gmail.com>
parents:
8
diff
changeset
|
84 if not os.path.exists(options.directory): |
5a909090fb24
make the destination directory for mirroring if it doesnt exist
k0s <k0scist@gmail.com>
parents:
8
diff
changeset
|
85 os.mkdir(options.directory) |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
86 for repo in repos: |
33 | 87 source = '{}/{}'.format(options.host, repo) |
32 | 88 dest = os.path.join(options.directory, repo) |
33 | 89 update(source, dest) |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
90 |
3 | 91 if __name__ == '__main__': |
92 main() |