interactive user/password query & some better error handling

This commit is contained in:
Lukas 2021-11-09 13:18:00 +01:00
parent 319c48b816
commit bb15567abb
3 changed files with 41 additions and 16 deletions

View File

@ -1,3 +1,3 @@
# Cloudy
easy tool to zip and upload directories to own/nextcloud
Easy tool to zip and upload a single directory to own/nextcloud.

View File

@ -1,7 +1,7 @@
__author__ = "Lukas Mahler"
__version__ = "0.0.1"
__date__ = "07.11.2021"
__email__ = "lm@ankerlab.de"
__version__ = "0.0.2"
__date__ = "09.11.2021"
__email__ = "m@hler.eu"
__status__ = "Development"
@ -11,6 +11,7 @@ try:
import sys
import stat
import shutil
import getpass
import requests
import tempfile
import datetime
@ -28,6 +29,11 @@ except ImportError as e:
def del_rw(action, name, exc):
"""
this custom shutil.rmtree on error function
makes read-only files writeable so we can delete them
"""
os.chmod(name, stat.S_IWRITE)
os.remove(name)
@ -37,16 +43,23 @@ def create_duplicate_dir(folder):
Creates a duplicate of the folder in the tempdir.
Throws errors when there is a Thumbs.db blocking...
"""
temp_dir = tempfile.gettempdir()
date = datetime.datetime.now()
ext = date.strftime("%Y-%m-%d")
src = folder
dst = os.path.join(temp_dir, ext + "_" + "Dump")
print("[*] Trying to create a Duplicate, please wait...")
if not os.path.exists(src):
raise FileNotFoundError(f"The given path [{src}] was not found.")
if not os.path.isdir(src):
raise ValueError(f"The given path [{src}] is not a directory.")
if os.path.exists(dst):
shutil.rmtree(dst, onerror=del_rw)
print("[*] Trying to create a Duplicate, please wait...")
shutil.copytree(src, dst)
print("[*] Successfully created Duplicate")
@ -56,7 +69,6 @@ def create_duplicate_dir(folder):
def zip_folder(folder):
"""
Zips the duplicated folder in the tempdir.
src: https://stackoverflow.com/questions/60087965/how-to-zip-a-folder-in-python-with-password
"""
print("[*] Trying to create corresponding zipfile")
@ -106,6 +118,16 @@ def upload_to_nextcloud(file):
pwd = config["Auth"]["password"]
pth = config["Other"]["savepath"]
if not usr:
usr = input(f"Please provide a username to [{url}]: ")
if not pwd:
pwd = getpass.getpass(f"Please provide the password for [{usr}]: ")
# If no protocol is added to the host url assume it's https://
if not any(x in ["http://", "https://"] for x in url):
url = "https://" + url
# Test Connection
r = requests.head(url)
httpc = str(r.status_code)[0]
@ -127,7 +149,11 @@ def upload_to_nextcloud(file):
print(f"[*] Finished uploading to {url}")
def clean_temp(files):
def cleanup(files):
"""
given a list of files/directories (full path) delete them
"""
for file in files:
if os.path.isfile(file) or os.path.islink(file):
os.remove(file)
@ -139,17 +165,15 @@ def clean_temp(files):
def main():
# This will be uploaded
folder = os.path.join(os.environ['USERPROFILE'], 'Desktop', "Dump")
# Load toml config
global config
config = util.getConf("prod.toml")
folder = config["Other"]["uploaddir"]
dupe = create_duplicate_dir(folder)
zipped = zip_folder(dupe)
upload_to_nextcloud(zipped)
clean_temp([dupe, zipped])
cleanup([dupe, zipped])
if __name__ == "__main__":

View File

@ -1,10 +1,11 @@
[Connection]
host = "myexamplecloud.com"
host = "https://myexamplecloud.com" # Hostname on which the own/nextloud instance resides
[Auth]
user = "exampleuser"
password = "examplepassword"
user = "exampleuser" # User whom has access to the own/nextcloud instance [Can be empty]
password = "examplepassword" # Password corresponding to the user [Can be empty]
[Other]
savepath = "Zips/"
zippw = ""
uploaddir = 'C:\Users\exampleuser\Pictures' # Path to the dir u want to upload
savepath = 'Zips/' # Path on own/nextcloud where u want to save the zip
zippw = "" # Password to the zipfile [Can be empty for no password]