Actually implement the code to loop over a list of runfolders
[htsworkflow.git] / scripts / mark_archived_data
index bd83b3bc9469e5984acf8a5b09bd1ba85de0a999..288ec1ac00c1f05d327c48a0cb4cf735e16d6f3c 100755 (executable)
@@ -1,48 +1,60 @@
 #!/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 os
+import re
 import sys
-import urllib
+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():
     """
     """
-    parser = OptionParser("usage: %prog -f <flowcell> -d </dev/sdX> OR\n\t %prog -f <flowcell> -s <dev_serial_num>")
-    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",
+    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", action="store", type="string", dest="serial",
-                      help="serial num. of archive device")
-    parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False)
+    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(flowcell, serial, debug=False):
+def update_db(root_url, flowcells, serial, debug=False):
     """
     Creates link between flowcell and storage device over http
     """
-    www = urllib.urlopen(settings.LINK_FLOWCELL_STORAGE_DEVICE_URL+'%s/%s/' % (flowcell, serial))
-    httpcode = www.getcode()
-    if httpcode != 200:
-        print 'ERROR - HTTP OUTPUT (Return Code: %s); use -v/--verbose for more details.' % (httpcode)
-        if debug:
-            print www.read()
-        sys.exit(httpcode)
+    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()
     
-    print "DB Update of %s & %s succeeded" % (flowcell, serial)
-    print www.read()
-
 
 def process_args(parser):
     """
@@ -54,31 +66,44 @@ def process_args(parser):
     
     # 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")
-    
+        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:
-        msg.append("  --device OR --serial required")
-    
-    if len(msg) > 0:
-        print '\n'.join(msg)
-        sys.exit(3)
+        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(flowcell=options.flowcell, serial=serial, debug=options.verbose)
+        update_db(root_url, flowcells, serial=serial, debug=options.verbose)
     elif options.serial is not None:
-        update_db(flowcell=options.flowcell, serial=options.serial, debug=options.verbose)
+        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
@@ -95,4 +120,4 @@ def main():
     sys.exit(0)
 
 if __name__ == '__main__':
-    main()
\ No newline at end of file
+    main()