]> git.donarmstrong.com Git - neurodebian.git/blob - tools/nd_querycfg
Also for stats report which repo and which job number use our setup
[neurodebian.git] / tools / nd_querycfg
1 #!/usr/bin/python
2 """
3 Dead simple script to query the NeuroDebian dev config (or any other Python config file)
4 """
5
6 import os, sys
7
8 from ConfigParser import SafeConfigParser
9 from optparse import OptionParser
10
11 __prog__ = os.path.basename(sys.argv[0])
12 __version__ = '0.0.2'
13
14 if __name__ == '__main__':
15     parser = OptionParser(
16         usage="%s [OPTIONS] [section] [field]\n" % __prog__ + __doc__,
17         version="%prog " + __version__)
18
19     parser.add_option(
20         '-f', '--config-file', default="/etc/neurodebian/neurodebian.cfg",
21         help="name of the config file")
22
23     parser.add_option(
24         '-F', '--value-separator', default="=",
25         help="string to separate key from value")
26
27     opts, argv = parser.parse_args()
28
29     if not os.path.exists(opts.config_file):
30         sys.stderr.write("ERROR: File %s does not exist\n" % opts.config_file)
31         sys.exit(1)
32
33     cfg = SafeConfigParser()
34     cfg.read(opts.config_file)
35     if len(argv) == 2:
36         print cfg.get(argv[0], argv[1])
37     elif len(argv) == 1:
38         # Print values of the section, =-separate key from value
39         print '\n'.join([opts.value_separator.join(x)
40                          for x in cfg.items(argv[0])])
41     else:
42         # Print section names
43         print '\n'.join(cfg.sections())