Python tempfile.NamedTemporaryFile() Examples

The following are code examples for showing how to use tempfile.NamedTemporaryFile(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: fs_image   Author: facebookincubator   File: yum_dnf_from_snapshot.py    MIT License 7 votes vote down vote up
def _dummies_for_protected_paths(protected_paths) -> Mapping[str, str]:
    '''
    Some locations (e.g. /meta/ and mountpoints) should be off-limits to
    writes by RPMs.  We enforce that by bind-mounting an empty file or
    directory on top of each one of them.
    '''
    with tempfile.TemporaryDirectory() as td_name, \
            tempfile.NamedTemporaryFile() as tf:
        # NB: There may be duplicates in protected_paths, so we normalize.
        # If the duplicates include both a file and a directory, this picks
        # one arbitrarily, and if the type on disk is different, we will
        # fail at mount time.  This doesn't seem worth an explicit check.
        yield {
            os.path.normpath(p): (td_name if p.endswith('/') else tf.name)
                for p in protected_paths
        }
        # NB: The bind mount is read-only, so this is just paranoia.  If it
        # were left RW, we'd need to check its owner / permissions too.
        for expected, actual in (
            ([], os.listdir(td_name)),
            (b'', tf.read()),
        ):
            assert expected == actual, \
                f'Some RPM wrote {actual} to {protected_paths}' 
Example 2
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 7 votes vote down vote up
def test_exports(self):
        # There are no surprising symbols in the tempfile module
        dict = tempfile.__dict__

        expected = {
            "NamedTemporaryFile" : 1,
            "TemporaryFile" : 1,
            "mkstemp" : 1,
            "mkdtemp" : 1,
            "mktemp" : 1,
            "TMP_MAX" : 1,
            "gettempprefix" : 1,
            "gettempdir" : 1,
            "tempdir" : 1,
            "template" : 1,
            "SpooledTemporaryFile" : 1
        }

        unexp = []
        for key in dict:
            if key[0] != '_' and key not in expected:
                unexp.append(key)
        self.assertTrue(len(unexp) == 0,
                        "unexpected keys: %s" % unexp) 
Example 3
Project: BERT-Classification-Tutorial   Author: Socialbird-AILab   File: tokenization_test.py    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 4
Project: Gurux.DLMS.Python   Author: Gurux   File: GXManufacturerCollection.py    GNU General Public License v2.0 6 votes vote down vote up
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 5
Project: fs_image   Author: facebookincubator   File: test_subvol_utils.py    MIT License 6 votes vote down vote up
def test_mark_readonly_and_send_to_new_loopback(self, temp_subvols):
        sv = temp_subvols.create('subvol')
        sv.run_as_root([
            'dd', 'if=/dev/zero', b'of=' + sv.path('d'), 'bs=1M', 'count=200',
        ])
        sv.run_as_root(['mkdir', sv.path('0')])
        sv.run_as_root(['tee', sv.path('0/0')], input=b'0123456789')
        with tempfile.NamedTemporaryFile() as loop_path:
            # The default waste factor succeeds in 1 try, but a too-low
            # factor results in 2 tries.
            waste_too_low = 1.0001
            self.assertEqual(2, sv.mark_readonly_and_send_to_new_loopback(
                loop_path.name, waste_factor=waste_too_low,
            ))
            self.assertEqual(
                1, sv.mark_readonly_and_send_to_new_loopback(loop_path.name),
            )
            # Same 2-try run, but this time, exercise the free space check
            # instead of relying on parsing `btrfs receive` output.
            with unittest.mock.patch(
                'subvol_utils.Subvol._OUT_OF_SPACE_SUFFIX', b'cypa',
            ):
                self.assertEqual(2, sv.mark_readonly_and_send_to_new_loopback(
                    loop_path.name, waste_factor=waste_too_low,
                )) 
Example 6
Project: fs_image   Author: facebookincubator   File: test_subvol_utils.py    MIT License 6 votes vote down vote up
def test_mark_readonly_and_send_to_new_loopback_writable(
        self,
        temp_subvols
    ):
        # `test_package_image_as_btrfs_loopback_writable` actually
        # tests that the subvolume is writable, here we just test that
        # the subvol util helper method works
        sv = temp_subvols.create('subvol')
        sv.run_as_root([
            'dd', 'if=/dev/zero', b'of=' + sv.path('d'), 'bs=1M', 'count=200',
        ])
        sv.run_as_root(['mkdir', sv.path('0')])
        sv.run_as_root(['tee', sv.path('0/0')], input=b'0123456789')
        with tempfile.NamedTemporaryFile() as loop_path:
            self.assertEqual(
                1, sv.mark_readonly_and_send_to_new_loopback(
                    loop_path.name, subvol_opts=SubvolOpts(readonly=False)),
            ) 
Example 7
Project: fs_image   Author: facebookincubator   File: test_nspawn_test_in_subvol.py    MIT License 6 votes vote down vote up
def test_rewrite_cmd(self):
        bin = '/layer-test-binary'

        # Test no-op rewriting
        cmd = [bin, 'foo', '--bar', 'beep', '--baz', '-xack', '7', '9']
        with rewrite_test_cmd(cmd, next_fd=1337) as cmd_and_fd:
            self.assertEqual((cmd, None), cmd_and_fd)

        with tempfile.NamedTemporaryFile(suffix='.json') as t:
            prefix = ['--zap=3', '--ou', 'boo', '--ou=3']
            suffix = ['garr', '-abc', '-gh', '-d', '--e"f']
            with rewrite_test_cmd(
                [bin, *prefix, f'--output={t.name}', *suffix], next_fd=37,
            ) as (new_cmd, fd_to_forward):
                self.assertIsInstance(fd_to_forward, int)
                self.assertEqual([
                    '/bin/bash', '-c', ' '.join([
                        'exec',
                        bin, '--output', '>(cat >&37)', *prefix, *suffix[:-1],
                        # The last argument deliberately requires shell quoting.
                        """'--e"f'""",
                    ])
                ], new_cmd) 
Example 8
Project: leapp-repository   Author: oamg   File: unit_test.py    Apache License 2.0 6 votes vote down vote up
def test_update_kernel_config(monkeypatch):
    temp = tempfile.NamedTemporaryFile(delete=False)
    with open('tests/files/original') as f:
        data = f.readlines()
        temp.writelines(data)
    temp.close()

    library.update_kernel_config(temp.name)

    with open(temp.name) as f:
        result = f.readlines()

    with open('tests/files/expected') as f:
        expected = f.readlines()

    assert result == expected

    os.unlink(temp.name)
    assert not os.path.exists(temp.name) 
Example 9
Project: incubator-spot   Author: apache   File: processing.py    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 10
Project: incubator-spot   Author: apache   File: processing.py    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 11
Project: incubator-spot   Author: apache   File: processing.py    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 12
Project: pyblish-win   Author: pyblish   File: _osx_support.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _read_output(commandstring):
    """Output from successful command execution or None"""
    # Similar to os.popen(commandstring, "r").read(),
    # but without actually using os.popen because that
    # function is not usable during python bootstrap.
    # tempfile is also not available then.
    import contextlib
    try:
        import tempfile
        fp = tempfile.NamedTemporaryFile()
    except ImportError:
        fp = open("/tmp/_osx_support.%s"%(
            os.getpid(),), "w+b")

    with contextlib.closing(fp) as fp:
        cmd = "%s 2>/dev/null >'%s'" % (commandstring, fp.name)
        return fp.read().strip() if not os.system(cmd) else None 
Example 13
Project: pyblish-win   Author: pyblish   File: make_ssl_certs.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def make_ca():
    os.mkdir(TMP_CADIR)
    with open(os.path.join('cadir','index.txt'),'a+') as f:
        pass # empty file
    with open(os.path.join('cadir','crl.txt'),'a+') as f:
        f.write("00")
    with open(os.path.join('cadir','index.txt.attr'),'w+') as f:
        f.write('unique_subject = no')

    with tempfile.NamedTemporaryFile("w") as t:
        t.write(req_template.format(hostname='our-ca-server'))
        t.flush()
        with tempfile.NamedTemporaryFile() as f:
            args = ['req', '-new', '-days', '3650', '-extensions', 'v3_ca', '-nodes',
                    '-newkey', 'rsa:2048', '-keyout', 'pycakey.pem',
                    '-out', f.name,
                    '-subj', '/C=XY/L=Castle Anthrax/O=Python Software Foundation CA/CN=our-ca-server']
            check_call(['openssl'] + args)
            args = ['ca', '-config', t.name, '-create_serial',
                    '-out', 'pycacert.pem', '-batch', '-outdir', TMP_CADIR,
                    '-keyfile', 'pycakey.pem', '-days', '3650',
                    '-selfsign', '-extensions', 'v3_ca', '-infiles', f.name ]
            check_call(['openssl'] + args)
            args = ['ca', '-config', t.name, '-gencrl', '-out', 'revocation.crl']
            check_call(['openssl'] + args) 
Example 14
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_no_leak_fd(self):
        # Issue #21058: don't leak file descriptor when fdopen() fails
        old_close = os.close
        old_fdopen = os.fdopen
        closed = []
        def close(fd):
            closed.append(fd)
        def fdopen(*args):
            raise ValueError()
        os.close = close
        os.fdopen = fdopen
        try:
            self.assertRaises(ValueError, tempfile.NamedTemporaryFile)
            self.assertEqual(len(closed), 1)
        finally:
            os.close = old_close
            os.fdopen = old_fdopen

    # How to test the mode and bufsize parameters? 
Example 15
Project: epyc   Author: simoninireland   File: test_summaryexperiments.py    GNU General Public License v2.0 6 votes vote down vote up
def testJSON( self ):
        '''Test that we can persist repeated results in JSON format.'''

        # reset the lab we're using to use a JSON notebook
        tf = NamedTemporaryFile()
        tf.close()
        self._lab = Lab(notebook = JSONLabNotebook(tf.name, create = True))
        
        repetitions = 5
        self._lab['a'] = [ 1, 2, 3 ]
        try:
            re = RepeatedExperiment(SampleExperiment4(),
                                    repetitions)
            self._lab.runExperiment(SummaryExperiment(re,
                                                      summarised_results = [ 'total', 'nestedArray' ]))

            self.assertTrue(re.success())
            res = self._lab.results()

            # getting here is enough to exercise the persistence regime
        finally:
            try:
                os.remove(tf.name)
            except OSError:
                pass 
Example 16
Project: models   Author: kipoi   File: model.py    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 17
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: yum.py    MIT License 6 votes vote down vote up
def fetch_rpm_from_url(spec, module=None):
    # download package so that we can query it
    package_name, _ = os.path.splitext(str(spec.rsplit('/', 1)[1]))
    package_file = tempfile.NamedTemporaryFile(dir=getattr(module, 'tmpdir', None), prefix=package_name, suffix='.rpm', delete=False)
    module.add_cleanup_file(package_file.name)
    try:
        rsp, info = fetch_url(module, spec)
        if not rsp:
            module.fail_json(msg="Failure downloading %s, %s" % (spec, info['msg']))
        data = rsp.read(BUFSIZE)
        while data:
            package_file.write(data)
            data = rsp.read(BUFSIZE)
        package_file.close()
    except Exception as e:
        if module:
            module.fail_json(msg="Failure downloading %s, %s" % (spec, to_native(e)))
        else:
            raise e

    return package_file.name 
Example 18
Project: pnp   Author: HazardDede   File: test_storage_dropbox.py    MIT License 6 votes vote down vote up
def test_dropbox_push_with_file(dbx_mock):
    dbx_mock = setup_mock(dbx_mock)
    dut = Dropbox(name='pytest', api_key='secret', create_shared_link=False)
    with NamedTemporaryFile() as tmp:
        res = dut.push(tmp.name)

    assert isinstance(res, dict)
    res = Box(res)
    assert res.name == "name_42"
    assert res.id == "id_42"
    assert res.content_hash == "hash_42"
    assert res.size == 42
    assert res.path == "path/42"
    assert res.shared_link is None
    assert res.raw_link is None

    dbx_mock.assert_called_with('secret')
    dbx_mock.return_value.files_upload.assert_called() 
Example 19
Project: parasweep   Author: eviatarbach   File: test_parasweep.py    MIT License 6 votes vote down vote up
def test_multiple_templates(self):
        with tempfile.NamedTemporaryFile('r') as out, \
                tempfile.NamedTemporaryFile('w') as template1, \
                tempfile.NamedTemporaryFile('w') as template2:
            template1.write('Hello {x:.2f}, ')
            template1.seek(0)

            template2.write('hello again {y}\n')
            template2.seek(0)

            run_sweep(' '.join([cat, '{sim_id}_1.txt', '{sim_id}_2.txt',
                                out.name]),
                      ['{sim_id}_1.txt', '{sim_id}_2.txt'],
                      templates=[template1.name, template2.name],
                      sweep=CartesianSweep({'x': [1/3, 2/3, 3/3], 'y': [4]}),
                      verbose=False, cleanup=True, save_mapping=False)

            self.assertEqual(set(out.read().splitlines()),
                             set(('Hello 0.33, hello again 4\nHello 0.67, '
                                  'hello again 4\nHello 1.00, '
                                  'hello again 4\n').splitlines())) 
Example 20
Project: parasweep   Author: eviatarbach   File: test_parasweep.py    MIT License 6 votes vote down vote up
def test_mako(self):
        from parasweep.templates import MakoTemplate

        with tempfile.NamedTemporaryFile('r') as out, \
                tempfile.NamedTemporaryFile('w') as template:
            template.write('Hello ${x*10}\n')
            template.seek(0)

            run_sweep(' '.join([cat, '{sim_id}.txt', out.name]),
                      ['{sim_id}.txt'], templates=[template.name],
                      sweep=CartesianSweep({'x': [1, 2, 3]}),
                      template_engine=MakoTemplate(), verbose=False,
                      cleanup=True, save_mapping=False)

            self.assertEqual(set(out.read().splitlines()),
                             set(('Hello 10\nHello 20\n'
                                  'Hello 30\n').splitlines())) 
Example 21
Project: parasweep   Author: eviatarbach   File: test_parasweep.py    MIT License 6 votes vote down vote up
def test_wait(self):
        with tempfile.NamedTemporaryFile('r') as out, \
                tempfile.NamedTemporaryFile('w') as template:
            template.write('Hello {x}\n')
            template.seek(0)

            run_sweep(' '.join([sleepcat, str(3), '{sim_id}.txt', out.name]),
                      ['{sim_id}.txt'], templates=[template.name],
                      sweep=CartesianSweep({'x': [1, 1, 1]}), wait=True,
                      verbose=False, save_mapping=False)

            self.assertEqual(out.read(), 'Hello 1\nHello 1\nHello 1\n')

        os.remove('0.txt')
        os.remove('1.txt')
        os.remove('2.txt') 
Example 22
Project: parasweep   Author: eviatarbach   File: test_parasweep.py    MIT License 6 votes vote down vote up
def test_filtered_cartesian(self):
        with tempfile.NamedTemporaryFile('r') as out, \
                tempfile.NamedTemporaryFile('w') as template:
            template.write('Hello {x}, {y}\n')
            template.seek(0)

            sweep = FilteredCartesianSweep({'x': [1, 2, 3], 'y': [1, 2, 3]},
                                           filter_func=lambda x, y: x > y)
            mapping = run_sweep(' '.join([cat, '{sim_id}.txt', out.name]),
                                ['{sim_id}.txt'], templates=[template.name],
                                sweep=sweep, verbose=False, cleanup=True,
                                sweep_id='test2', save_mapping=True)

            self.assertEqual(set(out.read().splitlines()),
                             set(('Hello 2, 1\nHello 3, 1\n'
                                  'Hello 3, 2').splitlines()))

            self.assertTrue(os.path.isfile('sim_ids_test2.json'))

            with open('sim_ids_test2.json', 'r') as json_file:
                self.assertEqual(mapping, json.load(json_file))

            os.remove('sim_ids_test2.json') 
Example 23
Project: parasweep   Author: eviatarbach   File: test_parasweep.py    MIT License 6 votes vote down vote up
def test_overwrite(self):
        with tempfile.NamedTemporaryFile('w') as template:
            template.write('Hello {x}\n')
            template.seek(0)

            run_sweep(' '.join([cat, ' {sim_id}.txt']),
                      ['{sim_id}.txt'], templates=[template.name],
                      sweep=CartesianSweep({'x': [1]}),
                      verbose=False, cleanup=False, save_mapping=False)

            with self.assertRaises(FileExistsError) as context:
                run_sweep(' '.join([cat, ' {sim_id}.txt']),
                          ['{sim_id}.txt'], templates=[template.name],
                          sweep=CartesianSweep({'x': [1]}),
                          verbose=False, cleanup=False, save_mapping=False,
                          overwrite=False)

            self.assertEqual('0.txt exists, set `overwrite` to True to '
                             'overwrite.', str(context.exception))

            os.remove('0.txt') 
Example 24
Project: advent-of-code-2018   Author: badouralix   File: go.py    MIT License 6 votes vote down vote up
def __init__(self, file):
        SubmissionWrapper.__init__(self)
        relpath = os.path.join(".", file)
        abspath = os.path.realpath(file)
        gopath = os.path.realpath(check_output(["go", "env", "GOPATH"]).decode().strip())
        if not abspath.startswith(gopath):
            dep_output = check_output(["go", "get", "-d", relpath]).decode()
            if dep_output:
                raise DependenciesError(dep_output)
        tmp = tempfile.NamedTemporaryFile(prefix="aoc")
        tmp.close()
        compile_output = check_output(["go", "build", "-o", tmp.name, file]).decode()
        if compile_output:
            raise CompilationError(compile_output)
        os.chmod(tmp.name, os.stat(tmp.name).st_mode | stat.S_IEXEC)
        self.executable = tmp.name 
Example 25
Project: fs_image   Author: facebookincubator   File: update_package_db.py    MIT License 5 votes vote down vote up
def _populate_temp_file_and_rename(dest_path: Path, *, mode='w'):
    with tempfile.NamedTemporaryFile(
        mode=mode, dir=dest_path.dirname(), delete=False,
    ) as tf:
        try:
            yield tf
        except BaseException:
            os.unlink(tf.name)
            raise
        # NB: This will fail to replace a directory, preventing us from
        # transparently converting JSON to BZL databases without using
        # `--out-db`.  This is fine since the DB paths should look different
        # anyhow (`dirname` vs `filename.bzl`).
        os.rename(tf.name, dest_path) 
Example 26
Project: fs_image   Author: facebookincubator   File: repo_snapshot.py    MIT License 5 votes vote down vote up
def add_sqlite_to_storage(
        cls, storage: Storage, dest_dir: Path,
    ) -> Iterable[sqlite3.Connection]:
        with tempfile.NamedTemporaryFile() as db_tf:
            with sqlite3.connect(db_tf.name) as db:
                RepoSnapshot._create_sqlite_tables(db)
                yield db
            db.close()
            with storage.writer() as db_out:
                for chunk in read_chunks(db_tf, cls._STORAGE_CHUNK_SIZE):
                    db_out.write(chunk)
                with create_ro(dest_dir / cls._STORAGE_ID_FILE, 'w') as sidf:
                    sidf.write(db_out.commit() + '\n') 
Example 27
Project: fs_image   Author: facebookincubator   File: yum_dnf_from_snapshot.py    MIT License 5 votes vote down vote up
def _prepare_isolated_yum_dnf_conf(
    yum_dnf: YumDnf, inp: TextIO, out: tempfile.NamedTemporaryFile,
    install_root: Path, host: str, port: int, versionlock_dir: Path,
):
    '''
    Reads a "{yum,dnf}.conf" from `inp`, and writes a modified version to
    `out`, installing into `install_root`, and getting packages + GPG keys
    from a snapshot `repo-server` at `http://host:port`.

    This is a context manager because in a prior iteration, the resulting
    isolated "{yum,dnf}.conf" was only valid for as long as some associated
    temporary directories continued to exist.  I'm keeping this contract in
    case we need to do this again in the future.
    '''
    server_url = urlparse(f'http://{host}:{port}')
    yc = YumDnfConfParser(yum_dnf, inp)
    yc.isolate().isolate_repos(
        repo._replace(
            base_url=urlunparse(server_url._replace(path=repo.name)),
            gpg_key_urls=[
                urlunparse(server_url._replace(path=os.path.join(
                    repo.name, os.path.basename(urlparse(key_url).path),
                ))) for key_url in repo.gpg_key_urls
            ],
        ) for repo in yc.gen_repos()
    ).isolate_main(
        install_root=install_root.decode(),
        config_path=out.name,
        versionlock_dir=versionlock_dir.decode(),
    ).write(out)
    out.flush()
    yield  # The config we wrote is valid only inside the context. 
Example 28
Project: fs_image   Author: facebookincubator   File: parse_repodata.py    MIT License 5 votes vote down vote up
def __enter__(self):
        self._tmp_db_ctx = tempfile.NamedTemporaryFile()
        self._tmp_db = self._tmp_db_ctx.__enter__()
        return self 
Example 29
Project: fs_image   Author: facebookincubator   File: test_yum_dnf_from_snapshot.py    MIT License 5 votes vote down vote up
def _install(self, *, protected_paths, version_lock=None):
        install_root = Path(tempfile.mkdtemp())
        try:
            # IMAGE_ROOT/meta/ is always required since it's always protected
            for p in set(protected_paths) | {'meta/'}:
                if p.endswith('/'):
                    os.makedirs(install_root / p)
                else:
                    os.makedirs(os.path.dirname(install_root / p))
                    with open(install_root / p, 'wb'):
                        pass
            snapshot_dir = Path(load_location('rpm', 'repo-snapshot'))
            # Note: this can't use `_yum_using_build_appliance` because that
            # would lose coverage info on `yum_dnf_from_snapshot.py`.  On
            # the other hand, running this test against the host is fragile
            # since it depends on the system packages available on CI
            # containers.  For this reason, this entire test is an
            # `image.python_unittest` that runs in a build appliance.
            with tempfile.NamedTemporaryFile(mode='w') as tf:
                if version_lock:
                    tf.write('\n'.join(version_lock) + '\n')
                tf.flush()
                yum_dnf_from_snapshot(
                    yum_dnf=self._YUM_DNF,
                    repo_server_bin=Path(load_location('rpm', 'repo-server')),
                    storage_cfg=json.dumps({
                        'key': 'test',
                        'kind': 'filesystem',
                        'base_dir': (snapshot_dir / 'storage').decode(),
                    }),
                    snapshot_dir=snapshot_dir,
                    install_root=Path(install_root),
                    protected_paths=protected_paths,
                    versionlock_list=tf.name,
                    yum_dnf_args=_INSTALL_ARGS,
                )
            yield install_root
        finally:
            assert install_root != '/'
            # Courtesy of `yum`, the `install_root` is now owned by root.
            subprocess.run(['sudo', 'rm', '-rf', install_root], check=True) 
Example 30
Project: fs_image   Author: facebookincubator   File: test_subvol_utils.py    MIT License 5 votes vote down vote up
def test_write_to_tarball(self, temp_subvols):
        # create a subvol from a demo sendstream, tar it, untar into a new
        # subvol, then compare the two
        demo_sv_name = 'demo_sv'
        demo_sv = temp_subvols.caller_will_create(demo_sv_name)
        with open(Path(__file__).dirname() / 'create_ops.sendstream') as f, \
                demo_sv.receive(f):
            pass

        unpacked_sv = temp_subvols.create('subvol')
        with tempfile.NamedTemporaryFile() as tar_file:
            with demo_sv.write_to_tarball(tar_file):
                pass

            demo_sv.run_as_root([
                'tar',
                'xzf',
                tar_file.name,
                '--xattrs',
                '-C',
                unpacked_sv.path(),
            ])

        demo_render = render_demo_subvols(create_ops=demo_sv_name)
        # Tar does not preserve the original's cloned extents of
        # zeros
        demo_render[1]['56KB_nuls'] = ['(File d57344)']
        demo_render[1]['56KB_nuls_clone'] = ['(File d57344)']
        # Tar des not preserve unix domain sockets, as these are usable only for
        # the lifetime of the associated process and should therefore be safe to
        # ignore.
        demo_render[1].pop('unix_sock')

        self.assertEqual(
            demo_render,
            render_sendstream(unpacked_sv.mark_readonly_and_get_sendstream()),
        ) 
Example 31
Project: fs_image   Author: facebookincubator   File: test_install_file.py    MIT License 5 votes vote down vote up
def test_install_file(self):
        with tempfile.NamedTemporaryFile() as tf:
            os.chmod(tf.name, stat.S_IXUSR)
            exe_item = _install_file_item(
                from_target='t', source={'source': tf.name}, dest='d/c',
            )
        ep = _InstallablePath(Path(tf.name), ProvidesFile(path='d/c'), 'a+rx')
        self.assertEqual((ep,), exe_item.paths)
        self.assertEqual(tf.name.encode(), exe_item.source)
        self._check_item(exe_item, {ep.provides}, {require_directory('d')})

        # Checks `image.source(path=...)`
        with temp_dir() as td:
            os.mkdir(td / 'b')
            open(td / 'b/q', 'w').close()
            data_item = _install_file_item(
                from_target='t',
                source={'source': td, 'path': '/b/q'},
                dest='d',
            )
        dp = _InstallablePath(td / 'b/q', ProvidesFile(path='d'), 'a+r')
        self.assertEqual((dp,), data_item.paths)
        self.assertEqual(td / 'b/q', data_item.source)
        self._check_item(data_item, {dp.provides}, {require_directory('/')})

        # NB: We don't need to get coverage for this check on ALL the items
        # because the presence of the ProvidesDoNotAccess items it the real
        # safeguard -- e.g. that's what prevents TarballItem from writing
        # to /meta/ or other protected paths.
        with self.assertRaisesRegex(AssertionError, 'cannot start with meta/'):
            _install_file_item(
                from_target='t', source={'source': 'a/b/c'}, dest='/meta/foo',
            ) 
Example 32
Project: fs_image   Author: facebookincubator   File: test_install_file.py    MIT License 5 votes vote down vote up
def test_install_file_command(self):
        with TempSubvolumes(sys.argv[0]) as temp_subvolumes, \
                tempfile.NamedTemporaryFile() as empty_tf:
            subvol = temp_subvolumes.create('tar-sv')
            subvol.run_as_root(['mkdir', subvol.path('d')])

            _install_file_item(
                from_target='t', source={'source': empty_tf.name},
                dest='/d/empty',
            ).build(subvol, DUMMY_LAYER_OPTS)
            self.assertEqual(
                ['(Dir)', {'d': ['(Dir)', {'empty': ['(File m444)']}]}],
                render_subvol(subvol),
            )

            # Fail to write to a nonexistent dir
            with self.assertRaises(subprocess.CalledProcessError):
                _install_file_item(
                    from_target='t', source={'source': empty_tf.name},
                    dest='/no_dir/empty',
                ).build(subvol, DUMMY_LAYER_OPTS)

            # Running a second copy to the same destination. This just
            # overwrites the previous file, because we have a build-time
            # check for this, and a run-time check would add overhead.
            _install_file_item(
                from_target='t', source={'source': empty_tf.name},
                dest='/d/empty',
                # A non-default mode & owner shows that the file was
                # overwritten, and also exercises HasStatOptions.
                mode='u+rw', user_group='12:34',
            ).build(subvol, DUMMY_LAYER_OPTS)
            self.assertEqual(
                ['(Dir)', {'d': ['(Dir)', {'empty': ['(File m600 o12:34)']}]}],
                render_subvol(subvol),
            ) 
Example 33
Project: fs_image   Author: facebookincubator   File: test_tarball.py    MIT License 5 votes vote down vote up
def test_tarball_generator(self):
        with temp_filesystem() as fs_path, tempfile.NamedTemporaryFile() as t, \
                ExitStack() as exit_stack:
            with tarfile.TarFile(t.name, 'w') as tar_obj:
                tar_obj.add(fs_path, filter=_tarinfo_strip_dir_prefix(fs_path))
            self._check_item(
                image_source_item(
                    TarballItem,
                    exit_stack=exit_stack,
                    layer_opts=DUMMY_LAYER_OPTS,
                )(
                    from_target='t',
                    into_dir='y',
                    source={
                        'generator': '/bin/bash',
                        'generator_args': [
                            '-c',
                            'cp "$1" "$2"; basename "$1"',
                            'test_tarball_generator',  # $0
                            t.name,  # $1, making $2 the output directory
                        ],
                        'content_hash':
                            'sha256:' + _hash_path(t.name, 'sha256'),
                    },
                    force_root_ownership=False,
                ),
                temp_filesystem_provides('y'),
                {require_directory('y')},
            ) 
Example 34
Project: jumpserver-python-sdk   Author: jumpserver   File: test_auth.py    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 35
Project: pyblish-win   Author: pyblish   File: make_ssl_certs.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def make_cert_key(hostname, sign=False):
    print("creating cert for " + hostname)
    tempnames = []
    for i in range(3):
        with tempfile.NamedTemporaryFile(delete=False) as f:
            tempnames.append(f.name)
    req_file, cert_file, key_file = tempnames
    try:
        with open(req_file, 'w') as f:
            f.write(req_template.format(hostname=hostname))
        args = ['req', '-new', '-days', '3650', '-nodes',
                '-newkey', 'rsa:1024', '-keyout', key_file,
                '-config', req_file]
        if sign:
            with tempfile.NamedTemporaryFile(delete=False) as f:
                tempnames.append(f.name)
                reqfile = f.name
            args += ['-out', reqfile ]

        else:
            args += ['-x509', '-out', cert_file ]
        check_call(['openssl'] + args)

        if sign:
            args = ['ca', '-config', req_file, '-out', cert_file, '-outdir', 'cadir',
                    '-policy', 'policy_anything', '-batch', '-infiles', reqfile ]
            check_call(['openssl'] + args)


        with open(cert_file, 'r') as f:
            cert = f.read()
        with open(key_file, 'r') as f:
            key = f.read()
        return cert, key
    finally:
        for name in tempnames:
            os.remove(name) 
Example 36
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_directory_writable(self):
        # gettempdir returns a directory writable by the user

        # sneaky: just instantiate a NamedTemporaryFile, which
        # defaults to writing into the directory returned by
        # gettempdir.
        try:
            file = tempfile.NamedTemporaryFile()
            file.write("blat")
            file.close()
        except:
            self.failOnException("create file in %s" % tempfile.gettempdir()) 
Example 37
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def do_create(self, dir=None, pre="", suf="", delete=True):
        if dir is None:
            dir = tempfile.gettempdir()
        try:
            file = tempfile.NamedTemporaryFile(dir=dir, prefix=pre, suffix=suf,
                                               delete=delete)
        except:
            self.failOnException("NamedTemporaryFile")

        self.nameCheck(file.name, dir, pre, suf)
        return file 
Example 38
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_basic(self):
        # NamedTemporaryFile can create files
        self.do_create()
        self.do_create(pre="a")
        self.do_create(suf="b")
        self.do_create(pre="a", suf="b")
        self.do_create(pre="aa", suf=".txt") 
Example 39
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_creates_named(self):
        # NamedTemporaryFile creates files with names
        f = tempfile.NamedTemporaryFile()
        self.assertTrue(os.path.exists(f.name),
                        "NamedTemporaryFile %s does not exist" % f.name) 
Example 40
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_del_on_close(self):
        # A NamedTemporaryFile is deleted when closed
        dir = tempfile.mkdtemp()
        try:
            f = tempfile.NamedTemporaryFile(dir=dir)
            f.write('blat')
            f.close()
            self.assertFalse(os.path.exists(f.name),
                        "NamedTemporaryFile %s exists after close" % f.name)
        finally:
            os.rmdir(dir) 
Example 41
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_dis_del_on_close(self):
        # Tests that delete-on-close can be disabled
        dir = tempfile.mkdtemp()
        tmp = None
        try:
            f = tempfile.NamedTemporaryFile(dir=dir, delete=False)
            tmp = f.name
            f.write('blat')
            f.close()
            self.assertTrue(os.path.exists(f.name),
                        "NamedTemporaryFile %s missing after close" % f.name)
        finally:
            if tmp is not None:
                os.unlink(tmp)
            os.rmdir(dir) 
Example 42
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_context_manager(self):
        # A NamedTemporaryFile can be used as a context manager
        with tempfile.NamedTemporaryFile() as f:
            self.assertTrue(os.path.exists(f.name))
        self.assertFalse(os.path.exists(f.name))
        def use_closed():
            with f:
                pass
        self.assertRaises(ValueError, use_closed) 
Example 43
Project: pyblish-win   Author: pyblish   File: test_hotshot.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_logreader_eof_error(self):
        emptyfile = tempfile.NamedTemporaryFile()
        try:
            self.assertRaises((IOError, EOFError), _hotshot.logreader,
                              emptyfile.name)
        finally:
            emptyfile.close()
        gc.collect() 
Example 44
Project: arm_now   Author: nongiach   File: filesystem.py    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 45
Project: sanctuary   Author: bzamecnik   File: mnist.py    MIT License 5 votes vote down vote up
def save_model(model):
    """
    Saved the model as an Sacred artifact.
    """
    with NamedTemporaryFile(suffix='_model.h5') as model_file:
        model.save(model_file.name)
        ex.add_artifact(model_file.name) 
Example 46
Project: prediction-constrained-topic-models   Author: dtak   File: make_launcher_script.py    MIT License 5 votes vote down vote up
def make_launcher_script_file():
    """ Create temporary file for launching job on grid system

    Post Condition
    --------------
    Temporary file written to /tmp/ or similar via tempfile module

    Returns
    -------
    fpath : string
        Valid path to temporary file
    """

    ext_str = detect_template_ext_for_current_system()
    template_fpath = os.path.join(
        os.path.expandvars("$PC_REPO_DIR/scripts/launcher_tools/"),
        "template.%s" % ext_str)
    with open(template_fpath, "r") as f:
        template_lines = f.readlines()

    launcher_f = tempfile.NamedTemporaryFile(
        mode="w",
        prefix="launcher_for_%s_" % os.environ['USER'],
        suffix="." + ext_str,
        delete=False)
    for line in template_lines:
        for key in DEFAULT_KEYS:
            line = line.replace("$" + key, os.environ[key])
        line = line.replace(
            '$XHOST_BASH_EXE',
            os.path.abspath(os.environ['XHOST_BASH_EXE']))
        launcher_f.write(line)
    launcher_f.close()
    return os.path.abspath(launcher_f.name) 
Example 47
Project: rpm2swidtag   Author: swidtags   File: repodata.py    Apache License 2.0 5 votes vote down vote up
def save(self):
		outpath = NamedTemporaryFile(dir=path.dirname(self.path), prefix="." + path.basename(self.path), delete=False)
		self.xml.write(outpath.file, xml_declaration=True, encoding="utf-8", pretty_print=True)
		orig_umask = umask(0)
		umask(orig_umask)
		chmod(outpath.name, 0o666 & ~orig_umask)
		rename(outpath.name, self.path) 
Example 48
Project: zmirror   Author: aploium   File: cache_system.py    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 49
Project: sandsifter   Author: Battelle   File: summarize.py    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 50
Project: epyc   Author: simoninireland   File: test_jsonnotebooks.py    GNU General Public License v2.0 5 votes vote down vote up
def setUp( self ):
        '''Set up with a temporary file.'''
        tf = NamedTemporaryFile()
        tf.close()
        self._fn = tf.name 
Example 51
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: panos_import.py    MIT License 5 votes vote down vote up
def download_file(url):
    r = requests.get(url, stream=True)
    fo = tempfile.NamedTemporaryFile(prefix='ai', delete=False)
    shutil.copyfileobj(r.raw, fo)
    fo.close()

    return fo.name 
Example 52
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: known_hosts.py    MIT License 5 votes vote down vote up
def sanity_check(module, host, key, sshkeygen):
    '''Check supplied key is sensible

    host and key are parameters provided by the user; If the host
    provided is inconsistent with the key supplied, then this function
    quits, providing an error to the user.
    sshkeygen is the path to ssh-keygen, found earlier with get_bin_path
    '''
    # If no key supplied, we're doing a removal, and have nothing to check here.
    if key is None:
        return
    # Rather than parsing the key ourselves, get ssh-keygen to do it
    # (this is essential for hashed keys, but otherwise useful, as the
    # key question is whether ssh-keygen thinks the key matches the host).

    # The approach is to write the key to a temporary file,
    # and then attempt to look up the specified host in that file.
    try:
        outf = tempfile.NamedTemporaryFile(mode='w+')
        outf.write(key)
        outf.flush()
    except IOError as e:
        module.fail_json(msg="Failed to write to temporary file %s: %s" %
                             (outf.name, to_native(e)))

    sshkeygen_command = [sshkeygen, '-F', host, '-f', outf.name]
    rc, stdout, stderr = module.run_command(sshkeygen_command, check_rc=True)
    try:
        outf.close()
    except:
        pass

    if stdout == '':  # host not found
        module.fail_json(msg="Host parameter does not match hashed host field in supplied key") 
Example 53
Project: spleeter   Author: deezer   File: github.py    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)
            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)
            tar = tarfile.open(name=archive.name)
            tar.extractall(path=path)
            tar.close()
        get_logger().info('%s model file(s) extracted', name) 
