Python tempfile._get_candidate_names() Examples

The following are 30 code examples for showing how to use tempfile._get_candidate_names(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module tempfile , or try the search function .

Example 1
Project: yatsm   Author: ceholden   File: test_cache.py    License: MIT License 6 votes vote down vote up
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 2
Project: gym-malware   Author: endgameinc   File: manipulate2.py    License: MIT License 6 votes vote down vote up
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 3
Project: sregistry-cli   Author: singularityhub   File: fileio.py    License: Mozilla Public License 2.0 6 votes vote down vote up
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 4
Project: RLs   Author: StepNeverStop   File: test_barracuda_converter.py    License: Apache License 2.0 6 votes vote down vote up
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 5
Project: singularity-cli   Author: singularityhub   File: base.py    License: Mozilla Public License 2.0 6 votes vote down vote up
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 6
Project: RENAT   Author: bachng2017   File: Fic.py    License: Apache License 2.0 6 votes vote down vote up
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 7
Project: bandersnatch   Author: pypa   File: test_storage_plugins.py    License: Academic Free License v3.0 6 votes vote down vote up
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
Project: keras-vis   Author: raghakot   File: utils.py    License: MIT License 6 votes vote down vote up
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 9
Project: wfuzz   Author: xmendez   File: test_acceptance.py    License: GNU General Public License v2.0 6 votes vote down vote up
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 10
Project: edgePy   Author: r-bioinformatics   File: test_DGEList.py    License: MIT License 6 votes vote down vote up
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 11
Project: owasp-pysec   Author: ebranca   File: temp.py    License: Apache License 2.0 6 votes vote down vote up
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 12
Project: deid   Author: pydicom   File: fileio.py    License: MIT License 6 votes vote down vote up
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 13
Project: dockerfiles   Author: demisto   File: utils.py    License: MIT License 6 votes vote down vote up
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 14
Project: grimoirelab-sortinghat   Author: chaoss   File: test_cmd_unify.py    License: GNU General Public License v3.0 5 votes vote down vote up
def setUp(self):
        super().setUp()
        self.recovery_path = os.path.join('/tmp', next(tempfile._get_candidate_names())) 
Example 15
Project: grimoirelab-sortinghat   Author: chaoss   File: test_cmd_unify.py    License: GNU General Public License v3.0 5 votes vote down vote up
def setUp(self):
        super().setUp()
        self.recovery_path = os.path.join('/tmp', next(tempfile._get_candidate_names())) 
Example 16
Project: pyvips   Author: libvips   File: helpers.py    License: MIT License 5 votes vote down vote up
def temp_filename(directory, suffix):
    temp_name = next(tempfile._get_candidate_names())
    filename = os.path.join(directory, temp_name + suffix)

    return filename


# test for an operator exists 
Example 17
Project: gym-malware   Author: endgameinc   File: manipulate2.py    License: MIT License 5 votes vote down vote up
def upx_pack(self, seed=None):
        # tested with UPX 3.91
        random.seed(seed)
        tmpfilename = os.path.join(
            tempfile._get_default_tempdir(), next(tempfile._get_candidate_names()))

        # dump bytez to a temporary file
        with open(tmpfilename, 'wb') as outfile:
            outfile.write(self.bytez)

        options = ['--force', '--overlay=copy']
        compression_level = random.randint(1, 9)
        options += ['-{}'.format(compression_level)]
        # --exact
        # compression levels -1 to -9
        # --overlay=copy [default]

        # optional things:
        # --compress-exports=0/1
        # --compress-icons=0/1/2/3
        # --compress-resources=0/1
        # --strip-relocs=0/1
        options += ['--compress-exports={}'.format(random.randint(0, 1))]
        options += ['--compress-icons={}'.format(random.randint(0, 3))]
        options += ['--compress-resources={}'.format(random.randint(0, 1))]
        options += ['--strip-relocs={}'.format(random.randint(0, 1))]

        with open(os.devnull, 'w') as DEVNULL:
            retcode = subprocess.call(
                ['upx'] + options + [tmpfilename, '-o', tmpfilename + '_packed'], stdout=DEVNULL, stderr=DEVNULL)

        os.unlink(tmpfilename)

        if retcode == 0:  # successfully packed

            with open(tmpfilename + '_packed', 'rb') as infile:
                self.bytez = infile.read()

            os.unlink(tmpfilename + '_packed')

        return self.bytez 
Example 18
Project: RoboGif   Author: izacus   File: utilities.py    License: Apache License 2.0 5 votes vote down vote up
def get_new_temp_file_path(extension):
    tmp_dir = tempfile._get_default_tempdir()
    tmp_name = next(tempfile._get_candidate_names())
    tmp_file = os.path.join(tmp_dir, tmp_name + "." + extension)
    return tmp_file 
Example 19
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    License: Apache License 2.0 5 votes vote down vote up
def test_retval(self):
        # _get_candidate_names returns a _RandomNameSequence object
        obj = tempfile._get_candidate_names()
        self.assertIsInstance(obj, tempfile._RandomNameSequence) 
Example 20
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    License: Apache License 2.0 5 votes vote down vote up
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 21
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    License: Apache License 2.0 5 votes vote down vote up
def _mock_candidate_names(*names):
    return support.swap_attr(tempfile,
                             '_get_candidate_names',
                             lambda: iter(names)) 
Example 22
Project: sregistry-cli   Author: singularityhub   File: api.py    License: Mozilla Public License 2.0 5 votes vote down vote up
def get_layer(self, image_id, repo_name, download_folder=None):
    """download an image layer (.tar.gz) to a specified download folder.

       Parameters
       ==========
       download_folder: download to this folder. If not set, uses temp.
       repo_name: the image name (library/ubuntu) to retrieve

    """
    url = self._get_layerLink(repo_name, image_id)

    bot.verbose("Downloading layers from %s" % url)

    download_folder = get_tmpdir(download_folder)
    download_folder = "%s/%s.tar.gz" % (download_folder, image_id)

    # Update user what we are doing
    bot.debug("Downloading layer %s" % image_id)

    # Step 1: Download the layer atomically
    file_name = "%s.%s" % (download_folder, next(tempfile._get_candidate_names()))

    tar_download = self.download(url, file_name)

    try:
        shutil.move(tar_download, download_folder)
    except:
        msg = "Cannot untar layer %s," % tar_download
        msg += " was there a problem with download?"
        bot.exit(msg)
    return download_folder 
Example 23
Project: sregistry-cli   Author: singularityhub   File: aws.py    License: Mozilla Public License 2.0 5 votes vote down vote up
def download_task(url, headers, download_to, download_type="layer"):
    """download an image layer (.tar.gz) to a specified download folder.
       This task is done by using local versions of the same download functions
       that are used for the client.
       core stream/download functions of the parent client.

       Parameters
       ==========
       image_id: the shasum id of the layer, already determined to not exist
       repo_name: the image name (library/ubuntu) to retrieve
       download_to: download to this folder. If not set, uses temp.
 

    """
    # Update the user what we are doing
    bot.verbose("Downloading %s from %s" % (download_type, url))

    # Step 1: Download the layer atomically
    file_name = "%s.%s" % (download_to, next(tempfile._get_candidate_names()))

    tar_download = download(url, file_name, headers=headers)

    try:
        shutil.move(tar_download, download_to)
    except:
        msg = "Cannot untar layer %s," % tar_download
        msg += " was there a problem with download?"
        bot.exit(msg)

    return download_to


################################################################################
## Base Functions for Tasks
##
##  These basic tasks are intended for the worker to use, without needing
##  to pickle them for multiprocessing. It works because they don't belong
##  to a client (which we cannot pickle) and are imported by the worker
##  functions directly.
##
################################################################################ 
Example 24
Project: sregistry-cli   Author: singularityhub   File: tasks.py    License: Mozilla Public License 2.0 5 votes vote down vote up
def download_task(url, headers, destination, download_type="layer"):
    """download an image layer (.tar.gz) to a specified download folder.
       This task is done by using local versions of the same download functions
       that are used for the client.
       core stream/download functions of the parent client.

       Parameters
       ==========
       image_id: the shasum id of the layer, already determined to not exist
       repo_name: the image name (library/ubuntu) to retrieve
       download_folder: download to this folder. If not set, uses temp.
 

    """
    # Update the user what we are doing
    bot.verbose("Downloading %s from %s" % (download_type, url))

    # Step 1: Download the layer atomically
    file_name = "%s.%s" % (destination, next(tempfile._get_candidate_names()))

    tar_download = download(url, file_name, headers=headers)

    try:
        shutil.move(tar_download, destination)
    except:
        msg = "Cannot untar layer %s," % tar_download
        msg += " was there a problem with download?"
        bot.exit(msg)

    return destination


################################################################################
## Base Functions for Tasks
##
##  These basic tasks are intended for the worker to use, without needing
##  to pickle them for multiprocessing. It works because they don't belong
##  to a client (which we cannot pickle) and are imported by the worker
##  functions directly.
##
################################################################################ 
Example 25
Project: BinderFilter   Author: dxwu   File: test_tempfile.py    License: MIT License 5 votes vote down vote up
def test_retval(self):
        # _get_candidate_names returns a _RandomNameSequence object
        obj = tempfile._get_candidate_names()
        self.assertIsInstance(obj, tempfile._RandomNameSequence) 
Example 26
Project: BinderFilter   Author: dxwu   File: test_tempfile.py    License: MIT License 5 votes vote down vote up
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 27
Project: oss-ftp   Author: aliyun   File: test_tempfile.py    License: MIT License 5 votes vote down vote up
def test_retval(self):
        # _get_candidate_names returns a _RandomNameSequence object
        obj = tempfile._get_candidate_names()
        self.assertIsInstance(obj, tempfile._RandomNameSequence) 
Example 28
Project: oss-ftp   Author: aliyun   File: test_tempfile.py    License: MIT License 5 votes vote down vote up
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 29
Project: oss-ftp   Author: aliyun   File: test_tempfile.py    License: MIT License 5 votes vote down vote up
def _mock_candidate_names(*names):
    return support.swap_attr(tempfile,
                             '_get_candidate_names',
                             lambda: iter(names)) 
Example 30
Project: differentiable-point-clouds   Author: eldar   File: render_point_cloud.py    License: MIT License 5 votes vote down vote up
def render_point_cloud(point_cloud, cfg):
    """
    Wraps the call to blender to render the image
    """
    cfg = edict(cfg)
    temp_dir = tempfile._get_default_tempdir()

    temp_name = next(tempfile._get_candidate_names())
    in_file = f"{temp_dir}/{temp_name}.npz"
    point_cloud_save = np.reshape(point_cloud, (1, -1, 3))
    np.savez(in_file, point_cloud_save)

    temp_name = next(tempfile._get_candidate_names())
    out_file = f"{temp_dir}/{temp_name}.png"

    args = build_command_line_args([["in_file", in_file],
                                    ["out_file", out_file],
                                    ["vis_azimuth", cfg.vis_azimuth],
                                    ["vis_elevation", cfg.vis_elevation],
                                    ["vis_dist", cfg.vis_dist],
                                    ["cycles_samples", cfg.render_cycles_samples],
                                    ["like_train_data", True],
                                    ["voxels", False],
                                    ["colored_subsets", False],
                                    ["image_size", cfg.render_image_size]],
                                   as_string=False)

    full_args = [blender_exec, "--background", "-P", python_script, "--"] + args
    subprocess.check_call(full_args, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)

    image = imageio.imread(out_file)
    os.remove(in_file)
    os.remove(out_file)

    return image