def recursive_checking_files(current_way, original_folder, local_syncing_folder): files = os.listdir(current_way) for i in files: if os.path.isdir(current_way + '/' + i): recursive_checking_files(current_way + '/' + i, original_folder, local_syncing_folder) elif os.path.isfile(current_way + '/' + i): folder_from_syncing = search_in_syncing(local_syncing_folder, current_way + '/' + i) if folder_from_syncing: if os.getmtime(folder_from_syncing + i) < os.getmtime(current_way + '/' + i): shutil.copy2(current_way + '/' + i, folder_from_syncing + i) are_there_copied_files = True else: folders = current_way.split('/') new_path = local_syncing_folder for folder in folders: if folder not in original_folder: new_path += '/' new_path += folder if not os.path.exists(new_path): os.makedirs(new_path) shutil.copy2(current_way + '/' + i, new_path) are_there_copied_files = True
def handlereq(req, root): global mimetype code = req.split('\n')[0] print("code start") print(code) print("code end") headers = parseheaders(req) for h, v in headers.items(): print("{} => {}".format(h, v)) if code != "GET": response = "HTTP/1.1 405 Method Not Allowed\n" + formatdate() try: for file in code.split(): if (file.startswith("/")): reqfile = root + file break print("reqfile is") print(reqfile) print("reqfile end") if (reqfile.endswith("html")): mimetype = content["html"] elif (reqfile.endswith("css")): mimetype = content["css"] elif (reqfile.endswith("gif")): mimetype = content["gif"] elif (reqfile.endswith("png")): mimetype = content["png"] elif (reqfile.endswith("jpg") or reqfile.endswith("jpeg")): mimetype = content["jpg"] elif (reqfile.endswith("js")): mimetype = content["js"] ctype = "Content-Type: " + mimetype + "\n" contentlength = "Content-Length: " + str( os.path.getsize(reqfile)) + "\n" if "If-Modified-Since" in headers: headertime = datetime.datetime.strptime( headers["if-modified-since"]) filetime = datetime.datetime(os.getmtime(reqfile)) if headertime > filetime: response = "HTTP/1.1 304 Not Modified\n" + formatdate() with open(reqfile, 'rb') as f: respheader = "HTTP/1.1 200 OK\n" + formatdate( ) + ctype + contentlength + "\n\n" return bytes(respheader, 'UTF-8') + f.read() except IOError: respheader = "HTTP/1.1 404 File Not Found\n" + "Date: " + date.strftime( datetime.datetime.now(), timeformat) + "\n\n" return bytes(respheader, 'UTF-8')
def reload_files(self): """ Lit le dossier et recharge les images qui ont changé """ import os import time from glob import glob # On importe la fonction 'glob' depuis le module 'glob' self.images = [] # On vide les images for filename in glob("{0}/*.[pP][nN][gG]".format(self.images_path)): # Pour chaque image trouvée dans le dossier # On relit l'image si elle a été modifiée dans la seconde if not filename in self.images or time.time() - os.getmtime(filename): try: # Utilisé pour gérer les erreurs (appellées "exceptions") image = pygame.image.load(filename).convert() # On la charge depuis le disque if self.scale > 1: # On redimensionne l'image # On redimensionne en gardant les proportions de l'image width = image.get_width() * self.scale height = image.get_height() * self.scale image = pygame.transform.scale(image, (width, height)) self.images.append(image) # On l'ajoute à la liste des images except: # Au cas ou une erreur à eu lieue depuis le try, on se retrouve la pass # On ignore l'erreur (par exemple: image illisible ...)
def restore_from_directories(self): """ as our heartbeat server has been down, there's no accurate way to set the heartbeat times, so we just use the current time, and hope for an update from the process before the heartbeat timeout """ if os.path.isdir(self.server_pid_dir): for file in os.listdir(self.server_pid_dir): with open(os.path.join(self.server_pid_dir, file)) as f: (pid, pgid) = f.readline().strip().split() if pid and pgid: self.server_job_list[file] = [time.time(), pid, pgid, None] # for finished proceses, though, we use the exit file time if os.path.isdir(self.server_exitrc_dir): for file in os.listdir(self.server_exitrc_dir): fn = os.path.join(self.server_exitrc_dir, file) with open(fn) as f: rc = f.readline().strip() if rc: if file in self.server_job_list: self.server_job_list[file][0] = os.path.getmtime(fn) self.server_job_list[file][3] = rc else: self.server_job_list[file] = [os.getmtime(fn), None, None, rc]
#!/bin/python #Author: Nick Blair #Checker: #Purpose: Provide logging to the database when user has logged in and when they chagned script files #This will log the entries when the admin has signed on and when he has modified files. This will be logged to a MYSQL database import datetime,os as p,MySQLdb as sql,sys,platform,time #This is importing the needed functions for python to execute and log to database conn = None #Will set the connection value default to none username = p.getlogin()#grabs who the user is currently logged in on the terminal userlog = get_nix_login_time()#grabs current login time#This doesn't work need work around filearray = [('/home/user/scripts/lanfilecheck.py'),('/home/user/scripts/wanfilecheck.py'),('/home/user/scripts/pymountmv.py')]#aray of files that will be checked filetime = p.getmtime(filearray)#checks the files in the array for the time of each curtime =time.time()#This reads out all the times timelasped = filetime - curtime# checks to see if time elapsed or not for file change isttimelaspe = datetime.datetime.fromtimestamp(timelasped.strftime('%Y-%m-%d %H:%M:%S'))#This will convert the unix epoch(seconds) time to readable time for x in filearray: if x == timelasped: x = filename try: conn = sql.connect("cloud","root","root","logdb")#will connect to the MySQL database cur = db.cursor()# #logger is a insert variable that will insert large values extracted from python into the mysql table # cur.execute("DROP TABLE IF EXISTS logs") if timelasped > 0: cur.execute( 'INSERT INTO logs VALUES(%s,%s,%s,%s)',(username,filename,userlog,isttimelaspe)) cursor.execute('SELECT * FROM logs') res = cur.fetchall() with open ('dblog.txt','w') as l: