VirtualBox

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

Last change on this file since 104884 was 98655, checked in by vboxsync, 22 months ago

ValKit: Pylint 2.16.2 adjustments.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# $Id: testboxscript.py 98655 2023-02-20 15:05:40Z 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
12from __future__ import print_function;
13
14__copyright__ = \
15"""
16Copyright (C) 2012-2023 Oracle and/or its affiliates.
17
18This file is part of VirtualBox base platform packages, as
19available from https://www.virtualbox.org.
20
21This program is free software; you can redistribute it and/or
22modify it under the terms of the GNU General Public License
23as published by the Free Software Foundation, in version 3 of the
24License.
25
26This program is distributed in the hope that it will be useful, but
27WITHOUT ANY WARRANTY; without even the implied warranty of
28MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29General Public License for more details.
30
31You should have received a copy of the GNU General Public License
32along with this program; if not, see <https://www.gnu.org/licenses>.
33
34The contents of this file may alternatively be used under the terms
35of the Common Development and Distribution License Version 1.0
36(CDDL), a copy of it is provided in the "COPYING.CDDL" file included
37in the VirtualBox distribution, in which case the provisions of the
38CDDL are applicable instead of those of the GPL.
39
40You may elect to license modified versions of this file under the
41terms and conditions of either the GPL or the CDDL or both.
42
43SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
44"""
45__version__ = "$Revision: 98655 $"
46
47import platform;
48import subprocess;
49import sys;
50import os;
51import time;
52
53
54## @name Test Box script exit statuses (see also RTEXITCODE)
55# @remarks These will _never_ change
56# @{
57TBS_EXITCODE_FAILURE = 1; # RTEXITCODE_FAILURE
58TBS_EXITCODE_SYNTAX = 2; # RTEXITCODE_SYNTAX
59TBS_EXITCODE_NEED_UPGRADE = 9;
60## @}
61
62
63class 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: # pylint: disable=unspecified-encoding
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
135if __name__ == '__main__':
136 sys.exit(TestBoxScriptWrapper().run());
137
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