Browse Source

Add universal_newlines=True to subprocess.Popen()

We have modified the code in this way to overcome
byte-string and string comparison problems.
Matteo Bonamassa 4 years ago
parent
commit
84c5c65202
1 changed files with 7 additions and 7 deletions
  1. 7 7
      labcalcoloctl

+ 7 - 7
labcalcoloctl

@@ -74,7 +74,7 @@ class Host(Thread):
 	def vmstatus(self):
 		statuscmd = "ps aux | grep qemu | grep -v grep"
 		ssh = self.sshcommand(statuscmd)
-		result = [ l for l in ssh.stdout.readlines() if b'qemu' in l ]
+		result = [ l for l in ssh.stdout.readlines() if 'qemu' in l ]
 		if result == []:
 			return False
 		else:
@@ -82,7 +82,7 @@ class Host(Thread):
 
 	def sshcommand(self, command):
 		if self.up:
-			ssh = subprocess.Popen( ["ssh", "%s" % self.hostname, command], shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
+			ssh = subprocess.Popen( ["ssh", "%s" % self.hostname, command], shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE, universal_newlines=True )
 			return ssh
 		else:
 			print(self.hostname + ' is not up.')
@@ -120,13 +120,13 @@ class Host(Thread):
 		if self.up:
 			ssh = self.sshcommand(status_query).stdout.readlines()
 
-			qemu_status = [ l for l in ssh if b'qemu' in l ] # Filter for a single command
+			qemu_status = [ l for l in ssh if 'qemu' in l ] # Filter for a single command
 			if len(qemu_status) :
-				print("Qemu command running on", self.hostname+":\n", qemu_status)
+				print("Qemu command running on", self.hostname+":\n", *qemu_status)
 			else :
 				print("Qemu is not running on", self.hostname)
 
-			spicec_status = [ l for l in ssh if b'spicec' in l ]
+			spicec_status = [ l for l in ssh if 'spicec' in l ]
 			if not len(spicec_status) and len(qemu_status) :
 				print("Spicec is not running on", self.hostname)
 				if input("Do you want to start it now? [y/n] ")=="y" :
@@ -134,9 +134,9 @@ class Host(Thread):
 					spiceccmd="export DISPLAY=:4 ; spicec -f -h 127.0.0.1 -p 5900 -w "+pw+" &"
 					self.sshcommand(spiceccmd) # How to check if everything went as expected?
 					# Check if everything works fine now
-					spicec_status = [ l for l in self.sshcommand(status_query).stdout.readlines() if b'spicec' in l ]
+					spicec_status = [ l for l in self.sshcommand(status_query).stdout.readlines() if 'spicec' in l ]
 			if len(spicec_status) :
-				print("Spicec command running on", self.hostname+": ", spicec_status)
+				print("Spicec command running on", self.hostname+": ", *spicec_status)
 
 ### end class Host