#!/usr/bin/env python #import os #os.environ['DJANGO_SETTINGS_MODULE'] = 'htsworkflow.frontend.settings' from htsworkflow.util.hdquery import get_hd_serial_num from htsworkflow.frontend import settings #from django.conf import settings from optparse import OptionParser import sys import urllib2 def construct_parser(): """ """ parser = OptionParser("usage: %prog -f -d OR\n\t %prog -f -s ") parser.add_option("-f", "--flowcell", action="store", type="string", dest="flowcell", help="flowcell being archived") parser.add_option("-d", "--device", action="store", type="string", dest="device", help="device flowcell is being archived to") parser.add_option("-s", "--serial", action="store", type="string", dest="serial", help="serial num. of archive device") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False) return parser def update_db(flowcell, serial, debug=False): """ Creates link between flowcell and storage device over http """ url = settings.LINK_FLOWCELL_STORAGE_DEVICE_URL+'%s/%s/' % (flowcell, serial) req = urllib2.Request(url) try: response = urllib2.urlopen(req) except urllib2.URLError, e: print 'ERROR - HTTP OUTPUT (Return Code: %s); use -v/--verbose for more details.' % (e.code) if debug: print e.read() sys.exit(e.code) print "DB Update of %s & %s succeeded" % (flowcell, serial) print response.read() def process_args(parser): """ returns flowcell and serial# """ options, args = parser.parse_args() msg = [] # Only provide device or serial if options.device is not None and options.serial is not None: print "ERROR: Please only provide --device or --serial.\n" \ " The serial number is extracted automatically if the device is provided." sys.exit(2) print 'Flowcell:', options.flowcell print ' Device:', options.device print ' Serial:', options.serial if options.flowcell is None: msg.append(" --flowcell required") # if device and serial missing: if options.device is None and options.serial is None: msg.append(" --device OR --serial required") if len(msg) > 0: print '\n'.join(msg) sys.exit(3) # Update db records if options.device is not None: serial = get_hd_serial_num(options.device) update_db(flowcell=options.flowcell, serial=serial, debug=options.verbose) elif options.serial is not None: update_db(flowcell=options.flowcell, serial=options.serial, debug=options.verbose) else: msg ="FATAL should not happen error occured; i.e. the best kind!" raise ValueError, msg def main(): """ """ parser = construct_parser() process_args(parser) #print "Database Updated." sys.exit(0) if __name__ == '__main__': main()