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