convert some print statements to logger messages
[htsworkflow.git] / htsworkflow / util / hdquery.py
1 import os
2 import sys
3 import logging
4
5 LOGGER = logging.getLogger(__name__)
6
7 try:
8     import py_sg
9     
10     
11     def get_hd_serial_num(device):
12         """
13         device = '/dev/sdX'
14         
15         returns hard drive serial number for a device; requires read permissions.
16         """
17         fd = os.open(device, os.O_RDONLY)
18         
19         # fd: device object
20         # \x12: INQUIRY CMD; \x01: EVPD bit set to 1; \x80: Unit Serial Number page
21         #  See http://en.wikipedia.org/wiki/SCSI_Inquiry_Command for helpful chart
22         # ##: # byte buffer for returned data
23         data = py_sg.read(fd, "\x12\x01\x80", 32)
24         
25         # Remove extra \x00's, and split remaining data into two chunks,
26         #  the 2nd of which is the serial number
27         return data.strip('\x00').split()[1]
28     
29 except ImportError as e:
30     LOGGER.error("hdquery requires py_sg")
31
32     def get_hd_serial_num(device):
33         raise NotImplemented('get_hd_serial_num is not available for anything other than linux')
34