forked from jawher/xavr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
181 lines (139 loc) · 4.8 KB
/
setup.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
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
import errno
import os
import re
import shutil
import string
import subprocess
import sys
ITER_BEGIN = re.compile('\s*@iter\s+(.+?)@\s*')
ITER_END = re.compile('\s*@end@\s*')
def exec_iter(items, template, output):
lines = []
for line in template:
m = ITER_END.match(line)
if m:
break
else:
lines.append(line)
for item in items:
for line in lines:
output.write(line.format(**item))
def exec_template(from_template, to, model):
with open(from_template, 'r') as template:
with open(to, 'w') as output:
for line in template:
m = ITER_BEGIN.match(line)
if m:
items = model[m.group(1)]
exec_iter(items, template, output)
else:
output.write(line.format(**model))
ISYS = '#include <...> search starts here:'
def mcu_to_def(mcu):
defi = mcu.upper()
families = ['XMEGA', 'MEGA', 'TINY']
for family in families:
defi = defi.replace(family, family.lower())
return '__AVR_' + defi + '__'
def supported_mcus():
HEADER = 'Known MCU names:'
proc = subprocess.Popen('avr-gcc --target-help', stdout=subprocess.PIPE, stderr=subprocess.PIPE,
shell=True)
out, err = proc.communicate()
lines = out.decode('utf-8').split('\n')
mcus = []
consider = False
for line in lines:
print(line)
if HEADER in line:
consider = True
elif consider:
if line.startswith(' '):
for mcu in line.split():
mcus.append({'mcu': mcu, 'defi': mcu_to_def(mcu)})
else:
break
return mcus
def supported_programmers():
HEADER = 'Valid programmers are:'
PROG_LINE = re.compile(' (.+?)\s+=.*')
proc = subprocess.Popen('avrdude -c?', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = proc.communicate()
lines = err.decode('utf-8').split('\n')
programmers = []
consider = False
for line in lines:
if line == HEADER:
consider = True
elif consider:
m = PROG_LINE.match(line)
if m:
programmers.append({'programmer': m.group(1)})
else:
break
return programmers
def avr_loc():
bin_path = subprocess.check_output('which avr-gcc', shell=True).strip()
return os.path.dirname(os.path.dirname(os.path.realpath(bin_path)))
def avrdude_loc():
return subprocess.check_output('which avrdude', shell=True).strip()
def isystem():
proc = subprocess.Popen('echo | avr-cpp -v', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = proc.communicate()
lines = err.decode('utf-8').split('\n')
isys = []
consider = False
for line in lines:
if line == ISYS:
consider = True
elif consider:
if line.startswith(' '):
isys.append(os.path.normpath(line.strip()))
else:
break
return isys
def ensure_installed(tool):
proc = subprocess.Popen('which ' + tool, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = proc.communicate()
out = out.decode('utf-8').strip()
exitcode = proc.returncode
if exitcode == 0:
print('Found {t} install in "{p}"'.format(t=tool, p=out))
return out
else:
print(tool + ' is not installed (or is not in the PATH). Exiting')
sys.exit(1)
def mkdirs_p(dirs):
try:
os.makedirs(dirs)
except OSError as e:
if e.errno == errno.EEXIST:
pass
else:
raise
def main():
model = {}
tools = ['avr-gcc', 'avr-g++', 'avr-objcopy', 'avr-objdump', 'avr-size', 'avr-nm', 'avrdude']
for tool in tools:
model[tool + '_loc'] = ensure_installed(tool)
exec_template('Makefile.tpl', 'Makefile', model)
model = {'isystem': ' '.join(isystem()),
'mcus': supported_mcus(),
'programmers': supported_programmers()
}
exec_template('TemplateInfo.plist.tpl', 'TemplateInfo.plist', model)
print('Generated template:\n\tMCUs : {}\n\tProgrammers : {}'
.format(len(model['mcus']), len(model['programmers'])))
DEST_DIR = os.path.join(os.path.expanduser('~'),
'Library/Developer/Xcode/Templates/Project Template/xavr/xavr.xctemplate/')
print('Installing template in: "{}"'.format(DEST_DIR))
mkdirs_p(DEST_DIR)
shutil.copy('main.c', DEST_DIR)
shutil.copy('Makefile', DEST_DIR)
shutil.copy('TemplateInfo.plist', DEST_DIR)
shutil.copy('TemplateIcon.icns', DEST_DIR)
os.remove('Makefile')
os.remove('TemplateInfo.plist')
print('Done. Hack away !\n')
if __name__ == '__main__':
main()