X-Git-Url: http://woldlab.caltech.edu/gitweb/?a=blobdiff_plain;f=scripts%2Fhtsw-record-runfolder;fp=scripts%2Fhtsw-record-runfolder;h=288ec1ac00c1f05d327c48a0cb4cf735e16d6f3c;hb=aa63dd36b1de178acea25d43d3aaef8716763d3d;hp=0000000000000000000000000000000000000000;hpb=5eba67d1e483b3b9abbb68ec3efdb1ef0e95bf46;p=htsworkflow.git diff --git a/scripts/htsw-record-runfolder b/scripts/htsw-record-runfolder new file mode 100755 index 0000000..288ec1a --- /dev/null +++ b/scripts/htsw-record-runfolder @@ -0,0 +1,123 @@ +#!/usr/bin/env python + +from htsworkflow.util.hdquery import get_hd_serial_num +from htsworkflow.frontend import settings + +from optparse import OptionParser +import os +import re +import sys +import urllib2 +import urlparse + +runfolder_pattern = re.compile(r'[0-9]{6}_[-A-Za-z\d]+_\d+_(?P[A-Z\d]+)\.tgz') + +def extract_flowcell(runfolder_name): + path, basename = os.path.split(runfolder_name) + match = runfolder_pattern.match(basename) + if match is not None: + return match.group('flowcell') + else: + return None + +def construct_parser(): + """ + """ + msg = "usage: %prog [-d | -s ] [archived dirs]" + parser = OptionParser() + parser.add_option('-u', '--url', default=None, + help="Alternate url for marking archived flowcells") + parser.add_option("-f", "--flowcell", type="string", help="flowcell being archived") + parser.add_option("-d", "--device", type="string", + help="device flowcell is being archived to") + parser.add_option("-s", "--serial", type="string", help="serial num. of archive device") + parser.add_option("-v", "--verbose", action="store_true", default=False) + + return parser + + +def update_db(root_url, flowcells, serial, debug=False): + """ + Creates link between flowcell and storage device over http + """ + for fc in flowcells: + url = urlparse.urljoin(root_url, '%s/%s/' % (fc, 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" % (fc, 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: + parser.error("Please provide only --device or --serial.\n"\ + "The serial number is extracted automatically if the"\ + "device is provided.") + + # allow user to override the default destination URL + if options.url is not None: + root_url = options.url + else: + root_url = settings.LINK_FLOWCELL_STORAGE_DEVICE_URL + + # if device and serial missing: + if options.device is None and options.serial is None: + parser.error('One of --device or --serial is required') + + flowcells = [] + + # sanitize args + for runfolder in args: + flowcell_id = extract_flowcell(runfolder) + if flowcell_id is None: + parser.error('archive names must look like YYMMDD_MACHINE_RUN_FLOWCELLID.tgz\n'\ + '(got %s)' % (runfolder,)) + else: + flowcells.append(flowcell_id) + + if options.flowcell is not None: + flowcells.append(options.flowcell) + + if len(flowcells) == 0: + parser.error('please specify a --flowcell or list of runfolder archives\n'\ + 'for archival. I need something to do.') + + # Update db records + if options.device is not None: + serial = get_hd_serial_num(options.device) + update_db(root_url, flowcells, serial=serial, debug=options.verbose) + elif options.serial is not None: + update_db(root_url, flowcells, 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()