~rootmos/action

0c27aa858c2524a83b64e0cac6fb3903c764c969 — Gustav Behm 1 year, 3 months ago
Add python infrastructure
9 files changed, 137 insertions(+), 0 deletions(-)

A .gitignore
A Makefile
A Pipfile
A Pipfile.lock
A pyproject.toml
A setup.cfg
A src/action/__init__.py
A src/action/cli.py
A src/action/util.py
A  => .gitignore +3 -0
@@ 1,3 @@
.install.flag
build
*.egg-info

A  => Makefile +11 -0
@@ 1,11 @@
.PHONY: install
install:
	pipx install --force .

.PHONY: shell
shell: .install.flag
	pipenv shell

.install.flag: setup.cfg
	pipenv install -e .
	touch $@

A  => Pipfile +13 -0
@@ 1,13 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
chess-hack = {editable = true, path = "."}
action = {editable = true, path = "."}

[dev-packages]

[requires]
python_version = "3.11"

A  => Pipfile.lock +25 -0
@@ 1,25 @@
{
    "_meta": {
        "hash": {
            "sha256": "832f2a842bce66865b3a08fedf268d7077019327e5ba34d7a3e02712a183c7c4"
        },
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.11"
        },
        "sources": [
            {
                "name": "pypi",
                "url": "https://pypi.org/simple",
                "verify_ssl": true
            }
        ]
    },
    "default": {
        "action": {
            "editable": true,
            "path": "."
        }
    },
    "develop": {}
}

A  => pyproject.toml +3 -0
@@ 1,3 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

A  => setup.cfg +18 -0
@@ 1,18 @@
[metadata]
name = action
version = 0.0.1

[options]
packages = find_namespace:
package_dir =
    = src

install_requires =
  importlib-metadata; python_version < "3.8"

[options.packages.find]
where = src

[options.entry_points]
console_scripts =
  action = action.cli:main

A  => src/action/__init__.py +7 -0
@@ 1,7 @@
import importlib.metadata

package_name = __name__
package_version = importlib.metadata.version(package_name)

app_name = __name__.replace("_", "-")
env_prefix = package_name.upper() + "_"

A  => src/action/cli.py +28 -0
@@ 1,28 @@
import argparse
import sys

from . import package_version
from . import util
from .util import env

import logging
logger = logging.getLogger(__name__)

def parse_args():
    parser = argparse.ArgumentParser(
            description="Action!",
            formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument("-v", "--version", action="version", version=f"%(prog)s {package_version}")

    parser.add_argument("--log", default=env("LOG_LEVEL", "WARN"), help="set log level")
    parser.add_argument("--log-file", metavar="FILE", default=env("LOG_FILE"), help="redirect stdout and stderr to FILE")

    return parser.parse_args()

def main():
    args = parse_args()
    if args.log_file is not None:
        sys.stderr = sys.stdout = open(args.log_file, "a")
    util.setup_logger(args.log)
    logger.debug(f"args: {args}")

A  => src/action/util.py +29 -0
@@ 1,29 @@
import logging
import os

from . import package_name, env_prefix

import logging
logger = logging.getLogger(__name__)

def env_name(var):
    return env_prefix + var

def env(var, default=None):
    return os.environ.get(env_name(var), default)

def setup_logger(level):
    level = level.upper()
    l = logging.getLogger(package_name)
    l.setLevel(level)

    ch = logging.StreamHandler()
    ch.setLevel(level)

    f = logging.Formatter(fmt="%(asctime)s:%(name)s:%(levelname)s %(message)s", datefmt="%Y-%m-%dT%H:%M:%S%z")
    ch.setFormatter(f)

    l.addHandler(ch)

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)