Created
May 25, 2025 15:56
-
-
Save yaasita/34310a91db033906e634858be33f3426 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import hashlib | |
import json | |
import sys | |
# 設定 | |
if len(sys.argv) <= 1: | |
print("エラー: パートファイルのパターンを引数で指定してください。", file=sys.stderr) | |
sys.exit(1) | |
PART_FILES = f"{sys.argv[1]}*" # 分割されたパートファイルのパターン | |
OUTPUT_JSON = "parts.json" # 出力するJSONファイル名 | |
def calculate_md5(file_path): | |
"""ファイルのMD5ハッシュを計算""" | |
md5_hash = hashlib.md5() | |
with open(file_path, "rb") as f: | |
for chunk in iter(lambda: f.read(4096), b""): | |
md5_hash.update(chunk) | |
return md5_hash.hexdigest() | |
def main(): | |
print(PART_FILES) | |
# パートファイルを取得(アルファベット順でソート) | |
part_files = sorted(glob.glob(PART_FILES)) | |
if not part_files: | |
print("パートファイルが見つかりません。") | |
sys.exit(1) | |
parts = [] | |
for part_number, part_file in enumerate(part_files, start=1): | |
print(f"処理中: {part_file} (パート{part_number})") | |
# ローカルファイルのMD5ハッシュを計算 | |
md5_hash = calculate_md5(part_file) | |
print(f"パート{part_number} - MD5: {md5_hash}") | |
# parts.json用のデータ(ETagとしてMD5ハッシュを引用符付きで追加) | |
parts.append({"PartNumber": part_number, "ETag": f'"{md5_hash}"'}) | |
# parts.jsonを生成 | |
parts_json = {"Parts": parts} | |
with open(OUTPUT_JSON, "w") as f: | |
json.dump(parts_json, f, indent=2) | |
print(f"{OUTPUT_JSON} を生成しました。") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment