Skip to content

Commit

Permalink
1.1.5
Browse files Browse the repository at this point in the history
updater
  • Loading branch information
DoctorFuchs committed Jun 5, 2021
1 parent 5103113 commit c21de79
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 18 deletions.
2 changes: 0 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
# Auto detect text files and perform LF normalization
* text=auto
33 changes: 27 additions & 6 deletions core/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,37 @@
from core.new import commands, debug, Listener, auth
from core.new.Interpret import interpreter
from core.functions.system.Sections import *
from core.functions.system import updater


class doc:
def __init__(self, username="USER", live_debug=False, guest=False, dev=False):
start = time.time()

self.debug = debug.debug(instance=self, live_debug=live_debug)
self.debug.start = True

self.debug.addEvent("Starting DOC...", SYSTEM)
self.debug.addEvent("Checking for updates...", SYSTEM)

new_version = updater.getGithubVersion()

if new_version == version.version:
self.debug.addEvent("No updates found", SYSTEM)

else:
while True:
want_update = self.docinput("Do you want to update to version "+new_version+"? [Y/n]", debug_it=False)
if want_update == "Y":
self.debug.addEvent("updating to "+new_version, SYSTEM)

elif want_update == "n":
self.debug.addEvent("user don't want to upgrade to version: "+new_version, SYSTEM)
break

else:
self.debug.addEvent("Try again!", SYSTEM)
pass

self.debug.addEvent("Listener Build...", SYSTEM)

Expand All @@ -23,9 +47,6 @@ def __init__(self, username="USER", live_debug=False, guest=False, dev=False):
self.Listener.ConsoleStart()

self.debug.addEvent("Listener Build... Finished", SYSTEM)
self.debug.start = True

self.debug.addEvent("Starting DOC...", SYSTEM)
self.debug.addEvent("Clear Commands...", SYSTEM)

commands.clear()
Expand Down Expand Up @@ -101,16 +122,16 @@ def docprint(self, text, end="\n", display_section=OUTPUT, debug_it=True):
pass

else:
sys.stdout.write(text+end)
sys.stdout.write(text + end)
sys.stdout.flush()

def docinput(self, placeholder="", debug_it=True) -> str:
# input
self.docprint(placeholder, end="")
self.docprint(placeholder, end="", debug_it=False)
userinput = sys.stdin.readline().replace("\n", "")

if debug_it:
self.debug.addEvent("Docinput: "+userinput, source=INPUT)
self.debug.addEvent("Docinput: " + userinput, source=INPUT)
self.Listener.UserInput(self.username, userinput)

return userinput
Expand Down
174 changes: 167 additions & 7 deletions core/functions/system/updater.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,171 @@
import requests as req
import os
import shutil

import version
from core.functions.system.getMainPath import getMainPath

try:
from version import *

def getGithubVersion() -> str:
raw = req.get("https://raw.githubusercontent.com/DoctorFuchs/Doc/main/version.py") # request raw document
if raw.status_code != 200: # if user is not online
return version.version
except:
version = "1.1.4"

return raw.text.split("version = ")[1].split("<")[0].replace("\"", "")
try:
import requests as req
import torpy as tor
from torpy.http import requests
from torpy.http.adapter import TorHttpAdapter

except:
print("request or torpy is not installed. (install both with pip3) Please update manually")

blocked = ["core/new/auths.py", "core/plugins", "core/startup.txt"] # files, that don't get update


def getPath():
path = str(__file__).split("core/functions/system/updater.py")
del path[len(path) - 1]

path = "/".join(path).split("/")
path.append("Doc-" + getGithubVersion())

del path[len(path) - 3]
if not os.path.isdir("/".join(path)):
os.mkdir("/".join(path))

return ("/".join(path) + "/").replace("//", "/")


def getGithubVersion(session=None) -> str:
if session is None:
raw = req.get("https://raw.githubusercontent.com/DoctorFuchs/Doc/main/version.py") # request raw document

