Skip to content

Commit f4c4e16

Browse files
committed
Factor out version replacement so its shared between individual
files and files in a package. This fixes the bytes-like error that happens when trying to build packages.
1 parent 88731d5 commit f4c4e16

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

circuitpython_build_tools/build.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ def mpy_cross(mpy_cross_filename, circuitpython_tag, quiet=False):
7676
if make.returncode != 0:
7777
sys.exit(make.returncode)
7878

79+
def _munge_to_temp(original_path, temp_file, library_version):
80+
with open(original_path, "rb") as original_file:
81+
for line in original_file:
82+
line = line.decode("utf-8").strip("\n")
83+
if line.startswith("__version__"):
84+
line = line.replace("0.0.0-auto.0", library_version)
85+
temp_file.write(line.encode("utf-8") + b"\r\n")
86+
temp_file.flush()
7987

8088
def library(library_path, output_directory, mpy_cross=None):
8189
py_files = []
@@ -119,13 +127,7 @@ def library(library_path, output_directory, mpy_cross=None):
119127
output_file = os.path.join(output_directory,
120128
filename.replace(".py", new_extension))
121129
with tempfile.NamedTemporaryFile() as temp_file:
122-
with open(full_path, "rb") as original_file:
123-
for line in original_file:
124-
line = line.decode("utf-8").strip("\n")
125-
if line.startswith("__version__"):
126-
line = line.replace("0.0.0-auto.0", library_version)
127-
temp_file.write(line.encode("utf-8") + b"\r\n")
128-
temp_file.flush()
130+
_munge_to_temp(full_path, temp_file, library_version)
129131

130132
if mpy_cross:
131133
mpy_success = subprocess.call([mpy_cross,
@@ -140,12 +142,7 @@ def library(library_path, output_directory, mpy_cross=None):
140142
for filename in package_files:
141143
full_path = os.path.join(library_path, filename)
142144
with tempfile.NamedTemporaryFile() as temp_file:
143-
with open(full_path) as original_file:
144-
for line in original_file:
145-
if line.startswith("__version__"):
146-
line = line.replace("0.0.0-auto.0", library_version)
147-
temp_file.write(line + "\r\n")
148-
temp_file.flush()
145+
_munge_to_temp(full_path, temp_file, library_version)
149146
if (not mpy_cross or
150147
os.stat(full_path).st_size == 0 or
151148
filename.endswith("__init__.py")):

0 commit comments

Comments
 (0)