MLECO-3036: Update to use Pathlib in Python scripts
* Pathlib used in Python scripts over os
* Bug fix for build_default.py
* Minor code style updates
Signed-off-by: Richard Burton <richard.burton@arm.com>
Change-Id: I5fc2e582a84443c3fb79250eb711b960d63ed8fd
diff --git a/scripts/py/gen_model_cpp.py b/scripts/py/gen_model_cpp.py
index c43c93a..de71992 100644
--- a/scripts/py/gen_model_cpp.py
+++ b/scripts/py/gen_model_cpp.py
@@ -19,9 +19,9 @@
should the models need to be generated at configuration stage.
"""
import datetime
-import os
from argparse import ArgumentParser
from pathlib import Path
+
from jinja2 import Environment, FileSystemLoader
import binascii
@@ -36,7 +36,7 @@
default="header_template.txt")
args = parser.parse_args()
-env = Environment(loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')),
+env = Environment(loader=FileSystemLoader(Path(__file__).parent / 'templates'),
trim_blocks=True,
lstrip_blocks=True)
@@ -70,20 +70,20 @@
def main(args):
- if not os.path.isfile(args.tflite_path):
+ if not Path(args.tflite_path).is_file():
raise Exception(f"{args.tflite_path} not found")
# Cpp filename:
- cpp_filename = Path(os.path.join(args.output_dir, os.path.basename(args.tflite_path) + ".cc")).absolute()
- print(f"++ Converting {os.path.basename(args.tflite_path)} to\
- {os.path.basename(cpp_filename)}")
+ cpp_filename = (Path(args.output_dir) / (Path(args.tflite_path).name + ".cc")).resolve()
+ print(f"++ Converting {Path(args.tflite_path).name} to\
+ {cpp_filename.name}")
- os.makedirs(cpp_filename.parent, exist_ok=True)
+ cpp_filename.parent.mkdir(exist_ok=True)
header_template = env.get_template(args.license_template)
- hdr = header_template.render(script_name=os.path.basename(__file__),
- file_name=os.path.basename(args.tflite_path),
+ hdr = header_template.render(script_name=Path(__file__).name,
+ file_name=Path(args.tflite_path).name,
gen_time=datetime.datetime.now(),
year=datetime.datetime.now().year)