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 get_file_size(file_path): return os.stat(file_path).st_size 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 temp_forge_libs_json(): with open('temp_forge_libs.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_full_path = os.path.join(forge_path, forge_jar) #forge_submodules_list = generate_lib_list(os.path.join(forge_path, 'libs'), download_url) forge_submodules_list = temp_forge_libs_json() forge_module = [{ 'id': 'net.minecraftForge:forge:' + forge_jar[:-4], 'name': 'Minecraft Forge', 'type': 'ForgeHosted', 'artifact': { 'size': get_file_size(forge_full_path), 'MD5': calculate_md5(forge_full_path), 'url': download_url + 'forge/' + forge_jar }, 'subModules': forge_submodules_list }] forge_module = [temp_forge_json()] 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_client_mods_path = config.get(section, 'client_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_client_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 get_mod_name(mod_id): try: return config.get('Mods Names', mod_id) except: print("No mod name for " + mod_id) return mod_id def generate_mod_list(mods_path, download_url, isRequired): mods_file_list = list_files_in_folder(os.path.join(mods_path)) mod_list = [] for mod_file_name in mods_file_list: path_to_mod = os.path.join(mods_path, mod_file_name) parsed_mod = mod_file_name[:-4] parsed_mod = parsed_mod.split(':') mod_id = parsed_mod[1] if parsed_mod[-1] is not None: if parsed_mod[-1] == 'ServerSide': continue new_mod = { 'id': mod_file_name[:-4], 'name': get_mod_name(parsed_mod[1]), 'type': 'ForgeMod', 'required': isRequired, 'artifact': {'size': get_file_size(path_to_mod), 'MD5': calculate_md5(path_to_mod), 'url': download_url + 'mods/' + mod_file_name } } 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_file_name in libs_file_list: path_to_lib = os.path.join(libs_path, lib_file_name) if lib_file_name[-2:] == 'xz': lib_name = lib_file_name[:-12] + "@jar.pack.xz" else: lib_name = lib_file_name[:-4] new_lib = { 'id': lib_name, 'name': lib_name, 'type': 'Library', 'artifact': {'size': get_file_size(path_to_lib), 'MD5': calculate_md5(path_to_lib), 'url': download_url + 'libs/' + lib_file_name } } 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: print("print distribution to " + f.name) json.dump(distribution, f, ensure_ascii=False, indent=4)