Skip to content

Commit 380020d

Browse files
ci/cd + dependencies
1 parent 5d78722 commit 380020d

File tree

5 files changed

+198
-18
lines changed

5 files changed

+198
-18
lines changed

.github/workflows/python_test.yml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
2+
# https://github.com/actions/setup-python/blob/main/docs/advanced-usage.md#caching-packages
3+
# https://www.peterbe.com/plog/install-python-poetry-github-actions-faster
4+
5+
name: python_test
6+
7+
on: [push, workflow_dispatch] # pull_request, push, workflow_dispatch
8+
9+
jobs:
10+
ci:
11+
strategy:
12+
matrix:
13+
python-version: ["3.10", "3.11"]
14+
os: ["ubuntu-latest"]
15+
arch: ['x64']
16+
17+
runs-on: ${{ matrix.os }}
18+
19+
steps:
20+
- name: checkout repo content
21+
uses: actions/checkout@v3
22+
# TODO: debug 'cache not found for input keys dotlocal-Linux-8e1cebf...'
23+
- name: load cached $HOME/.local
24+
uses: actions/cache@v2.1.6
25+
id: cash-money
26+
with:
27+
path: ~/.local
28+
key: dotlocal-${{ runner.os }}-${{ hashFiles('.github/workflows/python_test.yml') }}
29+
# - run: echo '${{ steps.cash-money.outputs.cache-hit }}' # true if cache-hit occured on the primary key
30+
- name: set variables
31+
run: |
32+
BASE_DIR=$(cat BASE_DIR)
33+
echo "BASE_DIR=$BASE_DIR" >> $GITHUB_ENV
34+
- name: Read .tool-versions # dynamic versions
35+
uses: marocchino/tool-versions-action@v1
36+
id: versions
37+
with:
38+
path: ${{ env.BASE_DIR }}/.tool-versions
39+
- name: shuffle files
40+
run: |
41+
cat ${{ env.BASE_DIR }}/.tool-versions | awk '/python/ {print $NF}' >> ${{ env.BASE_DIR }}/.python-version
42+
[[ -e "$(pwd)/pyproject.toml" ]] && mv "$(pwd)/pyproject.toml" "$(pwd)/pyproject.toml.bak"
43+
cp "${{ env.BASE_DIR }}/pyproject.toml" $(pwd)/
44+
- name: setup python
45+
uses: actions/setup-python@v4
46+
with:
47+
python-version-file: ${{ env.BASE_DIR }}/.python-version
48+
cache: 'pip'
49+
- name: setup poetry
50+
uses: snok/install-poetry@v1
51+
id: cp310
52+
with:
53+
version: ${{ steps.versions.outputs.poetry }}
54+
virtualenvs-create: true
55+
virtualenvs-in-project: true
56+
virtualenvs-path: ${{ env.BASE_DIR }}
57+
installer-parallel: true
58+
- name: load cached venv
59+
id: cached-poetry-dependencies
60+
uses: actions/cache@v2.1.6
61+
with:
62+
path: ${{ env.BASE_DIR }}/.venv
63+
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}-${{ hashFiles('.github/workflows/python_test.yml') }}
64+
- name: install virtualenv
65+
run: |
66+
cd ${{ env.BASE_DIR }}
67+
poetry install
68+
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
69+
- name: Lint with flake8
70+
run: |
71+
# stop the build if there are Python syntax errors or undefined names
72+
poetry run flake8 ${{ env.BASE_DIR }} --count --select=E9,F63,F7,F82 --show-source --statistics
73+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
74+
poetry run flake8 ${{ env.BASE_DIR }} --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
75+
# TODO: write tests
76+
- name: Test with pytest
77+
run: |
78+
# exit code 5 == 'collected 0 items'
79+
poetry run pytest ${{ env.BASE_DIR }}

.github/workflows/run_script.yml

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# https://github.com/actions/setup-python/blob/main/docs/advanced-usage.md#caching-packages
2+
13
name: run_script
24