Example 54
Project: pnp   Author: HazardDede   File: test_mail_gmail.py    MIT License 5 votes vote down vote up
def token_file():
    import pickle
    with NamedTemporaryFile('wb', delete=False) as fp:
        pickle.dump(CredentialDummy(), fp)

    yield fp.name
    if os.path.isfile(fp.name):
        os.remove(fp.name) 
Example 55
Project: pnp   Author: HazardDede   File: test_mail_gmail.py    MIT License 5 votes vote down vote up
def expired_token_file():
    import pickle
    with NamedTemporaryFile('wb', delete=False) as fp:
        pickle.dump(CredentialDummy(valid=False, expired=True), fp)

    yield fp.name
    if os.path.isfile(fp.name):
        os.remove(fp.name) 
Example 56
Project: pnp   Author: HazardDede   File: test_mail_gmail.py    MIT License 5 votes vote down vote up
def attachment():
    with NamedTemporaryFile('wb') as fp:
        yield fp.name 
Example 57
Project: pnp   Author: HazardDede   File: test_storage_dropbox.py    MIT License 5 votes vote down vote up
def test_dropbox_push_with_file_and_shared_link(dbx_mock):
    dbx_mock = setup_mock(dbx_mock)
    dut = Dropbox(name='pytest', api_key='secret', target_file_name="42", create_shared_link=True)
    with NamedTemporaryFile() as tmp:
        res = dut.push(tmp.name)

    assert isinstance(res, dict)
    res = Box(res)
    assert res.shared_link == 'http://42:42/42?dl=1'
    assert res.raw_link == 'http://42:42/42?raw=1'

    dbx_mock.assert_called_with('secret')
    dbx_mock.return_value.files_upload.assert_called()
    dbx_mock.return_value.sharing_create_shared_link.assert_called_with('/42') 
