Skip to content

Commit

Permalink
Merge branch 'feature/add-code-linting-#165' into feature/fix-code-li…
Browse files Browse the repository at this point in the history
…nting-#184
  • Loading branch information
MRichards99 authored Dec 18, 2020
2 parents 1013e82 + ed448ca commit 36dfeb0
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import tempfile

import nox
Expand All @@ -7,8 +8,10 @@
code_locations = "datagateway_api", "test", "util", "noxfile.py"


def install_with_constraints(session, req_dir=None, *args, **kwargs):
with tempfile.NamedTemporaryFile(dir=req_dir) as requirements:
def install_with_constraints(session, *args, **kwargs):
# Auto file deletion is turned off to prevent a PermissionError experienced on
# Windows
with tempfile.NamedTemporaryFile(delete=False) as requirements:
session.run(
"poetry",
"export",
Expand All @@ -19,36 +22,27 @@ def install_with_constraints(session, req_dir=None, *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
try:
# Due to delete=False, the file must be deleted manually
requirements.close()
os.unlink(requirements.name)
except IOError:
session.log("Error: The temporary requirements file could not be closed")


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

install_with_constraints(session, tmp_dir, "black")
install_with_constraints(session, "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 @@ -66,9 +60,8 @@ def lint(session):

@nox.session(python="3.6", reuse_venv=True)
def safety(session):
tmp_dir = get_tmp_dir(session)
install_with_constraints(session, tmp_dir, "safety")
with tempfile.NamedTemporaryFile(dir=tmp_dir) as requirements:
install_with_constraints(session, "safety")
with tempfile.NamedTemporaryFile(delete=False) as requirements:
session.run(
"poetry",
"export",
Expand All @@ -79,3 +72,10 @@ def safety(session):
external=True,
)
session.run("safety", "check", f"--file={requirements.name}", "--full-report")

try:
# Due to delete=False, the file must be deleted manually
requirements.close()
os.unlink(requirements.name)
except IOError:
session.log("Error: The temporary requirements file could not be closed")

0 comments on commit 36dfeb0

Please sign in to comment.