Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
160 lines (144 loc) · 6.44 KB
/
Copy pathsetup.py
File metadata and controls
160 lines (144 loc) · 6.44 KB
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
159
160
"""Build the single Analyzer runtime from its reviewed source tree."""
from __future__ import annotations
import importlib.util
import json
import re
import shutil
from pathlib import Path
from setuptools import find_packages, setup
from setuptools.command.build_py import build_py
from setuptools.command.sdist import sdist
ROOT = Path(__file__).resolve().parent
RUNTIME_DIRS = ("scripts", "agents", "data", "schemas", "templates")
RUNTIME_ROOT_SCRIPTS = {"appsec-analyst-cli", "mermaid_validate.mjs"}
RETIRED_RUNTIME = {
"plugin_meta.py",
"plugin_read_gate.py",
"plugin_write_gate.py",
"appsec_status.py",
"session_banner.py",
"version_status.py",
}
RETIRED_ANALYZERS = {"security_steering.py"}
USER_DOCS = (
"authnz-review.md",
"security-assessment.md",
"security-score.md",
"threat-analyst.md",
"configuration.md",
"model-selection.md",
"profiles.md",
"providers.md",
"headless-mode.md",
"security-requirements-audit-skill.md",
"threat-dragon-export.md",
"local-release-preparation.md",
"org-profiles.md",
"organization-distributions.md",
)
_LOCAL_LINK = re.compile(r"\]\(([^)#]+)(?:#[^)]*)?\)")
def _read_git(root: Path) -> dict:
# The runtime module reads Git the same way; load it by path because the package is not importable yet.
spec = importlib.util.spec_from_file_location("appsec_version_info", ROOT / "appsec_analyzer" / "version_info.py")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module.read_git(root)
def _ignore(directory: str, names: list[str]) -> set[str]:
ignored = {
name
for name in names
if name in {"__pycache__", ".pytest_cache", ".venv", ".git", "node_modules", "dist", "build", "wheelhouse"}
or name.endswith((".pyc", ".pyo", ".egg-info"))
}
if Path(directory) == ROOT / "scripts":
ignored.update(name for name in names if Path(directory, name).is_file() and name not in RUNTIME_ROOT_SCRIPTS)
if Path(directory) == ROOT / "scripts" / "runtime":
ignored.update(name for name in names if name in RETIRED_RUNTIME)
if Path(directory) == ROOT / "scripts" / "analyzers":
ignored.update(name for name in names if name in RETIRED_ANALYZERS)
return ignored
def _copy_local_link_targets(source: Path, package: Path) -> None:
for reference in _LOCAL_LINK.findall(source.read_text(encoding="utf-8")):
if ":" in reference or reference.startswith(("/", "#")):
continue
path = source.parent / reference
target = path.resolve()
if not target.is_relative_to(ROOT) or not target.is_file() or path.is_symlink():
raise ValueError(f"installed document has an invalid local link: {source.name}: {reference}")
destination = package / target.relative_to(ROOT)
destination.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(target, destination)
class BuildAnalyzer(build_py):
def run(self) -> None:
package = Path(self.build_lib) / "appsec_analyzer"
source_package = ROOT / "appsec_analyzer"
if package.is_symlink() or package.resolve() == source_package.resolve():
raise ValueError("build output must be a separate, non-symlinked package directory")
if package.exists():
shutil.rmtree(package)
super().run()
for name in RUNTIME_DIRS:
shutil.copytree(ROOT / name, package / name, dirs_exist_ok=True, ignore=_ignore)
for relative in (
"appsec-analyzer/SKILL.md",
"appsec-analyzer/business-context.md",
"appsec-analyzer/claude-code.md",
"review-assessment/SKILL.md",
"analyze-threats/SKILL.md",
"analyze-threats/HELP.txt",
"analyze-threats/references/analysis-contract.md",
"audit-security-requirements/config.json",
):
target = package / "skills" / relative
target.parent.mkdir(parents=True, exist_ok=True)
source = ROOT / "skills" / relative
shutil.copy2(source, target)
if source.suffix == ".md":
_copy_local_link_targets(source, package)
examples = package / "examples"
examples.mkdir(exist_ok=True)
shutil.copy2(ROOT / "examples" / "appsec-requirements-example.yaml", examples)
for folder in ("profiles", "model-profiles"):
shutil.copytree(ROOT / "examples" / folder, examples / folder, dirs_exist_ok=True)
shutil.copytree(
ROOT / "examples/organization-distribution",
examples / "organization-distribution",
dirs_exist_ok=True,
ignore=_ignore,
)
docs = package / "docs"
docs.mkdir(parents=True, exist_ok=True)
for name in USER_DOCS:
source = ROOT / "docs" / name
shutil.copy2(source, docs / name)
_copy_local_link_targets(source, package)
shutil.copy2(ROOT / "engine.json", package / "engine.json")
info = _read_git(ROOT)
if info:
(package / "build-info.json").write_text(json.dumps(info) + "\n", encoding="utf-8")
elif (ROOT / "build-info.json").is_file():
# A build from a source archive has no Git data; it carries the stamp made when the archive was built.
shutil.copy2(ROOT / "build-info.json", package / "build-info.json")
class StampedSdist(sdist):
def make_release_tree(self, base_dir, files) -> None:
super().make_release_tree(base_dir, files)
info = _read_git(ROOT)
if info:
Path(base_dir, "build-info.json").write_text(json.dumps(info) + "\n", encoding="utf-8")
setup(
name="appsec-analyzer",
version="0.1.1.dev0",
description="Standalone deterministic AppSec scanning and assessment engine",
long_description=(ROOT / "README.md").read_text(encoding="utf-8"),
long_description_content_type="text/markdown",
url="https://github.com/appsec-foundry/appsec-analyzer",
author="AppSec Foundry",
license="Apache-2.0",
python_requires=">=3.12",
packages=find_packages(include=["appsec_analyzer", "appsec_analyzer.*"]),
include_package_data=True,
install_requires=["PyYAML>=6,<7", "jsonschema>=4.20,<5", "Jinja2>=3.1,<4"],
extras_require={"requirements": ["requests>=2.32,<3", "beautifulsoup4>=4.12,<5"]},
entry_points={"console_scripts": ["appsec-analyzer=appsec_analyzer.cli:main"]},
cmdclass={"build_py": BuildAnalyzer, "sdist": StampedSdist},
)