From 15cea03cf8d781be1fdb2600200613138a2e024d Mon Sep 17 00:00:00 2001 From: Diane Trout Date: Mon, 28 Sep 2009 19:18:08 +0000 Subject: [PATCH] Modify hdquery to not die when being imported on non-linux systems. (Makes nosetests --with-doctests work better) --- htsworkflow/util/hdquery.py | 44 +++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/htsworkflow/util/hdquery.py b/htsworkflow/util/hdquery.py index ff16387..57c05e6 100644 --- a/htsworkflow/util/hdquery.py +++ b/htsworkflow/util/hdquery.py @@ -1,25 +1,31 @@ import os +import sys -#try: -import py_sg -#except: -# print 'ERROR: Please install py_sg (easy_install py_sg)' +try: + import py_sg -def get_hd_serial_num(device): - """ - device = '/dev/sdX' + def get_hd_serial_num(device): + """ + device = '/dev/sdX' + + returns hard drive serial number for a device; requires read permissions. + """ + fd = os.open(device, os.O_RDONLY) + + # fd: device object + # \x12: INQUIRY CMD; \x01: EVPD bit set to 1; \x80: Unit Serial Number page + # See http://en.wikipedia.org/wiki/SCSI_Inquiry_Command for helpful chart + # ##: # byte buffer for returned data + data = py_sg.read(fd, "\x12\x01\x80", 32) + + # Remove extra \x00's, and split remaining data into two chunks, + # the 2nd of which is the serial number + return data.strip('\x00').split()[1] - returns hard drive serial number for a device; requires read permissions. - """ - fd = os.open(device, os.O_RDONLY) - - # fd: device object - # \x12: INQUIRY CMD; \x01: EVPD bit set to 1; \x80: Unit Serial Number page - # See http://en.wikipedia.org/wiki/SCSI_Inquiry_Command for helpful chart - # ##: # byte buffer for returned data - data = py_sg.read(fd, "\x12\x01\x80", 32) +except ImportError, e: + print >>sys.stderr, "hdquery requires py_sg" + + def get_hd_serial_num(device): + raise NotImplemented('get_hd_serial_num is not available for anything other than linux') - # Remove extra \x00's, and split remaining data into two chunks, - # the 2nd of which is the serial number - return data.strip('\x00').split()[1] -- 2.30.2