[Piuparts-commits] [piuparts] 04/07: *.py: consistently name and use file I/O helpers
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 77c2c075c5a5438bdbd8d239d204eac13361a548
Author: Andreas Beckmann <anbe at debian.org>
Date: Mon Sep 11 14:13:13 2017 +0200
*.py: consistently name and use file I/O helpers
Signed-off-by: Andreas Beckmann <anbe at debian.org>
Signed-off-by: Holger Levsen <holger at layer-acht.org>
---
piuparts-report.py | 33 ++++++++++++++++-----------------
piuparts.py | 27 ++++++++++++---------------
piupartslib/packagesdb.py | 3 ---
tests/unittests.py | 5 -----
4 files changed, 28 insertions(+), 40 deletions(-)
diff --git a/piuparts-report.py b/piuparts-report.py
index 19e8a61..1bd5486 100644
--- a/piuparts-report.py
+++ b/piuparts-report.py
@@ -662,7 +662,7 @@ def remove_old_logs(logs_by_dir, output_dir):
os.remove(os.path.join(fulldir, basename))
-def write_file(filename, contents):
+def create_file(filename, contents):
f = file(filename, "w")
f.write(contents)
f.close()
@@ -675,6 +675,11 @@ def append_file(filename, contents):
def read_file(filename):
+ with open(filename, "r") as f:
+ return f.read()
+
+
+def readlines_file(filename):
f = file(filename, "r")
l = f.readlines()
f.close()
@@ -735,7 +740,7 @@ def write_template_html(filename, body, mapping={}, defer_if_unmodified=False, m
"piuparts_version": PIUPARTS_VERSION,
"time": time.strftime("%Y-%m-%d %H:%M %Z"),
})
- write_file(filename, htmlpage.safe_substitute(mapping))
+ create_file(filename, htmlpage.safe_substitute(mapping))
if md5cache is not None:
md5cache['written'] += 1
@@ -1034,10 +1039,10 @@ class Section:
countsfile = os.path.join(self._section_directory, "counts.txt")
if not os.path.isfile(countsfile):
logging.debug("writing new file: %s" % countsfile)
- write_file(countsfile, header)
+ create_file(countsfile, header)
last_line = ""
else:
- last_line = read_file(countsfile)[-1]
+ last_line = readlines_file(countsfile)[-1]
if not current_day in last_line:
append_file(countsfile, counts)
logging.debug("appending line: %s" % counts.strip())
@@ -1264,8 +1269,8 @@ class Section:
maintainers[email] = []
maintainers[email].append(source)
- write_file(os.path.join(self._output_directory, "sources.txt"), sources)
- write_file(os.path.join(self._output_directory, "sources.yaml"),
+ create_file(os.path.join(self._output_directory, "sources.txt"), sources)
+ create_file(os.path.join(self._output_directory, "sources.yaml"),
yaml.dump(sources_data, default_flow_style=False))
self.create_maintainer_summaries(maintainers, source_binary_rows)
@@ -1319,9 +1324,7 @@ class Section:
tpl = os.path.join(self._output_directory, template)
try:
- f = file(tpl, "r")
- rows = file.read(f)
- f.close()
+ rows = read_file(tpl)
os.unlink(tpl)
self._write_template_html(
@@ -1652,13 +1655,9 @@ def generate_global_summary(dir, sections):
def get_bug_text(logpath):
bugpath = replace_ext(logpath, BUG_EXT)
- txt = ""
if os.path.exists(bugpath):
- bf = open(bugpath, 'r')
- txt = bf.read()
- bf.close()
-
- return txt
+ return read_file(bugpath)
+ return ""
def populate_tpl(tmpl, vals):
@@ -1773,7 +1772,7 @@ def main():
if os.path.exists(master_directory):
packagedb_cache = {}
- write_file(os.path.join(output_directory, "sections.yaml"),
+ create_file(os.path.join(output_directory, "sections.yaml"),
yaml.dump(section_names, default_flow_style=False))
for section_name in process_section_names:
try:
@@ -1788,7 +1787,7 @@ def main():
logging.debug("Writing static pages")
for page in ("index", "bug_howto"):
tpl = os.path.join(output_directory, page + ".tpl")
- INDEX_BODY = "".join(read_file(tpl))
+ INDEX_BODY = read_file(tpl)
write_template_html(
os.path.join(output_directory, page + ".html"),
INDEX_BODY,
diff --git a/piuparts.py b/piuparts.py
index 5204193..249ebc9 100644
--- a/piuparts.py
+++ b/piuparts.py
@@ -599,17 +599,22 @@ def create_temp_file():
return (fd, path)
-def create_file(name, contents):
+def create_file(filename, contents):
"""Create a new file with the desired name and contents."""
try:
- f = file(name, "w")
+ f = file(filename, "w")
f.write(contents)
f.close()
except IOError as detail:
- logging.error("Couldn't create file %s: %s" % (name, detail))
+ logging.error("Couldn't create file %s: %s" % (filename, detail))
panic()
+def readlines_file(filename):
+ with file(filename, "r") as f:
+ return f.readlines()
+
+
def remove_files(filenames):
"""Remove some files."""
for filename in filenames:
@@ -1610,14 +1615,12 @@ class Chroot:
for basename in os.listdir(vdir):
if basename.endswith(".list"):
pkg = basename[:-len(".list")]
- f = file(os.path.join(vdir, basename), "r")
- for line in f:
+ for line in readlines_file(os.path.join(vdir, basename)):
pathname = line.strip()
if pathname in vdict:
vdict[pathname].append(pkg)
else:
vdict[pathname] = [pkg]
- f.close()
return vdict
def check_for_no_processes(self, fail=None):
@@ -1780,8 +1783,7 @@ class Chroot:
if not os.path.exists(os.path.join(vdir, basename)):
continue
- f = file(os.path.join(vdir, basename), "r")
- for line in f:
+ for line in readlines_file(os.path.join(vdir, basename)):
pathname = line.strip()
if pathname.startswith("/etc/cron."):
if os.path.isfile(self.relative(pathname.strip("/"))):
@@ -1793,7 +1795,6 @@ class Chroot:
has_cronfiles = True
vlist.append(pathname)
logging.info("Package " + p + " contains cron file: " + pathname)
- f.close()
return has_cronfiles, vlist
@@ -1827,8 +1828,7 @@ class Chroot:
if not os.path.exists(os.path.join(vdir, basename)):
continue
- f = file(os.path.join(vdir, basename), "r")
- for line in f:
+ for line in readlines_file(os.path.join(vdir, basename)):
pathname = line.strip()
if pathname.startswith("/etc/logrotate.d/"):
if os.path.isfile(self.relative(pathname.strip("/"))):
@@ -1836,7 +1836,6 @@ class Chroot:
has_logrotatefiles = True
vlist.append(pathname)
logging.info("Package " + p + " contains logrotate file: " + pathname)
- f.close()
return has_logrotatefiles, vlist
@@ -2861,14 +2860,12 @@ def find_default_debian_mirrors():
"""Find the default Debian mirrors."""
mirrors = []
try:
- f = file("/etc/apt/sources.list", "r")
- for line in f:
+ for line in readlines_file("/etc/apt/sources.list"):
line = re.sub('\[arch=.*\]', '', line)
parts = line.split()
if len(parts) > 2 and parts[0] == "deb":
mirrors.append((parts[1], parts[3:]))
break # Only use the first one, at least for now.
- f.close()
except IOError:
return None
return mirrors
diff --git a/piupartslib/packagesdb.py b/piupartslib/packagesdb.py
index ea55420..a0c3d20 100644
--- a/piupartslib/packagesdb.py
+++ b/piupartslib/packagesdb.py
@@ -193,9 +193,6 @@ class LogDB:
if basename.endswith(".log"):
cache[os.path.join(dirname, basename)] = True
- def open_file(self, pathname, mode):
- return file(pathname, mode)
-
def remove_file(self, pathname):
os.remove(pathname)
diff --git a/tests/unittests.py b/tests/unittests.py
index ab65804..b18dce8 100644
--- a/tests/unittests.py
+++ b/tests/unittests.py
@@ -37,11 +37,6 @@ class FakeLogDB(piupartslib.packagesdb.LogDB):
vdir, base = self._parse(pathname)
return base in self.dict[vdir]
- def open_file(self, pathname, mode):
- vdir, base = self._parse(pathname)
- self.dict[vdir].append(base)
- return StringIO.StringIO()
-
def remove_file(self, pathname):
vdir, base = self._parse(pathname)
if base in self.dict[vdir]:
--
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