Skip to content

Commit 957c40d

Browse files
committed
remove AWS docs from wheel and minify JSON
1 parent c02b8ef commit 957c40d

File tree

5 files changed

+55
-39
lines changed

5 files changed

+55
-39
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ repos:
1313
rev: v0.5.3
1414
hooks:
1515
- id: ruff
16-
files: ^policy_sentry/
16+
files: ^(policy_sentry/|setup.py)
1717
- id: ruff-format
18-
files: ^policy_sentry/
18+
files: ^(policy_sentry/|setup.py)
1919
- repo: https://github.com/Lucas-C/pre-commit-hooks-safety
2020
rev: v1.3.3
2121
hooks:

MANIFEST.in

Lines changed: 0 additions & 2 deletions
This file was deleted.

policy_sentry/command/initialize.py

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from __future__ import annotations
77

88
import logging
9-
import os
109
import shutil
1110
from pathlib import Path
1211

@@ -17,7 +16,6 @@
1716
from policy_sentry.shared.constants import (
1817
BUNDLED_DATA_DIRECTORY,
1918
BUNDLED_DATASTORE_FILE_PATH,
20-
BUNDLED_HTML_DIRECTORY_PATH,
2119
CONFIG_DIRECTORY,
2220
DATASTORE_FILE_PATH,
2321
LOCAL_ACCESS_OVERRIDES_FILE,
@@ -101,8 +99,8 @@ def initialize(
10199
# Create the config directory
102100
database_path = create_policy_sentry_config_directory()
103101

104-
# Copy over the html docs, which will be used to build the database
105-
create_html_docs_directory()
102+
# Create local html docs folder, if it doesn't exist
103+
LOCAL_HTML_DIRECTORY_PATH.mkdir(parents=True, exist_ok=True)
106104

107105
# Create overrides file, which allows us to override the Access Levels
108106
# provided by AWS documentation
@@ -126,6 +124,9 @@ def initialize(
126124
if fetch:
127125
# `wget` the html docs to the local directory
128126
update_html_docs_directory(LOCAL_HTML_DIRECTORY_PATH)
127+
elif not next(LOCAL_HTML_DIRECTORY_PATH.glob("*.html"), None):
128+
print("No HTML docs found, fetching from AWS!")
129+
update_html_docs_directory(LOCAL_HTML_DIRECTORY_PATH)
129130

130131
# --build
131132
if build or access_level_overrides_file or fetch:
@@ -134,9 +135,8 @@ def initialize(
134135

135136
# Query the database for all the services that are now in the database.
136137
all_aws_service_prefixes = get_all_service_prefixes()
137-
total_count_of_services = str(len(all_aws_service_prefixes))
138138
print("Initialization complete!")
139-
print(f"Total AWS services in the IAM database: {total_count_of_services}")
139+
print(f"Total AWS services in the IAM database: {len(all_aws_service_prefixes)}")
140140
logger.debug("\nService prefixes:")
141141
logger.debug(", ".join(all_aws_service_prefixes))
142142

@@ -157,22 +157,3 @@ def create_policy_sentry_config_directory() -> Path:
157157
else:
158158
CONFIG_DIRECTORY.mkdir(exist_ok=True)
159159
return LOCAL_DATASTORE_FILE_PATH
160-
161-
162-
def create_html_docs_directory() -> None:
163-
"""
164-
Copies the HTML files from the pip package over to its own folder in the CONFIG_DIRECTORY.
165-
Essentially:
166-
mkdir -p ~/.policy_sentry/data/docs
167-
cp -r $MODULE_DIR/policy_sentry/shared/data/docs ~/.policy_sentry/data/docs
168-
:return:
169-
"""
170-
if os.path.exists(LOCAL_HTML_DIRECTORY_PATH):
171-
pass
172-
else:
173-
os.makedirs(LOCAL_HTML_DIRECTORY_PATH)
174-
# Copy from the existing html docs folder - the path ./policy_sentry/shared/data/docs within this repository
175-
logger.debug(BUNDLED_HTML_DIRECTORY_PATH)
176-
if os.path.exists(LOCAL_HTML_DIRECTORY_PATH):
177-
shutil.rmtree(LOCAL_HTML_DIRECTORY_PATH)
178-
shutil.copytree(BUNDLED_HTML_DIRECTORY_PATH, LOCAL_HTML_DIRECTORY_PATH)

setup.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
"""Setup script for Policy Sentry"""
22

3-
import setuptools
4-
import os
3+
import json
54
import re
5+
from pathlib import Path
6+
7+
import setuptools
8+
from setuptools.command.build_py import build_py
69

7-
HERE = os.path.abspath(os.path.dirname(__file__))
10+
HERE = Path(__file__).parent
811
VERSION_RE = re.compile(r"""__version__ = ['"]([0-9.]+)['"]""")
912
TESTS_REQUIRE = ["coverage", "pytest"]
1013
REQUIRED_PACKAGES = [
@@ -23,15 +26,32 @@
2326
}
2427

2528

26-
def get_version():
27-
init = open(os.path.join(HERE, "policy_sentry", "bin", "version.py")).read()
29+
class PreBuildCommand(build_py):
30+
"""Pre-build command"""
31+
32+
def minify_iam_data_json(self) -> None:
33+
"""Minifies the IAM DB JSON file"""
34+
src_iam_data_path = Path("policy_sentry/shared/data/iam-definition.json")
35+
build_iam_data_path = Path(self.build_lib) / src_iam_data_path
36+
37+
self.mkpath(str(build_iam_data_path.parent))
38+
minified = json.dumps(
39+
json.loads(src_iam_data_path.read_bytes()), separators=(",", ":")
40+
)
41+
build_iam_data_path.write_text(minified)
42+
43+
def run(self) -> None:
44+
self.execute(self.minify_iam_data_json, ())
45+
build_py.run(self)
46+
47+
48+
def get_version() -> str:
49+
init = (HERE / "policy_sentry/bin/version.py").read_text()
2850
return VERSION_RE.search(init).group(1)
2951

3052

31-
def get_description():
32-
return open(
33-
os.path.join(os.path.abspath(HERE), "README.md"), encoding="utf-8"
34-
).read()
53+
def get_description() -> str:
54+
(HERE / "README.md").read_text()
3555

3656

3757
setuptools.setup(
@@ -45,6 +65,14 @@ def get_description():
4565
long_description_content_type="text/markdown",
4666
url="https://github.com/salesforce/policy_sentry",
4767
packages=setuptools.find_packages(exclude=["test*"]),
68+
package_data={
69+
"policy_sentry": ["py.typed"],
70+
"policy_sentry.shared": [
71+
"data/*.json",
72+
"data/*.yml",
73+
"data/audit/*.txt",
74+
],
75+
},
4876
tests_require=TESTS_REQUIRE,
4977
install_requires=REQUIRED_PACKAGES,
5078
project_urls=PROJECT_URLS,
@@ -64,4 +92,7 @@ def get_description():
6492
zip_safe=True,
6593
keywords="aws iam roles policy policies privileges security",
6694
python_requires=">=3.8",
95+
cmdclass={
96+
"build_py": PreBuildCommand,
97+
},
6798
)

tasks.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#!/usr/bin/env python
2+
import shutil
23
import sys
34
import os
45
import logging
56
from invoke import task, Collection, UnexpectedExit, Failure
67

8+
from policy_sentry.shared.constants import LOCAL_HTML_DIRECTORY_PATH, BUNDLED_HTML_DIRECTORY_PATH
9+
710
sys.path.append(
811
os.path.abspath(
912
os.path.join(os.path.dirname(__file__), os.path.pardir, "policy_sentry")
@@ -63,7 +66,7 @@ def build_package(c):
6366
@task(pre=[build_package])
6467
def install_package(c):
6568
"""Install the policy_sentry package built from the current directory contents (not PyPi)"""
66-
c.run("pip3 install -q dist/policy_sentry-*.tar.gz")
69+
c.run("pip3 install -q dist/policy_sentry-*.whl")
6770

6871

6972
@task
@@ -110,6 +113,9 @@ def clean_config_directory(c):
110113
def create_db(c):
111114
"""Integration testing: Initialize the policy_sentry database"""
112115
try:
116+
LOCAL_HTML_DIRECTORY_PATH.mkdir(parents=True, exist_ok=True)
117+
shutil.copytree(BUNDLED_HTML_DIRECTORY_PATH, LOCAL_HTML_DIRECTORY_PATH, dirs_exist_ok=True)
118+
113119
initialize.initialize("")
114120
except UnexpectedExit as u_e:
115121
logger.critical(f"FAIL! UnexpectedExit: {u_e}")

0 commit comments

Comments
 (0)