else:
raw = session.get("https://raw.githubusercontent.com/DoctorFuchs/Doc/main/version.py")

if raw.status_code != 200: # if user is not online
return version

return raw.text.split("version = ")[1].split("<")[0].replace("\"", "").replace("\n", "")


def getContent(path: str, session=None) -> str:
if session is None:
raw = req.get("https://raw.githubusercontent.com/DoctorFuchs/Doc/main/" + path) # request raw document

else:
raw = session.get("https://raw.githubusercontent.com/DoctorFuchs/Doc/main/" + path)

if raw.status_code != 200: # if user is not online
return ""

return raw.text


def getAllFilesInDictionary(path: str, session=None):
if session is None:
info = req.get("https://api.github.com/repos/doctorfuchs/doc/contents/" + path)

else:
info = session.get("https://api.github.com/repos/doctorfuchs/doc/contents/" + path)

if info.status_code != 200:
return []

file = {}
returner = []
final = info.text.split(">")[0].split("</pre>")[0].replace("{", ",").replace("}", ",").replace("\"", "").split(",")

# convert to dict
for i in range(len(final)):
if final[i] == "":
pass

if final[i].split(":", 1)[0] == "name" and file != {}:
file[final[i].split(":", 1)[0]] = final[i].split(":", 1)[1]
returner.append(file)
file = {}

else:
try:
file[final[i].split(":", 1)[0]] = final[i].split(":", 1)[1]

except IndexError:
pass

returner.append(file)

return returner


def update(sess=None):
file_paths = []
dicts = ["", ""]

while True:
try:
act = getAllFilesInDictionary(dicts[0], session=sess)
for i in range(len(act)):
if act[i]["path"] in blocked:
continue

if act[i]["type"] == "file":
file_paths.append(act[i]["path"])

elif act[i]["type"] == "dir":

try:
os.mkdir(getPath() + act[i]["path"])

except FileExistsError:
pass

dicts.append(act[i]["path"])

del dicts[0]

except:
break

for element in range(len(file_paths)):
content = getContent(file_paths[element], session=sess)

if file_paths[element] in blocked:
continue

elif os.path.isfile(getPath() + file_paths[element]):
mode = "r+t"

else:
mode = "w+t"

file = open(getPath() + file_paths[element], mode)
if "\n".join(file.readlines()) == content:
pass

else:
file.writelines(content.split("\n"))

file.close()

for i in range(len(blocked)):
try:
shutil.copy(getMainPath() + blocked[i], getPath() + blocked[i])

except IsADirectoryError:
try:
shutil.copytree(getMainPath() + blocked[i], getPath() + blocked[i])

except:
pass

return "updated"


def tor_update():
with tor.TorClient() as torc:
with torc.get_guard() as guard:
adapter = TorHttpAdapter(guard, 2)
with requests.Session() as sess:
sess.headers.update({"User-Agent": "Mozilla/5.0"})
sess.mount("https://", adapter)
update(sess)
1 change: 1 addition & 0 deletions core/plugins/PluginCommands.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
clock c:plugins.plugincodes.clock(instance);
2 changes: 1 addition & 1 deletion core/plugins/clock/DOC.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def clock(instance):
import time, datetime
try:
while True:
instance.docprint("\r" + datetime.datetime.now().strftime("%H:%M:%S"), end="")
instance.docprint("\r" + str(datetime.datetime.now().strftime("%H:%M:%S")), end="")
time.sleep(1)

except:
Expand Down
10 changes: 10 additions & 0 deletions core/plugins/plugincodes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def clock(instance):
"""live time clock"""
import time, datetime
try:
while True:
instance.docprint("\r" + str(datetime.datetime.now().strftime("%H:%M:%S")), end="")
time.sleep(1)

except:
instance.docprint("", end="\n")
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests
requests
torpy
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "1.1.4"
version = "1.1.5"

0 comments on commit c21de79

Please sign in to comment.