-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_to_github_fixed.py
More file actions
138 lines (115 loc) · 4.85 KB
/
push_to_github_fixed.py
File metadata and controls
138 lines (115 loc) · 4.85 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
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
128
129
130
131
132
133
134
135
136
137
138
# BEGIN: user added these matplotlib lines to ensure any plots do not pop-up in their UI
import matplotlib
matplotlib.use('Agg') # Set the backend to non-interactive
import matplotlib.pyplot as plt
plt.ioff()
import os
os.environ['TERM'] = 'dumb'
# END: user added these matplotlib lines to ensure any plots do not pop-up in their UI
# filename: push_to_github_fixed.py
# execution: true
import os
import subprocess
def run_command(command):
"""Run a shell command and return the output."""
try:
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
return result.stdout.strip()
except subprocess.CalledProcessError as e:
print(f"Error executing command: {command}")
print(f"Error message: {e.stderr}")
return None
def setup_git_repo(repo_url, token, branch="main"):
"""Set up the Git repository."""
# Check if git is installed
if run_command("git --version") is None:
print("Git is not installed or not in PATH.")
return False
# Initialize git repo if not already initialized
if not os.path.exists(".git"):
print("Initializing Git repository...")
if run_command("git init") is None:
return False
# Configure Git credentials
print("Configuring Git credentials...")
run_command("git config --local user.name 'PromptShield Bot'")
run_command("git config --local user.email 'bot@promptshield.ai'")
# Add the remote repository with token authentication
print(f"Adding remote repository: {repo_url}")
# First remove any existing remote with the same name
run_command("git remote remove origin 2>/dev/null || true")
# Add the new remote with token authentication
auth_url = repo_url.replace("https://", f"https://{token}@")
if run_command(f"git remote add origin {auth_url}") is None:
return False
return True
def commit_and_push(branch="main"):
"""Commit all changes and push to the repository."""
# Add all files
print("Adding files to Git...")
if run_command("git add .") is None:
return False
# Commit changes
print("Committing changes...")
commit_message = "Initial commit of PromptShield middleware"
if run_command(f"git commit -m '{commit_message}'") is None:
# If commit fails, it might be because there are no changes
print("No changes to commit or commit failed.")
return False
# Create branch if it doesn't exist
print(f"Creating branch: {branch}...")
run_command(f"git checkout -b {branch}")
# Push to the remote repository
print(f"Pushing to branch: {branch}...")
if run_command(f"git push -u origin {branch}") is None:
return False
return True
def list_files():
"""List all files that will be pushed."""
files = []
for root, dirs, filenames in os.walk("."):
# Skip .git directory
if ".git" in dirs:
dirs.remove(".git")
# Skip any hidden directories
dirs[:] = [d for d in dirs if not d.startswith(".")]
for filename in filenames:
# Skip hidden files and temporary files
if not filename.startswith(".") and not filename.startswith("tmp_"):
path = os.path.join(root, filename)
files.append(path)
return files
def main():
"""Main function to push code to GitHub."""
# Repository URL and token from the requirements
repo_url = "https://github.com/harshadindigal/PromptGuard"
token = "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" # Token from the original requirements
branch = "main"
print("Preparing to push files to GitHub...")
print(f"Repository: {repo_url}")
print(f"Branch: {branch}")
# List files that will be pushed
files = list_files()
print(f"\nFound {len(files)} files to push:")
for file in files[:10]: # Show first 10 files
print(f" {file}")
if len(files) > 10:
print(f" ... and {len(files) - 10} more files")
# Set up the repository
if not setup_git_repo(repo_url, token, branch):
print("Failed to set up Git repository.")
return
# Commit and push changes
if commit_and_push(branch):
print(f"\nSuccessfully pushed code to {repo_url} on branch {branch}.")
else:
print("\nFailed to push code to GitHub.")
print("\nThis might be because the token has expired or lacks permissions.")
print("To push the code manually, you can use these commands:")
print("\n git checkout -b main")
print(" git push -u origin main")
print("\nOr if you need to authenticate with a new token:")
print(" git remote set-url origin https://USERNAME:NEW_TOKEN@github.com/harshadindigal/PromptGuard.git")
print(" git push -u origin main")
if __name__ == "__main__":
main()