Made script compatible for v3 page

This commit is contained in:
Stijn Bannink 2024-08-04 16:37:08 +02:00
parent 88fede5639
commit 12bf4ad377
Signed by: SBDeveloper
GPG key ID: B730712F2C3A9D7A
3 changed files with 81 additions and 27 deletions

View file

@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager"> <component name="ProjectRunConfigurationManager">
<configuration default="false" name="Refresh wiki" type="PythonConfigurationType" factoryName="Python"> <configuration default="false" name="Refresh wiki v2" type="PythonConfigurationType" factoryName="Python">
<module name="VehiclesPlus" /> <module name="VehiclesPlus" />
<option name="ENV_FILES" value="" /> <option name="ENV_FILES" value="" />
<option name="INTERPRETER_OPTIONS" value="" /> <option name="INTERPRETER_OPTIONS" value="" />
@ -14,7 +14,7 @@
<option name="ADD_SOURCE_ROOTS" value="true" /> <option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" /> <EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/.internal/Sources/refresh.py" /> <option name="SCRIPT_NAME" value="$PROJECT_DIR$/.internal/Sources/refresh.py" />
<option name="PARAMETERS" value="" /> <option name="PARAMETERS" value="v2" />
<option name="SHOW_COMMAND_LINE" value="false" /> <option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" /> <option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" /> <option name="MODULE_MODE" value="false" />

View file

@ -0,0 +1,25 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Refresh wiki v3" type="PythonConfigurationType" factoryName="Python">
<module name="VehiclesPlus" />
<option name="ENV_FILES" value="" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/.internal/Sources" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/.internal/Sources/refresh.py" />
<option name="PARAMETERS" value="v3" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
</component>

View file

