From 9d1726051dfa212974cf46d6bbf8d1d35d9c0ef2 Mon Sep 17 00:00:00 2001 From: bom Date: Thu, 9 Feb 2023 14:32:55 +0100 Subject: [PATCH] Implement python file handler --- file_handlers.py | 46 +++++++++++++++++++--- test/config.py | 78 ++++++++++++++++++++++++++++++++++++++ test/test_version_class.py | 21 +++++++++- 3 files changed, 138 insertions(+), 7 deletions(-) create mode 100644 test/config.py diff --git a/file_handlers.py b/file_handlers.py index 6c0813a..e3f627d 100644 --- a/file_handlers.py +++ b/file_handlers.py @@ -13,7 +13,9 @@ class FileHandler(ABC): case '.gradle': file_handler = GradleFileHandler() case '.clj': - file_handler = CljFileHandler() + file_handler = ClojureFileHandler() + case '.py': + file_handler = PythonFileHandler() case _: raise Exception(f'The file type "{config_file_type}" is not implemented') @@ -55,7 +57,7 @@ class GradleFileHandler(FileHandler): def parse(self) -> tuple[list[int], bool]: with open(self.config_file_path, 'r') as gradle_file: contents = gradle_file.read() - version_line = re.search("\nversion = .*\n", contents) + version_line = re.search("\nversion = .*", contents) exception = Exception("Version not found in gradle file") if version_line is None: raise exception @@ -77,13 +79,47 @@ class GradleFileHandler(FileHandler): def write(self, version_string): with open(self.config_file_path, 'r+') as gradle_file: - gradle_contents = gradle_file.read() - version_substitute = re.sub("\nversion = .*\n", f'\nversion = "{version_string}"\n', gradle_contents) + contents = gradle_file.read() + version_substitute = re.sub('\nversion = "[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?"', f'\nversion = "{version_string}"', contents) gradle_file.seek(0) gradle_file.write(version_substitute) gradle_file.truncate() -class CljFileHandler(FileHandler): + +class PythonFileHandler(FileHandler): + + def parse(self) -> tuple[list[int], bool]: + with open(self.config_file_path, 'r') as python_file: + contents = python_file.read() + version_line = re.search("\nversion = .*\n", contents) + exception = Exception("Version not found in gradle file") + if version_line is None: + raise exception + + version_line = version_line.group() + version_string = re.search('[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', version_line) + if version_string is None: + raise exception + + version_string = version_string.group() + is_snapshot = False + if '-SNAPSHOT' in version_string: + is_snapshot = True + version_string = version_string.replace('-SNAPSHOT', '') + + version = [int(x) for x in version_string.split('.')] + + return version, is_snapshot + + def write(self, version_string): + with open(self.config_file_path, 'r+') as python_file: + contents = python_file.read() + version_substitute = re.sub('\nversion = "[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?"', f'\nversion = "{version_string}"', contents) + python_file.seek(0) + python_file.write(version_substitute) + python_file.truncate() + +class ClojureFileHandler(FileHandler): def parse(self) -> tuple[list[int], bool]: with open(self.config_file_path, 'r') as clj_file: diff --git a/test/config.py b/test/config.py new file mode 100644 index 0000000..bb47bcd --- /dev/null +++ b/test/config.py @@ -0,0 +1,78 @@ +# dda_devops_build +# Copyright 2019 meissa GmbH. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from pybuilder.core import init, use_plugin, Author + +use_plugin("python.core") +use_plugin("copy_resources") +use_plugin("filter_resources") +#use_plugin("python.unittest") +#use_plugin("python.coverage") +use_plugin("python.distutils") + +#use_plugin("python.install_dependencies") + +default_task = "publish" + +name = "ddadevops" +version = "3.1.3" +summary = "tools to support builds combining gopass, terraform, dda-pallet, aws & hetzner-cloud" +description = __doc__ +authors = [Author("meissa GmbH", "buero@meissa-gmbh.de")] +url = "https://github.com/DomainDrivenArchitecture/dda-devops-build" +requires_python = ">=2.7,!=3.0,!=3.1,!=3.2,!=3.3,!=3.4" # CHECK IF NEW VERSION EXISTS +license = "Apache Software License" + +@init +def initialize(project): + #project.build_depends_on('mockito') + #project.build_depends_on('unittest-xml-reporting') + + project.set_property("verbose", True) + project.get_property("filter_resources_glob").append("main/python/ddadevops/__init__.py") + #project.set_property("dir_source_unittest_python", "src/unittest/python") + + project.set_property("copy_resources_target", "$dir_dist/ddadevops") + project.get_property("copy_resources_glob").append("LICENSE") + project.get_property("copy_resources_glob").append("src/main/resources/terraform/*") + project.get_property("copy_resources_glob").append("src/main/resources/docker/image/resources/*") + project.include_file("ddadevops", "LICENSE") + project.include_file("ddadevops", "src/main/resources/terraform/*") + project.include_file("ddadevops", "src/main/resources/docker/image/resources/*") + + #project.set_property('distutils_upload_sign', True) + #project.set_property('distutils_upload_sign_identity', '') + project.set_property("distutils_readme_description", True) + project.set_property("distutils_description_overwrite", True) + project.set_property("distutils_classifiers", [ + 'License :: OSI Approved :: Apache Software License', + 'Programming Language :: Python', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Operating System :: POSIX :: Linux', + 'Operating System :: OS Independent', + 'Development Status :: 5 - Production/Stable', + 'Environment :: Console', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: Apache Software License', + 'Topic :: Software Development :: Build Tools', + 'Topic :: Software Development :: Quality Assurance', + 'Topic :: Software Development :: Testing' + ]) diff --git a/test/test_version_class.py b/test/test_version_class.py index 9811e09..ddf38c3 100644 --- a/test/test_version_class.py +++ b/test/test_version_class.py @@ -60,7 +60,7 @@ def test_json(tmp_path): # check assert '"version": "123.123.456-SNAPSHOT"' in f.read_text() -def test_clj(tmp_path): +def test_clojure(tmp_path): # init file_name = 'config.clj' with open(f'test/{file_name}', 'r') as gradle_file: @@ -75,4 +75,21 @@ def test_clj(tmp_path): version.to_file() # check - assert '1.1.3-SNAPSHOT' in f.read_text() \ No newline at end of file + assert '1.1.3-SNAPSHOT' in f.read_text() + +def test_python(tmp_path): + # init + file_name = 'config.py' + with open(f'test/{file_name}', 'r') as gradle_file: + contents = gradle_file.read() + + f = tmp_path / file_name + f.write_text(contents) + + # test + version = Version.from_file(f) + version.increment(ReleaseLevel.SNAPSHOT) + version.to_file() + + # check + assert '3.1.3-SNAPSHOT' in f.read_text() \ No newline at end of file