Skip to content

Commit 70f0e90

Browse files
authored
Merge pull request #168 from ral-facilities/DSEGOG-383-Update-Poetry.toml-for-noxfile.py
Dsegog 383 update poetry.toml for noxfile.py
2 parents 07d9de3 + 7c45fb7 commit 70f0e90

File tree

7 files changed

+963
-522
lines changed

7 files changed

+963
-522
lines changed

.github/ci_requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
poetry==1.3.2
2-
nox==2022.1.7
1+
poetry==2.1.1
2+
nox==2025.2.9

.github/workflows/ci.yml

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ jobs:
1717
- name: Checkout OperationsGateway API
1818
uses: actions/checkout@v3
1919

20+
- name: Setup Python
21+
uses: actions/setup-python@v4
22+
with:
23+
python-version: "3.11"
24+
2025
# Install dependencies of python-ldap
2126
- name: Install python-ldap dependencies
2227
run: |
2328
sudo apt-get update
2429
sudo apt-get install -y libsasl2-dev libldap2-dev libssl-dev
2530
26-
# Setup Python and environment dependencies (via cache)
27-
- name: Setup Python
28-
uses: actions/setup-python@v4
29-
with:
30-
python-version: 3.11
3131
- name: Load Pip cache
3232
uses: actions/cache@v3
3333
with:
@@ -51,7 +51,6 @@ jobs:
5151
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
5252
sudo apt-get update
5353
sudo apt-get install -y mongodb-database-tools
54-
5554
# Read the database name from the config file and store it in an environment variable
5655
- name: Get database name from ci_config.yml
5756
run: echo "DATABASE_NAME=$(grep database_name .github/ci_config.yml | cut -d ':' -f 2 | tr -d '[:space:]')" >> $GITHUB_ENV
@@ -60,10 +59,8 @@ jobs:
6059
- name: Load Poetry cache
6160
uses: actions/cache@v3
6261
with:
63-
path: ~/.cache/pypoetry/virtualenvs
64-
key: ${{ runner.os }}-poetry-3.11-${{ env.pythonLocation }}-${{ hashFiles('poetry.lock') }}
65-
- name: Install dependencies
66-
run: poetry install --without simulated-data
62+
path: ~/.cache/pypoetry
63+
key: ${{ runner.os }}-poetry-3.11-${{ hashFiles('poetry.lock') }}
6764

6865
# Setup minio and create bucket
6966
- name: Setup minio
@@ -78,11 +75,12 @@ jobs:
7875
export AWS_SECRET_ACCESS_KEY=minioadmin
7976
export AWS_EC2_METADATA_DISABLED=true
8077
aws --endpoint-url http://127.0.0.1:9000/ s3 mb s3://og-actions-test
81-
8278
- name: Move CI config.yml to correct place
8379
run: cp .github/ci_config.yml operationsgateway_api/config.yml
80+
8481
- name: Setup logging configuration
8582
run: cp operationsgateway_api/logging.ini.example operationsgateway_api/logging.ini
83+
8684
- name: Create log file
8785
run: touch "$GITHUB_WORKSPACE/logs.log"
8886

@@ -116,21 +114,15 @@ jobs:
116114
- name: Run Echo Ingest script
117115
run: poetry run python util/realistic_data/ingest_echo_data.py
118116

119-
- name: Load Poetry cache for Nox tests session
120-
uses: actions/cache@v3
121-
with:
122-
path: /home/runner/work/operationsgateway-api/operationsgateway-api/.nox/tests*
123-
key: ${{ runner.os }}-poetry-nox-tests-3.11-${{ env.pythonLocation }}-${{ hashFiles('poetry.lock') }}
124-
125117
# Run Nox tests session, saves and uploads a coverage report to codecov
126118
- name: Run Nox tests session
127-
run: nox -p 3.11 -s tests -- --cov=operationsgateway_api --cov-report=xml
119+
run: nox -s tests -- --cov=operationsgateway_api --cov-report=xml
120+
128121
- name: Upload code coverage report
129122
uses: codecov/codecov-action@v3
130123
with:
131124
token: ${{ secrets.CODECOV_TOKEN }}
132125

133-
134126
linting:
135127
runs-on: ubuntu-latest
136128
name: Code Linting
@@ -139,6 +131,7 @@ jobs:
139131
uses: actions/setup-python@v4
140132
with:
141133
python-version: "3.11"
134+
142135
- name: Checkout OperationsGateway API
143136
uses: actions/checkout@v3
144137

@@ -150,24 +143,24 @@ jobs:
150143
- name: Install Poetry & Nox
151144
run: pip install -r .github/ci_requirements.txt
152145

