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

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

# This file is dual licensed under the terms of the Apache License, Version 

# 2.0, and the BSD License. See the LICENSE file in the root of this repository 

# for complete details. 

from __future__ import absolute_import, division, print_function 

 

import operator 

import os 

import platform 

import sys 

 

from pkg_resources.extern.pyparsing import ParseException, ParseResults, stringStart, stringEnd 

from pkg_resources.extern.pyparsing import ZeroOrMore, Group, Forward, QuotedString 

from pkg_resources.extern.pyparsing import Literal as L # noqa 

 

from ._compat import string_types 

from .specifiers import Specifier, InvalidSpecifier 

 

 

__all__ = [ 

"InvalidMarker", "UndefinedComparison", "UndefinedEnvironmentName", 

"Marker", "default_environment", 

] 

 

 

class InvalidMarker(ValueError): 

""" 

An invalid marker was found, users should refer to PEP 508. 

""" 

 

 

class UndefinedComparison(ValueError): 

""" 

An invalid operation was attempted on a value that doesn't support it. 

""" 

 

 

class UndefinedEnvironmentName(ValueError): 

""" 

A name was attempted to be used that does not exist inside of the 

environment. 

""" 

 

 

class Node(object): 

 

def __init__(self, value): 

self.value = value 

 

def __str__(self): 

return str(self.value) 

 

def __repr__(self): 

return "<{0}({1!r})>".format(self.__class__.__name__, str(self)) 

 

def serialize(self): 

raise NotImplementedError 

 

 

class Variable(Node): 

 

def serialize(self): 

return str(self) 

 

 

class Value(Node): 

 

def serialize(self): 

return '"{0}"'.format(self) 

 

 

class Op(Node): 

 

def serialize(self): 

return str(self) 

 

 

VARIABLE = ( 

L("implementation_version") | 

L("platform_python_implementation") | 

L("implementation_name") | 

L("python_full_version") | 

L("platform_release") | 

L("platform_version") | 

L("platform_machine") | 

L("platform_system") | 

L("python_version") | 

L("sys_platform") | 

L("os_name") | 

L("os.name") | # PEP-345 

L("sys.platform") | # PEP-345 

L("platform.version") | # PEP-345 

L("platform.machine") | # PEP-345 

L("platform.python_implementation") | # PEP-345 

L("python_implementation") | # undocumented setuptools legacy 

L("extra") 

) 

ALIASES = { 

'os.name': 'os_name', 

'sys.platform': 'sys_platform', 

'platform.version': 'platform_version', 

'platform.machine': 'platform_machine', 

'platform.python_implementation': 'platform_python_implementation', 

'python_implementation': 'platform_python_implementation' 

} 

VARIABLE.setParseAction(lambda s, l, t: Variable(ALIASES.get(t[0], t[0]))) 

 

VERSION_CMP = ( 

L("===") | 

L("==") | 

L(">=") | 

L("<=") | 

L("!=") | 

L("~=") | 

L(">") | 

L("<") 

) 

 

MARKER_OP = VERSION_CMP | L("not in") | L("in") 

MARKER_OP.setParseAction(lambda s, l, t: Op(t[0])) 

 

MARKER_VALUE = QuotedString("'") | QuotedString('"') 

MARKER_VALUE.setParseAction(lambda s, l, t: Value(t[0])) 

 

BOOLOP = L("and") | L("or") 

 

MARKER_VAR = VARIABLE | MARKER_VALUE 

 

MARKER_ITEM = Group(MARKER_VAR + MARKER_OP + MARKER_VAR) 

MARKER_ITEM.setParseAction(lambda s, l, t: tuple(t[0])) 

 

LPAREN = L("(").suppress() 

RPAREN = L(")").suppress() 

 

MARKER_EXPR = Forward() 

MARKER_ATOM = MARKER_ITEM | Group(LPAREN + MARKER_EXPR + RPAREN) 

MARKER_EXPR << MARKER_ATOM + ZeroOrMore(BOOLOP + MARKER_EXPR) 

 

MARKER = stringStart + MARKER_EXPR + stringEnd 

 

 

def _coerce_parse_result(results): 

if isinstance(results, ParseResults): 

return [_coerce_parse_result(i) for i in results] 

else: 

return results 

 

 

def _format_marker(marker, first=True): 

assert isinstance(marker, (list, tuple, string_types)) 

 

