3 from htsworkflow.util.hdquery import get_hd_serial_num
4 from htsworkflow.frontend import settings
6 from optparse import OptionParser
13 runfolder_pattern = re.compile(r'[0-9]{6}_[-A-Za-z\d]+_\d+_(?P<flowcell>[A-Z\d]+)\.tgz')
15 def extract_flowcell(runfolder_name):
16 path, basename = os.path.split(runfolder_name)
17 match = runfolder_pattern.match(basename)
19 return match.group('flowcell')
23 def construct_parser():
26 msg = "usage: %prog [-d </dev/sdX> | -s <serial_number] [-f <flowcell>] [archived dirs]"
27 parser = OptionParser()
28 parser.add_option('-u', '--url', default=None,
29 help="Alternate url for marking archived flowcells")
30 parser.add_option("-f", "--flowcell", type="string", help="flowcell being archived")
31 parser.add_option("-d", "--device", type="string",
32 help="device flowcell is being archived to")
33 parser.add_option("-s", "--serial", type="string", help="serial num. of archive device")
34 parser.add_option("-v", "--verbose", action="store_true", default=False)
39 def update_db(root_url, flowcells, serial, debug=False):
41 Creates link between flowcell and storage device over http
44 url = urlparse.urljoin(root_url, '%s/%s/' % (fc, serial))
46 req = urllib2.Request(url)
48 response = urllib2.urlopen(req)
49 except urllib2.URLError, e:
50 print 'ERROR - HTTP OUTPUT (Return Code: %s); use -v/--verbose for more details.' % (e.code)
55 print "DB Update of %s & %s succeeded" % (fc, serial)
59 def process_args(parser):
61 returns flowcell and serial#
63 options, args = parser.parse_args()
67 # Only provide device or serial
68 if options.device is not None and options.serial is not None:
69 parser.error("Please provide only --device or --serial.\n"\
70 "The serial number is extracted automatically if the"\
71 "device is provided.")
73 # allow user to override the default destination URL
74 if options.url is not None:
75 root_url = options.url
77 root_url = settings.LINK_FLOWCELL_STORAGE_DEVICE_URL
79 # if device and serial missing:
80 if options.device is None and options.serial is None:
81 parser.error('One of --device or --serial is required')
86 for runfolder in args:
87 flowcell_id = extract_flowcell(runfolder)
88 if flowcell_id is None:
89 parser.error('archive names must look like YYMMDD_MACHINE_RUN_FLOWCELLID.tgz\n'\
90 '(got %s)' % (runfolder,))
92 flowcells.append(flowcell_id)
94 if options.flowcell is not None:
95 flowcells.append(options.flowcell)
97 if len(flowcells) == 0:
98 parser.error('please specify a --flowcell or list of runfolder archives\n'\
99 'for archival. I need something to do.')
102 if options.device is not None:
103 serial = get_hd_serial_num(options.device)
104 update_db(root_url, flowcells, serial=serial, debug=options.verbose)
105 elif options.serial is not None:
106 update_db(root_url, flowcells, serial=options.serial, debug=options.verbose)
108 msg ="FATAL should not happen error occured; i.e. the best kind!"
109 raise ValueError, msg
116 parser = construct_parser()
119 #print "Database Updated."
122 if __name__ == '__main__':