Python tempfile.NamedTemporaryFile() Examples

The following are 30 code examples for showing how to use tempfile.NamedTemporaryFile(). 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: BERT-Classification-Tutorial   Author: Socialbird-AILab   File: tokenization_test.py    License: Apache License 2.0 6 votes vote down vote up
def test_full_tokenizer(self):
        vocab_tokens = [
            "[UNK]", "[CLS]", "[SEP]", "want", "##want", "##ed", "wa", "un", "runn",
            "##ing", ","
        ]
        with tempfile.NamedTemporaryFile(delete=False) as vocab_writer:
            vocab_writer.write("".join([x + "\n" for x in vocab_tokens]))

            vocab_file = vocab_writer.name

        tokenizer = tokenization.FullTokenizer(vocab_file)
        os.unlink(vocab_file)

        tokens = tokenizer.tokenize(u"UNwant\u00E9d,running")
        self.assertAllEqual(tokens, ["un", "##want", "##ed", ",", "runn", "##ing"])

        self.assertAllEqual(
            tokenizer.convert_tokens_to_ids(tokens), [7, 4, 5, 10, 8, 9]) 
Example 2
def isUpdatesAvailable(cls, path):
        if sys.version_info < (3, 0):
            return False
        # pylint: disable=broad-except
        if not os.path.isfile(os.path.join(path, "files.xml")):
            return True
        try:
            available = dict()
            for it in ET.parse(os.path.join(path, "files.xml")).iter():
                if it.tag == "File":
                    available[it.text] = datetime.datetime.strptime(it.attrib["Modified"], "%d-%m-%Y")

            path = NamedTemporaryFile()
            path.close()
            urllib.request.urlretrieve("https://www.gurux.fi/obis/files.xml", path.name)
            for it in ET.parse(path.name).iter():
                if it.tag == "File":
                    tmp = datetime.datetime.strptime(it.attrib["Modified"], "%d-%m-%Y")
                    if not it.text in available or available[it.text] != tmp:
                        return True
        except Exception as e:
            print(e)
            return True
        return False 
Example 3
Project: incubator-spot   Author: apache   File: processing.py    License: Apache License 2.0 6 votes vote down vote up
def convert(netflow, tmpdir, opts='', prefix=None):
    '''
        Convert `nfcapd` file to a comma-separated output format.

    :param netflow : Path of binary file.
    :param tmpdir  : Path of local staging area.
    :param opts    : A set of options for `nfdump` command.
    :param prefix  : If `prefix` is specified, the file name will begin with that;
                     otherwise, a default `prefix` is used.
    :returns       : Path of CSV-converted file.
    :rtype         : ``str``
    :raises OSError: If an error occurs while executing the `nfdump` command.
    '''
    logger = logging.getLogger('SPOT.INGEST.FLOW.PROCESS')

    with tempfile.NamedTemporaryFile(prefix=prefix, dir=tmpdir, delete=False) as fp:
        command = COMMAND.format(netflow, opts, fp.name)

        logger.debug('Execute command: {0}'.format(command))
        Util.popen(command, raises=True)

        return fp.name 
Example 4
Project: incubator-spot   Author: apache   File: processing.py    License: Apache License 2.0 6 votes vote down vote up
def convert(logfile, tmpdir, opts='', prefix=None):
    '''
        Copy log file to the local staging area.

    :param logfile: Path of log file.
    :param tmpdir : Path of local staging area.
    :param opts   : A set of options for the `cp` command.
    :param prefix : If `prefix` is specified, the file name will begin with that;
                     otherwise, a default `prefix` is used.
    :returns      : Path of log file in local staging area.
    :rtype        : ``str``
    '''
    logger = logging.getLogger('SPOT.INGEST.PROXY.PROCESS')

    with tempfile.NamedTemporaryFile(prefix=prefix, dir=tmpdir, delete=False) as fp:
        command = COMMAND.format(opts, logfile, fp.name)

        logger.debug('Execute command: {0}'.format(command))
        Util.popen(command, raises=True)

        return fp.name 