# Sometimes we have a structure like [[...]] which is a single item list 

# where the single item is itself it's own list. In that case we want skip 

# the rest of this function so that we don't get extraneous () on the 

# outside. 

if (isinstance(marker, list) and len(marker) == 1 and 

isinstance(marker[0], (list, tuple))): 

return _format_marker(marker[0]) 

 

if isinstance(marker, list): 

inner = (_format_marker(m, first=False) for m in marker) 

if first: 

return " ".join(inner) 

else: 

return "(" + " ".join(inner) + ")" 

elif isinstance(marker, tuple): 

return " ".join([m.serialize() for m in marker]) 

else: 

return marker 

 

 

_operators = { 

"in": lambda lhs, rhs: lhs in rhs, 

"not in": lambda lhs, rhs: lhs not in rhs, 

"<": operator.lt, 

"<=": operator.le, 

"==": operator.eq, 

"!=": operator.ne, 

">=": operator.ge, 

">": operator.gt, 

} 

 

 

def _eval_op(lhs, op, rhs): 

try: 

spec = Specifier("".join([op.serialize(), rhs])) 

except InvalidSpecifier: 

pass 

else: 

return spec.contains(lhs) 

 

oper = _operators.get(op.serialize()) 

if oper is None: 

raise UndefinedComparison( 

"Undefined {0!r} on {1!r} and {2!r}.".format(op, lhs, rhs) 

) 

 

return oper(lhs, rhs) 

 

 

_undefined = object() 

 

 

def _get_env(environment, name): 

value = environment.get(name, _undefined) 

 

if value is _undefined: 

raise UndefinedEnvironmentName( 

"{0!r} does not exist in evaluation environment.".format(name) 

) 

 

return value 

 

 

def _evaluate_markers(markers, environment): 

groups = [[]] 

 

for marker in markers: 

assert isinstance(marker, (list, tuple, string_types)) 

 

if isinstance(marker, list): 

groups[-1].append(_evaluate_markers(marker, environment)) 

elif isinstance(marker, tuple): 

lhs, op, rhs = marker 

 

if isinstance(lhs, Variable): 

lhs_value = _get_env(environment, lhs.value) 

rhs_value = rhs.value 

else: 

lhs_value = lhs.value 

rhs_value = _get_env(environment, rhs.value) 

 

groups[-1].append(_eval_op(lhs_value, op, rhs_value)) 

else: 

assert marker in ["and", "or"] 

if marker == "or": 

groups.append([]) 

 

return any(all(item) for item in groups) 

 

 

def format_full_version(info): 

version = '{0.major}.{0.minor}.{0.micro}'.format(info) 

kind = info.releaselevel 

if kind != 'final': 

version += kind[0] + str(info.serial) 

return version 

 

 

def default_environment(): 

if hasattr(sys, 'implementation'): 

iver = format_full_version(sys.implementation.version) 

implementation_name = sys.implementation.name 

else: 

iver = '0' 

implementation_name = '' 

 

return { 

"implementation_name": implementation_name, 

"implementation_version": iver, 

"os_name": os.name, 

"platform_machine": platform.machine(), 

"platform_release": platform.release(), 

"platform_system": platform.system(), 

"platform_version": platform.version(), 

"python_full_version": platform.python_version(), 

"platform_python_implementation": platform.python_implementation(), 

"python_version": platform.python_version()[:3], 

"sys_platform": sys.platform, 

} 

 

 

class Marker(object): 

 

def __init__(self, marker): 

try: 

self._markers = _coerce_parse_result(MARKER.parseString(marker)) 

except ParseException as e: 

err_str = "Invalid marker: {0!r}, parse error at {1!r}".format( 

marker, marker[e.loc:e.loc + 8]) 

raise InvalidMarker(err_str) 

 

def __str__(self): 

return _format_marker(self._markers) 

 

def __repr__(self): 

return "<Marker({0!r})>".format(str(self)) 

 

def evaluate(self, environment=None): 

"""Evaluate a marker. 

 

Return the boolean from evaluating the given marker against the 

environment. environment is an optional argument to override all or 

part of the determined environment. 

 

The environment is determined from the current Python process. 

""" 

current_environment = default_environment() 

if environment is not None: 

current_environment.update(environment) 

 

return _evaluate_markers(self._markers, current_environment)