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