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, flowcell, serial, debug=False):
41 Creates link between flowcell and storage device over http
43 url = urlparse.urljoin(root_url, '%s/%s/' % (flowcell, serial))
45 req = urllib2.Request(url)
47 response = urllib2.urlopen(req)
48 except urllib2.URLError, e:
49 print 'ERROR - HTTP OUTPUT (Return Code: %s); use -v/--verbose for more details.' % (e.code)
54 print "DB Update of %s & %s succeeded" % (flowcell, serial)
58 def process_args(parser):
60 returns flowcell and serial#
62 options, args = parser.parse_args()
66 # Only provide device or serial
67 if options.device is not None and options.serial is not None:
68 parser.error("Please provide only --device or --serial.\n"\
69 "The serial number is extracted automatically if the"\
70 "device is provided.")
72 # allow user to override the default destination URL
73 if options.url is not None:
74 root_url = options.url
76 root_url = settings.LINK_FLOWCELL_STORAGE_DEVICE_URL
78 # if device and serial missing:
79 if options.device is None and options.serial is None:
80 parser.error('One of --device or --serial is required')
85 for runfolder in args:
86 flowcell_id = extract_flowcell(runfolder)
87 if flowcell_id is None:
88 parser.error('archive names must look like YYMMDD_MACHINE_RUN_FLOWCELLID.tgz\n'\
89 '(got %s)' % (runfolder,))
91 flowcells.append(flowcell_id)
93 if options.flowcell is not None:
94 flowcells.append(options.flowcell)
96 if len(flowcells) == 0:
97 parser.error('please specify a --flowcell or list of runfolder archives\n'\
98 'for archival. I need something to do.')
104 if options.device is not None:
105 serial = get_hd_serial_num(options.device)
106 update_db(root_url, flowcells, serial=serial, debug=options.verbose)
107 elif options.serial is not None:
108 update_db(root_url, flowcells, serial=options.serial, debug=options.verbose)
110 msg ="FATAL should not happen error occured; i.e. the best kind!"
111 raise ValueError, msg
118 parser = construct_parser()
121 #print "Database Updated."
124 if __name__ == '__main__':