Code style and module guard

This commit is contained in:
Frederik Möllers
2022-01-28 20:06:07 +01:00
parent 92e796c159
commit 912d3af794
+121 -116
View File
@@ -1,116 +1,121 @@
import requests import datetime
import xml.etree.ElementTree as ET import sys
import sys import time
import time import xml.etree.ElementTree as ET
import datetime
import requests
# Prepare and send the propfind request return xml string
def propfind(path,auth):
headers = {"Depth":"1"} # Prepare and send the propfind request return xml string
# This body returns only the timelastmodified and the fileid variable def propfind(path, auth):
data = "<d:propfind xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"+"<d:prop>"+"<d:getlastmodified />"+ "<oc:fileid />"+"</d:prop>"+"</d:propfind>" headers = {"Depth": "1"}
req = r = requests.Request("PROPFIND", path, headers=headers, auth=auth, data=data) # This body returns only the timelastmodified and the fileid variable
prepped = req.prepare() data = "<d:propfind xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">" + "<d:prop>" + "<d:getlastmodified />" + "<oc:fileid />" + "</d:prop>" + "</d:propfind>"
req = r = requests.Request("PROPFIND", path, headers=headers, auth=auth, data=data)
s = requests.Session() prepped = req.prepare()
resp = s.send(prepped)
return resp.text s = requests.Session()
resp = s.send(prepped)
# Function iterates through the given xml which contains all files and folders in the provided path return resp.text
def search_folder(requestreturn):
# List to collect path of folders stored in path
innerfolders = [] # Function iterates through the given xml which contains all files and folders in the provided path
# List to collect path of files with wrong timestamp def search_folder(requestreturn):
linkswrongtime = [] # List to collect path of folders stored in path
# First folder provided in the xml file is always the folder we are currently in. innerfolders = []
# To prevent searching this folder twice, or ending in a loop, we can not store this folder in the folderlist again. # List to collect path of files with wrong timestamp
firstfolder = True linkswrongtime = []
# Get a xml tree # First folder provided in the xml file is always the folder we are currently in.
tree = ET.ElementTree(ET.fromstring(requestreturn)) # To prevent searching this folder twice, or ending in a loop, we can not store this folder in the folderlist again.
# Find all responses in the tree, those contain the fielpath, lasttimemodified, typeoffile etc. firstfolder = True
for resp in tree.findall('.//{DAV:}response'): # Get a xml tree
# Here we can get the filpath out of href and get further information in prop (lastimemodified, typeoffile, etc.) tree = ET.ElementTree(ET.fromstring(requestreturn))
for p in resp: # Find all responses in the tree, those contain the fielpath, lasttimemodified, typeoffile etc.
# In case p.text is not none, it contains the filepath for resp in tree.findall('.//{DAV:}response'):
if not(p.text is None): # Here we can get the filpath out of href and get further information in prop (lastimemodified, typeoffile, etc.)
if (p.text[-1] == '/'): for p in resp:
# If the current object is a folder, check it its not first folder # In case p.text is not none, it contains the filepath
if not(firstfolder): if not (p.text is None):
innerfolders.append(p.text) if (p.text[-1] == '/'):
else: # If the current object is a folder, check it its not first folder
firstfolder = False if not (firstfolder):
break innerfolders.append(p.text)
# In case p.text is none, it contains the further information else:
else: firstfolder = False
for t in p.findall('.//{DAV:}getlastmodified'): break
#this function converts the given date to unix timestamp # In case p.text is none, it contains the further information
lastmodified = time.mktime(datetime.datetime.strptime(t.text, "%a, %d %b %Y %H:%M:%S GMT").timetuple()) else:
#631148400 is the unix timestamp of 01.01.1990 00:00:00, because we know there is no file older than this in our nextcloud system for t in p.findall('.//{DAV:}getlastmodified'):
if lastmodified < 631148400: # this function converts the given date to unix timestamp
for fileid in p.findall('.//{http://owncloud.org/ns}fileid'): lastmodified = time.mktime(
linkswrongtime.append(fileid.text) datetime.datetime.strptime(t.text, "%a, %d %b %Y %H:%M:%S GMT").timetuple())
return [innerfolders, linkswrongtime] # 631148400 is the unix timestamp of 01.01.1990 00:00:00, because we know there is no file older than this in our nextcloud system
if lastmodified < 631148400:
# This function returns the fileid of the version of a given fileid with the most current timestamp or None if there are no versions with a timestamp younger than 01.01.1990 for fileid in p.findall('.//{http://owncloud.org/ns}fileid'):
def version_check(xmlfile): linkswrongtime.append(fileid.text)
tree = ET.ElementTree(ET.fromstring(xmlfile)) return [innerfolders, linkswrongtime]
# Name of files are stored as a string. But we can check the timestamp of the file only after we can check the name,
# so it needs to be stored temporary in case the timestamp is the most current
temp = "this is a temporary string" # This function returns the fileid of the version of a given fileid with the most current timestamp or None if there are no versions with a timestamp younger than 01.01.1990
# These two variables are used to store the highest/most current timestamp and the associated fileid def version_check(xmlfile):
most_current_timestamp = 631148400 tree = ET.ElementTree(ET.fromstring(xmlfile))
most_current_timestamp_fileid = 0 # Name of files are stored as a string. But we can check the timestamp of the file only after we can check the name,
for resp in tree.findall('.//{DAV:}response'): # so it needs to be stored temporary in case the timestamp is the most current
for p in resp: temp = "this is a temporary string"
# In case p.text is not none, it contains the filepath # These two variables are used to store the highest/most current timestamp and the associated fileid
if not(p.text is None): most_current_timestamp = 631148400
temp = p.text most_current_timestamp_fileid = 0
else: for resp in tree.findall('.//{DAV:}response'):
for t in p.findall('.//{DAV:}getlastmodified'): for p in resp:
if not(t.text is None): # In case p.text is not none, it contains the filepath
#this function converts the given date to unix timestamp if not (p.text is None):
lastmodified = time.mktime(datetime.datetime.strptime(t.text, "%a, %d %b %Y %H:%M:%S GMT").timetuple()) temp = p.text
# else:
if lastmodified > most_current_timestamp: for t in p.findall('.//{DAV:}getlastmodified'):
most_current_timestamp = lastmodified if not (t.text is None):
fileid_old_version = temp.split('/') # this function converts the given date to unix timestamp
most_current_timestamp_fileid = fileid_old_version[-1] lastmodified = time.mktime(
else: datetime.datetime.strptime(t.text, "%a, %d %b %Y %H:%M:%S GMT").timetuple())
break #
# Check if there is another version and a file with a current timestamp if lastmodified > most_current_timestamp:
if most_current_timestamp_fileid != 0: most_current_timestamp = lastmodified
return most_current_timestamp_fileid fileid_old_version = temp.split('/')
else: most_current_timestamp_fileid = fileid_old_version[-1]
return None else:
break
def main(): # Check if there is another version and a file with a current timestamp
# Enter username and password to enter nextcloud via webdav if most_current_timestamp_fileid != 0:
user = sys.argv[1] return most_current_timestamp_fileid
passw = sys.argv[2] else:
auth = requests.auth.HTTPBasicAuth(user,passw) return None
# Prepare the path we want to use
prefix_path = "https://kingsx.cs.uni-saarland.de"
mainpath = "/remote.php/dav/files/"+ user + "/Testrequests/" if __name__ == "__main__":
# List of all folderpaths we need to enter # Enter username and password to enter nextcloud via webdav
folders = [mainpath] user = sys.argv[1]
# List of all fileids with wrong time passw = sys.argv[2]
wrongtime = [] auth = requests.auth.HTTPBasicAuth(user, passw)
# Prepare the path we want to use
# Iterate through all folders and check for wrong timestamps prefix_path = "https://kingsx.cs.uni-saarland.de"
while folders: mainpath = "/remote.php/dav/files/" + user + "/Testrequests/"
path_suffix = folders.pop(0) # List of all folderpaths we need to enter
path = prefix_path + str(path_suffix) folders = [mainpath]
r = propfind(path,auth) # List of all fileids with wrong time
res = search_folder(r) wrongtime = []
# Append all found folders and files with wrong timestamps to global list
folders = folders + res[0] # Iterate through all folders and check for wrong timestamps
wrongtime = wrongtime + res[1] while folders:
# Iterate through all fileids with wrong timestamps and check for versions with intact timestamp path_suffix = folders.pop(0)
while wrongtime: path = prefix_path + str(path_suffix)
fileid = wrongtime.pop(0) r = propfind(path, auth)
version_suffix = "/remote.php/dav/versions/" + user + "/versions/" + fileid res = search_folder(r)
version_path = prefix_path + version_suffix # Append all found folders and files with wrong timestamps to global list
versions = propfind(version_path,auth) folders = folders + res[0]
print(version_check(versions)) wrongtime = wrongtime + res[1]
# Iterate through all fileids with wrong timestamps and check for versions with intact timestamp
main() while wrongtime:
fileid = wrongtime.pop(0)
version_suffix = "/remote.php/dav/versions/" + user + "/versions/" + fileid
version_path = prefix_path + version_suffix
versions = propfind(version_path, auth)
print(version_check(versions))