--- /dev/null
+#!/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<flowcell>[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 </dev/sdX> | -s <serial_number] [-f <flowcell>] [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()