|
|
@ -7,6 +7,9 @@ import platform |
|
|
|
from dotenv import load_dotenv |
|
|
|
from dotenv import load_dotenv |
|
|
|
from requests.exceptions import HTTPError |
|
|
|
from requests.exceptions import HTTPError |
|
|
|
from tqdm import tqdm |
|
|
|
from tqdm import tqdm |
|
|
|
|
|
|
|
import zipfile |
|
|
|
|
|
|
|
import tempfile |
|
|
|
|
|
|
|
import shutil |
|
|
|
|
|
|
|
|
|
|
|
current_directory = os.path.dirname(os.path.realpath(__file__)) |
|
|
|
current_directory = os.path.dirname(os.path.realpath(__file__)) |
|
|
|
config_directory = os.path.expanduser("~/.config/fabric") |
|
|
|
config_directory = os.path.expanduser("~/.config/fabric") |
|
|
@ -214,124 +217,101 @@ class Standalone: |
|
|
|
|
|
|
|
|
|
|
|
class Update: |
|
|
|
class Update: |
|
|
|
def __init__(self): |
|
|
|
def __init__(self): |
|
|
|
""" Initialize the object with default values and update patterns. |
|
|
|
"""Initialize the object with default values.""" |
|
|
|
|
|
|
|
self.repo_zip_url = "https://github.com/danielmiessler/fabric/archive/refs/heads/main.zip" |
|
|
|
This method initializes the object with default values for root_api_url, config_directory, and pattern_directory. |
|
|
|
|
|
|
|
It then creates the pattern_directory if it does not exist and calls the update_patterns method to update the patterns. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Raises: |
|
|
|
|
|
|
|
OSError: If there is an issue creating the pattern_directory. |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.root_api_url = "https://api.github.com/repos/danielmiessler/fabric/contents/patterns?ref=main" |
|
|
|
|
|
|
|
self.config_directory = os.path.expanduser("~/.config/fabric") |
|
|
|
self.config_directory = os.path.expanduser("~/.config/fabric") |
|
|
|
self.pattern_directory = os.path.join( |
|
|
|
self.pattern_directory = os.path.join( |
|
|
|
self.config_directory, "patterns") |
|
|
|
self.config_directory, "patterns") |
|
|
|
os.makedirs(self.pattern_directory, exist_ok=True) |
|
|
|
os.makedirs(self.pattern_directory, exist_ok=True) |
|
|
|
self.update_patterns() # Call the update process from a method. |
|
|
|
self.update_patterns() # Start the update process immediately |
|
|
|
|
|
|
|
|
|
|
|
def update_patterns(self): |
|
|
|
def update_patterns(self): |
|
|
|
""" Update the patterns by downloading from the GitHub directory. |
|
|
|
"""Update the patterns by downloading the zip from GitHub and extracting it.""" |
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as temp_dir: |
|
|
|
Raises: |
|
|
|
zip_path = os.path.join(temp_dir, "repo.zip") |
|
|
|
HTTPError: If there is an HTTP error while downloading patterns. |
|
|
|
self.download_zip(self.repo_zip_url, zip_path) |
|
|
|
""" |
|
|
|
extracted_folder_path = self.extract_zip(zip_path, temp_dir) |
|
|
|
|
|
|
|
# The patterns folder will be inside "fabric-main" after extraction |
|
|
|
try: |
|
|
|
patterns_source_path = os.path.join( |
|
|
|
self.progress_bar = tqdm(desc="Downloading Patterns…", unit="file") |
|
|
|
extracted_folder_path, "fabric-main", "patterns") |
|
|
|
self.get_github_directory_contents( |
|
|
|
if os.path.exists(patterns_source_path): |
|
|
|
self.root_api_url, self.pattern_directory |
|
|
|
# If the patterns directory already exists, remove it before copying over the new one |
|
|
|
) |
|
|
|
if os.path.exists(self.pattern_directory): |
|
|
|
# Close progress bar on success before printing the message. |
|
|
|
shutil.rmtree(self.pattern_directory) |
|
|
|
self.progress_bar.close() |
|
|
|
shutil.copytree(patterns_source_path, self.pattern_directory) |
|
|
|
except HTTPError as e: |
|
|
|
print("Patterns updated successfully.") |
|
|
|
# Ensure progress bar is closed on HTTPError as well. |
|
|
|
|
|
|
|
self.progress_bar.close() |
|
|
|
|
|
|
|
if e.response.status_code == 403: |
|
|
|
|
|
|
|
print( |
|
|
|
|
|
|
|
"GitHub API rate limit exceeded. Please wait before trying again." |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
sys.exit() |
|
|
|
|
|
|
|
else: |
|
|
|
else: |
|
|
|
print(f"Failed to download patterns due to an HTTP error: {e}") |
|
|
|
print("Patterns folder not found in the downloaded zip.") |
|
|
|
sys.exit() # Exit after handling the error. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def download_file(self, url, local_path): |
|
|
|
|
|
|
|
""" Download a file from the given URL and save it to the local path. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Args: |
|
|
|
|
|
|
|
url (str): The URL of the file to be downloaded. |
|
|
|
|
|
|
|
local_path (str): The local path where the file will be saved. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Raises: |
|
|
|
|
|
|
|
HTTPError: If an HTTP error occurs during the download process. |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
response = requests.get(url) |
|
|
|
|
|
|
|
response.raise_for_status() |
|
|
|
|
|
|
|
with open(local_path, "wb") as f: |
|
|
|
|
|
|
|
f.write(response.content) |
|
|
|
|
|
|
|
self.progress_bar.update(1) |
|
|
|
|
|
|
|
except HTTPError as e: |
|
|
|
|
|
|
|
print(f"Failed to download file {url}. HTTP error: {e}") |
|
|
|
|
|
|
|
sys.exit() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_item(self, item, local_dir): |
|
|
|
def download_zip(self, url, save_path): |
|
|
|
""" Process the given item and save it to the local directory. |
|
|
|
"""Download the zip file from the specified URL.""" |
|
|
|
|
|
|
|
response = requests.get(url) |
|
|
|
|
|
|
|
response.raise_for_status() # Check if the download was successful |
|
|
|
|
|
|
|
with open(save_path, 'wb') as f: |
|
|
|
|
|
|
|
f.write(response.content) |
|
|
|
|
|
|
|
print("Downloaded zip file successfully.") |
|
|
|
|
|
|
|
|
|
|
|
Args: |
|
|
|
def extract_zip(self, zip_path, extract_to): |
|
|
|
item (dict): The item to be processed, containing information about the type, download URL, name, and URL. |
|
|
|
"""Extract the zip file to the specified directory.""" |
|
|
|
local_dir (str): The local directory where the item will be saved. |
|
|
|
with zipfile.ZipFile(zip_path, 'r') as zip_ref: |
|
|
|
|
|
|
|
zip_ref.extractall(extract_to) |
|
|
|
Returns: |
|
|
|
print("Extracted zip file successfully.") |
|
|
|
None |
|
|
|
return extract_to # Return the path to the extracted contents |
|
|
|
|
|
|
|
|
|
|
|
Raises: |
|
|
|
|
|
|
|
OSError: If there is an issue creating the new directory using os.makedirs. |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if item["type"] == "file": |
|
|
|
|
|
|
|
self.download_file( |
|
|
|
|
|
|
|
item["download_url"], os.path.join(local_dir, item["name"]) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
elif item["type"] == "dir": |
|
|
|
|
|
|
|
new_dir = os.path.join(local_dir, item["name"]) |
|
|
|
|
|
|
|
os.makedirs(new_dir, exist_ok=True) |
|
|
|
|
|
|
|
self.get_github_directory_contents(item["url"], new_dir) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_github_directory_contents(self, api_url, local_dir): |
|
|
|
|
|
|
|
""" Get the contents of a directory from GitHub API and process each item. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Args: |
|
|
|
|
|
|
|
api_url (str): The URL of the GitHub API endpoint for the directory. |
|
|
|
|
|
|
|
local_dir (str): The local directory where the contents will be processed. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Returns: |
|
|
|
|
|
|
|
None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Raises: |
|
|
|
|
|
|
|
HTTPError: If an HTTP error occurs while fetching the directory contents. |
|
|
|
|
|
|
|
If the status code is 403, it prints a message about GitHub API rate limit exceeded |
|
|
|
|
|
|
|
and closes the progress bar. For any other status code, it prints a message |
|
|
|
|
|
|
|
about failing to fetch directory contents due to an HTTP error. |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
class Alias: |
|
|
|
response = requests.get(api_url) |
|
|
|
def __init__(self): |
|
|
|
response.raise_for_status() |
|
|
|
self.config_files = [] |
|
|
|
jsonList = response.json() |
|
|
|
home_directory = os.path.expanduser("~") |
|
|
|
for item in jsonList: |
|
|
|
self.patterns = os.path.join(home_directory, ".config/fabric/patterns") |
|
|
|
self.process_item(item, local_dir) |
|
|
|
if os.path.exists(os.path.join(home_directory, ".bashrc")): |
|
|
|
except HTTPError as e: |
|
|
|
self.config_files.append(os.path.join(home_directory, ".bashrc")) |
|
|
|
if e.response.status_code == 403: |
|
|
|
if os.path.exists(os.path.join(home_directory, ".zshrc")): |
|
|
|
print( |
|
|
|
self.config_files.append(os.path.join(home_directory, ".zshrc")) |
|
|
|
"GitHub API rate limit exceeded. Please wait before trying again." |
|
|
|
if os.path.exists(os.path.join(home_directory, ".bash_profile")): |
|
|
|
) |
|
|
|
self.config_files.append(os.path.join( |
|
|
|
self.progress_bar.close() # Ensure the progress bar is cleaned up properly |
|
|
|
home_directory, ".bash_profile")) |
|
|
|
else: |
|
|
|
self.remove_all_patterns() |
|
|
|
print( |
|
|
|
self.add_patterns() |
|
|
|
f"Failed to fetch directory contents due to an HTTP error: {e}") |
|
|
|
|
|
|
|
|
|
|
|
def add(self, name, alias): |
|
|
|
|
|
|
|
for file in self.config_files: |
|
|
|
|
|
|
|
with open(file, "a") as f: |
|
|
|
|
|
|
|
f.write(f"alias {name}='{alias}'\n") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove(self, pattern): |
|
|
|
|
|
|
|
for file in self.config_files: |
|
|
|
|
|
|
|
# Read the whole file first |
|
|
|
|
|
|
|
with open(file, "r") as f: |
|
|
|
|
|
|
|
wholeFile = f.read() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Determine if the line to be removed is in the file |
|
|
|
|
|
|
|
target_line = f"alias {pattern}='fabric --pattern {pattern}'\n" |
|
|
|
|
|
|
|
if target_line in wholeFile: |
|
|
|
|
|
|
|
# If the line exists, replace it with nothing (remove it) |
|
|
|
|
|
|
|
wholeFile = wholeFile.replace(target_line, "") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Write the modified content back to the file |
|
|
|
|
|
|
|
with open(file, "w") as f: |
|
|
|
|
|
|
|
f.write(wholeFile) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_all_patterns(self): |
|
|
|
|
|
|
|
allPatterns = os.listdir(self.patterns) |
|
|
|
|
|
|
|
for pattern in allPatterns: |
|
|
|
|
|
|
|
self.remove(pattern) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_line(self, name): |
|
|
|
|
|
|
|
for file in self.config_files: |
|
|
|
|
|
|
|
with open(file, "r") as f: |
|
|
|
|
|
|
|
lines = f.readlines() |
|
|
|
|
|
|
|
for line in lines: |
|
|
|
|
|
|
|
if line.strip("\n") == f"alias ${name}='{alias}'": |
|
|
|
|
|
|
|
return line |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_patterns(self): |
|
|
|
|
|
|
|
allPatterns = os.listdir(self.patterns) |
|
|
|
|
|
|
|
for pattern in allPatterns: |
|
|
|
|
|
|
|
self.add(pattern, f"fabric --pattern {pattern}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Setup: |
|
|
|
class Setup: |
|
|
@ -374,7 +354,6 @@ class Setup: |
|
|
|
""" |
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
Update() |
|
|
|
Update() |
|
|
|
sys.exit() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run(self): |
|
|
|
def run(self): |
|
|
|
""" Execute the Fabric program. |
|
|
|
""" Execute the Fabric program. |
|
|
|