Python tempfile._get_candidate_names() Examples
The following are 30
code examples of tempfile._get_candidate_names().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tempfile
, or try the search function
.
Example #1
Source File: test_DGEList.py From edgePy with MIT License | 6 votes |
def test_cycle_dge_npz(): import tempfile import os tempdir = tempfile.mkdtemp(prefix="edgePy_tmp") file_name = tempdir + os.sep + next(tempfile._get_candidate_names()) dge_list_first = dge_list() dge_list_first.write_npz_file(filename=file_name) dge_list_second = DGEList(filename=file_name + ".npz") assert np.array_equal(dge_list_first.counts, dge_list_second.counts) assert np.array_equal(dge_list_first.genes, dge_list_second.genes) assert np.array_equal(dge_list_first.samples, dge_list_second.samples) assert np.array_equal(dge_list_first.norm_factors, dge_list_second.norm_factors) assert np.array_equal(dge_list_first.groups_list, dge_list_second.groups_list) os.remove(file_name + ".npz") os.rmdir(tempdir)
Example #2
Source File: test_cache.py From yatsm with MIT License | 6 votes |
def test_test_cache(mkdir_permissions): # Test when cache dir exists already path = mkdir_permissions(read=False, write=False) assert (False, False) == cache.test_cache(dict(cache_line_dir=path)) path = mkdir_permissions(read=False, write=True) assert (False, True) == cache.test_cache(dict(cache_line_dir=path)) path = mkdir_permissions(read=True, write=False) assert (True, False) == cache.test_cache(dict(cache_line_dir=path)) path = mkdir_permissions(read=True, write=True) assert (True, True) == cache.test_cache(dict(cache_line_dir=path)) # Test when cache dir doesn't exist tmp = os.path.join(tempfile.tempdir, next(tempfile._get_candidate_names()) + '_yatsm') read_write = cache.test_cache(dict(cache_line_dir=tmp)) os.removedirs(tmp) assert (True, True) == read_write
Example #3
Source File: Fic.py From RENAT with Apache License 2.0 | 6 votes |
def get_element_image(self,element=u'//body',filename=None): """ Get and opencv image object of the element and save it to file Returns a numpy array and temporarily filename """ result_path = Common.get_result_path() tmp_file = '%s/screen_%s.png' % (Common.get_result_path(),next(tempfile._get_candidate_names())) self._selenium.capture_page_screenshot(tmp_file) _element = self._selenium.get_webelement(element) pos = _element.location size = _element.size screen = cv2.imread(tmp_file) img = screen[int(pos['y']):int(pos['y']+size['height']),int(pos['x']):int(pos['x']+size['width'])] if filename: cv2.imwrite('%s/%s' % (result_path,filename),img) BuiltIn().log('Save image of element to file `%s`' % filename) return img,tmp_file
Example #4
Source File: manipulate2.py From gym-malware with MIT License | 6 votes |
def upx_unpack(self, seed=None): # dump bytez to a temporary file tmpfilename = os.path.join( tempfile._get_default_tempdir(), next(tempfile._get_candidate_names())) with open(tmpfilename, 'wb') as outfile: outfile.write(self.bytez) with open(os.devnull, 'w') as DEVNULL: retcode = subprocess.call( ['upx', tmpfilename, '-d', '-o', tmpfilename + '_unpacked'], stdout=DEVNULL, stderr=DEVNULL) os.unlink(tmpfilename) if retcode == 0: # sucessfully unpacked with open(tmpfilename + '_unpacked', 'rb') as result: self.bytez = result.read() os.unlink(tmpfilename + '_unpacked') return self.bytez
Example #5
Source File: utils.py From dockerfiles with MIT License | 6 votes |
def plt_t0_b64(plt: matplotlib.pyplot, figsize=None, dpi=None): """ Matplotlib to base64 url """ path = Path(tempfile.mkdtemp()) / Path( next(tempfile._get_candidate_names()) + '.png') figsize = figsize if figsize else (1, 1) dpi = dpi if dpi else DEFAULT_DPI # Remove paddings plt.tight_layout() plt.savefig(str(path), format='png', figsize=figsize, dpi=dpi) with open(str(path), "rb") as f: img_base64 = base64.b64encode(f.read()).decode("utf-8", "ignore") b64 = f'data:image/png;base64,{img_base64}' path.unlink() return b64
Example #6
Source File: temp.py From owasp-pysec with Apache License 2.0 | 6 votes |
def mkdtemp(dirpath, prefix='', suffix='', mode=0700): """Creates a directory in directory *dir* using *prefix* and *suffix* to name it: (dir)/<prefix><random_string><postfix> Returns absolute path of directory. """ dirpath = os.path.abspath(dirpath) names = _get_candidate_names() mode = int(mode) if not fcheck.mode_check(mode): raise ValueError("wrong mode: %r" % oct(mode)) for _ in xrange(TMP_MAX): name = names.next() fpath = os.path.abspath(os.path.join(dirpath, '%s%s%s' % (prefix, name, suffix))) try: os.mkdir(fpath, mode) return fpath except OSError, ex: if ex.errno == errno.EEXIST: # try again continue raise
Example #7
Source File: test_storage_plugins.py From bandersnatch with Academic Free License v3.0 | 6 votes |
def test_rmdir(self) -> None: tmp_filename = next(tempfile._get_candidate_names()) # type: ignore tmp_file = self.plugin.PATH_BACKEND( os.path.join(self.mirror_base_path, "test_dir", tmp_filename) ) tmp_file.write_text("") self.assertTrue( self.plugin.PATH_BACKEND( os.path.join(self.mirror_base_path, "test_dir") ).exists() ) tmp_file.unlink() self.assertFalse( self.plugin.PATH_BACKEND( os.path.join(self.mirror_base_path, "test_dir") ).exists() )
Example #8
Source File: fileio.py From deid with MIT License | 6 votes |
def get_temporary_name(prefix=None, ext=None): """get a temporary name, can be used for a directory or file. This does so without creating the file, and adds an optional prefix Parameters ========== prefix: if defined, add the prefix after deid ext: if defined, return the file extension appended. Do not specify "." """ deid_prefix = "deid-" if prefix: deid_prefix = "deid-%s-" % prefix tmpname = os.path.join( tempfile.gettempdir(), "%s%s" % (deid_prefix, next(tempfile._get_candidate_names())), ) if ext: tmpname = "%s.%s" % (tmpname, ext) return tmpname ################################################################################ ## FILE OPERATIONS ############################################################# ################################################################################
Example #9
Source File: utils.py From keras-vis with MIT License | 6 votes |
def apply_modifications(model, custom_objects=None): """Applies modifications to the model layers to create a new Graph. For example, simply changing `model.layers[idx].activation = new activation` does not change the graph. The entire graph needs to be updated with modified inbound and outbound tensors because of change in layer building function. Args: model: The `keras.models.Model` instance. Returns: The modified model with changes applied. Does not mutate the original `model`. """ # The strategy is to save the modified model and load it back. This is done because setting the activation # in a Keras layer doesnt actually change the graph. We have to iterate the entire graph and change the # layer inbound and outbound nodes with modified tensors. This is doubly complicated in Keras 2.x since # multiple inbound and outbound nodes are allowed with the Graph API. model_path = os.path.join(tempfile.gettempdir(), next(tempfile._get_candidate_names()) + '.h5') try: model.save(model_path) return load_model(model_path, custom_objects=custom_objects) finally: os.remove(model_path)
Example #10
Source File: test_barracuda_converter.py From RLs with Apache License 2.0 | 6 votes |
def test_barracuda_converter(): path_prefix = os.path.dirname(os.path.abspath(__file__)) tmpfile = os.path.join( tempfile._get_default_tempdir(), next(tempfile._get_candidate_names()) + ".nn" ) # make sure there are no left-over files if os.path.isfile(tmpfile): os.remove(tmpfile) tf2bc.convert(path_prefix + "/BasicLearning.pb", tmpfile) # test if file exists after conversion assert os.path.isfile(tmpfile) # currently converter produces small output file even if input file is empty # 100 bytes is high enough to prove that conversion was successful assert os.path.getsize(tmpfile) > 100 # cleanup os.remove(tmpfile)
Example #11
Source File: test_acceptance.py From wfuzz with GNU General Public License v2.0 | 6 votes |
def wfuzz_me_test_generator_previous_session(prev_session_cli, next_session_cli, expected_list): def test(self): temp_name = next(tempfile._get_candidate_names()) defult_tmp_dir = tempfile._get_default_tempdir() filename = os.path.join(defult_tmp_dir, temp_name) # first session with wfuzz.get_session(prev_session_cli) as s: ret_list = [x.eval(x._description) if x._description else x.description for x in s.fuzz(save=filename)] # second session wfuzzp as payload with wfuzz.get_session(next_session_cli.replace("$$PREVFILE$$", filename)) as s: ret_list = [x.eval(x._description) if x._description else x.description for x in s.fuzz()] self.assertEqual(sorted(ret_list), sorted(expected_list)) return test
Example #12
Source File: fileio.py From sregistry-cli with Mozilla Public License 2.0 | 6 votes |
def get_tmpdir(requested_tmpdir=None, prefix="", create=True): """get a temporary directory for an operation. If SREGISTRY_TMPDIR is set, return that. Otherwise, return the output of tempfile.mkdtemp Parameters ========== requested_tmpdir: an optional requested temporary directory, first priority as is coming from calling function. prefix: Given a need for a sandbox (or similar), we will need to create a subfolder *within* the SREGISTRY_TMPDIR. create: boolean to determine if we should create folder (True) """ from sregistry.defaults import SREGISTRY_TMPDIR # First priority for the base goes to the user requested. tmpdir = requested_tmpdir or SREGISTRY_TMPDIR prefix = prefix or "sregistry-tmp" prefix = "%s.%s" % (prefix, next(tempfile._get_candidate_names())) tmpdir = os.path.join(tmpdir, prefix) if not os.path.exists(tmpdir) and create is True: os.mkdir(tmpdir) return tmpdir
Example #13
Source File: base.py From singularity-cli with Mozilla Public License 2.0 | 6 votes |
def _get_conversion_outfile(self): """a helper function to return a conversion temporary output file based on kind of conversion Parameters ========== convert_to: a string either docker or singularity, if a different """ prefix = "spythonRecipe" if hasattr(self, "name"): prefix = self.name suffix = next(tempfile._get_candidate_names()) return "%s.%s" % (prefix, suffix) # Printing
Example #14
Source File: test_tempfile.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_same_thing(self): # _get_candidate_names always returns the same object a = tempfile._get_candidate_names() b = tempfile._get_candidate_names() self.assertTrue(a is b)
Example #15
Source File: test_tempfile.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_retval(self): # _get_candidate_names returns a _RandomNameSequence object obj = tempfile._get_candidate_names() self.assertIsInstance(obj, tempfile._RandomNameSequence)
Example #16
Source File: test_tempfile.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_same_thing(self): # _get_candidate_names always returns the same object a = tempfile._get_candidate_names() b = tempfile._get_candidate_names() self.assertTrue(a is b)
Example #17
Source File: util.py From picrust2 with GNU General Public License v3.0 | 5 votes |
def generate_temp_filename(temp_dir=None, prefix="", suffix=""): '''Function to generate path to temporary filenames (does not create the) files). The input arguments can be used to customize the temporary filename. If no temporary directory is specified then the default temprary directory will be used.''' # If temp_dir not set then get default directory. if not temp_dir: temp_dir = tempfile._get_default_tempdir() return(join(temp_dir, prefix + next(tempfile._get_candidate_names()) + suffix))
Example #18
Source File: test_tempfile.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def test_same_thing(self): # _get_candidate_names always returns the same object a = tempfile._get_candidate_names() b = tempfile._get_candidate_names() self.assertTrue(a is b)
Example #19
Source File: test_tempfile.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def _mock_candidate_names(*names): return support.swap_attr(tempfile, '_get_candidate_names', lambda: iter(names))
Example #20
Source File: test_tempfile.py From medicare-demo with Apache License 2.0 | 5 votes |
def test_retval(self): # _get_candidate_names returns a _RandomNameSequence object obj = tempfile._get_candidate_names() self.assert_(isinstance(obj, tempfile._RandomNameSequence))
Example #21
Source File: test_tempfile.py From medicare-demo with Apache License 2.0 | 5 votes |
def test_same_thing(self): # _get_candidate_names always returns the same object a = tempfile._get_candidate_names() b = tempfile._get_candidate_names() self.assert_(a is b)
Example #22
Source File: temp.py From owasp-pysec with Apache License 2.0 | 5 votes |
def mkstemp(dirpath, prefix='', suffix='', unlink=1, mode=0600): """Creates a file in directory *dir* using *prefix* and *suffix* to name it: (dir)/<prefix><random_string><postfix> Returns a couple of files (pysec.io.fd.File): (Read_File, Write_File) If *unlink* is true registers a unlink function at exit. """ dirpath = os.path.abspath(dirpath) mode = int(mode) if not fcheck.mode_check(mode): raise ValueError("wrong mode: %r" % oct(mode)) names = _get_candidate_names() for _ in xrange(TMP_MAX): name = names.next() fpath = os.path.join(dirpath, '%s%s%s' % (prefix, name, suffix)) if unlink: atexit.register(os.unlink, fpath) fdr = fdw = -1 try: fdr = os.open(fpath, os.O_RDONLY | os.O_CREAT | os.O_EXCL, mode) fdw = os.open(fpath, os.O_WRONLY, mode) _set_cloexec(fdr) _set_cloexec(fdw) return fd.File(fdr), fd.File(fdw) except OSError, ex: if fdr != -1: os.close(fdr) if fdw != -1: os.close(fdw) if ex.errno == errno.EEXIST: continue else: try: os.unlink(fpath) except IOError: pass raise except:
Example #23
Source File: test_ankidirect.py From AnkiTools with MIT License | 5 votes |
def __init__(self, collection): if collection is None: collection = get_collection_path() self.collection_path = next(tempfile._get_candidate_names()) shutil.copy(src=collection, dst=self.collection_path) self.ankidirect = AnkiDirect(anki_database=self.collection_path)
Example #24
Source File: test_acceptance.py From wfuzz with GNU General Public License v2.0 | 5 votes |
def wfuzz_me_test_generator_recipe(url, payloads, params, expected_list): def test(self): temp_name = next(tempfile._get_candidate_names()) defult_tmp_dir = tempfile._get_default_tempdir() filename = os.path.join(defult_tmp_dir, temp_name) # Wfuzz results with wfuzz.FuzzSession(url=url, **params) as s: s.export_to_file(filename) if payloads is None: fuzzed = s.fuzz() else: fuzzed = s.get_payloads(payloads).fuzz() ret_list = [(x.code, x.history.urlparse.path) for x in fuzzed] # repeat test with recipe as only parameter with wfuzz.FuzzSession(recipe=[filename]) as s: if payloads is None: same_list = [(x.code, x.history.urlparse.path) for x in s.fuzz()] else: same_list = [(x.code, x.history.urlparse.path) for x in s.get_payloads(payloads).fuzz()] self.assertEqual(sorted(ret_list), sorted(same_list)) return test
Example #25
Source File: test_acceptance.py From wfuzz with GNU General Public License v2.0 | 5 votes |
def wfuzz_me_test_generator_saveres(url, payloads, params, expected_list): def test(self): if not expected_list: return temp_name = next(tempfile._get_candidate_names()) defult_tmp_dir = tempfile._get_default_tempdir() filename = os.path.join(defult_tmp_dir, temp_name) # Wfuzz results with wfuzz.FuzzSession(url=url, **dict(list(params.items()) + list(dict(save=filename).items()))) as s: if payloads is None: fuzzed = s.fuzz() else: fuzzed = s.get_payloads(payloads).fuzz() ret_list = [(x.code, x.history.urlparse.path) for x in fuzzed] # repeat test with performaing same saved request with wfuzz.FuzzSession(payloads=[("wfuzzp", dict(fn=filename))], url="FUZZ") as s: same_list = [(x.code, x.history.urlparse.path) for x in s.fuzz()] self.assertEqual(sorted(ret_list), sorted(same_list)) # repeat test with performaing FUZZ[url] saved request with wfuzz.FuzzSession(payloads=[("wfuzzp", dict(fn=filename))], url="FUZZ[url]") as s: same_list = [(x.code, x.history.urlparse.path) for x in s.fuzz()] self.assertEqual(sorted(ret_list), sorted(same_list)) return test
Example #26
Source File: screenshot.py From wfuzz with GNU General Public License v2.0 | 5 votes |
def process(self, fuzzresult): temp_name = next(tempfile._get_candidate_names()) defult_tmp_dir = tempfile._get_default_tempdir() filename = os.path.join(defult_tmp_dir, temp_name + ".png") subprocess.call(['cutycapt', '--url=%s' % pipes.quote(fuzzresult.url), '--out=%s' % filename]) self.add_result("Screnshot taken, output at %s" % filename)
Example #27
Source File: utils.py From snn_toolbox with MIT License | 5 votes |
def apply_modifications(model, custom_objects=None): """Applies modifications to the model layers to create a new Graph. For example, simply changing ``model.layers[idx].activation = new activation`` does not change the graph. The entire graph needs to be updated with modified inbound and outbound tensors because of change in layer building function. Parameters ---------- model: keras.models.Model custom_objects: dict Returns ------- The modified model with changes applied. Does not mutate the original ``model``. """ # The strategy is to save the modified model and load it back. This is done # because setting the activation in a Keras layer doesnt actually change # the graph. We have to iterate the entire graph and change the layer # inbound and outbound nodes with modified tensors. This is doubly # complicated in Keras 2.x since multiple inbound and outbound nodes are # allowed with the Graph API. # Taken from # https://github.com/raghakot/keras-vis/blob/master/vis/utils/utils.py # noinspection PyProtectedMember model_path = os.path.join(tempfile.gettempdir(), next(tempfile._get_candidate_names()) + '.h5') try: model.save(model_path) return keras.models.load_model(model_path, custom_objects) finally: os.remove(model_path)
Example #28
Source File: test_storage_plugins.py From bandersnatch with Academic Free License v3.0 | 5 votes |
def test_mkdir(self) -> None: tmp_filename = next(tempfile._get_candidate_names()) # type: ignore tmp_file = self.plugin.PATH_BACKEND( os.path.join(self.mirror_base_path, "test_dir", tmp_filename) ) tmp_file.write_text("") self.assertTrue( self.plugin.PATH_BACKEND( os.path.join(self.mirror_base_path, "test_dir") ).exists() ) tmp_file.unlink()
Example #29
Source File: bigip_ucs_fetch.py From ansible_f5 with Apache License 2.0 | 5 votes |
def src(self): if self._values['src'] is not None: return self._values['src'] result = next(tempfile._get_candidate_names()) return result
Example #30
Source File: bigip_config.py From ansible_f5 with Apache License 2.0 | 5 votes |
def merge(self, verify=True): temp_name = next(tempfile._get_candidate_names()) remote_path = "/var/config/rest/downloads/{0}".format(temp_name) temp_path = '/tmp/' + temp_name if self.client.check_mode: return True self.upload_to_device(temp_name) self.move_on_device(remote_path) response = self.merge_on_device( remote_path=temp_path, verify=verify ) self.remove_temporary_file(remote_path=temp_path) return response