Skip to content

Commit 67a114b

Browse files
Add py3-compatible wrapper for commands.getstatusoutput()
Fix the code base to work at least with one Python version(Python2) After the previous commit updated the code base to work with the exit status returned by Python3 getstatusoutput(), add a compatible wrapper for commands.getstatusoutput() to have one Python version functional. Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com>
1 parent cf065a4 commit 67a114b

10 files changed

+60
-57
lines changed

XSConsoleData.py

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@
2626
from XSConsoleState import *
2727
from XSConsoleUtils import *
2828

29+
# PR #22: Add py3-compatible wrapper for commands.getstatusoutput() to keep the code base functional:
30+
if sys.version_info >= (3, 0):
31+
getoutput = subprocess.getoutput
32+
getstatusoutput = subprocess.getstatusoutput
33+
34+
else:
35+
import commands
36+
37+
getoutput = commands.getoutput
38+
39+
def getstatusoutput(command):
40+
"""getstatusoutput with status = exit code, compatible with Python3 getstatusoutput()"""
41+
42+
status, output = commands.getstatusoutput(command)
43+
return os.WEXITSTATUS(status), output # https://github.com/benjaminp/six/issues/207
44+
45+
2946
class DataMethod:
3047
def __init__(self, inSend, inName):
3148
self.send = inSend
@@ -98,31 +115,31 @@ def Create(self):
98115
self.ReadTimezones()
99116
self.ReadKeymaps()
100117

101-
(status, output) = subprocess.getstatusoutput("dmidecode")
118+
(status, output) = getstatusoutput("dmidecode")
102119
if status != 0:
103120
# Use test dmidecode file if there's no real output
104-
(status, output) = subprocess.getstatusoutput("/bin/cat ./dmidecode.txt")
121+
(status, output) = getstatusoutput("/bin/cat ./dmidecode.txt")
105122

106123
if status == 0:
107124
self.ScanDmiDecode(output.split("\n"))
108125

109-
(status, output) = subprocess.getstatusoutput("/sbin/lspci -m")
126+
(status, output) = getstatusoutput("/sbin/lspci -m")
110127
if status != 0:
111-
(status, output) = subprocess.getstatusoutput("/usr/bin/lspci -m")
128+
(status, output) = getstatusoutput("/usr/bin/lspci -m")
112129

113130
if status == 0:
114131
self.ScanLspci(output.split("\n"))
115132

116133
if os.path.isfile("/usr/bin/ipmitool"):
117-
(status, output) = subprocess.getstatusoutput("/usr/bin/ipmitool mc info")
134+
(status, output) = getstatusoutput("/usr/bin/ipmitool mc info")
118135
if status == 0:
119136
self.ScanIpmiMcInfo(output.split("\n"))
120137

121-
(status, output) = subprocess.getstatusoutput("/bin/cat /etc/xensource-inventory")
138+
(status, output) = getstatusoutput("/bin/cat /etc/xensource-inventory")
122139
if status == 0:
123140
self.ScanInventory(output.split("\n"))
124141

125-
(status, output) = subprocess.getstatusoutput("/usr/bin/openssl x509 -in %s/xapi-ssl.pem -fingerprint -noout" % (Config.Inst().XCPConfigDir()))
142+
(status, output) = getstatusoutput("/usr/bin/openssl x509 -in %s/xapi-ssl.pem -fingerprint -noout" % (Config.Inst().XCPConfigDir()))
126143
if status == 0:
127144
fp = output.split("=")
128145
if len(fp) >= 2:
@@ -454,30 +471,30 @@ def LoggingDestinationSet(self, inDestination):
454471
self.session.xenapi.host.syslog_reconfigure(self.host.opaqueref())
455472

456473
def UpdateFromResolveConf(self):
457-
(status, output) = subprocess.getstatusoutput("/usr/bin/grep -v \"^;\" /etc/resolv.conf")
474+
(status, output) = getstatusoutput("/usr/bin/grep -v \"^;\" /etc/resolv.conf")
458475
if status == 0:
459476
self.ScanResolvConf(output.split("\n"))
460477