Example 5
Project: incubator-spot   Author: apache   File: processing.py    License: Apache License 2.0 6 votes vote down vote up
def convert(pcap, tmpdir, opts='', prefix=None):
    '''
        Convert `pcap` file to a comma-separated output format.

    :param pcap    : Path of binary file.
    :param tmpdir  : Path of local staging area.
    :param opts    : A set of options for `tshark` command.
    :param prefix  : If `prefix` is specified, the file name will begin with that;
                     otherwise, a default `prefix` is used.
    :returns       : Path of CSV-converted file.
    :rtype         : ``str``
    :raises OSError: If an error occurs while executing the `tshark` command.
    '''
    logger = logging.getLogger('SPOT.INGEST.DNS.PROCESS')

    with tempfile.NamedTemporaryFile(prefix=prefix, dir=tmpdir, delete=False) as fp:
        command = COMMAND.format(pcap, opts, fp.name)

        logger.debug('Execute command: {0}'.format(command))
        Util.popen(command, raises=True)

        return fp.name 
Example 6
Project: mmdetection   Author: open-mmlab   File: upgrade_model_version.py    License: Apache License 2.0 6 votes vote down vote up
def parse_config(config_strings):
    temp_file = tempfile.NamedTemporaryFile()
    config_path = f'{temp_file.name}.py'
    with open(config_path, 'w') as f:
        f.write(config_strings)

    config = Config.fromfile(config_path)
    is_two_stage = True
    is_ssd = False
    is_retina = False
    reg_cls_agnostic = False
    if 'rpn_head' not in config.model:
        is_two_stage = False
        # check whether it is SSD
        if config.model.bbox_head.type == 'SSDHead':
            is_ssd = True
        elif config.model.bbox_head.type == 'RetinaHead':
            is_retina = True
    elif isinstance(config.model['bbox_head'], list):
        reg_cls_agnostic = True
    elif 'reg_class_agnostic' in config.model.bbox_head:
        reg_cls_agnostic = config.model.bbox_head \
            .reg_class_agnostic
    temp_file.close()
    return is_two_stage, is_ssd, is_retina, reg_cls_agnostic 
Example 7
Project: models   Author: kipoi   File: model.py    License: MIT License 6 votes vote down vote up
def predict_on_batch(self, inputs):
        # write test fasta file
        temp_input = tempfile.NamedTemporaryFile(suffix = ".txt")
        test_fname = temp_input.name
        encode_sequence_into_fasta_file(ofname = test_fname, seq = inputs.tolist())
        # test gkmsvm
        temp_ofp = tempfile.NamedTemporaryFile(suffix = ".txt")
        threads_option = '-T %s' % (str(self.threads))
        verbosity_option = '-v 0'
        command = ' '.join(['gkmpredict',
                            test_fname,
                            self.model_file,
                            temp_ofp.name,
                            threads_option,
                            verbosity_option])
        #process = subprocess.Popen(command, shell=True)
        #process.wait()  # wait for it to finish
        exit_code = os.system(command)
        temp_input.close()
        assert exit_code == 0
        # get classification results
        temp_ofp.seek(0)
        y = np.array([line.split()[-1] for line in temp_ofp], dtype=float)
        temp_ofp.close()
        return np.expand_dims(y, 1) 
Example 8
Project: PyOptiX   Author: ozen   File: setup.py    License: MIT License 6 votes vote down vote up
def save_pyoptix_conf(nvcc_path, compile_args, include_dirs, library_dirs, libraries):
    try:
        config = ConfigParser()
        config.add_section('pyoptix')

        config.set('pyoptix', 'nvcc_path', nvcc_path)
        config.set('pyoptix', 'compile_args', os.pathsep.join(compile_args))
        config.set('pyoptix', 'include_dirs', os.pathsep.join(include_dirs))
        config.set('pyoptix', 'library_dirs', os.pathsep.join(library_dirs))
        config.set('pyoptix', 'libraries', os.pathsep.join(libraries))

        tmp = NamedTemporaryFile(mode='w+', delete=False)
        config.write(tmp)
        tmp.close()
        config_path = os.path.join(os.path.dirname(sys.executable), 'pyoptix.conf')
        check_call_sudo_if_fails(['cp', tmp.name, config_path])
        check_call_sudo_if_fails(['cp', tmp.name, '/etc/pyoptix.conf'])
        check_call_sudo_if_fails(['chmod', '644', config_path])
        check_call_sudo_if_fails(['chmod', '644', '/etc/pyoptix.conf'])
    except Exception as e:
        print("PyOptiX configuration could not be saved. When you use pyoptix.Compiler, "
              "nvcc path must be in PATH, OptiX library paths must be in LD_LIBRARY_PATH, and pyoptix.Compiler "
              "attributes should be set manually.") 