Example 58
Project: pnp   Author: HazardDede   File: test_fitbit.py    MIT License 5 votes vote down vote up
def token_file():
    tokens = {
        'client_id': '123456',
        'client_secret': 'abcdefg',
        'access_token': 'zyx',
        'refresh_token': 'refresh_token',
        'expires_at': 123.567
    }
    with tempfile.NamedTemporaryFile('w') as tf:
        yaml.dump(tokens, tf)
        tf.flush()
        os.fsync(tf)
        yield tf.name 
Example 59
Project: DJFeet   Author: libre-man   File: transitioners.py    MIT License 5 votes vote down vote up
def write_sample(self, sample):
        """Write the given sample to the output stream.

        Write a given sample to the output stream. This is defined by the set
        output folder when intializing the InfJukeboxTransitioner instance. As
        a side-effect, a WAV file will be written in addition to the MP3 file.

        :param numpy.array sample: The created part / sample to write.
        :returns: Nothing of value
        :rtype: None
        """
        l.info("Writing part %d to %s.", self.part_no, self.output_folder)
        with tempfile.NamedTemporaryFile() as wavfile:
            mp3file = os.path.join(self.output_folder,
                                   "part{}.mp3".format(self.part_no))
            l.debug("Using %s as wavfile and %s as mp3 file", wavfile.name,
                    mp3file)

            librosa.output.write_wav(
                wavfile.name, sample, sr=22050, norm=False)
            wavfile.flush()
            pydub.AudioSegment.from_wav(wavfile.name).export(
                mp3file, format='mp3')

        l.debug("Wrote mp3 file.")
        self.part_no += 1 
