tools/generate_package_info.py
2024-08-28 12:36:58 +08:00

67 lines
2.8 KiB
Python

import os
import json
import hashlib
import zipfile
import argparse
def calculate_sha256(filename):
sha256_hash = hashlib.sha256()
with open(filename, "rb") as f:
for byte_block in iter(lambda: f.read(4096), b""):
sha256_hash.update(byte_block)
return sha256_hash.hexdigest()
def get_package_info(package_dir):
package_json_path = os.path.join(package_dir, "package.json")
if os.path.exists(package_json_path):
with open(package_json_path, "r", encoding="utf-8") as f:
package_data = json.load(f)
return {
"name": package_data.get("name", ""),
"version": package_data.get("version", ""),
"title": package_data.get("title", ""),
"description": package_data.get("description", ""),
"author": package_data.get("author", ""),
"require": package_data.get("require", {}),
}
return None
def create_zip(directory, output_dir):
zip_filename = os.path.join(output_dir, f"{os.path.basename(directory)}.zip")
with zipfile.ZipFile(zip_filename, "w", zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
zipf.write(file_path, os.path.relpath(file_path, directory))
return zip_filename
def main():
parser = argparse.ArgumentParser(description="Create package info and generate zip files")
parser.add_argument("input_dir", help="Input directory containing package subdirectories")
parser.add_argument("output_dir", help="Output directory for generated files")
args = parser.parse_args()
result = []
for subdir in os.listdir(args.input_dir):
if os.path.isdir(os.path.join(args.input_dir, subdir)):
package_info = get_package_info(os.path.join(args.input_dir, subdir))
if package_info:
zip_filename = create_zip(os.path.join(args.input_dir, subdir), args.output_dir)
shasum = calculate_sha256(zip_filename)
dist_info = {
"type": "zip",
#"url": f"https://oss.windycloud.cn/plugins-dist/{os.path.basename(zip_filename)}",
"url": f"https://ct.windycloud.cn:2100/job/Blessing-Skin-Plugins/lastSuccessfulBuild/artifact/pacages/{os.path.basename(zip_filename)}",
"shasum": shasum,
}
package_info["dist"] = dist_info
result.append(package_info)
output_file_path = os.path.join(args.output_dir, "output.json")
with open(output_file_path, "w", encoding="utf-8") as output_file:
json.dump(result, output_file, indent=2, ensure_ascii=False)
if __name__ == "__main__":
main()