Example 9
Project: Paradrop   Author: ParadropLabs   File: test_hostconfig.py    License: Apache License 2.0 6 votes vote down vote up
def test_prepareHostConfig(settings, detectSystemDevices):
    """
    Test paradrop.core.config.hostconfig.prepareHostConfig
    """
    from paradrop.core.config.hostconfig import prepareHostConfig

    devices = {
        'wan': [{'name': 'eth0'}],
        'lan': list(),
        'wifi': list()
    }
    detectSystemDevices.return_value = devices

    source = tempfile.NamedTemporaryFile(delete=True)
    source.write("{test: value}")
    source.flush()

    settings.HOST_CONFIG_FILE = source.name
    settings.DEFAULT_LAN_ADDRESS = "1.2.3.4"
    settings.DEFAULT_LAN_NETWORK = "1.0.0.0/24"

    config = prepareHostConfig()
    assert config['test'] == 'value' 
Example 10
Project: glazier   Author: google   File: download.py    License: Apache License 2.0 6 votes vote down vote up
def DownloadFileTemp(self, url, max_retries=5, show_progress=False):
    """Downloads a file to temporary storage.

    Args:
      url:  The address of the file to be downloaded.
      max_retries:  The number of times to attempt to download
        a file if the first attempt fails.
      show_progress: Print download progress to stdout (overrides default).

    Returns:
      A string containing a path to the temporary file.
    """
    destination = tempfile.NamedTemporaryFile()
    self._save_location = destination.name
    destination.close()
    if self._beyondcorp.CheckBeyondCorp():
      url = self._SetUrl(url)
      max_retries = -1
    file_stream = self._OpenStream(url, max_retries)
    self._StreamToDisk(file_stream, show_progress)
    return self._save_location 
Example 11
Project: smother   Author: ChrisBeaumont   File: test_cli.py    License: MIT License 6 votes vote down vote up
def test_csv():
    expected = '\n'.join([
        'source_context, test_context',
        'smother/tests/demo.py:11,test2',
        'smother/tests/demo.py:4,test4',
        '',
    ])

    runner = CliRunner()

    with NamedTemporaryFile(mode='w+') as tf:
        result = runner.invoke(
            cli,
            ['-r', 'smother/tests/.smother_2',
             'csv',
             tf.name
             ]
        )
        assert result.exit_code == 0
        tf.seek(0)
        assert tf.read() == expected 
Example 12
Project: smother   Author: ChrisBeaumont   File: test_cli.py    License: MIT License 6 votes vote down vote up
def test_semantic_csv():
    expected = '\n'.join([
        'source_context, test_context',
        'smother.tests.demo,test4',
        'smother.tests.demo:bar,test2',
        '',
    ])

    runner = CliRunner()

    with NamedTemporaryFile(mode='w+') as tf:
        result = runner.invoke(
            cli,
            ['-r', 'smother/tests/.smother_2',
             '--semantic',
             'csv',
             tf.name
             ]
        )
        assert result.exit_code == 0
        tf.seek(0)
        assert tf.read() == expected 
Example 13
Project: flores   Author: facebookresearch   File: translate.py    License: Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
def translate_files_slurm(args, cmds, expected_output_files):
    conda_env = '/private/home/pipibjc/.conda/envs/fairseq-20190509'
    for cmd in cmds:
        with TempFile('w') as script:
            sh = f"""#!/bin/bash
            source activate {conda_env}
            {cmd}
            """
            print(sh)
            script.write(sh)
            script.flush()
            cmd = f"sbatch --gres=gpu:1 -c {args.cpu + 2} {args.sbatch_args} --time=15:0:0 {script.name}"
            import sys
            print(cmd, file=sys.stderr)
            check_call(cmd, shell=True)

    # wait for all outputs has finished
    num_finished = 0
    while num_finished < len(expected_output_files):
        num_finished = 0
        for output_file in expected_output_files:
            num_finished += 1 if check_finished(output_file) else 0
        if num_finished < len(expected_output_files):
            time.sleep(3 * 60)
            print("sleeping for 3m ...") 