153-
- name: Load Poetry cache for Nox lint session
146+
- name: Load Poetry cache
154147
uses: actions/cache@v3
155148
with:
156-
path: /home/runner/work/operationsgateway-api/operationsgateway-api/.nox/lint*
157-
key: ${{ runner.os }}-poetry-nox-lint-3.11-${{ env.pythonLocation }}-${{ hashFiles('poetry.lock') }}
149+
path: ~/.cache/pypoetry
150+
key: poetry-${{ runner.os }}-3.11-${{ hashFiles('poetry.lock') }}
158151

159152
- name: Run Nox lint session
160153
run: nox -s lint
161154

162-
163155
formatting:
164156
runs-on: ubuntu-latest
165-
name: Code Formatting
157+
name: Black Formatting
166158
steps:
167159
- name: Setup Python
168160
uses: actions/setup-python@v4
169161
with:
170162
python-version: "3.11"
163+
171164
- name: Checkout OperationsGateway API
172165
uses: actions/checkout@v3
173166

@@ -179,16 +172,15 @@ jobs:
179172
- name: Install Poetry & Nox
180173
run: pip install -r .github/ci_requirements.txt
181174

182-
- name: Load Poetry cache for Nox black session
175+
- name: Load Poetry cache
183176
uses: actions/cache@v3
184177
with:
185-
path: /home/runner/work/operationsgateway-api/operationsgateway-api/.nox/black*
186-
key: ${{ runner.os }}-poetry-nox-black-3.11-${{ env.pythonLocation }}-${{ hashFiles('poetry.lock') }}
178+
path: ~/.cache/pypoetry
179+
key: poetry-${{ runner.os }}-3.11-${{ hashFiles('poetry.lock') }}
187180

188181
- name: Run Nox black session
189182
run: nox -s black -- --check --diff
190183

191-
192184
safety:
193185
runs-on: ubuntu-latest
194186
name: Dependency Safety
@@ -197,6 +189,7 @@ jobs:
197189
uses: actions/setup-python@v4
198190
with:
199191
python-version: "3.11"
192+
200193
- name: Checkout OperationsGateway API
201194
uses: actions/checkout@v3
202195

@@ -208,11 +201,11 @@ jobs:
208201
- name: Install Poetry & Nox
209202
run: pip install -r .github/ci_requirements.txt
210203

211-
- name: Load Poetry cache for Nox safety session
204+
- name: Load Poetry cache
212205
uses: actions/cache@v3
213206
with:
214-
path: /home/runner/work/operationsgateway-api/operationsgateway-api/.nox/safety*
215-
key: ${{ runner.os }}-poetry-nox-safety-3.11-${{ env.pythonLocation }}-${{ hashFiles('poetry.lock') }}
207+
path: ~/.cache/pypoetry
208+
key: poetry-${{ runner.os }}-3.11-${{ hashFiles('poetry.lock') }}
216209

217-
- name: Run Nox safety session
218-
run: nox -s safety
210+
- name: Run Safety Scan
211+
run: nox -s safety

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ venv/
55
*.log.*
66
config.yml*
77
!config.yml.example
8+
schedulued_maintenance.yml*
9+
!schedulued_maintenance.yml.example
10+
maintenance.yml*
11+
!maintenance.yml.example
812
.vscode/
913
.nox/
1014
.python-version

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ The following instructions will detail how to get a development instance up and
1313
## Prerequisites
1414

