1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | # $Id: testboxscript.py 52776 2014-09-17 14:51:43Z 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-2014 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: 52776 $"
|
---|
34 |
|
---|
35 | import subprocess
|
---|
36 | import sys
|
---|
37 | import os
|
---|
38 | import time
|
---|
39 |
|
---|
40 |
|
---|
41 | ## @name Test Box script exit statuses (see also RTEXITCODE)
|
---|
42 | # @remarks These will _never_ change
|
---|
43 | # @{
|
---|
44 | TBS_EXITCODE_FAILURE = 1 # RTEXITCODE_FAILURE
|
---|
45 | TBS_EXITCODE_SYNTAX = 2 # RTEXITCODE_SYNTAX
|
---|
46 | TBS_EXITCODE_NEED_UPGRADE = 9
|
---|
47 | ## @}
|
---|
48 |
|
---|
49 |
|
---|
50 | class TestBoxScriptWrapper(object): # pylint: disable=R0903
|
---|
51 | """
|
---|
52 | Wrapper class
|
---|
53 | """
|
---|
54 |
|
---|
55 | TESTBOX_SCRIPT_FILENAME = 'testboxscript_real.py'
|
---|
56 |
|
---|
57 | def __init__(self):
|
---|
58 | """
|
---|
59 | Init
|
---|
60 | """
|
---|
61 | self.task = None
|
---|
62 |
|
---|
63 | def __del__(self):
|
---|
64 | """
|
---|
65 | Cleanup
|
---|
66 | """
|
---|
67 | if self.task is not None:
|
---|
68 | print 'Wait for child task...'
|
---|
69 | self.task.terminate()
|
---|
70 | self.task.wait()
|
---|
71 | print 'done. Exiting'
|
---|
72 | self.task = None;
|
---|
73 |
|
---|
74 | def run(self):
|
---|
75 | """
|
---|
76 | Start spawning the real TestBox script.
|
---|
77 | """
|
---|
78 |
|
---|
79 | # Figure out where we live first.
|
---|
80 | try:
|
---|
81 | __file__
|
---|
82 | except:
|
---|
83 | __file__ = sys.argv[0];
|
---|
84 | sTestBoxScriptDir = os.path.dirname(os.path.abspath(__file__));
|
---|
85 |
|
---|
86 | # Construct the argument list for the real script (same dir).
|
---|
87 | sRealScript = os.path.join(sTestBoxScriptDir, TestBoxScriptWrapper.TESTBOX_SCRIPT_FILENAME);
|
---|
88 | asArgs = sys.argv[1:];
|
---|
89 | asArgs.insert(0, sRealScript);
|
---|
90 | if sys.executable is not None and len(sys.executable) > 0:
|
---|
91 | asArgs.insert(0, sys.executable);
|
---|
92 |
|
---|
93 | # Look for --pidfile <name> and write a pid file.
|
---|
94 | sPidFile = None;
|
---|
95 | for i in range(len(asArgs)):
|
---|
96 | if asArgs[i] == '--pidfile' and i + 1 < len(asArgs):
|
---|
97 | sPidFile = asArgs[i + 1];
|
---|
98 | break;
|
---|
99 | if asArgs[i] == '--':
|
---|
100 | break;
|
---|
101 | if sPidFile is not None and len(sPidFile) > 0:
|
---|
102 | oPidFile = open(sPidFile, 'w');
|
---|
103 | oPidFile.write(str(os.getpid()));
|
---|
104 | oPidFile.close();
|
---|
105 |
|
---|
106 | # Execute the testbox script almost forever in a relaxed loop.
|
---|
107 | rcExit = TBS_EXITCODE_FAILURE;
|
---|
108 | while True:
|
---|
109 | self.task = subprocess.Popen(asArgs, shell=False);
|
---|
110 | rcExit = self.task.wait();
|
---|
111 | self.task = None;
|
---|
112 | if rcExit == TBS_EXITCODE_SYNTAX:
|
---|
113 | break;
|
---|
114 |
|
---|
115 | # Relax.
|
---|
116 | time.sleep(1);
|
---|
117 | return rcExit;
|
---|
118 |
|
---|
119 | if __name__ == '__main__':
|
---|
120 | sys.exit(TestBoxScriptWrapper().run());
|
---|
121 |
|
---|