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