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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
8 import os
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
9 import sys
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
10 import argparse
32
fa1fe03ad924 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 31
diff changeset
11 from hglib.error import ServerError
27
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
12
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
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
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
16 except ImportError as _import_error:
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
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
fa1fe03ad924 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 31
diff changeset
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
fa1fe03ad924 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 31
diff changeset
25 repo = hglib.open(path)
31
2adda506392b STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 30
diff changeset
26 print ('Updating {}:'.format(path))
34
f0b30886801f STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
27 repo.pull(source, update=True, insecure=True)
32
fa1fe03ad924 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 31
diff changeset
28 except ServerError:
33
d5e0bce5e411 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 32
diff changeset
29 print ('Cloning {} -> {}'.format(source, path))
32
fa1fe03ad924 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 31
diff changeset
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
19ee8dfe2325 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 29
diff changeset
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
16a12d660609 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 25
diff changeset
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
183cee927d8b STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 17
diff changeset
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
8a5d2083fbd1 adding stub for main()
k0s <k0scist@gmail.com>
parents: 2
diff changeset
60
6
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
61
3
8a5d2083fbd1 adding stub for main()
k0s <k0scist@gmail.com>
parents: 2
diff changeset
62 def main(args=sys.argv[1:]):
27
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
63 """CLI"""
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
64
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
65 # parse command line
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
66 parser = argparse.ArgumentParser(description=__doc__)
29
882b26fbfcd3 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 28
diff changeset
67 parser.add_argument('host',
27
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
68 help="URL of mercurial repository index page")
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
69 parser.add_argument('-d', '--directory', dest='directory',
29
882b26fbfcd3 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 28
diff changeset
70 default=os.path.join(os.environ['HOME'], 'hg'),
882b26fbfcd3 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 28
diff changeset
71 help="base directory to clone/update to [DEFAULT: %(default)s]")
27
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
72 parser.add_argument('--list', dest='list',
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
73 action='store_true', default=False,
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
74 help="list repositories and exit")
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
75 options = parser.parse_args(args)
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
76 if _import_error is not None:
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
77 # XXX better error handling
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
78 parser.error("Must have hglib and lxml package to use, sorry: {}".format(_import_error))
3
8a5d2083fbd1 adding stub for main()
k0s <k0scist@gmail.com>
parents: 2
diff changeset
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
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
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
a648f57b1921 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
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
d5e0bce5e411 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 32
diff changeset
94 source = '{}/{}'.format(options.host, repo)
32
fa1fe03ad924 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 31
diff changeset
95 dest = os.path.join(options.directory, repo)
33
d5e0bce5e411 STUB: silvermirror/hg.py
Jeff Hammel <k0scist@gmail.com>
parents: 32
diff changeset
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
8a5d2083fbd1 adding stub for main()
k0s <k0scist@gmail.com>
parents: 2
diff changeset
98 if __name__ == '__main__':
8a5d2083fbd1 adding stub for main()
k0s <k0scist@gmail.com>
parents: 2
diff changeset
99 main()