-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuto_Commit.py
70 lines (56 loc) · 2.29 KB
/
Auto_Commit.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
"""
# Author: Hamed Gharghi
# Date: 2024-08-04
# Description: This script automates the process of creating a Python file with a comment header, committing it to a GitHub repository, and pushing the changes.
"""
import os
import subprocess
from datetime import datetime
import re
import time
# Configuration (Update these with your own values before running)
REPO_PATH = 'path_to_your_repository' # Replace with your repository path
GITHUB_USERNAME = 'your_github_username' # Replace with your GitHub username
GITHUB_TOKEN = 'your_github_token' # Replace with your GitHub token
def sanitize_filename(filename):
"""Sanitize filename to ensure it's valid."""
return re.sub(r'[<>:"/\\|?*\x00-\x1F]', '', filename)
def write_file(file_path, content):
"""Write content to a file."""
with open(file_path, 'w') as f:
f.write(content)
def make_commit():
"""Create and commit a file to the Git repository."""
os.chdir(REPO_PATH)
# Define filename and content
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
filename = f'generated_script_{timestamp}.py'
# Add comment header
header = f"""\
# Author: Hamed Gharghi
# Date: {datetime.now().strftime('%Y-%m-%d')}
# Description: This script is generated by the automated commit bot and added to the repository.
"""
# Example content for the new file
code_content = """\
# This is a placeholder for the actual script content.
print("Hello, world!")
"""
code_with_header = header + '\n' + code_content
# Write code to file
file_path = os.path.join(REPO_PATH, filename)
write_file(file_path, code_with_header)
# Add changes to git
subprocess.run(['git', 'add', filename], check=True)
# Commit changes
commit_message = f'Automated commit: Added {filename} with a comment header'
subprocess.run(['git', 'commit', '-m', commit_message], check=True)
# Push changes to GitHub
# Configure Git to use token for this repository
subprocess.run(['git', 'remote', 'set-url', 'origin', f'https://{GITHUB_USERNAME}:{GITHUB_TOKEN}@github.com/{GITHUB_USERNAME}/your_repository_name.git'], check=True)
subprocess.run(['git', 'push', 'origin', 'main'], check=True)
if __name__ == "__main__":
make_commit()
while True:
time.sleep(86400) # Sleep for 24 hours
make_commit()