Example 14
Project: neural-style-docker   Author: albarji   File: algorithms.py    License: MIT License 6 votes vote down vote up
def gatys(content, style, outfile, size, weight, stylescale, algparams):
    """Runs Gatys et al style-transfer algorithm

    References:
        * https://arxiv.org/abs/1508.06576
        * https://github.com/jcjohnson/neural-style
    """
    # Gatys can only process one combination of content, style, weight and scale at a time, so we need to iterate
    tmpout = NamedTemporaryFile(suffix=".png")
    runalgorithm("gatys", [
        "-content_image", content,
        "-style_image", style,
        "-style_weight", weight * 100,  # Because content weight is 100
        "-style_scale", stylescale,
        "-output_image", tmpout.name,
        "-image_size", size if size is not None else shape(content)[0],
        *algparams
    ])
    # Transform to original file format
    convert(tmpout.name, outfile)
    tmpout.close() 
Example 15
Project: Authenticator   Author: bilelmoussaoui   File: gnupg.py    License: GNU General Public License v2.0 6 votes vote down vote up
def __on_apply(self, *__):
        from ...models import BackupJSON
        try:
            paraphrase = self.paraphrase_widget.entry.get_text()
            if not paraphrase:
                paraphrase = " "
            output_file = path.join(GLib.get_user_cache_dir(),
                                    path.basename(NamedTemporaryFile().name))
            status = GPG.get_default().decrypt_json(self._filename, paraphrase, output_file)
            if status.ok:
                BackupJSON.import_file(output_file)
                self.destroy()
            else:
                self.__send_notification(_("There was an error during the import of the encrypted file."))

        except AttributeError:
            Logger.error("[GPG] Invalid JSON file.") 
Example 16
Project: gnocchi   Author: gnocchixyz   File: file.py    License: Apache License 2.0 6 votes vote down vote up
def _store_new_measures(self, metric_id, data):
        tmpfile = tempfile.NamedTemporaryFile(
            prefix='gnocchi', dir=self.basepath_tmp,
            delete=False)
        tmpfile.write(data)
        tmpfile.close()
        path = self._build_measure_path(metric_id, True)
        while True:
            try:
                os.rename(tmpfile.name, path)
                break
            except OSError as e:
                if e.errno != errno.ENOENT:
                    raise
                try:
                    os.mkdir(self._build_measure_path(metric_id))
                except OSError as e:
                    # NOTE(jd) It's possible that another process created the
                    # path just before us! In this case, good for us, let's do
                    # nothing then! (see bug #1475684)
                    if e.errno != errno.EEXIST:
                        raise 
Example 17
Project: pyscf   Author: pyscf   File: test_diis.py    License: Apache License 2.0 6 votes vote down vote up
def test_diis_restart(self):
        mol = gto.M(
            verbose = 7,
            output = '/dev/null',
            atom = '''
        O     0    0        0
        H     0    -1.757   1.587
        H     0    1.757    1.587''',
            basis = '631g',
        )
        tmpf = tempfile.NamedTemporaryFile()
        mf = scf.RHF(mol)
        mf.diis_file = tmpf.name
        eref = mf.kernel()
        self.assertAlmostEqual(eref, -75.44606939063496, 9)

        mf = scf.RHF(mol)
        mf.diis = scf.diis.DIIS().restore(tmpf.name)
        mf.max_cycle = 3
        e = mf.kernel()
        self.assertAlmostEqual(e, eref, 9) 