1515
1) Local development on Windows or Mac can be challenging because they lack the Linux libraries required by the API. Since the API is designed to run in a Python 3.11 environment on a Rocky 9 Linux machine, it’s best to use a Rocky 9 development VM to closely mirror the production setup.
16-
2) One of the dependencies used in this API [(`epac-data-sim`)](https://github.com/CentralLaserFacility/EPAC-DataSim) is a private repository, so the appropriate permissions and SSH keys need to be set up. Guidance for setting up SSH keys for the Rocky 9 VM can be found [here](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent?platform=linux).
16+
2) You'll need a developer bucket in echo to use the test script to prefill echo, and a local database, with data.
17+
3) One of the dependencies used in this API [(`epac-data-sim`)](https://github.com/CentralLaserFacility/EPAC-DataSim) is a private repository, so the appropriate permissions and SSH keys need to be set up. Guidance for setting up SSH keys for the Rocky 9 VM can be found [here](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent?platform=linux).
1718

1819

1920
## Environment Setup
@@ -45,6 +46,8 @@ cd operationsgateway-api
4546
poetry env use python3.11
4647
# Install the dependencies
4748
poetry install
49+
# If you don't need the simulated data, or don't have access to the epac repo...
50+
poetry install --without simulated-data
4851
```
4952

5053
### MongoDB:

noxfile.py

Lines changed: 29 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,48 @@
1-
import os
2-
import tempfile
3-
41
import nox
5-
from nox.command import CommandFailed
62

7-
# Separating Black away from the rest of the sessions
8-
nox.options.sessions = "lint", "safety", "tests"
3+
nox.options.sessions = "black", "lint", "safety", "tests"
94
code_locations = "operationsgateway_api", "test", "noxfile.py", "util"
105

116

12-
def install_with_constraints(session, *args, **kwargs):
13-
# Auto file deletion is turned off to prevent a PermissionError experienced on
14-
# Windows
15-
with tempfile.NamedTemporaryFile(delete=False) as requirements:
16-
session.run(
17-
"poetry",
18-
"export",
19-
"--dev",
20-
"--format=requirements.txt",
21-
"--without-hashes",
22-
f"--output={requirements.name}",
23-
external=True,
24-
)
25-
# Using sed to remove extras from the constraints file, thereby fixing the
26-
# error "Constraints cannot have extras". A better solution might be to
27-
# use `poetry install --only dev` but this requires a newer version of
28-
# Poetry than we're using in our development environments. See
29-
# https://github.com/jazzband/pip-tools/issues/1300#issuecomment-818122483
30-
# for more info relating to the sed solution
31-
sed_expression = r"s/\[.*\]//g"
32-
try:
33-
session.run("sed", "-i", sed_expression, requirements.name, external=True)
34-
except CommandFailed:
35-
# Try running the Mac version of the command
36-
session.run(
37-
"sed",
38-
"-i",
39-
"",
40-
sed_expression,
41-
requirements.name,
42-
external=True,
43-
)
44-
45-
session.install(f"--constraint={requirements.name}", *args, **kwargs)
46-
47-
try:
48-
# Due to delete=False, the file must be deleted manually
49-
requirements.close()
50-
os.unlink(requirements.name)
51-
except IOError:
52-
session.log("Error: The temporary requirements file could not be closed")
53-
54-
55-
@nox.session(reuse_venv=True)
7+
@nox.session(python=False)
568
def black(session):
9+
# Use Poetry’s existing virtual environment
10+
session.env["POETRY_VIRTUALENVS_CREATE"] = "false"
11+
# The dependencies should already be installed,
12+
# but explicitly setting in case they're not
13+
session.run("poetry", "install", "--without", "simulated-data", external=True)
5714
args = session.posargs
58-
install_with_constraints(session, "black")
59-
session.run("black", *code_locations, *args, external=True)
15+
session.run("poetry", "run", "black", *code_locations, *args, external=True)
6016

6117

62-
@nox.session(reuse_venv=True)
18+
@nox.session(python=False)
6319
def lint(session):
6420
args = session.posargs or code_locations
65-
install_with_constraints(
66-
session,
67-
"flake8",
68-
"flake8-black",
69-
"flake8-broken-line",
70-
"flake8-bugbear",
71-
"flake8-builtins",
72-
"flake8-commas",
73-
"flake8-comprehensions",
74-
"flake8-import-order",
75-
"flake8-logging-format",
76-
"pep8-naming",
77-
)
78-
session.run("flake8", *args)
21+
session.env["POETRY_VIRTUALENVS_CREATE"] = "false"
22+
session.run("poetry", "install", "--without", "simulated-data", external=True)
23+
session.run("poetry", "run", "flake8", *args, external=True)
7924

8025

81-
@nox.session(reuse_venv=True)
26+
@nox.session(python=False)
8227
def safety(session):
83-
install_with_constraints(session, "safety")
84-
with tempfile.NamedTemporaryFile(delete=False) as requirements:
85-
session.run(
86-
"poetry",
87-
"export",
88-
"--dev",
89-
"--format=requirements.txt",
90-
"--without-hashes",
91-
f"--output={requirements.name}",
92-
external=True,
93-
)
94-
session.run(
95-
"safety",
96-
"check",
97-
f"--file={requirements.name}",
98-
"--full-report",
99-
# jinja2 report widely disputed as not valid, no fix available:
100-
# https://github.com/pallets/jinja/issues/1994
101-
"--ignore",
102-
"70612",
103-
)
104-
105-
try:
106-
# Due to delete=False, the file must be deleted manually
107-
requirements.close()
108-
os.unlink(requirements.name)
109-
except IOError:
110-
session.log("Error: The temporary requirements file could not be closed")
28+
session.env["POETRY_VIRTUALENVS_CREATE"] = "false"
29+
session.run("poetry", "install", "--without", "simulated-data", external=True)
30+
# Can't fix 70790 because epac data sim uses it
31+
session.run(
32+
"poetry",
33+
"run",
34+
"safety",
35+
"check",
36+
"--full-report",
37+
"--ignore",
38+
"70790",
39+
external=True,
40+
)
11141

11242

113-
@nox.session(python=["3.11"], reuse_venv=True)
43+
@nox.session(python=False)
11444
def tests(session):
11545
args = session.posargs
46+
session.env["POETRY_VIRTUALENVS_CREATE"] = "false"
11647
session.run("poetry", "install", "--without", "simulated-data", external=True)
117-
session.run("pytest", *args)
48+
session.run("poetry", "run", "pytest", *args, external=True)

0 commit comments

Comments
 (0)