ResourcePackUpdater/src/pack.py

77 lines
2.1 KiB
Python

import os
import re
import shutil
import zipfile
import hashlib
from dotenv import load_dotenv
from b2sdk.v1 import InMemoryAccountInfo
from b2sdk.v1 import B2Api
class Pack:
def __init__(self):
load_dotenv()
self.repourl = os.environ.get("RESOURCE_PACK_URL")
command = "git clone " + self.repourl + " resourcepack"
status = os.system(command)
print(status)
info = InMemoryAccountInfo() # store credentials, tokens and cache in memory
b2_api = B2Api(info)
b2_api.authorize_account("production", os.environ.get("APPLICATION_KEY_ID"),
os.environ.get("APPLICATION_KEY"))
self.bucket = b2_api.get_bucket_by_name(os.environ.get("BUCKET"))
def __del__(self):
shutil.rmtree(self.dir)
def pull(self) -> None:
os.system("git -C {} pull origin master".format(self.dir))
def collate(self) -> None:
if os.path.exists("pack"):
shutil.rmtree("pack")
os.mkdir("pack")
shutil.copytree("resourcepack/assets", "pack/assets")
shutil.copy("resourcepack/pack.mcmeta", "pack/pack.mcmeta")
shutil.copy("resourcepack/pack.png", "pack/pack.png")
def compress(self) -> None:
if not os.path.exists("files"):
os.mkdir("files")
os.system("zip -r files/pack.zip pack")
shutil.rmtree("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):
bucket.upload_local_file(
local_file="files/pack.zip",
file_name="pack.zip")
bucket.upload_local_file(
local_file="files/hash",
file_name="hash")
if __name__ == "__main__":
from dotenv import load_dotenv
load_dotenv()
pack = Pack()
pack.pull()
pack.collate()
pack.compress()
pack.hash()
#pack = None