-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
152 lines (125 loc) · 4.5 KB
/
bot.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
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
152
import subprocess
import time
import glob
import os
import sys
import argparse
from pyrogram import filters
from dotenv import load_dotenv
from pyrogram.client import Client
from pyrogram.enums import ParseMode
from pyrogram.types import Message
# load environment variables -
# from secrets keys GitHub
load_dotenv()
elapsed_minutes = 0
elapsed_seconds = 0
elapsed_minutes_formatted = ""
chat_id = int(os.getenv('CHAT_ID', 0))
api_id = int(os.getenv('API_ID', 0))
api_hash = os.getenv('API_HASH', '')
bot_token = os.getenv('BOT_TOKEN', '')
app = Client(
name="cache",
api_id=api_id,
api_hash=api_hash,
bot_token=bot_token
)
if os.path.isfile("build_count.txt"):
with open("build_count.txt", "r") as file:
build_count = int(file.read())
else:
build_count = 0
build_count += 1
with open("build_count.txt", "w") as file:
file.write(str(build_count))
commit_head = subprocess.check_output(
"git log --oneline -1 --pretty=format:'%h - %an'",
shell=True).decode().strip()
commit_id = subprocess.check_output(
"git log --oneline -1 --pretty=format:'%h'",
shell=True).decode().strip()
author_name = commit_head.split(" - ")[1]
commit_hash = commit_head.split(" - ")[0]
kernel_version = subprocess.check_output(
"make kernelversion 2>/dev/null",
shell=True
).decode().strip()
parser = argparse.ArgumentParser()
parser.add_argument(
"-t", "--build-type",
choices=["dev", "release"],
default="release",
help="Specify the build type (dev or release)"
)
args = parser.parse_args()
build_type = args.build_type
tag = f"ginkgo_{commit_hash[:7]}_{time.strftime('%Y%m%d')}"
@app.on_message(filters.command("compile") & filters.user(chat_id))
async def message_compile(bot: Client, msg: Message):
global elapsed_minutes, elapsed_seconds, elapsed_minutes_formatted
start_time = time.perf_counter()
start_message = await bot.send_message(
chat_id,
"Compilation started... please wait."
)
return_code = subprocess.call(
"./ksu_update.sh -t stable",
shell=True
)
return_code = subprocess.call(
"./moe.sh",
shell=True
)
if return_code == 0:
commit_head = subprocess.check_output("git log --oneline -1 --pretty=format:'%h - %an'", shell=True).decode().strip()
commit_id = subprocess.check_output("git log --oneline -1 --pretty=format:'%h'", shell=True).decode().strip()
author_name = commit_head.split(" - ")[1]
commit_hash = commit_head.split(" - ")[0]
message_commit = subprocess.check_output("git log --oneline -1", shell=True).decode().strip().split(" ")[1:]
commit_text = " ".join(message_commit)
commit_link = f'<a href="https://github.com/MoeKernel/android_kernel_xiaomi_ginkgo/commit/{commit_hash}">{commit_head}</a>'
elapsed_time = int(time.perf_counter() - start_time)
elapsed_minutes = elapsed_time // 60
elapsed_seconds = elapsed_time % 60
elapsed_minutes_formatted = "{:.2f}".format(
elapsed_minutes + elapsed_seconds / 60
)
completion_message = f"\nCompleted in {elapsed_minutes_formatted} minute(s) and {elapsed_seconds} second(s) !"
completed_compile_text = f"**Compilation completed!**\n\nCommit: {commit_link}\n{completion_message}"
build_info = f"**ginkgo build (#{build_count}) has succeeded**\n" \
f"**Kernel Version**: {kernel_version}\n" \
f"**Build Type**: `{build_type}` **(KSU/Fourteen)**\n" \
f"**Tag**: `{tag}`\n" \
f"\n" \
f"**Duration**: {elapsed_minutes} Minutes {elapsed_seconds} Seconds" \
f"\n" \
f"\n@MoeKernel #ginkgo #ksu"
await start_message.edit_text(text=completed_compile_text)
zip_files = glob.glob("*.zip")
if zip_files:
zip_file = zip_files[0]
caption = f"""**Build Information**
• **Commit**: `{commit_id}`
• **Message**: `{commit_text}`
• **Author**: `{author_name}`"""
await bot.send_document(
chat_id,
zip_file,
caption=caption,
parse_mode=ParseMode.MARKDOWN
)
await bot.send_message(
"@MoeNyanCI",
build_info,
parse_mode=ParseMode.MARKDOWN
)
sys.exit(0)
else:
await bot.send_message(
chat_id,
"No zip files found in the current directory."
)
sys.exit(0)
print("bot is runnig...")
app.run()