Skip to content

Commit

Permalink
Simplify and maintain build.py (#398)
Browse files Browse the repository at this point in the history
* Simplify and maintain build.py

Signed-off-by: sschulz92 <bastie92_spam@gmx.de>

* Remove explicit close() of opened file resource if context manager is used

Signed-off-by: sschulz92 <bastie92_spam@gmx.de>

* Correct typo

Signed-off-by: Chad Wilson <chadw@thoughtworks.com>

---------

Signed-off-by: sschulz92 <bastie92_spam@gmx.de>
Signed-off-by: Chad Wilson <chadw@thoughtworks.com>
Co-authored-by: Chad Wilson <chadw@thoughtworks.com>
  • Loading branch information
sschulz92 and chadlwilson authored Oct 2, 2024
1 parent 97093f6 commit 46e0873
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 44 deletions.
48 changes: 22 additions & 26 deletions build.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -20,6 +22,8 @@

DEPLOY = os.path.join(cwd, 'deploy')

ROOT_DIR = Path(__file__).resolve().parent


def install():
plugin_zip = create_zip()
Expand All @@ -28,29 +32,26 @@ 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()
with open("setup.py", "w+", encoding="utf-8") as setup:
v = get_version()
setup.write(tmpl_content.format(v))


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)


def create_zip():
Expand All @@ -64,15 +65,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]


Expand All @@ -93,7 +94,7 @@ def copy(src, dest):


usage = """
Usage: python install.py --[option]
Usage: python build.py --[option]
Options:
--test : runs unit tests.
Expand All @@ -103,16 +104,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


Expand Down
1 change: 0 additions & 1 deletion getgauge/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 0 additions & 1 deletion getgauge/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion setup.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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},
)
31 changes: 16 additions & 15 deletions tests/test_refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <vowels>.")
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 <vowels>.")
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 <vowels>.', None,
RefactorTests.path)

Expand Down Expand Up @@ -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

0 comments on commit 46e0873

Please sign in to comment.