Skip to content

Commit aed2471

Browse files
authored
ccpp_prebuild.py: remove execute function from common.py, clean files the Python way (#642)
User interface changes?: No Closes #641
1 parent d322103 commit aed2471

File tree

3 files changed

+9
-45
lines changed

3 files changed

+9
-45
lines changed

scripts/ccpp_prebuild.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import sys
1414

1515
# CCPP framework imports
16-
from common import encode_container, decode_container, decode_container_as_dict, execute
16+
from common import encode_container, decode_container, decode_container_as_dict
1717
from common import CCPP_STAGES, CCPP_INTERNAL_VARIABLES, CCPP_STATIC_API_MODULE, CCPP_INTERNAL_VARIABLE_DEFINITON_FILE
1818
from common import STANDARD_VARIABLE_TYPES, STANDARD_INTEGER_TYPE, CCPP_TYPE
1919
from common import SUITE_DEFINITION_FILENAME_PATTERN
@@ -157,9 +157,14 @@ def clean_files(config, namespace):
157157
os.path.join(config['static_api_dir'], static_api_file),
158158
config['static_api_sourcefile'],
159159
]
160-
# Not very pythonic, but the easiest way w/o importing another Python module
161-
cmd = 'rm -vf {0}'.format(' '.join(files_to_remove))
162-
execute(cmd)
160+
for f in files_to_remove:
161+
try:
162+
os.remove(f)
163+
except FileNotFoundError:
164+
pass
165+
except Exception as e:
166+
logging.error(f"Error removing {f}: {e}")
167+
success = False
163168
return success
164169

165170
def get_all_suites(suites_dir):

scripts/common.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,35 +78,6 @@
7878
# Maximum number of concurrent CCPP instances per MPI task
7979
CCPP_NUM_INSTANCES = 200
8080

81-
def execute(cmd, abort = True):
82-
"""Runs a local command in a shell. Waits for completion and
83-
returns status, stdout and stderr. If abort = True, abort in
84-
case an error occurs during the execution of the command."""
85-
86-
# Set debug to true if logging level is debug
87-
debug = logging.getLogger().getEffectiveLevel() == logging.DEBUG
88-
89-
logging.debug('Executing "{0}"'.format(cmd))
90-
p = subprocess.Popen(cmd, stdout = subprocess.PIPE,
91-
stderr = subprocess.PIPE, shell = True)
92-
(stdout, stderr) = p.communicate()
93-
status = p.returncode
94-
if debug:
95-
message = 'Execution of "{0}" returned with exit code {1}\n'.format(cmd, status)
96-
message += ' stdout: "{0}"\n'.format(stdout.decode(encoding='ascii', errors='ignore').rstrip('\n'))
97-
message += ' stderr: "{0}"'.format(stderr.decode(encoding='ascii', errors='ignore').rstrip('\n'))
98-
logging.debug(message)
99-
if not status == 0:
100-
message = 'Execution of command {0} failed, exit code {1}\n'.format(cmd, status)
101-
message += ' stdout: "{0}"\n'.format(stdout.decode(encoding='ascii', errors='ignore').rstrip('\n'))
102-
message += ' stderr: "{0}"'.format(stderr.decode(encoding='ascii', errors='ignore').rstrip('\n'))
103-
if abort:
104-
raise Exception(message)
105-
else:
106-
logging.error(message)
107-
return (status, stdout.decode(encoding='ascii', errors='ignore').rstrip('\n'),
108-
stderr.decode(encoding='ascii', errors='ignore').rstrip('\n'))
109-
11081
def split_var_name_and_array_reference(var_name):
11182
"""Split an expression like foo(:,a,1:ddt%ngas)
11283
into components foo and (:,a,1:ddt%ngas)."""

test/unit_tests/test_common.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,6 @@ class CommonTestCase(unittest.TestCase):
3434

3535
"""Tests functionality of functions in common.py"""
3636

37-
def test_execute(self):
38-
"""Test execute() function"""
39-
40-
# Input for successful test: ls command on this file
41-
self.assertEqual(common.execute(f"ls {TEST_FILE}"),(0,f"{TEST_FILE}",""))
42-
43-
# Input for failing test (no exception): exit 1 from a subshell
44-
self.assertEqual(common.execute(f"(exit 1)",abort=False),(1,"",f""))
45-
46-
# Input for failing test (raise exception): exit 1 from a subshell
47-
self.assertRaises(Exception,common.execute,f"(exit 1)",abort=True)
48-
4937
def test_split_var_name_and_array_reference(self):
5038
"""Test split_var_name_and_array_reference() function"""
5139

0 commit comments

Comments
 (0)