2019-07-28 16:18:49 +00:00
|
|
|
import os
|
2020-12-09 01:43:20 +00:00
|
|
|
import shutil
|
2020-12-09 01:42:02 +00:00
|
|
|
import sys
|
2019-08-10 01:43:07 +00:00
|
|
|
import pathlib
|
|
|
|
import pickle
|
2020-12-09 02:47:09 +00:00
|
|
|
|
2019-08-10 01:43:07 +00:00
|
|
|
from hashlib import sha256
|
2019-07-28 16:18:49 +00:00
|
|
|
|
2020-12-09 02:47:09 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
|
2020-12-09 03:37:51 +00:00
|
|
|
from misc import alert
|
|
|
|
from misc import get_os
|
|
|
|
from misc import path_join
|
2019-08-10 01:43:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def __get_config_folder():
|
|
|
|
_os = get_os()
|
|
|
|
if _os == "linux":
|
2020-12-09 03:37:51 +00:00
|
|
|
folder = path_join(os.getenv("HOME"), ".munyal")
|
2019-08-10 01:43:07 +00:00
|
|
|
else:
|
2020-12-09 03:37:51 +00:00
|
|
|
folder = path_join(os.path.expandvars("%APPDATA%"), "Munyal")
|
2019-08-10 01:43:07 +00:00
|
|
|
return folder
|
|
|
|
|
2020-12-09 01:42:02 +00:00
|
|
|
def __get_files_folder():
|
2020-12-09 03:37:51 +00:00
|
|
|
folder = path_join(os.path.expanduser("~"), "Munyal")
|
2020-12-09 01:45:31 +00:00
|
|
|
return folder
|
2019-08-10 01:43:07 +00:00
|
|
|
|
2020-12-17 18:57:38 +00:00
|
|
|
def get_last_history():
|
|
|
|
last_history_path = path_join(__get_config_folder(), "history.pkl")
|
|
|
|
if not os.path.exists(last_history_path):
|
|
|
|
return None
|
|
|
|
with open(last_history_path, "rb") as history:
|
|
|
|
return pickle.load(history)
|
|
|
|
|
|
|
|
def set_last_history(last_history):
|
|
|
|
last_history_path = path_join(__get_config_folder(), "history.pkl")
|
|
|
|
with open(last_history_path, "wb") as history:
|
|
|
|
pickle.dump(last_history, history)
|
|
|
|
|
2019-08-10 01:43:07 +00:00
|
|
|
def get_config():
|
2020-12-09 01:45:31 +00:00
|
|
|
for _ in range(3):
|
2019-08-10 01:43:07 +00:00
|
|
|
config = __get_config()
|
|
|
|
if not config:
|
2020-12-17 18:57:38 +00:00
|
|
|
try:
|
|
|
|
gui_config()
|
|
|
|
except ModuleNotFoundError as e:
|
|
|
|
cli_config()
|
2019-08-10 01:43:07 +00:00
|
|
|
else:
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
def __get_config():
|
|
|
|
folder = __get_config_folder()
|
2020-12-09 03:37:51 +00:00
|
|
|
config_file = path_join(folder, "config")
|
2019-08-10 01:43:07 +00:00
|
|
|
if os.path.exists(config_file):
|
|
|
|
config_bytes = open(config_file, "rb")
|
|
|
|
config = pickle.load(config_bytes)
|
|
|
|
config_bytes.close()
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
def __validate_values(user, password):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-12-17 18:57:38 +00:00
|
|
|
def _gui_validate_config(root, user, password, folder):
|
2019-08-10 01:43:07 +00:00
|
|
|
passwd_hash = sha256(password.get().encode("utf-8")).hexdigest()
|
|
|
|
if not user:
|
|
|
|
alert(root, "Introduce tu usuario de la red Munyal")
|
|
|
|
elif not password:
|
|
|
|
alert(root, "Introduce tu contraseña")
|
|
|
|
elif not folder:
|
|
|
|
alert(root, "Elige la carpeta que quieres sincronizar")
|
|
|
|
elif not __validate_values(user, passwd_hash):
|
|
|
|
alert(
|
|
|
|
root,
|
|
|
|
"Tu usuario o contraseña son incorrectos, favor de reintentarlo")
|
|
|
|
else:
|
|
|
|
password.set("")
|
2020-12-17 18:57:38 +00:00
|
|
|
__set_config(user, passwd_hash, folder)
|
2019-08-10 01:43:07 +00:00
|
|
|
root.destroy()
|
|
|
|
|
|
|
|
|
2020-12-17 18:57:38 +00:00
|
|
|
def _cli_validate_config(user, password, folder):
|
|
|
|
if not user:
|
|
|
|
print("! Introduce tu usuario de la red munyal !")
|
|
|
|
elif not password:
|
|
|
|
print("! Introduce tu contraseña !")
|
|
|
|
elif not folder:
|
|
|
|
print("! Elige la carpeta que quieres sincronizar !")
|
|
|
|
elif not __validate_values(user, passwd_hash):
|
|
|
|
print("! Tu usuario o contraseña son incorrectos !")
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def __set_config(user, password, folder):
|
|
|
|
folder_config = __get_config_folder()
|
|
|
|
# Create config folder
|
|
|
|
path = pathlib.Path(folder_config)
|
|
|
|
path.mkdir(parents=True, exist_ok=True)
|
|
|
|
# Create Munyal folder
|
|
|
|
path = pathlib.Path(folder)
|
|
|
|
path.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
with open(path_join(folder_config, "config"), "wb") as config_file:
|
|
|
|
config_file.write(
|
|
|
|
pickle.dumps({
|
|
|
|
"login": {
|
|
|
|
"user": user,
|
|
|
|
"password": passwd_hash
|
|
|
|
},
|
|
|
|
"folder": folder,
|
|
|
|
"uuid": str(uuid4())
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
2019-08-10 01:43:07 +00:00
|
|
|
def gui_config():
|
2020-12-17 18:57:38 +00:00
|
|
|
from tkinter import Button, Entry, Label, StringVar, Tk, filedialog
|
|
|
|
from PIL import Image, ImageTk
|
|
|
|
|
2019-07-28 16:18:49 +00:00
|
|
|
root = Tk()
|
2019-08-10 01:43:07 +00:00
|
|
|
root.geometry("250x420")
|
2019-07-28 16:18:49 +00:00
|
|
|
|
2019-08-10 01:43:07 +00:00
|
|
|
img_logo = Image.open("img/logo.png")
|
|
|
|
img_tk = ImageTk.PhotoImage(img_logo.resize((200, 150), Image.ANTIALIAS))
|
2019-07-28 16:18:49 +00:00
|
|
|
|
|
|
|
host = StringVar()
|
2019-08-10 01:43:07 +00:00
|
|
|
host.set("localhost")
|
|
|
|
host_field = Entry(root, textvariable=host)
|
2019-07-28 16:18:49 +00:00
|
|
|
|
|
|
|
passwd = StringVar()
|
2019-08-10 01:43:07 +00:00
|
|
|
passwd_field = Entry(root, textvariable=passwd, show="*")
|
2019-07-28 16:18:49 +00:00
|
|
|
|
|
|
|
folder = StringVar()
|
2020-12-09 01:42:02 +00:00
|
|
|
folder.set(__get_files_folder())
|
2019-08-10 01:43:07 +00:00
|
|
|
folder_field = Entry(root, textvariable=folder)
|
|
|
|
btn_folder = Button(root,
|
|
|
|
text="Examinar",
|
|
|
|
command=lambda: search_folder(folder))
|
2019-07-28 16:18:49 +00:00
|
|
|
|
2019-08-10 01:43:07 +00:00
|
|
|
connect = Button(
|
|
|
|
root,
|
|
|
|
text="Conectar",
|
2020-12-17 18:57:38 +00:00
|
|
|
command=lambda: _gui_validate_config(root, host.get(), passwd, folder.get()))
|
2019-07-28 16:18:49 +00:00
|
|
|
|
2019-08-10 01:43:07 +00:00
|
|
|
Label(root, text="MUNYAL").pack()
|
|
|
|
Label(root, image=img_tk).pack()
|
2019-07-28 16:18:49 +00:00
|
|
|
Label(root, text="").pack()
|
2019-08-10 01:43:07 +00:00
|
|
|
Label(root, text="Ruta del servidor").pack()
|
2019-07-28 16:18:49 +00:00
|
|
|
host_field.pack()
|
|
|
|
Label(root, text="").pack()
|
|
|
|
Label(root, text="Contraseña").pack()
|
|
|
|
passwd_field.pack()
|
|
|
|
Label(root, text="").pack()
|
|
|
|
Label(root, text="Carpeta a sincronizar").pack()
|
|
|
|
folder_field.pack()
|
2019-08-10 01:43:07 +00:00
|
|
|
btn_folder.pack()
|
2019-07-28 16:18:49 +00:00
|
|
|
Label(root, text="").pack()
|
|
|
|
connect.pack()
|
|
|
|
|
|
|
|
root.mainloop()
|
|
|
|
|
|
|
|
|
2020-12-17 18:57:38 +00:00
|
|
|
def cli_config():
|
|
|
|
while True:
|
|
|
|
host = input("Introduce tu usuario de Munyal\n>>> ")
|
|
|
|
password = input("Introduce tu contraseña\n>>> ")
|
|
|
|
folder = input("Introduce el directorio que quieres sincronizar\n>> ")
|
|
|
|
if _cli_validate_config(host, password, folder):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
print("! Los datos introducidos no son correctos, intentalo de nuevo !")
|
|
|
|
passwd_hash = sha256(password.encode("utf-8")).hexdigest()
|
|
|
|
__set_config(user, password, folder)
|
|
|
|
|
|
|
|
|
2019-08-10 01:43:07 +00:00
|
|
|
def search_folder(field):
|
|
|
|
path = filedialog.askdirectory()
|
|
|
|
field.set(path)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-12-09 01:42:02 +00:00
|
|
|
if "--restart" in sys.argv:
|
|
|
|
if __get_config():
|
2020-12-09 01:45:31 +00:00
|
|
|
shutil.rmtree(__get_config_folder())
|
2019-08-10 01:43:07 +00:00
|
|
|
print(get_config())
|