Skip to content

Commit f76557f

Browse files
committed
Start Python formatting review with pre-commit and isort
This patch draws on Cyber Domain Ontology deployments from the noted PRs. References: * casework/CASE-Utilities-Python#37 * Cyber-Domain-Ontology/CDO-Shapes-Example#1 Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
1 parent 45a2974 commit f76557f

File tree

5 files changed

+81
-4
lines changed

5 files changed

+81
-4
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ jobs:
3737
sudo apt update
3838
sudo apt install --yes libxml2-utils
3939
40+
- name: Pre-commit Checks
41+
run: |
42+
pip -q install pre-commit
43+
pre-commit run --all-files
44+
4045
- name: Make check
4146
run: make check
4247

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ python/demo.dfxml
1111
.pytest_cache
1212
*.egg-info
1313
*.log
14+
.venv-pre-commit

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
repos:
2+
- repo: https://github.com/pycqa/isort
3+
rev: 5.13.2
4+
hooks:
5+
- id: isort
6+
name: isort (python)

Makefile

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@ ifeq ($(shell basename $(SHELL)),sh)
1818
SHELL := $(shell which /bin/bash 2>/dev/null || which /usr/local/bin/bash)
1919
endif
2020

21-
all:
21+
PYTHON3 ?= python3
22+
ifeq ($(PYTHON3),)
23+
$(error python3 not found)
24+
endif
25+
26+
all: \
27+
.venv-pre-commit/var/.pre-commit-built.log
2228

2329
.PHONY: \
2430
check-mypy \
25-
check-supply-chain
31+
check-supply-chain \
32+
check-supply-chain-pre-commit
2633

2734
.git_submodule_init.done.log: .gitmodules
2835
# Confirm dfxml_schema has been checked out at least once.
@@ -31,28 +38,86 @@ all:
3138
test -r dependencies/dfxml_schema/dfxml.xsd
3239
touch $@
3340

41+
# This virtual environment is meant to be built once and then persist, even through 'make clean'.
42+
# If a recipe is written to remove this flag file, it should first run `pre-commit uninstall`.
43+
.venv-pre-commit/var/.pre-commit-built.log:
44+
rm -rf .venv-pre-commit
45+
test -r .pre-commit-config.yaml \
46+
|| (echo "ERROR:Makefile:pre-commit is expected to install for this repository, but .pre-commit-config.yaml does not seem to exist." >&2 ; exit 1)
47+
$(PYTHON3) -m venv \
48+
.venv-pre-commit
49+
source .venv-pre-commit/bin/activate \
50+
&& pip install \
51+
--upgrade \
52+
pip \
53+
setuptools \
54+
wheel
55+
source .venv-pre-commit/bin/activate \
56+
&& pip install \
57+
pre-commit
58+
source .venv-pre-commit/bin/activate \
59+
&& pre-commit install
60+
mkdir -p \
61+
.venv-pre-commit/var
62+
touch $@
63+
3464
clean:
3565
find . -name '*~' -exec rm {} \;
3666
$(MAKE) \
3767
--directory tests \
3868
clean
3969

4070
check: \
41-
check-mypy
71+
.git_submodule_init.done.log \
72+
.venv-pre-commit/var/.pre-commit-built.log
4273
$(MAKE) \
74+
PYTHON3=$(PYTHON3) \
4375
SHELL=$(SHELL) \
4476
--directory tests \
4577
check
4678

4779
check-mypy: \
4880
.git_submodule_init.done.log
4981
$(MAKE) \
82+
PYTHON3=$(PYTHON3) \
5083
SHELL=$(SHELL) \
5184
--directory tests \
5285
check-mypy
5386

5487
check-supply-chain: \
88+
check-supply-chain-pre-commit \
5589
check-mypy
5690

91+
# Update pre-commit configuration and use the updated config file to
92+
# review code. Only have Make exit if 'pre-commit run' modifies files.
93+
check-supply-chain-pre-commit: \
94+
.venv-pre-commit/var/.pre-commit-built.log
95+
source .venv-pre-commit/bin/activate \
96+
&& pre-commit autoupdate
97+
git diff \
98+
--exit-code \
99+
.pre-commit-config.yaml \
100+
|| ( \
101+
source .venv-pre-commit/bin/activate \
102+
&& pre-commit run \
103+
--all-files \
104+
--config .pre-commit-config.yaml \
105+
) \
106+
|| git diff \
107+
--stat \
108+
--exit-code \
109+
|| ( \
110+
echo \
111+
"WARNING:Makefile:pre-commit configuration can be updated. It appears the updated would change file formatting." \
112+
>&2 \
113+
; exit 1 \
114+
)
115+
@git diff \
116+
--exit-code \
117+
.pre-commit-config.yaml \
118+
|| echo \
119+
"INFO:Makefile:pre-commit configuration can be updated. It appears the update would not change file formatting." \
120+
>&2
121+
57122
check-tools:
58123
(cd tests/misc_object_tests;make check)

tests/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ endif
1818

1919
top_srcdir := $(shell cd .. ; pwd)
2020

21-
PYTHON3 ?= $(shell which python3.9 2>/dev/null || which python3.8 2>/dev/null || which python3.7 2>/dev/null || which python3.6 2>/dev/null || which python3)
21+
PYTHON3 ?= python3
2222
ifeq ($(PYTHON3),)
2323
$(error python3 not found)
2424
endif

0 commit comments

Comments
 (0)