Example 18
Project: pyscf   Author: pyscf   File: uintermediates_slow.py    License: Apache License 2.0 6 votes vote down vote up
def cc_Wvvvv(t1,t2,eris):
    tau = make_tau(t2,t1,t1)
    #eris_vovv = np.array(eris.ovvv).transpose(1,0,3,2)
    #tmp = einsum('mb,amef->abef',t1,eris_vovv)
    #Wabef = eris.vvvv - tmp + tmp.transpose(1,0,2,3)
    #Wabef += 0.25*einsum('mnab,mnef->abef',tau,eris.oovv)
    if t1.dtype == np.complex: ds_type = 'c16'
    else: ds_type = 'f8'
    _tmpfile1 = tempfile.NamedTemporaryFile(dir=lib.param.TMPDIR)
    fimd = h5py.File(_tmpfile1.name)
    nocc, nvir = t1.shape
    Wabef = fimd.create_dataset('vvvv', (nvir,nvir,nvir,nvir), ds_type)
    for a in range(nvir):
        Wabef[a] = eris.vvvv[a] 
        Wabef[a] -= einsum('mb,mfe->bef',t1,eris.ovvv[:,a,:,:]) 
        Wabef[a] += einsum('m,mbfe->bef',t1[:,a],eris.ovvv) 
        Wabef[a] += 0.25*einsum('mnb,mnef->bef',tau[:,:,a,:],eris.oovv)
    return Wabef 
Example 19
Project: pyscf   Author: pyscf   File: uintermediates_slow.py    License: Apache License 2.0 6 votes vote down vote up
def Wvvvv(t1,t2,eris):
    tau = make_tau(t2,t1,t1)
    #Wabef = cc_Wvvvv(t1,t2,eris) + 0.25*einsum('mnab,mnef->abef',tau,eris.oovv)
    if t1.dtype == np.complex: ds_type = 'c16'
    else: ds_type = 'f8'
    _tmpfile1 = tempfile.NamedTemporaryFile(dir=lib.param.TMPDIR)
    fimd = h5py.File(_tmpfile1.name)
    nocc, nvir = t1.shape
    Wabef = fimd.create_dataset('vvvv', (nvir,nvir,nvir,nvir), ds_type)
    #_cc_Wvvvv = cc_Wvvvv(t1,t2,eris)
    for a in range(nvir):
        #Wabef[a] = _cc_Wvvvv[a]
        Wabef[a] = eris.vvvv[a] 
        Wabef[a] -= einsum('mb,mfe->bef',t1,eris.ovvv[:,a,:,:]) 
        Wabef[a] += einsum('m,mbfe->bef',t1[:,a],eris.ovvv) 
        #Wabef[a] += 0.25*einsum('mnb,mnef->bef',tau[:,:,a,:],eris.oovv)

        #Wabef[a] += 0.25*einsum('mnb,mnef->bef',tau[:,:,a,:],eris.oovv) 
        Wabef[a] += 0.5*einsum('mnb,mnef->bef',tau[:,:,a,:],eris.oovv) 
    return Wabef 
Example 20
Project: pyscf   Author: pyscf   File: test_mole.py    License: Apache License 2.0 6 votes vote down vote up
def test_tofile(self):
        tmpfile = tempfile.NamedTemporaryFile()
        mol = gto.M(atom=[[1  , (0.,1.,1.)],
                          ["O1", (0.,0.,0.)],
                          [1  , (1.,1.,0.)], ])
        out1 = mol.tofile(tmpfile.name, format='xyz')
        ref = '''3
XYZ from PySCF
H           0.00000        1.00000        1.00000
O           0.00000        0.00000        0.00000
H           1.00000        1.00000        0.00000
'''
        with open(tmpfile.name, 'r') as f:
            self.assertEqual(f.read(), ref)
        self.assertEqual(out1, ref[:-1])

        tmpfile = tempfile.NamedTemporaryFile(suffix='.zmat')
        str1 = mol.tofile(tmpfile.name, format='zmat')
        #FIXME:self.assertEqual(mol._atom, mol.fromfile(tmpfile.name)) 
Example 21
Project: jumpserver-python-sdk   Author: jumpserver   File: test_auth.py    License: GNU General Public License v2.0 5 votes vote down vote up
def test_load_from_f(self):
        with tempfile.NamedTemporaryFile('w+t') as f:
            f.write(self.access_key_val)
            f.flush()
            access_key = AccessKey()
            access_key.load_from_f(f.name)
            self.assertEqual(access_key, self.access_key) 
