wonkitty/apple_oh
0
1import os2import csv3import shutil4import tarfile5import subprocess6from pathlib import Path7from datetime import datetime8 9def install_packages_but_jank_af():10 packages = ['build-essential', 'python3-dev', 'ffmpeg', 'aria2']11 pip_packages = ['pip', 'setuptools', 'wheel', 'httpx==0.23.0', 'faiss-gpu', 'fairseq', 'gradio==3.34.0',12 'ffmpeg', 'ffmpeg-python', 'praat-parselmouth', 'pyworld', 'numpy==1.23.5',13 'numba==0.56.4', 'librosa==0.9.2', 'mega.py', 'gdown', 'onnxruntime', 'pyngrok==4.1.12',14 'gTTS', 'elevenlabs', 'wget', 'tensorboardX', 'unidecode', 'huggingface-hub', 'stftpitchshift==1.5.1', 15 'yt-dlp', 'pedalboard', 'pathvalidate', 'nltk', 'edge-tts', 'git+https://github.com/suno-ai/bark.git', 'python-dotenv' , 'av']16 17 print("Updating and installing system packages...")18 for package in packages:19 print(f"Installing {package}...")20 subprocess.check_call(['apt-get', 'install', '-qq', '-y', package])21 22 print("Updating and installing pip packages...")23 subprocess.check_call(['pip', 'install', '--upgrade'] + pip_packages)24 25 print('Packages up to date.')26 27 28def setup_environment(ForceUpdateDependencies, ForceTemporaryStorage):29 # Mounting Google Drive30 if not ForceTemporaryStorage:31 from google.colab import drive32 33 if not os.path.exists('/content/drive'):34 drive.mount('/content/drive')35 else:36 print('Drive is already mounted. Proceeding...')37 38 # Function to install dependencies with progress39 def install_packages():40 packages = ['build-essential', 'python3-dev', 'ffmpeg', 'aria2']41 pip_packages = ['pip', 'setuptools', 'wheel', 'httpx==0.23.0', 'faiss-gpu', 'fairseq', 'gradio==3.34.0',42 'ffmpeg', 'ffmpeg-python', 'praat-parselmouth', 'pyworld', 'numpy==1.23.5',43 'numba==0.56.4', 'librosa==0.9.2', 'mega.py', 'gdown', 'onnxruntime', 'pyngrok==4.1.12',44 'gTTS', 'elevenlabs', 'wget', 'tensorboardX', 'unidecode', 'huggingface-hub', 'stftpitchshift==1.5.1', 45 'yt-dlp', 'pedalboard', 'pathvalidate', 'nltk', 'edge-tts', 'git+https://github.com/suno-ai/bark.git', 'python-dotenv' , 'av']46 47 print("Updating and installing system packages...")48 for package in packages:49 print(f"Installing {package}...")50 subprocess.check_call(['apt-get', 'install', '-qq', '-y', package])51 52 print("Updating and installing pip packages...")53 subprocess.check_call(['pip', 'install', '--upgrade'] + pip_packages)54 55 56 print('Packages up to date.')57 58 # Function to scan a directory and writes filenames and timestamps59 def scan_and_write(base_path, output_file):60 with open(output_file, 'w', newline='') as f:61 writer = csv.writer(f)62 for dirpath, dirs, files in os.walk(base_path):63 for filename in files:64 fname = os.path.join(dirpath, filename)65 try:66 mtime = os.path.getmtime(fname)67 writer.writerow([fname, mtime])68 except Exception as e:69 print(f'Skipping irrelevant nonexistent file {fname}: {str(e)}')70 print(f'Finished recording filesystem timestamps to {output_file}.')71 72 # Function to compare files73 def compare_files(old_file, new_file):74 old_files = {}75 new_files = {}76 77 with open(old_file, 'r') as f:78 reader = csv.reader(f)79 old_files = {rows[0]:rows[1] for rows in reader}80 81 with open(new_file, 'r') as f:82 reader = csv.reader(f)83 new_files = {rows[0]:rows[1] for rows in reader}84 85 removed_files = old_files.keys() - new_files.keys()86 added_files = new_files.keys() - old_files.keys()87 unchanged_files = old_files.keys() & new_files.keys()88 89 changed_files = {f for f in unchanged_files if old_files[f] != new_files[f]}90 91 for file in removed_files:92 print(f'File has been removed: {file}')93 94 for file in changed_files:95 print(f'File has been updated: {file}')96 97 return list(added_files) + list(changed_files)98 99 # Check if CachedRVC.tar.gz exists100 if ForceTemporaryStorage:101 file_path = '/content/CachedRVC.tar.gz'102 else:103 file_path = '/content/drive/MyDrive/RVC_Cached/CachedRVC.tar.gz'104 105 content_file_path = '/content/CachedRVC.tar.gz'106 extract_path = '/'107 108 if not os.path.exists(file_path):109 folder_path = os.path.dirname(file_path)110 os.makedirs(folder_path, exist_ok=True)111 print('No cached dependency install found. Attempting to download GitHub backup..')112 113 try:114 download_url = "https://github.com/kalomaze/QuickMangioFixes/releases/download/release3/CachedRVC.tar.gz"115 subprocess.run(["wget", "-O", file_path, download_url])116 print('Download completed successfully!')117 except Exception as e:118 print('Download failed:', str(e))119 120 # Delete the failed download file121 if os.path.exists(file_path):122 os.remove(file_path)123 print('Failed download file deleted. Continuing manual backup..')124 125 if Path(file_path).exists():126 if ForceTemporaryStorage:127 print('Finished downloading CachedRVC.tar.gz.')128 else:129 print('CachedRVC.tar.gz found on Google Drive. Proceeding to copy and extract...')130 131 # Check if ForceTemporaryStorage is True and skip copying if it is132 if ForceTemporaryStorage:133 pass134 else:135 shutil.copy(file_path, content_file_path)136 137 print('Beginning backup copy operation...')138 139 with tarfile.open(content_file_path, 'r:gz') as tar:140 for member in tar.getmembers():141 target_path = os.path.join(extract_path, member.name)142 try:143 tar.extract(member, extract_path)144 except Exception as e:145 print('Failed to extract a file (this isn\'t normal)... forcing an update to compensate')146 ForceUpdateDependencies = True147 print(f'Extraction of {content_file_path} to {extract_path} completed.')148 149 if ForceUpdateDependencies:150 install_packages()151 ForceUpdateDependencies = False152 else:153 print('CachedRVC.tar.gz not found. Proceeding to create an index of all current files...')154 scan_and_write('/usr/', '/content/usr_files.csv')155 156 install_packages()157 158 scan_and_write('/usr/', '/content/usr_files_new.csv')159 changed_files = compare_files('/content/usr_files.csv', '/content/usr_files_new.csv')160 161 with tarfile.open('/content/CachedRVC.tar.gz', 'w:gz') as new_tar:162 for file in changed_files:163 new_tar.add(file)164 print(f'Added to tar: {file}')165 166 os.makedirs('/content/drive/MyDrive/RVC_Cached', exist_ok=True)167 shutil.copy('/content/CachedRVC.tar.gz', '/content/drive/MyDrive/RVC_Cached/CachedRVC.tar.gz')168 print('Updated CachedRVC.tar.gz copied to Google Drive.')169 print('Dependencies fully up to date; future runs should be faster.')170 171 