@@ -12,7 +12,6 @@
# See the COPYING file in the top-level directory.
from contextlib import contextmanager
-import errno
import os
import re
from typing import (
@@ -65,21 +64,19 @@ def write(self, output_dir: str) -> None:
return
pathname = os.path.join(output_dir, self.fname)
odir = os.path.dirname(pathname)
+
if odir:
- try:
- os.makedirs(odir)
- except os.error as e:
- if e.errno != errno.EEXIST:
- raise
+ os.makedirs(odir, exist_ok=True)
+
+ # use os.open for O_CREAT to create and read a non-existant file
fd = os.open(pathname, os.O_RDWR | os.O_CREAT, 0o666)
- f = open(fd, 'r+', encoding='utf-8')
- text = self.get_content()
- oldtext = f.read(len(text) + 1)
- if text != oldtext:
- f.seek(0)
- f.truncate(0)
- f.write(text)
- f.close()
+ with os.fdopen(fd, 'r+', encoding='utf-8') as fp:
+ text = self.get_content()
+ oldtext = fp.read(len(text) + 1)
+ if text != oldtext:
+ fp.seek(0)
+ fp.truncate(0)
+ fp.write(text)
def _wrap_ifcond(ifcond: List[str], before: str, after: str) -> str: