Skip to content

Commit

Permalink
utils: Use context managers for subprocesses and opening files (#4905)
Browse files Browse the repository at this point in the history
  • Loading branch information
echoix authored Jan 16, 2025
1 parent e5bedcf commit f958f45
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 48 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ ignore = [
"temporal/t.register/testsuite/test_t_register_raster.py" = ["FLY002"]
"temporal/t.register/testsuite/test_t_register_raster_file.py" = ["FLY002"]
"temporal/t.remove/t.remove.py" = ["SIM115"]
"utils/**.py" = ["SIM115"]
"utils/generate_release_notes.py" = ["PGH004"]
"utils/gitlog2changelog.py" = ["SIM115"]
"utils/thumbnails.py" = ["PTH208"]
"vector/v.fill.holes/examples.ipynb" = ["PTH201"]

Expand Down
6 changes: 1 addition & 5 deletions utils/create_python_init_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,11 @@ def main(path):
continue
modules.append(os.path.splitext(os.path.basename(f))[0])

fd = open(os.path.join(path, "__init__.py"), "w")
try:
with open(os.path.join(path, "__init__.py"), "w") as fd:
fd.write("all = [%s" % os.linesep)
for m in modules:
fd.write(" '%s',%s" % (m, os.linesep))
fd.write(" ]%s" % os.linesep)
finally:
fd.close()

return 0


Expand Down
23 changes: 11 additions & 12 deletions utils/g.html2man/g.html2man.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@ def fix(content):
def main():
# parse HTML
infile = sys.argv[1]
inf = open(infile)
p = HTMLParser(entities)
for n, line in enumerate(inf):
try:
p.feed(line)
except Exception as err:
sys.stderr.write(
"%s:%d:0: Error (%s): %s\n" % (infile, n + 1, repr(err), line)
)
sys.exit(1)
p.close()
inf.close()
with open(infile) as inf:
p = HTMLParser(entities)
for n, line in enumerate(inf):
try:
p.feed(line)
except Exception as err:
sys.stderr.write(
"%s:%d:0: Error (%s): %s\n" % (infile, n + 1, repr(err), line)
)
sys.exit(1)
p.close()

# generate groff
sf = StringIO()
Expand Down
18 changes: 8 additions & 10 deletions utils/md_isvalid.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@


def check_md(filename):
p = subprocess.Popen(["mdl", filename])
p.wait()
with subprocess.Popen(["mdl", filename]) as p:
p.wait()


def print_line():
Expand All @@ -27,20 +27,18 @@ def check_module(module):
print(module)
tmp_file = gs.tempfile()
with open(tmp_file, "w") as fp:
p = subprocess.Popen([module, "--md-description"], stdout=fp)
p.wait()

p = subprocess.Popen(
with subprocess.Popen([module, "--md-description"], stdout=fp) as p:
p.wait()
with subprocess.Popen(
[
"mdl",
"--style",
os.path.join(os.path.dirname(__file__), "mdl_style.rb"),
fp.name,
]
)
p.wait()

return p.returncode
) as p:
p.wait()
return p.returncode


if __name__ == "__main__":
Expand Down
11 changes: 5 additions & 6 deletions utils/mkrest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import re
import subprocess
from datetime import datetime
from pathlib import Path

pgm = sys.argv[1]
year = sys.argv[2] if len(sys.argv) > 1 else str(datetime.now().year)
Expand Down Expand Up @@ -47,10 +48,7 @@

def read_file(name):
try:
f = open(name, "rb")
s = f.read()
f.close()
return s
return Path(name).read_bytes()
except OSError:
return ""

Expand Down Expand Up @@ -79,8 +77,9 @@ def read_file(name):
sys.stdout.write(tmp_data)

arguments = ["pandoc", "-s", "-r", "html", src_file, "-w", "rst"]
process = subprocess.Popen(arguments, stdout=subprocess.PIPE)
html_text = process.communicate()[0]
with subprocess.Popen(arguments, stdout=subprocess.PIPE) as process:
html_text = process.communicate()[0]

if html_text:
for k, v in replacement.iteritems():
html_text = html_text.replace(k, v)
Expand Down
18 changes: 8 additions & 10 deletions utils/ppmrotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import os
import atexit
import array
from pathlib import Path

import grass.script as gs

tmp_img = None
Expand All @@ -36,10 +38,8 @@ def cleanup():

def read_ppm(src):
global width, height
text = Path(src).read_bytes()

fh = open(src, "rb")
text = fh.read()
fh.close()
i = 0
j = text.find("\n", i)
if text[i:j] != "P6":
Expand All @@ -62,10 +62,9 @@ def read_ppm(src):
def write_ppm(dst, data):
w = height
h = width
fh = open(dst, "wb")
fh.write("P6\n%d %d\n%d\n" % (w, h, 255))
data.tofile(fh)
fh.close()
with open(dst, "wb") as fh:
fh.write("P6\n%d %d\n%d\n" % (w, h, 255))
data.tofile(fh)


def rotate_ppm(srcd):
Expand All @@ -92,9 +91,8 @@ def ppmtopng(dst, src):
if gs.find_program("g.ppmtopng", "--help"):
gs.run_command("g.ppmtopng", input=src, output=dst, quiet=True)
elif gs.find_program("pnmtopng"):
fh = open(dst, "wb")
gs.call(["pnmtopng", src], stdout=fh)
fh.close()
with open(dst, "wb") as fh:
gs.call(["pnmtopng", src], stdout=fh)
elif gs.find_program("convert"):
gs.call(["convert", src, dst])
else:
Expand Down
7 changes: 3 additions & 4 deletions utils/thumbnails.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import os
import atexit
from pathlib import Path

import grass.script as gs


Expand All @@ -35,10 +37,7 @@ def cleanup():


def make_gradient(path):
fh = open(path)
text = fh.read()
fh.close()

text = Path(path).read_text()
lines = text.splitlines()
records = []
for line in lines:
Expand Down

0 comments on commit f958f45

Please sign in to comment.