Bug 1916786 - Modernize python/mozbuild/mozbuild to use exist_ok=True parameter from os.makedirs r=ahochheiden

Differential Revision: https://phabricator.services.mozilla.com/D221070
This commit is contained in:
serge-sans-paille 2024-09-05 05:52:56 +00:00
parent 5bc16ac9c3
commit 384a41d933
10 changed files with 16 additions and 73 deletions

View File

@ -3,7 +3,6 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
import codecs import codecs
import errno
import io import io
import itertools import itertools
import logging import logging
@ -154,12 +153,7 @@ def main(argv):
js_config = config.copy() js_config = config.copy()
pwd = os.getcwd() pwd = os.getcwd()
try: try:
try: os.makedirs("js/src", exist_ok=True)
os.makedirs("js/src")
except OSError as e:
if e.errno != errno.EEXIST:
raise
os.chdir("js/src") os.chdir("js/src")
js_config["OLD_CONFIGURE_SUBSTS"] = old_js_configure_substs js_config["OLD_CONFIGURE_SUBSTS"] = old_js_configure_substs
js_config["OLD_CONFIGURE_DEFINES"] = old_js_configure_defines js_config["OLD_CONFIGURE_DEFINES"] = old_js_configure_defines

View File

@ -2,7 +2,6 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this # License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
import errno
import io import io
import itertools import itertools
import os import os
@ -284,11 +283,7 @@ class BuildBackend(LoggingMixin):
assert fh is not None assert fh is not None
dirname = mozpath.dirname(fh.name) dirname = mozpath.dirname(fh.name)
try: os.makedirs(dirname, exist_ok=True)
os.makedirs(dirname)
except OSError as error:
if error.errno != errno.EEXIST:
raise
yield fh yield fh

View File

@ -118,11 +118,7 @@ class CppEclipseBackend(CommonBackend):
workspace_settings_dir, workspace_settings_dir,
self._workspace_lang_dir, self._workspace_lang_dir,
]: ]:
try: os.makedirs(dir_name, exist_ok=True)
os.makedirs(dir_name)
except OSError as e:
if e.errno != errno.EEXIST:
raise
project_path = os.path.join(self._project_dir, ".project") project_path = os.path.join(self._project_dir, ".project")
with open(project_path, "w") as fh: with open(project_path, "w") as fh:

View File

@ -4,7 +4,6 @@
# This modules provides functionality for dealing with compiler warnings. # This modules provides functionality for dealing with compiler warnings.
import errno
import io import io
import json import json
import os import os
@ -282,12 +281,8 @@ class WarningsDatabase(object):
def save_to_file(self, filename): def save_to_file(self, filename):
"""Save the database to a file.""" """Save the database to a file."""
try: # Ensure the directory exists
# Ensure the directory exists os.makedirs(os.path.dirname(filename), exist_ok=True)
os.makedirs(os.path.dirname(filename))
except OSError as e:
if e.errno != errno.EEXIST:
raise
with io.open(filename, "w", encoding="utf-8", newline="\n") as fh: with io.open(filename, "w", encoding="utf-8", newline="\n") as fh:
self.serialize(fh) self.serialize(fh)

View File

