From b5856c4619a4c2d3df0a90ed354eb51e9991ebfa Mon Sep 17 00:00:00 2001 From: anzoman Date: Wed, 12 May 2021 13:17:38 +0200 Subject: [PATCH] Refactor code to satisfy pylint With updating pylint to its latest version, we also need to apply some minor code fixes. The biggest problem was duplicate code issues, which we needed to disable throughout all files because it's currently not possible to disable just certain code blocks and functions. --- .sanity-config.ini | 3 ++- src/opera/executor/utils.py | 19 ++++++++----------- src/opera/parser/tosca/csar.py | 4 ++-- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/.sanity-config.ini b/.sanity-config.ini index 0c5a0de4..e28e9340 100644 --- a/.sanity-config.ini +++ b/.sanity-config.ini @@ -196,7 +196,8 @@ disable=print-statement, unused-argument, too-few-public-methods, too-many-return-statements, - too-many-branches + too-many-branches, + duplicate-code # Enable the message, report, category or checker with the given id(s). You can # either give multiple identifier separated by comma (,) or put this option diff --git a/src/opera/executor/utils.py b/src/opera/executor/utils.py index 73d2a833..a63bb81a 100644 --- a/src/opera/executor/utils.py +++ b/src/opera/executor/utils.py @@ -16,17 +16,14 @@ def copy(source, target): def write(dest_dir, content, suffix=None): - dest = tempfile.NamedTemporaryFile(dir=dest_dir, delete=False, suffix=suffix) - dest.write(content.encode("utf-8")) - dest.close() - return dest.name + with tempfile.NamedTemporaryFile(dir=dest_dir, delete=False, suffix=suffix) as dest: + dest.write(content.encode("utf-8")) + return dest.name def run_in_directory(dest_dir, cmd, env): - fstdout = tempfile.NamedTemporaryFile(dir=dest_dir, delete=False, suffix=".stdout") - fstderr = tempfile.NamedTemporaryFile(dir=dest_dir, delete=False, suffix=".stderr") - result = subprocess.run(cmd, cwd=dest_dir, stdout=fstdout, stderr=fstderr, # nosec - env=dict(os.environ, **env), check=False) - fstdout.close() - fstderr.close() - return result.returncode, fstdout.name, fstderr.name + with tempfile.NamedTemporaryFile(dir=dest_dir, delete=False, suffix=".stdout") as fstdout, \ + tempfile.NamedTemporaryFile(dir=dest_dir, delete=False, suffix=".stderr") as fstderr: + result = subprocess.run(cmd, cwd=dest_dir, stdout=fstdout, stderr=fstderr, # nosec + env=dict(os.environ, **env), check=False) + return result.returncode, fstdout.name, fstderr.name diff --git a/src/opera/parser/tosca/csar.py b/src/opera/parser/tosca/csar.py index 8180573e..046bd90d 100644 --- a/src/opera/parser/tosca/csar.py +++ b/src/opera/parser/tosca/csar.py @@ -256,7 +256,7 @@ def __init__(self, csar_file: Path): """csar_file is guaranteed to exist, be absolute, and be a file.""" super().__init__(str(csar_file.name)) self.csar_file = csar_file - self.backing_zip = ZipFile(csar_file, mode="r") + self.backing_zip = ZipFile(csar_file, mode="r") # pylint: disable=consider-using-with def package_csar(self, output_file, service_template=None, csar_format="zip") -> str: raise NotImplementedError("Repackaging a packaged CSAR is not implemented.") @@ -269,7 +269,7 @@ def members(self) -> List[PurePath]: return [PurePath(zi.filename) for zi in self.backing_zip.infolist()] def open_member(self, path: PurePath) -> IO: - return self.backing_zip.open(str(path), mode="r") + return self.backing_zip.open(str(path), mode="r") # pylint: disable=consider-using-with def _member_exists(self, member: PurePath) -> bool: try: