forked from robotframework/robotframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter.py
More file actions
220 lines (177 loc) · 5.97 KB
/
interpreter.py
File metadata and controls
220 lines (177 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from os.path import abspath, dirname, exists, join
import os
import subprocess
import sys
PROJECT_ROOT = dirname(dirname(abspath(__file__)))
ROBOT_PATH = join(PROJECT_ROOT, 'src', 'robot')
def get_variables(path, name=None, version=None):
interpreter = InterpreterFactory(path, name, version)
u = '' if interpreter.is_py3 or interpreter.is_ironpython else 'u'
return {'INTERPRETER': interpreter, 'UNICODE PREFIX': u}
def InterpreterFactory(path, name=None, version=None):
if path.endswith('.jar'):
return StandaloneInterpreter(path, name, version)
return Interpreter(path, name, version)
class Interpreter(object):
def __init__(self, path, name=None, version=None):
self.path = path
self.interpreter = self._get_interpreter(path)
if not name:
name, version = self._get_name_and_version()
self.name = name
self.version = version
self.version_info = tuple(int(item) for item in version.split('.'))
def _get_interpreter(self, path):
return [path] if os.path.exists(path) else path.split()
def _get_name_and_version(self):
try:
output = subprocess.check_output(self.interpreter + ['-V'],
stderr=subprocess.STDOUT,
encoding='UTF-8')
except (subprocess.CalledProcessError, FileNotFoundError):
raise ValueError('Invalid interpreter: %s' % self.path)
name, version = output.split()[:2]
name = name if 'PyPy' not in output else 'PyPy'
version = '.'.join(version.split('.')[:2])
return name, version
@property
def os(self):
for condition, name in [(self.is_linux, 'Linux'),
(self.is_osx, 'OS X'),
(self.is_windows, 'Windows')]:
if condition:
return name
return sys.platform
@property
def excludes(self):
if self.is_jython:
yield 'no-jython'
yield 'require-lxml'
else:
yield 'require-jython'
if self.is_ironpython:
yield 'no-ipy'
yield 'require-lxml'
yield 'require-docutils' # https://github.com/IronLanguages/main/issues/1230
else:
yield 'require-ipy'
for exclude in self._platform_excludes:
yield exclude
@property
def _platform_excludes(self):
if self.is_py3:
yield 'require-py2'
else:
yield 'require-py3'
if self.version_info < (3, 5):
yield 'require-py3.5'
if self.version_info < (3, 7):
yield 'require-py3.7'
if self.is_windows:
yield 'no-windows'
if self.is_jython:
yield 'no-windows-jython'
if not self.is_windows:
yield 'require-windows'
if self.is_osx:
yield 'no-osx'
if self.is_python:
yield 'no-osx-python'
@property
def classpath(self):
if not self.is_jython:
return None
classpath = os.environ.get('CLASSPATH')
if classpath and 'tools.jar' in classpath:
return classpath
tools_jar = join(PROJECT_ROOT, 'ext-lib', 'tools.jar')
if not exists(tools_jar):
return classpath
if classpath:
return classpath + os.pathsep + tools_jar
return tools_jar
@property
def is_python(self):
return self.name == 'Python'
@property
def is_jython(self):
return self.name == 'Jython'
@property
def is_ironpython(self):
return self.name == 'IronPython'
@property
def is_pypy(self):
return self.name == 'PyPy'
@property
def is_py2(self):
return self.version[0] == '2'
@property
def is_py3(self):
return self.version[0] == '3'
@property
def is_linux(self):
return 'linux' in sys.platform
@property
def is_osx(self):
return sys.platform == 'darwin'
@property
def is_windows(self):
return os.name == 'nt'
@property
def runner(self):
return self.interpreter + [join(ROBOT_PATH, 'run.py')]
@property
def rebot(self):
return self.interpreter + [join(ROBOT_PATH, 'rebot.py')]
@property
def libdoc(self):
return self.interpreter + [join(ROBOT_PATH, 'libdoc.py')]
@property
def testdoc(self):
return self.interpreter + [join(ROBOT_PATH, 'testdoc.py')]
@property
def tidy(self):
return self.interpreter + [join(ROBOT_PATH, 'tidy.py')]
def __str__(self):
return '%s %s on %s' % (self.name, self.version, self.os)
class StandaloneInterpreter(Interpreter):
def __init__(self, path, name=None, version=None):
Interpreter.__init__(self, abspath(path), name or 'Standalone JAR',
version or '2.7')
def _get_interpreter(self, path):
interpreter = ['java', '-jar', path]
classpath = self.classpath
if classpath:
interpreter.insert(1, '-Xbootclasspath/a:%s' % classpath)
return interpreter
@property
def excludes(self):
for exclude in ['no-standalone', 'no-jython', 'require-lxml',
'require-docutils', 'require-ipy']:
yield exclude
for exclude in self._platform_excludes:
yield exclude
@property
def is_python(self):
return False
@property
def is_jython(self):
return True
@property
def is_ironpython(self):
return False
@property
def runner(self):
return self.interpreter + ['run']
@property
def rebot(self):
return self.interpreter + ['rebot']
@property
def libdoc(self):
return self.interpreter + ['libdoc']
@property
def testdoc(self):
return self.interpreter + ['testdoc']
@property
def tidy(self):
return self.interpreter + ['tidy']