[project @ First working version of script which retrieves eland config file from...
[htsworkflow.git] / bin / retrieve_eland_config.py
1 #!/usr/bin/env python
2
3 from optparse import OptionParser
4 from ConfigParser import SafeConfigParser
5
6 import os
7 import sys
8 import urllib
9
10
11 def constructOptionParser():
12   parser = OptionParser()
13   
14   parser.add_option("-u", "--url",
15                     action="store", type="string", dest="url")
16   
17   parser.add_option("-o", "--output",
18                     action="store", type="string", dest="output_filepath")
19   
20   parser.add_option("-f", "--flowcell",
21                     action="store", type="string", dest="flowcell")
22   
23   #parser.set_default("url", "default")
24   
25   return parser
26
27 def constructConfigParser():
28   parser = SafeConfigParser()
29   parser.read(['/etc/elandifier/elandifier.conf',
30                os.path.expanduser('~/.elandifier.conf')])
31   if not parser.has_section('server_info'):
32     parser.add_section('server_info')
33   
34   return parser
35
36
37 def getCombinedOptions():
38   cl_parser = constructOptionParser()
39   conf_parser = constructConfigParser()
40   
41   options, args = cl_parser.parse_args()
42   
43   if options.url is None:
44     if conf_parser.has_option('server_info', 'base_host_url'):
45       options.url = conf_parser.get('server_info', 'base_host_url')
46   
47   print 'URL:', options.url
48   print 'OUT:', options.output_filepath
49   print ' FC:', options.flowcell
50   
51   return options
52
53
54 def saveConfigFile(flowcell, base_host_url, output_filepath):
55   """
56   retrieves the flowcell eland config file, give the base_host_url
57   (i.e. http://sub.domain.edu:port)
58   """
59   url = base_host_url + '/elandifier/config/%s/' % (flowcell)
60   
61   f = open(output_filepath, 'w')
62   web = urllib.urlopen(url)
63   f.write(web.read())
64   web.close()
65   f.close()
66
67 if __name__ == '__main__':
68   options = getCombinedOptions()
69   msg_list = ['ERROR MESSAGES:']
70   if options.output_filepath is None:
71     msg_list.append("  Output filepath argument required. -o <filepath> or --output=<filepath>")
72     
73   if options.flowcell is None:
74     msg_list.append("  Flow cell argument required. -f <flowcell> or --flowcell=<flowcell>")
75     
76   if options.url is None:
77     msg_list.append("  URL argument required (-u <url> or --url=<url>), or entry\n" \
78                     "    in /etc/elandifier/elandifer.conf or ~/.elandifier.conf")
79     
80   if len(msg_list) > 1:
81     print '\n'.join(msg_list)
82     sys.exit(0)
83   
84   saveConfigFile(options.flowcell, options.url, options.output_filepath)
85