Skip to content

Commit 0601d82

Browse files
build: port all python from python2 to python3
ONIE still shipped python2-only code throughout the tree. Port every python asset to python3 so the build no longer needs a python2 interpreter (a prerequisite for moving to a modern Debian build host) and so nothing in contrib/ or the machine tests is left on a dead runtime. This change deliberately does NOT touch the CI Dockerfile, so the cross-toolchain cache key is preserved (the Debian-13 build-host bump is deferred to the toolchain-upgrade step, where it must land alongside crosstool-NG/GCC -- the current GCC 8.3.0 toolchain cannot be built on a GCC 14 host). Build path: - build-config/scripts/mk-part-table.py (the only python invoked by the image build -- it writes the recovery ISO MBR partition table): env python3 shebang, integer division (/ -> //) so CHS values stay ints, and the ISO opened in binary mode ('r+b') so struct.pack() bytes are written correctly. A shebang-only change is insufficient; without the // and binary fixes the script silently writes a wrong partition table under python3. Verified to emit a correct MBR (0xaa55 signature, 0x80 boot flag, 0xEF EFI type) on a test image. - build-config/Makefile: retarget $(PYTHON) from python2 to python3. grub's autogen.sh invokes "${PYTHON:-python}", and a bare "python" no longer exists once python2 is gone, so the build must export PYTHON=python3 (grub's gentpl.py/import_gcry.py are python3-safe). The old block only set PYTHON=python2 when "python" was absent; replace it with an exported `PYTHON ?= python3`. Also switch DEBIAN_BUILD_HOST_PACKAGES from python-all-dev/python-sphinx to python3-all-dev/python3-sphinx. Tooling and tests (mechanical 2to3, behaviour preserved): - contrib/oce, contrib/onie-server, contrib/git-stats/gitdm (vendored), and the machine/*/test DUT scripts: python3 shebangs; print() functions; `except E, e` -> `except E as e`; iteritems/iterkeys/ itervalues -> items/keys/values; has_key -> `in`; raw_input -> input; cStringIO/StringIO -> io; py2 stdlib renames (Queue->queue, SimpleHTTPServer/SocketServer->http.server/socketserver, thread-> _thread); sort(cmp=...) -> key=cmp_to_key(...); list() wrapping of views/maps that are indexed or sorted; onie-server.py spawns its helpers with "python3" instead of "python". - contrib/onie-server/proxy-server.py was cp1252-encoded; re-encode it as UTF-8 and update the coding declaration so its (Portuguese) comments survive the python3 port intact. Every python file in the tree now passes `python3 -m py_compile`, all shebang scripts use `#!/usr/bin/env python3`, and no python2-only syntax remains. Signed-off-by: Brad House <bhouse@nexthop.ai> Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent cc4e3f8 commit 0601d82

21 files changed

Lines changed: 170 additions & 170 deletions

File tree

build-config/Makefile

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -515,13 +515,11 @@ ifeq ($(FIRMWARE_UPDATE_ENABLE),yes)
515515
include make/firmware-update.make
516516
endif
517517

518-
# Pre Debian 11 environments just had /usr/bin/python
519-
# Now the environment distinguishes between python2 and python3
520-
# If 'python' is not present, default to python2
521-
ifeq (, $(shell which python ))
522-
export PYTHON=python2
523-
endif
524-
518+
# Some sub-package build scripts (notably grub's autogen.sh) invoke the
519+
# interpreter as "${PYTHON:-python}". A bare "python" no longer exists on
520+
# modern systems now that python2 is gone, so point $(PYTHON) at python3.
521+
# (grub's build scripts -- gentpl.py, import_gcry.py -- are python3-safe.)
522+
export PYTHON ?= python3
525523

526524
#-------------------------------------------------------------------------------
527525
#
@@ -594,11 +592,11 @@ machine-prefix:
594592
# The onie/build-config/scripts/onie-build-targets.json file lists
595593
# platforms and known working build environments.
596594
DEBIAN_BUILD_HOST_PACKAGES = build-essential stgit u-boot-tools util-linux \
597-
gperf device-tree-compiler python-all-dev xorriso \
595+
gperf device-tree-compiler python3-all-dev xorriso \
598596
autoconf automake bison flex texinfo libtool libtool-bin \
599597
gawk libncurses5 libncurses5-dev bc \
600598
dosfstools mtools pkg-config git wget help2man libexpat1 \
601-
libexpat1-dev fakeroot python-sphinx rst2pdf \
599+
libexpat1-dev fakeroot python3-sphinx rst2pdf \
602600
libefivar-dev libnss3-tools libnss3-dev libpopt-dev \
603601
libssl-dev sbsigntool uuid-runtime uuid-dev cpio \
604602
bsdmainutils unzip

build-config/scripts/mk-part-table.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/python2
1+
#!/usr/bin/env python3
22

33
from struct import pack
44
import sys
@@ -23,8 +23,8 @@
2323
# See MBR partition table entry format here:
2424
# http://en.wikipedia.org/wiki/Master_boot_record#Partition_table_entries
2525
def chs(sector_z):
26-
C = sector_z / (63 * 255)
27-
H = (sector_z % (63 * 255)) / 63
26+
C = sector_z // (63 * 255)
27+
H = (sector_z % (63 * 255)) // 63
2828
# convert zero-based sector to CHS format
2929
S = (sector_z % 63) + 1
3030
# munge accord to partition table format
@@ -42,7 +42,7 @@ def chs(sector_z):
4242
#
4343
# See the partition table format here:
4444
# http://en.wikipedia.org/wiki/Master_boot_record#Sector_layout
45-
f = open(iso, 'r+')
45+
f = open(iso, 'r+b')
4646
f.seek(0x1BE)
4747
f.write(pack("<8BLL48xH", 0x80, s_H, s_S, s_C,
4848
fs_type, e_H, e_S, e_C, start, length, 0xaa55))

contrib/git-stats/gitdm/ConfigFile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ def ReadFileType (filename):
147147
m = regex_file_type.match (line)
148148
if not m or len (m.groups ()) != 2:
149149
ConfigFile.croak ('Funky file type line "%s"' % (line))
150-
if not patterns.has_key (m.group (1)):
150+
if m.group (1) not in patterns:
151151
patterns[m.group (1)] = []
152152
if m.group (1) not in order:
153-
print '%s not found, appended to the last order' % m.group (1)
153+
print('%s not found, appended to the last order' % m.group (1))
154154
order.append (m.group (1))
155155

156156
patterns[m.group (1)].append (re.compile (m.group (2), re.IGNORECASE))

contrib/git-stats/gitdm/csvdump.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def store_patch(patch):
5050
ChangeSets.append([patch.commit, str(patch.date),
5151
patch.email, domain, author, employer,
5252
patch.added, patch.removed])
53-
for (filetype, (added, removed)) in patch.filetypes.iteritems():
53+
for (filetype, (added, removed)) in patch.filetypes.items():
5454
FileTypes.append([patch.commit, filetype, added, removed])
5555

5656

contrib/git-stats/gitdm/database.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def emailemployer (self, email, date):
3838
for edate, empl in self.employer[i]:
3939
if edate > date:
4040
return empl
41-
print 'OOPS. ', self.name, self.employer, self.email, email, date
41+
print('OOPS. ', self.name, self.employer, self.email, email, date)
4242
return None # Should not happen
4343

4444
def addpatch (self, patch):
@@ -118,11 +118,11 @@ def LookupStoreHacker(name, email, mapunknown = True):
118118

119119

120120
def AllHackers ():
121-
return HackersByID.values ()
121+
return list(HackersByID.values ())
122122

123123
def DumpDB ():
124124
out = open ('database.dump', 'w')
125-
names = HackersByName.keys ()
125+
names = list(HackersByName.keys ())
126126
names.sort ()
127127
for name in names:
128128
h = HackersByName[name]
@@ -179,7 +179,7 @@ def GetEmployer (name):
179179
return e
180180

181181
def AllEmployers ():
182-
return Employers.values ()
182+
return list(Employers.values ())
183183

184184
#
185185
# Certain obnoxious developers, who will remain nameless (because we
@@ -209,8 +209,8 @@ def applysplits (self):
209209
self.__init__ (name) # Reset counts just in case
210210

211211
def store (self):
212-
if Employers.has_key (self.name):
213-
print Employers[self.name]
212+
if self.name in Employers:
213+
print(Employers[self.name])
214214
sys.stderr.write ('WARNING: Virtual empl %s overwrites another\n'
215215
% (self.name))
216216
if len (self.splits) == 0:
@@ -229,7 +229,7 @@ def guess_file_type (self, filename, patterns=None, order=None):
229229
order = order or self.order
230230

231231
for file_type in order:
232-
if patterns.has_key (file_type):
232+
if file_type in patterns:
233233
for patt in patterns[file_type]:
234234
if patt.search (filename):
235235
return file_type
@@ -255,7 +255,7 @@ def MixVirtuals ():
255255
EmailAliases = { }
256256

257257
def AddEmailAlias (variant, canonical):
258-
if EmailAliases.has_key (variant):
258+
if variant in EmailAliases:
259259
sys.stderr.write ('Duplicate email alias for %s\n' % (variant))
260260
EmailAliases[variant] = canonical
261261

@@ -282,7 +282,7 @@ def AddEmailEmployerMapping (email, employer, end = nextyear):
282282
for i in range (0, len(l)):
283283
date, xempl = l[i]
284284
if date == end: # probably both nextyear
285-
print 'WARNING: duplicate email/empl for %s' % (email)
285+
print('WARNING: duplicate email/empl for %s' % (email))
286286
if date > end:
287287
l.insert (i, (end, empl))
288288
return
@@ -299,7 +299,7 @@ def MapToEmployer (email, unknown = 0):
299299
pass
300300
namedom = email.split ('@')
301301
if len (namedom) < 2:
302-
print 'Oops...funky email %s' % email
302+
print('Oops...funky email %s' % email)
303303
return [(nextyear, GetEmployer ('Funky'))]
304304
s = namedom[1].split ('.')
305305
for dots in range (len (s) - 2, -1, -1):

contrib/git-stats/gitdm/findoldfiles

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/python
1+
#!/usr/bin/env python3
22
#
33
# Another quick hack of a script to find files unchanged
44
# since a given commit.
@@ -19,7 +19,7 @@ def CheckFile(file):
1919
git = os.popen('git log --pretty=oneline -1 ' + file, 'r')
2020
line = git.readline()
2121
if line.startswith(OriginalSin):
22-
print file
22+
print(file)
2323
git.close()
2424
#
2525
# Here we just plow through all the files.

contrib/git-stats/gitdm/gitlog.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def SaveLine(line):
6161
def get_header(patch, line, input):
6262
if line == '':
6363
if patch.author == '':
64-
print 'Funky auth line in', patch.commit
64+
print('Funky auth line in', patch.commit)
6565
patch.author = database.LookupStoreHacker('Unknown',
6666
'unknown@hacker.net')
6767
return S_DESC
@@ -78,7 +78,7 @@ def get_header(patch, line, input):
7878

7979
def get_desc(patch, line, input):
8080
if not line:
81-
print 'Missing desc in', patch.commit
81+
print('Missing desc in', patch.commit)
8282
return S_CHANGELOG
8383
patch.desc = line
8484
line = getline(input)
@@ -186,7 +186,7 @@ def grabpatch(input):
186186
return None
187187
m = patterns['commit'].match(line)
188188
if not m:
189-
print 'noncommit', line
189+
print('noncommit', line)
190190
return None
191191
p = patch(m.group(1))
192192
state = S_HEADER
@@ -197,7 +197,7 @@ def grabpatch(input):
197197
line = getline(input)
198198
if line is None:
199199
if state != S_NUMSTAT:
200-
print 'Ran out of patch', state
200+
print('Ran out of patch', state)
201201
return None
202202
return p
203203
state = grabbers[state](p, line, input)

contrib/git-stats/gitdm/logparser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
#-*- coding:utf-8 -*-
33
#
44
# Copyright © 2009 Germán Póo-Caamaño <gpoo@gnome.org>
@@ -85,6 +85,6 @@ def __grab_patch__(self):
8585
patches = LogPatchSplitter(sys.stdin)
8686

8787
for patch in patches:
88-
print '---------- NEW PATCH ----------'
88+
print('---------- NEW PATCH ----------')
8989
for line in patch:
90-
print line,
90+
print(line, end=' ')

0 commit comments

Comments
 (0)