Example 60
Project: parasweep   Author: eviatarbach   File: test_parasweep.py    MIT License 5 votes vote down vote up
def test_basic(self):
        with tempfile.NamedTemporaryFile('r') as out, \
                tempfile.NamedTemporaryFile('w') as template:
            template.write('Hello {x:.2f}\n')
            template.seek(0)

            run_sweep(' '.join([cat, ' {sim_id}.txt', out.name]),
                      ['{sim_id}.txt'], templates=[template.name],
                      sweep=CartesianSweep({'x': [1/3, 2/3, 3/3]}),
                      verbose=False, cleanup=True, save_mapping=False)

            # Check unordered, because we can't guarantee order
            self.assertEqual(set(out.read().splitlines()),
                             set(('Hello 0.33\nHello 0.67\n'
                                  'Hello 1.00\n').splitlines())) 
Example 61
Project: parasweep   Author: eviatarbach   File: test_parasweep.py    MIT License 5 votes vote down vote up
def test_single(self):
        with tempfile.NamedTemporaryFile('r') as out, \
                tempfile.NamedTemporaryFile('w') as template:
            template.write('Hello {x}, {y}\n')
            template.seek(0)

            run_sweep(' '.join([cat, '{sim_id}.txt', out.name]),
                      ['{sim_id}.txt'], templates=[template.name],
                      sweep=CartesianSweep({'x': [1], 'y': [2]}),
                      verbose=False, cleanup=True, save_mapping=False)

            self.assertEqual(out.read(), 'Hello 1, 2\n') 
