Debian Codegraph @

pypy/module/micronumpy/tool/numready/main.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 This should be run under PyPy.
6 """
7
8 import os
9 import platform
10 import subprocess
11 import tempfile
12 import webbrowser
13 from collections import OrderedDict
14
15 import jinja2
16
17 from .kinds import KINDS
18
19
20 class SearchableSet(object):
21 def __init__(self, items=()):
22 self._items = {}
23 for item in items:
24 self.add(item)
25
26 def __iter__(self):
27 return iter(self._items)
28
29 def __contains__(self, other):
30 return other in self._items
31
32 def __getitem__(self, idx):
33 return self._items[idx]
34
35 def add(self, item):
36 self._items[item] = item
37
38 def __len__(self):
39 return len(self._items)
40
41 class Item(object):
42 def __init__(self, name, kind, subitems=[]):
43 self.name = name
44 self.kind = kind
45 self.subitems = subitems
46
47 def __hash__(self):
48 return hash(self.name)
49
50 def __eq__(self, other):
51 if isinstance(other, str):
52 return self.name == other
53 return self.name == other.name
54
55
56 class ItemStatus(object):
57 def __init__(self, name, pypy_exists):
58 self.name = name
59 self.cls = 'exists' if pypy_exists else ''
60 self.symbol = u"✔" if pypy_exists else u'✖'
61
62 def __lt__(self, other):
63 return self.name < other.name
64
65 def find_numpy_items(python, modname="numpy", attr=None):
66 args = [
67 python, os.path.join(os.path.dirname(__file__), "search.py"), modname
68 ]
69 if attr is not None:
70 args.append(attr)
71 lines = subprocess.check_output(args).splitlines()
72 items = SearchableSet()
73 for line in lines:
74 # since calling a function in "search.py" may have printed side effects,
75 # make sure the line begins with '[UT] : '
76 if not (line[:1] in KINDS.values() and line[1:4] == ' : '):
77 continue
78 kind, name = line.split(" : ", 1)
79 subitems = []
80 if kind == KINDS["TYPE"] and name in SPECIAL_NAMES and attr is None:
81 subitems = find_numpy_items(python, modname, name)
82 items.add(Item(name, kind, subitems))
83 return items
84
85 def get_version_str(python):
86 args = [python, '-c', 'import sys; print sys.version']
87 lines = subprocess.check_output(args).splitlines()
88 return lines[0]
89
90 def split(lst):
91 SPLIT = 5
92 lgt = len(lst) // SPLIT + 1
93 l = [[] for i in range(lgt)]
94 for i in range(lgt):
95 for k in range(SPLIT):
96 if k * lgt + i < len(lst):
97 l[i].append(lst[k * lgt + i])
98 return l
99
100 SPECIAL_NAMES = ["ndarray", "dtype", "generic", "flatiter", "ufunc",
101 "nditer"]
102
103 def main(argv):
104 if 'help' in argv[1]:
105 print '\nusage: python', os.path.dirname(__file__), '<path-to-pypy> [<outfile.html>] [<path-to-cpython-with-numpy>]'
106 print ' path-to-cpython-with-numpy defaults to "/usr/bin/python"\n'
107 return
108 if len(argv) < 4:
109 cpython = '/usr/bin/python'
110 else:
111 cpython = argv[3]
112 cpy_items = find_numpy_items(cpython)
113 pypy_items = find_numpy_items(argv[1])
114 ver = get_version_str(argv[1])
115 all_items = []
116
117 msg = "{:d}/{:d} names".format(len(pypy_items), len(cpy_items)) + " "
118 msg += ", ".join(
119 "{:d}/{:d} {} attributes".format(
120 len(pypy_items[name].subitems), len(cpy_items[name].subitems), name
121 )
122 for name in SPECIAL_NAMES
123 )
124 for item in cpy_items:
125 pypy_exists = item in pypy_items
126 if item.subitems:
127 for sub in item.subitems:
128 all_items.append(
129 ItemStatus(item.name + "." + sub.name, pypy_exists=pypy_exists and pypy_items[item].subitems and sub in pypy_items[item].subitems)
130 )
131 all_items.append(ItemStatus(item.name, pypy_exists=item in pypy_items))
132 env = jinja2.Environment(
133 loader=jinja2.FileSystemLoader(os.path.dirname(__file__))
134 )
135 html = env.get_template("page.html").render(all_items=split(sorted(all_items)),
136 msg=msg, ver=ver)
137 if len(argv) > 2:
138 with open(argv[2], 'w') as f:
139 f.write(html.encode("utf-8"))
140 else:
141 with tempfile.NamedTemporaryFile(delete=False, suffix=".html") as f:
142 f.write(html.encode("utf-8"))
143 print "Saved in: %s" % f.name