Skip to content

Commit

Permalink
Added setup.py, github action and necessary bits to publish to PyPi
Browse files Browse the repository at this point in the history
Signed-off-by: Ole Herman Schumacher Elgesem <[email protected]>
  • Loading branch information
olehermanse committed Apr 22, 2021
1 parent bb9b5fd commit 7acf5e7
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 38 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -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/*
46 changes: 8 additions & 38 deletions cfbs/__main__.py
Original file line number Diff line number Diff line change
@@ -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()
43 changes: 43 additions & 0 deletions cfbs/main.py
Original file line number Diff line number Diff line change
@@ -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()}")
10 changes: 10 additions & 0 deletions cfbs/version.py
Original file line number Diff line number Diff line change
@@ -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)"
41 changes: 41 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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="[email protected]",
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",
],
)

0 comments on commit 7acf5e7

Please sign in to comment.