def deleteValue(self, name): ''' Delete a value with name name. ''' # clean up names to avoid stupid debug.add('deleting value ', name) # figure out the path to the named value file if name in self.names: filePath = self.namePaths[name] # remove the metadata entry del self.namePaths[name] # write changes to database metadata file writeFile(pathJoin(self.path, 'names.table'), pickle(self.namePaths)) # update the length and names attributes self.names = self.namePaths.keys() self.length = len(self.names) else: return False if pathExists(filePath): # remove the file accocated with the value removeFile(filePath) return True else: return False
def zip_dir(zip_name, directory, destructive=False): with ZipFile(zip_name, mode="w", compression=ZIP_DEFLATED) as zipfile: for file_path in file_walk(directory): relpath = path.relpath(file_path, directory) zipfile.write(file_path, relpath) if destructive: removeFile(file_path)
def createSvgFile(inputDirName, outputDirName, prefix, npage): ipage = npage - 1 infpath = pathjoin(inputDirName, '{}.pdf'.format(prefix)) outfpath = pathjoin(outputDirName, '{}-p{}-fixedDims.svg'.format(prefix, ipage)) outfpathFinal = pathjoin(outputDirName, '{}-p{}.svg'.format(prefix, npage)) cmd = 'pdf2svg {} {} {}'.format(infpath, outfpath, npage) out, err = ut.run(cmd) if err: msg = ( 'dotaxes.py/writeEmptyHtmlPages: command [%s] returned error [%s] and output [%s]' % (cmd, err, out)) log.error(msg) raise Exception(msg) with open(outfpath) as f: svg = f.read() # todo move draftNotice to separate file # todo this switch shouldnt be here .but. dont want to make it easy to switch off! insertDraftNotice = 1 if insertDraftNotice: draftNotice = '<svg>' \ '<g fill="gray70" opacity="0.40"' \ ' transform="rotate(-50 420 350)"><text x="6" y="24"' \ ' transform="scale(10)">DRAFT</text><text x="-6" y="60"' \ ' transform="scale(7)">DO NOT USE</text></g></svg>' else: draftNotice = '' # insert draftNotice at end of svg file svg = svg \ .replace(' width="612pt" height="792pt"', '') \ .replace('</svg>', draftNotice + '</svg>') with open(outfpathFinal, 'w') as f: f.write(svg) removeFile(outfpath)
def __removeFromDir(subPath, parentDirPath): """ Removes 1 item (recursively) """ parentDirPath = absPath(parentDirPath) remPath = joinPath(parentDirPath, subPath) if isFile(remPath): removeFile(remPath) print('Removed "%s"' % (remPath, )) elif isDir(remPath): removeDir(remPath) print('Removed "%s"' % (remPath, ))
def cleanup_files(form): '''remove intermediate files''' prefix = form.prefix if 'c' in cfg.steps: rawXmlFname = pathjoin(cfg.dirName, '{}-text.xml'.format(prefix)) fmtXmlFname = pathjoin(cfg.dirName, '{}-fmt.xml'.format(prefix)) if ut.exists(rawXmlFname): removeFile(rawXmlFname) if ut.exists(fmtXmlFname): removeFile(fmtXmlFname)
def delUserFile(user_id): ''' Deletes all drugs for user with 'user_id'\n (Actually it just deletes the file)\n Returns 'False' if there is no file for user ''' try: removeFile(USER_DATA_PATH + str(user_id) + ".json") consoleLog(str(user_id) + "'s file was deleted.") except IOError: return False
def remove(s: str): try: if not exists(s): return if isfile(s): removeFile(s) elif isdir(s): p = s if s[-1] in ['/', '\\'] else f"{s}/" for v in listdir(s): remove(f"{p}{v}") removedirs(s) except: remove(s)
def findWord(self, bot, update, word=None): """A method to find a word in dictionary. If word is provided, it is used. If not, it is taken from the update.""" if word: msg = word else: msg = update.message.text chat_id = update.message.chat_id lS = LanguageSupport(self.userparams.getLang(chat_id)).languageSupport lang = self.userparams.getEntry(chat_id, "dict_lang") links_on = bool(self.userparams.getEntry(chat_id, 'word_links')) result = dictQuery(msg, lang, links_on) if result == 1: self.sendMessage(bot, update, MULTITRAN_DOWN_MESSAGE) else: reply = '' if isinstance(result, tuple): page_url = result[2] cur_lang = [i for i in LANGUAGE_INDICIES.keys() if LANGUAGE_INDICIES[i] == lang][0] db_variants = "" transcription_filename = "" if result[0] == 0: #word found, print result reply += result[1] db_variants = ";".join(map(lambda x: x.replace(";", "").strip(" \n\t\r"),result[3])) transcription_filename = result[4] elif result[0] == 2: # Word not found. Replacements may be present variants = result[1] string_variants = "" for n, variant in enumerate(variants): string_variants += "/" + str(n) + " " + variant + "\n" db_variants = ";".join(map(lambda x: x.replace(";", "").strip(" \n\t\r"), variants)) self.userparams.setEntry(chat_id, "variants", db_variants) reply = "{0}\n{1}\n{2}".format(lS(WORD_NOT_FOUND_MESSAGE), lS(POSSIBLE_REPLACEMENTS_MESSAGE) if variants else "", string_variants) reply += "{0}: {1}\n{2} {3}.".format(lS(LINK_TO_DICT_PAGE_MESSAGE), page_url, lS(CURRENT_LANGUAGE_IS_MESSAGE), cur_lang) self.sendMessage(bot, update, reply) self.userparams.setEntry(chat_id, "variants", db_variants) if self.userparams.getEntry(chat_id, "transcriptions_on"): if transcription_filename: self.sendPic(bot, update, transcription_filename, caption=msg) removeFile(transcription_filename)
def __windowsElevated(exePath, args=[], wrkDir=None): def __runElevated(exePath, args, wrkDir, sharedFilePath): verb = "runas" pyPath = PYTHON_PATH binPathArg = "'%s'" % (_normEscapePath(exePath), ) argsArg = '[ %s ]' % (','.join( ["'%s'" % (_normEscapePath(a), ) for a in args]), ) wrkDirArg = "'%s'" % (wrkDir, ) if wrkDir else "None" isElevatedArg = "True" isDebugArg = "True" sharedFilePathArg = "'%s'" % (_normEscapePath(sharedFilePath), ) args = ('-c "' 'from distbuilder import _run;' '_run( %s, args=%s, wrkDir=%s, isElevated=%s, ' 'isDebug=%s, sharedFilePath=%s );' '"' % (binPathArg, argsArg, wrkDirArg, isElevatedArg, isDebugArg, sharedFilePathArg)) if PY2: verb = unicode(verb) # @UndefinedVariable pyPath = unicode(pyPath) # @UndefinedVariable args = unicode(args) # @UndefinedVariable hwd = ctypes.windll.shell32.ShellExecuteW(None, verb, pyPath, args, None, __SW_HIDE) return int(hwd) > 32 # check if launched elevated def __getResults(sharedFilePath): POLL_FREQ_SECONDS = 0.25 sharedFile = _WindowsSharedFile(isProducer=False, filePath=sharedFilePath) retCode = None while retCode is None: sleep(POLL_FREQ_SECONDS) chunk = sharedFile.read() if chunk is None: continue if __SHARED_RET_CODE_PREFIX in chunk: parts = chunk.split(__SHARED_RET_CODE_PREFIX) chunk = parts[0] try: retCode = int(parts[1]) except: retCode = -1 stdout.write(chunk) stdout.flush() sharedFile.close() return retCode sharedFilePath = _reserveTempFile() isRun = __runElevated(exePath, args, wrkDir, sharedFilePath) retCode = __getResults(sharedFilePath) if isRun else None removeFile(sharedFilePath) return retCode
def loadHistoryFromDisk(self): path = self.DOWNLOADED_IMAGE_PATH + "/" removeAllIconsFromPath(path) list = listdir(path) for item in list: if checkIconName(item): filePath = path + item if delta > 0 and delta <= getModifyDeltaDateInDays(filePath): try: removeFile(filePath) except: print("ERROR: while removing file %s" % filePath) else: self.queueAA.append(item)
def to_ion_texture_model(texture_model_path, dest_directory=None, minimize_space=True): is_tmp = False if dest_directory is None: is_tmp = True dest_directory = mkdtemp() dest_file = path.join(dest_directory, path.basename(texture_model_path)) try: unzip_dir = path.join(dest_directory, "_tmp") mkdir(unzip_dir) with ZipFile(texture_model_path) as zipfile: zipfile.extractall(unzip_dir) files_to_delete = set() found_geo = False for file_name in file_walk(unzip_dir): if file_name.endswith(DELETE_EXTENSIONS): files_to_delete.add(file_name) elif file_name.endswith(".obj"): if "_geo" in path.basename(file_name): found_geo = True else: file_name = path.splitext(file_name)[0] files_to_delete.add(file_name + OBJ_FILE_EXTENSION) files_to_delete.add(file_name + MTL_FILE_EXTENSION) if not found_geo: raise IonInvalidZip("Unable to find geo file") for file_name in files_to_delete: if not path.isfile(file_name): continue removeFile(file_name) zip_dir(dest_file, unzip_dir, destructive=minimize_space) rmtree(unzip_dir) except Exception as e: if is_tmp: rmtree(dest_directory) raise e return dest_file, dest_directory
def __init__(self): def __check_file_integrity(local_src, remote_src): """ :param local_src: local txt file stored in 'self.__bw_local_path' :param remote_src: original txt file fetched from '__self.__bw_src' :return: 'True' if the files match and 'False' if otherwise. this Method compares original file's hash value (sha256) with current local file to verify it's integrity """ fetched_bw = str([ i.decode('utf-8') for i in remote_src.splitlines() ]).encode("utf-8") return sha256(fetched_bw).hexdigest() == sha256( str(local_src.read().splitlines()).encode( 'utf-8')).hexdigest() try: with open(self.__bw_local_path) as (f): resp = requests.get(self.__bw_src) if resp.status_code == 200 and not __check_file_integrity( f, resp.content): raise Exception('Corrupted File: {}'.format( self.__bw_local_path)) self.__blocked_words = f.read().splitlines() except Exception as e: """ if app reaches here, means that the local file stored in 'self.__bw_local_path' is corrupted. it will attempt to download the original list, remove current corrupt file and replace it. """ print(str(e)) self.__blocked_words = [ i.decode('utf-8') for i in requests.get(self.__bw_src).content.splitlines() ] if osPath.exists(self.__bw_local_path): removeFile(self.__bw_local_path) with open(self.__bw_local_path, "x") as (f): f.write('\n'.join(self.__blocked_words)) f.close()
def toZipFile(sourceDir, zipDest=None, removeScr=True, isWrapperDirIncluded=False): sourceDir = absPath(sourceDir) if zipDest is None: zipDest = sourceDir # make_archive adds extension else: if isFile(zipDest): removeFile(zipDest) zipDest, _ = splitExt(zipDest) if isWrapperDirIncluded: filePath = make_archive(zipDest, 'zip', dirPath(sourceDir), fileBaseName(sourceDir)) else: filePath = make_archive(zipDest, 'zip', sourceDir) print('Created zip file: "%s"' % (filePath, )) if removeScr: removeDir(sourceDir) print('Removed directory: "%s"' % (sourceDir, )) return filePath
def switchContact(self): try: try: if self.driver.find_element_by_xpath( '//*[@id="main"]/header/div[2]/div[1]/div/span' ).text == contactName: return None except NoSuchElementException: pass self.driver.find_element_by_xpath( f'//*[@title="{contactName}"]').click() print(f'Connected to: "{contactName}"') try: removeFile('./QR-Code.png') except: pass except NoSuchElementException: print("Contact doesn't exist!") exit(1)
def loadHistoryFromDisk(self): path = self.DOWNLOADED_IMAGE_PATH + "/" try: delta = int(config.plugins.BoardReader.deleteIcons.value) except: delta = -1 # check if we need to remove all old icons: if 0 == delta: removeAllIconsFromPath(path) list = listdir(path) for item in list: if checkIconName(item): filePath = path + item if delta > 0 and delta <= getModifyDeltaDateInDays(filePath): try: removeFile(filePath) except: print("ERROR: while removing file %s" % filePath) else: self.queueAA.append(item)
def deleteValue(self,name): ''' Delete a value with name name. ''' # clean up names to avoid stupid debug.add('deleting value ',name) # figure out the path to the named value file if name in self.names: filePath=self.namePaths[name] # remove the metadata entry del self.namePaths[name] # write changes to database metadata file writeFile(pathJoin(self.path,'names.table'),pickle(self.namePaths)) # update the length and names attributes self.names=self.namePaths.keys() self.length=len(self.names) else: return False if pathExists(filePath): # remove the file accocated with the value removeFile(filePath) return True else: return False
def __cleanSavedModel(self): if Path.exists(self.__FileName): removeFile(self.__FileName)
def remove(self, f): """Remove a file if it exists.""" if path.exists(f): removeFile(f)
def remove(self): self.close() if isFile(self.filePath): removeFile(self.filePath)
def __del__(self): removeFile(self.mTmpFileName)
def removeToken(): try: removeFile(TOKEN_FILE) return True except Exception: return False
except FileNotFoundError: localLogger.warning( "Failed to find resource locally. Embedded by page?") localLogger.debug("Writing temporary file") fileLoc = "file.tmp" with open(fileLoc, "wb") as file: file.write( BodyGenerator.Page.Tags.getHTMLContent( resourceCache[resource])) makeResource = DataURI.from_file(fileLoc, base64=True).replace("\n", "") localLogger.debug("Removing temporary file") removeFile(fileLoc) localLogger.debug("Made URI for '{}' from file in '{}'".format( resourceCache[resource], fileLoc)) resourceCache[resource] = makeResource BodyGenerator.HelperFunctions.Save("logs/resourcePackDump.log", str(resourceCache)) resourcePackVarLine = BodyGenerator.HelperFunctions.Read( "PublicResources/Scripts/resourcePackVarTemplate.js") resourcePackVarLine = resourcePackVarLine.replace("{}", str(resourceCache)) resourcePackVarScript = BodyGenerator.Page.Tags.HTMLElement( "script", selfClosing=False, innerHTML=resourcePackVarLine) resourcePackVarScript = str(resourcePackVarScript)
def command_send_activity_graph(self, bot, update): filename = self.activity_logger.visualizeTicks() self.sendFile(bot, update, filename, caption="Bot statistics") removeFile(filename)
def remove_file(response): try: removeFile(filename_zip) except Exception as error: app.logger.error("Error removing or closing downloaded file handle", error) return response
def execute(full): if full: print('[WARNING] Running fully integration') else: CSV_CONFIG["openFile"]["file"] = CSV_CONFIG["openFile"]["file"].format(complement=f"{datetime.today().strftime('%Y%m%d')}-") print('[WARNING] Normal execute') try: removeFile(CSV_CONFIG["openFile"]["file"]) except Exception as e: print('[ERROR] Error in remove old file', CSV_CONFIG["openFile"]["file"]) print(e) total = 0 currentOffset = 0 try: total = get_total_pokemons(config=REQUESTS_CONFIG["getPokemons"]) except Exception as e: print('[Error] Error in get total pokemons') raise Exception() print(f'[WARNING] Total pokemons: {total}') try: generate_csv(config=CSV_CONFIG, rows=[CSV_CONFIG["header"]]) print('[WARNING] Success in generate csv') except Exception as e: print('[ERROR] Error in generate csv') print(e) raise Exception() while currentOffset < total: print(f'[WARNING] Current offset: {currentOffset}') csvRows = None try: csvRows = proccess_pokemons( config=REQUESTS_CONFIG["getPokemon"], pokemons=get_pokemons(config=REQUESTS_CONFIG["getPokemons"], offset=currentOffset) ) print('[WARNING] Success in get and process pokemons') except Exception as e: print('[ERROR] Error in get and process pokemons') print(e) raise Exception() try: generate_csv(config=CSV_CONFIG, rows=csvRows) print('[WARNING] Success in append csv') except Exception as e: print('[ERROR] Error in append csv') print(e) raise Exception() currentOffset += REQUESTS_CONFIG["getPokemons"]["limit"] progress = round(currentOffset * 100/total, 2) print(f'[WARNING] Progress {progress if progress <= 100.00 else 100.00}%')
def remove(self,f): """Remove a file if it exists.""" if path.exists(f): removeFile(f)
def forward_request(service, method=None): # STEP 1. Read requests auth params auth = None if request.authorization is not None and len( request.authorization) > 0: auth = () for i in request.authorization: auth = auth + (request.authorization[i], ) if method == None: method = request.method if service == "upload/": if self.settings.SAFE_UPLOAD: self.log("New upload request detected") service = "/api/tools" data = dict(request.form) tmp_files = AdminFunctions.storeTmpFiles( request.files, self.settings.TMP_DIRECTORY) self.log("All files were temporary stored at: " + ", ".join(tmp_files)) self.log("Forwarding the files uploading...") history_id = data.get("history_id")[0] galaxy_key = data.get("key")[0] gi = GalaxyInstance(self.settings.GALAXY_SERVER, galaxy_key) responses = [] for tmp_file in tmp_files: responses.append( gi.tools.upload_file(tmp_file, history_id)) for tmp_file in tmp_files: removeFile(tmp_file) return jsonify({'success': True, 'responses': responses}) else: service = "/api/tools" data = dict(request.form) # STEP 2. Generate the new requests resp = requests.request(method=method, url=self.settings.GALAXY_SERVER + service, data=data, files=request.files, auth=auth, cookies=request.cookies, allow_redirects=False) elif service == "signup/": self.log("New sign up request detected") service = "/user/create?cntrller=user" data = dict(request.form) # STEP 2. Generate the new requests resp = requests.request( method=method, url=self.settings.GALAXY_SERVER + service, params=dict(request.args), headers={ u'content-type': u'application/x-www-form-urlencoded' }, data=data, auth=auth, cookies=request.cookies, allow_redirects=False) else: service = "/api/" + service self.log("New request detected (" + service + ")") # STEP 2. Generate the new requests resp = requests.request(method=method, url=self.settings.GALAXY_SERVER + service, params=dict(request.args), data=request.get_data(), auth=auth, cookies=request.cookies, allow_redirects=False) headers = [] excluded_headers = [ 'content-encoding', 'content-length', 'transfer-encoding', 'connection' ] headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers] self.log("Done! Returning response...") response = flask_response(resp.content, resp.status_code, headers) return response