Skip to content
This repository was archived by the owner on Mar 13, 2024. It is now read-only.

Commit 3e11d1b

Browse files
committed
Merge branch 'review-nov22' of github.com:DiamondLightSource/python3-pip-skeleton into review-nov22
2 parents 7530dd9 + d0a8b23 commit 3e11d1b

File tree

9 files changed

+139
-103
lines changed

9 files changed

+139
-103
lines changed

.devcontainer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"dockerfile": "Dockerfile",
66
"target": "build",
77
"context": ".",
8-
"args": {}
8+
"args": {
9+
"PIP_OPTIONS": "-e .[dev]"
10+
}
911
},
1012
"remoteEnv": {
1113
"DISPLAY": "${localEnv:DISPLAY}"
@@ -24,6 +26,7 @@
2426
"initializeCommand": "bash -c 'for i in $HOME/.inputrc; do [ -f $i ] || touch $i; done'",
2527
"runArgs": [
2628
"--net=host",
29+
"--security-opt=label=type:container_runtime_t",
2730
"-v=${localEnv:HOME}/.ssh:/root/.ssh",
2831
"-v=${localEnv:HOME}/.inputrc:/root/.inputrc"
2932
],
@@ -33,7 +36,5 @@
3336
],
3437
// make the workspace folder the same inside and outside of the container
3538
"workspaceMount": "source=${localWorkspaceFolder},target=${localWorkspaceFolder},type=bind",
36-
"workspaceFolder": "${localWorkspaceFolder}",
37-
// After the container is created, install the python project in editable form
38-
"postCreateCommand": "pip install $([ -f requirements_dev.txt ] && echo -r requirements_dev.txt ) -e .[dev]"
39+
"workspaceFolder": "${localWorkspaceFolder}"
3940
}

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Ignore folders that can be large.
2+
# This saves time copying them into the context at the start of a build.
13
build/
24
.mypy_cache
35
.tox
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Install requirements
2+
description: Run pip install with requirements and upload resulting requirements
3+
inputs:
4+
requirements_file:
5+
description: Name of requirements file to use and upload
6+
required: true
7+
install_options:
8+
description: Parameters to pass to pip install
9+
required: true
10+
python_version:
11+
description: Python version to install
12+
default: "3.x"
13+
14+
runs:
15+
using: composite
16+
17+
steps:
18+
- name: Setup python
19+
uses: actions/setup-python@v4
20+
with:
21+
python-version: ${{ inputs.python_version }}
22+
23+
- name: Pip install
24+
run: |
25+
touch ${{ inputs.requirements_file }}
26+
# -c uses requirements.txt as constraints, see 'Validate requirements file'
27+
pip install -c ${{ inputs.requirements_file }} ${{ inputs.install_options }}
28+
shell: bash
29+
30+
- name: Create lockfile
31+
run: |
32+
mkdir -p lockfiles
33+
pip freeze --exclude-editable > lockfiles/${{ inputs.requirements_file }}
34+
# delete the self referencing line
35+
sed -i '/file:/d' lockfiles/${{ inputs.requirements_file }}
36+
shell: bash
37+
38+
- name: Upload lockfiles
39+
uses: actions/upload-artifact@v3
40+
with:
41+
name: lockfiles
42+
path: lockfiles
43+
44+
# This eliminates the class of problems where the requirements being given no
45+
# longer match what the packages themselves dictate. E.g. In the rare instance
46+
# where I install some-package which used to depend on vulnerable-dependency
47+
# but now uses good-dependency (despite being nominally the same version)
48+
# pip will install both if given a requirements file with -r
49+
- name: If requirements file exists, check it matches pip installed packages
50+
run: |
51+
if [ -s ${{ inputs.requirements_file }} ]; then
52+
if ! diff -u ${{ inputs.requirements_file }} lockfiles/${{ inputs.requirements_file }}; then
53+
echo "Error: ${{ inputs.requirements_file }} need the above changes to be exhaustive"
54+
exit 1
55+
fi
56+
fi
57+
shell: bash
58+

.github/workflows/code.yml

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
schedule:
77
# Run every Monday at 8am to check latest versions of dependencies
88
- cron: "0 8 * * WED"
9+
env:
10+
# The target python version, which must match the Dockerfile version
11+
CONTAINER_PYTHON: "3.11"
912

1013
jobs:
1114
lint:
@@ -17,29 +20,28 @@ jobs:
1720
- name: Checkout
1821
uses: actions/checkout@v3
1922

