Skip to content

Commit

Permalink
Merge pull request #9 from zopefoundation/close-files-immediately
Browse files Browse the repository at this point in the history
Replace file reads and writes utilizing Path (fixes #8)
  • Loading branch information
jan-jockusch committed Nov 29, 2023
2 parents 9406c9c + 161f007 commit 727c26c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Expand Up @@ -4,7 +4,7 @@ Changes
3.1 (unreleased)
================

- Nothing changed yet.
- Modify read/write operations to use Path() to avoid dangling file handles


3.0 (2023-02-07)
Expand Down
3 changes: 2 additions & 1 deletion setup.py
@@ -1,11 +1,12 @@
import os
from pathlib import Path

from setuptools import find_packages
from setuptools import setup


def read(*rnames):
return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
return Path(os.path.join(os.path.dirname(__file__), *rnames)).read_text()


long_description = (
Expand Down
12 changes: 7 additions & 5 deletions src/zc/zodbrecipes/__init__.py
Expand Up @@ -15,6 +15,7 @@
import logging
import os
from io import StringIO
from pathlib import Path

import zc.buildout
import zc.recipe.egg
Expand Down Expand Up @@ -106,7 +107,7 @@ def install(self):

logrotate = options['logrotate']
if logrotate:
open(logrotate, 'w').write(logrotate_template % dict(
Path(logrotate).write_text(logrotate_template % dict(
logfile=event_log_path,
rc=os.path.join(options['rc-directory'], rc),
conf=zdaemon_conf_path,
Expand Down Expand Up @@ -202,8 +203,8 @@ def install(self):
self.egg.install()
requirements, ws = self.egg.working_set()

open(zeo_conf_path, 'w').write(str(zeo_conf))
open(zdaemon_conf_path, 'w').write(str(zdaemon_conf))
Path(zeo_conf_path).write_text(str(zeo_conf))
Path(zdaemon_conf_path).write_text(str(zdaemon_conf))

if options.get('shell-script') == 'true':
if not os.path.exists(options['zdaemon']):
Expand All @@ -221,8 +222,9 @@ def install(self):
contents = "#!/bin/sh\n%s\n" % contents

dest = os.path.join(options['rc-directory'], rc)
if not (os.path.exists(dest) and open(dest).read() == contents):
open(dest, 'w').write(contents)
if not (os.path.exists(dest) and
Path(dest).read_text() == contents):
Path(dest).write_text(contents)
os.chmod(dest, 0o755)
logger.info("Generated shell script %r.", dest)

Expand Down

0 comments on commit 727c26c

Please sign in to comment.