-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
146 lines (108 loc) · 4.16 KB
/
script.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
# Built in modules
import os
import urllib
import re
import shutil
import smtplib
from email import message
import datetime
# Pip modules
from github import Github
from dotenv import load_dotenv
from git import Repo
from gnupg import GPG
###############################################################################
### Setting up the environment
print("==> Setting up the environment...")
# Load the .env file
load_dotenv()
# Misc
date = datetime.datetime.today().strftime('%Y-%m-%d')
# SMTP Setup and Template
smtp_server = os.getenv('SMTP_SERVER')
smtp_port = os.getenv('SMTP_PORT')
sender = os.getenv('SENDER')
destination = os.getenv('DESTINATION')
smtp_username = os.getenv('SMTP_USERNAME')
smtp_password = os.getenv('SMTP_PASSWORD')
email_subtype = "plain"
email_subject = "yiffOS Vim update "
# Login to Github
g = Github(os.environ.get("GITHUB_TOKEN"))
# Import GPG key
gpg_key = GPG(gnupghome=os.path.expanduser("~/.gnupg"), verbose=False, use_agent=True)
gpg_import_result = gpg_key.import_keys(os.environ.get("GPG_KEY"))
assert gpg_import_result.fingerprints[0] == os.environ.get("GPG_FINGERPRINT"), "GPG key import failed, aborting."
gpg_key_id = os.environ.get("GPG_KEY_ID")
print("Cloning PKGSCRIPT repo...")
repo = Repo.init(os.path.join(os.getcwd(), "PKGSCRIPT"))
origin = repo.create_remote("origin", os.environ.get("REPO_URL"))
origin.fetch()
repo.create_head("main", origin.refs.main).set_tracking_branch(origin.refs.main).checkout()
# Set local git config with name and email
repo.config_writer().set_value("user", "name", os.environ.get("GIT_NAME")).release()
repo.config_writer().set_value("user", "email", os.environ.get("GIT_EMAIL")).release()
### Run Vim CI
print("==> Running Vim CI...")
print("Getting the latest tag...")
# Get the latest tag for vim
tag_version = g.get_repo("vim/vim").get_tags()[0].name
tag_tar = "https://github.com/vim/vim/archive/refs/tags/{}.tar.gz".format(tag_version)
# Download the tarball and calculate the sha512sum
print("Downloading vim tarball...")
urllib.request.urlretrieve(tag_tar, tag_version+ ".tar.gz")
print("Calculating sha512sum...")
sha512sum = os.popen('sha512sum ' + tag_version + '.tar.gz').read().split(' ')[0]
print("Modifying PKGSCRIPT...")
with open(os.path.join(os.getcwd(), "PKGSCRIPT/vim/PKGSCRIPT"), "r+") as f:
pkgscript = f.read()
pkgscript = re.sub("VERSION=\".+\"", "VERSION=\"" + tag_version[1:] + "\"", pkgscript)
pkgscript = re.sub("SUM=\(\".+\"\)", "SUM=(\"" + sha512sum + "\")", pkgscript)
f.seek(0)
f.write(pkgscript)
f.truncate()
print("Modifying PKGINFO...")
with open(os.path.join(os.getcwd(), "PKGSCRIPT/vim/PKGINFO"), "r+") as f:
pkginfo = f.read()
pkginfo = re.sub(" \"version\": \".+\",", " \"version\": \"" + tag_version[1:] + "\",", pkginfo)
f.seek(0)
f.write(pkginfo)
f.truncate()
## Create and push commit
print("==>Committing...")
print("Adding files...")
repo.index.add([os.path.join(os.getcwd(), "PKGSCRIPT/vim/PKGSCRIPT"), os.path.join(os.getcwd(), "PKGSCRIPT/vim/PKGINFO")])
repo.index.write()
print("Creating commit...")
repo.git.commit('-S', f'--gpg-sign={gpg_key_id}', '-m', f'Update Vim to {tag_version} (Automated)')
commit_id = repo.commit().hexsha
print("==> Pushing commit...")
origin.push().raise_if_error()
## Clean up
print("==> Cleaning up...")
os.remove(os.path.join(os.getcwd(), tag_version + ".tar.gz"))
shutil.rmtree(os.path.join(os.getcwd(), "PKGSCRIPT"))
###############################################################################
print("Sending email...")
email_content = """\
Vim has been updated to {} on {}!
Commit id: {}
GitLab link: {}
""".format(tag_version, date, commit_id, os.environ.get("COMMIT_WEB_URL") + commit_id)
try:
msg = message.EmailMessage()
msg.set_content(email_content)
msg["Subject"] = email_subject + tag_version + " (" + date + ")"
msg["From"] = sender
conn = smtplib.SMTP(smtp_server, smtp_port)
conn.set_debuglevel(False)
conn.ehlo()
conn.starttls()
conn.login(smtp_username, smtp_password)
try:
conn.sendmail(sender, destination, msg.as_string())
finally:
conn.quit()
except smtplib.SMTPException as e:
print("Error: unable to send email")
print(e)