-
Notifications
You must be signed in to change notification settings - Fork 345
/
create-zipball.py
executable file
·195 lines (147 loc) · 5.8 KB
/
create-zipball.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python3
import json
import logging
import os
import re
import subprocess
import tempfile
import zipfile
from pathlib import Path
from typing import Callable
DISALLOWED_FILENAME_PATTERNS = [
re.compile(pattern)
for pattern in [
r"^\.git(hub|ignore|attributes|keep)$",
r"^\.travis\.yml$",
r"^\.editorconfig$",
r"(?i)^changelog",
r"(?i)^contributing",
r"(?i)^upgrading",
r"(?i)^copying",
r"(?i)^readme",
r"(?i)^licen[cs]e",
r"(?i)^version",
r"^phpunit",
r"^l?gpl\.txt$",
r"^composer\.(json|lock)$",
r"^Makefile$",
r"^build\.xml$",
r"^phpcs-ruleset\.xml$",
r"^\.php_cs$",
r"^phpmd\.xml$",
]
]
DISALLOWED_DEST_PATTERNS = [
re.compile(pattern)
for pattern in [
r"^vendor/htmlawed/htmlawed/htmLawed(Test\.php|(.*\.(htm|txt)))$",
r"^vendor/smalot/pdfparser/\.atoum\.php$",
r"^vendor/smottt/wideimage/demo",
r"^vendor/simplepie/simplepie/(db\.sql|autoload\.php)$",
r"^vendor/simplepie/simplepie/library$",
r"^vendor/composer/installed\.json$",
r"(?i)^vendor/[^/]+/[^/]+/(test|doc)s?",
r"^vendor/smalot/pdfparser/samples",
r"^vendor/smalot/pdfparser/src/Smalot/PdfParser/Tests",
]
]
def is_not_unimportant(dest: Path) -> bool:
filename = dest.name
filename_disallowed = any(r.match(filename) for r in DISALLOWED_FILENAME_PATTERNS)
dest_disallowed = any(r.match(str(dest)) for r in DISALLOWED_DEST_PATTERNS)
allowed = not (filename_disallowed or dest_disallowed)
return allowed
class ZipFile(zipfile.ZipFile):
def create_directory_entry(self, path: str) -> None:
# Directories are empty files whose path ends with a slash.
# https://mail.python.org/pipermail/python-list/2003-June/205859.html
self.writestr(str(self.prefix / path) + "/", "")
def directory(
self,
name: str,
allowed: Callable[[Path], bool] = lambda item: True,
) -> None:
self.create_directory_entry(name)
for _root, dirs, files in os.walk(name):
root = Path(_root)
if not allowed(root):
# Do not traverse child directories.
dirs.clear()
continue
for directory in dirs:
path = root / directory
if allowed(path):
self.create_directory_entry(str(path))
for file in files:
path = root / file
if allowed(path):
self.write(path, self.prefix / path)
def file(self, name: str) -> None:
self.write(name, self.prefix / name)
def is_repo_dirty(source_dir: Path) -> bool:
p = subprocess.run(["git", "-C", source_dir, "diff-index", "--quiet", "HEAD"])
return p.returncode == 1
def clone_repo_to(source_dir: Path, target_dir: Path) -> None:
subprocess.check_call(["git", "clone", "--shared", source_dir, target_dir])
def get_short_commit_id() -> str:
return subprocess.check_output(
["git", "rev-parse", "--short", "HEAD"],
encoding="utf-8",
).strip()
def main() -> None:
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("create-zipfile")
source_dir = Path.cwd()
with tempfile.TemporaryDirectory(prefix="selfoss-dist-") as _temp_dir:
temp_dir = Path(_temp_dir)
if is_repo_dirty(source_dir):
logger.warning(
"Repository contains uncommitted changes that will not be included in the dist archive."
)
logger.info("Cloning the repository into a temporary directory…")
clone_repo_to(source_dir, temp_dir)
os.chdir(temp_dir)
with open("package.json", encoding="utf-8") as package_json:
pkg = json.load(package_json)
version = pkg["ver"]
# Tagged releases will be bumped by a developer to version not including -SNAPSHOT suffix and we do not need to include the commit.
if "SNAPSHOT" in version:
logger.info("Inserting commit hash into version numbers…")
version = version.replace("SNAPSHOT", get_short_commit_id())
subprocess.check_call(["npm", "run", "bump-version", version])
logger.info("Installing dependencies…")
subprocess.check_call(["npm", "run", "install-dependencies"])
logger.info("Building asset bundles…")
subprocess.check_call(["npm", "run", "build"])
logger.info("Generating config-example.ini…")
subprocess.check_call(["php", "utils/generate-config-example.php"])
logger.info("Optimizing PHP dependencies…")
subprocess.check_call(
["composer", "install", "--no-dev", "--optimize-autoloader"]
)
filename = f"selfoss-{version}.zip"
# Fill archive with data.
with ZipFile(
source_dir / filename,
mode="w",
compression=zipfile.ZIP_DEFLATED,
strict_timestamps=False,
) as archive:
archive.prefix = Path("selfoss")
archive.create_directory_entry("")
archive.directory("src/")
archive.directory("vendor/", is_not_unimportant)
# Pack all bundles and bundled client assets.
archive.directory("public/")
# Copy data directory structure and .htaccess for deny.
archive.directory("data/")
archive.file(".htaccess")
archive.file(".nginx.conf")
archive.file("README.md")
archive.file("config-example.ini")
archive.file("index.php")
archive.file("run.php")
archive.file("cliupdate.php")
logger.info(f"Zipball ‘{filename}’ was successfully generated.")
if __name__ == "__main__":
main()