35
on: [workflow_dispatch] # pull_request, push, workflow_dispatch
@@ -18,34 +20,38 @@ jobs:
1820
steps:
1921
- name: checkout repo content
2022
uses: actions/checkout@v3
21-
# dynamic versions
22-
- name: Read .tool-versions
23+
- name: Read .tool-versions # dynamic versions
2324
uses: marocchino/tool-versions-action@v1
2425
id: versions
2526
- name: setup python
2627
uses: actions/setup-python@v4
28+
id: cp310
2729
with:
2830
python-version: ${{ steps.versions.outputs.python }}
2931
architecture: ${{ matrix.arch }}
30-
- name: run poetry image
31-
uses: abatilo/actions-poetry@v2.0.0
32-
with:
33-
poetry-version: ${{ steps.versions.outputs.poetry }}
34-
# - name: install gh cli
35-
# shell: bash
36-
# run: |
37-
# curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
38-
# sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg
39-
# echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
40-
# sudo apt update
41-
# sudo apt install --no-install-recommends -y gh
32+
cache: 'poetry' # cache dependencies
33+
- run: echo '${{ steps.cp310.outputs.cache-hit }}' # true if cache-hit occured on the primary key
34+
- name: Install poetry & dependencies
35+
run: |
36+
python -m pip install --upgrade pip
37+
python -m pip install poetry
38+
poetry install
4239
- name: run script
4340
shell: bash
4441
env:
45-
# github token needed for gh cli
46-
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42+
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # gh cli
4743
username: ${{ secrets.USERNAME }}
4844
run: |
49-
python -m pip install --upgrade pip
50-
poetry install
5145
poetry run python hello.py
46+
47+
48+
steps:
49+
- uses: actions/checkout@v3
50+
- name: Install poetry
51+
run: pipx install poetry
52+
- uses: actions/setup-python@v4
53+
with:
54+
python-version: '3.9'
55+
cache: 'poetry'
56+
- run: poetry install
57+
- run: poetry run pytest

BASE_DIR

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./cwd

ci_dir.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python3
2+
3+
import subprocess
4+
from pathlib import Path
5+
6+
"""
7+
Over-engineered python solution to local directory for GitHub Actions.
8+
9+
cf. bash:
10+
w=$(basename $(echo $PWD)); echo "./${w}" > ../BASE_DIR
11+
"""
12+
13+
# get path of working directory
14+
cwd = Path.cwd()
15+
16+
# basename of working directory
17+
cwd_bn = cwd.name
18+
19+
# add './' prefix to cwd_bn
20+
rel = "./" + cwd_bn
21+
22+
# add 'CWD=' prefix to rel
23+
cwd_str = "CWD=" + rel
24+
25+
# call git via subprocess to find name of repo
26+
abs = subprocess.check_output(["git", "rev-parse", "--show-toplevel"]).decode("utf-8").strip()
27+
28+
# split at last '/' and assign last element to repo
29+
raw = abs.split("/")
30+
repo = raw[-1]
31+
32+
# path to relative directory
33+
up = str(Path(f"{repo}/{rel}"))
34+
35+
# dot path to working directory
36+
dot = f"{rel}"
37+
38+
# list locations right aligned up to n chars (needed for absolute path)
39+
# print(f"abs: {abs:>30}")
40+
# print(f"cwd: {up:>30}")
41+
# print(f"dot: {dot:>30}")
42+
# print(f"tld: {repo:>30}")
43+
44+
# write dot to "{abs}/WORKDIR"
45+
with open(f"{abs}/BASE_DIR", "w") as f:
46+
f.write(dot)

pyproject.toml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,54 @@ dnspython = "^2.2.1"
2121
[tool.poetry.dev-dependencies]
2222
icecream = "^2.1.1"
2323

24+
[tool.black]
25+
line-length = 130
26+
target-version = ['py310']
27+
include = '\.pyi?$'
28+
exclude = '''
29+
/(
30+
\.git
31+
| \.hg
32+
| \.mypy_cache
33+
| \.tox
34+
| \.venv
35+
| _build
36+
| buck-out
37+
| build
38+
)/
39+
'''
40+
41+
[flake8]
42+
max-line-length = 130
43+
extend-ignore = ["E203", "E251", "E266", "E302", "E305", "E401", "E402", "E501", "W503", "F403", "F401"]
44+
45+
[tool.interrogate]
46+
ignore-init-method = true
47+
ignore-init-module = false
48+
ignore-magic = false
49+
ignore-semiprivate = false
50+
ignore-private = false
51+
ignore-property-decorators = false
52+
ignore-module = true
53+
ignore-nested-functions = false
54+
ignore-nested-classes = true
55+
ignore-setters = false
56+
fail-under = 95
57+
exclude = ["setup.py", "docs", "build"]
58+
ignore-regex = ["^get$", "^mock_.*", ".*BaseClass.*"]
59+
verbose = 0
60+
quiet = false
61+
whitelist-regex = []
62+
color = true
63+
generate-badge = "."
64+
badge-format = "svg"
65+
66+
[tool.isort]
67+
atomic = true
68+
profile = "black"
69+
line_length = 130
70+
skip_gitignore = true
71+
2472
[build-system]
2573
requires = ["poetry-core>=1.0.0"]
2674
build-backend = "poetry.core.masonry.api"

0 commit comments

Comments
 (0)