def process(folder): for root, directories, filenames in os.walk(folder): if "Images " in root: for item in filenames: if not item.startswith("."): if any(ext in item for ext in imageTypes): print("THIS IS AN IMAGE, DUDE") # THIS STEP REPLACES ALL BAD CHARACTERS WITH AN ASTERISK. for char in badCharacterList: if char in item: item = item.replace(char, "*") base = item print(base) # THE NEXT FEW LINES CUT OUT ANYTHING BETWEEN THE FIRST ASTERISK AND THE # CHARACTER AFTER THE LAST ASTERISK. THAT WAY, rejectFile() CAN USE # GLOB TO FIND THE FILEPATH OF THE EXISTING FILE (WITH BAD CHARACTERS # IN THE FILENAME), E.G. thisIsAFileThatHad*BadCharacters.ext charIndex = 0 charIndexList = [] while charIndex < len(base): charIndex = base.find('*', charIndex) if charIndex == -1: break charIndexList.append(charIndex) charIndex += 1 if not charIndexList == []: base = base[:charIndexList[0]] + base[ (charIndexList[-1]):] # AND IF THERE WERE NO ASTERISKS, LEAVE IT ALONE. else: base = base filePath = root + "/" + base checkFilenameFormat(base, filePath) else: # REJECT NON-IMAGE FILES OR IMAGES WITHOUT CORRECT FILETYPE # base = item filePath = root + "/" + base currentAction = "Bad filetype" statusLog(currentAction, filePath, base)
def acceptFile(base, filePath): currentAction = "accepting a file" print(currentAction) statusLog(currentAction, filePath, base) if not re.search(base, allLogged): currentAction = "sending to Piction" print(base + " not in list") statusLog(currentAction, filePath, base) sortSend(base, filePath) else: currentAction = "already in Piction" print(base + " found in list, skipping") statusLog(currentAction, filePath, base)
def checkDate(base, filePath): try: with open(filePath) as thingie: if re.match(combinedDateFormats, base): currentAction = "accepting date" statusLog(currentAction, filePath, base) acceptFile(base, filePath) else: currentAction = "bad date" statusLog(currentAction, filePath, base) rejectFile(base, filePath) except FileNotFoundError as ex: currentAction = "event image already rejected" statusLog(currentAction, filePath, base)
def checkFilenameFormat(base, filePath): currentAction = "Checking the Filename Format on " print("STARTING THE FILECHECK PROCESS on " + base) statusLog(currentAction, filePath, base) # CHECK FOR BAD CHARACTERS. THIS IS SORT OF REDUNDANT # GIVEN THE CHECK IN process(), BUT IT WILL CATCH ANYTHING THAT SLIPS THROUGH if re.match(badCharacterRegex, base): currentAction = "Bad characters found" statusLog(currentAction, filePath, base) print(currentAction + " in " + base) rejectFile(base, filePath) else: # CHECKING FILM STILLS # if "Film " in filePath: if re.match(filmRegex, base): print("THE FILM STILL IS ACCEPTED " + base + "\r") acceptFile(base, filePath) else: currentAction = "rejecting a film still" statusLog(currentAction, filePath, base) rejectFile(base, filePath) # CHECKING EVENT IMAGES # elif "Events " in filePath: try: with open(filePath) as eventFile: if re.match(eventRegex, base): currentAction = "event image name format is ok" statusLog(currentAction, filePath, base) checkDate(base, filePath) else: currentAction = "rejecting an event image" statusLog(currentAction, filePath, base) rejectFile(base, filePath) except FileNotFoundError as ex: currentAction = "event image already rejected" statusLog(currentAction, filePath, base) # CHECKING GALLERY EXHIBITION PHOTOS # else: if "Exhibitions " in filePath: try: with open(filePath) as gallImage: if re.match(exhibitionRegex, base): acceptFile( base, filePath ) # ORIGINALLY THIS SENT EXH IMAGES TO checkDate() else: currentAction = "rejecting an exhibition image" statusLog(currentAction, filePath, base) rejectFile(base, filePath) except FileNotFoundError as ex: currentAction = "exhibition image already rejected" statusLog(currentAction, filePath, base)
def put(folder): for source in glob(folder): try: if not source.startswith( "_" ): # ONLY THE RESEARCH_HUB_COLLECTIONS/ FOLDER STARTS WITH SOMETHING OTHER THAN '_' os.chdir(source) pwd = os.getcwd() currentAction = "trying to ftp" statusLog(currentAction, pwd, source) if ''.join(os.listdir('.')) == '.DS_Store': currentAction = "nothing to ftp" statusLog(currentAction, pwd, source) print("SKIPPING " + source) else: child = pexpect.spawnu( 'ftp ucb1.piction.com', timeout=1000 ) # LOG INTO THE PICTION SERVER, TIMEOUT SET TO 1K SECONDS. child.expect('.*ame.*:') child.sendline('bampfa') child.expect('.*assword.*') child.sendline(answer) child.expect('ftp>') print("CONNECTED") child.sendline( 'cd ' + source) # NAVIGATE TO THE CORRESPONDING PICTION FOLDER child.expect('ftp>') child.sendline('prompt off') child.expect('ftp>') child.sendline('binary') for file in os.listdir('.'): if not file.startswith("."): if not re.search(file, allText): print(file + " ready to ftp") currentAction = "now ftp per file" statusLog(currentAction, pwd, file) try: child.expect('ftp>') child.sendline('mput ' + file) currentAction = "ftp file success" statusLog(currentAction, pwd, file) listLog(file) except Error as error: currentAction = "ftp file failure" statusLog(currentAction, pwd, file) try: child.expect('ftp>') currentAction = "ftp folder success" print("SUCCESS WE FTPed " + source) statusLog(currentAction, pwd, source) fileList = os.listdir('.') for item in fileList: if not item == '.DS_Store': os.remove(item) except Error as error: currentAction = "failed to FTP" print("FAILED TO ftp " + source) pass currentAction = "cleanup" statusLog(currentAction, pwd, source) os.chdir(root) else: pass except: pwd = os.getcwd() currentAction = "failed before FTP" statusLog(currentAction, pwd, source)
from ftpLogger import statusLog from datetime import date today = str(date.today()) root = "/Users/michael/Google Drive" ftpRoot = "/Users/michael/Desktop/drive2Piction/FTPs/Research_Hub_Collections" rejectPath = "/Users/michael/Desktop/drive2Piction/FTPs/_Rejects" driveSourceFolder = "/**/Images */**" globPattern = "**" try: currentAction = "initial" os.chdir(root) File = "Start" statusLog(currentAction, root, File) process(root) currentAction = "time to ftp" File = "Ftp" statusLog(currentAction, root, File) os.chdir(ftpRoot) put(globPattern) currentAction = "done" statusLog(currentAction, driveSourceFolder, File) print((("*") * 100) + "\nDONE\n" + (("*") * 100)) except: pass