apdev 0.1.5__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {apdev-0.1.5 → apdev-0.2.0}/PKG-INFO +1 -1
- {apdev-0.1.5 → apdev-0.2.0}/pyproject.toml +2 -2
- {apdev-0.1.5 → apdev-0.2.0}/src/apdev/__init__.py +1 -1
- apdev-0.2.0/src/apdev/charsets/base.json +44 -0
- apdev-0.2.0/src/apdev/charsets/chinese.json +9 -0
- apdev-0.2.0/src/apdev/charsets/japanese.json +11 -0
- apdev-0.2.0/src/apdev/charsets/korean.json +11 -0
- apdev-0.2.0/src/apdev/check_chars.py +296 -0
- {apdev-0.1.5 → apdev-0.2.0}/src/apdev/check_imports.py +38 -3
- {apdev-0.1.5 → apdev-0.2.0}/src/apdev/cli.py +41 -1
- {apdev-0.1.5 → apdev-0.2.0}/src/apdev/release.sh +57 -12
- {apdev-0.1.5 → apdev-0.2.0}/src/apdev.egg-info/PKG-INFO +1 -1
- {apdev-0.1.5 → apdev-0.2.0}/src/apdev.egg-info/SOURCES.txt +4 -0
- apdev-0.2.0/tests/test_check_chars.py +315 -0
- apdev-0.2.0/tests/test_check_imports.py +182 -0
- {apdev-0.1.5 → apdev-0.2.0}/tests/test_cli.py +51 -0
- apdev-0.1.5/src/apdev/check_chars.py +0 -79
- apdev-0.1.5/tests/test_check_chars.py +0 -80
- apdev-0.1.5/tests/test_check_imports.py +0 -103
- {apdev-0.1.5 → apdev-0.2.0}/README.md +0 -0
- {apdev-0.1.5 → apdev-0.2.0}/setup.cfg +0 -0
- {apdev-0.1.5 → apdev-0.2.0}/src/apdev/__main__.py +0 -0
- {apdev-0.1.5 → apdev-0.2.0}/src/apdev/config.py +0 -0
- {apdev-0.1.5 → apdev-0.2.0}/src/apdev/py.typed +0 -0
- {apdev-0.1.5 → apdev-0.2.0}/src/apdev.egg-info/dependency_links.txt +0 -0
- {apdev-0.1.5 → apdev-0.2.0}/src/apdev.egg-info/entry_points.txt +0 -0
- {apdev-0.1.5 → apdev-0.2.0}/src/apdev.egg-info/requires.txt +0 -0
- {apdev-0.1.5 → apdev-0.2.0}/src/apdev.egg-info/top_level.txt +0 -0
- {apdev-0.1.5 → apdev-0.2.0}/tests/test_config.py +0 -0
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "apdev"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.2.0"
|
|
8
8
|
description = "Shared development tools for Python projects - character validation, circular import detection, and more"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = {text = "Apache-2.0"}
|
|
@@ -43,7 +43,7 @@ dev = [
|
|
|
43
43
|
where = ["src"]
|
|
44
44
|
|
|
45
45
|
[tool.setuptools.package-data]
|
|
46
|
-
apdev = ["release.sh"]
|
|
46
|
+
apdev = ["release.sh", "charsets/*.json"]
|
|
47
47
|
|
|
48
48
|
[tool.pytest.ini_options]
|
|
49
49
|
testpaths = ["tests"]
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "base",
|
|
3
|
+
"description": "Default: ASCII + emoji + technical symbols",
|
|
4
|
+
"emoji_ranges": [
|
|
5
|
+
{ "start": "0x1F300", "end": "0x1F5FF", "name": "Symbols and Pictographs" },
|
|
6
|
+
{ "start": "0x1F600", "end": "0x1F64F", "name": "Emoticons" },
|
|
7
|
+
{ "start": "0x1F680", "end": "0x1F6FF", "name": "Transport and Map Symbols" },
|
|
8
|
+
{ "start": "0x1F780", "end": "0x1F7FF", "name": "Geometric Shapes Extended" },
|
|
9
|
+
{ "start": "0x1F900", "end": "0x1F9FF", "name": "Supplemental Symbols and Pictographs" },
|
|
10
|
+
{ "start": "0x2600", "end": "0x26FF", "name": "Miscellaneous Symbols" },
|
|
11
|
+
{ "start": "0x2700", "end": "0x27BF", "name": "Dingbats" }
|
|
12
|
+
],
|
|
13
|
+
"extra_ranges": [
|
|
14
|
+
{ "start": "0x0080", "end": "0x00FF", "name": "Latin-1 Supplement" },
|
|
15
|
+
{ "start": "0x2000", "end": "0x206F", "name": "General Punctuation" },
|
|
16
|
+
{ "start": "0x2100", "end": "0x214F", "name": "Letterlike Symbols" },
|
|
17
|
+
{ "start": "0x2190", "end": "0x21FF", "name": "Arrows" },
|
|
18
|
+
{ "start": "0x2200", "end": "0x22FF", "name": "Mathematical Operators" },
|
|
19
|
+
{ "start": "0x2300", "end": "0x23FF", "name": "Miscellaneous Technical" },
|
|
20
|
+
{ "start": "0x2500", "end": "0x257F", "name": "Box Drawing" },
|
|
21
|
+
{ "start": "0x2580", "end": "0x259F", "name": "Block Elements" },
|
|
22
|
+
{ "start": "0x25A0", "end": "0x25FF", "name": "Geometric Shapes" },
|
|
23
|
+
{ "start": "0x2800", "end": "0x28FF", "name": "Braille Patterns" },
|
|
24
|
+
{ "start": "0x2B00", "end": "0x2BFF", "name": "Miscellaneous Symbols and Arrows" },
|
|
25
|
+
{ "start": "0xFE00", "end": "0xFE0F", "name": "Variation Selectors" }
|
|
26
|
+
],
|
|
27
|
+
"dangerous": [
|
|
28
|
+
{ "code": "0x202A", "name": "LEFT-TO-RIGHT EMBEDDING" },
|
|
29
|
+
{ "code": "0x202B", "name": "RIGHT-TO-LEFT EMBEDDING" },
|
|
30
|
+
{ "code": "0x202C", "name": "POP DIRECTIONAL FORMATTING" },
|
|
31
|
+
{ "code": "0x202D", "name": "LEFT-TO-RIGHT OVERRIDE" },
|
|
32
|
+
{ "code": "0x202E", "name": "RIGHT-TO-LEFT OVERRIDE" },
|
|
33
|
+
{ "code": "0x2066", "name": "LEFT-TO-RIGHT ISOLATE" },
|
|
34
|
+
{ "code": "0x2067", "name": "RIGHT-TO-LEFT ISOLATE" },
|
|
35
|
+
{ "code": "0x2068", "name": "FIRST STRONG ISOLATE" },
|
|
36
|
+
{ "code": "0x2069", "name": "POP DIRECTIONAL ISOLATE" },
|
|
37
|
+
{ "code": "0x200B", "name": "ZERO WIDTH SPACE" },
|
|
38
|
+
{ "code": "0x200C", "name": "ZERO WIDTH NON-JOINER" },
|
|
39
|
+
{ "code": "0x200D", "name": "ZERO WIDTH JOINER" },
|
|
40
|
+
{ "code": "0x200E", "name": "LEFT-TO-RIGHT MARK" },
|
|
41
|
+
{ "code": "0x200F", "name": "RIGHT-TO-LEFT MARK" },
|
|
42
|
+
{ "code": "0x2060", "name": "WORD JOINER" }
|
|
43
|
+
]
|
|
44
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "chinese",
|
|
3
|
+
"description": "Chinese: CJK Unified Ideographs, CJK Punctuation, Fullwidth Forms",
|
|
4
|
+
"extra_ranges": [
|
|
5
|
+
{ "start": "0x3000", "end": "0x303F", "name": "CJK Symbols and Punctuation" },
|
|
6
|
+
{ "start": "0x4E00", "end": "0x9FFF", "name": "CJK Unified Ideographs" },
|
|
7
|
+
{ "start": "0xFF00", "end": "0xFFEF", "name": "Fullwidth Forms" }
|
|
8
|
+
]
|
|
9
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "japanese",
|
|
3
|
+
"description": "Japanese: Hiragana, Katakana, CJK Unified Ideographs, CJK Punctuation, Fullwidth Forms",
|
|
4
|
+
"extra_ranges": [
|
|
5
|
+
{ "start": "0x3000", "end": "0x303F", "name": "CJK Symbols and Punctuation" },
|
|
6
|
+
{ "start": "0x3040", "end": "0x309F", "name": "Hiragana" },
|
|
7
|
+
{ "start": "0x30A0", "end": "0x30FF", "name": "Katakana" },
|
|
8
|
+
{ "start": "0x4E00", "end": "0x9FFF", "name": "CJK Unified Ideographs" },
|
|
9
|
+
{ "start": "0xFF00", "end": "0xFFEF", "name": "Fullwidth Forms" }
|
|
10
|
+
]
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "korean",
|
|
3
|
+
"description": "Korean: Hangul Jamo, Hangul Syllables, CJK Unified Ideographs, CJK Punctuation, Fullwidth Forms",
|
|
4
|
+
"extra_ranges": [
|
|
5
|
+
{ "start": "0x1100", "end": "0x11FF", "name": "Hangul Jamo" },
|
|
6
|
+
{ "start": "0x3000", "end": "0x303F", "name": "CJK Symbols and Punctuation" },
|
|
7
|
+
{ "start": "0x4E00", "end": "0x9FFF", "name": "CJK Unified Ideographs" },
|
|
8
|
+
{ "start": "0xAC00", "end": "0xD7AF", "name": "Hangul Syllables" },
|
|
9
|
+
{ "start": "0xFF00", "end": "0xFFEF", "name": "Fullwidth Forms" }
|
|
10
|
+
]
|
|
11
|
+
}
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
"""Character validation tool.
|
|
2
|
+
|
|
3
|
+
Checks that files contain only allowed characters: ASCII, common emoji,
|
|
4
|
+
and standard technical symbols (arrows, box-drawing, math operators, etc.).
|
|
5
|
+
|
|
6
|
+
Additionally flags dangerous invisible/bidi characters in code regions
|
|
7
|
+
(Trojan Source - CVE-2021-42574) while allowing them in comments.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import importlib.resources
|
|
13
|
+
import json
|
|
14
|
+
import os
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def load_charset(name_or_path: str) -> dict:
|
|
19
|
+
"""Load a charset definition by preset name or file path.
|
|
20
|
+
|
|
21
|
+
If name_or_path contains a path separator or ends with .json,
|
|
22
|
+
it is treated as a file path. Otherwise it is looked up from
|
|
23
|
+
the bundled charsets/ directory.
|
|
24
|
+
"""
|
|
25
|
+
if os.sep in name_or_path or name_or_path.endswith(".json"):
|
|
26
|
+
p = Path(name_or_path)
|
|
27
|
+
if not p.is_file():
|
|
28
|
+
raise FileNotFoundError(f"Charset file not found: {name_or_path}")
|
|
29
|
+
return json.loads(p.read_text(encoding="utf-8"))
|
|
30
|
+
|
|
31
|
+
ref = importlib.resources.files("apdev").joinpath("charsets", f"{name_or_path}.json")
|
|
32
|
+
try:
|
|
33
|
+
text = ref.read_text(encoding="utf-8")
|
|
34
|
+
except Exception as exc:
|
|
35
|
+
raise FileNotFoundError(f"Unknown charset: {name_or_path}") from exc
|
|
36
|
+
return json.loads(text)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _parse_ranges(entries: list[dict]) -> list[tuple[int, int]]:
|
|
40
|
+
"""Convert JSON range entries to (start, end) tuples."""
|
|
41
|
+
return [(int(e["start"], 16), int(e["end"], 16)) for e in entries]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _parse_dangerous(entries: list[dict]) -> dict[int, str]:
|
|
45
|
+
"""Convert JSON dangerous entries to {code: name} dict."""
|
|
46
|
+
return {int(e["code"], 16): e["name"] for e in entries}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def resolve_charsets(
|
|
50
|
+
charset_names: list[str],
|
|
51
|
+
charset_files: list[str],
|
|
52
|
+
) -> tuple[list[tuple[int, int]], dict[int, str]]:
|
|
53
|
+
"""Load base charset and merge any additional charsets.
|
|
54
|
+
|
|
55
|
+
Returns (all_ranges, dangerous_codepoints).
|
|
56
|
+
"""
|
|
57
|
+
base = load_charset("base")
|
|
58
|
+
ranges_set: set[tuple[int, int]] = set()
|
|
59
|
+
ranges_set.update(_parse_ranges(base.get("emoji_ranges", [])))
|
|
60
|
+
ranges_set.update(_parse_ranges(base.get("extra_ranges", [])))
|
|
61
|
+
dangerous = _parse_dangerous(base.get("dangerous", []))
|
|
62
|
+
|
|
63
|
+
for name in charset_names:
|
|
64
|
+
data = load_charset(name)
|
|
65
|
+
ranges_set.update(_parse_ranges(data.get("emoji_ranges", [])))
|
|
66
|
+
ranges_set.update(_parse_ranges(data.get("extra_ranges", [])))
|
|
67
|
+
dangerous.update(_parse_dangerous(data.get("dangerous", [])))
|
|
68
|
+
|
|
69
|
+
for path in charset_files:
|
|
70
|
+
data = load_charset(path)
|
|
71
|
+
ranges_set.update(_parse_ranges(data.get("emoji_ranges", [])))
|
|
72
|
+
ranges_set.update(_parse_ranges(data.get("extra_ranges", [])))
|
|
73
|
+
dangerous.update(_parse_dangerous(data.get("dangerous", [])))
|
|
74
|
+
|
|
75
|
+
return sorted(ranges_set), dangerous
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _is_in_ranges(c: str, ranges: list[tuple[int, int]]) -> bool:
|
|
79
|
+
"""Return True if the character is in any of the given ranges or ASCII."""
|
|
80
|
+
code = ord(c)
|
|
81
|
+
if code <= 127:
|
|
82
|
+
return True
|
|
83
|
+
for start, end in ranges:
|
|
84
|
+
if start <= code <= end:
|
|
85
|
+
return True
|
|
86
|
+
return False
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
# Lazy-loaded base charset defaults for backward-compatible public API
|
|
90
|
+
_BASE_RANGES: list[tuple[int, int]] | None = None
|
|
91
|
+
_BASE_DANGEROUS: dict[int, str] | None = None
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _get_base_defaults() -> tuple[list[tuple[int, int]], dict[int, str]]:
|
|
95
|
+
global _BASE_RANGES, _BASE_DANGEROUS
|
|
96
|
+
if _BASE_RANGES is None or _BASE_DANGEROUS is None:
|
|
97
|
+
_BASE_RANGES, _BASE_DANGEROUS = resolve_charsets([], [])
|
|
98
|
+
return _BASE_RANGES, _BASE_DANGEROUS
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def is_allowed_char(c: str) -> bool:
|
|
102
|
+
"""Return True if the character is in the base allowed set.
|
|
103
|
+
|
|
104
|
+
Dangerous codepoints (Trojan Source vectors) are excluded even though
|
|
105
|
+
they fall within allowed Unicode ranges.
|
|
106
|
+
"""
|
|
107
|
+
ranges, dangerous = _get_base_defaults()
|
|
108
|
+
if ord(c) in dangerous:
|
|
109
|
+
return False
|
|
110
|
+
return _is_in_ranges(c, ranges)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def is_dangerous_char(c: str) -> bool:
|
|
114
|
+
"""Return True if the character is a dangerous invisible/bidi codepoint."""
|
|
115
|
+
_, dangerous = _get_base_defaults()
|
|
116
|
+
return ord(c) in dangerous
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
_PYTHON_SUFFIXES = {".py"}
|
|
120
|
+
_JS_SUFFIXES = {".js", ".ts", ".tsx", ".jsx", ".mjs", ".cjs"}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _compute_comment_mask(content: str, suffix: str) -> set[int]:
|
|
124
|
+
"""Return the set of character indices that are within comments.
|
|
125
|
+
|
|
126
|
+
Uses a simple state machine that tracks string literals to avoid
|
|
127
|
+
treating ``#`` / ``//`` inside strings as comment starts.
|
|
128
|
+
"""
|
|
129
|
+
if suffix in _PYTHON_SUFFIXES:
|
|
130
|
+
return _compute_comment_mask_python(content)
|
|
131
|
+
if suffix in _JS_SUFFIXES:
|
|
132
|
+
return _compute_comment_mask_js(content)
|
|
133
|
+
return set()
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _compute_comment_mask_python(content: str) -> set[int]:
|
|
137
|
+
mask: set[int] = set()
|
|
138
|
+
i = 0
|
|
139
|
+
n = len(content)
|
|
140
|
+
|
|
141
|
+
while i < n:
|
|
142
|
+
# Triple-quoted strings (""" or ''')
|
|
143
|
+
if i + 2 < n and content[i : i + 3] in ('"""', "'''"):
|
|
144
|
+
quote = content[i : i + 3]
|
|
145
|
+
i += 3
|
|
146
|
+
while i < n:
|
|
147
|
+
if content[i] == "\\" and i + 1 < n:
|
|
148
|
+
i += 2
|
|
149
|
+
continue
|
|
150
|
+
if i + 2 < n and content[i : i + 3] == quote:
|
|
151
|
+
i += 3
|
|
152
|
+
break
|
|
153
|
+
i += 1
|
|
154
|
+
continue
|
|
155
|
+
|
|
156
|
+
# Single / double quoted strings
|
|
157
|
+
if content[i] in ('"', "'"):
|
|
158
|
+
quote_char = content[i]
|
|
159
|
+
i += 1
|
|
160
|
+
while i < n and content[i] != "\n":
|
|
161
|
+
if content[i] == "\\" and i + 1 < n:
|
|
162
|
+
i += 2
|
|
163
|
+
continue
|
|
164
|
+
if content[i] == quote_char:
|
|
165
|
+
i += 1
|
|
166
|
+
break
|
|
167
|
+
i += 1
|
|
168
|
+
continue
|
|
169
|
+
|
|
170
|
+
# Line comment
|
|
171
|
+
if content[i] == "#":
|
|
172
|
+
while i < n and content[i] != "\n":
|
|
173
|
+
mask.add(i)
|
|
174
|
+
i += 1
|
|
175
|
+
continue
|
|
176
|
+
|
|
177
|
+
i += 1
|
|
178
|
+
|
|
179
|
+
return mask
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _compute_comment_mask_js(content: str) -> set[int]:
|
|
183
|
+
mask: set[int] = set()
|
|
184
|
+
i = 0
|
|
185
|
+
n = len(content)
|
|
186
|
+
|
|
187
|
+
while i < n:
|
|
188
|
+
# Template literal
|
|
189
|
+
if content[i] == "`":
|
|
190
|
+
i += 1
|
|
191
|
+
while i < n:
|
|
192
|
+
if content[i] == "\\" and i + 1 < n:
|
|
193
|
+
i += 2
|
|
194
|
+
continue
|
|
195
|
+
if content[i] == "`":
|
|
196
|
+
i += 1
|
|
197
|
+
break
|
|
198
|
+
i += 1
|
|
199
|
+
continue
|
|
200
|
+
|
|
201
|
+
# Single / double quoted strings
|
|
202
|
+
if content[i] in ('"', "'"):
|
|
203
|
+
quote_char = content[i]
|
|
204
|
+
i += 1
|
|
205
|
+
while i < n and content[i] != "\n":
|
|
206
|
+
if content[i] == "\\" and i + 1 < n:
|
|
207
|
+
i += 2
|
|
208
|
+
continue
|
|
209
|
+
if content[i] == quote_char:
|
|
210
|
+
i += 1
|
|
211
|
+
break
|
|
212
|
+
i += 1
|
|
213
|
+
continue
|
|
214
|
+
|
|
215
|
+
# Line comment
|
|
216
|
+
if i + 1 < n and content[i : i + 2] == "//":
|
|
217
|
+
while i < n and content[i] != "\n":
|
|
218
|
+
mask.add(i)
|
|
219
|
+
i += 1
|
|
220
|
+
continue
|
|
221
|
+
|
|
222
|
+
# Block comment
|
|
223
|
+
if i + 1 < n and content[i : i + 2] == "/*":
|
|
224
|
+
while i < n:
|
|
225
|
+
if i + 1 < n and content[i : i + 2] == "*/":
|
|
226
|
+
mask.add(i)
|
|
227
|
+
mask.add(i + 1)
|
|
228
|
+
i += 2
|
|
229
|
+
break
|
|
230
|
+
mask.add(i)
|
|
231
|
+
i += 1
|
|
232
|
+
continue
|
|
233
|
+
|
|
234
|
+
i += 1
|
|
235
|
+
|
|
236
|
+
return mask
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def check_file(
|
|
240
|
+
path: Path,
|
|
241
|
+
*,
|
|
242
|
+
max_problems: int = 5,
|
|
243
|
+
extra_ranges: list[tuple[int, int]] | None = None,
|
|
244
|
+
dangerous: dict[int, str] | None = None,
|
|
245
|
+
) -> list[str]:
|
|
246
|
+
"""Check a single file for illegal characters.
|
|
247
|
+
|
|
248
|
+
Returns a list of problem descriptions (empty if the file is clean).
|
|
249
|
+
"""
|
|
250
|
+
problems: list[str] = []
|
|
251
|
+
if extra_ranges is None or dangerous is None:
|
|
252
|
+
default_ranges, default_dangerous = _get_base_defaults()
|
|
253
|
+
if extra_ranges is None:
|
|
254
|
+
extra_ranges = default_ranges
|
|
255
|
+
if dangerous is None:
|
|
256
|
+
dangerous = default_dangerous
|
|
257
|
+
try:
|
|
258
|
+
content = path.read_text(encoding="utf-8")
|
|
259
|
+
suffix = path.suffix.lower()
|
|
260
|
+
comment_mask = _compute_comment_mask(content, suffix)
|
|
261
|
+
for i, char in enumerate(content):
|
|
262
|
+
if ord(char) in dangerous:
|
|
263
|
+
if i not in comment_mask:
|
|
264
|
+
code = ord(char)
|
|
265
|
+
name = dangerous[code]
|
|
266
|
+
problems.append(
|
|
267
|
+
f"Dangerous character in code at position {i + 1}: "
|
|
268
|
+
f"U+{code:04X} ({name})"
|
|
269
|
+
)
|
|
270
|
+
elif not _is_in_ranges(char, extra_ranges):
|
|
271
|
+
problems.append(
|
|
272
|
+
f"Illegal character at position {i + 1}: " f"{char!r} (U+{ord(char):04X})"
|
|
273
|
+
)
|
|
274
|
+
if len(problems) >= max_problems:
|
|
275
|
+
break
|
|
276
|
+
except Exception as e:
|
|
277
|
+
problems.append(f"Failed to read file: {e}")
|
|
278
|
+
return problems
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def check_paths(
|
|
282
|
+
paths: list[Path],
|
|
283
|
+
*,
|
|
284
|
+
extra_ranges: list[tuple[int, int]] | None = None,
|
|
285
|
+
dangerous: dict[int, str] | None = None,
|
|
286
|
+
) -> int:
|
|
287
|
+
"""Check multiple files. Returns 0 if all clean, 1 if any have problems."""
|
|
288
|
+
has_error = False
|
|
289
|
+
for path in paths:
|
|
290
|
+
problems = check_file(path, extra_ranges=extra_ranges, dangerous=dangerous)
|
|
291
|
+
if problems:
|
|
292
|
+
has_error = True
|
|
293
|
+
print(f"\n{path} contains illegal characters:")
|
|
294
|
+
for p in problems:
|
|
295
|
+
print(f" {p}")
|
|
296
|
+
return 1 if has_error else 0
|
|
@@ -15,15 +15,46 @@ from pathlib import Path
|
|
|
15
15
|
class ImportAnalyzer(ast.NodeVisitor):
|
|
16
16
|
"""Collect fully-qualified imports from a single Python file."""
|
|
17
17
|
|
|
18
|
-
def __init__(
|
|
18
|
+
def __init__(
|
|
19
|
+
self,
|
|
20
|
+
current_module: str = "",
|
|
21
|
+
is_package: bool = False,
|
|
22
|
+
) -> None:
|
|
19
23
|
self.imports: set[str] = set()
|
|
24
|
+
self._current_module = current_module
|
|
25
|
+
self._is_package = is_package
|
|
20
26
|
|
|
21
27
|
def visit_Import(self, node: ast.Import) -> None:
|
|
22
28
|
for alias in node.names:
|
|
23
29
|
self.imports.add(alias.name)
|
|
24
30
|
|
|
25
31
|
def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
|
|
26
|
-
if node.
|
|
32
|
+
if node.level > 0 and self._current_module:
|
|
33
|
+
# Relative import – resolve against current module/package.
|
|
34
|
+
parts = self._current_module.split(".")
|
|
35
|
+
# For a package (__init__.py) the package is the module itself;
|
|
36
|
+
# for a regular module the package is the parent.
|
|
37
|
+
if self._is_package:
|
|
38
|
+
levels_to_strip = node.level - 1
|
|
39
|
+
else:
|
|
40
|
+
levels_to_strip = node.level
|
|
41
|
+
|
|
42
|
+
if levels_to_strip > 0 and levels_to_strip < len(parts):
|
|
43
|
+
base_parts = parts[:-levels_to_strip]
|
|
44
|
+
elif levels_to_strip == 0:
|
|
45
|
+
base_parts = list(parts)
|
|
46
|
+
else:
|
|
47
|
+
base_parts = []
|
|
48
|
+
|
|
49
|
+
if node.module:
|
|
50
|
+
resolved = ".".join(base_parts + node.module.split("."))
|
|
51
|
+
self.imports.add(resolved)
|
|
52
|
+
else:
|
|
53
|
+
# ``from . import foo, bar`` – each name is a sub-module.
|
|
54
|
+
for alias in node.names:
|
|
55
|
+
resolved = ".".join(base_parts + [alias.name])
|
|
56
|
+
self.imports.add(resolved)
|
|
57
|
+
elif node.module:
|
|
27
58
|
self.imports.add(node.module)
|
|
28
59
|
|
|
29
60
|
|
|
@@ -70,7 +101,11 @@ def build_dependency_graph(
|
|
|
70
101
|
print(f"Warning: could not parse {py_file}: {e}", file=sys.stderr)
|
|
71
102
|
continue
|
|
72
103
|
|
|
73
|
-
|
|
104
|
+
is_package = py_file.name == "__init__.py"
|
|
105
|
+
analyzer = ImportAnalyzer(
|
|
106
|
+
current_module=module_name,
|
|
107
|
+
is_package=is_package,
|
|
108
|
+
)
|
|
74
109
|
analyzer.visit(tree)
|
|
75
110
|
|
|
76
111
|
deps = _resolve_imports(analyzer.imports, base_package)
|
|
@@ -44,6 +44,19 @@ def _build_parser() -> argparse.ArgumentParser:
|
|
|
44
44
|
type=Path,
|
|
45
45
|
help="Files to check",
|
|
46
46
|
)
|
|
47
|
+
chars_parser.add_argument(
|
|
48
|
+
"--charset",
|
|
49
|
+
action="append",
|
|
50
|
+
default=[],
|
|
51
|
+
help="Extra charset preset to enable (repeatable, e.g. --charset chinese)",
|
|
52
|
+
)
|
|
53
|
+
chars_parser.add_argument(
|
|
54
|
+
"--charset-file",
|
|
55
|
+
action="append",
|
|
56
|
+
default=[],
|
|
57
|
+
dest="charset_files",
|
|
58
|
+
help="Path to custom charset JSON file (repeatable)",
|
|
59
|
+
)
|
|
47
60
|
|
|
48
61
|
# check-imports
|
|
49
62
|
imports_parser = subparsers.add_parser(
|
|
@@ -66,6 +79,12 @@ def _build_parser() -> argparse.ArgumentParser:
|
|
|
66
79
|
"release",
|
|
67
80
|
help="Interactive release automation (build, tag, GitHub release, PyPI upload)",
|
|
68
81
|
)
|
|
82
|
+
release_parser.add_argument(
|
|
83
|
+
"--yes",
|
|
84
|
+
"-y",
|
|
85
|
+
action="store_true",
|
|
86
|
+
help="Auto-accept all defaults (silent mode)",
|
|
87
|
+
)
|
|
69
88
|
release_parser.add_argument(
|
|
70
89
|
"version",
|
|
71
90
|
nargs="?",
|
|
@@ -85,7 +104,26 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
85
104
|
return 0
|
|
86
105
|
|
|
87
106
|
if args.command == "check-chars":
|
|
88
|
-
|
|
107
|
+
from apdev.check_chars import resolve_charsets
|
|
108
|
+
|
|
109
|
+
charset_names = list(args.charset)
|
|
110
|
+
charset_files = list(args.charset_files)
|
|
111
|
+
|
|
112
|
+
# Fall back to APDEV_EXTRA_CHARS env var if no CLI args
|
|
113
|
+
if not charset_names and not charset_files:
|
|
114
|
+
env_val = os.environ.get("APDEV_EXTRA_CHARS", "")
|
|
115
|
+
if env_val:
|
|
116
|
+
for item in env_val.split(","):
|
|
117
|
+
item = item.strip()
|
|
118
|
+
if not item:
|
|
119
|
+
continue
|
|
120
|
+
if os.sep in item or item.endswith(".json"):
|
|
121
|
+
charset_files.append(item)
|
|
122
|
+
else:
|
|
123
|
+
charset_names.append(item)
|
|
124
|
+
|
|
125
|
+
extra_ranges, dangerous = resolve_charsets(charset_names, charset_files)
|
|
126
|
+
return check_paths(args.files, extra_ranges=extra_ranges, dangerous=dangerous)
|
|
89
127
|
|
|
90
128
|
if args.command == "check-imports":
|
|
91
129
|
config = load_config()
|
|
@@ -108,6 +146,8 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
108
146
|
print("Error: release.sh not found in package", file=sys.stderr)
|
|
109
147
|
return 1
|
|
110
148
|
cmd = ["bash", str(script)]
|
|
149
|
+
if args.yes:
|
|
150
|
+
cmd.append("--yes")
|
|
111
151
|
if args.version:
|
|
112
152
|
cmd.append(args.version)
|
|
113
153
|
result = subprocess.run(cmd, env={**os.environ})
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
#!/bin/bash
|
|
2
2
|
# Interactive release script for Python projects
|
|
3
|
-
# Usage: ./release.sh [version]
|
|
3
|
+
# Usage: ./release.sh [--yes|-y] [version]
|
|
4
4
|
# Example: ./release.sh 0.2.0
|
|
5
|
+
# ./release.sh --yes # silent mode, auto-accept defaults
|
|
6
|
+
# ./release.sh --yes 0.2.0 # silent mode with specific version
|
|
5
7
|
#
|
|
6
8
|
# Auto-detects project name from pyproject.toml and GitHub repo from git remote.
|
|
7
9
|
# Override with environment variables: PROJECT_NAME, PACKAGE_NAME, GITHUB_REPO
|
|
@@ -9,6 +11,17 @@
|
|
|
9
11
|
# Note: set -e is disabled to allow step-by-step execution
|
|
10
12
|
# Individual steps will handle their own error handling
|
|
11
13
|
|
|
14
|
+
# Silent mode flag (--yes or -y to auto-accept all defaults)
|
|
15
|
+
SILENT=false
|
|
16
|
+
POSITIONAL_ARGS=()
|
|
17
|
+
for arg in "$@"; do
|
|
18
|
+
case "$arg" in
|
|
19
|
+
--yes|-y) SILENT=true ;;
|
|
20
|
+
*) POSITIONAL_ARGS+=("$arg") ;;
|
|
21
|
+
esac
|
|
22
|
+
done
|
|
23
|
+
set -- "${POSITIONAL_ARGS[@]}"
|
|
24
|
+
|
|
12
25
|
# Colors for output
|
|
13
26
|
RED='\033[0;31m'
|
|
14
27
|
GREEN='\033[0;32m'
|
|
@@ -98,13 +111,24 @@ ask_yn() {
|
|
|
98
111
|
local prompt="$1"
|
|
99
112
|
local default="$2"
|
|
100
113
|
local answer
|
|
101
|
-
|
|
114
|
+
|
|
115
|
+
# Silent mode: auto-accept default
|
|
116
|
+
if [ "$SILENT" = true ]; then
|
|
117
|
+
if [ "$default" = "y" ]; then
|
|
118
|
+
echo -e "${YELLOW}${prompt} [Y/n] y (auto)${NC}"
|
|
119
|
+
return 0
|
|
120
|
+
else
|
|
121
|
+
echo -e "${YELLOW}${prompt} [y/N] n (auto)${NC}"
|
|
122
|
+
return 1
|
|
123
|
+
fi
|
|
124
|
+
fi
|
|
125
|
+
|
|
102
126
|
if [ "$default" = "y" ]; then
|
|
103
127
|
prompt="${prompt} [Y/n]"
|
|
104
128
|
else
|
|
105
129
|
prompt="${prompt} [y/N]"
|
|
106
130
|
fi
|
|
107
|
-
|
|
131
|
+
|
|
108
132
|
read -p "$(echo -e "${YELLOW}${prompt}${NC}") " answer
|
|
109
133
|
answer=${answer:-$default}
|
|
110
134
|
[[ $answer =~ ^[Yy]$ ]]
|
|
@@ -139,12 +163,19 @@ ask_menu() {
|
|
|
139
163
|
# Function to show main menu with all steps
|
|
140
164
|
# Returns selection via global variable MENU_SELECTION
|
|
141
165
|
show_main_menu() {
|
|
166
|
+
# Silent mode: auto-select "all"
|
|
167
|
+
if [ "$SILENT" = true ]; then
|
|
168
|
+
echo -e "${CYAN}Silent mode: auto-selecting all steps${NC}"
|
|
169
|
+
MENU_SELECTION="all"
|
|
170
|
+
return
|
|
171
|
+
fi
|
|
172
|
+
|
|
142
173
|
echo ""
|
|
143
174
|
echo -e "${CYAN}╔══════════════════════════════════════════════════════════╗${NC}"
|
|
144
175
|
echo -e "${CYAN}║ Release Steps Selection ║${NC}"
|
|
145
176
|
echo -e "${CYAN}╚══════════════════════════════════════════════════════════╝${NC}"
|
|
146
177
|
echo ""
|
|
147
|
-
|
|
178
|
+
|
|
148
179
|
# Display menu options directly
|
|
149
180
|
echo -e "${CYAN}Select a step to execute:${NC}"
|
|
150
181
|
echo -e " ${CYAN}all) Execute all steps (with interactive prompts)${NC}"
|
|
@@ -159,7 +190,7 @@ show_main_menu() {
|
|
|
159
190
|
echo -e " ${CYAN}9) Show Summary${NC}"
|
|
160
191
|
echo -e " ${CYAN}0) Exit${NC}"
|
|
161
192
|
echo ""
|
|
162
|
-
|
|
193
|
+
|
|
163
194
|
# Read user input directly (not captured)
|
|
164
195
|
read -p "$(echo -e "${YELLOW}Select option [all]:${NC} ") " MENU_SELECTION
|
|
165
196
|
MENU_SELECTION=${MENU_SELECTION:-all}
|
|
@@ -763,19 +794,33 @@ while true; do
|
|
|
763
794
|
echo -e "${CYAN}Executing all steps with interactive prompts...${NC}"
|
|
764
795
|
echo ""
|
|
765
796
|
|
|
797
|
+
# Helper: run a step, abort in silent mode on failure
|
|
798
|
+
run_step() {
|
|
799
|
+
local step_name="$1"
|
|
800
|
+
shift
|
|
801
|
+
if ! "$@"; then
|
|
802
|
+
if [ "$SILENT" = true ]; then
|
|
803
|
+
echo -e "${RED}${step_name} failed. Aborting (silent mode).${NC}"
|
|
804
|
+
exit 1
|
|
805
|
+
else
|
|
806
|
+
echo -e "${YELLOW}${step_name} failed, continuing...${NC}"
|
|
807
|
+
fi
|
|
808
|
+
fi
|
|
809
|
+
}
|
|
810
|
+
|
|
766
811
|
# First verify version (required)
|
|
767
812
|
if ! step1_version_verification; then
|
|
768
813
|
echo -e "${RED}Version verification failed. Exiting.${NC}"
|
|
769
814
|
exit 1
|
|
770
815
|
fi
|
|
771
|
-
|
|
816
|
+
|
|
772
817
|
step2_check_status
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
818
|
+
run_step "Step 3" step3_clean_build
|
|
819
|
+
run_step "Step 4" step4_build_package
|
|
820
|
+
run_step "Step 5" step5_check_package
|
|
821
|
+
run_step "Step 6" step6_git_tag
|
|
822
|
+
run_step "Step 6.5" step6_5_create_github_release
|
|
823
|
+
run_step "Step 7" step7_upload_pypi
|
|
779
824
|
step_summary
|
|
780
825
|
break
|
|
781
826
|
;;
|