-
Notifications
You must be signed in to change notification settings - Fork 7
/
ci.py
158 lines (124 loc) · 4.22 KB
/
ci.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
"""Continuous integration (CI) support
To execute all checks and tests in a CI script, use::
pykern ci run
:copyright: Copyright (c) 2022 RadiaSoft LLC. All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
from pykern import pkcli
from pykern import pkio
from pykern import pksetup
from pykern import pkunit
from pykern.pkcollections import PKDict
from pykern.pkdebug import pkdp, pkdlog
import re
#: static/js/ext is where Sirepo stores 3rd party library files
_CHECK_FILES = PKDict(
check_eof_newline=PKDict(
exclude_files=re.compile(r"/static/js/ext/|node_modules/|^run/|^tests/|^venv/"),
include_files=re.compile(r"\.(html|jinja|js|json|md|py|tsx|yml)$"),
),
check_main=PKDict(
exclude_files=re.compile(r".*(_console\.py)|^tests/|^venv/|^pyproject.toml$"),
include_files=re.compile(r".*(\.py)$"),
),
check_prints=PKDict(
exclude_files=re.compile(
rf".*(?:{pkunit.DATA_DIR_SUFFIX}|{pkunit.WORK_DIR_SUFFIX})/"
+ rf"|^\w+/{pksetup.PACKAGE_DATA}/"
+ r"|pkdebug[^/]*\.py$"
+ r"|(?:^|/)\."
+ r"|^run/"
+ r"|^venv/"
+ r"|^pyproject.toml$"
),
include_files=re.compile(r".py$"),
),
)
_MAIN = re.compile(r"^if.*__name__")
_PRINT = re.compile(r"(?:\s|^)(?:pkdp|print)\(")
_PRINT_OK = re.compile(r"^\s*#\s*(?:pkdp|print)\(")
def check_eof_newline():
"""Recursively check repo for files missing newline at end of file.
Checks html, jinja, js, json, md, py, tsx, and yml files.
Excludes external files, tests, and run directory.
"""
def _c(lines):
return [] if lines[-1] == "" else [""]
_check_files("check_eof_newline", _c)
def check_main():
"""Recursively check repo for modules with main programs.
Checks .py files.
Excludes ``<project>_console.py``, tests, and venv.
"""
def _c(lines):
r = []
for j, l in enumerate(lines, start=1):
if re.search(_MAIN, l):
r.append(f":{j} {l}")
return r
_check_files("check_main", _c)
def check_prints():
"""Recursively check repo for (naked) print and pkdp calls.
Checks .py files, excluding hidden files, pkdebug module, test data/work, run directory,
package_data and venv.
See the
`DesignHints <https://github.com/radiasoft/pykern/wiki/DesignHints#output-for-programmers-logging>_`
wiki for an explanation of why this check is necessary.
If you really need a print, use `pykern.pkconst.builtin_print`.
"""
def _c(lines):
r = []
for j, l in enumerate(lines, start=1):
if re.search(_PRINT, l) and not re.search(_PRINT_OK, l):
r.append(f":{j} {l}")
return r
_check_files("check_prints", _c)
def run():
"""Run the continuous integration checks and tests:
#. Runs `check_prints`
#. Checks formatting
#. Runs `pykern.pkcli.test.default_command`
"""
from pykern.pkcli import fmt, test
from pykern import pkio
check_eof_newline()
check_main()
check_prints()
pkdlog(
"Checking fmt diff. If a diff is printed below, you can fix this failure by running `pykern fmt run .` from the repo root."
)
fmt.diff(*_paths(pkio.py_path()))
test.default_command()
def _check_files(case, check_file):
def _error(m):
pkcli.command_error("{}: {}", case, m)
d = _CHECK_FILES[case]
r = []
n = 0
c = pkio.py_path()
for p in _paths(c):
for f in pkio.walk_tree(p, d.include_files):
f = c.bestrelpath(f)
if re.search(d.exclude_files, f):
continue
n += 1
for l in check_file(pkio.read_text(f).split("\n")):
r.append(f"{f}{l}")
if n == 0:
_error("no files found")
if r:
_error("\n".join(r))
def _paths(cwd):
def _exists(*names):
return list(filter(lambda n: cwd.join(n).isfile(), names))
if _exists("README.rst", "README.md") and (
(x := _exists("setup.py")) or _exists("pyproject.toml")
):
return (
# POSIT: repo name
cwd.basename,
"tests",
*x,
)
return (cwd,)