VirtualBox

source: vbox/trunk/src/VBox/Main/webservice/samples/python/clienttest.py@ 68019

Last change on this file since 68019 was 67049, checked in by vboxsync, 8 years ago

Main/glue/vboxapi.py: clean up, remove vbox attribute, eliminate unnecessary parameter of getSessionObject (it is a completely ignored parameter with a default parameter to make life a little simpler for API clients in Python to deal with the previous version still)
Frontends/VBoxShell: adapt to the cleanup, reduce the unnecessary variations in the code dealing with sessions
ValidationKit etc.: adapt to the cleanup

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1#!/usr/bin/python
2#
3# Copyright (C) 2012-2016 Oracle Corporation
4#
5# This file is part of VirtualBox Open Source Edition (OSE), as
6# available from http://www.virtualbox.org. This file is free software;
7# you can redistribute it and/or modify it under the terms of the GNU
8# General Public License (GPL) as published by the Free Software
9# Foundation, in version 2 as it comes in the "COPYING" file of the
10# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
11# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
12#
13# Things needed to be set up before running this sample:
14# - Install Python and verify it works (2.7.2 will do, 3.x is untested yet)
15# - On Windows: Install the PyWin32 extensions for your Python version
16# (see http://sourceforge.net/projects/pywin32/)
17# - If not already done, set the environment variable "VBOX_INSTALL_PATH"
18# to point to your VirtualBox installation directory (which in turn must have
19# the "sdk" subfolder")
20# - Install the VirtualBox Python bindings by doing a
21# "[python] vboxapisetup.py install"
22# - Run this sample with "[python] clienttest.py"
23
24import os,sys
25import traceback
26
27#
28# Converts an enumeration to a printable string.
29#
30def enumToString(constants, enum, elem):
31 all = constants.all_values(enum)
32 for e in all.keys():
33 if str(elem) == str(all[e]):
34 return e
35 return "<unknown>"
36
37def main(argv):
38
39 from vboxapi import VirtualBoxManager
40 # This is a VirtualBox COM/XPCOM API client, no data needed.
41 mgr = VirtualBoxManager(None, None)
42
43 # Get the global VirtualBox object
44 vbox = mgr.getVirtualBox()
45
46 print "Running VirtualBox version %s" %(vbox.version)
47
48 # Get all constants through the Python manager code
49 vboxConstants = mgr.constants
50
51 # Enumerate all defined machines
52 for mach in mgr.getArray(vbox, 'machines'):
53
54 try:
55 # Be prepared for failures - the VM can be inaccessible
56 vmname = '<inaccessible>'
57 try:
58 vmname = mach.name
59 except Exception, e:
60 None
61 vmid = '';
62 try:
63 vmid = mach.id
64 except Exception, e:
65 None
66
67 # Print some basic VM information even if there were errors
68 print "Machine name: %s [%s]" %(vmname,vmid)
69 if vmname == '<inaccessible>' or vmid == '':
70 continue
71
72 # Print some basic VM information
73 print " State: %s" %(enumToString(vboxConstants, "MachineState", mach.state))
74 print " Session state: %s" %(enumToString(vboxConstants, "SessionState", mach.sessionState))
75
76 # Do some stuff which requires a running VM
77 if mach.state == vboxConstants.MachineState_Running:
78
79 # Get the session object
80 session = mgr.getSessionObject()
81
82 # Lock the current machine (shared mode, since we won't modify the machine)
83 mach.lockMachine(session, vboxConstants.LockType_Shared)
84
85 # Acquire the VM's console and guest object
86 console = session.console
87 guest = console.guest
88
89 # Retrieve the current Guest Additions runlevel and print
90 # the installed Guest Additions version
91 addRunLevel = guest.additionsRunLevel
92 print " Additions State: %s" %(enumToString(vboxConstants, "AdditionsRunLevelType", addRunLevel))
93 if addRunLevel != vboxConstants.AdditionsRunLevelType_None:
94 print " Additions Ver: %s" %(guest.additionsVersion)
95
96 # Get the VM's display object
97 display = console.display
98
99 # Get the VM's current display resolution + bit depth + position
100 screenNum = 0 # From first screen
101 (screenW, screenH, screenBPP, screenX, screenY, _) = display.getScreenResolution(screenNum)
102 print " Display (%d): %dx%d, %d BPP at %d,%d" %(screenNum, screenW, screenH, screenBPP, screenX, screenY)
103
104 # We're done -- don't forget to unlock the machine!
105 session.unlockMachine()
106
107 except Exception, e:
108 print "Errror [%s]: %s" %(mach.name, str(e))
109 traceback.print_exc()
110
111 # Call destructor and delete manager
112 del mgr
113
114if __name__ == '__main__':
115 main(sys.argv)
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette