VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/testboxscript/testboxscript.py@ 70548

Last change on this file since 70548 was 70548, checked in by vboxsync, 7 years ago

testboxscript: Python 3 adjustments.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# $Id: testboxscript.py 70548 2018-01-11 20:46:02Z vboxsync $
4
5"""
6TestBox Script Wrapper.
7
8This script aimes at respawning the Test Box Script when it terminates
9abnormally or due to an UPGRADE request.
10"""
11
12__copyright__ = \
13"""
14Copyright (C) 2012-2017 Oracle Corporation
15
16This file is part of VirtualBox Open Source Edition (OSE), as
17available from http://www.virtualbox.org. This file is free software;
18you can redistribute it and/or modify it under the terms of the GNU
19General Public License (GPL) as published by the Free Software
20Foundation, in version 2 as it comes in the "COPYING" file of the
21VirtualBox OSE distribution. VirtualBox OSE is distributed in the
22hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
23
24The contents of this file may alternatively be used under the terms
25of the Common Development and Distribution License Version 1.0
26(CDDL) only, as it comes in the "COPYING.CDDL" file of the
27VirtualBox OSE distribution, in which case the provisions of the
28CDDL are applicable instead of those of the GPL.
29
30You may elect to license modified versions of this file under the
31terms and conditions of either the GPL or the CDDL or both.
32"""
33__version__ = "$Revision: 70548 $"
34
35import platform;
36import subprocess;
37import sys;
38import os;
39import time;
40
41
42## @name Test Box script exit statuses (see also RTEXITCODE)
43# @remarks These will _never_ change
44# @{
45TBS_EXITCODE_FAILURE = 1; # RTEXITCODE_FAILURE
46TBS_EXITCODE_SYNTAX = 2; # RTEXITCODE_SYNTAX
47TBS_EXITCODE_NEED_UPGRADE = 9;
48## @}
49
50
51class TestBoxScriptWrapper(object): # pylint: disable=R0903
52 """
53 Wrapper class
54 """
55
56 TESTBOX_SCRIPT_FILENAME = 'testboxscript_real.py'
57
58 def __init__(self):
59 """
60 Init
61 """
62 self.oTask = None
63
64 def __del__(self):
65 """
66 Cleanup
67 """
68 if self.oTask is not None:
69 print('Wait for child task...')
70 self.oTask.terminate()
71 self.oTask.wait()
72 print('done. Exiting')
73 self.oTask = None;
74
75 def run(self):
76 """
77 Start spawning the real TestBox script.
78 """
79
80 # Figure out where we live first.
81 try:
82 __file__
83 except:
84 __file__ = sys.argv[0];
85 sTestBoxScriptDir = os.path.dirname(os.path.abspath(__file__));
86
87 # Construct the argument list for the real script (same dir).
88 sRealScript = os.path.join(sTestBoxScriptDir, TestBoxScriptWrapper.TESTBOX_SCRIPT_FILENAME);
89 asArgs = sys.argv[1:];
90 asArgs.insert(0, sRealScript);
91 if sys.executable:
92 asArgs.insert(0, sys.executable);
93
94 # Look for --pidfile <name> and write a pid file.
95 sPidFile = None;
96 for i, _ in enumerate(asArgs):
97 if asArgs[i] == '--pidfile' and i + 1 < len(asArgs):
98 sPidFile = asArgs[i + 1];
99 break;
100 if asArgs[i] == '--':
101 break;
102 if sPidFile:
103 oPidFile = open(sPidFile, 'w');
104 oPidFile.write(str(os.getpid()));
105 oPidFile.close();
106
107 # Execute the testbox script almost forever in a relaxed loop.
108 rcExit = TBS_EXITCODE_FAILURE;
109 while True:
110 fCreationFlags = 0;
111 if platform.system() == 'Windows':
112 fCreationFlags = getattr(subprocess, 'CREATE_NEW_PROCESS_GROUP', 0x00000200); # for Ctrl-C isolation (python 2.7)
113 self.oTask = subprocess.Popen(asArgs, shell = False, creationflags = fCreationFlags);
114 rcExit = self.oTask.wait();
115 self.oTask = None;
116 if rcExit == TBS_EXITCODE_SYNTAX:
117 break;
118
119 # Relax.
120 time.sleep(1);
121 return rcExit;
122
123if __name__ == '__main__':
124 sys.exit(TestBoxScriptWrapper().run());
125
Note: See TracBrowser for help on using the repository browser.

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