Example 22
Project: aegea   Author: kislyuk   File: build_docker_image.py    License: Apache License 2.0 5 votes vote down vote up
def build_docker_image(args):
    for key, value in config.build_image.items():
        getattr(args, key).extend(value)
    args.tags += ["AegeaVersion={}".format(__version__),
                  'description="Built by {} for {}"'.format(__name__, ARN.get_iam_username())]
    ensure_ecr_repo(args.name, read_access=args.read_access)
    with tempfile.NamedTemporaryFile(mode="wt") as exec_fh:
        exec_fh.write(build_docker_image_shellcode.format(dockerfile=encode_dockerfile(args),
                                                          use_cache=json.dumps(args.use_cache)))
        exec_fh.flush()
        submit_args = submit_parser.parse_args(["--execute", exec_fh.name])
        submit_args.volumes = [["/var/run/docker.sock", "/var/run/docker.sock"]]
        submit_args.privileged = True
        submit_args.watch = True
        submit_args.dry_run = args.dry_run
        submit_args.image = args.builder_image
        submit_args.environment = [
            dict(name="TAG", value=args.tag),
            dict(name="REPO", value=args.name),
            dict(name="AWS_DEFAULT_REGION", value=ARN.get_region()),
            dict(name="AWS_ACCOUNT_ID", value=ARN.get_account_id())
        ]
        builder_iam_role = ensure_iam_role(__name__, trust=["ecs-tasks"], policies=args.builder_iam_policies)
        submit_args.job_role = builder_iam_role.name
        job = submit(submit_args)
    return dict(job=job) 
Example 23
Project: arm_now   Author: nongiach   File: filesystem.py    License: MIT License 5 votes vote down vote up
def create(self, dest, content, right=444):
        with tempfile.NamedTemporaryFile() as temp:
            temp.write(bytes(content, "utf-8"))
            temp.flush()
            subprocess.check_call("e2cp -G 0 -O 0 -P".split(' ') + [str(right), temp.name, self.rootfs + ":" + dest]) 
Example 24
Project: zmirror   Author: aploium   File: cache_system.py    License: MIT License 5 votes vote down vote up
def put_obj(self, key, obj, expires=DEFAULT_EXPIRE, obj_size=0, last_modified=None, info_dict=None):
        """
        将一个对象存入缓存
        :param key: key
        :param last_modified: str  format: "Mon, 18 Nov 2013 09:02:42 GMT"
        :param obj_size: too big object should not be cached
        :param expires: seconds to expire
        :param info_dict: custom dict contains information, stored in memory, so can access quickly
        :type key: str
        :type last_modified: str
        :type info_dict: dict or None
        :type obj: Any
        """
        if expires <= 0 or obj_size > self.max_size_byte:
            return False

        self.delete(key)

        temp_file = tempfile.NamedTemporaryFile(prefix="zmirror_", suffix=".tmp", delete=False)
        pickle.dump(obj, temp_file, protocol=pickle.HIGHEST_PROTOCOL)

        cache_item = (
            temp_file.name,  # 0 cache file path
            info_dict,  # 1 custom dict contains information
            int(time.time()),  # 2 added time (unix time)
            expires,  # 3 expires second
            _time_str_to_unix(last_modified),  # 4 last modified, unix time
        )
        temp_file.close()
        self.items_dict[key] = cache_item
        return True 
Example 25
Project: sandsifter   Author: Battelle   File: summarize.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def disassemble(disassembler, bitness, data):
    if supported[disassembler] and disassemblers[disassembler][bitness]:
        temp_file = tempfile.NamedTemporaryFile()
        temp_file.write(data)

        # disassemble
        result, errors = \
            subprocess.Popen(
                    disassemblers[disassembler][bitness][0].format(temp_file.name),
                    shell=True,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE
                    ).communicate()

        disas = cleanup(result)

        # raw
        result, errors = \
            subprocess.Popen(
                    disassemblers[disassembler][bitness][1].format(temp_file.name),
                    shell=True,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE
                    ).communicate()

        raw = cleanup(result)

        temp_file.close()

        return (disas, raw)
    else:
        return (None, None) 
