-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
127 lines (104 loc) · 4.86 KB
/
install.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os
import shutil
import subprocess
import sys
# Simple color print functions without dependencies
def print_info(message):
print("\033[96m{}\033[0m".format(message))
def print_warning(message):
print("\033[93m{}\033[0m".format(message))
def print_error(message):
print("\033[91m{}\033[0m".format(message))
def print_success(message):
print("\033[92m{}\033[0m".format(message))
def clear_console():
os.system('cls' if os.name == 'nt' else 'clear')
def install_dependencies():
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
print_success("Dependencies installed successfully.")
except subprocess.CalledProcessError as e:
print_error("Failed to install dependencies. Error: {}".format(e))
sys.exit(1)
def set_environment_variable(key, value):
os.environ[key] = value
shell_config = os.path.expanduser("~/.zshrc" if os.environ.get("SHELL", "").endswith("zsh") else "~/.bash_profile")
with open(shell_config, "a") as file:
file.write(f"\nexport {key}={value}\n")
print_success("Environment variable {} set to: {}".format(key, value))
def on_rm_error(func, path, exc_info):
import stat
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise
def move_repository_contents(source_dir, target_dir):
if os.path.exists(target_dir):
print_warning("Removing existing directory at {}".format(target_dir))
shutil.rmtree(target_dir, onerror=on_rm_error)
os.makedirs(target_dir)
for item in os.listdir(source_dir):
s = os.path.join(source_dir, item)
d = os.path.join(target_dir, item)
if os.path.isdir(s):
shutil.copytree(s, d, dirs_exist_ok=True)
else:
shutil.copy2(s, d)
print_success("Repository contents moved to {}".format(target_dir))
def add_to_path(directory):
shell_config = os.path.expanduser("~/.zshrc" if os.environ.get("SHELL", "").endswith("zsh") else "~/.bash_profile")
with open(shell_config, "a") as file:
file.write(f'\nexport PATH="$PATH:{directory}"\n')
print_success("Added {} to system PATH.".format(directory))
def create_script_file(directory):
script_content = "#!/bin/bash\npython3 {}/ai_diff_commit/src/ai_diff_commit.py $*\n".format(directory)
script_file_path = os.path.join(directory, "ai_diff_commit.sh")
with open(script_file_path, "w") as script_file:
script_file.write(script_content)
os.chmod(script_file_path, 0o755)
print_success("Script file created at {}".format(script_file_path))
def create_alias(scripts_dir):
shell_config = os.path.expanduser("~/.zshrc" if os.environ.get("SHELL", "").endswith("zsh") else "~/.bash_profile")
alias_command = f"alias ai_diff_commit='{scripts_dir}/ai_diff_commit.sh'\n"
with open(shell_config, "a") as file:
file.write(alias_command)
print_success("Alias for ai_diff_commit added to {}".format(shell_config))
def source_shell_config():
shell = os.environ.get("SHELL", "")
shell_config = os.path.expanduser("~/.zshrc" if shell.endswith("zsh") else "~/.bash_profile")
if shell.endswith("zsh"):
try:
subprocess.check_call(["zsh", "-c", f"source {shell_config}"])
print_success(f"Sourced {shell_config} to apply changes.")
except subprocess.CalledProcessError:
print_warning(f"Failed to source {shell_config}. Please run 'source {shell_config}' manually.")
else:
print_warning(f"Automatic sourcing is not supported for the current shell ({shell}). Please run 'source {shell_config}' manually.")
def main():
clear_console()
source_dir = os.getcwd()
home_dir = os.path.expanduser("~")
scripts_dir = os.path.join(home_dir, "Scripts")
target_dir = os.path.join(scripts_dir, "ai_diff_commit")
print_info("Installing dependencies...")
install_dependencies()
if not os.environ.get("OPENAI_API_KEY"):
api_key = input("Please enter your OpenAI API key: ")
print_info("Setting environment variable...")
set_environment_variable("OPENAI_API_KEY", api_key)
else:
print_warning("Environment variable OPENAI_API_KEY already exists.")
print_info("Moving repository contents...")
move_repository_contents(source_dir, target_dir)
print_info("Adding to system PATH...")
add_to_path(scripts_dir)
print_info("Creating script file...")
create_script_file(scripts_dir)
print_info("Creating alias for the command...")
create_alias(scripts_dir)
print_info("Sourcing shell configuration file to apply changes...")
source_shell_config()
print_success("Installation completed successfully. If the command 'ai_diff_commit' is still not found, please restart your terminal or run 'source ~/.zshrc' or 'source ~/.bash_profile' manually.")
if __name__ == "__main__":
main()