E5D5 open new branch restructure_and_doc by rosjat · Pull Request #1 · python-scsi/python-scsi · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions scsi.py

This file was deleted.

23 changes: 0 additions & 23 deletions scsi_command.py

This file was deleted.

15 changes: 0 additions & 15 deletions scsi_device.py

This file was deleted.

8 changes: 5 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from distutils.core import setup, Extension
# coding: utf-8

module1 = Extension('sgio',
sources = ['sgiomodule.c'])
from distutils.core import setup, Extension
module1 = Extension('sgio.sgio',
sources = ['sgio/sgiomodule.c'])

setup (name = 'SGIO',
version = '1.0',
description = 'Module for calling ioctl(SG_IO)',
packages=['sgio','sgio.pyscsi','sgio.tests'],
ext_modules = [module1])

7 changes: 7 additions & 0 deletions sgio/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# coding: utf-8

from pyscsi import *
from tests import *
from sgio import open,close, execute

__author__ = 'mr'
2 changes: 2 additions & 0 deletions sgio/pyscsi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__all_=['scsi','scsi_cdb_inquiry','scsi_command', 'scsi_device', 'scsi_sense']
__author__ = 'mr'
46 changes: 46 additions & 0 deletions sgio/pyscsi/scsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# coding: utf-8


# Copyright (C) 2014 by Ronnie Sahlberg<ronniesahlberg@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

from scsi_device import SCSIDevice

from scsi_cdb_inquiry import Inquiry

SCSI_STATUS_GOOD = 0x00
SCSI_STATUS_CHECK_CONDITION = 0x02
SCSI_STATUS_CONDITIONS_MET = 0x04
SCSI_STATUS_BUSY = 0x08
SCSI_STATUS_RESERVATION_CONFLICT = 0x18
SCSI_STATUS_TASK_SET_FULL = 0x28
SCSI_STATUS_ACA_ACTIVE = 0x30
SCSI_STATUS_TASK_ABORTED = 0x40
SCSI_STATUS_SGIO_ERROR = 0xff

class SCSI(SCSIDevice):
'''

'''
def Inquiry(self, evpd = 0, page_code = 0, alloc_len = 96):
'''

:param evpd:
:param page_code:
:param alloc_len:
:return:
'''
return Inquiry(self, evpd, page_code, alloc_len)

41 changes: 40 additions & 1 deletion scsi_cdb_inquiry.py → sgio/pyscsi/scsi_cdb_inquiry.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
import sys
# coding: utf-8


# Copyright (C) 2014 by Ronnie Sahlberg<ronniesahlberg@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

from scsi_command import SCSICommand

#
Expand All @@ -13,14 +30,32 @@
DEVICE_IDENTIFICATION = 0x83

class Inquiry(SCSICommand):
'''

'''
def __init__(self, dev, evpd = 0, page_code = 0, alloclen = 96):
'''

:param dev:
:param evpd:
:param page_code:
:param alloclen:
:return:
'''
self._evpd = evpd
self._page_code = page_code
SCSICommand.__init__(self, dev, 0, alloclen)
self.cdb = self.build_cdb(evpd, page_code, alloclen)
self.execute()

def build_cdb(self, evpd, page_code, alloclen ):
'''

:param evpd:
:param page_code:
:param alloclen:
:return:
'''
cdb = bytearray([SCSI_CDB_INQUIRY, 0x00, 0x00, 0x00, 0x00, 0x00])
if (evpd):
cdb[1] |= 0x01
Expand All @@ -30,6 +65,10 @@ def build_cdb(self, evpd, page_code, alloclen ):
return cdb

def unmarshall(self):
'''

:return:
'''
if self._evpd == 0:
self.add_result('peripheral_qualifier', self.datain[0] >> 5)
self.add_result('peripheral_qualifier', self.datain[0] >> 5)
Expand Down
70 changes: 70 additions & 0 deletions sgio/pyscsi/scsi_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# coding: utf-8


