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