@ -5,6 +5,7 @@ import shutil
import subprocess import subprocess
import tempfile import tempfile
from pathlib import Path from pathlib import Path
import argparse
# Load configuration from config.ini # Load configuration from config.ini
config = configparser.ConfigParser() config = configparser.ConfigParser()
@ -14,14 +15,25 @@ config.read('config.ini')
USERNAME = config.get('git', 'username') USERNAME = config.get('git', 'username')
PASSWORD = config.get('git', 'password') PASSWORD = config.get('git', 'password')
# Configuration # Define constants
REPO_URL = f'https://{USERNAME}:{PASSWORD}@git.sbdevelopment.tech/SBDevelopment/docs'
REPO_DIR = tempfile.mkdtemp()
SETUP_FILE_PATH = 'vehiclesplus/setup.md'
RESOURCE_PACK_DIR = '../../ResourcePacks/Examples-v2'
VERSIONS_FILE_PATH = 'versions_v2.txt'
BRANCH = 'main' BRANCH = 'main'
BASE_URL = 'https://git.sbdevelopment.tech/SBDevelopment/VehiclesPlus/raw/branch/master/ResourcePacks/Examples-v2/'
def get_version_paths(version):
"""Return the configuration paths based on the version."""
if version == 'v3':
return {
'setup_file_path': 'vehiclesplus-v3/setup.md',
'resource_pack_dir': '../../ResourcePacks/Examples-v3',
'base_url': 'https://git.sbdevelopment.tech/SBDevelopment/VehiclesPlus/raw/branch/master/ResourcePacks/Examples-v3/',
'versions_file_path': 'versions_v3.txt'
}
else: # Default to v2
return {
'setup_file_path': 'vehiclesplus/setup.md',
'resource_pack_dir': '../../ResourcePacks/Examples-v2',
'base_url': 'https://git.sbdevelopment.tech/SBDevelopment/VehiclesPlus/raw/branch/master/ResourcePacks/Examples-v2/',
'versions_file_path': 'versions_v2.txt'
}
def run_command(command): def run_command(command):
"""Run a shell command and return its output.""" """Run a shell command and return its output."""
@ -41,26 +53,26 @@ def clone_repo():
print(f"Cloning repository from {REPO_URL} into {REPO_DIR}") print(f"Cloning repository from {REPO_URL} into {REPO_DIR}")
run_command(f'git clone {REPO_URL} {REPO_DIR}') run_command(f'git clone {REPO_URL} {REPO_DIR}')
def find_setup_file(): def find_setup_file(setup_file_path):
"""Find and return the path of the setup.md file.""" """Find and return the path of the setup.md file."""
setup_file = Path(REPO_DIR) / SETUP_FILE_PATH setup_file = Path(REPO_DIR) / setup_file_path
if not setup_file.exists(): if not setup_file.exists():
raise FileNotFoundError(f"{setup_file} does not exist.") raise FileNotFoundError(f"{setup_file} does not exist.")
return setup_file return setup_file
def list_zip_files(): def list_zip_files(resource_pack_dir):
"""List all ZIP files in the ResourcePacks/Examples-v2 directory.""" """List all ZIP files in the ResourcePacks directory."""
resource_pack_path = Path(RESOURCE_PACK_DIR) resource_pack_path = Path(resource_pack_dir)
if not resource_pack_path.exists(): if not resource_pack_path.exists():
raise FileNotFoundError(f"{resource_pack_path} does not exist.") raise FileNotFoundError(f"{resource_pack_path} does not exist.")
zip_files = list(resource_pack_path.glob('VPExample-v2-*.zip')) zip_files = list(resource_pack_path.glob('VPExample-*-*.zip'))
print(f"Found ZIP files: {zip_files}") print(f"Found ZIP files: {zip_files}")
return zip_files return zip_files
def extract_version_info(zip_filename): def extract_version_info(zip_filename):
"""Extract version information from the zip filename.""" """Extract version information from the zip filename."""
pattern = re.compile(r'^VPExample-v2-(\d+\.\d+(?:\.\d+)?)(?:-(\d+\.\d+(?:\.\d+)?))?\.zip$') pattern = re.compile(r'^VPExample-v\d-(\d+\.\d+(?:\.\d+)?)(?:-(\d+\.\d+(?:\.\d+)?))?\.zip$')
match = pattern.match(zip_filename.name) match = pattern.match(zip_filename.name)
if match: if match:
if match.group(2): # Handle version ranges if match.group(2): # Handle version ranges
@ -78,10 +90,10 @@ def calculate_sha1(file_path):
sha1.update(chunk) sha1.update(chunk)
return sha1.hexdigest() return sha1.hexdigest()
def read_versions_file(): def read_versions_file(versions_file_path):
"""Read the versions_v2.txt file and return a list of (order, version) tuples.""" """Read the versions file and return a list of (order, version) tuples."""
versions = [] versions = []
with open(VERSIONS_FILE_PATH, 'r') as file: with open(versions_file_path, 'r') as file:
for line in file: for line in file:
parts = line.strip().split('#') parts = line.strip().split('#')
if len(parts) == 2: if len(parts) == 2:
@ -92,7 +104,7 @@ def read_versions_file():
versions.sort() versions.sort()
return [version for _, version in versions] return [version for _, version in versions]
def update_setup_file(setup_file_path, zip_files): def update_setup_file(setup_file_path, zip_files, base_url):
"""Update the setup.md file with correct paths and SHA1 checksums.""" """Update the setup.md file with correct paths and SHA1 checksums."""
with open(setup_file_path, 'r') as file: with open(setup_file_path, 'r') as file:
lines = file.readlines() lines = file.readlines()
@ -118,7 +130,7 @@ def update_setup_file(setup_file_path, zip_files):
new_lines = lines[:table_start_index + 2] # Keep header and alignment lines new_lines = lines[:table_start_index + 2] # Keep header and alignment lines
# Read the versions file to determine the order # Read the versions file to determine the order
version_order = read_versions_file() version_order = read_versions_file(VERSIONS_FILE_PATH) # Read versions file based on selected version
# Create a dictionary for quick lookup # Create a dictionary for quick lookup
zip_file_dict = {} zip_file_dict = {}
@ -135,9 +147,11 @@ def update_setup_file(setup_file_path, zip_files):
zip_file = zip_file_dict.get(version) zip_file = zip_file_dict.get(version)
print(f"Checking version: {version}") print(f"Checking version: {version}")
if zip_file: if zip_file:
print (f"Found ZIP file: {zip_file}") print(f"Found ZIP file: {zip_file}")
file_url = f"{BASE_URL}{zip_file.name}" file_url = f"{base_url}{zip_file.name}"
sha1_checksum = calculate_sha1(zip_file) sha1_checksum = calculate_sha1(zip_file)
if '-' in version:
version = version.replace('-', ' - ')
new_row = f"| {version} | [Click here]({file_url}) | `{sha1_checksum}` |\n" new_row = f"| {version} | [Click here]({file_url}) | `{sha1_checksum}` |\n"
new_rows.append(new_row) new_rows.append(new_row)
@ -167,11 +181,26 @@ def cleanup():
shutil.rmtree(REPO_DIR) shutil.rmtree(REPO_DIR)
def main(): def main():
parser = argparse.ArgumentParser(description='Update the vehicle setup file.')
parser.add_argument('version', choices=['v2', 'v3'], help='The version to use (v2 or v3)')
args = parser.parse_args()
# Determine paths and URLs based on the version
paths = get_version_paths(args.version)
global REPO_URL, REPO_DIR, SETUP_FILE_PATH, RESOURCE_PACK_DIR, BASE_URL, VERSIONS_FILE_PATH
REPO_URL = f'https://{USERNAME}:{PASSWORD}@git.sbdevelopment.tech/SBDevelopment/docs'
REPO_DIR = tempfile.mkdtemp()
SETUP_FILE_PATH = paths['setup_file_path']
RESOURCE_PACK_DIR = paths['resource_pack_dir']
BASE_URL = paths['base_url']
VERSIONS_FILE_PATH = paths['versions_file_path']
try: try:
clone_repo() clone_repo()
setup_file_path = find_setup_file() setup_file_path = find_setup_file(SETUP_FILE_PATH)
zip_files = list_zip_files() zip_files = list_zip_files(RESOURCE_PACK_DIR)
update_setup_file(setup_file_path, zip_files) update_setup_file(setup_file_path, zip_files, BASE_URL)
commit_and_push_changes() commit_and_push_changes()
finally: finally:
cleanup() cleanup()