annotate 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
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
9b139702a8f9 use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff changeset
2 """
9b139702a8f9 use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff changeset
3 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
4 """
9b139702a8f9 use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff changeset
5
6
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
6 import lxml.html
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
7 import mercurial
1
9b139702a8f9 use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff changeset
8 import os
3
8a5d2083fbd1 adding stub for main()
k0s <k0scist@gmail.com>
parents: 2
diff changeset
9 import sys
1
9b139702a8f9 use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff changeset
10 from mercurial import commands, hg, ui
3
8a5d2083fbd1 adding stub for main()
k0s <k0scist@gmail.com>
parents: 2
diff changeset
11 from optparse import OptionParser
1
9b139702a8f9 use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff changeset
12
9b139702a8f9 use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff changeset
13 def update(host, path):
9b139702a8f9 use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff changeset
14 """
9b139702a8f9 use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff changeset
15 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
16 """
6
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
17 _ui = ui.ui()
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
18 url = '%s/%s' % (host, path)
1
9b139702a8f9 use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff changeset
19 try:
6
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
20 repo = hg.repository(_ui, path)
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
21 print 'Updating %s:' % path
1
9b139702a8f9 use a real backend architecture with an inteface and vastly simplify unify.py
k0s <k0scist@gmail.com>
parents:
diff changeset
22 except mercurial.repo.RepoError:
6
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
23 repo = hg.repository(_ui, url)
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
24 print 'Cloning %s:' % path
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
25 commands.clone(_ui, repo, pull=False, uncompressed=False, rev=None, noupdate=False)
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
26 return
2
0d9094bb98b0 adding function to return all hg repos on a path
k0s <k0scist@gmail.com>
parents: 1
diff changeset
27
6
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
28 commands.pull(_ui, repo, url, rev=None, force=False, update=True)
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
29
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
30 # def repositories(path):
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
31 # """
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
32 # 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
33 # """
2
0d9094bb98b0 adding function to return all hg repos on a path
k0s <k0scist@gmail.com>
parents: 1
diff changeset
34
6
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
35 # ui = ui.ui()
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
36 # 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
37 # directories = [ f for f in files if os.path.isdir(f) ]
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 # repos = []
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
40 # for d in directories:
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
41 # try:
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
42 # repo = hg.repository(ui, d)
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
43 # 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
44 # except mercurial.repo.RepoError:
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
45 # pass
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
46 # return repos
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
47
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
48 def repositories(url):
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
49 """
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
50 returns the list of reposotories under a URL of an hg server
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 element = lxml.html.parse(url)
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
53 tds = element.xpath('//tr[position() > 1]/td[1]')
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
54 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
55 return repos
3
8a5d2083fbd1 adding stub for main()
k0s <k0scist@gmail.com>
parents: 2
diff changeset
56
6
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
57
3
8a5d2083fbd1 adding stub for main()
k0s <k0scist@gmail.com>
parents: 2
diff changeset
58 def main(args=sys.argv[1:]):
8a5d2083fbd1 adding stub for main()
k0s <k0scist@gmail.com>
parents: 2
diff changeset
59 parser = OptionParser()
5
660c74f33ed2 adding more to skeleton for main program
k0s <k0scist@gmail.com>
parents: 3
diff changeset
60 parser.add_option('-H', '--host', dest='host')
6
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
61 parser.add_option('-d', '--directory', dest='directory',
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
62 default=os.path.join(os.environ['HOME'], 'hg'))
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
63 parser.add_option('--list', dest='list',
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
64 action='store_true', default=False)
3
8a5d2083fbd1 adding stub for main()
k0s <k0scist@gmail.com>
parents: 2
diff changeset
65 options, args = parser.parse_args(args)
6
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
66 if not options.host:
5
660c74f33ed2 adding more to skeleton for main program
k0s <k0scist@gmail.com>
parents: 3
diff changeset
67 parser.print_usage()
660c74f33ed2 adding more to skeleton for main program
k0s <k0scist@gmail.com>
parents: 3
diff changeset
68 parser.exit()
3
8a5d2083fbd1 adding stub for main()
k0s <k0scist@gmail.com>
parents: 2
diff changeset
69
6
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
70 # kill trailing slash
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
71 options.host = options.host.rstrip('/')
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
72
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
73 repos = repositories(options.host)
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
74 if options.list:
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
75 for repo in repos:
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
76 print repo
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
77 sys.exit(0)
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
78
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
79 os.chdir(options.directory)
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
80 for repo in repos:
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
81 update(options.host, repo)
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
82
c999b2f16159 adding functionality to update/clone a bunch of hg repos
k0s <k0scist@gmail.com>
parents: 5
diff changeset
83
3
8a5d2083fbd1 adding stub for main()
k0s <k0scist@gmail.com>
parents: 2
diff changeset
84 if __name__ == '__main__':
8a5d2083fbd1 adding stub for main()
k0s <k0scist@gmail.com>
parents: 2
diff changeset
85 main()