Python tempfile.TemporaryFile() Examples
The following are 30
code examples of tempfile.TemporaryFile().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tempfile
, or try the search function
.
![](https://www.programcreek.com/common/static/images/search.png)
Example #1
Source File: device.py From Paradrop with Apache License 2.0 | 10 votes |
def create(ctx): """ Install a chute from the working directory. """ url = "{}/chutes/".format(ctx.obj['base_url']) headers = {'Content-Type': 'application/x-tar'} if not os.path.exists("paradrop.yaml"): raise Exception("No paradrop.yaml file found in working directory.") with tempfile.TemporaryFile() as temp: tar = tarfile.open(fileobj=temp, mode="w") for dirName, subdirList, fileList in os.walk('.'): for fname in fileList: path = os.path.join(dirName, fname) arcname = os.path.normpath(path) tar.add(path, arcname=arcname) tar.close() temp.seek(0) res = router_request("POST", url, headers=headers, data=temp) data = res.json() ctx.invoke(watch, change_id=data['change_id'])
Example #2
Source File: device.py From Paradrop with Apache License 2.0 | 10 votes |
def update(ctx): """ Update the chute from the working directory. """ url = ctx.obj['chute_url'] headers = {'Content-Type': 'application/x-tar'} if not os.path.exists("paradrop.yaml"): raise Exception("No paradrop.yaml file found in working directory.") with tempfile.TemporaryFile() as temp: tar = tarfile.open(fileobj=temp, mode="w") for dirName, subdirList, fileList in os.walk('.'): for fname in fileList: path = os.path.join(dirName, fname) arcname = os.path.normpath(path) tar.add(path, arcname=arcname) tar.close() temp.seek(0) res = router_request("PUT", url, headers=headers, data=temp) data = res.json() ctx.invoke(watch, change_id=data['change_id'])
Example #3
Source File: test_self.py From python-netsurv with MIT License | 6 votes |
def test_no_out_encoding(self): """test redirection of stdout with non ascii caracters """ # This test reproduces bug #48066 ; it happens when stdout is redirected # through '>' : the sys.stdout.encoding becomes then None, and if the # output contains non ascii, pylint will crash if sys.version_info < (3, 0): strio = tempfile.TemporaryFile() else: strio = StringIO() assert strio.encoding is None self._runtest( [join(HERE, "regrtest_data/no_stdout_encoding.py"), "--enable=all"], out=strio, code=28, )
Example #4
Source File: stdlib.py From tox with MIT License | 6 votes |
def suppress_output(): """suppress both stdout and stderr outputs""" if sys.version_info >= (3, 5): from contextlib import redirect_stdout, redirect_stderr else: class _RedirectStream(object): _stream = None def __init__(self, new_target): self._new_target = new_target self._old_targets = [] def __enter__(self): self._old_targets.append(getattr(sys, self._stream)) setattr(sys, self._stream, self._new_target) return self._new_target def __exit__(self, exctype, excinst, exctb): setattr(sys, self._stream, self._old_targets.pop()) class redirect_stdout(_RedirectStream): _stream = "stdout" class redirect_stderr(_RedirectStream): _stream = "stderr" with TemporaryFile("wt") as file: with redirect_stdout(file): with redirect_stderr(file): yield
Example #5
Source File: pastebin.py From python-netsurv with MIT License | 6 votes |
def pytest_configure(config): if config.option.pastebin == "all": tr = config.pluginmanager.getplugin("terminalreporter") # if no terminal reporter plugin is present, nothing we can do here; # this can happen when this function executes in a slave node # when using pytest-xdist, for example if tr is not None: # pastebin file will be utf-8 encoded binary file config._pastebinfile = tempfile.TemporaryFile("w+b") oldwrite = tr._tw.write def tee_write(s, **kwargs): oldwrite(s, **kwargs) if isinstance(s, str): s = s.encode("utf-8") config._pastebinfile.write(s) tr._tw.write = tee_write
Example #6
Source File: test_self.py From python-netsurv with MIT License | 6 votes |
def test_no_out_encoding(self): """test redirection of stdout with non ascii caracters """ # This test reproduces bug #48066 ; it happens when stdout is redirected # through '>' : the sys.stdout.encoding becomes then None, and if the # output contains non ascii, pylint will crash if sys.version_info < (3, 0): strio = tempfile.TemporaryFile() else: strio = StringIO() assert strio.encoding is None self._runtest( [join(HERE, "regrtest_data/no_stdout_encoding.py"), "--enable=all"], out=strio, code=28, )
Example #7
Source File: capture.py From python-netsurv with MIT License | 6 votes |
def __init__(self, targetfd, tmpfile=None, now=True, patchsys=False): """ save targetfd descriptor, and open a new temporary file there. If no tmpfile is specified a tempfile.Tempfile() will be opened in text mode. """ self.targetfd = targetfd if tmpfile is None and targetfd != 0: f = tempfile.TemporaryFile('wb+') tmpfile = dupfile(f, encoding="UTF-8") f.close() self.tmpfile = tmpfile self._savefd = os.dup(self.targetfd) if patchsys: self._oldsys = getattr(sys, patchsysdict[targetfd]) if now: self.start()
Example #8
Source File: terminal.py From TerminalView with MIT License | 6 votes |
def capture(self, data, term_instance=None): """ Stores *data* as a temporary file and returns that file's object. *term_instance* can be used by overrides of this function to make adjustments to the terminal emulator after the *data* is captured e.g. to make room for an image. """ # Remove the extra \r's that the terminal adds: data = data.replace(b'\r\n', b'\n') logging.debug("capture() len(data): %s" % len(data)) # Write the data to disk in a temporary location self.file_obj = tempfile.TemporaryFile() self.file_obj.write(data) self.file_obj.flush() # Leave it open return self.file_obj
Example #9
Source File: capture.py From py with MIT License | 6 votes |
def __init__(self, targetfd, tmpfile=None, now=True, patchsys=False): """ save targetfd descriptor, and open a new temporary file there. If no tmpfile is specified a tempfile.Tempfile() will be opened in text mode. """ self.targetfd = targetfd if tmpfile is None and targetfd != 0: f = tempfile.TemporaryFile('wb+') tmpfile = dupfile(f, encoding="UTF-8") f.close() self.tmpfile = tmpfile self._savefd = os.dup(self.targetfd) if patchsys: self._oldsys = getattr(sys, patchsysdict[targetfd]) if now: self.start()
Example #10
Source File: test_libraries.py From fusesoc with BSD 2-Clause "Simplified" License | 6 votes |
def test_library_location(): from fusesoc.main import _get_core, init_coremanager tcf = tempfile.TemporaryFile(mode="w+") tcf.write( EXAMPLE_CONFIG.format( build_root=build_root, cache_root=cache_root, cores_root=cores_root, library_root=library_root, auto_sync="false", sync_uri=sync_uri, sync_type="git", ) ) tcf.seek(0) conf = Config(file=tcf) cm = init_coremanager(conf, []) _get_core(cm, "mor1kx-generic") _get_core(cm, "atlys")
Example #11
Source File: sysvalidator.py From upvm with Apache License 2.0 | 6 votes |
def check_for_writable_imgdir(): c.debug("Testing write perms by creating tempfile in {}".format(cfg.opts.img_dir)) try: f = tempfile.TemporaryFile(dir=cfg.opts.img_dir) f.close() except: dirstat = os.stat(cfg.opts.img_dir) user = pwd.getpwuid(dirstat.st_uid).pw_name group = grp.getgrgid(dirstat.st_gid).gr_name print(c.RED("Unable to create new file in image dir '{}' owned by {}:{}".format(cfg.opts.img_dir, user, group))) if myUser in grp.getgrnam(group).gr_mem: print("Your user ({}) *is* a member of the appropriate group ({}); however ...\n" "Your current login session is not running with that group credential\n" "To fix this, open a new session (ssh/su -) or log out & log back in (GUI)".format(myUser, group)) else: print("Either fix directory permissions as root or specify alternate dir with '--img-dir' option") exit(1)
Example #12
Source File: ruleset.py From ScoutSuite with GNU General Public License v2.0 | 6 votes |
def __init__(self, cloud_provider, rule_dirs=None, rule_filename=None, rule_args=None, rule_level='danger'): super().__init__(cloud_provider) rule_dirs = [] if rule_dirs is None else rule_dirs rule_args = [] if rule_args is None else rule_args self.rule_type = 'findings' tmp_ruleset = {'rules': {}, 'about': 'Temporary, single-rule ruleset.'} tmp_ruleset['rules'][rule_filename] = [] rule = {'enabled': True, 'level': rule_level} if len(rule_args): rule['args'] = rule_args tmp_ruleset['rules'][rule_filename].append(rule) tmp_ruleset_file = tempfile.TemporaryFile('w+t') tmp_ruleset_file.write(json.dumps(tmp_ruleset)) self.rules_data_path = os.path.dirname( os.path.dirname(os.path.abspath(__file__))) + '/providers/%s/rules' % cloud_provider self.load_rules(file=tmp_ruleset_file, rule_type='findings') self.shared_init(False, rule_dirs, '', [])
Example #13
Source File: cmd2plus.py From OpenTrader with GNU Lesser General Public License v3.0 | 6 votes |
def redirect_output(self, statement): if statement.parsed.pipeTo: self.kept_state = Statekeeper(self, ('stdout',)) self.kept_sys = Statekeeper(sys, ('stdout',)) self.redirect = subprocess.Popen(statement.parsed.pipeTo, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) sys.stdout = self.stdout = self.redirect.stdin elif statement.parsed.output: if (not statement.parsed.outputTo) and (not can_clip): raise EnvironmentError('Cannot redirect to paste buffer; install ``xclip`` and re-run to enable') self.kept_state = Statekeeper(self, ('stdout',)) self.kept_sys = Statekeeper(sys, ('stdout',)) if statement.parsed.outputTo: mode = 'w' if statement.parsed.output == 2 * self.redirector: mode = 'a' sys.stdout = self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode) else: sys.stdout = self.stdout = tempfile.TemporaryFile(mode="w+") if statement.parsed.output == '>>': self.stdout.write(get_paste_buffer())
Example #14
Source File: capture.py From python-netsurv with MIT License | 6 votes |
def __init__(self, targetfd, tmpfile=None, now=True, patchsys=False): """ save targetfd descriptor, and open a new temporary file there. If no tmpfile is specified a tempfile.Tempfile() will be opened in text mode. """ self.targetfd = targetfd if tmpfile is None and targetfd != 0: f = tempfile.TemporaryFile('wb+') tmpfile = dupfile(f, encoding="UTF-8") f.close() self.tmpfile = tmpfile self._savefd = os.dup(self.targetfd) if patchsys: self._oldsys = getattr(sys, patchsysdict[targetfd]) if now: self.start()
Example #15
Source File: node.py From Paradrop with Apache License 2.0 | 6 votes |
def update_chute(ctx, directory): """ Install a new version of the chute from the working directory. Install the files in the current directory as a chute on the node. The directory must contain a paradrop.yaml file. The entire directory will be copied to the node for installation. """ os.chdir(directory) if not os.path.exists("paradrop.yaml"): raise Exception("No paradrop.yaml file found in chute directory.") with open('paradrop.yaml', 'r') as source: config = yaml.safe_load(source) if 'name' not in config: click.echo('Chute name is not defined in paradrop.yaml.') return client = ctx.obj['client'] with tempfile.TemporaryFile() as temp: tar = tarfile.open(fileobj=temp, mode="w") for dirName, subdirList, fileList in os.walk("."): for fname in fileList: path = os.path.join(dirName, fname) arcname = os.path.normpath(path) tar.add(path, arcname=arcname) tar.close() temp.seek(0) result = client.install_tar(temp, name=config['name']) ctx.invoke(watch_change_logs, change_id=result['change_id'])
Example #16
Source File: node.py From Paradrop with Apache License 2.0 | 6 votes |
def install_chute(ctx, directory): """ Install a chute from the working directory. Install the files in the current directory as a chute on the node. The directory must contain a paradrop.yaml file. The entire directory will be copied to the node for installation. """ os.chdir(directory) if not os.path.exists("paradrop.yaml"): raise Exception("No paradrop.yaml file found in chute directory.") client = ctx.obj['client'] with tempfile.TemporaryFile() as temp: tar = tarfile.open(fileobj=temp, mode="w") for dirName, subdirList, fileList in os.walk("."): for fname in fileList: path = os.path.join(dirName, fname) arcname = os.path.normpath(path) tar.add(path, arcname=arcname) tar.close() temp.seek(0) result = client.install_tar(temp) ctx.invoke(watch_change_logs, change_id=result['change_id'])
Example #17
Source File: pastebin.py From python-netsurv with MIT License | 6 votes |
def pytest_configure(config): if config.option.pastebin == "all": tr = config.pluginmanager.getplugin("terminalreporter") # if no terminal reporter plugin is present, nothing we can do here; # this can happen when this function executes in a slave node # when using pytest-xdist, for example if tr is not None: # pastebin file will be utf-8 encoded binary file config._pastebinfile = tempfile.TemporaryFile("w+b") oldwrite = tr._tw.write def tee_write(s, **kwargs): oldwrite(s, **kwargs) if isinstance(s, str): s = s.encode("utf-8") config._pastebinfile.write(s) tr._tw.write = tee_write
Example #18
Source File: server.py From Pyro5 with MIT License | 6 votes |
def annotation_stream(self, with_checksum=False): # create a large temporary file f = tempfile.TemporaryFile() for _ in range(5000): f.write(b"1234567890!" * 1000) filesize = f.tell() f.seek(os.SEEK_SET, 0) # return the file data via annotation stream (remote iterator) annotation_size = 500000 print("transmitting file via annotations stream (%d bytes in chunks of %d)..." % (filesize, annotation_size)) with f: while True: chunk = f.read(annotation_size) if not chunk: break # store the file data chunk in the FDAT response annotation, # and return the current file position and checksum (if asked). current_context.response_annotations = {"FDAT": chunk} yield f.tell(), zlib.crc32(chunk) if with_checksum else 0
Example #19
Source File: test_exec_command.py From lambda-packs with MIT License | 6 votes |
def test_exec_command_stdout(): # Regression test for gh-2999 and gh-2915. # There are several packages (nose, scipy.weave.inline, Sage inline # Fortran) that replace stdout, in which case it doesn't have a fileno # method. This is tested here, with a do-nothing command that fails if the # presence of fileno() is assumed in exec_command. # The code has a special case for posix systems, so if we are on posix test # both that the special case works and that the generic code works. # Test posix version: with redirect_stdout(StringIO()): with redirect_stderr(TemporaryFile()): exec_command.exec_command("cd '.'") if os.name == 'posix': # Test general (non-posix) version: with emulate_nonposix(): with redirect_stdout(StringIO()): with redirect_stderr(TemporaryFile()): exec_command.exec_command("cd '.'")
Example #20
Source File: capture.py From python-netsurv with MIT License | 6 votes |
def __init__(self, targetfd, tmpfile=None): self.targetfd = targetfd try: self.targetfd_save = os.dup(self.targetfd) except OSError: self.start = lambda: None self.done = lambda: None else: if targetfd == 0: assert not tmpfile, "cannot set tmpfile with stdin" tmpfile = open(os.devnull, "r") self.syscapture = SysCapture(targetfd) else: if tmpfile is None: f = TemporaryFile() with f: tmpfile = safe_text_dupfile(f, mode="wb+") if targetfd in patchsysdict: self.syscapture = SysCapture(targetfd, tmpfile) else: self.syscapture = NoCapture() self.tmpfile = tmpfile self.tmpfile_fd = tmpfile.fileno()
Example #21
Source File: caching.py From filesystem_spec with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _makefile(self): import tempfile import mmap if self.size == 0: return bytearray() # posix version if self.location is None or not os.path.exists(self.location): if self.location is None: fd = tempfile.TemporaryFile() self.blocks = set() else: fd = io.open(self.location, "wb+") fd.seek(self.size - 1) fd.write(b"1") fd.flush() else: fd = io.open(self.location, "rb+") return mmap.mmap(fd.fileno(), self.size)
Example #22
Source File: test_exec_command.py From recruit with Apache License 2.0 | 6 votes |
def test_exec_command_stdout(): # Regression test for gh-2999 and gh-2915. # There are several packages (nose, scipy.weave.inline, Sage inline # Fortran) that replace stdout, in which case it doesn't have a fileno # method. This is tested here, with a do-nothing command that fails if the # presence of fileno() is assumed in exec_command. # The code has a special case for posix systems, so if we are on posix test # both that the special case works and that the generic code works. # Test posix version: with redirect_stdout(StringIO()): with redirect_stderr(TemporaryFile()): exec_command.exec_command("cd '.'") if os.name == 'posix': # Test general (non-posix) version: with emulate_nonposix(): with redirect_stdout(StringIO()): with redirect_stderr(TemporaryFile()): exec_command.exec_command("cd '.'")
Example #23
Source File: __main__.py From vscode-esp-idf-extension with Apache License 2.0 | 5 votes |
def run_adapter(self): debug("CWD: " + os.getcwd()) # pipe_output = tempfile.TemporaryFile() da_main_path = pathlib.Path(__file__).parent.parent.parent.joinpath("debug_adapter_main.py") p_path = pathlib.Path(which("python3")) adapter_cmd = [p_path, str(da_main_path)] adapter_cmd += ADAPTER_START_ARGS # os.system("tree %s" % pathlib.Path(__file__).parent.parent.parent) info("run_adapter {}".format(adapter_cmd)) da_proc = subprocess.Popen( bufsize=0, args=adapter_cmd, stdout=subprocess.PIPE, # pipe_output, stderr=subprocess.STDOUT, universal_newlines=True) # let adapter to start, if we try to read its stdout imediately it can be empty # or may not contain DEBUG_ADAPTER_READY2CONNECT yet end_time = time.process_time() + 5.0 while True: now = time.process_time() line = da_proc.stdout.readline() debug("Readed from DA: " + line) # line = pipe_output.readline() # print("Debug adapter is loading... :" + str(line), flush=True, end="") if "DEBUG_ADAPTER_READY2CONNECT" in line: info('Debug adapter is LOADED!') break if end_time <= now: self.fail("Failed to wait for DebugAdpater to start!")
Example #24
Source File: formparser.py From jbox with MIT License | 5 votes |
def default_stream_factory(total_content_length, filename, content_type, content_length=None): """The stream factory that is used per default.""" if total_content_length > 1024 * 500: return TemporaryFile('wb+') return BytesIO()
Example #25
Source File: pickle_tests.py From paramz with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_param(self): param = Param('test', np.arange(4*2).reshape(4,2)) param[0].constrain_positive() param[1].fix() pcopy = param.copy() self.assertListEqual(param.tolist(), pcopy.tolist()) self.assertListEqual(str(param).split('\n'), str(pcopy).split('\n')) self.assertIsNot(param, pcopy) with tempfile.TemporaryFile('w+b') as f: pickle.dump(param, f) f.seek(0) pcopy = paramz.load(f) self.assertListEqual(param.tolist(), pcopy.tolist()) self.assertSequenceEqual(str(param), str(pcopy))
Example #26
Source File: pickle_tests.py From paramz with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_parameterized(self): par = Parameterized('parameterized') p2 = Parameterized('rbf') p2.p1 = Param('lengthscale', np.random.uniform(0.1,.5,3), Exponent()) p2.link_parameter(p2.p1) par.p1 = p2 par.p2 = Param('linear', np.random.uniform(0.1, .5, 2), Logexp()) par.link_parameters(par.p1, par.p2) par.gradient = 10 par.randomize() pcopy = par.copy() self.assertIsInstance(pcopy.constraints, ParameterIndexOperations) self.assertIsInstance(pcopy.rbf.constraints, ParameterIndexOperationsView) self.assertIs(pcopy.constraints, pcopy.rbf.constraints._param_index_ops) self.assertIs(pcopy.constraints, pcopy.rbf.lengthscale.constraints._param_index_ops) self.assertListEqual(par.param_array.tolist(), pcopy.param_array.tolist()) pcopy.gradient = 10 # gradient does not get copied anymore self.assertListEqual(par.gradient_full.tolist(), pcopy.gradient_full.tolist()) self.assertSequenceEqual(str(par), str(pcopy)) self.assertIsNot(par.param_array, pcopy.param_array) self.assertIsNot(par.gradient_full, pcopy.gradient_full) with tempfile.TemporaryFile('w+b') as f: par.pickle(f) f.seek(0) pcopy = paramz.load(f) self.assertListEqual(par.param_array.tolist(), pcopy.param_array.tolist()) pcopy.gradient = 10 np.testing.assert_allclose(par.linear.gradient_full, pcopy.linear.gradient_full) np.testing.assert_allclose(pcopy.linear.gradient_full, 10) self.assertSequenceEqual(str(par), str(pcopy))
Example #27
Source File: __init__.py From target-postgres with GNU Affero General Public License v3.0 | 5 votes |
def flush_records(o, csv_files_to_load, row_count, primary_key_exists, sync): stream = o['stream'] sync.load_csv(csv_files_to_load[stream], row_count[stream]) row_count[stream] = 0 primary_key_exists[stream] = {} csv_files_to_load[stream] = TemporaryFile(mode='w+b')
Example #28
Source File: test_threadedtempfile.py From ironpython2 with Apache License 2.0 | 5 votes |
def run(self): self.errors = StringIO.StringIO() startEvent.wait() for i in range(FILES_PER_THREAD): try: f = tempfile.TemporaryFile("w+b") f.close() except: self.error_count += 1 print_exc(file=self.errors) else: self.ok_count += 1
Example #29
Source File: test_plot.py From mars with Apache License 2.0 | 5 votes |
def _check_plot_works(f, filterwarnings="always", **kwargs): # pragma: no cover import matplotlib.pyplot as plt ret = None with warnings.catch_warnings(): warnings.simplefilter(filterwarnings) try: try: fig = kwargs["figure"] except KeyError: fig = plt.gcf() plt.clf() kwargs.get("ax", fig.add_subplot(211)) ret = f(**kwargs) assert_is_valid_plot_return_object(ret) if f is pd.plotting.bootstrap_plot: assert "ax" not in kwargs else: kwargs["ax"] = fig.add_subplot(212) ret = f(**kwargs) assert_is_valid_plot_return_object(ret) with tempfile.TemporaryFile() as path: plt.savefig(path) finally: close(fig) return ret
Example #30
Source File: setup.py From ivre with GNU General Public License v3.0 | 5 votes |
def _write_pkg_file(self, file): with TemporaryFile(mode="w+") as tmpfd: _write_pkg_file_orig(self, tmpfd) tmpfd.seek(0) for line in tmpfd: if line.startswith('Metadata-Version: '): file.write('Metadata-Version: 2.1\n') elif line.startswith('Description: '): file.write('Description-Content-Type: %s; charset=UTF-8\n' % long_description_content_type) file.write(line) else: file.write(line)