Example 26
Project: spleeter   Author: deezer   File: github.py    License: MIT License 5 votes vote down vote up
def download(self, name, path):
        """ Download model denoted by the given name to disk.

        :param name: Name of the model to download.
        :param path: Path of the directory to save model into.
        """
        url = '{}/{}/{}/{}/{}.tar.gz'.format(
            self._host,
            self._repository,
            self.RELEASE_PATH,
            self._release,
            name)
        get_logger().info('Downloading model archive %s', url)
        with requests.get(url, stream=True) as response:
            response.raise_for_status()
            archive = NamedTemporaryFile(delete=False)
            try:
                with archive as stream:
                    # Note: check for chunk size parameters ?
                    for chunk in response.iter_content(chunk_size=8192):
                        if chunk:
                            stream.write(chunk)
                get_logger().info('Validating archive checksum')
                if compute_file_checksum(archive.name) != self.checksum(name):
                    raise IOError('Downloaded file is corrupted, please retry')
                get_logger().info('Extracting downloaded %s archive', name)
                with tarfile.open(name=archive.name) as tar:
                    tar.extractall(path=path)
            finally:
                os.unlink(archive.name)
        get_logger().info('%s model file(s) extracted', name) 
Example 27
Project: DOTA_models   Author: ringringyi   File: parser_eval.py    License: Apache License 2.0 5 votes vote down vote up
def RewriteContext(task_context):
  context = task_spec_pb2.TaskSpec()
  with gfile.FastGFile(task_context, 'rb') as fin:
    text_format.Merge(fin.read(), context)
  for resource in context.input:
    for part in resource.part:
      if part.file_pattern != '-':
        part.file_pattern = os.path.join(FLAGS.resource_dir, part.file_pattern)
  with tempfile.NamedTemporaryFile(delete=False) as fout:
    fout.write(str(context))
    return fout.name 
Example 28
Project: macops   Author: google   File: profiles.py    License: Apache License 2.0 5 votes vote down vote up
def Install(self, sudo_password=None):
    """Install the profile.

    Args:
      sudo_password: str, the password to use for installing the profile.

    Raises:
      ProfileInstallationError: profile failed to install.
      ProfileValidationError: profile data was not valid.
      ProfileSaveError: profile could not be saved.
    """
    self._ValidateProfile()

    with tempfile.NamedTemporaryFile(suffix='.mobileconfig',
                                     prefix='profile_') as f:
      temp_file = f.name

      self.Save(temp_file)
      command = [CMD_PROFILES, '-I', '-F', temp_file]

      try:
        (stdout, stderr, status) = gmacpyutil.RunProcess(
            command, sudo=sudo_password, sudo_password=sudo_password)
      except gmacpyutil.GmacpyutilException as e:
        raise ProfileInstallationError('Profile installation failed!\n%s' % e)

      if status:
        raise ProfileInstallationError('Profile installation failed!\n'
                                       '%s, %s, %s' % (stdout, stderr, status)) 
Example 29
Project: pinnwand   Author: supakeen   File: test_command.py    License: MIT License 5 votes vote down vote up
def test_main():
    runner = CliRunner()

    result = runner.invoke(command.main, [])
    assert result.exit_code == 0

    result = runner.invoke(command.main, ["unknown"])
    assert result.exit_code == 2

    result = runner.invoke(command.main, ["--configuration-path"])
    assert result.exit_code == 2

    result = runner.invoke(
        command.main, ["--configuration-path", "/spam/eggs/ham"]
    )
    assert result.exit_code == 2

    result = runner.invoke(
        command.main, ["--configuration-path", "/spam/eggs/ham", "reap"]
    )
    assert result.exit_code == 1

    with tempfile.NamedTemporaryFile() as f:
        result = runner.invoke(
            command.main, ["--configuration-path", f.name, "reap"]
        )
        assert result.exit_code == 0

    with tempfile.NamedTemporaryFile() as f:
        f.write(b"foo=1")
        f.flush()

        result = runner.invoke(
            command.main, ["--configuration-path", f.name, "reap"]
        )
        assert result.exit_code == 0

        import pinnwand.configuration

        assert pinnwand.configuration.foo == 1 
Example 30
Project: bioservices   Author: cokelaer   File: test_apps_fasta.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_fasta():
    fasta = FASTA()
    fasta.load_fasta(None)
    fasta.load_fasta("P43403")
    fasta.load_fasta("P43403") # already there 
    fasta.header
    fasta.gene_name
    fasta.sequence
    fasta.fasta
    fasta.identifier
    fh = tempfile.NamedTemporaryFile(delete=False)
    fasta.save_fasta(fh.name)
    fasta.read_fasta(fh.name)
    fh.delete = True
    fh.close()