461478
def UpdateFromSysconfig(self):
462-
(status, output) = subprocess.getstatusoutput("/bin/cat /etc/sysconfig/network")
479+
(status, output) = getstatusoutput("/bin/cat /etc/sysconfig/network")
463480
if status == 0:
464481
self.ScanSysconfigNetwork(output.split("\n"))
465482

466483
def UpdateFromHostname(self):
467-
(status, output) = subprocess.getstatusoutput("/bin/cat /etc/hostname")
484+
(status, output) = getstatusoutput("/bin/cat /etc/hostname")
468485
if status == 0:
469486
self.ScanHostname(output.split("\n"))
470487

471488
def UpdateFromNTPConf(self):
472-
(status, output) = subprocess.getstatusoutput("/bin/cat /etc/chrony.conf")
489+
(status, output) = getstatusoutput("/bin/cat /etc/chrony.conf")
473490
if status == 0:
474491
self.ScanNTPConf(output.split("\n"))
475492

476493
def StringToBool(self, inString):
477494
return inString.lower().startswith('true')
478495

479496
def RootLabel(self):
480-
output = subprocess.getoutput('/bin/cat /proc/cmdline')
497+
output = getoutput('/bin/cat /proc/cmdline')
481498
match = re.search(r'root=\s*LABEL\s*=\s*(\S+)', output)
482499
if match:
483500
retVal = match.group(1)
@@ -675,7 +692,7 @@ def ScanIpmiMcInfo(self, inLines):
675692
self.data['bmc']['version'] = match.group(1)
676693

677694
def ScanService(self, service):
678-
(status, output) = subprocess.getstatusoutput("systemctl is-enabled %s" % (service,))
695+
(status, output) = getstatusoutput("systemctl is-enabled %s" % (service,))
679696
self.data['chkconfig'][service] = status == 0
680697

681698
def ScanResolvConf(self, inLines):
@@ -779,7 +796,7 @@ def TimezoneSet(self, inTimezone):
779796
cfg.write('/etc/sysconfig/clock')
780797

781798
def CurrentTimeString(self):
782-
return subprocess.getoutput('/bin/date -R')
799+
return getoutput('/bin/date -R')
783800

