-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.py
executable file
·69 lines (48 loc) · 1.82 KB
/
common.py
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import sys
import logging
from Peach.Engine.common import highlight
def isSupportedOS(platforms):
return filter(lambda x: x == sys.platform, platforms)
def isPosix():
return 'posix' in sys.builtin_module_names
def isLinux():
return sys.platform == "linux2"
def isMacOS():
return sys.platform == "darwin"
def isWindows():
return sys.platform == "win32"
def printHex(src):
FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or '.' for x in range(256)])
N = 0
result = ''
length = 16
while src:
s, src = src[:length], src[length:]
hexa = ' '.join(["%02X" % ord(x) for x in s])
s = s.translate(FILTER)
result += "%04X %-*s %s\n" % (N, length * 3, hexa, s)
N += length
print(result)
def getBooleanAttribute(args, name):
val = args.get(name, '0').lower().replace("'''", "")
result = val in ('true', 'yes', '1')
if not result:
assert val in ('false', 'no', '0')
return result
def getFloatAttribute(args, name, default=""):
return float(args.get(name, default).replace("'''", ""))
def getStringAttribute(args, name, default=""):
return args.get(name, default).replace("'''", "")
def setAttributesFromParams(node):
if node is not None and node.get('params') is not None:
for kv in node.get('params').split(','):
try:
k, v = [s.strip() for s in kv.split('=', 1)]
except ValueError:
logging.error(highlight.error("The macro %s has no value." % kv))
sys.exit(-1)
k = k[0].lower() + k[1:] # CamelCase -> camelCase
node.set(k, v)