# Copyright (C) 2014 by Ronnie Sahlberg<ronniesahlberg@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

class SCSICommand:
'''

'''
def __init__(self, dev, dataout_alloclen, datain_alloclen):
'''

:param dev:
:param dataout_alloclen:
:param datain_alloclen:
:return:
'''
self.dev = dev
self.sense = bytearray(32)
self.dataout = bytearray(dataout_alloclen)
self.datain = bytearray(datain_alloclen)
self._result = {}

def execute(self):
'''

:return:
'''
self.dev.execute(self.cdb, self.dataout,
self.datain, self.sense)
self.unmarshall()

@property
def result(self):
'''

:return:
'''
return self._result

@result.setter
def result(self, value):
'''

:param value:
:return:
'''
self._result = value

def add_result(self, key, value):
'''

:param key:
:param value:
:return:
'''
self.result.update({key:value})
49 changes: 49 additions & 0 deletions sgio/pyscsi/scsi_device.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# coding: utf-8


# Copyright (C) 2014 by Ronnie Sahlberg<ronniesahlberg@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

import scsi
import scsi_sense
import sgio

class SCSIDevice:
'''

'''
def __init__(self, device):
'''

:param device:
:return:
'''
self._fd = sgio.open(device)

def execute(self, cdb, dataout, datain, sense):
'''

:param cdb:
:param dataout:
:param datain:
:param sense:
:return:
'''
status = sgio.execute(self._fd, cdb, dataout, datain, sense)
if status == scsi.SCSI_STATUS_CHECK_CONDITION:
raise scsi_sense.SCSICheckCondition(sense)
if status == scsi.SCSI_STATUS_SGIO_ERROR:
raise scsi_sense.SCSISGIOError()

47 changes: 40 additions & 7 deletions scsi_sense.py → sgio/pyscsi/scsi_sense.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# coding: utf-8

# Copyright (C) 2014 by Ronnie Sahlberg<ronniesahlberg@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

#
# SPC4 4.6 Sense Data
#
Expand All @@ -6,14 +23,24 @@
SENSE_FORMAT_CURRENT_DESCRIPTOR = 0x72
SENSE_FORMAT_DEFERRED_DESCRIPTOR = 0x73

sense_key_dict = { 0x00: 'No Sense', 0x01: 'Recovered Error',
0x02: 'Not Ready', 0x03: 'Medium Error',
0x04: 'Hardware Error', 0x05: 'Illegal Request',
0x06: 'Unit Attention', 0x07: 'Data Protect',
0x08: 'Blank Check', 0x09: 'Vendor Specific',
0x0a: 'Copy Aborted', 0x0b: 'Aborted Command',
0x0d: 'Volume Overflow', 0x0e: 'Miscompare',
# dict with common key code qualifiers
sense_key_dict = { 0x00: 'No Sense',
0x01: 'Recovered Error',
0x02: 'Not Ready',
0x03: 'Medium Error',
0x04: 'Hardware Error',
0x05: 'Illegal Request',
0x06: 'Unit Attention',
0x07: 'Data Protect',
0x08: 'Blank Check',
0x09: 'Vendor Specific',
0x0a: 'Copy Aborted',
0x0b: 'Aborted Command',
0x0d: 'Volume Overflow',
0x0e: 'Miscompare',
0x0f: 'Completed'}

# dict with additional sense data
sense_ascq_dict = { 0x2400: 'Invalid Field In CDB',
0x2401: 'CDB Decryption Error',
0x2404: 'Security Audit Value Frozen',
Expand All @@ -23,7 +50,13 @@
0x2408: 'Invalid XCDB'
}

# Exception Class for a SCSI REQUEST SENSE command
class SCSICheckCondition(Exception):
'''Exception raised for errors in the SCSIDevice execute method

Attributes:
sense -- bytearray with additional data
'''
def __init__(self, sense):
self.valid = sense[0] & 0x80
self.response_code = sense[0] & 0x7f
Expand Down
Loading
0