784801
def ReadKeymaps(self):
785802
self.data['keyboard'] = {
@@ -811,7 +828,7 @@ def KeymapSet(self, inKeymap):
811828

812829
keymapParam = ShellUtils.MakeSafeParam(inKeymap)
813830
# Load the keymap now
814-
status, output = subprocess.getstatusoutput('/bin/loadkeys "'+keymapParam+'"')
831+
status, output = getstatusoutput('/bin/loadkeys "'+keymapParam+'"')
815832
if status != 0:
816833
raise Exception(output)
817834

@@ -914,7 +931,7 @@ def ReconfigureManagement(self, inPIF, inMode, inIP, inNetmask, inGateway, in
914931
self.RequireSession()
915932
self.session.xenapi.PIF.reconfigure_ip(inPIF['opaqueref'], inMode, inIP, inNetmask, inGateway, FirstValue(inDNS, ''))
916933
self.session.xenapi.host.management_reconfigure(inPIF['opaqueref'])
917-
status, output = subprocess.getstatusoutput('%s host-signal-networking-change' % (Config.Inst().XECLIPath()))
934+
status, output = getstatusoutput('%s host-signal-networking-change' % (Config.Inst().XECLIPath()))
918935
if status != 0:
919936
raise Exception(output)
920937
finally:
@@ -1090,32 +1107,32 @@ def StartXAPI(self):
10901107
State.Inst().SaveIfRequired()
10911108

10921109
def EnableService(self, service):
1093-
status, output = subprocess.getstatusoutput("systemctl enable %s" % (service,))
1110+
status, output = getstatusoutput("systemctl enable %s" % (service,))
10941111
if status != 0:
10951112
raise Exception(output)
10961113

10971114
def DisableService(self, service):
1098-
status, output = subprocess.getstatusoutput("systemctl disable %s" % (service,))
1115+
status, output = getstatusoutput("systemctl disable %s" % (service,))
10991116
if status != 0:
11001117
raise Exception(output)
11011118

11021119
def RestartService(self, service):
1103-
status, output = subprocess.getstatusoutput("systemctl restart %s" % (service,))
1120+
status, output = getstatusoutput("systemctl restart %s" % (service,))
11041121
if status != 0:
11051122
raise Exception(output)
11061123

11071124
def StartService(self, service):
1108-
status, output = subprocess.getstatusoutput("systemctl start %s" % (service,))
1125+
status, output = getstatusoutput("systemctl start %s" % (service,))
11091126
if status != 0:
11101127
raise Exception(output)
11111128

11121129
def StopService(self, service):
1113-
status, output = subprocess.getstatusoutput("systemctl stop %s" % (service,))
1130+
status, output = getstatusoutput("systemctl stop %s" % (service,))
11141131
if status != 0:
11151132
raise Exception(output)
11161133

11171134
def NTPStatus(self):
1118-
status, output = subprocess.getstatusoutput("/usr/bin/ntpstat")
1135+
status, output = getstatusoutput("/usr/bin/ntpstat")
11191136
return output
11201137

11211138
def SetVerboseBoot(self, inVerbose):
@@ -1124,7 +1141,7 @@ def SetVerboseBoot(self, inVerbose):
11241141
else:
11251142
name = 'quiet'
11261143

1127-
status, output = subprocess.getstatusoutput(
1144+
status, output = getstatusoutput(
11281145
"(export TERM=xterm && /opt/xensource/libexec/set-boot " + name + ")")
11291146
if status != 0:
11301147
raise Exception(output)

XSConsoleDataUtils.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def DeviceList(cls, inWritableOnly):
8080
@classmethod
8181
def SRDeviceList(self):
8282
retVal= []
83-
status, output = subprocess.getstatusoutput("/opt/xensource/libexec/list_local_disks")
83+
status, output = getstatusoutput("/opt/xensource/libexec/list_local_disks")
8484
if status == 0:
8585
regExp = re.compile(r"\s*\(\s*'([^']*)'\s*,\s*'([^']*)'\s*,\s*'([^']*)'\s*,\s*'([^']*)'\s*,\s*'([^']*)'\s*\)")
8686
for line in output.split("\n"):
@@ -168,18 +168,18 @@ def USBFormat(self, inVDI):
168168
if e.errno != errno.EINTR: # Loop if EINTR
169169
raise
170170

171-
status, output = subprocess.getstatusoutput('/bin/sync')
171+
status, output = getstatusoutput('/bin/sync')
172172

173173
if status != 0:
174174
raise Exception(output)
175175

176176
# Format the new partition with VFAT
177-
status, output = subprocess.getstatusoutput("/sbin/mkfs.vfat -n 'XenServer Backup' -F 32 '" +partitionName + "' 2>&1")
177+
status, output = getstatusoutput("/sbin/mkfs.vfat -n 'XenServer Backup' -F 32 '" +partitionName + "' 2>&1")
178178

179179
if status != 0:
180180
raise Exception(output)
181181

182-
status, output = subprocess.getstatusoutput('/bin/sync')
182+
status, output = getstatusoutput('/bin/sync')
183183

184184
if status != 0:
185185
raise Exception(output)
@@ -237,7 +237,7 @@ def __init__(self, inVDI, inMode = None):
237237
FileUtils.AssertSafePath(self.mountDev)
238238
self.mountPoint = tempfile.mkdtemp(".xsconsole")
239239

240-
status, output = subprocess.getstatusoutput("/bin/mount -t auto -o " + self.mode + ' ' +self.mountDev+" "+self.mountPoint + " 2>&1")
240+
status, output = getstatusoutput("/bin/mount -t auto -o " + self.mode + ' ' +self.mountDev+" "+self.mountPoint + " 2>&1")
241241
if status != 0:
242242
try:
243243
self.Unmount()
@@ -276,7 +276,7 @@ def HandleMountFailure(self, inOutput):
276276
raise Exception(inOutput)
277277

278278
realDevice = FileUtils.DeviceFromVDI(self.vdi)
279-
status, output = subprocess.getstatusoutput("/sbin/fdisk -l '" +realDevice+"'")
279+
status, output = getstatusoutput("/sbin/fdisk -l '" +realDevice+"'")
280280
if status != 0:
281281
raise Exception(output)
282282

@@ -313,7 +313,7 @@ def Scan(self, inRegExp = None, inNumToReturn = None):
313313
def Unmount(self):
314314
status = 0
315315
if self.mountedVBD:
316-
status, output = subprocess.getstatusoutput("/bin/umount '"+self.mountPoint + "' 2>&1")
316+
status, output = getstatusoutput("/bin/umount '"+self.mountPoint + "' 2>&1")
317317
os.rmdir(self.mountPoint)
318318
self.mountedVBD = False
319319
if self.pluggedVBD:
@@ -360,7 +360,7 @@ def __init__(self, inVDI, inMode = None):
360360
FileUtils.AssertSafePath(self.mountDev)
361361
self.mountPoint = tempfile.mkdtemp(".xsconsole")
362362

363-
status, output = subprocess.getstatusoutput("/bin/mount -t auto -o " + self.mode + ' ' +self.mountDev+" "+self.mountPoint + " 2>&1")
363+
status, output = getstatusoutput("/bin/mount -t auto -o " + self.mode + ' ' +self.mountDev+" "+self.mountPoint + " 2>&1")
364364
if status != 0:
365365
try:
366366
self.Unmount()
@@ -400,7 +400,7 @@ def HandleMountFailure(self, inStatus, inOutput):
400400
raise Exception(inOutput)
401401

402402
realDevice = FileUtils.DeviceFromVDI(self.vdi)
403-
status, output = subprocess.getstatusoutput("/sbin/fdisk -l '" +realDevice+"'")
403+
status, output = getstatusoutput("/sbin/fdisk -l '" +realDevice+"'")
404404
if status != 0:
405405
raise Exception(output)
406406

@@ -437,7 +437,7 @@ def Scan(self, inRegExp = None, inNumToReturn = None):
437437
def Unmount(self):
438438
status = 0
439439
if self.mountedVDI:
440-
status, output = subprocess.getstatusoutput("/bin/umount '"+self.mountPoint + "' 2>&1")
440+
status, output = getstatusoutput("/bin/umount '"+self.mountPoint + "' 2>&1")
441441
os.rmdir(self.mountPoint)
442442
self.mountedVDI = False
443443
XSLog('Unmounted '+self.mountPoint)

plugins-base/XSFeatureDRBackup.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
if __name__ == "__main__":
1717
raise Exception("This script is a plugin for xsconsole and cannot run independently")
1818

19-
import subprocess
20-
2119
from XSConsoleStandard import *
2220

2321
class DRBackupDialogue(SRDialogue):
@@ -38,7 +36,7 @@ def DoAction(self, inSR):
3836
sr_uuid = inSR['uuid']
3937
command = "%s/xe-backup-metadata -n -u %s" % (Config.Inst().HelperPath(), sr_uuid)
4038

41-
status, output = subprocess.getstatusoutput(command)
39+
status, output = getstatusoutput(command)
4240
initalize_vdi = ""
4341
if status == 3:
4442
initalize_vdi = "-c"
@@ -47,7 +45,7 @@ def DoAction(self, inSR):
4745

4846
Layout.Inst().TransientBanner(Lang("Backing up metadata... This may take several minutes."))
4947
command = "%s/xe-backup-metadata %s -u %s" % (Config.Inst().HelperPath(), initalize_vdi, sr_uuid)
50-
status, output = subprocess.getstatusoutput(command)
48+
status, output = getstatusoutput(command)
5149
if status == 0:
5250
Layout.Inst().PushDialogue(InfoDialogue(Lang("Backup Successful"), output))
5351
else:

plugins-base/XSFeatureDRRestore.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def HandleMethodChoice(self, inChoice, dryRun):
9292
dry_flag=""
9393
Layout.Inst().TransientBanner(Lang("Restoring VM Metadata. This may take a few minutes..."))
9494
command = "%s/xe-restore-metadata -y %s -u %s -x %s -d %s -m %s" % (Config.Inst().HelperPath(), dry_flag, self.sr_uuid, self.vdi_uuid, self.chosen_date, chosen_mode)
95-
status, output = subprocess.getstatusoutput(command)
95+
status, output = getstatusoutput(command)
9696
Layout.Inst().PopDialogue()
9797
if status == 0:
9898
Layout.Inst().PushDialogue(InfoDialogue(Lang("Metadata Restore Succeeded: ") + output))

plugins-base/XSFeatureSRCreate.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
if __name__ == "__main__":
1717
raise Exception("This script is a plugin for xsconsole and cannot run independently")
1818

19-
import subprocess
20-
2119
from XSConsoleStandard import *
2220
import xml.dom.minidom
2321

@@ -1288,7 +1286,7 @@ def CommitNFS_ATTACH(self):
12881286
'user')
12891287

12901288
def CommitNFS_ISO_ATTACH(self):
1291-
self.srParams['uuid'] = subprocess.getoutput('/usr/bin/uuidgen')
1289+
self.srParams['uuid'] = getoutput('/usr/bin/uuidgen')
12921290
self.CommitAttach(self.srTypes['NFS_ISO'], { # device_config
12931291
'location':self.srParams['server']+':'+self.srParams['serverpath'],
12941292
'options':self.srParams['options']
@@ -1300,7 +1298,7 @@ def CommitNFS_ISO_ATTACH(self):
13001298
)
13011299

13021300
def CommitCIFS_ISO_ATTACH(self):
1303-
self.srParams['uuid'] = subprocess.getoutput('/usr/bin/uuidgen')
1301+
self.srParams['uuid'] = getoutput('/usr/bin/uuidgen')
13041302
deviceConfig = {
13051303
'location':'//'+self.srParams['server']+'/'+self.srParams['serverpath'],
13061304
'type':'cifs',

plugins-base/XSFeatureSaveBugReport.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
if __name__ == "__main__":
1717
raise Exception("This script is a plugin for xsconsole and cannot run independently")
1818

19-
import subprocess
20-
2119
from XSConsoleStandard import *
2220

2321
class SaveBugReportDialogue(FileDialogue):
@@ -52,7 +50,7 @@ def DoAction(self):
5250
file = open(filename, "w")
5351
# xen-bugtool requires a value for $USER
5452
command = "( export USER=root && /usr/sbin/xen-bugtool --yestoall --silent --output=tar --outfd="+str(file.fileno()) + ' )'
55-
status, output = subprocess.getstatusoutput(command)
53+
status, output = getstatusoutput(command)
5654
file.close()
5755

5856
if status != 0:

plugins-base/XSFeatureUploadBugReport.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
if __name__ == "__main__":
1717
raise Exception("This script is a plugin for xsconsole and cannot run independently")
1818

19-
import subprocess
20-
2119
from XSConsoleStandard import *
2220

2321
class UploadBugReportDialogue(InputDialogue):
@@ -48,7 +46,7 @@ def HandleCommit(self, inValues):
4846
if proxy != '':
4947
command += " http_proxy='"+proxy+"'"
5048

51-
status, output = subprocess.getstatusoutput(command)
49+
status, output = getstatusoutput(command)
5250

5351
if status != 0:
5452
XSLogError('Upload bugreport failed', output) # Error output can be verbose, so syslog only

plugins-oem/XSFeatureOEMBackup.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
if __name__ == "__main__":
1717
raise Exception("This script is a plugin for xsconsole and cannot run independently")
1818

19-
import subprocess
20-
2119
from XSConsoleStandard import *
2220

2321
class OEMBackupDialogue(FileDialogue):
@@ -70,7 +68,7 @@ def DoCommit(self):
7068
filename = self.vdiMount.MountedPath(self.filename)
7169
FileUtils.AssertSafePath(filename)
7270
command = "/opt/xensource/bin/xe host-backup file-name='"+filename+"' host="+hostRef
73-
status, output = subprocess.getstatusoutput(command)
71+
status, output = getstatusoutput(command)
7472

7573
if status != 0:
7674
raise Exception(output)

0 commit comments

Comments
 (0)