From 2c2fdd0e4e3b591ce070d0bc417fac35f920f9ce Mon Sep 17 00:00:00 2001 From: sschulz92 Date: Tue, 1 Oct 2024 13:49:47 +0200 Subject: [PATCH 1/3] Simplify and maintain build.py Signed-off-by: sschulz92 --- build.py | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/build.py b/build.py index a28f3f2..8905526 100644 --- a/build.py +++ b/build.py @@ -1,9 +1,11 @@ # /usr/bin/env python + +import glob import json import os import shutil import sys -from datetime import date +from pathlib import Path from subprocess import call cwd = os.getcwd() @@ -20,6 +22,8 @@ DEPLOY = os.path.join(cwd, 'deploy') +ROOT_DIR = Path(__file__).resolve().parent + def install(): plugin_zip = create_zip() @@ -28,29 +32,30 @@ def install(): os.path.join(BIN, plugin_zip)]) generate_package() p = os.listdir("dist")[0] - print("Installing getgauge package using pip: \n\tpip install dist/{}".format(p)) + print(f"Installing getgauge package using pip: \n\tpip install dist/{p}") call([sys.executable, "-m", "pip", "install", - "dist/{}".format(p), "--upgrade", "--user"]) + f"dist/{p}", "--upgrade", "--user"]) sys.exit(exit_code) def create_setup_file(): - tmpl = open("setup.tmpl", "r") - setup = open("setup.py", "w+") - v = get_version() - setup.write(tmpl.read().format( - v, "{\n\t\t':python_version == \"2.7\"': ['futures']\n\t}")) - setup.close() - tmpl.close() + with open("setup.tmpl", "r", encoding="utf-8") as tmpl: + tmpl_content = tmpl.read() + tmpl.close() + with open("setup.py", "w+", encoding="utf-8") as setup: + v = get_version() + setup.write(tmpl_content.format( + v, "{\n\t\t':python_version == \"2.7\"': ['futures']\n\t}")) + setup.close() def generate_package(): shutil.rmtree('dist', True) print('Creating getgauge package.') create_setup_file() - fnull = open(os.devnull, 'w') - call([sys.executable, 'setup.py', 'sdist'], stdout=fnull, stderr=fnull) - fnull.close() + with open(os.devnull, 'w', encoding="utf-8") as fnull: + call([sys.executable, 'setup.py', 'sdist'], stdout=fnull, stderr=fnull) + fnull.close() def create_zip(): @@ -64,15 +69,15 @@ def create_zip(): if os.path.exists(BIN): shutil.rmtree(BIN) os.mkdir(BIN) - plugin_zip = '{0}.zip'.format(output_file) + plugin_zip = f"{output_file}.zip" shutil.move(plugin_zip, BIN) print('Zip file created.') return plugin_zip def get_version(): - json_data = open(PLUGIN_JSON).read() - data = json.loads(json_data) + with open(PLUGIN_JSON, "r", encoding="utf-8") as json_data: + data = json.loads(json_data.read()) return data[VERSION] @@ -103,16 +108,11 @@ def copy(src, dest): def run_tests(): pp = "PYTHONPATH" - os.environ[pp] = "{0}{1}{2}".format(os.environ.get( - pp), os.pathsep, os.path.abspath(os.path.curdir)) - test_dir = os.path.join(os.path.curdir, "tests") + os.environ[pp] = str(ROOT_DIR) exit_code = 0 - for root, _, files in os.walk(test_dir): - for item in files: - if item.startswith("test_") and item.endswith(".py"): - fileNamePath = str(os.path.join(root, item)) - exit_code = call([sys.executable, fileNamePath] - ) if exit_code == 0 else exit_code + for file_name_path in glob.glob(f"{ROOT_DIR}/tests/**/test_*.py", recursive=True): + exit_code = call([sys.executable, file_name_path] + ) if exit_code == 0 else exit_code return exit_code From edc2347aece5ed5ef48e90176d115cc26d7c34db Mon Sep 17 00:00:00 2001 From: sschulz92 Date: Tue, 1 Oct 2024 15:53:23 +0200 Subject: [PATCH 2/3] Remove explicit close() of opened file resource if context manager is used Signed-off-by: sschulz92 --- build.py | 6 +----- getgauge/registry.py | 1 - getgauge/util.py | 1 - setup.tmpl | 1 - tests/test_refactor.py | 31 ++++++++++++++++--------------- 5 files changed, 17 insertions(+), 23 deletions(-) diff --git a/build.py b/build.py index 8905526..902ce34 100644 --- a/build.py +++ b/build.py @@ -41,12 +41,9 @@ def install(): def create_setup_file(): with open("setup.tmpl", "r", encoding="utf-8") as tmpl: tmpl_content = tmpl.read() - tmpl.close() with open("setup.py", "w+", encoding="utf-8") as setup: v = get_version() - setup.write(tmpl_content.format( - v, "{\n\t\t':python_version == \"2.7\"': ['futures']\n\t}")) - setup.close() + setup.write(tmpl_content.format(v)) def generate_package(): @@ -55,7 +52,6 @@ def generate_package(): create_setup_file() with open(os.devnull, 'w', encoding="utf-8") as fnull: call([sys.executable, 'setup.py', 'sdist'], stdout=fnull, stderr=fnull) - fnull.close() def create_zip(): diff --git a/getgauge/registry.py b/getgauge/registry.py index 16b413e..037e60a 100644 --- a/getgauge/registry.py +++ b/getgauge/registry.py @@ -265,7 +265,6 @@ def capture_to_file(): content = registry.screenshot_provider()() with open(screenshot_file, "wb") as file: file.write(content) - file.close() return os.path.basename(screenshot_file) screenshot_file = registry.screenshot_provider()() if not os.path.isabs(screenshot_file): diff --git a/getgauge/util.py b/getgauge/util.py index eeb8f52..d1051d3 100644 --- a/getgauge/util.py +++ b/getgauge/util.py @@ -36,7 +36,6 @@ def read_file_contents(file_name): if os.path.isfile(file_name): with open(file_name, "r", encoding="utf-8") as f: content = f.read().replace('\r\n', '\n') - f.close() return content return None diff --git a/setup.tmpl b/setup.tmpl index 885821d..fd93899 100644 --- a/setup.tmpl +++ b/setup.tmpl @@ -18,5 +18,4 @@ setup( 'Programming Language :: Python :: 3 :: Only', ], install_requires=['redBaron', 'debugpy', 'grpcio>=1.39.0', 'protobuf>=3.5.2'], - extras_require={1}, ) diff --git a/tests/test_refactor.py b/tests/test_refactor.py index 83d5596..f479d64 100644 --- a/tests/test_refactor.py +++ b/tests/test_refactor.py @@ -15,15 +15,17 @@ class RefactorTests(unittest.TestCase): def setUp(self): self.preservesNewlines = True RefactorTests.path = os.path.join(tempfile.gettempdir(), 'step_impl.py') - RefactorTests.file = open(RefactorTests.path, 'w') - RefactorTests.file.write("""@step("Vowels in English language are .") -def assert_default_vowels(arg0): - Messages.write_message("Given vowels are {0}".format(given_vowels)) - assert given_vowels == "".join(vowels)\n""") - RefactorTests.file.close() - RefactorTests.file = open(RefactorTests.path, 'r') - RefactorTests.data = RefactorTests.file.read() - RefactorTests.file.close() + with open(RefactorTests.path, 'w', encoding="utf-8") as refactor_file: + RefactorTests.file = refactor_file + RefactorTests.file.write("""@step("Vowels in English language are .") + def assert_default_vowels(arg0): + Messages.write_message("Given vowels are {0}".format(given_vowels)) + assert given_vowels == "".join(vowels)\n""") + + with open(RefactorTests.path, 'r', encoding="utf-8") as refactor_file: + RefactorTests.file = refactor_file + RefactorTests.data = RefactorTests.file.read() + registry.add_step('Vowels in English language are .', None, RefactorTests.path) @@ -337,10 +339,9 @@ def assert_default_vowels(arg0, arg1): self.assertIn('arg0, arg1', diff_contents) def getActualText(self): - _file = open(RefactorTests.path, 'r+') - actual_data = _file.read() - _file.seek(0) - _file.truncate() - _file.write(RefactorTests.data) - _file.close() + with open(RefactorTests.path, 'r+', encoding="utf-8") as _file: + actual_data = _file.read() + _file.seek(0) + _file.truncate() + _file.write(RefactorTests.data) return actual_data From 7d04d90d09780e2704210dbe21af9726b6bc47b8 Mon Sep 17 00:00:00 2001 From: Chad Wilson Date: Wed, 2 Oct 2024 10:24:08 +0800 Subject: [PATCH 3/3] Correct typo Signed-off-by: Chad Wilson --- build.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.py b/build.py index 902ce34..a6a4dc1 100644 --- a/build.py +++ b/build.py @@ -94,7 +94,7 @@ def copy(src, dest): usage = """ -Usage: python install.py --[option] +Usage: python build.py --[option] Options: --test : runs unit tests.