Fix update version

This commit is contained in:
Lovi 2024-11-23 15:46:59 +01:00
parent 3836148ff3
commit 6e44bf8b34

View File

@ -4,12 +4,15 @@ import os
import shutil
from io import BytesIO
from zipfile import ZipFile
from datetime import datetime
# External library
import httpx
from rich.console import Console
from rich.prompt import Prompt
from rich.panel import Panel
from rich.table import Table
# Variable
@ -26,7 +29,6 @@ def move_content(source: str, destination: str):
- source (str): The path to the source folder.
- destination (str): The path to the destination folder.
"""
os.makedirs(destination, exist_ok=True)
# Iterate through all elements in the source folder
@ -37,8 +39,6 @@ def move_content(source: str, destination: str):
# If it's a directory, recursively call the function
if os.path.isdir(source_path):
move_content(source_path, destination_path)
# Otherwise, move the file, replacing if it already exists
else:
shutil.move(source_path, destination_path)
@ -52,11 +52,10 @@ def keep_specific_items(directory: str, keep_folder: str, keep_file: str):
- keep_folder (str): The name of the folder to keep.
- keep_file (str): The name of the file to keep.
"""
try:
if not os.path.exists(directory) or not os.path.isdir(directory):
raise ValueError(f"Error: '{directory}' is not a valid directory.")
# Iterate through items in the directory
for item in os.listdir(directory):
item_path = os.path.join(directory, item)
@ -75,21 +74,66 @@ def keep_specific_items(directory: str, keep_folder: str, keep_file: str):
console.print(f"[red]Error: {e}")
def print_commit_info(commit_info: dict):
"""
Print detailed information about the commit in a formatted table.
Parameters:
- commit_info (dict): The commit information from GitHub API
"""
# Create a table for commit information
table = Table(title=f"[bold green]Latest Commit Information - {__title__}", show_header=False)
table.add_column("Field", style="cyan")
table.add_column("Value", style="yellow")
# Basic commit info
commit = commit_info['commit']
commit_date = datetime.strptime(commit['author']['date'], "%Y-%m-%dT%H:%M:%SZ")
formatted_date = commit_date.strftime("%Y-%m-%d %H:%M:%S")
# Add rows to the table
table.add_row("Repository", f"{__author__}/{__title__}")
table.add_row("Commit SHA", commit_info['sha'][:8])
table.add_row("Author", f"{commit['author']['name']} <{commit['author']['email']}>")
table.add_row("Date", formatted_date)
table.add_row("Committer", f"{commit['committer']['name']} <{commit['committer']['email']}>")
table.add_row("Message", commit['message'])
# Add stats if available
if 'stats' in commit_info:
stats = commit_info['stats']
table.add_row("Changes", f"+{stats['additions']} -[red]{stats['deletions']}[/red] ({stats['total']} total)")
# Add URL info
table.add_row("HTML URL", commit_info['html_url'])
# Print the table in a panel
console.print(Panel.fit(table))
def download_and_extract_latest_commit():
"""
Download and extract the latest commit from a GitHub repository.
"""
try:
# Get the latest commit information using GitHub API
api_url = f'https://api.github.com/repos/{__author__}/{__title__}/commits?per_page=1'
response = httpx.get(api_url, timeout=10)
console.log("[green]Requesting latest commit from GitHub repository...")
headers = {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': f'{__title__}-updater'
}
response = httpx.get(api_url, headers=headers, timeout=10)
if response.status_code == 200:
commit_info = response.json()[0]
commit_sha = commit_info['sha']
# Print detailed commit information
print_commit_info(commit_info)
zipball_url = f'https://github.com/{__author__}/{__title__}/archive/{commit_sha}.zip'
console.log("[green]Downloading latest commit zip file...")
@ -114,9 +158,8 @@ def download_and_extract_latest_commit():
# Move all folder to main folder
new_folder_name = f"{__title__}-{commit_sha}"
move_content(new_folder_name, ".")
# Remove old temp folder
shutil.rmtree(new_folder_name)
console.log("[cyan]Latest commit downloaded and extracted successfully.")
else:
console.log(f"[red]Failed to fetch commit information. Status code: {response.status_code}")
@ -132,16 +175,20 @@ def main_upload():
"""
Main function to upload the latest commit of a GitHub repository.
"""
cmd_insert = Prompt.ask("[bold red]Are you sure you want to delete all files? (Only 'Video' folder and 'update_version.py' will remain)", choices=['y', 'n'], default='y', show_choices=True)
cmd_insert = Prompt.ask(
"[bold red]Are you sure you want to delete all files? (Only 'Video' folder and 'update_version.py' will remain)",
choices=['y', 'n'],
default='y',
show_choices=True
)
if cmd_insert.lower() == "yes":
if cmd_insert.lower().strip() == 'y' or cmd_insert.lower().strip() == 'yes':
console.print("[red]Deleting all files except 'Video' folder and 'update_version.py'...")
keep_specific_items(".", "Video", "upload.py")
download_and_extract_latest_commit()
else:
console.print("[red]Operation cancelled.")
if __name__ == "__main__":
main_upload()
main_upload()