[Piuparts-commits] [piuparts] 05/07: *.py: consistently use 'with open(...) as ...:' for scoped open files
Holger Levsen
holger at layer-acht.org
Tue Sep 12 11:59:20 UTC 2017
This is an automated email from the git hooks/post-receive script.
holger pushed a commit to branch develop
in repository piuparts.
commit b52e3ac73618c94e48336a75c094d97f55e09d3b
Author: Andreas Beckmann <anbe at debian.org>
Date: Mon Sep 11 14:20:53 2017 +0200
*.py: consistently use 'with open(...) as ...:' for scoped open files
Signed-off-by: Andreas Beckmann <anbe at debian.org>
Signed-off-by: Holger Levsen <holger at layer-acht.org>
---
piuparts-analyze.py | 29 ++++++++++++-----------------
piuparts-report.py | 16 ++++++----------
piuparts-slave.py | 18 ++++++++----------
piuparts.py | 33 +++++++++++++--------------------
piupartslib/dwke.py | 22 ++++++++--------------
5 files changed, 47 insertions(+), 71 deletions(-)
diff --git a/piuparts-analyze.py b/piuparts-analyze.py
index f027676..1e1d143 100644
--- a/piuparts-analyze.py
+++ b/piuparts-analyze.py
@@ -80,9 +80,8 @@ def package_source_version(log):
def extract_errors(log):
"""This pretty stupid implementation is basically just 'grep -i error', and then
removing the timestamps and the name of the chroot and the package version itself."""
- f = open(log)
- data = f.read()
- f.close()
+ with open(log, "r") as f:
+ data = f.read()
whole = ''
pversion = package_version(log)
for match in error_pattern.finditer(data):
@@ -100,9 +99,8 @@ def extract_errors(log):
def extract_headers(log):
- f = open(log)
- data = f.read()
- f.close()
+ with open(log, "r") as f:
+ data = f.read()
headers = []
headers = data.partition("\nExecuting:")[0]
if headers and not headers.endswith("\n"):
@@ -111,13 +109,11 @@ def extract_headers(log):
def prepend_to_file(filename, data):
- f = file(filename, "r")
- old_data = f.read()
- f.close()
- f = file(filename + ".tmp", "w")
- f.write(data)
- f.write(old_data)
- f.close()
+ with open(filename, "r") as f:
+ old_data = f.read()
+ with open(filename + ".tmp", "w") as f:
+ f.write(data)
+ f.write(old_data)
shutil.copymode(filename, filename + ".tmp")
@@ -143,10 +139,9 @@ def get_bug_versions(bug):
def write_bug_file(failed_log, bugs):
if bugs:
- f = file(os.path.splitext(failed_log)[0] + '.bug', "w")
- for bug in bugs:
- f.write('<a href="https://bugs.debian.org/%s" target="_blank">#%s</a>\n' % (bug, bug))
- f.close()
+ with open(os.path.splitext(failed_log)[0] + '.bug', "w") as f:
+ for bug in bugs:
+ f.write('<a href="https://bugs.debian.org/%s" target="_blank">#%s</a>\n' % (bug, bug))
def move_to_bugged(failed_log, bugged="bugged", bug=None):
diff --git a/piuparts-report.py b/piuparts-report.py
index 1bd5486..9898121 100644
--- a/piuparts-report.py
+++ b/piuparts-report.py
@@ -663,15 +663,13 @@ def remove_old_logs(logs_by_dir, output_dir):
def create_file(filename, contents):
- f = file(filename, "w")
- f.write(contents)
- f.close()
+ with open(filename, "w") as f:
+ f.write(contents)
def append_file(filename, contents):
- f = file(filename, "a")
- f.write(contents)
- f.close()
+ with open(filename, "a") as f:
+ f.write(contents)
def read_file(filename):
@@ -680,10 +678,8 @@ def read_file(filename):
def readlines_file(filename):
- f = file(filename, "r")
- l = f.readlines()
- f.close()
- return l
+ with open(filename, "r") as f:
+ return f.readlines()
def fileage(filename):
diff --git a/piuparts-slave.py b/piuparts-slave.py
index b596036..e44a7d9 100644
--- a/piuparts-slave.py
+++ b/piuparts-slave.py
@@ -260,12 +260,11 @@ class Slave:
package, rest = basename.split("_", 1)
version = rest[:-len(".log")]
self._writeline(pass_or_fail, package, version)
- f = file(filename, "r")
- for line in f:
- if line.endswith("\n"):
- line = line[:-1]
- self._writeline(" " + line)
- f.close()
+ with open(filename, "r") as f:
+ for line in f:
+ if line.endswith("\n"):
+ line = line[:-1]
+ self._writeline(" " + line)
self._writeline(".")
line = self._readline()
if line != "ok\n":
@@ -671,7 +670,7 @@ class Section:
output_name = log_name(pname, pvers)
logging.debug("Opening log file %s" % output_name)
new_name = os.path.join("new", output_name)
- output = file(new_name, "we")
+ output = open(new_name, "we")
output.write(time.strftime("Start: %Y-%m-%d %H:%M:%S %Z\n",
time.gmtime()))
@@ -926,9 +925,8 @@ def create_chroot(config, tarball, distro):
def create_file(filename, contents):
- f = file(filename, "w")
- f.write(contents)
- f.close()
+ with open(filename, "w") as f:
+ f.write(contents)
def main():
diff --git a/piuparts.py b/piuparts.py
index 249ebc9..803a046 100644
--- a/piuparts.py
+++ b/piuparts.py
@@ -602,16 +602,15 @@ def create_temp_file():
def create_file(filename, contents):
"""Create a new file with the desired name and contents."""
try:
- f = file(filename, "w")
- f.write(contents)
- f.close()
+ with open(filename, "w") as f:
+ f.write(contents)
except IOError as detail:
logging.error("Couldn't create file %s: %s" % (filename, detail))
panic()
def readlines_file(filename):
- with file(filename, "r") as f:
+ with open(filename, "r") as f:
return f.readlines()
@@ -1925,9 +1924,8 @@ class VirtServ(Chroot):
try:
(_, tf) = create_temp_file()
self._command(['copyup', (filename,), (tf,)])
- f = file(tf)
- d = f.read()
- f.close()
+ with open(tf, 'r') as f:
+ d = f.read()
finally:
os.remove(tf)
return d
@@ -2058,9 +2056,8 @@ class VirtServ(Chroot):
path = self._tbpath(path)
try:
(_, tf) = create_temp_file()
- f = file(tf, 'w')
- f.write(tf)
- f.close()
+ with open(tf, 'w') as f:
+ f.write(data)
self._command(['copydown', (tf,), (path,)])
finally:
os.remove(tf)
@@ -2399,9 +2396,8 @@ def process_changes(changes):
\n([^ ]|$) # Start of a new field or EOF
''',
re.MULTILINE | re.DOTALL | re.VERBOSE)
- f = open(changes_path)
- file_text = f.read()
- f.close()
+ with open(changes_path, "r") as f:
+ file_text = f.read()
matches = pattern.split(file_text)
# Append all the packages found in the changes file to a package list.
@@ -2680,18 +2676,15 @@ def install_upgrade_test(chroot, chroot_state, package_files, packages, old_pack
def save_meta_data(filename, chroot_state):
"""Save directory tree meta data into a file for fast access later."""
logging.debug("Saving chroot meta data to %s" % filename)
- f = file(filename, "w")
- pickle.dump(chroot_state, f)
- f.close()
+ with open(filename, "w") as f:
+ pickle.dump(chroot_state, f)
def load_meta_data(filename):
"""Load meta data saved by 'save_meta_data'."""
logging.debug("Loading chroot meta data from %s" % filename)
- f = file(filename, "r")
- chroot_state = pickle.load(f)
- f.close()
- return chroot_state
+ with open(filename, "r") as f:
+ return pickle.load(f)
def install_and_upgrade_between_distros(package_files, packages_qualified):
diff --git a/piupartslib/dwke.py b/piupartslib/dwke.py
index 3217ee1..d5c96db 100644
--- a/piupartslib/dwke.py
+++ b/piupartslib/dwke.py
@@ -70,9 +70,8 @@ class Problem():
def init_problem(self):
"""Load problem file parameters (HELPTEXT="foo" -> self.HELPTEXT)"""
- pb = open(self.probpath, 'r')
- probbody = pb.read()
- pb.close()
+ with open(self.probpath, 'r') as pb:
+ probbody = pb.read()
tagged = re.sub("^([A-Z_]+=)", "<hdr>\g<0>", probbody, 0, re.MULTILINE)
@@ -136,14 +135,10 @@ class FailureManager():
for pkgspec in self.logdict:
logpath = self.logdict[pkgspec]
try:
- kp = open(get_kpr_path(logpath), 'r')
-
- for line in kp.readlines():
- (where, problem) = self.parse_kpr_line(line)
-
- self.failures.append(make_failure(where, problem, pkgspec))
-
- kp.close()
+ with open(get_kpr_path(logpath), 'r') as kp:
+ for line in kp:
+ (where, problem) = self.parse_kpr_line(line)
+ self.failures.append(make_failure(where, problem, pkgspec))
except IOError:
logging.error("Error processing %s" % get_kpr_path(logpath))
@@ -258,9 +253,8 @@ def make_kprs(logdict, kprdict, problem_list):
logpath = logdict[pkg_spec]
try:
- lb = open(logpath, 'r')
- logbody = lb.read()
- lb.close()
+ with open(logpath, 'r') as lb:
+ logbody = lb.read()
where = get_where(logpath)
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/piuparts/piuparts.git
More information about the Piuparts-commits
mailing list