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