From 75a9c477bb930736962844dbb9ca633dd3ac003f Mon Sep 17 00:00:00 2001 From: firewave Date: Mon, 10 Jun 2024 17:25:44 +0200 Subject: [PATCH] reduced usage of `os.chdir()` in Python scripts --- htmlreport/cppcheck-htmlreport | 11 +++-------- tools/test-my-pr.py | 6 ++---- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/htmlreport/cppcheck-htmlreport b/htmlreport/cppcheck-htmlreport index a70fbdb4c83..10300ea50ec 100755 --- a/htmlreport/cppcheck-htmlreport +++ b/htmlreport/cppcheck-htmlreport @@ -441,9 +441,8 @@ def git_blame(errors, path, file, blame_options): full_path = os.path.join(path, file) path, filename = os.path.split(full_path) - cwd = os.getcwd() - if path: - os.chdir(path) + if not path: + path = None # make sure this is None cmd_args = ['git', 'blame', '-L %d,%d' % (first_line, last_line)] if '-w' in blame_options: @@ -453,12 +452,10 @@ def git_blame(errors, path, file, blame_options): cmd_args = cmd_args + ['--porcelain', '--incremental', '--', filename] try: - result = subprocess.check_output(cmd_args) + result = subprocess.check_output(cmd_args, cwd=path) result = result.decode(locale.getpreferredencoding()) except: return [] - finally: - os.chdir(cwd) if result.startswith('fatal'): return [] @@ -704,7 +701,6 @@ def main() -> None: parser.error('No report directory set.') # Get the directory where source code files are located. - cwd = os.getcwd() source_dir = os.getcwd() if options.source_dir: source_dir = options.source_dir @@ -990,7 +986,6 @@ def main() -> None: sys.stderr.write("\nConsider changing source-encoding (for example: \"htmlreport ... --source-encoding=\"iso8859-1\"\"\n") print('Creating style.css file') - os.chdir(cwd) # going back to the cwd to find style.css with io.open(os.path.join(options.report_dir, 'style.css'), 'w') as css_file: css_file.write(STYLE_FILE) diff --git a/tools/test-my-pr.py b/tools/test-my-pr.py index de897c09bd6..4a40f544ee1 100755 --- a/tools/test-my-pr.py +++ b/tools/test-my-pr.py @@ -52,7 +52,7 @@ def format_float(a, b=1): main_dir = os.path.join(work_path, 'tree-main') lib.set_jobs('-j' + str(args.j)) - result_file = os.path.join(work_path, args.o) + result_file = os.path.abspath(os.path.join(work_path, args.o)) (f, ext) = os.path.splitext(result_file) timing_file = f + '_timing' + ext your_repo_dir = os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))) @@ -75,8 +75,7 @@ def format_float(a, b=1): sys.exit(1) try: - os.chdir(your_repo_dir) - commit_id = (subprocess.check_output(['git', 'merge-base', 'origin/main', 'HEAD'])).strip().decode('ascii') + commit_id = (subprocess.check_output(['git', 'merge-base', 'origin/main', 'HEAD'], cwd=your_repo_dir)).strip().decode('ascii') with open(result_file, 'a') as myfile: myfile.write('Common ancestor: ' + commit_id + '\n\n') package_width = '140' @@ -85,7 +84,6 @@ def format_float(a, b=1): myfile.write('{:{package_width}} {:{timing_width}} {:{timing_width}} {:{timing_width}}\n'.format( 'Package', 'main', 'your', 'Factor', package_width=package_width, timing_width=timing_width)) - os.chdir(main_dir) subprocess.check_call(['git', 'fetch', '--depth=1', 'origin', commit_id]) subprocess.check_call(['git', 'checkout', '-f', commit_id]) except BaseException as e: