-
Notifications
You must be signed in to change notification settings - Fork 3
/
lookup-server.py
123 lines (106 loc) · 3.95 KB
/
lookup-server.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
# discord-ppa
# Copyright (C) 2020 - Javinator9889
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import os
import sys
from logging.handlers import RotatingFileHandler
from pathlib import Path
from sched import scheduler
from subprocess import PIPE, Popen
from tempfile import NamedTemporaryFile
from time import sleep, time
import urllib3
from daemonize import Daemonize
delay_secs = 900
discord_url = r"https://discordapp.com/api/download?platform=linux&format=deb"
discord_pbeta_url = r"https://discordapp.com/api/download/ptb?platform=linux&format=deb"
discord_canary_url = (
r"https://discordapp.com/api/download/canary?platform=linux&format=deb"
)
try:
ppa_path = sys.argv[1]
except IndexError:
print("You must provide the PPA directory")
exit(1)
reprepro_cmd = "reprepro -b {0} includedeb %dist% %file%".format(ppa_path)
http = urllib3.PoolManager()
home = str(Path.home())
pid = "{0}/discord-ppa/discord-ppa.pid".format(home)
try:
os.mkdir("{0}/discord-ppa".format(home))
except FileExistsError:
pass
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
fmt = logging.Formatter("%(process)d - %(asctime)s | [%(levelname)s]: %(message)s")
file_handler = RotatingFileHandler(
"{0}/discord-ppa/discord-ppa.log".format(home), "w", maxBytes=2 << 20, backupCount=2
)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(fmt)
logger.addHandler(file_handler)
keep_fds = [file_handler.stream.fileno()]
def main():
sched = scheduler(time, sleep)
run_update_process()
try:
while True:
sched.enter(delay_secs, 0, run_update_process)
sched.run()
except InterruptedError:
exit(0)
def download_latest_deb(fp: NamedTemporaryFile, url: str):
result = http.request("GET", url, redirect=True)
if result.status == 200:
logger.info("Downloaded correctly Discord .deb file")
fp.write(result.data)
else:
logger.error(
"Discord .deb file could not be downloaded - status "
"code: {0}".format(result.status)
)
def update_reprepro(fp: NamedTemporaryFile, dist: str):
cmd = reprepro_cmd.replace("%dist%", dist).replace("%file%", fp.name).split()
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
if proc.returncode != 0:
error = err.decode("utf-8")
logger.error(
"reprepro ended with an error - ret. code: "
"{0} | output: \n{1}".format(proc.returncode, error)
)
else:
output = out.decode("utf-8") + "\n" + err.decode("utf-8")
logger.info("reprepro finished OK | output:\n {0}".format(output))
def run_update_process():
stable = NamedTemporaryFile(suffix=".deb")
beta = NamedTemporaryFile(suffix=".deb")
canary = NamedTemporaryFile(suffix=".deb")
try:
download_latest_deb(stable, discord_url)
update_reprepro(stable, "all")
download_latest_deb(beta, discord_pbeta_url)
update_reprepro(beta, "public-beta")
download_latest_deb(canary, discord_canary_url)
update_reprepro(canary, "canary")
finally:
stable.close()
beta.close()
canary.close()
daemon = Daemonize(
app="discord-ppa", pid=pid, action=main, keep_fds=keep_fds, logger=logger
)
daemon.start()