Mercurial > hg > silvermirror
annotate silvermirror/hg.py @ 34:f0b30886801f
STUB: silvermirror/hg.py
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 31 Jan 2014 23:25:46 -0800 |
parents | d5e0bce5e411 |
children | 52937c25edf0 |
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 |
28
03911cb46f53
STUB: setup.py silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents:
27
diff
changeset
|
7 import hglib # http://mercurial.selenic.com/wiki/PythonHglib |
27 | 8 import os |
9 import sys | |
10 import argparse | |
32 | 11 from hglib.error import ServerError |
27 | 12 |
13 _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
|
14 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
|
15 import lxml.html |
27 | 16 except ImportError as _import_error: |
17 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
|
18 |
1
9b139702a8f9
use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
19 |
32 | 20 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
|
21 """ |
9b139702a8f9
use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
22 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
|
23 """ |
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
|
24 try: |
32 | 25 repo = hglib.open(path) |
31 | 26 print ('Updating {}:'.format(path)) |
34 | 27 repo.pull(source, update=True, insecure=True) |
32 | 28 except ServerError: |
33 | 29 print ('Cloning {} -> {}'.format(source, path)) |
32 | 30 repo = hglib.clone(source, path) |
2
0d9094bb98b0
adding function to return all hg repos on a path
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
31 |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
32 |
30 | 33 # TODO: local repository |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
34 # def repositories(path): |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
35 # """ |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
36 # return all hg repositories in a path |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
37 # """ |
2
0d9094bb98b0
adding function to return all hg repos on a path
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
38 |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
39 # ui = ui.ui() |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
40 # files = [ os.path.join(path, f) for f in os.listdir(path) ] |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
41 # directories = [ f for f in files if os.path.isdir(f) ] |
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
|
42 |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
43 # repos = [] |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
44 # for d in directories: |
26 | 45 # try: |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
46 # repo = hg.repository(ui, d) |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
47 # repos.append(os.path.basename(d)) |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
48 # except mercurial.repo.RepoError: |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
49 # pass |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
50 # return repos |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
51 |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
52 def repositories(url): |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
53 """ |
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
|
54 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
|
55 """ |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
56 element = lxml.html.parse(url) |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
57 tds = element.xpath('//tr[position() > 1]/td[1]') |
24 | 58 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
|
59 return repos |
3 | 60 |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
61 |
3 | 62 def main(args=sys.argv[1:]): |
27 | 63 """CLI""" |
64 | |
65 # parse command line | |
66 parser = argparse.ArgumentParser(description=__doc__) | |
29 | 67 parser.add_argument('host', |
27 | 68 help="URL of mercurial repository index page") |
69 parser.add_argument('-d', '--directory', dest='directory', | |
29 | 70 default=os.path.join(os.environ['HOME'], 'hg'), |
71 help="base directory to clone/update to [DEFAULT: %(default)s]") | |
27 | 72 parser.add_argument('--list', dest='list', |
73 action='store_true', default=False, | |
74 help="list repositories and exit") | |
75 options = parser.parse_args(args) | |
76 if _import_error is not None: | |
77 # XXX better error handling | |
78 parser.error("Must have hglib and lxml package to use, sorry: {}".format(_import_error)) | |
3 | 79 |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
80 # kill trailing slash |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
81 options.host = options.host.rstrip('/') |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
82 |
27 | 83 # get repositories |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
84 repos = repositories(options.host) |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
85 if options.list: |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
86 for repo in repos: |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
87 print repo |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
88 sys.exit(0) |
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
89 |
27 | 90 # 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
|
91 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
|
92 os.mkdir(options.directory) |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
93 for repo in repos: |
33 | 94 source = '{}/{}'.format(options.host, repo) |
32 | 95 dest = os.path.join(options.directory, repo) |
33 | 96 update(source, dest) |
6
c999b2f16159
adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents:
5
diff
changeset
|
97 |
3 | 98 if __name__ == '__main__': |
99 main() |