20-
- name: Setup python
21-
uses: actions/setup-python@v4
23+
- name: Install python packages
24+
uses: ./.github/actions/install_requirements
2225
with:
23-
python-version: "3.x"
26+
requirements_file: requirements-dev-3.x.txt
27+
install_options: -e .[dev]
2428

2529
- name: Lint
26-
run: |
27-
.github/workflows/pip_install.sh lint -e .[dev]
28-
tox -e pre-commit,mypy
29-
30-
- name: Upload lockfiles
31-
uses: actions/upload-artifact@v3
32-
with:
33-
name: lockfiles
34-
path: lockfiles
30+
run: tox -e pre-commit,mypy
3531

3632
test:
3733
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.repository
3834
strategy:
3935
fail-fast: false
4036
matrix:
4137
os: ["ubuntu-latest"] # can add windows-latest, macos-latest
42-
python: ["3.8", "3.9", "3.10", "3.11"]
38+
python: ["3.9", "3.10", "3.11"]
39+
install: ["-e .[dev]"]
40+
# Make one version be non-editable to test both paths of version code
41+
include:
42+
- os: "ubuntu-latest"
43+
python: "3.8"
44+
install: ".[dev]"
4345

4446
runs-on: ${{ matrix.os }}
4547
env:
@@ -55,62 +57,61 @@ jobs:
5557
- name: Checkout
5658
uses: actions/checkout@v3
5759
with:
60+
# Need this to get version number from last tag
5861
fetch-depth: 0
5962

60-
- name: Setup python ${{ matrix.python }}
61-
uses: actions/setup-python@v4
63+
- name: Install python packages
64+
uses: ./.github/actions/install_requirements
6265
with:
63-
python-version: ${{ matrix.python }}
64-
65-
- name: Install with latest dependencies
66-
run: .github/workflows/pip_install.sh test-${{ matrix.python }}-${{ matrix.os }} .[dev]
66+
python_version: ${{ matrix.python }}
67+
requirements_file: requirements-test-${{ matrix.os }}-${{ matrix.python }}.txt
68+
install_options: ${{ matrix.install }}
6769

6870
- name: Run tests
69-
run: pytest tests
71+
run: pytest
7072

7173
- name: Upload coverage to Codecov
7274
uses: codecov/codecov-action@v3
7375
with:
7476
name: ${{ matrix.python }}/${{ matrix.os }}
7577
files: cov.xml
7678

77-
- name: Upload lockfiles
78-
uses: actions/upload-artifact@v3
79-
with:
80-
name: lockfiles
81-
path: lockfiles
82-
8379
dist:
8480
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.repository
8581
runs-on: "ubuntu-latest"
8682

8783
steps:
88-
- name: Checkout Source
84+
- name: Checkout
8985
uses: actions/checkout@v3
9086
with:
87+
# Need this to get version number from last tag
9188
fetch-depth: 0
9289

