-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
83 lines (64 loc) · 2.77 KB
/
setup.py
File metadata and controls
83 lines (64 loc) · 2.77 KB
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
import os
import subprocess
import json
import sys
from pathlib import Path
CONFIG_PATH = os.path.expanduser("~/.gitcommit.json")
INSTALL_PATH = "/usr/local/bin/gitcommit"
VENV_PATH = os.path.expanduser("~/.gitcommit-venv")
def create_config():
"""Create or update the configuration file."""
print("Setting up gitcommit...")
api_key = input("Enter your OpenAI API Key: ").strip()
model = input("Enter the OpenAI model to use (default: gpt-4o): ").strip() or "gpt-4o"
max_tokens = input("Enter max tokens for the response (default: 300): ").strip() or "300"
config = {
"api_key": api_key,
"model": model,
"max_tokens": int(max_tokens),
}
with open(CONFIG_PATH, "w") as f:
json.dump(config, f, indent=4)
print(f"Configuration saved to {CONFIG_PATH}.")
def create_virtual_env():
"""Create a virtual environment for gitcommit."""
if not os.path.exists(VENV_PATH):
print(f"Creating virtual environment at {VENV_PATH}...")
subprocess.run([sys.executable, "-m", "venv", VENV_PATH], check=True)
pip_path = Path(VENV_PATH) / "bin" / "pip"
print("Installing required packages in the virtual environment...")
subprocess.run([str(pip_path), "install", "--upgrade", "pip"], check=True)
subprocess.run([str(pip_path), "install", "openai"], check=True)
def install_script():
"""Install the gitcommit script."""
script_source = Path(__file__).resolve().parent / "gitcommit.py"
venv_python = Path(VENV_PATH) / "bin" / "python"
if not script_source.exists():
raise FileNotFoundError(f"Cannot find gitcommit.py at {script_source}.")
script_content = f"#!{venv_python}\n" + script_source.read_text()
try:
# Ensure target directory exists
os.makedirs(os.path.dirname(INSTALL_PATH), exist_ok=True)
with open(INSTALL_PATH, "w") as f:
f.write(script_content)
os.chmod(INSTALL_PATH, 0o755)
except (PermissionError, FileNotFoundError):
print("Permission or path issue. Retrying with sudo...")
temp_path = "/tmp/gitcommit.py"
with open(temp_path, "w") as f:
f.write(script_content)
# Ensure /usr/local/bin exists with sudo
subprocess.run(["sudo", "mkdir", "-p", os.path.dirname(INSTALL_PATH)], check=True)
subprocess.run(["sudo", "mv", temp_path, INSTALL_PATH], check=True)
subprocess.run(["sudo", "chmod", "755", INSTALL_PATH], check=True)
print(f"gitcommit installed successfully at {INSTALL_PATH}.")
def main():
try:
create_config()
create_virtual_env()
install_script()
print("\nSetup complete! Use `gitcommit` to generate commit messages.")
except Exception as e:
print(f"Error during setup: {e}")
if __name__ == "__main__":
main()