-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
306 lines (258 loc) · 10.5 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# common.py -
import os
import re
import tarfile
import requests
from datetime import datetime
from lxml import html
#-------------------------------------------------------------------------------
# Globals
#-------------------------------------------------------------------------------
# Under lfs_data there will be a pkgs directory. Set by parse.py.
lfs_data = None
# Files persistend in between runs. Set by set_tmp_path(), called by book.py.
tmp_path = None
sec_pat = r'(([0-9])\.([0-9]{1,2})\.?)'
pkg_pat = '(([$a-zA-Z0-9:_]+)(-[$a-zA-Z0-9:_]+)?)'
ver_pat = '([a-z0-9.]+)'
#-------------------------------------------------------------------------------
# get_sect_id -
#-------------------------------------------------------------------------------
def get_sect_id(s):
m = re.match(f'{sec_pat}', s)
if m:
# Normalize before returning
return f'{m.group(2)}.{m.group(3):>02}'
#-------------------------------------------------------------------------------
# normalize_sect_id -
#-------------------------------------------------------------------------------
def normalize_sect_id(s):
m = re.match(f'{sec_pat}', s)
if m:
return f'{m.group(2)}.{m.group(3):>02}'
#-------------------------------------------------------------------------------
# parse_preps - preparation titles in ch.5 and 6
#-------------------------------------------------------------------------------
def parse_preps(s):
# Section here is not normalized
section = name = None
m = re.match(f'{sec_pat} (.*)$', s)
if m:
section = f'{m.group(2)}.{m.group(3):>02}'
name = m.group(4)
return section, name
#-------------------------------------------------------------------------------
# parse_title - extract information from LFS book section titles in ch.5 and 6
#-------------------------------------------------------------------------------
# Could make the version optional, and remove parse_preps
def parse_title(s):
sect_id = name = version = None
m = re.match(f'{sec_pat} {pkg_pat}-{ver_pat}', s)
if m:
sect_id = f'{m.group(2)}.{m.group(3):>02}'
name = m.group(4)
version = m.group(7)
else:
# Special case of libstdc+ and elflibs
m = re.match(f'{sec_pat} [a-zA-Z+]+ from {pkg_pat}-{ver_pat}$', s)
if m:
sect_id = f'{m.group(2)}.{m.group(3):>02}'
name = m.group(4)
version = m.group(7)
return sect_id, name, version
#-------------------------------------------------------------------------------
# parse_filename - extract information from the shell script filenames
#-------------------------------------------------------------------------------
def parse_filename(s):
sect_id = sec_type = name = version = None
pat = f'{sec_pat}_[0-9]{2}_(build|run|run_chk|check)_{pkg_pat}(-({ver_pat}))?.sh$'
m = re.match(pat, s)
if m:
sect_id = m.group(1)
sec_type = m.group(4)
name = m.group(5)
version = m.group(8)
else:
print(f'parse_filename: couldn\'t parse "{s}"')
return sect_id, sec_type, name, version
#-------------------------------------------------------------------------------
# commentify - turn a multi-line string into a multi-line bash comment
#-------------------------------------------------------------------------------
def commentify(t):
"""Turn a multi-line string into a multi-line bash comment
This actually turns some text into a bash script echoing the same
text as a bash comment.
The argument to echo will be in between single quotes, because the
text may reference shell variables and we don't want to expand them
here, so any single quotes in the text need to be escaped first.
However, the bash manual clearly states that "a single quote may not
occur between single quotes, even when preceded by a backslash".
https://stackoverflow.com/questions/1250079 has a nice (if somewhat
unreadable) trick for this: write the single quote in between double
quotes (for bash), and add an outer layer of single quotes so that
bash actually concatenates 3 strings (in single quotes, double
quotes, and single quotes again), using the outermost single quotes
from your echo command.
"""
z = ''
for s in t.splitlines():
s = s.replace("'", "'\"'\"'")
z += f"echo '# {s}'\n"
return z
#-------------------------------------------------------------------------------
# replace_values -
#-------------------------------------------------------------------------------
def replace_values(text, values):
"""Replace placeholders with actual values in the text.
Placeholders are of the formm <code>xxx</code>. They are replaced
with actual values from the 'values' argument, which is an array of
strings.
"""
s = text
vals = iter(values)
t = ''
while True:
# Note the non-greedy * operator, this is requird so we can
# match something like <code>xxx</code><code>yyy</code>
m = re.search('<code>.*?</code>', s, re.MULTILINE)
if not m:
t += s
return t
try:
t += s[:m.start()] + next(vals)
except StopIteration:
raise RuntimeError(f'Not enough values to match text "{text}"')
s = s[m.end():]
#-------------------------------------------------------------------------------
# tar_extract - un-tar a package file
#-------------------------------------------------------------------------------
def tar_extract(name, version):
"""Un-tar the given file in the current directory."""
filename = get_pkg_filename(name, version)
# print(f'Opening tarfile {filename}')
with tarfile.open(filename) as t:
m = t.getmembers()[0]
t.extractall()
# Confirm directory creation with the expected name
pkg_dir = m.name.split('/', maxsplit=1)[0]
if not os.path.isdir(pkg_dir):
print(f'Expected directory {pkg_dir} not found')
return
return pkg_dir
#-------------------------------------------------------------------------------
# file_header -
#-------------------------------------------------------------------------------
def file_header(filepath, book, chrooted=False):
x = filepath.split('/', maxsplit=1)
filename = x[1] if len(x) == 2 else x[0]
dt = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
s = ''
# Generate file header comments
s += f"""
# {filepath} generated on {dt}
# from {book}
echo "#{'='*79}"
echo "# $0 user \\"$USER\\" pid $$"
echo "#{'='*79}"
date
"""
# Path depends on whether the environment is chroot'ed or not when this
# script gets run
path = '/pkg_lfs' if chrooted else f'{tmp_path}'
# Support for running SBU calculations
s += f"""
# If SBU values have been calculated, get them
if [ -f {path}/sbu_in_secs.txt ]; then
sbu_in_secs=$(cat {path}/sbu_in_secs.txt)
fi
if [ -f {path}/sbu_total.txt ]; then
sbu_total=$(cat {path}/sbu_total.txt)
fi
# Bash function to display elapsed time as hh mm ss
function elapsed {{
local duration=$1
local minutes=$(( $duration/60))
local secs=$(( $duration%60))
printf "%02d:%02d:%02d" $(( minutes/60)) $(( minutes%60)) ${{secs}}
}}
"""
return s
#-------------------------------------------------------------------------------
# file_footer -
#-------------------------------------------------------------------------------
def file_footer(filepath):
s = ''
s += f"""
echo "#{'='*79}"
echo "# End of {filepath}"
echo "#{'='*79}"
"""
return s
#-------------------------------------------------------------------------------
# set_tmp_path -
#-------------------------------------------------------------------------------
def set_tmp_path(version):
"""Set the path for files persisted in between runs.
Script files generated by parse.py are stored in this directory, so
they can be reused, for example, if the build run is interrupted by
a reboot.
"""
global tmp_path
tmp_path = os.path.join('/var/tmp/lfs', version)
if not os.path.isdir(tmp_path):
oldmask = os.umask(000)
os.makedirs(tmp_path, mode=0o777, exist_ok=True)
os.umask(oldmask)
#-------------------------------------------------------------------------------
# get_html_root -
#-------------------------------------------------------------------------------
def get_html_root(version):
"""Return the path of the LFS book to be used.
We use the single-page HTML version of the book. We search for it
locally, and if we don't have it, then we download it from the LFS
website.
"""
# Directory for LFS data such as pkgs and the book itself
global lfs_data
if 'LFS_DATA' in os.environ:
lfs_data = os.environ['LFS_DATA']
lfs_data = os.path.join(lfs_data, version)
print(f'LFS_DATA: using {lfs_data}')
else:
lfs_data = os.path.join(os.environ['HOME'], 'lfs')
lfs_data = os.path.join(lfs_data, version)
print(f'LFS_DATA: defaulting to {lfs_data}')
if not os.path.isdir(lfs_data):
oldmask = os.umask(000)
os.makedirs(lfs_data, mode=0o777, exist_ok=True)
os.umask(oldmask)
# Do we have the file already?
for f in os.listdir(lfs_data):
pat = f'LFS-BOOK-{version}-NOCHUNKS-(.*).html'
m = re.match(pat, f)
if m:
# We have the file, return the root of the HTML tree
filepath = os.path.join(lfs_data, f)
print(f'Using local file "{filepath}"')
with open(filepath, 'r', encoding=m.group(1)) as f:
content = f.read()
root = html.fromstring(content)
return root
# We couldn't find the file
filename = f'LFS-BOOK-{version}-NOCHUNKS.html'
print(f'No LFS book found, downloading "{filename}" from the web.')
url = f'http://www.linuxfromscratch.org/lfs/downloads/{version}/{filename}'
r = requests.get(url)
if r.status_code >= 400:
raise RuntimeError(f'[{r.status_code}] {r.text}')
# Store the encoding from the HTTP headers in the filename
filename = f'LFS-BOOK-{version}-NOCHUNKS-{r.encoding}.html'
filepath = os.path.join(lfs_data, filename)
with open(filepath, 'w', encoding=r.encoding) as f:
f.write(r.text)
return html.fromstring(r.text)
#===============================================================================
# main
#===============================================================================
if __name__ == '__main__':
print("""This module is not meant to run directly.""")