/
/mkconfig.py
 1 #!/usr/bin/env python
 2 
 3 """
 4 A darcsweb configuration generator
 5 ----------------------------------
 6 
 7 This is a small utility that generates configuration files for darcsweb, in
 8 case you're lazy and/or have many repositories.
 9 
10 It receives four parameters: the base URL for your repositories, the base
11 description, the encoding and the directory to get the repositories from.
12 It replaces the string "NAME" with the name of the directory that holds the
13 repository, so you can specify urls and descriptions with the name in them.
14 
15 Then, it generates an appropiate configuration for each repository in the
16 directory. It outputs the configuration to stdout, so you can redirect it to
17 config.py. For example:
18 
19 $ mkconf.py "http://example.com/darcs/NAME" "Repo for NAME" latin1 \\
20 	~/devel/repos/ >> config.py
21 
22 Remember that you still need to do the base configuration by hand. You can do
23 that by copying the sample included with darcsweb.
24 """
25 
26 
27 import sys
28 import os
29 import string
30 import urllib
31 
32 
33 def help():
34 	print "Error: wrong parameter count"
35 	print __doc__
36 
37 def filter_class(s):
38 	"Filter s so the new string can be used as a class name."
39 	allowed = string.ascii_letters + string.digits + '_'
40 	l = [c for c in s if c in allowed]
41 	return string.join(l, "")
42 
43 def filter_url(s):
44 	"Filter s so the new string can be used in a raw url."
45 	return urllib.quote_plus(s, ':/')
46 
47 
48 # check parameters
49 if len(sys.argv) != 5:
50 	help()
51 	sys.exit(0)
52 
53 myself, baseurl, basedesc, baseencoding, basepath = sys.argv
54 
55 dirs = os.listdir(basepath)
56 for d in dirs:
57 	path = basepath + '/' + d
58 	if not os.path.isdir(path + '/_darcs'):
59 		# not a repo, skip
60 		continue
61 	s = \
62 """
63 class %(classname)s:
64 	reponame = '%(name)s'
65 	repodesc = '%(desc)s'
66 	repodir = '%(dir)s'
67 	repourl = '%(url)s'
68 	repoencoding = '%(encoding)s'
69 """ % {
70 		'classname': filter_class(d),
71 		'name': d,
72 		'desc': basedesc.replace('NAME', d),
73 		'dir': os.path.abspath(basepath + '/' + d),
74 		'url': filter_url(baseurl.replace('NAME', d)),
75 		'encoding': baseencoding,
76 	}
77 	print s