e760318f3d8799a603e8ea53940906173e79e0cb
[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
8 #from django.conf import settings
9 from optparse import OptionParser
10
11 import sys
12 import urllib
13
14 #FIXME: Remove hard coded URL
15 HTSW_URL = "http://localhost:8000/inventory/lts/link/"
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     
28     return parser
29
30
31 def update_db(flowcell, serial):
32     """
33     Creates link between flowcell and storage device over http
34     """
35     www = urllib.urlopen(HTSW_URL+'%s/%s/' % (flowcell, serial))
36     httpcode = www.getcode()
37     if httpcode != 200:
38         print 'ERROR - HTTP OUTPUT:'
39         print www.read()
40         sys.exit(httpcode)
41     
42     print "DB Update of %s & %s succeeded" % (flowcell, serial)
43
44
45 def process_args(parser):
46     """
47     returns flowcell and serial#
48     """
49     options, args = parser.parse_args()
50     
51     msg = []
52     
53     # Only provide device or serial
54     if options.device is not None and options.serial is not None:
55         print "ERROR: Please only provide --device or --serial.\n" \
56               "  The serial number is extracted automatically if the device is provided."
57         sys.exit(2)
58     
59     print options.flowcell
60     print options.device
61     print options.serial
62     
63     if options.flowcell is None:
64         msg.append("  --flowcell required")
65     
66     # if device and serial missing:
67     if options.device is None and options.serial is None:
68         msg.append("  --device OR --serial required")
69     
70     if len(msg) > 0:
71         print '\n'.join(msg)
72         sys.exit(3)
73     
74     # Update db records
75     if options.device is not None:
76         serial = get_hd_serial_num(options.device)
77         update_db(flowcell=options.flowcell, serial=serial)
78     elif options.serial is not None:
79         update_db(flowcell=options.flowcell, serial=options.serial)
80     else:
81         msg ="FATAL should not happen error occured; i.e. the best kind!"
82         raise ValueError, msg
83     
84     
85
86 def main():
87     """
88     """
89     parser = construct_parser()
90     process_args(parser)
91     
92     #print "Database Updated."
93     sys.exit(0)
94
95 if __name__ == '__main__':
96     main()