@ -5,7 +5,6 @@
# This file contains miscellaneous utility functions that don't belong anywhere # This file contains miscellaneous utility functions that don't belong anywhere
# in particular. # in particular.
import errno
import os import os
import sys import sys
@ -20,11 +19,7 @@ def ensureParentDir(path):
"""Ensures the directory parent to the given file exists.""" """Ensures the directory parent to the given file exists."""
d = os.path.dirname(path) d = os.path.dirname(path)
if d and not os.path.exists(path): if d and not os.path.exists(path):
try: os.makedirs(d, exist_ok=True)
os.makedirs(d)
except OSError as error:
if error.errno != errno.EEXIST:
raise
def mkdir(path, not_indexed=False): def mkdir(path, not_indexed=False):
@ -33,11 +28,7 @@ def mkdir(path, not_indexed=False):
If ``not_indexed`` is True, an attribute is set that disables content If ``not_indexed`` is True, an attribute is set that disables content
indexing on the directory. indexing on the directory.
""" """
try: os.makedirs(path, exist_ok=True)
os.makedirs(path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
if not_indexed: if not_indexed:
if sys.platform == "win32": if sys.platform == "win32":

View File

@ -402,11 +402,7 @@ class JarMaker(object):
if self.outputFormat == "jar": if self.outputFormat == "jar":
# jar # jar
jarfilepath = jarfile + ".jar" jarfilepath = jarfile + ".jar"
try: os.makedirs(os.path.dirname(jarfilepath), exist_ok=True)
os.makedirs(os.path.dirname(jarfilepath))
except OSError as error:
if error.errno != errno.EEXIST:
raise
jf = ZipFile(jarfilepath, "a", lock=True) jf = ZipFile(jarfilepath, "a", lock=True)
outHelper = self.OutputHelper_jar(jf) outHelper = self.OutputHelper_jar(jf)
else: else:
@ -578,11 +574,8 @@ class JarMaker(object):
out = os.path.join(self.basepath, name) out = os.path.join(self.basepath, name)
outdir = os.path.dirname(out) outdir = os.path.dirname(out)
if not os.path.isdir(outdir): if not os.path.isdir(outdir):
try: os.makedirs(outdir, exist_ok=True)
os.makedirs(outdir)
except OSError as error:
if error.errno != errno.EEXIST:
raise
return out return out
class OutputHelper_symlink(OutputHelper_flat): class OutputHelper_symlink(OutputHelper_flat):

View File

@ -22,7 +22,6 @@ value :
| \w+ # string identifier or value; | \w+ # string identifier or value;
""" """
import errno
import io import io
import os import os
import re import re
@ -504,11 +503,8 @@ class Preprocessor:
encoding = "utf-8" encoding = "utf-8"
dir = os.path.dirname(path) dir = os.path.dirname(path)
if dir: if dir:
try: os.makedirs(dir, exist_ok=True)
os.makedirs(dir)
except OSError as error:
if error.errno != errno.EEXIST:
raise
return io.open(path, "w", encoding=encoding, newline="\n") return io.open(path, "w", encoding=encoding, newline="\n")
p = self.getCommandLineParser() p = self.getCommandLineParser()

View File

@ -2,7 +2,6 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this # License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
import errno
import os import os
import shutil import shutil
@ -26,11 +25,7 @@ def prepare_tmp_topsrcdir(path):
"build/moz.configure/util.configure", "build/moz.configure/util.configure",
): ):
file_path = os.path.join(path, p) file_path = os.path.join(path, p)
try: os.makedirs(os.path.dirname(file_path), exist_ok=True)
os.makedirs(os.path.dirname(file_path))
except OSError as e:
if e.errno != errno.EEXIST:
raise
shutil.copy(os.path.join(topsrcdir, p), file_path) shutil.copy(os.path.join(topsrcdir, p), file_path)

View File

@ -304,11 +304,7 @@ class FileCopier(FileRegistry):
# symlinks and permissions and parent directories of the destination # symlinks and permissions and parent directories of the destination
# directory may have their own weird schema. The contract is we only # directory may have their own weird schema. The contract is we only
# manage children of destination, not its parents. # manage children of destination, not its parents.
try: os.makedirs(destination, exist_ok=True)
os.makedirs(destination)
except OSError as e:
if e.errno != errno.EEXIST:
raise
# Because we could be handling thousands of files, code in this # Because we could be handling thousands of files, code in this
# function is optimized to minimize system calls. We prefer CPU time # function is optimized to minimize system calls. We prefer CPU time
@ -336,11 +332,7 @@ class FileCopier(FileRegistry):
# to directory X and we attempt to install a symlink in this directory # to directory X and we attempt to install a symlink in this directory
# to a file in directory X, we may create a recursive symlink! # to a file in directory X, we may create a recursive symlink!
for d in sorted(required_dirs, key=len): for d in sorted(required_dirs, key=len):
try: os.makedirs(d, exist_ok=True)
os.mkdir(d)
except OSError as error:
if error.errno != errno.EEXIST:
raise
# We allow the destination to be a symlink because the caller # We allow the destination to be a symlink because the caller
# is responsible for managing the destination and we assume # is responsible for managing the destination and we assume

View File

@ -193,11 +193,7 @@ class BaseFile(object):
if getattr(self, "path", None) and getattr(dest, "path", None): if getattr(self, "path", None) and getattr(dest, "path", None):
# The destination directory must exist, or CopyFile will fail. # The destination directory must exist, or CopyFile will fail.
destdir = os.path.dirname(dest.path) destdir = os.path.dirname(dest.path)
try: os.makedirs(destdir, exist_ok=True)
os.makedirs(destdir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
_copyfile(self.path, dest.path) _copyfile(self.path, dest.path)
shutil.copystat(self.path, dest.path) shutil.copystat(self.path, dest.path)
else: else: