-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease_ghost.py
More file actions
276 lines (222 loc) · 10.2 KB
/
Copy pathrelease_ghost.py
File metadata and controls
276 lines (222 loc) · 10.2 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python3
"""
release_ghost.py - Ukagaka ghost release tooling
Subcommands:
dau Generate updates2.dau
Spec: https://ssp.shillest.net/ukadoc/manual/spec_update_file.html
nar Create .nar archive (ZIP with .nar extension)
delete Generate delete.txt (files removed since a previous git ref)
Usage:
python release_ghost.py dau [--root DIR] [--output PATH]
python release_ghost.py nar [--root DIR] [--output PATH]
python release_ghost.py delete [--root DIR] [--output PATH] [--prev-ref REF]
Defaults:
--root current directory
--output <root>/updates2.dau (dau)
<root>/<dirname>.nar (nar)
<root>/delete.txt (delete)
Ignore files (gitignore syntax):
.updateignore patterns excluded from updates2.dau and delete.txt
.narignore patterns excluded from .nar archive
"""
import argparse
import fnmatch
import hashlib
import subprocess
import zipfile
from pathlib import Path
# ── .???ignore パターン ──────────────────────────────────────────────────────
def _load_ignore_patterns(path: Path) -> list[str]:
"""Load patterns from a .updateignore/.narignore file (gitignore syntax)."""
if not path.exists():
return []
patterns = []
for line in path.read_text(encoding='utf-8').splitlines():
line = line.strip()
if line and not line.startswith('#'):
patterns.append(line)
return patterns
def _is_ignored(rel_str: str, patterns: list[str]) -> bool:
"""Check if a relative path (forward-slash separated) matches any ignore pattern."""
parts = rel_str.split('/')
basename = parts[-1]
for pattern in patterns:
if pattern.endswith('/'):
# Directory pattern: match if any ancestor component matches
dir_pat = pattern.rstrip('/')
if any(fnmatch.fnmatch(p, dir_pat) for p in parts[:-1]):
return True
elif '/' in pattern:
# Anchored path pattern (contains slash)
if fnmatch.fnmatch(rel_str, pattern.lstrip('/')):
return True
else:
# Filename pattern: match against basename
if fnmatch.fnmatch(basename, pattern):
return True
return False
# ── ファイル列挙 ─────────────────────────────────────────────────────────────
def collect_files(root: Path, ignore_patterns: list[str]) -> list[tuple[str, Path]]:
"""ゴースト配布対象ファイルの一覧を返す。
`git ls-files` を使うことで .gitignore の除外設定を自動的に尊重する。
git が使えない環境ではディレクトリ走査にフォールバックする。
Returns:
[(フォワードスラッシュ区切り相対パス, 絶対パス), ...] のソート済みリスト
"""
try:
result = subprocess.run(
['git', 'ls-files', '--cached', '--others', '--exclude-standard'],
cwd=root,
capture_output=True,
text=True,
check=True,
)
raw_paths = [p.strip() for p in result.stdout.splitlines() if p.strip()]
except (subprocess.CalledProcessError, FileNotFoundError):
raw_paths = [
'/'.join(p.relative_to(root).parts)
for p in root.rglob('*')
if p.is_file()
]
results = []
for rel_str in raw_paths:
rel_str = rel_str.replace('\\', '/')
# ツール自身は常に除外
if rel_str == 'release_ghost.py':
continue
if _is_ignored(rel_str, ignore_patterns):
continue
abs_path = root / Path(*rel_str.split('/'))
if abs_path.is_file():
results.append((rel_str, abs_path))
results.sort(key=lambda x: x[0])
return results
# ── updates2.dau 生成 ────────────────────────────────────────────────────────
def _md5(path: Path) -> str:
h = hashlib.md5()
with open(path, 'rb') as f:
for chunk in iter(lambda: f.read(65536), b''):
h.update(chunk)
return h.hexdigest()
def cmd_dau(root: Path, output: Path) -> None:
"""updates2.dau を生成する。"""
patterns = _load_ignore_patterns(root / '.updateignore')
patterns.append('updates2.dau') # 自己参照を避ける
files = collect_files(root, patterns)
with open(output, 'wb') as f:
for i, (rel_str, abs_path) in enumerate(files):
stat = abs_path.stat()
fields = [
rel_str,
_md5(abs_path),
f'size={stat.st_size}',
]
if i == 0:
fields.append('charset=UTF-8')
f.write(('\x01'.join(fields) + '\r\n').encode('utf-8'))
print(f'Generated {output} ({len(files)} files)')
# ── delete.txt 生成 ──────────────────────────────────────────────────────────
def cmd_delete(root: Path, output: Path, prev_ref: str | None) -> None:
"""delete.txt を累積方式で生成する。
既存の delete.txt(蓄積済みの削除履歴)に前リリースからの削除分を追加し、
現在存在するファイルを除外して書き出す。
配布対象外のファイル(.updateignore に一致するもの)も除外する。
"""
ignore_patterns = _load_ignore_patterns(root / '.updateignore')
# これらは配布物だが削除リスト対象外
always_exclude = {'updates2.dau', 'delete.txt', 'release_ghost.py'}
# 既存の delete.txt から蓄積済みエントリを読み込む
accumulated: set[str] = set()
if output.exists():
for line in output.read_text(encoding='utf-8').splitlines():
line = line.strip()
if line:
accumulated.add(line)
# 現在のファイル一覧(除外判定に使用)
curr_result = subprocess.run(
['git', 'ls-files', '--cached', '--others', '--exclude-standard'],
cwd=root, capture_output=True, text=True, check=True,
)
curr_files = {r.replace('\\', '/') for r in curr_result.stdout.splitlines()}
# 前リリースから今回削除されたファイルを取得
newly_deleted: set[str] = set()
if prev_ref is not None:
try:
prev_result = subprocess.run(
['git', 'ls-tree', '-r', '--name-only', prev_ref],
cwd=root, capture_output=True, text=True, check=True,
)
prev_files = {r.replace('\\', '/') for r in prev_result.stdout.splitlines()}
newly_deleted = prev_files - curr_files
except subprocess.CalledProcessError:
print(f'Warning: could not resolve ref {prev_ref!r}; skipping diff')
# 累積 ∪ 新規削除 − 現存ファイル − 除外対象
deleted = sorted(
rel for rel in (accumulated | newly_deleted)
if rel not in always_exclude
and rel not in curr_files
and not _is_ignored(rel, ignore_patterns)
)
content = '\n'.join(deleted) + ('\n' if deleted else '')
output.write_bytes(content.encode('utf-8'))
print(f'Generated {output} ({len(deleted)} files to delete)')
for f in deleted:
print(f' Delete: {f}')
# ── .nar アーカイブ生成 ──────────────────────────────────────────────────────
def cmd_nar(root: Path, output: Path) -> None:
"""_the_hand_.nar(中身は ZIP)を生成する。
updates2.dau は除外対象に含めないので、事前に dau コマンドで
生成しておくと自動的に同梱される。
"""
patterns = _load_ignore_patterns(root / '.narignore')
files = collect_files(root, patterns)
with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as zf:
for rel_str, abs_path in files:
zf.write(abs_path, rel_str)
print(f' Added: {rel_str}')
print(f'Created {output} ({len(files)} files)')
# ── CLI ─────────────────────────────────────────────────────────────────────
def main() -> None:
parser = argparse.ArgumentParser(
description='Ukagaka ghost release tooling',
)
parser.add_argument(
'--root', type=Path, default=Path('.'),
metavar='DIR', help='ghost root directory (default: current directory)',
)
sub = parser.add_subparsers(dest='command', required=True)
# dau サブコマンド
p_dau = sub.add_parser('dau', help='generate updates2.dau')
p_dau.add_argument(
'--output', type=Path, default=None,
metavar='PATH', help='output path (default: <root>/updates2.dau)',
)
# nar サブコマンド
p_nar = sub.add_parser('nar', help='create .nar archive')
p_nar.add_argument(
'--output', type=Path, default=None,
metavar='PATH', help='output path (default: <root>/<dirname>.nar)',
)
# delete サブコマンド
p_del = sub.add_parser('delete', help='generate delete.txt')
p_del.add_argument(
'--output', type=Path, default=None,
metavar='PATH', help='output path (default: <root>/delete.txt)',
)
p_del.add_argument(
'--prev-ref', default=None,
metavar='REF', help='previous git ref (tag, SHA, etc.) to compare against',
)
args = parser.parse_args()
root = args.root.resolve()
if args.command == 'dau':
output = args.output or root / 'updates2.dau'
cmd_dau(root, output.resolve())
elif args.command == 'nar':
output = args.output or (root / (root.name + '.nar'))
cmd_nar(root, output.resolve())
elif args.command == 'delete':
output = args.output or root / 'delete.txt'
cmd_delete(root, output.resolve(), args.prev_ref)
if __name__ == '__main__':
main()