diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml new file mode 100644 index 00000000..1a03a7b6 --- /dev/null +++ b/.github/workflows/python-publish.yml @@ -0,0 +1,31 @@ +# This workflow will upload a Python Package using Twine when a release is created +# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries + +name: Upload Python Package + +on: + release: + types: [created] + +jobs: + deploy: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install setuptools wheel twine + - name: Build and publish + env: + TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} + TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} + run: | + python setup.py sdist bdist_wheel + twine upload dist/* diff --git a/cfbs/__main__.py b/cfbs/__main__.py index a0ea8156..f4695af9 100755 --- a/cfbs/__main__.py +++ b/cfbs/__main__.py @@ -1,41 +1,11 @@ #!/usr/bin/env python3 -"""CFEngine Build System""" +if __name__ == "__main__": + import os + import sys -__authors__ = ["Ole Herman Schumacher Elgesem"] -__copyright__ = ["Northern.tech AS"] + above_dir = os.path.dirname(os.path.realpath(__file__)) + "/../" + abspath = os.path.abspath(above_dir) + sys.path.insert(0, abspath) -def main(commands): - version = "0.0.1" - print(f"Welcome to cfbs version {version}") - -def get_args(): - parser = argparse.ArgumentParser(description='CFEngine Build System.') - parser.add_argument('commands', metavar='cmd', type=str, nargs='+', - help='The command to perform') - parser.add_argument('--loglevel', '-l', - help='Set log level for more/less detailed output', - type=str, default="error") - - args = parser.parse_args() - return args - -def set_log_level(level): - level = level.strip().lower() - if level == "critical": - log.basicConfig(level=log.CRITICAL) - elif level == "error": - log.basicConfig(level=log.ERROR) - elif level == "warning": - log.basicConfig(level=log.WARNING) - elif level == "info": - log.basicConfig(level=log.INFO) - elif level == "debug": - log.basicConfig(level=log.DEBUG) - else: - raise ValueError("Unknown log level: {}".format(level)) - - -if __name__ == '__main__': - args = get_args() - set_log_level(args.loglevel) - main(args.commands) + from cfbs.main import main + main() diff --git a/cfbs/main.py b/cfbs/main.py new file mode 100644 index 00000000..ca58ab84 --- /dev/null +++ b/cfbs/main.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +"""CFEngine Build System""" + +__authors__ = ["Ole Herman Schumacher Elgesem"] +__copyright__ = ["Northern.tech AS"] + +import argparse +import logging as log + +from cfbs import version + +def get_args(): + parser = argparse.ArgumentParser(description='CFEngine Build System.') + parser.add_argument('commands', metavar='cmd', type=str, nargs='?', + help='The command to perform') + parser.add_argument('--loglevel', '-l', + help='Set log level for more/less detailed output', + type=str, default="error") + + args = parser.parse_args() + return args + + +def set_log_level(level): + level = level.strip().lower() + if level == "critical": + log.basicConfig(level=log.CRITICAL) + elif level == "error": + log.basicConfig(level=log.ERROR) + elif level == "warning": + log.basicConfig(level=log.WARNING) + elif level == "info": + log.basicConfig(level=log.INFO) + elif level == "debug": + log.basicConfig(level=log.DEBUG) + else: + raise ValueError("Unknown log level: {}".format(level)) + + +def main(): + args = get_args() + set_log_level(args.loglevel) + print(f"Welcome to cfbs version {version.string()}") diff --git a/cfbs/version.py b/cfbs/version.py new file mode 100644 index 00000000..e0198491 --- /dev/null +++ b/cfbs/version.py @@ -0,0 +1,10 @@ +import os +def string(): + try: + with open(os.path.dirname(__file__) + "/VERSION", "r", encoding="utf-8") as fh: + version = fh.read().strip() + if version: + return version + except: + pass + return "unknown (git checkout)" diff --git a/setup.py b/setup.py new file mode 100644 index 00000000..48f8d12e --- /dev/null +++ b/setup.py @@ -0,0 +1,41 @@ +import setuptools +import subprocess +import os + +cfbs_version = subprocess.run(['git', 'describe', '--tags'], stdout=subprocess.PIPE).stdout.decode("utf-8").strip() +assert "." in cfbs_version + +assert os.path.isfile("cfbs/version.py") +with open("cfbs/VERSION", "w", encoding="utf-8") as fh: + fh.write(f"{cfbs_version}\n") + +with open("README.md", "r", encoding="utf-8") as fh: + long_description = fh.read() + +setuptools.setup( + name="cfbs", + version=cfbs_version, + author="Northern.tech, Inc.", + author_email="contact@northern.tech", + description="Tooling to build, manage and deploy CFEngine policy", + long_description=long_description, + long_description_content_type="text/markdown", + url="https://github.com/olehermanse/cfbs", + packages=setuptools.find_packages(), + package_data={'cfbs': ['VERSION']}, + include_package_data=True, + classifiers=[ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + ], + python_requires='>=3.6', + entry_points={ + "console_scripts": [ + "cfbs = cfbs.main:main" + ] + }, + install_requires=[ + "requests >= 2.25.1", + ], +)