93-
- name: Build Sdist and wheel
90+
- name: Build sdist and wheel
9491
run: |
9592
export SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct) && \
9693
pipx run build
9794
98-
- name: Test module --version works using the installed wheel
99-
run: |
100-
touch requirements.txt
101-
pip install -r requirements.txt dist/*.whl
102-
# If more than one module in src/ replace with module name to test
103-
python -m $(ls src | head -1) --version
104-
105-
- name: Check for packaging errors
106-
run: pipx run twine check dist/*
107-
10895
- name: Upload sdist and wheel as artifacts
10996
uses: actions/upload-artifact@v3
11097
with:
11198
name: dist
11299
path: dist
113100

101+
- name: Check for packaging errors
102+
run: pipx run twine check dist/*
103+
104+
- name: Install python packages
105+
uses: ./.github/actions/install_requirements
106+
with:
107+
python_version: ${{env.CONTAINER_PYTHON}}
108+
requirements_file: requirements.txt
109+
install_options: dist/*.whl
110+
111+
- name: Test module --version works using the installed wheel
112+
# If more than one module in src/ replace with module name to test
113+
run: python -m $(ls src | head -1) --version
114+
114115
container:
115116
needs: [lint, dist, test]
116117
runs-on: ubuntu-latest
@@ -124,11 +125,11 @@ jobs:
124125
uses: actions/checkout@v3
125126

126127
# image names must be all lower case
127-
- run: |
128-
echo IMAGE_REPOSITORY=ghcr.io/$(tr '[:upper:]' '[:lower:]' <<< "${{ github.repository }}") >> $GITHUB_ENV
128+
- name: Generate image repo name
129+
run: echo IMAGE_REPOSITORY=ghcr.io/$(tr '[:upper:]' '[:lower:]' <<< "${{ github.repository }}") >> $GITHUB_ENV
129130

130-
# obtain the python wheel from the dist step
131-
- uses: actions/download-artifact@v3
131+
- name: Download wheel and lockfiles
132+
uses: actions/download-artifact@v3
132133

133134
- name: Log in to GitHub Docker Registry
134135
if: github.event_name != 'pull_request'
@@ -154,6 +155,8 @@ jobs:
154155
- name: Build runtime image
155156
uses: docker/build-push-action@v3
156157
with:
158+
build-args: |
159+
PIP_OPTIONS=-r lockfiles/requirements.txt dist/*.whl
157160
push: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags') }}
158161
load: ${{ ! (github.event_name == 'push' && startsWith(github.ref, 'refs/tags')) }}
159162
tags: ${{ steps.meta.outputs.tags }}
@@ -162,17 +165,7 @@ jobs:
162165
cache-to: type=gha,mode=max
163166

164167
- name: Test cli works in runtime image
165-
# check that the latest tag can run with --version parameter
166-
run: |
167-
docker run ${{ env.IMAGE_REPOSITORY }} --version
168-
mkdir -p lockfiles
169-
docker run --entrypoint pip ${{ env.IMAGE_REPOSITORY }} freeze > lockfiles/requirements.txt
170-
171-
- name: Upload lockfiles
172-
uses: actions/upload-artifact@v3
173-
with:
174-
name: lockfiles
175-
path: lockfiles
168+
run: docker run ${{ env.IMAGE_REPOSITORY }} --version
176169

177170
release:
178171
# upload to PyPI and make a release on every tag
@@ -183,6 +176,10 @@ jobs:
183176
steps:
184177
- uses: actions/download-artifact@v3
185178

179+
- name: Fixup blank lockfiles
180+
# Github release artifacts can't be blank
181+
run: for f in lockfiles/*; [ -s $f ] || echo '# No requirements' >> $f; done
182+
186183
- name: Github Release
187184
# We pin to the SHA, not the tag, for security reasons.
188185
# https://docs.github.com/en/actions/learn-github-actions/security-hardening-for-github-actions#using-third-party-actions

.github/workflows/docs.yml

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,28 @@ on:
77
jobs:
88
docs:
99
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.repository
10-
strategy:
11-
fail-fast: false
12-
matrix:
13-
python: ["3.10"]
14-
1510
runs-on: ubuntu-latest
1611

1712
steps:
1813
- name: Avoid git conflicts when tag and branch pushed at same time
1914
if: startsWith(github.ref, 'refs/tags')
2015
run: sleep 60
2116

22-
- name: Install python version
23-
uses: actions/setup-python@v4
17+
- name: Checkout
18+
uses: actions/checkout@v3
2419
with:
25-
python-version: ${{ matrix.python }}
20+
# Need this to get version number from last tag
21+
fetch-depth: 0
2622

27-
- name: Install Packages
23+
- name: Install system packages
2824
# Can delete this if you don't use graphviz in your docs
2925
run: sudo apt-get install graphviz
3026

31-
- name: checkout
32-
uses: actions/checkout@v3
27+
- name: Install python packages
28+
uses: ./.github/actions/install_requirements
3329
with:
34-
fetch-depth: 0
35-
36-
- name: Install dependencies
37-
run: |
38-
touch requirements_dev.txt
39-
pip install -r requirements_dev.txt -e .[dev]
30+
requirements_file: requirements-dev-3.x.txt
31+
install_options: -e .[dev]
4032

4133
- name: Build docs
4234
run: tox -e docs

.github/workflows/docs_clean.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
runs-on: ubuntu-latest
1818

1919
steps:
20-
- name: checkout
20+
- name: Checkout
2121
uses: actions/checkout@v3
2222
with:
2323
ref: gh-pages
@@ -35,7 +35,7 @@ jobs:
3535

3636
- name: update index and push changes
3737
run: |
38-
rm -r ${{ env.DOCS_VERSION }}
38+
rm -r $DOCS_VERSION
3939
python make_switcher.py --remove $DOCS_VERSION ${{ github.repository }} switcher.json
4040
git config --global user.name 'GitHub Actions Docs Cleanup CI'
4141
git config --global user.email '[email protected]'

.github/workflows/pip_install.sh

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)