Make sure that containing directories exist before writing output files

We don't use os.makedirs(dir, exist_ok=True) as that is too strict: it fails if
the directory already exists with different permissions (e. g. with 775). Thus
introduce a helper function ensuredir().
master
Colin Watson 11 years ago committed by Martin Pitt
parent f32a8e37da
commit a2dcb26901

@ -208,6 +208,7 @@ from britney2.utils import (old_libraries_format, undo_changes,
create_provides_map, read_release_file,
read_sources_file, get_dependency_solvers,
invalidate_excuses, compile_nuninst,
ensuredir,
)
__author__ = 'Fabio Tranchitella and the Debian Release Team'
@ -1633,6 +1634,7 @@ class Britney(object):
if not self.options.dry_run:
self.log("> Writing Excuses to %s" % self.options.excuses_output, type="I")
sorted_excuses = sorted(excuses.values(), key=lambda x: x.sortkey())
ensuredir(os.path.dirname(self.options.excuses_output))
write_excuses(sorted_excuses, self.options.excuses_output,
output_format="legacy-html")
if hasattr(self.options, 'excuses_yaml_output'):
@ -2742,6 +2744,7 @@ class Britney(object):
except AttributeError:
return
ensuredir(os.path.dirname(self.options.upgrade_output))
with open(self.options.upgrade_output, 'w', encoding='utf-8') as f:
self.__output = f

@ -8,6 +8,7 @@ from urllib.parse import quote
import apt_pkg
from britney2.hints import Hint, split_into_one_hint_per_package
from britney2.utils import ensuredir
@unique
@ -385,6 +386,7 @@ class AgePolicy(BasePolicy):
basename = 'Dates'
old_file = None
filename = os.path.join(directory, basename)
ensuredir(directory)
filename_tmp = os.path.join(directory, '%s_new' % basename)
with open(filename_tmp, 'w', encoding='utf-8') as fd:
for pkg in sorted(dates):

@ -346,6 +346,16 @@ def make_migrationitem(package, sources):
return MigrationItem("%s/%s" % (item.uvname, sources[item.suite][item.package].version))
def ensuredir(dir):
"""Create dir if it does not exist
os.makedirs(dir, exist_ok=True) is too strict as that will fail if the
direcotry already exists with different permissions.
"""
if not os.path.isdir(dir):
os.makedirs(dir)
def write_excuses(excuselist, dest_file, output_format="yaml"):
"""Write the excuses to dest_file
@ -354,6 +364,7 @@ def write_excuses(excuselist, dest_file, output_format="yaml"):
or "legacy-html".
"""
if output_format == "yaml":
ensuredir(os.path.dirname(dest_file))
with open(dest_file, 'w', encoding='utf-8') as f:
edatalist = [e.excusedata() for e in excuselist]
excusesdata = {
@ -362,6 +373,7 @@ def write_excuses(excuselist, dest_file, output_format="yaml"):
}
f.write(yaml.dump(excusesdata, default_flow_style=False, allow_unicode=True))
elif output_format == "legacy-html":
ensuredir(os.path.dirname(dest_file))
with open(dest_file, 'w', encoding='utf-8') as f:
f.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n")
f.write("<html><head><title>excuses...</title>")
@ -425,6 +437,7 @@ def write_controlfiles(sources, packages, suite, basedir):
(PROVIDES, 'Provides'), (CONFLICTS, 'Conflicts'),
(ESSENTIAL, 'Essential'))
ensuredir(basedir)
for arch in packages_s:
filename = os.path.join(basedir, 'Packages_%s' % arch)
binaries = packages_s[arch][0]

Loading…
Cancel
Save