import os import re import shutil from shutil import make_archive import hashlib from b2sdk.v1 import InMemoryAccountInfo from b2sdk.v1 import B2Api class Pack: def __init__(self): command = "git clone -b {branch} --single-branch {url} /files/resourcepack" \ .format(branch=os.environ.get("BRANCH"), url=os.environ.get("RESOURCE_PACK_URL")) print(command) status = os.system(command) print(status) info = InMemoryAccountInfo() # store credentials, tokens and cache in memory self.b2_api = B2Api(info) self.b2_api.authorize_account("production", os.environ.get("B2_ID"), os.environ.get("B2_KEY")) self.bucket = self.b2_api.get_bucket_by_name(os.environ.get("BUCKET")) def __del__(self): shutil.rmtree("/files/resourcepack") def pull(self) -> None: os.system("git -C /files/resourcepack pull origin {}".format(os.environ.get("BRANCH"))) def collate(self) -> None: if os.path.exists("/files/pack"): shutil.rmtree("/files/pack") os.mkdir("/files/pack") shutil.copytree("/files/resourcepack/assets", "/files/pack/assets") shutil.copy("/files/resourcepack/pack.mcmeta", "/files/pack/pack.mcmeta") shutil.copy("/files/resourcepack/pack.png", "/files/pack/pack.png") def compress(self) -> None: shutil.make_archive("/files/pack", 'zip', "/files/pack") shutil.rmtree("/files/pack") def hash(self): sha1sum = hashlib.sha1() with open("/files/pack.zip", "rb") as pack: block = pack.read(2**16) while len(block) != 0: sha1sum.update(block) block = pack.read(2**16) self.hash = sha1sum.hexdigest() with open("/files/hash", 'w') as hashfile: hashfile.write(self.hash) def upload(self): self.b2_api.authorize_account("production", os.environ.get("B2_ID"), os.environ.get("B2_KEY")) self.bucket.upload_local_file( local_file="files/pack.zip", file_name="pack.zip") self.bucket.upload_local_file( local_file="/files/hash", file_name="hash") if __name__ == "__main__": print("Pack") pack = Pack() # pack.pull() # pack.collate() # pack.compress() # pack.hash() # pack.upload() #pack = None