Update the inventory tracker code for the split from lanes being
[htsworkflow.git] / scripts / mark_archived_data
1 #!/usr/bin/env python
2
3 from htsworkflow.util.hdquery import get_hd_serial_num
4 from htsworkflow.frontend import settings
5
6 from optparse import OptionParser
7 import os
8 import re
9 import sys
10 import urllib2
11 import urlparse
12
13 runfolder_pattern = re.compile(r'[0-9]{6}_[-A-Za-z\d]+_\d+_(?P<flowcell>[A-Z\d]+)\.tgz')
14
15 def extract_flowcell(runfolder_name):
16     path, basename = os.path.split(runfolder_name)
17     match = runfolder_pattern.match(basename)
18     if match is not None:
19         return match.group('flowcell')
20     else:
21         return None
22     
23 def construct_parser():
24     """
25     """
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)
35     
36     return parser
37
38
39 def update_db(root_url, flowcell, serial, debug=False):
40     """
41     Creates link between flowcell and storage device over http
42     """
43     url = urlparse.urljoin(root_url, '%s/%s/' % (flowcell, serial))
44     
45     req = urllib2.Request(url)
46     try:
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)
50         if debug:
51             print e.read()
52         sys.exit(e.code)
53     
54     print "DB Update of %s & %s succeeded" % (flowcell, serial)
55     print response.read()
56
57
58 def process_args(parser):
59     """
60     returns flowcell and serial#
61     """
62     options, args = parser.parse_args()
63     
64     msg = []
65     
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.")
71
72     # allow user to override the default destination URL
73     if options.url is not None:
74         root_url = options.url
75     else:
76         root_url = settings.LINK_FLOWCELL_STORAGE_DEVICE_URL
77
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')
81
82     flowcells = []
83     
84     # sanitize args    
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,))
90         else:
91             flowcells.append(flowcell_id)
92             
93     if options.flowcell is not None:
94         flowcells.append(options.flowcell)
95         
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.')
99
100     print flowcells
101     sys.exit(1)
102     
103     # Update db records
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)
109     else:
110         msg ="FATAL should not happen error occured; i.e. the best kind!"
111         raise ValueError, msg
112     
113     
114
115 def main():
116     """
117     """
118     parser = construct_parser()
119     process_args(parser)
120     
121     #print "Database Updated."
122     sys.exit(0)
123
124 if __name__ == '__main__':
125     main()