Modify hdquery to not die when being imported on non-linux systems.
authorDiane Trout <diane@caltech.edu>
Mon, 28 Sep 2009 19:18:08 +0000 (19:18 +0000)
committerDiane Trout <diane@caltech.edu>
Mon, 28 Sep 2009 19:18:08 +0000 (19:18 +0000)
(Makes nosetests --with-doctests work better)

htsworkflow/util/hdquery.py

index ff163874e40675f0b5964743d5fe08e0bacd3216..57c05e6025bf847e31e32875f44b34ea49c6adc6 100644 (file)
@@ -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]