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