.. `impl.pyvmrun`: ####################### ``pyvix2/vmrun.py`` ####################### This is a fallback in the rare event that the :mod:`ctypes` interface doesn't work. It uses ``vmrun`` to execute commands. And, no, there's no really *good* reason for this. :: """pyvix2/vmrun.py -- Implements VIX API via VMRun subprocess execution. Really. """ from __future__ import print_function import subprocess import re import os class VMRun( object ): vix_app= r"C:\Program Files\VMware\VMware VIX\vmrun.exe" def __init__( self ): self.proc= None self.pid= None self.status= None self.cmd= None self._name= VMRun.vix_app def __call__( self, *args ): self.cmd= (VMRun.vix_app,"-T","workstation") + args self.proc= subprocess.Popen( self.cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT ) self.pid= self.proc.pid self.output, self.stderr = self.proc.communicate() self.status= self.proc.returncode return self.status @property def success( self ): return self.status == 0 _vmrun= VMRun() print( "VMWare:", _vmrun._name ) def show( text ): for line in text: print( ">>>", line.rstrip() ) class Host( object ): def connect(self, hostname = None, hostport = 0, username = None, password = None): self.hostname, self.hostport= hostname, hostport self.username, self.password= username, password self.vmx= None def disconnect( self ): pass def find_items( self ): _vmrun( "list" ) show( _vmrun.output.splitlines() ) def openVM( self, vmxFilePathName ): self.vmx= vmxFilePathName return VM( self ) class VM( object ): def __init__( self, host ): self.host= host self.tools_state= None def login( self, username, password ): self.gusername, self.gpassword = username, password def logout( self ): self.gusername, self.gpassword = None, None def power_on( self ): _vmrun("start", self.host.vmx) show( _vmrun.output.splitlines() ) def wait_for_tools_in_guest( self, timeout=120 ): pass def power_off( self ): _vmrun("stop", self.host.vmx, "soft") show( _vmrun.output.splitlines() ) def file_copy_from_guest( self, guestPathName, hostPathName ): _vmrun("-gu",self.gusername, "-gp",self.gpassword, "copyFileFromGuestToHost", guest_os, guestPathName, hostPathName ) show( _vmrun.output.splitlines() ) def process_list( self ): _vmrun("-gu",self.gusername, "-gp",self.gpassword, "listProcessesInGuest", self.host.vmx) line_iter= iter( x.rstrip() for x in _vmrun.output.splitlines() ) heading= next( line_iter ) show( heading ) details= ( self.proc_pat.match( line ).groupdict() for line in line_iter ) self.current_procs= dict( (int(d['pid']), d) for d in details ) return self.current_procs def program_run( self, guestProgramName, commandLineArgs, nowait=False ): _vmrun("-gu",self.gusername, "-gp",self.gpassword, "runProgramInGuest", self.host.vmx, guestProgramName, commandLineArgs ) self.show( _vmrun.output.splitlines() ) def script_run( self, interpreter, scriptText, nowait=False): _vmrun("-gu",self.gusername, "-gp",self.gpassword, "runScriptInGuest", self.host.vmx, interpreter, scriptText ) self.show( _vmrun.output.splitlines() ) Demo ====== :: guest_os= r"G:\VMplanet-openSUSE11.3\[VMplanet] openSUSE 11.3.vmx" #guest_os= r"C:\Fedora14\Fedora.vmx" def startup_shutdown_demo(): host= Host() host.connect() print( "Running:", host.find_items() ) vm= host.openVM(guest_os) vm.power_on() print( "Running:", host.find_items() ) print( "Pre-wait Tools:", vm.tools_state() ) try: vm.wait_for_tools_in_guest(10) except Error as e: if VIX_ERROR_CODE(e.errorCode) in ( VIX_E_TIMEOUT_WAITING_FOR_TOOLS, VIX_E_TOOLS_NOT_RUNNING ): # No tools. Get out of the pool. print( e ) else: raise print( "Post-wait Tools:", vm.tools_state() ) vm.power_off() if __name__ == "__main__": startup_shutdown_demo()