Dawn/tools/assetstool/assetstool.py
2024-10-06 22:23:26 -05:00

63 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
import os
import tarfile
import argparse
# Args
parser = argparse.ArgumentParser(
description='Bundles all assets into the internal archive format.'
)
parser.add_argument('-i', '--input');
parser.add_argument('-o', '--output');
args = parser.parse_args()
# Ensure the directory for the output path exists
if not os.path.exists(os.path.dirname(args.output)):
os.makedirs(os.path.dirname(args.output))
# Create a ZIP archive and add the specified directory
# archive = tarfile.open(args.output, 'w:bz2') # BZ2 Compression
# Does the archive already exist?
filesInArchive = []
if os.path.exists(args.output) and False:
# Yes, open it
archive = tarfile.open(args.output, 'r:')
# Get all the files in the archive
for member in archive.getmembers():
filesInArchive.append(member.name)
archive.close()
# Open archive for appending.
archive = tarfile.open(args.output, 'a:')
else:
# No, create it
archive = tarfile.open(args.output, 'w:')
# Add all files in the input directory
for foldername, subfolders, filenames in os.walk(args.input):
for filename in filenames:
# Is the file already in the archive?
absolute_path = os.path.join(foldername, filename)
relative_path = os.path.relpath(absolute_path, args.input)
if relative_path in filesInArchive:
# Yes, skip it
continue
# No, add it
print(f"Archiving asset {filename}...")
archive.add(absolute_path, arcname=relative_path)
# Close the archive
archive.close()