Update the inventory tracker code for the split from lanes being
authorDiane Trout <diane@caltech.edu>
Sat, 30 Jan 2010 01:28:52 +0000 (01:28 +0000)
committerDiane Trout <diane@caltech.edu>
Sat, 30 Jan 2010 01:28:52 +0000 (01:28 +0000)
in the flowcell table to their own stand-alone model.

Also I made the mark_archived_data script take a list of
runfolder archives so I can archive a whole hard disk in one go.

htsworkflow/frontend/inventory/views.py
scripts/mark_archived_data

index f1d4607da33f763039c71f4d4797cf0158cdd1b2..7ff5b401f0e9dc6f19b0d6e2f1e968789700707e 100644 (file)
@@ -48,7 +48,7 @@ def __expand_longtermstorage_context(context, item):
     for lts in item.longtermstorage_set.all():
         flowcell_list.append(lts.flowcell)
         flowcell_id_list.append(lts.flowcell.flowcell_id)
-        library_id_list.extend([ lib.library_id for lib in lts.libraries.all() ])
+        library_id_list.extend([ lib.id for lib in lts.libraries.all() ])
 
     flowcell_list.sort(__flowcell_rundate_sort)
     context['oldest_rundate'] = flowcell_list[0].run_date
@@ -295,47 +295,11 @@ def link_flowcell_and_device(request, flowcell, serial):
     
     ###########################################
     # Add Library Links to LTS
-    
-    if fc.lane_1_library not in lts.libraries.all():
-        lts.libraries.add(fc.lane_1_library)
-        LIBRARY_UPDATED = True
-        #print 1
-    
-    if fc.lane_2_library not in lts.libraries.all():
-        lts.libraries.add(fc.lane_2_library)
-        LIBRARY_UPDATED = True
-        #print 2
-    
-    if fc.lane_3_library not in lts.libraries.all():
-        lts.libraries.add(fc.lane_3_library)
-        LIBRARY_UPDATED = True
-        #print 3
-    
-    if fc.lane_4_library not in lts.libraries.all():
-        lts.libraries.add(fc.lane_4_library)
-        LIBRARY_UPDATED = True
-        #print 4
-    
-    
-    if fc.lane_5_library not in lts.libraries.all():
-        lts.libraries.add(fc.lane_5_library)
-        LIBRARY_UPDATED = True
-        #print 5
-    
-    if fc.lane_6_library not in lts.libraries.all():
-        lts.libraries.add(fc.lane_6_library)
-        LIBRARY_UPDATED = True
-        #print 6
-    
-    if fc.lane_7_library not in lts.libraries.all():
-        lts.libraries.add(fc.lane_7_library)
-        LIBRARY_UPDATED = True
-        #print 7
-    
-    if fc.lane_8_library not in lts.libraries.all():
-        lts.libraries.add(fc.lane_8_library)
-        LIBRARY_UPDATED = True
-        #print 8
+
+    for lane in fc.lane_set.all():
+        if lane.library not in lts.libraries.all():
+            lts.libraries.add(lane.library)
+            LIBRARY_UPDATED = True        
         
     # Save Changes
     lts.save()
@@ -348,4 +312,4 @@ def link_flowcell_and_device(request, flowcell, serial):
     else:
         msg.append('  No Updates Needed.')
     
-    return HttpResponse('\n'.join(msg))
\ No newline at end of file
+    return HttpResponse('\n'.join(msg))
index eadafb6099f5418ef8678736d1f0c5cbebb32bdd..6d72ab2b0d71da672052a7e98fb71d7b6cf47a49 100755 (executable)
@@ -1,39 +1,46 @@
 #!/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 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, flowcell, serial, debug=False):
     """
     Creates link between flowcell and storage device over http
     """
-    url = settings.LINK_FLOWCELL_STORAGE_DEVICE_URL+'%s/%s/' % (flowcell, serial)
+    url = urlparse.urljoin(root_url, '%s/%s/' % (flowcell, serial))
     
     req = urllib2.Request(url)
     try:
@@ -58,31 +65,47 @@ 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")
+        parser.error('One of --device or --serial is required')
+
+    flowcells = []
     
-    if len(msg) > 0:
-        print '\n'.join(msg)
-        sys.exit(3)
+    # 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.')
+
+    print flowcells
+    sys.exit(1)
     
     # 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
@@ -99,4 +122,4 @@ def main():
     sys.exit(0)
 
 if __name__ == '__main__':
-    main()
\ No newline at end of file
+    main()