Skip to content

Commit 4780cd2

Browse files
authored
reduced usage of os.chdir() in Python scripts (danmar#6510)
1 parent 542da04 commit 4780cd2

File tree

2 files changed

+5
-12
lines changed

2 files changed

+5
-12
lines changed

htmlreport/cppcheck-htmlreport

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,8 @@ def git_blame(errors, path, file, blame_options):
441441
full_path = os.path.join(path, file)
442442
path, filename = os.path.split(full_path)
443443

444-
cwd = os.getcwd()
445-
if path:
446-
os.chdir(path)
444+
if not path:
445+
path = None # make sure this is None
447446

448447
cmd_args = ['git', 'blame', '-L %d,%d' % (first_line, last_line)]
449448
if '-w' in blame_options:
@@ -453,12 +452,10 @@ def git_blame(errors, path, file, blame_options):
453452
cmd_args = cmd_args + ['--porcelain', '--incremental', '--', filename]
454453

455454
try:
456-
result = subprocess.check_output(cmd_args)
455+
result = subprocess.check_output(cmd_args, cwd=path)
457456
result = result.decode(locale.getpreferredencoding())
458457
except:
459458
return []
460-
finally:
461-
os.chdir(cwd)
462459

463460
if result.startswith('fatal'):
464461
return []
@@ -704,7 +701,6 @@ def main() -> None:
704701
parser.error('No report directory set.')
705702

706703
# Get the directory where source code files are located.
707-
cwd = os.getcwd()
708704
source_dir = os.getcwd()
709705
if options.source_dir:
710706
source_dir = options.source_dir
@@ -990,7 +986,6 @@ def main() -> None:
990986
sys.stderr.write("\nConsider changing source-encoding (for example: \"htmlreport ... --source-encoding=\"iso8859-1\"\"\n")
991987

992988
print('Creating style.css file')
993-
os.chdir(cwd) # going back to the cwd to find style.css
994989
with io.open(os.path.join(options.report_dir, 'style.css'), 'w') as css_file:
995990
css_file.write(STYLE_FILE)
996991

tools/test-my-pr.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def format_float(a, b=1):
5252
main_dir = os.path.join(work_path, 'tree-main')
5353

5454
lib.set_jobs('-j' + str(args.j))
55-
result_file = os.path.join(work_path, args.o)
55+
result_file = os.path.abspath(os.path.join(work_path, args.o))
5656
(f, ext) = os.path.splitext(result_file)
5757
timing_file = f + '_timing' + ext
5858
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):
7575
sys.exit(1)
7676

7777
try:
78-
os.chdir(your_repo_dir)
79-
commit_id = (subprocess.check_output(['git', 'merge-base', 'origin/main', 'HEAD'])).strip().decode('ascii')
78+
commit_id = (subprocess.check_output(['git', 'merge-base', 'origin/main', 'HEAD'], cwd=your_repo_dir)).strip().decode('ascii')
8079
with open(result_file, 'a') as myfile:
8180
myfile.write('Common ancestor: ' + commit_id + '\n\n')
8281
package_width = '140'
@@ -85,7 +84,6 @@ def format_float(a, b=1):
8584
myfile.write('{:{package_width}} {:{timing_width}} {:{timing_width}} {:{timing_width}}\n'.format(
8685
'Package', 'main', 'your', 'Factor', package_width=package_width, timing_width=timing_width))
8786

88-
os.chdir(main_dir)
8987
subprocess.check_call(['git', 'fetch', '--depth=1', 'origin', commit_id])
9088
subprocess.check_call(['git', 'checkout', '-f', commit_id])
9189
except BaseException as e:

0 commit comments

Comments
 (0)