forked from Shogun89/fancy_job
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_number.py
125 lines (98 loc) · 3.67 KB
/
update_number.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
#!/usr/bin/env python3
import os
import random
import subprocess
from datetime import datetime
script_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(script_dir)
def read_number():
with open("number.txt", "r") as f:
return int(f.read().strip())
def write_number(num):
with open("number.txt", "w") as f:
f.write(str(num))
def generate_random_commit_message():
from transformers import pipeline
generator = pipeline(
"text-generation",
model="openai-community/gpt2",
)
prompt = """
Generate a Git commit message following the Conventional Commits standard. The message should include a type, an optional scope, and a subject.Please keep it short. Here are some examples:
- feat(auth): add user authentication module
- fix(api): resolve null pointer exception in user endpoint
- docs(readme): update installation instructions
- chore(deps): upgrade lodash to version 4.17.21
- refactor(utils): simplify date formatting logic
Now, generate a new commit message:
"""
generated = generator(
prompt,
max_new_tokens=50,
num_return_sequences=1,
temperature=0.9, # Slightly higher for creativity
top_k=50, # Limits sampling to top 50 logits
top_p=0.9, # Nucleus sampling for diversity
truncation=True,
)
text = generated[0]["generated_text"]
if "- " in text:
return text.rsplit("- ", 1)[-1].strip()
else:
raise ValueError(f"Unexpected generated text {text}")
def git_commit():
# Stage the changes
subprocess.run(["git", "add", "number.txt"])
# Create commit with current date
if "FANCY_JOB_USE_LLM" in os.environ:
commit_message = generate_random_commit_message()
else:
date = datetime.now().strftime("%Y-%m-%d")
commit_message = f"Update number: {date}"
subprocess.run(["git", "commit", "-m", commit_message])
def git_push():
# Push the committed changes to GitHub
result = subprocess.run(["git", "push"], capture_output=True, text=True)
if result.returncode == 0:
print("Changes pushed to GitHub successfully.")
else:
print("Error pushing to GitHub:")
print(result.stderr)
def update_cron_with_random_time():
# Generate random hour (0-23) and minute (0-59)
random_hour = random.randint(0, 23)
random_minute = random.randint(0, 59)
# Define the new cron job command
new_cron_command = f"{random_minute} {random_hour} * * * cd {script_dir} && python3 {os.path.join(script_dir, 'update_number.py')}\n"
# Get the current crontab
cron_file = "/tmp/current_cron"
os.system(
f"crontab -l > {cron_file} 2>/dev/null || true"
) # Save current crontab, or create a new one if empty
# Update the crontab file
with open(cron_file, "r") as file:
lines = file.readlines()
with open(cron_file, "w") as file:
for line in lines:
# Remove existing entry for `update_number.py` if it exists
if "update_number.py" not in line:
file.write(line)
# Add the new cron job at the random time
file.write(new_cron_command)
# Load the updated crontab
os.system(f"crontab {cron_file}")
os.remove(cron_file)
print(f"Cron job updated to run at {random_hour}:{random_minute} tomorrow.")
def main():
try:
current_number = read_number()
new_number = current_number + 1
write_number(new_number)
git_commit()
git_push()
update_cron_with_random_time()
except Exception as e:
print(f"Error: {str(e)}")
exit(1)
if __name__ == "__main__":
main()