Skip to content

Commit

Permalink
#165: Allow tmp directory to be configured for nox sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Dec 8, 2020
1 parent 3682165 commit 0366d08
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ machine.
To start, install [pyenv](https://github.com/pyenv/pyenv). There is a Windows version of
this tool ([pyenv-win](https://github.com/pyenv-win/pyenv-win)), however this is
currently untested on this repo. This is used to manage the various versions of Python
that will be used to test/lint Python during development. Install by executing the following:
that will be used to test/lint Python during development. Install by executing the
following:

```bash
curl https://pyenv.run | bash
Expand Down Expand Up @@ -154,6 +155,21 @@ Currently, the following Nox sessions have been created:
dependencies (pulled directly from Poetry) for any known vulnerabilities. This session
gives the output in a full ASCII style report.

Each Nox session builds an environment using the repo's dependencies (defined using
Poetry) using `install_with_constraints()`. This stores the dependencies in a
`requirements.txt`-like format temporarily during this process, using the OS' default
temporary location. This could result in permissions issues (this has been seen by a
colleague on Windows), so adding the `--tmpdir [DIRECTORY PATH]` allows the user to
define where this file should be stored. Due to Nox session being initiated in the
command line, this argument needs to be a positional argument (denoted by the `--` in
the Nox command). This argument is optional, but **must** be the final argument avoid
interference with Nox's argument parsing. An example:

```bash
nox -s lint -- util datagateway_api --tmpdir /root
```


### Pre Commit (Automated Checks during Git Commit)
To make use of Git's ability to run custom hooks, [pre-commit](https://pre-commit.com/)
is used. Like Nox, Pip is used to install this tool:
Expand Down
2 changes: 1 addition & 1 deletion datagateway_api/src/swagger/initialise_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def initialise_spec(spec):
"lt": {"value": [{"id": {"lt": 10}}]},
"lte": {"value": [{"id": {"lte": 50}}]},
"gt": {"value": [{"id": {"gt": 10}}]},
"gte": {"value": [{"id": {"gte": 50}}]},
"gte": {"value": [{"id": {"gte": 50}}]},
"in": {"value": [{"id": {"in": [1, 2, 3]}}]},
},
},
Expand Down
32 changes: 26 additions & 6 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import nox
import tempfile

import nox


# Separating Black away from the rest of the sessions
nox.options.sessions = "lint", "safety"
code_locations = "datagateway_api", "test", "util", "noxfile.py"


def install_with_constraints(session, *args, **kwargs):
with tempfile.NamedTemporaryFile() as requirements:
def install_with_constraints(session, req_dir=None, *args, **kwargs):
with tempfile.NamedTemporaryFile(dir=req_dir) as requirements:
session.run(
"poetry",
"export",
Expand All @@ -19,18 +21,35 @@ def install_with_constraints(session, *args, **kwargs):
session.install(f"--constraint={requirements.name}", *args, **kwargs)


def get_tmp_dir(session):
tmp_dir = None

try:
if session.posargs[-2] == "--tmpdir":
tmp_dir = session.posargs.pop(-1)
session.posargs.remove("--tmpdir")
except IndexError:
session.log("Info: No --tmpdir option given")

return tmp_dir


@nox.session(python="3.6", reuse_venv=True)
def format(session):
tmp_dir = get_tmp_dir(session)
args = session.posargs or code_locations
install_with_constraints(session, "black")

install_with_constraints(session, tmp_dir, "black")
session.run("black", *args, external=True)


@nox.session(python="3.6", reuse_venv=True)
def lint(session):
tmp_dir = get_tmp_dir(session)
args = session.posargs or code_locations
install_with_constraints(
session,
tmp_dir,
"flake8",
"flake8-bandit",
"flake8-black",
Expand All @@ -48,8 +67,9 @@ def lint(session):

@nox.session(python="3.6", reuse_venv=True)
def safety(session):
install_with_constraints(session, "safety")
with tempfile.NamedTemporaryFile() as requirements:
tmp_dir = get_tmp_dir(session)
install_with_constraints(session, tmp_dir, "safety")
with tempfile.NamedTemporaryFile(dir=tmp_dir) as requirements:
session.run(
"poetry",
"export",
Expand Down

0 comments on commit 0366d08

Please sign in to comment.