Skip to content

Commit eb8f0fa

Browse files
committed
reduced usage of os.chdir() in Python scripts [skip ci]
1 parent 3199102 commit eb8f0fa

3 files changed

Lines changed: 8 additions & 22 deletions

File tree

htmlreport/cppcheck-htmlreport

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

425-
cwd = os.getcwd()
426-
if path:
427-
os.chdir(path)
425+
if not path:
426+
path = None # make sure this is None
428427

429428
cmd_args = ['git', 'blame', '-L %d,%d' % (first_line, last_line)]
430429
if '-w' in blame_options:
@@ -434,12 +433,10 @@ def git_blame(errors, path, file, blame_options):
434433
cmd_args = cmd_args + ['--porcelain', '--incremental', '--', filename]
435434

436435
try:
437-
result = subprocess.check_output(cmd_args)
436+
result = subprocess.check_output(cmd_args, cwd=path)
438437
result = result.decode(locale.getpreferredencoding())
439438
except:
440439
return []
441-
finally:
442-
os.chdir(cwd)
443440

444441
if result.startswith('fatal'):
445442
return []
@@ -659,7 +656,6 @@ def main() -> None:
659656
parser.error('No report directory set.')
660657

661658
# Get the directory where source code files are located.
662-
cwd = os.getcwd()
663659
source_dir = os.getcwd()
664660
if options.source_dir:
665661
source_dir = options.source_dir
@@ -918,7 +914,6 @@ def main() -> None:
918914
sys.stderr.write("\nConsider changing source-encoding (for example: \"htmlreport ... --source-encoding=\"iso8859-1\"\"\n")
919915

920916
print('Creating style.css file')
921-
os.chdir(cwd) # going back to the cwd to find style.css
922917
with io.open(os.path.join(options.report_dir, 'style.css'), 'w') as css_file:
923918
css_file.write(STYLE_FILE)
924919

test/cli/proj2_test.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ def create_compile_commands():
2323
with open('proj2/' + COMPILE_COMMANDS_JSON, 'wt') as f:
2424
f.write(json.dumps(j))
2525

26-
# Run Cppcheck from project path
27-
def cppcheck_local(args):
28-
cwd = os.getcwd()
29-
os.chdir('proj2')
30-
ret, stdout, stderr = cppcheck(args)
31-
os.chdir(cwd)
32-
return ret, stdout, stderr
3326

3427
def test_file_filter():
3528
ret, stdout, _ = cppcheck(['proj2/','--file-filter=proj2/a/*'])
@@ -43,7 +36,7 @@ def test_file_filter():
4336

4437
def test_local_path():
4538
create_compile_commands()
46-
ret, stdout, _ = cppcheck_local(['--project=compile_commands.json'])
39+
ret, stdout, _ = cppcheck(['--project=compile_commands.json'], cwd='proj2')
4740
file1 = os.path.join('a', 'a.c')
4841
file2 = os.path.join('b', 'b.c')
4942
assert ret == 0, stdout
@@ -52,13 +45,13 @@ def test_local_path():
5245

5346
def test_local_path_force():
5447
create_compile_commands()
55-
ret, stdout, _ = cppcheck_local(['--project=compile_commands.json', '--force'])
48+
ret, stdout, _ = cppcheck(['--project=compile_commands.json', '--force'], cwd='proj2')
5649
assert ret == 0, stdout
5750
assert stdout.find('AAA') >= 0
5851

5952
def test_local_path_maxconfigs():
6053
create_compile_commands()
61-
ret, stdout, _ = cppcheck_local(['--project=compile_commands.json', '--max-configs=2'])
54+
ret, stdout, _ = cppcheck(['--project=compile_commands.json', '--max-configs=2'], cwd='proj2')
6255
assert ret == 0, stdout
6356
assert stdout.find('AAA') >= 0
6457

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)