Skip to content

Commit 9b68a61

Browse files
committed
refactor package script
1 parent 57f2c7b commit 9b68a61

File tree

1 file changed

+71
-50
lines changed

1 file changed

+71
-50
lines changed

scripts/package_repo.py

+71-50
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,79 @@
11
import os
22
import shutil
33
import zipfile
4+
from tempfile import TemporaryDirectory
5+
from subprocess import check_call
6+
7+
# Only these top-level items will be included in the package
8+
INCLUDE = [
9+
"pilot",
10+
"Dockerfile",
11+
"docker-compose.yml",
12+
"LICENSE",
13+
"README.md",
14+
"requirements.txt",
15+
"setup.py"
16+
]
17+
18+
def find_repo_root() -> str:
19+
"""
20+
Returns the path to the root of the repository, or None if not found.
21+
"""
22+
# Find repository root
23+
dir = os.path.dirname(os.path.normpath(os.path.abspath(__file__)))
24+
25+
# While we haven't reached the root of the filesystem...
26+
while dir != os.path.dirname(dir):
27+
if os.path.exists(os.path.join(dir, ".git")):
28+
break
29+
dir = os.path.dirname(dir)
30+
else:
31+
return None
32+
33+
# Verify there's a "pilot" subdirectory in the repo root
34+
if not os.path.exists(os.path.join(dir, "pilot")):
35+
return None
36+
return dir
37+
438

539
def main():
6-
# Define the base directory (one level up from /scripts)
7-
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
8-
repo_path = os.path.abspath(base_dir)
9-
10-
# Files to exclude from the repo temporarily while packaging
11-
files_to_exclude = [
12-
"pilot/.env",
13-
"pilot/gpt-pilot"
14-
]
15-
16-
# Step 1: Move excluded files to /tmp
17-
tmp_excluded_paths = []
18-
for file in files_to_exclude:
19-
source_path = os.path.join(repo_path, file)
20-
if os.path.exists(source_path):
21-
tmp_path = os.path.join("/tmp", os.path.basename(file))
22-
shutil.move(source_path, tmp_path)
23-
tmp_excluded_paths.append((tmp_path, source_path))
24-
25-
# Items to package
26-
items_to_package = [
27-
"pilot",
28-
"Dockerfile",
29-
"docker-compose.yml",
30-
"LICENSE",
31-
"README.md",
32-
"requirements.txt",
33-
"setup.py"
34-
]
35-
36-
# Step 2: Package the specified items using Python's zipfile module
37-
parent_directory = os.path.dirname(base_dir)
38-
archive_path = os.path.join(parent_directory, "gpt-pilot-packaged.zip")
39-
40-
with zipfile.ZipFile(archive_path, 'w', zipfile.ZIP_DEFLATED) as archive:
41-
for item in items_to_package:
42-
item_path = os.path.join(repo_path, item)
43-
if os.path.isfile(item_path):
44-
archive.write(item_path, item)
45-
elif os.path.isdir(item_path):
46-
for root, _, files in os.walk(item_path):
47-
for file in files:
48-
file_path = os.path.join(root, file)
49-
archive_path = os.path.relpath(file_path, repo_path)
50-
archive.write(file_path, archive_path)
51-
52-
# Step 3: Move the excluded files back
53-
for tmp_path, orig_path in tmp_excluded_paths:
54-
if os.path.exists(tmp_path):
55-
shutil.move(tmp_path, orig_path)
40+
repo_dir = find_repo_root()
41+
if repo_dir is None:
42+
print("Could not find GPT Pilot: please run me from the repository root directory.")
43+
44+
os.chdir(repo_dir)
45+
46+
with TemporaryDirectory() as tmp_dir:
47+
# Create a repository archive
48+
temp_archive_path = os.path.join(tmp_dir, "repository.zip")
49+
check_call(["git", "archive", "-o", temp_archive_path, "main"])
50+
check_call(["unzip", "-qq", "-x", temp_archive_path], cwd=tmp_dir)
51+
52+
# Remove all items from the archive that aren't explictly whitelisted
53+
for item in os.listdir(tmp_dir):
54+
if item not in INCLUDE:
55+
path = os.path.join(tmp_dir, item)
56+
if os.path.isfile(path):
57+
os.remove(path)
58+
else:
59+
shutil.rmtree(path)
60+
61+
archive_path = os.path.abspath(os.path.join("..", "gpt-pilot-packaged.zip"))
62+
if os.path.exists(archive_path):
63+
os.remove(archive_path)
64+
65+
with zipfile.ZipFile(archive_path, "w", zipfile.ZIP_DEFLATED) as zip_file:
66+
for dpath, dirs, files in os.walk(tmp_dir):
67+
for file in files:
68+
full_path = os.path.join(dpath, file)
69+
if full_path != temp_archive_path:
70+
rel_path = os.path.relpath(full_path, tmp_dir)
71+
print(rel_path)
72+
zip_file.write(full_path, rel_path)
73+
74+
size = os.path.getsize(archive_path)
75+
print(f"\nCreated: {archive_path} ({size // 1024} KB)")
76+
5677

5778
if __name__ == "__main__":
5879
main()

0 commit comments

Comments
 (0)