Merge branch 'master' into debianized
[htsworkflow.git] / scripts / htsw-record-runfolder
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, flowcells, serial, debug=False):
40     """
41     Creates link between flowcell and storage device over http
42     """
43     for fc in flowcells:
44         url = urlparse.urljoin(root_url, '%s/%s/' % (fc, serial))
45         
46         req = urllib2.Request(url)
47         try:
48             response = urllib2.urlopen(req)
49         except urllib2.URLError, e:
50             print 'ERROR - HTTP OUTPUT (Return Code: %s); use -v/--verbose for more details.' % (e.code)
51             if debug:
52                 print e.read()
53             sys.exit(e.code)
54         
55         print "DB Update of %s & %s succeeded" % (fc, serial)
56         print response.read()
57     
58
59 def process_args(parser):
60     """
61     returns flowcell and serial#
62     """
63     options, args = parser.parse_args()
64     
65     msg = []
66     
67     # Only provide device or serial
68     if options.device is not None and options.serial is not None:
69         parser.error("Please provide only --device or --serial.\n"\
70                      "The serial number is extracted automatically if the"\
71                      "device is provided.")
72
73     # allow user to override the default destination URL
74     if options.url is not None:
75         root_url = options.url
76     else:
77         root_url = settings.LINK_FLOWCELL_STORAGE_DEVICE_URL
78
79     # if device and serial missing:
80     if options.device is None and options.serial is None:
81         parser.error('One of --device or --serial is required')
82
83     flowcells = []
84     
85     # sanitize args    
86     for runfolder in args:
87         flowcell_id = extract_flowcell(runfolder)
88         if flowcell_id is None:
89             parser.error('archive names must look like YYMMDD_MACHINE_RUN_FLOWCELLID.tgz\n'\
90                          '(got %s)' % (runfolder,))
91         else:
92             flowcells.append(flowcell_id)
93             
94     if options.flowcell is not None:
95         flowcells.append(options.flowcell)
96         
97     if len(flowcells) == 0:
98         parser.error('please specify a  --flowcell or list of runfolder archives\n'\
99                      'for archival. I need something to do.')
100
101     # Update db records
102     if options.device is not None:
103         serial = get_hd_serial_num(options.device)
104         update_db(root_url, flowcells, serial=serial, debug=options.verbose)
105     elif options.serial is not None:
106         update_db(root_url, flowcells, serial=options.serial, debug=options.verbose)
107     else:
108         msg ="FATAL should not happen error occured; i.e. the best kind!"
109         raise ValueError, msg
110     
111     
112
113 def main():
114     """
115     """
116     parser = construct_parser()
117     process_args(parser)
118     
119     #print "Database Updated."
120     sys.exit(0)
121
122 if __name__ == '__main__':
123     main()