forked from SublimeCodeIntel/SublimeCodeIntel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpybinary.py
More file actions
103 lines (69 loc) · 2.48 KB
/
pybinary.py
File metadata and controls
103 lines (69 loc) · 2.48 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
#!/usr/bin/env python
# Copyright (c) 2010 ActiveState Software Inc.
# See LICENSE.txt for license details.
"""Python binary file scanner for CodeIntel"""
import os
import sys
import io as io
import optparse
from process import ProcessOpen
SCAN_PROCESS_TIMEOUT = 2.0
class BinaryScanError(Exception):
pass
class TimedPopen(ProcessOpen):
def wait(self):
return super(TimedPopen, self).wait(SCAN_PROCESS_TIMEOUT)
def safe_scan(path, python):
"""
Performs a "safe" out-of-process scan.
It is more safe because if the module being scanned runs amuck,
it will not ruin the main Komodo interpreter.
"path" - is a path to a binary module
"python" - an absolute path to the interpreter to run the scanner
Returns a CIX 2.0 string.
In case of errors raises a BinaryScanError.
"""
# indirectly calls "_main" defined below
# this is needed to avoid picking up a stale .pyc file.
myself = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"pybinary.py")
proc = TimedPopen(cmd="%s %s %s" % (python, myself, path),
env=dict(PYTHONPATH=os.pathsep.join(sys.path)))
out, err = proc.communicate()
if err:
raise BinaryScanError(err)
return out
def scan(path):
"""
Performs an in-process binary module scan. That means the module is
loaded (imported) into the current Python interpreter.
"path" - a path to a binary module to scan
Returns a CIX 2.0 XML string.
"""
from gencix.python import gencixcore as gencix
name, _ = os.path.splitext(os.path.basename(path))
dir = os.path.dirname(path)
root = gencix.Element('codeintel', version='2.0', name=name)
gencix.docmodule(name, root, usefile=True, dir=dir)
gencix.perform_smart_analysis(root)
gencix.prettify(root)
tree = gencix.ElementTree(root)
stream = io.StringIO()
try:
stream.write('<?xml version="1.0" encoding="UTF-8"?>\n')
tree.write(stream)
return stream.getvalue()
finally:
stream.close()
def _main(argv):
parser = optparse.OptionParser(usage="%prog mdoulepath")
(options, args) = parser.parse_args(args=argv)
if len(args) != 1:
parser.error("Incorrect number of args")
mod_path = os.path.abspath(args[0])
if not os.path.isfile(mod_path):
parser.error("'%s' is not a file" % mod_path)
cix = scan(mod_path)
print(cix)
if __name__ == '__main__':
_main(sys.argv[1:])