diff --git a/picobuildWindows.py b/picobuildWindows.py index 5ad7ff2..274146a 100644 --- a/picobuildWindows.py +++ b/picobuildWindows.py @@ -1,89 +1,89 @@ import os +import shutil import requests import zipfile -from io import BytesIO -import shutil -def download_and_extract_latest_release(): - # GitHub API URL for repository releases - repo_api_url = "https://api.github.com/repos/nixietab/pds/releases/latest" +def create_folder(folder_name): + if not os.path.exists(folder_name): + os.makedirs(folder_name) + print(f"Created folder: {folder_name}") + else: + print(f"Folder already exists: {folder_name}") - try: - # Get the latest release data - response = requests.get(repo_api_url) - response.raise_for_status() - release_data = response.json() +def download_file(url, dest_path): + print(f"Downloading: {url}") + response = requests.get(url, stream=True) + if response.status_code == 200: + with open(dest_path, 'wb') as file: + shutil.copyfileobj(response.raw, file) + print(f"Downloaded to: {dest_path}") + else: + print(f"Failed to download: {url} (status code: {response.status_code})") - # Find the PDS.zip asset in the release assets - pds_zip_url = None - for asset in release_data.get("assets", []): - if asset["name"].lower() == "pds.zip": - pds_zip_url = asset["browser_download_url"] - break +def extract_zip(zip_path, extract_to): + print(f"Extracting {zip_path} to {extract_to}") + with zipfile.ZipFile(zip_path, 'r') as zip_ref: + zip_ref.extractall(extract_to) + print(f"Extraction complete") - if not pds_zip_url: - print("Error: PDS.zip not found in the latest release.") - return +def download_and_extract_repo(repo_url, dest_folder): + zip_url = repo_url.rstrip("/") + "/archive/refs/heads/main.zip" + zip_path = os.path.join(dest_folder, "repo.zip") + print(f"Downloading repository as ZIP: {zip_url}") + download_file(zip_url, zip_path) + extract_zip(zip_path, dest_folder) + os.remove(zip_path) + print(f"Repository extracted to: {dest_folder}") - # Download the PDS.zip file - print(f"Downloading PDS.zip from {pds_zip_url}...") - zip_response = requests.get(pds_zip_url) - zip_response.raise_for_status() +def move_folder_content(src_folder, dest_folder): + print(f"Moving contents of {src_folder} to {dest_folder}") + if os.path.exists(src_folder): + for item in os.listdir(src_folder): + src_path = os.path.join(src_folder, item) + dest_path = os.path.join(dest_folder, item) + shutil.move(src_path, dest_path) + print(f"Moved contents of {src_folder} to {dest_folder}") - # Extract the contents of the ZIP file - with zipfile.ZipFile(BytesIO(zip_response.content)) as zip_file: - process_folder = "progress" - os.makedirs(process_folder, exist_ok=True) - print(f"Extracting files to '{process_folder}'...") - zip_file.extractall(process_folder) + # Remove the folder if empty + if not os.listdir(src_folder): + os.rmdir(src_folder) + print(f"Removed empty folder: {src_folder}") + else: + print(f"Source folder does not exist: {src_folder}") - print("Download and extraction complete.") +def main(): + # Create "progress" folder + progress_folder = "progress" + create_folder(progress_folder) - except requests.exceptions.RequestException as e: - print(f"Network error: {e}") - except zipfile.BadZipFile: - print("Error: The downloaded file is not a valid ZIP file.") - except Exception as e: - print(f"An error occurred: {e}") + # Step 2: Download PDS.zip + pds_url = "https://github.com/nixietab/pds/releases/download/release/PDS.zip" + pds_zip_path = os.path.join(progress_folder, "PDS.zip") + download_file(pds_url, pds_zip_path) -def download_and_extract_repository(repo_url, destination_folder): - try: - # GitHub ZIP URL for the repository's main branch - repo_zip_url = f"{repo_url}/archive/refs/heads/main.zip" + # Step 3: Extract PDS.zip + extract_zip(pds_zip_path, progress_folder) - print(f"Downloading repository ZIP from {repo_zip_url}...") - response = requests.get(repo_zip_url) - response.raise_for_status() + # Step 4: Download and extract "picodulce" repository into "progress" + picodulce_repo_url = "https://github.com/nixietab/picodulce" + picodulce_dest_path = os.path.join(progress_folder, "picodulce") + create_folder(picodulce_dest_path) + download_and_extract_repo(picodulce_repo_url, picodulce_dest_path) - # Extract the ZIP contents - with zipfile.ZipFile(BytesIO(response.content)) as zip_file: - temp_folder = "temp_repo" - os.makedirs(temp_folder, exist_ok=True) - print(f"Extracting files to temporary folder '{temp_folder}'...") - zip_file.extractall(temp_folder) + # Step 4.1: Move "picodulce-main" contents to "progress" and remove the folder + picodulce_main_path = os.path.join(picodulce_dest_path, "picodulce-main") + move_folder_content(picodulce_main_path, progress_folder) - # Move extracted files to the destination folder - print(f"Moving files to '{destination_folder}'...") - if os.path.exists(destination_folder): - shutil.rmtree(destination_folder) # Remove existing folder - os.makedirs(destination_folder, exist_ok=True) - extracted_folder = os.path.join(temp_folder, os.listdir(temp_folder)[0]) - for item in os.listdir(extracted_folder): - shutil.move(os.path.join(extracted_folder, item), destination_folder) + # Step 4.2: Remove the "picodulce" folder after its contents are moved + if os.path.exists(picodulce_dest_path): + os.rmdir(picodulce_dest_path) + print(f"Removed folder: {picodulce_dest_path}") - # Clean up temporary folder - shutil.rmtree(temp_folder) - - print(f"Repository downloaded and extracted to '{destination_folder}'.") - - except requests.exceptions.RequestException as e: - print(f"Network error: {e}") - except zipfile.BadZipFile: - print("Error: The downloaded file is not a valid ZIP file.") - except Exception as e: - print(f"An error occurred: {e}") + # Step 5: Download and extract "2hsu" repository into current directory + hsu_repo_url = "https://github.com/nixietab/2hsu" + hsu_dest_path = os.path.join(os.getcwd(), "2hsu") + create_folder(hsu_dest_path) + download_and_extract_repo(hsu_repo_url, hsu_dest_path) if __name__ == "__main__": - download_and_extract_latest_release() - download_and_extract_repository("https://github.com/nixietab/picodulce", "progress") - download_and_extract_repository("https://github.com/nixietab/2hsu", ".") + main()