Icon added.
[htsworkflow.git] / scripts / mark_archived_data
1 #!/usr/bin/env python
2
3 #import os
4 #os.environ['DJANGO_SETTINGS_MODULE'] = 'htsworkflow.frontend.settings'
5
6 from htsworkflow.util.hdquery import get_hd_serial_num
7 from htsworkflow.frontend import settings
8
9 #from django.conf import settings
10 from optparse import OptionParser
11
12 import sys
13 import urllib2
14
15
16
17 def construct_parser():
18     """
19     """
20     parser = OptionParser("usage: %prog -f <flowcell> -d </dev/sdX> OR\n\t %prog -f <flowcell> -s <dev_serial_num>")
21     parser.add_option("-f", "--flowcell", action="store", type="string", dest="flowcell",
22                       help="flowcell being archived")
23     parser.add_option("-d", "--device", action="store", type="string", dest="device",
24                       help="device flowcell is being archived to")
25     parser.add_option("-s", "--serial", action="store", type="string", dest="serial",
26                       help="serial num. of archive device")
27     parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False)
28     
29     return parser
30
31
32 def update_db(flowcell, serial, debug=False):
33     """
34     Creates link between flowcell and storage device over http
35     """
36     url = settings.LINK_FLOWCELL_STORAGE_DEVICE_URL+'%s/%s/' % (flowcell, serial)
37     
38     req = urllib2.Request(url)
39     try:
40         response = urllib2.urlopen(req)
41     except urllib2.URLError, e:
42         print 'ERROR - HTTP OUTPUT (Return Code: %s); use -v/--verbose for more details.' % (e.code)
43         if debug:
44             print e.read()
45         sys.exit(e.code)
46     
47     print "DB Update of %s & %s succeeded" % (flowcell, serial)
48     print response.read()
49
50
51 def process_args(parser):
52     """
53     returns flowcell and serial#
54     """
55     options, args = parser.parse_args()
56     
57     msg = []
58     
59     # Only provide device or serial
60     if options.device is not None and options.serial is not None:
61         print "ERROR: Please only provide --device or --serial.\n" \
62               "  The serial number is extracted automatically if the device is provided."
63         sys.exit(2)
64     
65     print 'Flowcell:', options.flowcell
66     print '  Device:', options.device
67     print '  Serial:', options.serial
68     
69     if options.flowcell is None:
70         msg.append("  --flowcell required")
71     
72     # if device and serial missing:
73     if options.device is None and options.serial is None:
74         msg.append("  --device OR --serial required")
75     
76     if len(msg) > 0:
77         print '\n'.join(msg)
78         sys.exit(3)
79     
80     # Update db records
81     if options.device is not None:
82         serial = get_hd_serial_num(options.device)
83         update_db(flowcell=options.flowcell, serial=serial, debug=options.verbose)
84     elif options.serial is not None:
85         update_db(flowcell=options.flowcell, serial=options.serial, debug=options.verbose)
86     else:
87         msg ="FATAL should not happen error occured; i.e. the best kind!"
88         raise ValueError, msg
89     
90     
91
92 def main():
93     """
94     """
95     parser = construct_parser()
96     process_args(parser)
97     
98     #print "Database Updated."
99     sys.exit(0)
100
101 if __name__ == '__main__':
102     main()