annotate python/setup_repo.py @ 303:266d6b337373

still doesnt work but what the fuck
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 14 May 2013 16:26:44 -0700
parents fd0e81fdf1fa
children 8964b285c2f7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
3 """
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
4 setup a repository
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
5
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
6 Parameters:
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
7 - host URL
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
8 - location on host disk
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
9 """
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
10
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 import os
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 import subprocess
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 import sys
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
14 import urlparse
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
16 from subprocess import check_call as call
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 from optparse import OptionParser
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
19
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
20 # global variables
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 HOST='k0s.org'
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 HGRC="""[paths]
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 default = http://%(host)s/%(repo)s/%(name)s
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 default-push = ssh://%(host)s/%(repo)s/%(name)s
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 """
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 def call(command, *args, **kw):
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
28
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
29 code = subprocess.call(command, *args, **kw)
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
30 if isinstance(command, basestring):
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
31 cmdstr = command
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
32 else:
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
33 cmdstr = ' '.join(command)
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
34 print cmdstr
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
35 if code:
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
36 raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code))
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
37
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 def main(args=sys.argv[1:]):
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
40 # parse command line arguments
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
41 parser = OptionParser('%prog [options] location')
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
42 parser.add_option('-m', '--message', dest='message',
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
43 help='commit message')
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
44 parser.add_option('-u', '--remote-url', dest='remote_url',
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
45 default="http://k0s.org/hg/",
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
46 help="URL of host hg repository collection [Default: %default]")
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
47 parser.add_options('-p', '--remote-path', dest='remote_path',
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
48 help="remote path of hg repository links; otherwise taken from --remote-url, if specified")
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
49 options, args = parser.parse_args(args)
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
50 if len(args) != 1:
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
51 parser.print_usage()
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
52 parser.exit()
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
53 # TODO: sanity check for remote_url, remote_path
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54
285
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
55 # setup repository
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
56 name = os.path.basename(directory)
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
57 call(['hg', 'init'])
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
58 call(['hg', 'add'])
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
59 call(['hg', 'commit', '-m', options.message or 'initial commit of %s' %name])
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
60
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
61 # setup remote, if specified
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
62 if options.remote_url:
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
63
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
64 # parse remote URL
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
65 host, netloc, path, query, anchor = urlparse.urlsplit(options.remote_url)
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
66 if options.remote_path:
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
67 remote_host = host
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
68 if ':' in remote_path:
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
69 remote_host, remote_path = remote_path.split(':', 1)
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
70 else:
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
71 remote_path = path
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
72
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
73 # setup remote repository
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
74 call(['ssh', HOST, "cd %s/%s && hg init && hg add && hg ci -m '%s'" % (repo, name, options.message or 'initial commit of %s' % name)])
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
75
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
76 # write local .hgrc file
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
77 # TODO: use ConfigParser
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
78 f = file(os.path.join('.hg', 'hgrc'), 'w')
fd0e81fdf1fa making setup_repo almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
79 print >> f, HGRC % { 'host': HOST, 'repo': repo, 'name': name}
72
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
80
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
81 if __name__ == '__main__':
5a6f89cc6bc4 add a script to add a hg repo
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
82 main()