-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblocklists_git_push.py
More file actions
151 lines (124 loc) · 4.57 KB
/
blocklists_git_push.py
File metadata and controls
151 lines (124 loc) · 4.57 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
139
140
141
142
143
144
145
146
147
148
149
150
151
"""Git push script for automated blocklist updates in CI environment."""
import logging, subprocess, sys
from pathlib import Path
# Setup logger
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
def run_git_command(cmd: list, description: str) -> None:
"""Run a git command with proper error handling."""
try:
logging.info(f"{description}...")
result = subprocess.run(
cmd,
capture_output=True,
text=True,
check=True,
timeout=300 # 5 minutes timeout
)
if result.stdout.strip():
logging.info(f"Output: {result.stdout.strip()}")
except subprocess.CalledProcessError as e:
logging.error(f"Command failed: {' '.join(cmd)}")
logging.error(f"Exit code: {e.returncode}")
if e.stdout:
logging.error(f"Stdout: {e.stdout}")
if e.stderr:
logging.error(f"Stderr: {e.stderr}")
raise
except subprocess.TimeoutExpired:
logging.error(f"Command timed out: {' '.join(cmd)}")
raise
def add_gitignore_entries() -> None:
"""Add necessary entries to .gitignore."""
gitignore_path = Path(".gitignore")
gitignore_entries = [".gitignore", "venv_blocklist/", "maxmind/"]
try:
with gitignore_path.open("a", encoding="utf-8") as f:
for entry in gitignore_entries:
f.write(f"{entry}\n")
logging.info("Updated .gitignore")
except OSError as e:
logging.warning(f"Could not update .gitignore: {e}")
def main() -> None:
"""Main function to handle git operations for blocklist updates."""
if len(sys.argv) != 2:
logging.error("Usage: python blocklists_git_push.py <ACCESS_TOKEN>")
sys.exit(1)
access_token = sys.argv[1]
commit_message = "[bot] Update blocklists"
commit_author = "Maxime Wewer"
commit_email = "[email protected]"
repo_url = f"https://MaximeWewer{access_token}@github.com/MaximeWewer/HeimdallBlocklists.git"
branch_name = "main"
# Paths to add (only existing ones will be added)
paths_to_add = [
"./blocklists",
"./blocklists_split",
"./statistics",
"README.md"
]
try:
logging.info("Starting git push process")
# Add gitignore entries
add_gitignore_entries()
# Configure git user (required for CI)
run_git_command(
["git", "config", "user.email", commit_email],
"Setting git user email"
)
run_git_command(
["git", "config", "user.name", commit_author],
"Setting git user name"
)
# Add existing files/directories
paths_added = []
for path in paths_to_add:
if Path(path).exists():
run_git_command(
["git", "add", path],
f"Adding {path}"
)
paths_added.append(path)
else:
logging.debug(f"Path does not exist, skipping: {path}")
# Check if there are changes to commit
try:
result = subprocess.run(
["git", "diff", "--cached", "--name-only"],
capture_output=True,
text=True,
check=True
)
if not result.stdout.strip():
logging.info("No changes to commit")
return
changed_files = result.stdout.strip().split('\n')
logging.info(f"Found {len(changed_files)} changed files")
for file in changed_files[:10]: # Show first 10
logging.info(f" - {file}")
except subprocess.CalledProcessError as e:
logging.error(f"Failed to check git status: {e}")
return
# Create commit
run_git_command(
["git", "commit", "-m", commit_message],
"Creating commit"
)
# Set remote URL
run_git_command(
["git", "remote", "set-url", "origin", repo_url],
"Setting remote URL"
)
# Push to remote
run_git_command(
["git", "push", "origin", f"HEAD:{branch_name}"],
"Pushing to remote"
)
logging.info("Push completed successfully")
except Exception as e:
logging.error(f"Git operation failed: {e}")
sys.exit(1)
if __name__ == "__main__":
main()