HeliosDistributionGenerator/main.py

172 lines
5.8 KiB
Python

import os
import sys
import hashlib
import json
from pprint import pprint
from configparser import ConfigParser
def calculate_md5(file_path):
hash_md5 = hashlib.md5()
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def read_config(config_path):
config = ConfigParser()
config.read(config_path)
return config
def temp_forge_json():
with open('temp_forge.json') as json_file:
data = json.load(json_file)
return data
def generate_forge_list(forge_path, download_url):
forge_jar = scan_forge_folder(forge_path)
forge_submodules_list = generate_lib_list(os.path.join(forge_path, 'libs'), download_url)
forge_module = [{
'id': 'net.minecraftForge:forge:' + forge_jar.split('.')[0],
'name': 'Minecraft Forge',
'type': 'ForgeHosted',
'subModules': forge_submodules_list
}]
return (forge_module)
def parse_servers_config(config: ConfigParser):
servers = []
for section in config.sections():
if section.split(' ')[0] == 'Server' or section.split(' ')[0] == 'server':
download_url = config.get(section, 'download_url')
server_mods_path = os.path.join(config.get(section, 'path'), 'mods')
server_optional_mods_path = config.get(section, 'optional_mods_path')
forge_path = config.get(section, 'forge_reference_folder')
forge_required_modules = generate_mod_list(server_mods_path, download_url, True)
forge_optionals_modules = generate_mod_list(server_optional_mods_path, download_url, False)
forge_libs = generate_forge_list(forge_path, download_url)
new_server_modules = forge_libs + forge_required_modules + forge_optionals_modules
new_server = {'id': (config.get(section, 'id')),
'name': (config.get(section, 'name')),
'description': (config.get(section, 'description')),
'icon': (config.get(section, 'icon')),
'version': (config.get(section, 'version')),
'address': (config.get(section, 'address')),
'minecraftVersion': (config.get(section, 'minecraftVersion')),
'discord_shortId': (config.get(section, 'discord_shortId')),
'discord_largeImageText': (config.get(section, 'discord_largeImageText')),
'discord_largeImageKey': (config.get(section, 'discord_largeImageKey')),
'mainServer': (config.get(section, 'mainServer')),
'autoconnect': (config.get(section, 'autoconnect')),
'_path': (config.get(section, 'path')),
'_download_url': download_url,
'modules': new_server_modules
}
servers.append(new_server)
return servers
def scan_forge_folder(forge_path):
forge_jar = list_files_in_folder(forge_path)[0]
return forge_jar
def list_files_in_folder(path):
f = []
for (dirpath, dirnames, filenames) in os.walk(path):
f.extend(filenames)
break
return f
def generate_mod_list(mods_path, download_url, isRequired):
mods_file_list = list_files_in_folder(os.path.join(mods_path))
mod_list = []
# mod_list.append(temp_forge_json())
for mod in mods_file_list:
path_to_mod = os.path.join(mods_path, mod)
mod = mod[:-4]
parsed_mod = mod.split('_')
mod_id = parsed_mod[0]
parsed_mod[0] = parsed_mod[0].split(':')
parsed_mod[1] = parsed_mod[1].replace('-', ' ')
if parsed_mod[-1] is not None:
if parsed_mod[-1] == 'ServerSide':
continue
new_mod = {
'id': mod_id,
'name': parsed_mod[1],
'type': 'ForgeMod',
'required': isRequired,
'artifact': {'size': sys.getsizeof(path_to_mod),
'MD5': calculate_md5(path_to_mod),
'url': download_url + mod + '.jar'
}
}
mod_list.append(new_mod)
return mod_list
def generate_lib_list(libs_path, download_url):
libs_file_list = list_files_in_folder(os.path.join(libs_path))
lib_list = []
for lib in libs_file_list:
path_to_lib = os.path.join(libs_path, lib)
parsed_lib = lib.split(':')
lib_id = parsed_lib[1]
if lib.split('.')[-1] == 'xz':
lib_id += "@jar.pack.xz"
new_lib = {
'id': lib_id,
'name': parsed_lib[1],
'type': 'Library',
'artifact': {'size': sys.getsizeof(path_to_lib),
'MD5': calculate_md5(path_to_lib),
'url': download_url + lib
}
}
lib_list.append(new_lib)
return lib_list
def generate_json(config: ConfigParser):
new_distribution = {
'version': (config.get('Basic', 'version')),
'discord': {
'clientId': (config.get('Basic', 'version')),
'smallImageText': (config.get('Basic', 'discord_smallImageText')),
'smallImageKey': (config.get('Basic', 'dicord_smallImageKey'))
},
'java': {
'oracle': (config.get('Basic', 'java'))
},
'rss': (config.get('Basic', 'rss')),
'servers': parse_servers_config(config)
}
return new_distribution
if __name__ == '__main__':
temp_forge_json()
config = read_config(sys.argv[1])
distribution = generate_json(config)
pprint(distribution)
with open('distribution.json', 'w', encoding='utf-8') as f:
json.dump(distribution, f, ensure_ascii=False, indent=4)