Skip to content

Commit bfc5636

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

4 files changed

Lines changed: 13 additions & 34 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/helloworld_test.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@
99

1010
from testutils import create_gui_project_file, cppcheck
1111

12-
# Run Cppcheck from project path
13-
def cppcheck_local(args):
14-
cwd = os.getcwd()
15-
os.chdir('helloworld')
16-
ret, stdout, stderr = cppcheck(args)
17-
os.chdir(cwd)
18-
return ret, stdout, stderr
1912

2013
def getRelativeProjectPath():
2114
return 'helloworld'
@@ -46,7 +39,7 @@ def test_relative_path():
4639

4740

4841
def test_local_path():
49-
ret, stdout, stderr = cppcheck_local(['--template=cppcheck1', '.'])
42+
ret, stdout, stderr = cppcheck(['--template=cppcheck1', '.'], cwd='helloworld')
5043
assert ret == 0, stdout
5144
assert stderr == '[main.c:5]: (error) Division by zero.\n'
5245

@@ -58,13 +51,13 @@ def test_absolute_path():
5851
assert stderr == '[%s:5]: (error) Division by zero.\n' % filename
5952

6053
def test_addon_local_path():
61-
ret, stdout, stderr = cppcheck_local(['--addon=misra', '--enable=style', '--template=cppcheck1', '.'])
54+
ret, stdout, stderr = cppcheck(['--addon=misra', '--enable=style', '--template=cppcheck1', '.'], cwd='helloworld')
6255
assert ret == 0, stdout
6356
assert stderr == ('[main.c:5]: (error) Division by zero.\n'
6457
'[main.c:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n')
6558

6659
def test_addon_local_path_not_enable():
67-
ret, stdout, stderr = cppcheck_local(['--addon=misra', '--template=cppcheck1', '.'])
60+
ret, stdout, stderr = cppcheck(['--addon=misra', '--template=cppcheck1', '.'], cwd='helloworld')
6861
assert ret == 0, stdout
6962
assert stderr == '[main.c:5]: (error) Division by zero.\n'
7063

@@ -109,7 +102,7 @@ def test_basepath_absolute_path():
109102
assert stderr == '[main.c:5]: (error) Division by zero.\n'
110103

111104
def test_vs_project_local_path():
112-
ret, stdout, stderr = cppcheck_local(['--template=cppcheck1', '--project=helloworld.vcxproj'])
105+
ret, stdout, stderr = cppcheck(['--template=cppcheck1', '--project=helloworld.vcxproj'], cwd='helloworld')
113106
assert ret == 0, stdout
114107
assert getVsConfigs(stdout, 'main.c') == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
115108
assert stderr == '[main.c:5]: (error) Division by zero.\n'
@@ -131,7 +124,7 @@ def test_vs_project_absolute_path():
131124
assert stderr == '[%s:5]: (error) Division by zero.\n' % filename
132125

133126
def test_cppcheck_project_local_path():
134-
ret, stdout, stderr = cppcheck_local(['--template=cppcheck1', '--platform=win64', '--project=helloworld.cppcheck'])
127+
ret, stdout, stderr = cppcheck(['--template=cppcheck1', '--platform=win64', '--project=helloworld.cppcheck'], cwd='helloworld')
135128
assert ret == 0, stdout
136129
assert getVsConfigs(stdout, 'main.c') == 'Debug|x64'
137130
assert stderr == '[main.c:5]: (error) Division by zero.\n'

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, stderr = 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, stderr = cppcheck_local(['--project=compile_commands.json'])
39+
ret, stdout, stderr = 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, stderr = cppcheck_local(['--project=compile_commands.json', '--force'])
48+
ret, stdout, stderr = 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, stderr = cppcheck_local(['--project=compile_commands.json', '--max-configs=2'])
54+
ret, stdout, stderr = 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])))
@@ -77,8 +77,7 @@ def format_float(a, b=1):
7777
sys.exit(1)
7878

7979
try:
80-
os.chdir(your_repo_dir)
81-
commit_id = (subprocess.check_output(['git', 'merge-base', 'origin/main', 'HEAD'])).strip().decode('ascii')
80+
commit_id = (subprocess.check_output(['git', 'merge-base', 'origin/main', 'HEAD'], cwd=your_repo_dir)).strip().decode('ascii')
8281
with open(result_file, 'a') as myfile:
8382
myfile.write('Common ancestor: ' + commit_id + '\n\n')
8483
package_width = '140'
@@ -87,7 +86,6 @@ def format_float(a, b=1):
8786
myfile.write('{:{package_width}} {:{timing_width}} {:{timing_width}} {:{timing_width}}\n'.format(
8887
'Package', 'main', 'your', 'Factor', package_width=package_width, timing_width=timing_width))
8988

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

0 commit comments

Comments
 (0)