Skip to content

Commit b429bf1

Browse files
fix: close script file handles with context managers (#2153)
Signed-off-by: Acuspeedster <arnavrajsingh@gmail.com> Co-authored-by: Lou DeGenaro <lou.degenaro@gmail.com>
1 parent 589ede3 commit b429bf1

File tree

4 files changed

+22
-27
lines changed

4 files changed

+22
-27
lines changed

scripts/schema_integrity.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ def _run(self, args: argparse.Namespace) -> int:
4141
dir_of_schemas = pathlib.Path(args.directory)
4242
for path_of_schema in dir_of_schemas.glob('oscal_*_schema.json'):
4343
self.out(f'Examinining: {path_of_schema.name}')
44-
fp = path_of_schema.open('r', encoding='utf8')
45-
json_schema_raw = json.load(fp)
46-
fp.close()
44+
with path_of_schema.open('r', encoding='utf8') as fp:
45+
json_schema_raw = json.load(fp)
4746
# get list of ID's
4847
# Cannot use this technique using latest OSCAL code drop
4948
# ids = list(set(self.recursive_ref('$id', json_schema_raw))) # noqa: E800

scripts/tanium_transform_script.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
sample_data_f = pathlib.Path('small_sample_tanium.json')
2828

2929
if __name__ == '__main__':
30-
stringed = sample_data_f.open('r', encoding=const.FILE_ENCODING).read()
30+
with sample_data_f.open('r', encoding=const.FILE_ENCODING) as f:
31+
stringed = f.read()
3132
tanium_tf = transformer_factory.get('tanium')
3233
output_oscal = tanium_tf.transform(stringed)
3334
json_str = output_oscal.oscal_serialize_json()

scripts/utf8me.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,10 @@ def _run(self, args: argparse.Namespace) -> int:
4848
input_path = pathlib.Path(args.input[0])
4949
if not input_path.is_file():
5050
logger.error('Input file does not exist or is a directory.')
51-
fh = codecs.open(str(input_path.resolve()), mode='r', encoding='utf8', errors='replace')
52-
content = fh.read()
53-
# Force flushing incase we are writing over the file)
54-
fh.flush()
55-
fh.close()
56-
output_file = pathlib.Path(outfile).resolve().open('w', encoding='utf8')
57-
output_file.write(content)
58-
output_file.flush()
59-
output_file.close()
51+
with codecs.open(str(input_path.resolve()), mode='r', encoding='utf8', errors='replace') as fh:
52+
content = fh.read()
53+
with pathlib.Path(outfile).resolve().open('w', encoding='utf8') as output_file:
54+
output_file.write(content)
6055
return CmdReturnCodes.SUCCESS.value
6156

6257

scripts/website_automation.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@ def write_module_doc_metafile(dump_location: pathlib.Path, module_name: str) ->
3333
"""Create a markdown file which allows mkdocstrings to index an individual module."""
3434
write_file = dump_location / (module_name.replace('.', '/') + '.md')
3535
write_file.parent.mkdir(parents=True, exist_ok=True)
36-
fh = write_file.open('w', encoding='utf8')
37-
# yaml header
38-
fh.write('---\n')
39-
fh.write(f'title: {module_name}\n')
40-
fh.write(f'description: Documentation for {module_name} module\n')
41-
fh.write('---\n\n')
42-
fh.write(f'::: {module_name}\n') # noqa: E231
43-
fh.write('handler: python\n')
44-
fh.close()
36+
with write_file.open('w', encoding='utf8') as fh:
37+
# yaml header
38+
fh.write('---\n')
39+
fh.write(f'title: {module_name}\n')
40+
fh.write(f'description: Documentation for {module_name} module\n')
41+
fh.write('---\n\n')
42+
fh.write(f'::: {module_name}\n') # noqa: E231
43+
fh.write('handler: python\n')
4544

4645

4746
def get_key(var: Dict[str, Any]) -> str:
@@ -72,11 +71,12 @@ def create_module_markdowns(base_path: pathlib.Path, base_module: str, dump_loca
7271
def md_txt(original: pathlib.Path, md_ed_license: pathlib.Path):
7372
"""Convert the apache license into a markdown file by wrapping the content in a text escape block."""
7473
assert original.exists()
75-
orig_data = original.open('r', encoding='utf8').read()
76-
md_fh = md_ed_license.open('w', encoding='utf8')
77-
md_fh.write('```text\n')
78-
md_fh.write(orig_data)
79-
md_fh.write('\n```\n')
74+
with original.open('r', encoding='utf8') as orig_fh:
75+
orig_data = orig_fh.read()
76+
with md_ed_license.open('w', encoding='utf8') as md_fh:
77+
md_fh.write('```text\n')
78+
md_fh.write(orig_data)
79+
md_fh.write('\n```\n')
8080

8181

8282
def cleanup_directory(dump_location: pathlib.Path):

0 commit comments

Comments
 (0)