Example 62
Project: parasweep   Author: eviatarbach   File: test_parasweep.py    MIT License 5 votes vote down vote up
def test_drmaa(self):
        from parasweep.dispatchers import DRMAADispatcher
        from drmaa import JobTemplate

        with tempfile.NamedTemporaryFile('r') as out, \
                tempfile.NamedTemporaryFile('w') as template:
            template.write('Hello {x}\n')
            template.seek(0)

            run_sweep(' '.join([cat, '{sim_id}.txt', out.name]),
                      ['{sim_id}.txt'], templates=[template.name],
                      sweep=CartesianSweep({'x': [1, 1, 1]}), wait=True,
                      dispatcher=DRMAADispatcher(), verbose=False,
                      cleanup=True, save_mapping=False)

            self.assertEqual(out.read(), 'Hello 1\nHello 1\nHello 1\n')

            jt = JobTemplate(errorPath=':err_test.txt')
            run_sweep(' '.join([err, '{sim_id}']),
                      ['{sim_id}.txt'], templates=[template.name],
                      sweep=CartesianSweep({'x': [1]}), wait=True,
                      dispatcher=DRMAADispatcher(jt), verbose=False,
                      cleanup=True, save_mapping=False)

            with open('err_test.txt', 'r') as error_file:
                self.assertEqual('Error, simulation ID: 0\n',
                                 error_file.read())

            os.remove('err_test.txt') 
