3 from optparse import OptionParser, IndentedHelpFormatter
4 from ConfigParser import SafeConfigParser
10 CONFIG_SYSTEM = '/etc/ga_frontend/ga_frontend.conf'
11 CONFIG_USER = os.path.expanduser('~/.ga_frontend.conf')
14 class FlowCellNotFound(Exception): pass
15 class WebError404(Exception): pass
18 class PreformattedDescriptionFormatter(IndentedHelpFormatter):
20 #def format_description(self, description):
23 # return description + "\n"
27 def format_epilog(self, epilog):
29 It was removing my preformated epilog, so this should override
30 that behavior! Muhahaha!
33 return "\n" + epilog + "\n"
38 def constructOptionParser():
40 returns a pre-setup optparser
42 parser = OptionParser(formatter=PreformattedDescriptionFormatter())
44 parser.set_description('Retrieves eland config file from ga_frontend web frontend.')
49 * %s (User specific; overrides system)
50 * command line overrides all config file options
55 base_host_url=http://somewhere.domain:port
56 """ % (CONFIG_SYSTEM, CONFIG_USER)
58 #Special formatter for allowing preformatted description.
59 ##parser.format_epilog(PreformattedDescriptionFormatter())
61 parser.add_option("-u", "--url",
62 action="store", type="string", dest="url")
64 parser.add_option("-o", "--output",
65 action="store", type="string", dest="output_filepath")
67 parser.add_option("-f", "--flowcell",
68 action="store", type="string", dest="flowcell")
70 #parser.set_default("url", "default")
74 def constructConfigParser():
76 returns a pre-setup config parser
78 parser = SafeConfigParser()
79 parser.read([CONFIG_SYSTEM, CONFIG_USER])
80 if not parser.has_section('config_file_server'):
81 parser.add_section('config_file_server')
86 def getCombinedOptions():
88 Returns optparse options after it has be updated with ConfigParser
89 config files and merged with parsed commandline options.
91 cl_parser = constructOptionParser()
92 conf_parser = constructConfigParser()
94 options, args = cl_parser.parse_args()
96 if options.url is None:
97 if conf_parser.has_option('config_file_server', 'base_host_url'):
98 options.url = conf_parser.get('config_file_server', 'base_host_url')
100 print 'USING OPTIONS:'
101 print ' URL:', options.url
102 print ' OUT:', options.output_filepath
103 print ' FC:', options.flowcell
109 def saveConfigFile(flowcell, base_host_url, output_filepath):
111 retrieves the flowcell eland config file, give the base_host_url
112 (i.e. http://sub.domain.edu:port)
114 url = base_host_url + '/eland_config/%s/' % (flowcell)
116 f = open(output_filepath, 'w')
118 web = urllib.urlopen(url)
119 #except IOError, msg:
120 # if str(msg).find("Connection refused") >= 0:
121 # print 'Error: Connection refused for: %s' % (url)
124 # elif str(msg).find("Name or service not known") >= 0:
125 # print 'Error: Invalid domain or ip address for: %s' % (url)
133 if data.find('Hmm, config file for') >= 0:
134 msg = "Flowcell (%s) not found in DB; full url(%s)" % (flowcell, url)
135 raise FlowCellNotFound, msg
137 if data.find('404 - Not Found') >= 0:
138 msg = "404 - Not Found: Flowcell (%s); base_host_url (%s);\n full url(%s)\n " \
139 "Did you get right port #?" % (flowcell, base_host_url, url)
140 raise FlowCellNotFound, msg
145 print 'Wrote config file to %s' % (output_filepath)
148 if __name__ == '__main__':
149 #Display help if no args are presented
150 if len(sys.argv) == 1:
151 sys.argv.append('-h')
153 options = getCombinedOptions()
154 msg_list = ['ERROR MESSAGES:']
155 if options.output_filepath is None:
156 msg_list.append(" Output filepath argument required. -o <filepath> or --output=<filepath>")
158 if options.flowcell is None:
159 msg_list.append(" Flow cell argument required. -f <flowcell> or --flowcell=<flowcell>")
161 if options.url is None:
162 msg_list.append(" URL argument required (-u <url> or --url=<url>), or entry\n" \
163 " in /etc/elandifier/elandifer.conf or ~/.elandifier.conf")
165 if len(msg_list) > 1:
166 print '\n'.join(msg_list)
169 saveConfigFile(options.flowcell, options.url, options.output_filepath)