forked from m-labs/migen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
93 lines (75 loc) · 2.55 KB
/
tools.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import struct
from distutils.version import StrictVersion, LooseVersion
import re
import subprocess
import sys
import ctypes
def language_by_filename(name):
extension = name.rsplit(".")[-1]
if extension in ["v", "vh", "vo"]:
return "verilog"
if extension in ["vhd", "vhdl", "vho"]:
return "vhdl"
return None
def write_to_file(filename, contents, force_unix=False):
newline = None
if force_unix:
newline = "\n"
with open(filename, "w", newline=newline) as f:
f.write(contents)
def arch_bits():
return struct.calcsize("P")*8
def versions(path, strict=True):
for n in os.listdir(path):
full = os.path.join(path, n)
if not os.path.isdir(full):
continue
try:
if strict:
yield StrictVersion(n)
else:
yield LooseVersion(n)
except ValueError:
continue
def sub_rules(line, rules, max_matches=1):
for pattern, color in rules:
line, matches = re.subn(pattern, color, line, max_matches)
max_matches -= matches
if not max_matches:
break
return line
def subprocess_call_filtered(command, rules, *, max_matches=1, **kwargs):
with subprocess.Popen(command, stdout=subprocess.PIPE,
universal_newlines=True, bufsize=1,
**kwargs) as proc:
with open(proc.stdout.fileno(), errors="ignore", closefd=False) as stdout:
for line in stdout:
print(sub_rules(line, rules, max_matches), end="")
return proc.wait()
if sys.platform == "cygwin":
cygwin1 = ctypes.CDLL("/usr/bin/cygwin1.dll")
cygwin_conv_path_proto = ctypes.CFUNCTYPE(
ctypes.c_ssize_t, # Return
ctypes.c_uint, # what
ctypes.c_void_p, # from
ctypes.c_void_p, # to
ctypes.c_size_t) # size
cygwin_conv_path = cygwin_conv_path_proto(("cygwin_conv_path", cygwin1),
((1, "what"),
(1, "from"),
(1, "to"),
(1, "size")))
def cygpath_to_windows(path):
what = ctypes.c_uint(0) # CCP_POSIX_TO_WIN_A
fro = ctypes.c_char_p(path.encode('utf-8'))
to = ctypes.byref(ctypes.create_string_buffer(260))
size = ctypes.c_size_t(260)
cygwin_conv_path(what, fro, to, size)
return ctypes.cast(to, ctypes.c_char_p).value.decode('utf-8')
# Convert cygwin paths to Windows native paths. This is a noop otherwise.
def cygpath(p):
return cygpath_to_windows(p)
else:
def cygpath(p):
return p