Example 63
Project: parasweep   Author: eviatarbach   File: test_parasweep.py    MIT License 5 votes vote down vote up
def test_errors(self):
        with tempfile.NamedTemporaryFile('w') as template:
            template.write('Hello {x} {z}\n')
            template.seek(0)

            with self.assertRaises(TypeError) as context:
                run_sweep(' '.join([cat, '{sim_id}.txt']), '{sim_id}.txt',
                          templates=[template.name],
                          sweep=CartesianSweep({'x': [1, 2, 3]}),
                          verbose=False, cleanup=True, save_mapping=False)

            self.assertEqual('`configs` and `templates` must be a list.',
                             str(context.exception)) 
Example 64
Project: parasweep   Author: eviatarbach   File: test_parasweep.py    MIT License 5 votes vote down vote up
def test_param_mapping(self):
        import xarray

        with tempfile.NamedTemporaryFile('w') as template:
            template.write('Hello {x} {y} {z}\n')
            template.seek(0)

            param_array = run_sweep(' '.join([cat, '{sim_id}.txt']),
                                    ['{sim_id}.txt'],
                                    templates=[template.name],
                                    sweep=CartesianSweep({'x': [1, 2],
                                                          'y': [3, 4, 5],
                                                          'z': [6, 7, 8, 9]}),
                                    verbose=False, cleanup=True,
                                    sweep_id='test', save_mapping=True)

            self.assertEqual(param_array.coords.dims, ('x', 'y', 'z'))
            self.assertEqual(param_array.shape, (2, 3, 4))

            self.assertTrue(os.path.isfile('sim_ids_test.nc'))

            all_equal = (param_array
                         == xarray.open_dataarray('sim_ids_test.nc')).all()
            self.assertTrue(all_equal)

            os.remove('sim_ids_test.nc') 
Example 65
Project: parasweep   Author: eviatarbach   File: test_parasweep.py    MIT License 5 votes vote down vote up
def test_parameter_sets(self):
        with tempfile.NamedTemporaryFile('r') as out, \
                tempfile.NamedTemporaryFile('w') as template:
            template.write('Hello {x}, {y}, {z}\n')
            template.seek(0)

            mapping = run_sweep(' '.join([cat, '{sim_id}.txt', out.name]),
                                ['{sim_id}.txt'],
                                templates=[template.name],
                                sweep=SetSweep([{'x': 2, 'y': 8, 'z': 5},
                                                {'x': 1, 'y': -4, 'z': 9}]),
                                verbose=False, cleanup=True,
                                sweep_id='test1', save_mapping=True)

            self.assertEqual(set(out.read().splitlines()),
                             set(('Hello 2, 8, 5\n'
                                  'Hello 1, -4, 9\n').splitlines()))
            self.assertEqual(mapping, {'0': {'x': 2, 'y': 8, 'z': 5},
                                       '1': {'x': 1, 'y': -4, 'z': 9}})

            self.assertTrue(os.path.isfile('sim_ids_test1.json'))

            with open('sim_ids_test1.json', 'r') as json_file:
                self.assertEqual(mapping, json.load(json_file))

            os.remove('sim_ids_test1.json') 
Example 66
Project: parasweep   Author: eviatarbach   File: test_parasweep.py    MIT License 5 votes vote down vote up
def test_verbose(self):
        with tempfile.NamedTemporaryFile('w') as template:
            template.write('Hello {x}\n')
            template.seek(0)

            temp_stdout = io.StringIO()
            with contextlib.redirect_stdout(temp_stdout):
                run_sweep(' '.join([cat, ' {sim_id}.txt']),
                          ['{sim_id}.txt'], templates=[template.name],
                          sweep=CartesianSweep({'x': [1]}),
                          verbose=True, cleanup=True, save_mapping=False)

            self.assertEqual(temp_stdout.getvalue(),
                             'Running simulation 0 with parameters:\nx: 1\n') 
Example 67
Project: parasweep   Author: eviatarbach   File: test_parasweep.py    MIT License 5 votes vote down vote up
def test_max_procs(self):
        with tempfile.NamedTemporaryFile('w') as template:
            template.write('Hello {x}\n')
            template.seek(0)

            dispatcher = SubprocessDispatcher(max_procs=1)

            start_time = time.time()
            run_sweep(' '.join([sleep, str(2)]), ['{sim_id}.txt'],
                      templates=[template.name],
                      sweep=CartesianSweep({'x': [1, 2]}),
                      dispatcher=dispatcher, verbose=False, save_mapping=False,
                      cleanup=True)

            self.assertGreater(time.time() - start_time, 4) 
Example 68
Project: parasweep   Author: eviatarbach   File: test_parasweep.py    MIT License 5 votes vote down vote up
def test_errors(self):
        with tempfile.NamedTemporaryFile('w') as template:
            template.write('Hello {x} {z}\n')
            template.seek(0)

            with self.assertRaises(NameError) as context:
                run_sweep(' '.join([cat, '{sim_id}.txt']), ['{sim_id}.txt'],
                          templates=[template.name],
                          sweep=CartesianSweep({'x': [1, 2, 3]}),
                          verbose=False, cleanup=True, save_mapping=False)

            self.assertEqual("The name 'z' is used in the template but not "
                             "provided.", str(context.exception))

            template.truncate(0)
            template.write('Hello {x}\n')
            template.seek(0)

            with self.assertRaises(NameError) as context:
                run_sweep(' '.join([cat, '{sim_id}.txt']), ['{sim_id}.txt'],
                          templates=[template.name],
                          sweep=CartesianSweep({'x': [1, 2, 3], 'y': [4]}),
                          verbose=False, cleanup=True, save_mapping=False)

            self.assertEqual("The names {'y'} are not used in the "
                             "template.", str(context.exception)) 
