From 384a41d933c826c4f385b91304faac3b3cfde68d Mon Sep 17 00:00:00 2001 From: serge-sans-paille Date: Thu, 5 Sep 2024 05:52:56 +0000 Subject: [PATCH] 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 --- configure.py | 8 +------- python/mozbuild/mozbuild/backend/base.py | 7 +------ python/mozbuild/mozbuild/backend/cpp_eclipse.py | 6 +----- python/mozbuild/mozbuild/compilation/warnings.py | 9 ++------- python/mozbuild/mozbuild/dirutils.py | 13 ++----------- python/mozbuild/mozbuild/jar.py | 13 +++---------- python/mozbuild/mozbuild/preprocessor.py | 8 ++------ python/mozbuild/mozbuild/test/common.py | 7 +------ python/mozbuild/mozpack/copier.py | 12 ++---------- python/mozbuild/mozpack/files.py | 6 +----- 10 files changed, 16 insertions(+), 73 deletions(-) diff --git a/configure.py b/configure.py index 0302f935dc91..a48d49f13764 100644 --- a/configure.py +++ b/configure.py @@ -3,7 +3,6 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. import codecs -import errno import io import itertools import logging @@ -154,12 +153,7 @@ def main(argv): js_config = config.copy() pwd = os.getcwd() try: - try: - os.makedirs("js/src") - except OSError as e: - if e.errno != errno.EEXIST: - raise - + os.makedirs("js/src", exist_ok=True) os.chdir("js/src") js_config["OLD_CONFIGURE_SUBSTS"] = old_js_configure_substs js_config["OLD_CONFIGURE_DEFINES"] = old_js_configure_defines diff --git a/python/mozbuild/mozbuild/backend/base.py b/python/mozbuild/mozbuild/backend/base.py index 0f95942f5101..0f3f6d4bbc5b 100644 --- a/python/mozbuild/mozbuild/backend/base.py +++ b/python/mozbuild/mozbuild/backend/base.py @@ -2,7 +2,6 @@ # 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/. -import errno import io import itertools import os @@ -284,11 +283,7 @@ class BuildBackend(LoggingMixin): assert fh is not None dirname = mozpath.dirname(fh.name) - try: - os.makedirs(dirname) - except OSError as error: - if error.errno != errno.EEXIST: - raise + os.makedirs(dirname, exist_ok=True) yield fh diff --git a/python/mozbuild/mozbuild/backend/cpp_eclipse.py b/python/mozbuild/mozbuild/backend/cpp_eclipse.py index 04735b702f7f..d0df1b2c555d 100644 --- a/python/mozbuild/mozbuild/backend/cpp_eclipse.py +++ b/python/mozbuild/mozbuild/backend/cpp_eclipse.py @@ -118,11 +118,7 @@ class CppEclipseBackend(CommonBackend): workspace_settings_dir, self._workspace_lang_dir, ]: - try: - os.makedirs(dir_name) - except OSError as e: - if e.errno != errno.EEXIST: - raise + os.makedirs(dir_name, exist_ok=True) project_path = os.path.join(self._project_dir, ".project") with open(project_path, "w") as fh: diff --git a/python/mozbuild/mozbuild/compilation/warnings.py b/python/mozbuild/mozbuild/compilation/warnings.py index 4f0ef57e5148..66f2b17a9b99 100644 --- a/python/mozbuild/mozbuild/compilation/warnings.py +++ b/python/mozbuild/mozbuild/compilation/warnings.py @@ -4,7 +4,6 @@ # This modules provides functionality for dealing with compiler warnings. -import errno import io import json import os @@ -282,12 +281,8 @@ class WarningsDatabase(object): def save_to_file(self, filename): """Save the database to a file.""" - try: - # Ensure the directory exists - os.makedirs(os.path.dirname(filename)) - except OSError as e: - if e.errno != errno.EEXIST: - raise + # Ensure the directory exists + os.makedirs(os.path.dirname(filename), exist_ok=True) with io.open(filename, "w", encoding="utf-8", newline="\n") as fh: self.serialize(fh) diff --git a/python/mozbuild/mozbuild/dirutils.py b/python/mozbuild/mozbuild/dirutils.py index efa474630166..a571f6b7d6f9 100644 --- a/python/mozbuild/mozbuild/dirutils.py +++ b/python/mozbuild/mozbuild/dirutils.py @@ -5,7 +5,6 @@ # This file contains miscellaneous utility functions that don't belong anywhere # in particular. -import errno import os import sys @@ -20,11 +19,7 @@ def ensureParentDir(path): """Ensures the directory parent to the given file exists.""" d = os.path.dirname(path) if d and not os.path.exists(path): - try: - os.makedirs(d) - except OSError as error: - if error.errno != errno.EEXIST: - raise + os.makedirs(d, exist_ok=True) 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 indexing on the directory. """ - try: - os.makedirs(path) - except OSError as e: - if e.errno != errno.EEXIST: - raise + os.makedirs(path, exist_ok=True) if not_indexed: if sys.platform == "win32": diff --git a/python/mozbuild/mozbuild/jar.py b/python/mozbuild/mozbuild/jar.py index 1bf279f711d0..2980ab891022 100644 --- a/python/mozbuild/mozbuild/jar.py +++ b/python/mozbuild/mozbuild/jar.py @@ -402,11 +402,7 @@ class JarMaker(object): if self.outputFormat == "jar": # jar jarfilepath = jarfile + ".jar" - try: - os.makedirs(os.path.dirname(jarfilepath)) - except OSError as error: - if error.errno != errno.EEXIST: - raise + os.makedirs(os.path.dirname(jarfilepath), exist_ok=True) jf = ZipFile(jarfilepath, "a", lock=True) outHelper = self.OutputHelper_jar(jf) else: @@ -578,11 +574,8 @@ class JarMaker(object): out = os.path.join(self.basepath, name) outdir = os.path.dirname(out) if not os.path.isdir(outdir): - try: - os.makedirs(outdir) - except OSError as error: - if error.errno != errno.EEXIST: - raise + os.makedirs(outdir, exist_ok=True) + return out class OutputHelper_symlink(OutputHelper_flat): diff --git a/python/mozbuild/mozbuild/preprocessor.py b/python/mozbuild/mozbuild/preprocessor.py index b3a82a060ce9..942abb19c052 100644 --- a/python/mozbuild/mozbuild/preprocessor.py +++ b/python/mozbuild/mozbuild/preprocessor.py @@ -22,7 +22,6 @@ value : | \w+ # string identifier or value; """ -import errno import io import os import re @@ -504,11 +503,8 @@ class Preprocessor: encoding = "utf-8" dir = os.path.dirname(path) if dir: - try: - os.makedirs(dir) - except OSError as error: - if error.errno != errno.EEXIST: - raise + os.makedirs(dir, exist_ok=True) + return io.open(path, "w", encoding=encoding, newline="\n") p = self.getCommandLineParser() diff --git a/python/mozbuild/mozbuild/test/common.py b/python/mozbuild/mozbuild/test/common.py index 47f04a8dd3d2..47e3713a4e7c 100644 --- a/python/mozbuild/mozbuild/test/common.py +++ b/python/mozbuild/mozbuild/test/common.py @@ -2,7 +2,6 @@ # 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/. -import errno import os import shutil @@ -26,11 +25,7 @@ def prepare_tmp_topsrcdir(path): "build/moz.configure/util.configure", ): file_path = os.path.join(path, p) - try: - os.makedirs(os.path.dirname(file_path)) - except OSError as e: - if e.errno != errno.EEXIST: - raise + os.makedirs(os.path.dirname(file_path), exist_ok=True) shutil.copy(os.path.join(topsrcdir, p), file_path) diff --git a/python/mozbuild/mozpack/copier.py b/python/mozbuild/mozpack/copier.py index c042e5432f28..38ffb6166de4 100644 --- a/python/mozbuild/mozpack/copier.py +++ b/python/mozbuild/mozpack/copier.py @@ -304,11 +304,7 @@ class FileCopier(FileRegistry): # symlinks and permissions and parent directories of the destination # directory may have their own weird schema. The contract is we only # manage children of destination, not its parents. - try: - os.makedirs(destination) - except OSError as e: - if e.errno != errno.EEXIST: - raise + os.makedirs(destination, exist_ok=True) # Because we could be handling thousands of files, code in this # 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 a file in directory X, we may create a recursive symlink! for d in sorted(required_dirs, key=len): - try: - os.mkdir(d) - except OSError as error: - if error.errno != errno.EEXIST: - raise + os.makedirs(d, exist_ok=True) # We allow the destination to be a symlink because the caller # is responsible for managing the destination and we assume diff --git a/python/mozbuild/mozpack/files.py b/python/mozbuild/mozpack/files.py index 9ae16d5c1c61..bdfe7920fc38 100644 --- a/python/mozbuild/mozpack/files.py +++ b/python/mozbuild/mozpack/files.py @@ -193,11 +193,7 @@ class BaseFile(object): if getattr(self, "path", None) and getattr(dest, "path", None): # The destination directory must exist, or CopyFile will fail. destdir = os.path.dirname(dest.path) - try: - os.makedirs(destdir) - except OSError as e: - if e.errno != errno.EEXIST: - raise + os.makedirs(destdir, exist_ok=True) _copyfile(self.path, dest.path) shutil.copystat(self.path, dest.path) else: