File: mtx.py

package info (click to toggle)
mtx 1.2.16rel-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 668 kB
  • ctags: 696
  • sloc: ansic: 4,382; sh: 2,489; python: 203; makefile: 126; perl: 117
file content (306 lines) | stat: -rw-r--r-- 8,289 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# Copyright 2000 Enhanced Software Technologies Inc.
# Released under Free Software Foundation's General Public License,
# Version 2 or above
#
# This is an example of how to parse the 'mtx' output from a scripting
# language. 
#
# Routine to call 'mtx' and read status, or
# whatever.
#
# We do this here rather than creating a Python "C" module because:
#  1) Reduces complexity of compile environment
#  2) Easier debugging.
#  3) More in keeping with the Unix philosophy of things.
#  4) Easier to add new functionality. 
#
#

import string
import os
import time # we can do some waiting here...

def readpipe(command):
    result=""
    
    infile=os.popen(command,"r")

    try:
        s=infile.read()
    except:
        s=""
        pass
    if not s:
        return None  # didn't get anything :-(. 
    result=result+s
    return result




# these are returned by the mtx.status routine:
class slotstatus:
    def __init__(self,slotnum,middle,barcode=None):
        middle=string.strip(middle)
        try:
            left,right=string.split(middle," ")
        except:
            left=middle
            right=None
            pass
        # Note: We will not be able to test this at the moment since the
        # 220 has no import/export port!
        if right=="IMPORT/EXPORT":
            self.importexport=1
        else:
            self.importexport=0
            pass
        self.slotnum=int(slotnum) # make sure it's an integer...
        if left=="Full":
            self.full=1
        else:
            self.full=None
            pass
        if not barcode:
            self.voltag=None
        else:
            l=string.split(barcode,"=",1)
            self.voltag=l[1]
            pass
        return
    pass

# Drive status lines have this format:
#Data Transfer Element 0:Full (Storage Element 10 Loaded):VolumeTag = B0000009H
#Data Transfer Element 1:Empty

class drivestatus:
    def __init__(self,slotnum,middle,barcode=None):
        self.slotnum=slotnum
        if middle=="Empty":
            self.full=None
            self.origin=None
            self.voltag=None
            return
        else:
            self.full=1
            pass

        # okay, we know we have a tape in the drive. 
        # split and find out our origin: we will always have
        # an origin, even if the #$@% mtx program had to dig one
        # out of the air:

        l=string.split(middle," ")
        self.origin=int(l[3])

        if not barcode:
            self.voltag=None # barcode of this element.
        else:
            l=string.split(barcode," ")
            self.voltag=string.strip(l[2])
            pass
        return
    pass

# This is the return value for mtx.status. 
class mtxstatus:
    def __init__(self):
        self.numdrives=None
        self.numslots=None
        self.numexport=None
        self.drives=[]  # a list of drivestatus instances.
        self.slots=[]   # a list of slotstatus instances
        self.export=[]  # another list of slotstatus instances
        return
    pass

# call 'mtx' and get barcode info, if possible:
# okay, we now have a string that consists of a number of lines.
# we want to explode this string into its component parts.
# Example format:
# Storage Changer /dev/sgd:2 Drives, 21 Slots
# Data Transfer Element 0:Full (Storage Element '5' Loaded) 
# Data Transfer Element 1:Empty
# Storage Element 1:Full :VolumeTag=CLNA0001
# Storage Element 2:Full :VolumeTag=B0000009
# Storage Element 3:Full :VolumeTag=B0000010
# ....
# What we want to do, then, is:
# 1) Turn it into lines by doing a string.split on newline.
# 2) Split the 1st line on ":" to get left and right.
# 3) Split the right half on space to get #drives "Drives," #slots
# 4) process the drives (Full,Empty, etc.)
# 5) For each of the remaining lines: Split on ':'
# 6) Full/Empty status is in 2)
#
configdir="/opt/brupro/bin"  # sigh. 

def status(device):
    retval=mtxstatus()
    command="%s/mtx -f %s status" % (configdir,device)
    result=readpipe(command)
    if not result:
        return None  # sorry! 
    # now to parse:

    try:
        lines=string.split(result,"\n")
    except:
        return None # sorry, no status!
    
    # print "DEBUG:lines=",lines

    try:
        l=string.split(lines[0],":")
    except:
        return None # sorry, no status!
    
    # print "DEBUG:l=",l
    leftside=l[0]
    rightside=l[1]
    if len(l) > 2:
        barcode=l[3]
    else:
        barcode=None
        pass
    t=string.split(rightside)
    retval.numdrives=int(t[0])
    retval.numslots=int(t[2])
    
    for s in lines[1:]:
        if not s:
            continue # skip blank lines!
        #print "DEBUG:s=",s
        parts=string.split(s,":")
        leftpart=string.split(parts[0])
        rightpart=parts[1]
        try:
            barcode=parts[2]
        except:
            barcode=None
            pass
        #print "DEBUG:leftpart=",leftpart
        if leftpart[0]=="Data" and leftpart[1]=="Transfer":
            retval.drives.append(drivestatus(leftpart[3],rightpart,barcode))
            pass
        if leftpart[0]=="Storage" and leftpart[1]=="Element":
            element=slotstatus(leftpart[2],rightpart,barcode)
            if element.importexport:
                retval.export.append(element)
            else:
                retval.slots.append(element)
                pass
            pass
        continue

    return retval

# Output of a mtx inquiry looks like:
#
#Product Type: Medium Changer
#Vendor ID: 'EXABYTE '
#Product ID: 'Exabyte EZ17    '
#Revision: '1.07'
#Attached Changer: No
#
# We simply return a hash table with these values { left:right } format. 

def mtxinquiry(device):
    command="%s/mtx -f %s inquiry" % (configdir,device)
    str=readpipe(command) # calls the command, returns all its data.

    str=string.strip(str)
    lines=string.split(str,"\n")
    retval={}
    for l in lines:
        # DEBUG #
        l=string.strip(l)
        print "splitting line: '",l,"'"
        idx,val=string.split(l,':',1)
        val=string.strip(val)
        if val[0]=="'":
            val=val[1:-1] # strip off single quotes, sigh. 
            pass 
        retval[idx]=val
        continue
    return retval

# Now for the easy part:

def load(device,slot,drive=0):
    command="%s/mtx -f %s load %s %s >/dev/null " % (configdir,device,slot,drive)
    status=os.system(command)
    return status

def unload(device,slot,drive=0):
    command="%s/mtx -f %s unload %s %s >/dev/null " % (configdir,device,slot,drive)
    return os.system(command)

def inventory(device):
    command="%s/mtx -f %s inventory >/dev/null " % (configdir,device)
    return os.system(command)

def wait_for_inventory(device):
    # loop while we have an error return...
    errcount=0
    while inventory(device):
        if errcount==0:
            print "Waiting for loader '%s'" % device
            pass
        time.sleep(1)
        try:
            s=status(device)
        except:
            s=None
            pass
        if s:
            return 0 # well, whatever we're doing, we're inventoried :-(
        errcount=errcount+1
        if errcount==600:   # we've been waiting for 10 minutes :-(
            return 1 # sorry!
        continue
    return 0  # we succeeded!

# RCS REVISION LOG:
# $Log: mtx.py,v $
# Revision 1.1.1.1  2001/06/05 17:10:51  elgreen
# Initial import into SourceForge
#
# Revision 1.2  2000/12/22 14:17:19  eric
# mtx 1.2.11pre1
#
# Revision 1.14  2000/11/12 20:35:29  eric
# do string.strip on the voltag
#
# Revision 1.13  2000/11/04 00:33:38  eric
# if we can get an inventory on the loader after we send it 'mtx inventory',
# then obviously we managed to do SOMETHING.
#
# Revision 1.12  2000/10/28 00:04:34  eric
# added wait_for_inventory command
#
# Revision 1.11  2000/10/27 23:27:58  eric
# Added inventory command...
#
# Revision 1.10  2000/10/01 01:06:29  eric
# evening checkin
#
# Revision 1.9  2000/09/29 02:49:29  eric
# evening checkin
#
# Revision 1.8  2000/09/02 01:05:33  eric
# Evening Checkin
#
# Revision 1.7  2000/09/01 00:08:11  eric
# strip lines in mtxinquiry
#
# Revision 1.6  2000/09/01 00:05:33  eric
# debugging
#
# Revision 1.5  2000/08/31 23:46:01  eric
# fix def:
#
# Revision 1.4  2000/08/31 23:44:06  eric
# =->==
#