Example 69
Project: parasweep   Author: eviatarbach   File: test_parasweep.py    MIT License 5 votes vote down vote up
def test_errors(self):
        from parasweep.templates import MakoTemplate

        with tempfile.NamedTemporaryFile('w') as template:
            template.write('Hello ${x*10} ${z}\n')
            template.seek(0)

            with self.assertRaises(NameError) as context:
                run_sweep(' '.join([cat, '{sim_id}.txt']), ['{sim_id}.txt'],
                          templates=[template.name],
                          sweep=CartesianSweep({'x': [1, 2, 3]}),
                          template_engine=MakoTemplate(), verbose=False,
                          cleanup=True, save_mapping=False)

            self.assertEqual("'z' is not defined", str(context.exception))

            template.truncate(0)
            template.write('Hello ${x*10}\n')
            template.seek(0)

            with self.assertRaises(NameError) as context:
                run_sweep(' '.join([cat, '{sim_id}.txt']), ['{sim_id}.txt'],
                          templates=[template.name],
                          sweep=CartesianSweep({'x': [1, 2, 3], 'y': [4]}),
                          template_engine=MakoTemplate(), verbose=False,
                          cleanup=True, save_mapping=False)

            self.assertEqual("The names {'y'} are not used in the "
                             "template.", str(context.exception)) 
Example 70
Project: python-sii   Author: voipir   File: syscall.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def call_buff(self, data, printer=None):
        """ Print a buffer on a printer.

        :param bytes data: Data buffer to be printed.
        :param printer:    Name of the printer to print on. See `query_printers` to get a list.
        """
        if isinstance(data, str):
            data = bytes(data, 'UTF-8')

        with tempfile.NamedTemporaryFile() as tmp:
            tmp.file.write(data)
            tmp.file.flush()

            self.call(tmp.name, printer) 
Example 71
Project: python-sii   Author: voipir   File: validation.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def validate_signatures(xml):
    """ Validate internal Document Signatures. Public Key are provided by them, so no need for
    anything else than the XML itself.

    :param `etree.Element` xml: Element to the rootnode of the document.

    :return: [tuple(URI, True | False), ...]
    """
    xml = prepend_dtd(xml)

    signodes = extract_signodes(xml)
    results  = []
    for signode in signodes:
        cert = extract_signode_certificate(signode)
        ref  = extract_signode_reference(signode)

        # Load Public Key
        key_mgr = xmlsec.KeysManager()
        with tempfile.NamedTemporaryFile(mode='wb', buffering=0) as tmpf:
            tmpf.write(bytes(cert, 'utf8'))

            key_mgr.load_cert(tmpf.name, xmlsec.KeyFormat.PEM, xmlsec.KeyDataType.TRUSTED)

        # Verify the Document
        ctx = xmlsec.SignatureContext(key_mgr)

        try:
            ctx.verify(signode)
        except xmlsec.error.Error:
            validity = (ref.attrib['URI'], False)
        else:
            validity = (ref.attrib['URI'], True)

        results.append(validity)

    return results 
Example 72
Project: gimp-plugin-runway   Author: prusnak   File: runway.py    MIT License 5 votes vote down vote up
def python_runway(img, layer):
    if not layer.is_rgb:
        raise ValueError("Expected RGB layer")
    # because pdb cannot save to a buffer, we have to use a temporary file instead
    f = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
    f.close()
    pdb.file_png_save(img, layer, f.name, f.name, 0, 9, 0, 0, 0, 0, 0)
    # convert data from file to base64 encoded bytes
    data = open(f.name, "rb").read()
    b64 = base64.b64encode(data)
    os.unlink(f.name)
    # send data to Runway via a POST request
    data = json.dumps({"semantic_map": b64})
    req = urllib2.Request("http://localhost:8000/query", data, {"Content-Type": "application/json"})
    f = urllib2.urlopen(req)
    resp = json.loads(f.read())
    f.close()
    # save result to a temporary file, because pdb cannot load from a buffer
    jpg = base64.b64decode(resp["output"][22:])
    f = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
    f.close()
    open(f.name, "wb").write(jpg)
    # open the temp file
    image = pdb.gimp_file_load(f.name, f.name)
    # copy the first layer to clipboard
    pdb.gimp_edit_copy(image.layers[0])
    os.unlink(f.name)
    # paste clipboard contents as a floating selection
    floating = pdb.gimp_edit_paste(layer, 0)
    floating.name = layer.name + " [Runway]" 
Example 73
Project: DOTA_models   Author: ringringyi   File: parser_eval.py    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 74
Project: advent-of-code-2018   Author: badouralix   File: c.py    MIT License 5 votes vote down vote up
def __init__(self, file):
        SubmissionWrapper.__init__(self)
        tmp = tempfile.NamedTemporaryFile(prefix="aoc")
        tmp.close()
        compile_output = subprocess.check_output(["gcc", "-Wall", "-O3", "-std=c11", "-o", tmp.name, file]).decode()
        if compile_output:
            raise CompilationError(compile_output)
        self.executable = tmp.name 
Example 75
Project: advent-of-code-2018   Author: badouralix   File: cpp.py    MIT License 5 votes vote down vote up
def __init__(self, file):
        SubmissionWrapper.__init__(self)
        tmp = tempfile.NamedTemporaryFile(prefix="aoc")
        tmp.close()
        compile_output = subprocess.check_output(["g++", "-Wall", "-Wno-sign-compare", "-O3", "-std=c++14", "-o", tmp.name, file]).decode()
        if compile_output:
            raise CompilationError(compile_output)
        self.executable = tmp.name 
Example 76
Project: fs_image   Author: facebookincubator   File: test_package_image.py    MIT License 4 votes vote down vote up
def test_package_image_as_squashfs(self):
        with self._package_image(
            self._sibling_path('create_ops.layer'), 'squashfs',
        ) as out_path, TempSubvolumes(sys.argv[0]) as temp_subvolumes, \
                tempfile.NamedTemporaryFile() as temp_sendstream:
            subvol = temp_subvolumes.create('subvol')
            with Unshare([Namespace.MOUNT, Namespace.PID]) as unshare, \
                    tempfile.TemporaryDirectory() as mount_dir:
                subprocess.check_call(nsenter_as_root(
                    unshare, 'mount', '-t', 'squashfs', '-o', 'loop',
                    out_path, mount_dir,
                ))
                # `unsquashfs` would have been cleaner than `mount` +
                # `rsync`, and faster too, but unfortunately it corrupts
                # device nodes as of v4.3.
                subprocess.check_call(nsenter_as_root(
                    unshare, 'rsync', '--archive', '--hard-links',
                    '--sparse', '--xattrs', mount_dir + '/', subvol.path(),
                ))
            with subvol.mark_readonly_and_write_sendstream_to_file(
                temp_sendstream
            ):
                pass
            original_render = _render_sendstream_path(
                self._sibling_path('create_ops-original.sendstream'),
            )
            # SquashFS does not preserve the original's cloned extents of
            # zeros, nor the zero-hole-zero patter.  In all cases, it
            # (efficiently) transmutes the whole file into 1 sparse hole.
            self.assertEqual(original_render[1].pop('56KB_nuls'), [
                '(File d57344([email protected]_nuls_clone:[email protected]/' +
                '[email protected]_nuls_clone:[email protected]))'
            ])
            original_render[1]['56KB_nuls'] = ['(File h57344)']
            self.assertEqual(original_render[1].pop('56KB_nuls_clone'), [
                '(File d57344([email protected]_nuls:[email protected]/' +
                '[email protected]_nuls:[email protected]))'
            ])
            original_render[1]['56KB_nuls_clone'] = ['(File h57344)']
            self.assertEqual(original_render[1].pop('zeros_hole_zeros'), [
                '(File d16384h16384d16384)'
            ])
            original_render[1]['zeros_hole_zeros'] = ['(File h49152)']
            self.assertEqual(
                original_render, _render_sendstream_path(temp_sendstream.name),
            ) 
