1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | # $Id: testboxscript.py 70548 2018-01-11 20:46:02Z 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 | __copyright__ = \
|
---|
13 | """
|
---|
14 | Copyright (C) 2012-2017 Oracle Corporation
|
---|
15 |
|
---|
16 | This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
17 | available from http://www.virtualbox.org. This file is free software;
|
---|
18 | you can redistribute it and/or modify it under the terms of the GNU
|
---|
19 | General Public License (GPL) as published by the Free Software
|
---|
20 | Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
21 | VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
22 | hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
23 |
|
---|
24 | The contents of this file may alternatively be used under the terms
|
---|
25 | of the Common Development and Distribution License Version 1.0
|
---|
26 | (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
27 | VirtualBox OSE distribution, in which case the provisions of the
|
---|
28 | CDDL are applicable instead of those of the GPL.
|
---|
29 |
|
---|
30 | You may elect to license modified versions of this file under the
|
---|
31 | terms and conditions of either the GPL or the CDDL or both.
|
---|
32 | """
|
---|
33 | __version__ = "$Revision: 70548 $"
|
---|
34 |
|
---|
35 | import platform;
|
---|
36 | import subprocess;
|
---|
37 | import sys;
|
---|
38 | import os;
|
---|
39 | import time;
|
---|
40 |
|
---|
41 |
|
---|
42 | ## @name Test Box script exit statuses (see also RTEXITCODE)
|
---|
43 | # @remarks These will _never_ change
|
---|
44 | # @{
|
---|
45 | TBS_EXITCODE_FAILURE = 1; # RTEXITCODE_FAILURE
|
---|
46 | TBS_EXITCODE_SYNTAX = 2; # RTEXITCODE_SYNTAX
|
---|
47 | TBS_EXITCODE_NEED_UPGRADE = 9;
|
---|
48 | ## @}
|
---|
49 |
|
---|
50 |
|
---|
51 | class 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 |
|
---|
123 | if __name__ == '__main__':
|
---|
124 | sys.exit(TestBoxScriptWrapper().run());
|
---|
125 |
|
---|