Files
cmg-commands/install_scripts.py
2024-12-14 13:22:01 -06:00

95 lines
3.2 KiB
Python

import os
import shutil
import sys
import argparse
def find_files_starting_with(prefix):
# Get the list of all files and directories in the current working directory
items = os.listdir('.')
# Filter the list to include only files that start with the specified prefix
matching_files = [item for item in items if os.path.isfile(item) and item.startswith(prefix)]
return matching_files
def get_current_shell_name():
# Get the SHELL environment variable
current_shell = os.getenv('SHELL')
if current_shell:
# Extract the name of the shell (basename)
shell_name = os.path.basename(current_shell)
print(f"The current shell is: {shell_name}")
return shell_name
else:
print("Unable to determine the current shell.")
return None
def copy_files_to_bin(source_files, bin_path):
"""Copy specified files to the bin directory."""
for file_name in source_files:
source_file = os.path.join(os.getcwd(), file_name)
if os.path.exists(source_file) and os.path.isfile(source_file):
destination_file = os.path.join(bin_path, file_name)
shutil.copy2(source_file, destination_file)
print(f"Copied {source_file} to {destination_file}")
else:
print(f"File not found: {source_file}")
def ensure_bin_in_path(bin_path):
"""Ensure the bin directory is in the system's PATH environment variable."""
if bin_path in sys.path:
print(f"{bin_path} is already in the system path.")
else:
current_shell = get_current_shell_name()
if current_shell == 'zsh':
path_file = open(os.path.join(os.path.expanduser("~"), ".zshrc"), 'a')
elif current_shell == 'bash':
path_file = open(os.path.join(os.path.expanduser("~"), ".bashrc"), 'a')
else:
path_file = None
print(f"Appending {bin_path} to system' PATH.")
if path_file:
path_file.write(f"export PATH=$PATH:{bin_path}\n")
print(f"Added {bin_path} to the system path.")
def main():
# Define the prefix
prefix = "cmg-"
# Create an argument parser object
parser = argparse.ArgumentParser(description='Check for a specific flag.')
# Add a flag argument to the parser
parser.add_argument('-u', '--upgrade', action="store_true", help='Specify to not copy ~/bin/ to PATH')
# Parse the command-line arguments
args = parser.parse_args()
# Find all files starting with the given prefix
source_files = find_files_starting_with(prefix)
# source_files = ['cmg-get-drive-link.py', 'cmg-split-zip-smaller-chunks.py'] # Add your script names here
# Get home directory and create bin directory if it does not exist
home_dir = os.path.expanduser('~')
bin_path = os.path.join(home_dir, 'bin')
if not os.path.exists(bin_path):
os.makedirs(bin_path)
print(f"Created {bin_path}")
else:
print(f"{bin_path} already exists.")
# Copy files to the bin directory
copy_files_to_bin(source_files, bin_path)
# Ensure bin is in the system path
if not args.upgrade:
ensure_bin_in_path(bin_path)
if __name__ == "__main__":
main()