Example 77
Project: fs_image   Author: facebookincubator   File: test_symlink.py    MIT License 4 votes vote down vote up
def test_symlink_command(self):
        with TempSubvolumes(sys.argv[0]) as temp_subvolumes:
            subvol = temp_subvolumes.create('tar-sv')
            subvol.run_as_root(['mkdir', subvol.path('dir')])

            # We need a source file to validate a SymlinkToFileItem
            with tempfile.NamedTemporaryFile() as tf:
                InstallFileItem(
                    from_target='t', source=tf.name, dest='/file',
                ).build(subvol, DUMMY_LAYER_OPTS)

            SymlinkToDirItem(
                from_target='t', source='/dir', dest='/dir_symlink'
            ).build(subvol, DUMMY_LAYER_OPTS)
            SymlinkToFileItem(
                from_target='t', source='file', dest='/file_symlink'
            ).build(subvol, DUMMY_LAYER_OPTS)

            def quoted_subvol_path(p):
                return shlex.quote(subvol.path(p).decode())

            # Make a couple of absolute symlinks to test our behavior on
            # linking to paths that contain those.
            subvol.run_as_root(['bash', '-c', f'''\
                ln -s /file {quoted_subvol_path('abs_link_to_file')}
                mkdir {quoted_subvol_path('my_dir')}
                touch {quoted_subvol_path('my_dir/inner')}
                ln -s /my_dir {quoted_subvol_path('my_dir_link')}
            '''])
            # A simple case: we link to an absolute link.
            SymlinkToFileItem(
                from_target='t',
                source='/abs_link_to_file',
                dest='/link_to_abs_link',
            ).build(subvol, DUMMY_LAYER_OPTS)
            # This link traverses a directory that is an absolute link.  The
            # resulting relative symlink is not traversible from outside the
            # container.
            SymlinkToFileItem(
                from_target='t',
                source='my_dir_link/inner',
                dest='/dir/inner_link',
            ).build(subvol, DUMMY_LAYER_OPTS)

            self.assertEqual(['(Dir)', {
                'dir': ['(Dir)', {
                    'inner_link': ['(Symlink ../my_dir_link/inner)'],
                }],
                'dir_symlink': ['(Symlink dir)'],
                'file': ['(File m444)'],
                'file_symlink': ['(Symlink file)'],

                'abs_link_to_file': ['(Symlink /file)'],
                'my_dir': ['(Dir)', {'inner': ['(File)']}],
                'my_dir_link': ['(Symlink /my_dir)'],

                'link_to_abs_link': ['(Symlink abs_link_to_file)'],
            }], render_subvol(subvol)) 
Example 78
Project: Splunk_CBER_App   Author: MHaggis   File: main.py    MIT License 4 votes vote down vote up
def get_flask_server():
    flask_server = Flask('liveresponse')

    @flask_server.route('/', methods=['GET', 'POST'])
    def index():
        with live_response_lock:
            return render_template('index.html', sensors=live_response_results.keys())

    @flask_server.route('/sensor_result/<sensor_id>')
    def sensor_result(sensor_id):
        sensor_id = int(sensor_id)
        with live_response_lock:
            results = live_response_results.get(sensor_id, {})
            if 'results' in results:
                return render_template('sensor_result.html', results=results['results'], sensor_id=sensor_id)
            else:
                return render_template('no_such_result.html')

    @flask_server.route('/browser_history/<sensor_id>')
    def browser_history(sensor_id):
        sensor_id = int(sensor_id)
        with live_response_lock:
            results = live_response_results.get(sensor_id, {})
            if 'results' in results and 'browser_history' in results['results']:
                with NamedTemporaryFile(delete=False) as tf:
                    tf.write(results['results']['browser_history'])
                    tf.close()
                    db = sqlite3.connect(tf.name)
                    db.row_factory = sqlite3.Row
                    cur = db.cursor()
                    cur.execute("SELECT url, title, datetime(last_visit_time / 1000000 + (strftime('%s', '1601-01-01')), 'unixepoch') as last_visit_time FROM urls ORDER BY last_visit_time DESC")
                    urls = [dict(u) for u in cur.fetchall()]

                    for url in urls:
                        (scheme, netloc, _, _, _) = urlparse.urlsplit(url["url"])
                        url["favicon"] = urlparse.urlunsplit((scheme, netloc, "favicon.ico", "", ""))

                    return render_template('browser_history.html', urls=urls)

            return render_template('no_such_result.html')

    t = threading.Thread(target=flask_server.run, args=['127.0.0.1', 7982])
    t.daemon = True
    return t 
Example 79
Project: cmus-osx   Author: PhilipTrauner   File: __init__.py    MIT License 4 votes vote down vote up
def uninstall(ctx):
    cmus_config = ctx.obj["cmus_config"]
    cmus_osx_base_path = ctx.obj["cmus_osx_base_path"]
    rc_script_path = ctx.obj["rc_script_path"]
    sdp_script_path = ctx.obj["sdp_script_path"]

    try:
        rmtree(cmus_osx_base_path)
    except FileNotFoundError:
        pass

    cmus_instances = get_cmus_instances()

    if cmus_instances is not None:
        for pid in cmus_instances:
            kill(pid, SIGTERM)

    write_rc = False

    tmp_rc_file = NamedTemporaryFile("w", delete=False)
    with open(cmus_config.rc_path, "r") as f:
        for line in f:
            match = RC_ENTRY_REGEX.match(line)
            # Exclude invocations of 'rc' script
            if not (match is not None and Path(match.group(1)) == rc_script_path):
                tmp_rc_file.write(line)
            else:
                write_rc = True

    if write_rc:
        rename(tmp_rc_file.name, cmus_config.rc_path)
    else:
        remove(tmp_rc_file.name)

    write_autosave = False

    tmp_autosave_file = NamedTemporaryFile("w", delete=False)
    with open(cmus_config.autosave_path, "r") as f:
        for line in f:
            match = STATUS_DISPLAY_PROGRAM_REGEX.match(line)
            # Exclude invocations of 'status_display_program' script
            if not (match is not None and Path(match.group(1)) == sdp_script_path):
                tmp_autosave_file.write(line)
            else:
                tmp_autosave_file.write("set status_display_program=\n")
                write_autosave = True

    # No need to replace 'autosave' if no changes were written
    if write_autosave:
        rename(tmp_autosave_file.name, cmus_config.autosave_path)
    else:
        remove(tmp_autosave_file.name)

    if not write_rc and not write_autosave:
        echo(f"{style('NOTE', fg='magenta')}: Already uninstalled.")
    else:
        echo(f"{style('SUCCESS', fg='green')}: Successfully uninstalled.") 
Example 80
Project: myhoard   Author: aiven   File: basebackup_operation.py    Apache License 2.0 4 votes vote down vote up
def create_backup(self):
        self.abort_reason = None
        self.data_directory_size_start, self.data_directory_filtered_size = self._get_data_directory_size()
        self._update_progress()

        # Write encryption key to file to avoid having it on command line. NamedTemporaryFile has mode 0600
        with tempfile.NamedTemporaryFile(
            dir=self.temp_dir_base, delete=True, prefix="encrkey", suffix="bin"
        ) as encryption_key_file:
            encryption_key_file.write(base64.b64encode(self.encryption_key))
            encryption_key_file.flush()

            # Create new configuration file that has original MySQL config plus user and password
            # for connecting to it to avoid having those on command line
            with tempfile.NamedTemporaryFile(
                dir=self.temp_dir_base, delete=True, prefix="mysql", mode="w", suffix="conf"
            ) as mysql_config_file:
                mysql_config_file.write(self.mysql_config)
                client_params_str = "\n".join(f"{k}={v}" for k, v in self.mysql_client_params.items())
                mysql_config_file.write(f"\n[client]\n{client_params_str}\n")
                mysql_config_file.flush()

                self.temp_dir = tempfile.mkdtemp(dir=self.temp_dir_base, prefix="xtrabackup")
                # yapf: disable
                command_line = [
                    "xtrabackup",
                    # defaults file must be given with --defaults-file=foo syntax, space here does not work
                    f"--defaults-file={mysql_config_file.name}",
                    "--backup",
                    "--compress",
                    "--encrypt", self.encryption_algorithm,
                    "--encrypt-key-file", encryption_key_file.name,
                    "--no-version-check",
                    "--stream", "xbstream",
                    "--target-dir", self.temp_dir,
                ]

                with self.stats.timing_manager("myhoard.basebackup.xtrabackup_backup"):
                    with subprocess.Popen(
                        command_line, bufsize=0, stdout=subprocess.PIPE, stderr=subprocess.PIPE
                    ) as xtrabackup:
                        self.proc = xtrabackup
                        self._process_input_output()

        self.data_directory_size_end, self.data_directory_filtered_size = self._get_data_directory_size()
        self._update_progress()