Implement python file handler

This commit is contained in:
bom 2023-02-09 14:32:55 +01:00
parent a4c8f8934a
commit 9d1726051d
3 changed files with 138 additions and 7 deletions

View file

@ -13,7 +13,9 @@ class FileHandler(ABC):
case '.gradle': case '.gradle':
file_handler = GradleFileHandler() file_handler = GradleFileHandler()
case '.clj': case '.clj':
file_handler = CljFileHandler() file_handler = ClojureFileHandler()
case '.py':
file_handler = PythonFileHandler()
case _: case _:
raise Exception(f'The file type "{config_file_type}" is not implemented') 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]: def parse(self) -> tuple[list[int], bool]:
with open(self.config_file_path, 'r') as gradle_file: with open(self.config_file_path, 'r') as gradle_file:
contents = gradle_file.read() 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") exception = Exception("Version not found in gradle file")
if version_line is None: if version_line is None:
raise exception raise exception
@ -77,13 +79,47 @@ class GradleFileHandler(FileHandler):
def write(self, version_string): def write(self, version_string):
with open(self.config_file_path, 'r+') as gradle_file: with open(self.config_file_path, 'r+') as gradle_file:
gradle_contents = gradle_file.read() contents = gradle_file.read()
version_substitute = re.sub("\nversion = .*\n", f'\nversion = "{version_string}"\n', gradle_contents) version_substitute = re.sub('\nversion = "[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?"', f'\nversion = "{version_string}"', contents)
gradle_file.seek(0) gradle_file.seek(0)
gradle_file.write(version_substitute) gradle_file.write(version_substitute)
gradle_file.truncate() 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]: def parse(self) -> tuple[list[int], bool]:
with open(self.config_file_path, 'r') as clj_file: with open(self.config_file_path, 'r') as clj_file:

78
test/config.py Normal file
View file

@ -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'
])

View file

@ -60,7 +60,7 @@ def test_json(tmp_path):
# check # check
assert '"version": "123.123.456-SNAPSHOT"' in f.read_text() assert '"version": "123.123.456-SNAPSHOT"' in f.read_text()
def test_clj(tmp_path): def test_clojure(tmp_path):
# init # init
file_name = 'config.clj' file_name = 'config.clj'
with open(f'test/{file_name}', 'r') as gradle_file: with open(f'test/{file_name}', 'r') as gradle_file:
@ -75,4 +75,21 @@ def test_clj(tmp_path):
version.to_file() version.to_file()
# check # check
assert '1.1.3-SNAPSHOT' in f.read_text() 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()