Python tempfile.mkstemp() Examples
The following are 30
code examples of tempfile.mkstemp().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tempfile
, or try the search function
.

Example #1
Source Project: BASS Author: Cisco-Talos File: bindiff.py License: GNU General Public License v2.0 | 9 votes |
def bindiff_export(self, sample, is_64_bit = True, timeout = None): """ Load a sample into IDA Pro, perform autoanalysis and export a BinDiff database. :param sample: The sample's path :param is_64_bit: If the sample needs to be analyzed by the 64 bit version of IDA :param timeout: Timeout for the analysis in seconds :return: The file name of the exported bindiff database. The file needs to be deleted by the caller. Returns None on error. """ data_to_send = { "timeout": timeout, "is_64_bit": is_64_bit} url = "%s/binexport" % next(self._urls) log.debug("curl -XPOST --data '%s' '%s'", json.dumps(data_to_send), url) response = requests.post(url, data = data_to_send, files = {os.path.basename(sample): open(sample, "rb")}) if response.status_code == 200: handle, output = tempfile.mkstemp(suffix = ".BinExport") with os.fdopen(handle, "wb") as f: map(f.write, response.iter_content(1024)) return output else: log.error("Bindiff server responded with status code %d: %s", response.status_code, response.content) return None
Example #2
Source Project: BASS Author: Cisco-Talos File: core.py License: GNU General Public License v2.0 | 9 votes |
def get_num_triggering_samples(signature, samples): """ Get number of samples triggering ClamAV signature _signature_. :param signature: A dictionary with keys 'type' for the signature type and 'signature' for the signature string. :param samples: A list of sample paths to scan. :returns: The number of samples triggering this signature. """ handle, temp_sig = tempfile.mkstemp(suffix = "." + signature["type"]) try: with os.fdopen(handle, "w") as f: f.write(signature["signature"]) proc_clamscan = subprocess.Popen(["clamscan", "-d", temp_sig, "--no-summary", "--infected"] + samples, stdout = subprocess.PIPE, stderr = subprocess.PIPE) stdout, stderr = proc_clamscan.communicate() if not stdout: return 0 else: return len(stdout.strip().split("\n")) finally: os.unlink(temp_sig)
Example #3
Source Project: BASS Author: Cisco-Talos File: server.py License: GNU General Public License v2.0 | 8 votes |
def whitelist_add(): log.info("whitelist_add called") try: file_ = request.files["file"] handle, filename = tempfile.mkstemp() os.close(handle) file_.save(filename) data = request.get_json() if data and "functions" in data: functions = data["functions"] else: functions = None bass.whitelist_add(filename, functions) os.unlink(filename) except KeyError: log.exception("") return make_response(jsonify(message = "Sample file 'file' missing in POST request"), 400) return jsonify(message = "OK")
Example #4
Source Project: ALF Author: blackberry File: _gdb.py License: Apache License 2.0 | 7 votes |
def _symbolize(target, output, tool, exp_opt): fd, tmp_log = tempfile.mkstemp(prefix="%s_log" % tool, suffix=".txt", dir=".") try: os.write(fd, output) finally: os.close(fd) try: result = _common.run([TOOL_GDB, "-batch", "-nx", "-ex", "set python print-stack full", "-ex", "py import exploitable", "-ex", "exploitable -m %s %s" % (exp_opt, tmp_log), "-ex", "quit", target], timeout=180) finally: _common.delete(tmp_log) if result.classification == _common.TIMEOUT: raise RuntimeError("Timed out while processing %s output:\n%s" % (tool, output)) result.backtrace, result.classification = _process_gdb_output(result.text) result.text = _common._limit_output_length(result.text) if result.classification == _common.NOT_AN_EXCEPTION: raise RuntimeError("Failed to process %s output:\n%s" % (tool, output)) return result
Example #5
Source Project: py Author: pytest-dev File: test_local.py License: MIT License | 7 votes |
def test_setmtime(self): import tempfile import time try: fd, name = tempfile.mkstemp() os.close(fd) except AttributeError: name = tempfile.mktemp() open(name, 'w').close() try: mtime = int(time.time())-100 path = local(name) assert path.mtime() != mtime path.setmtime(mtime) assert path.mtime() == mtime path.setmtime() assert path.mtime() != mtime finally: os.remove(name)
Example #6
Source Project: pybotgram Author: rockneurotiko File: utils.py License: GNU Affero General Public License v3.0 | 7 votes |
def download_to_file(path, ext): # Is it worth to have the same name? # ppath = urlparse(path).path.split("/") # ppath = ppath[-1] if len(ppath) > 0 else None _, file_name = tempfile.mkstemp("." + ext) r = requests.get(path, stream=True) total_length = r.headers.get('content-length') dl = 0 with open(file_name, 'wb') as f: if total_length is None: f.write(r.content) else: total_length = int(total_length) pbar = ProgressBar(maxval=total_length).start() for chunk in r.iter_content(chunk_size=1024): if chunk: # filter out keep-alive new chunks pbar.update(dl * len(chunk)) dl += 1 f.write(chunk) f.flush() pbar.finish() return file_name
Example #7
Source Project: BASS Author: Cisco-Talos File: bindiff.py License: GNU General Public License v2.0 | 6 votes |
def pickle_export(self, sample, is_64_bit = True, timeout = None): """ Load a sample into IDA Pro, perform autoanalysis and export a pickle file. :param sample: The sample's path :param is_64_bit: If the sample needs to be analyzed by the 64 bit version of IDA :param timeout: Timeout for the analysis in seconds :return: The file name of the exported pickle database. The file needs to be deleted by the caller. Returns None on error. """ data_to_send = { "timeout": timeout, "is_64_bit": is_64_bit} url = "%s/pickle" % next(self._urls) log.debug("curl -XPOST --data '%s' '%s'", json.dumps(data_to_send), url) response = requests.post(url, data = data_to_send, files = {os.path.basename(sample): open(sample, "rb")}) if response.status_code == 200: handle, output = tempfile.mkstemp(suffix = ".pickle") with os.fdopen(handle, "wb") as f: map(f.write, response.iter_content(1024)) return output else: log.error("Bindiff server responded with status code %d: %s", response.status_code, response.content) return None
Example #8
Source Project: BASS Author: Cisco-Talos File: bindiff.py License: GNU General Public License v2.0 | 6 votes |
def compare(self, primary, secondary, timeout = None): """ Run BinDiff on the two BinDiff databases. :param primary: The first BinExport database :param secondary: The second BinExport database :param timeout: Timeout for the command in seconds :returns: The directory name of the directory with the generated data on the shared volume """ url = "%s/compare" % next(self._urls) log.debug("curl -XPOST --form 'timeout=%s' --form 'primary=@%s' --form 'secondary=@%s' '%s'", str(timeout), primary, secondary, url) response = requests.post(url, data = {"timeout": timeout}, \ files = {"primary": open(primary, "rb"), "secondary": open(secondary, "rb")}) if response.status_code == 200: handle, path = tempfile.mkstemp(suffix = ".bindiff.sqlite3") with os.fdopen(handle, "wb") as f: map(f.write, response.iter_content(1024)) return path else: log.error("Bindiff server responded with status code %d: %s", response.status_code, response.content) return None
Example #9
Source Project: fine-lm Author: akzaidi File: generator_utils_test.py License: MIT License | 6 votes |
def testGenerateFiles(self): tmp_dir = self.get_temp_dir() (_, tmp_file_path) = tempfile.mkstemp(dir=tmp_dir) tmp_file_name = os.path.basename(tmp_file_path) # Generate a trivial file and assert the file exists. def test_generator(): yield {"inputs": [1], "target": [1]} filenames = generator_utils.train_data_filenames(tmp_file_name, tmp_dir, 1) generator_utils.generate_files(test_generator(), filenames) self.assertTrue(tf.gfile.Exists(tmp_file_path + "-train-00000-of-00001")) # Clean up. os.remove(tmp_file_path + "-train-00000-of-00001") os.remove(tmp_file_path)
Example #10
Source Project: fine-lm Author: akzaidi File: generator_utils_test.py License: MIT License | 6 votes |
def testGunzipFile(self): tmp_dir = self.get_temp_dir() (_, tmp_file_path) = tempfile.mkstemp(dir=tmp_dir) # Create a test zip file and unzip it. with gzip.open(tmp_file_path + ".gz", "wb") as gz_file: gz_file.write(bytes("test line", "utf-8")) generator_utils.gunzip_file(tmp_file_path + ".gz", tmp_file_path + ".txt") # Check that the unzipped result is as expected. lines = [] for line in io.open(tmp_file_path + ".txt", "rb"): lines.append(line.decode("utf-8").strip()) self.assertEqual(len(lines), 1) self.assertEqual(lines[0], "test line") # Clean up. os.remove(tmp_file_path + ".gz") os.remove(tmp_file_path + ".txt") os.remove(tmp_file_path)
Example #11
Source Project: epr Author: wustho File: epr.py License: MIT License | 6 votes |
def open_media(scr, epub, src): sfx = os.path.splitext(src)[1] fd, path = tempfile.mkstemp(suffix=sfx) try: with os.fdopen(fd, "wb") as tmp: tmp.write(epub.file.read(src)) # run(VWR +" "+ path, shell=True) subprocess.call( VWR + [path], # shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ) k = scr.getch() finally: os.remove(path) return k
Example #12
Source Project: dockerfiles Author: floydhub File: dataset.py License: Apache License 2.0 | 6 votes |
def download(directory, filename): """Download (and unzip) a file from the MNIST dataset if not already done.""" filepath = os.path.join(directory, filename) if tf.gfile.Exists(filepath): return filepath if not tf.gfile.Exists(directory): tf.gfile.MakeDirs(directory) # CVDF mirror of http://yann.lecun.com/exdb/mnist/ url = 'https://storage.googleapis.com/cvdf-datasets/mnist/' + filename + '.gz' _, zipped_filepath = tempfile.mkstemp(suffix='.gz') print('Downloading %s to %s' % (url, zipped_filepath)) urllib.request.urlretrieve(url, zipped_filepath) with gzip.open(zipped_filepath, 'rb') as f_in, \ tf.gfile.Open(filepath, 'wb') as f_out: shutil.copyfileobj(f_in, f_out) os.remove(zipped_filepath) return filepath
Example #13
Source Project: dockerfiles Author: floydhub File: dataset.py License: Apache License 2.0 | 6 votes |
def download(directory, filename): """Download (and unzip) a file from the MNIST dataset if not already done.""" filepath = os.path.join(directory, filename) if tf.gfile.Exists(filepath): return filepath if not tf.gfile.Exists(directory): tf.gfile.MakeDirs(directory) # CVDF mirror of http://yann.lecun.com/exdb/mnist/ url = 'https://storage.googleapis.com/cvdf-datasets/mnist/' + filename + '.gz' _, zipped_filepath = tempfile.mkstemp(suffix='.gz') print('Downloading %s to %s' % (url, zipped_filepath)) urllib.request.urlretrieve(url, zipped_filepath) with gzip.open(zipped_filepath, 'rb') as f_in, \ tf.gfile.Open(filepath, 'wb') as f_out: shutil.copyfileobj(f_in, f_out) os.remove(zipped_filepath) return filepath
Example #14
Source Project: cutout Author: jojoin File: filecache.py License: MIT License | 6 votes |
def set(self, key, value, timeout=None): if timeout is None: timeout = self.default_timeout filename = self._get_filename(key) self._prune() try: fd, tmp = tempfile.mkstemp(suffix=self._fs_transaction_suffix, dir=self._path) f = os.fdopen(fd, 'wb') try: pickle.dump(int(time() + timeout), f, 1) pickle.dump(value, f, pickle.HIGHEST_PROTOCOL) finally: f.close() rename(tmp, filename) os.chmod(filename, self._mode) except (IOError, OSError): pass
Example #15
Source Project: S4 Author: MichaelAquilina File: local.py License: GNU General Public License v3.0 | 6 votes |
def put(self, key, sync_object, callback=None): path = os.path.join(self.path, key) self.ensure_path(path) BUFFER_SIZE = 4096 fd, temp_path = tempfile.mkstemp() try: with open(temp_path, "wb") as fp_1: while True: data = sync_object.fp.read(BUFFER_SIZE) fp_1.write(data) if callback is not None: callback(len(data)) if len(data) < BUFFER_SIZE: break shutil.move(temp_path, path) except Exception: os.remove(temp_path) raise finally: os.close(fd) self.set_remote_timestamp(key, sync_object.timestamp)
Example #16
Source Project: razzy-spinner Author: rafasashi File: util.py License: GNU General Public License v3.0 | 6 votes |
def cache_to_tempfile(cls, sequence, delete_on_gc=True): """ Write the given sequence to a temporary file as a pickle corpus; and then return a ``PickleCorpusView`` view for that temporary corpus file. :param delete_on_gc: If true, then the temporary file will be deleted whenever this object gets garbage-collected. """ try: fd, output_file_name = tempfile.mkstemp('.pcv', 'nltk-') output_file = os.fdopen(fd, 'wb') cls.write(sequence, output_file) output_file.close() return PickleCorpusView(output_file_name, delete_on_gc) except (OSError, IOError) as e: raise ValueError('Error while creating temp file: %s' % e) ###################################################################### #{ Block Readers ######################################################################
Example #17
Source Project: me-ica Author: ME-ICA File: linear_flows.py License: GNU Lesser General Public License v2.1 | 6 votes |
def dump(self, filename=None): """ Save a pickle dump of the crashing object on filename. If filename is None, the crash dump is saved on a file created by the tempfile module. Return the filename. """ if filename is None: # This 'temporary file' should actually stay 'forever', i.e. until # deleted by the user. (fd, filename)=_tempfile.mkstemp(suffix=".pic", prefix="MDPcrash_") fl = _os.fdopen(fd, 'w+b', -1) else: fl = open(filename, 'w+b', -1) _cPickle.dump(self.crashing_obj, fl) fl.close() return filename
Example #18
Source Project: trelby Author: trelby File: gutil.py License: GNU General Public License v2.0 | 6 votes |
def showTempPDF(pdfData, cfgGl, mainFrame): try: try: util.removeTempFiles(misc.tmpPrefix) fd, filename = tempfile.mkstemp(prefix = misc.tmpPrefix, suffix = ".pdf") try: os.write(fd, pdfData) finally: os.close(fd) util.showPDF(filename, cfgGl, mainFrame) except IOError, (errno, strerror): raise MiscError("IOError: %s" % strerror) except TrelbyError, e: wx.MessageBox("Error writing temporary PDF file: %s" % e, "Error", wx.OK, mainFrame)
Example #19
Source Project: faxplus-python Author: alohi File: api_client.py License: MIT License | 6 votes |
def __deserialize_file(self, response): """Deserializes body to file Saves response body into a file in a temporary folder, using the filename from the `Content-Disposition` header if provided. :param response: RESTResponse. :return: file path. """ fd, path = tempfile.mkstemp(dir=self.configuration.temp_folder_path) os.close(fd) os.remove(path) content_disposition = response.getheader("Content-Disposition") if content_disposition: filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', content_disposition).group(1) path = os.path.join(os.path.dirname(path), filename) with open(path, "wb") as f: f.write(response.data) return path
Example #20
Source Project: misp42splunk Author: remg427 File: template.py License: GNU Lesser General Public License v3.0 | 6 votes |
def _compile_module_file(template, text, filename, outputpath, module_writer): source, lexer = _compile( template, text, filename, generate_magic_comment=True ) if isinstance(source, compat.text_type): source = source.encode(lexer.encoding or "ascii") if module_writer: module_writer(source, outputpath) else: # make tempfiles in the same location as the ultimate # location. this ensures they're on the same filesystem, # avoiding synchronization issues. (dest, name) = tempfile.mkstemp(dir=os.path.dirname(outputpath)) os.write(dest, source) os.close(dest) shutil.move(name, outputpath)
Example #21
Source Project: misp42splunk Author: remg427 File: template.py License: GNU Lesser General Public License v3.0 | 6 votes |
def _compile_module_file(template, text, filename, outputpath, module_writer): source, lexer = _compile( template, text, filename, generate_magic_comment=True ) if isinstance(source, compat.text_type): source = source.encode(lexer.encoding or "ascii") if module_writer: module_writer(source, outputpath) else: # make tempfiles in the same location as the ultimate # location. this ensures they're on the same filesystem, # avoiding synchronization issues. (dest, name) = tempfile.mkstemp(dir=os.path.dirname(outputpath)) os.write(dest, source) os.close(dest) shutil.move(name, outputpath)
Example #22
Source Project: qutebrowser Author: qutebrowser File: link_pyqt.py License: GNU General Public License v3.0 | 6 votes |
def run_py(executable, *code): """Run the given python code with the given executable.""" if os.name == 'nt' and len(code) > 1: # Windows can't do newlines in arguments... oshandle, filename = tempfile.mkstemp() with os.fdopen(oshandle, 'w') as f: f.write('\n'.join(code)) cmd = [executable, filename] try: ret = subprocess.run(cmd, universal_newlines=True, check=True, stdout=subprocess.PIPE).stdout finally: os.remove(filename) else: cmd = [executable, '-c', '\n'.join(code)] ret = subprocess.run(cmd, universal_newlines=True, check=True, stdout=subprocess.PIPE).stdout return ret.rstrip()
Example #23
Source Project: BASS Author: Cisco-Talos File: bindiff.py License: GNU General Public License v2.0 | 5 votes |
def bindiff_pickle_export(self, sample, is_64_bit = True, timeout = None): """ Load a sample into IDA Pro, perform autoanalysis and export a pickle file. :param sample: The sample's path :param is_64_bit: If the sample needs to be analyzed by the 64 bit version of IDA :param timeout: Timeout for the analysis in seconds :return: The file name of the exported pickle database. The file needs to be deleted by the caller. Returns None on error. """ data_to_send = { "timeout": timeout, "is_64_bit": is_64_bit} url = "%s/binexport_pickle" % next(self._urls) log.debug("curl -XPOST --data '%s' '%s'", json.dumps(data_to_send), url) response = requests.post(url, data = data_to_send, files = {os.path.basename(sample): open(sample, "rb")}) if response.status_code == 200: handle_tar, path_tar = tempfile.mkstemp(suffix = ".tar.gz") with os.fdopen(handle_tar, "wb") as f: map(f.write, response.iter_content(1024)) directory = tempfile.mkdtemp() subprocess.check_call(["tar", "xf", path_tar], cwd = directory) handle_bindiff, output_bindiff = tempfile.mkstemp(suffix = ".BinExport") with os.fdopen(handle_bindiff, "wb") as f: with open(os.path.join(directory, "output.BinExport"), "rb") as f2: shutil.copyfileobj(f2, f) handle_pickle, output_pickle = tempfile.mkstemp(suffix = ".pickle") with os.fdopen(handle_pickle, "wb") as f: with open(os.path.join(directory, "output.pickle"), "rb") as f2: shutil.copyfileobj(f2, f) os.unlink(path_tar) shutil.rmtree(directory) return output_bindiff, output_pickle else: log.error("Bindiff server responded with status code %d: %s", response.status_code, response.content) return None
Example #24
Source Project: ALF Author: blackberry File: Radamsa.py License: Apache License 2.0 | 5 votes |
def test_fuzz_file(self): f = RadamsaFuzzer() f_d, f_n = tempfile.mkstemp() test_case = "Fuzztron 2000 ALF <test> (12x123x1234)" with os.fdopen(f_d, "wb") as fp: fp.write(test_case) for _ in range(10): f.fuzz_file(f_n, "tmp_fuzz_radamsa") with open("tmp_fuzz_radamsa", "rb") as fp: self.assertNotEqual(fp.read(), test_case) os.remove(f_n) os.remove("tmp_fuzz_radamsa")
Example #25
Source Project: cherrypy Author: cherrypy File: test_wsgi_unix_socket.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def usocket_path(): fd, path = tempfile.mkstemp('cp_test.sock') os.close(fd) os.remove(path) return path
Example #26
Source Project: python_labview_automation Author: ni File: labview.py License: MIT License | 5 votes |
def create_temp_ini(self, options={}): ini = ConfigParser.RawConfigParser() # iterate over sections for section, tokens in options.iteritems(): ini.add_section(section) for key, value in tokens.iteritems(): ini.set(section, key, value) fd, path = tempfile.mkstemp(suffix=".ini") with os.fdopen(fd, 'w') as f: ini.write(f) return path
Example #27
Source Project: ToonRooter Author: martenjacobs File: rooter.py License: MIT License | 5 votes |
def create_payload_tar(self): tar_path = tempfile.mkstemp(suffix=".tar.gz")[1] ssh_key = self._ssh_pubkey_data with tarfile.open(tar_path, "w:gz") as tar: tar.add('payload/', arcname='payload') ssh_key_str = StringIO.StringIO(ssh_key) info = tarfile.TarInfo(name="payload/id_rsa.pub") info.size=len(ssh_key) tar.addfile(tarinfo=info, fileobj=StringIO.StringIO(ssh_key)) return tar_path
Example #28
Source Project: sqs-s3-logger Author: ellimilial File: tests.py License: Apache License 2.0 | 5 votes |
def test_handler_uploads_queue_contents_to_bucket(self): self._send_messages_to_the_queue(1) b = self.environment.get_create_bucket() self.assertEqual(0, sum(1 for _ in b.objects.all())) _, temp_filepath = tempfile.mkstemp() os.environ.update({ 'QUEUE_NAME': self.environment._queue_name, 'BUCKET_NAME': self.environment._bucket_name, 'TEMP_FILE_PATH': temp_filepath }) handler(None, None) self.assertEqual(1, sum(1 for _ in b.objects.all()))
Example #29
Source Project: Paradrop Author: ParadropLabs File: pdmock.py License: Apache License 2.0 | 5 votes |
def writeTempFile(data): """ Write data to a temporary file and return path to that file. """ fd, path = tempfile.mkstemp() os.write(fd, data) os.close(fd) return path
Example #30
Source Project: Paradrop Author: ParadropLabs File: test_pdosq.py License: Apache License 2.0 | 5 votes |
def test_read_yaml_file(): fd, path = tempfile.mkstemp() os.close(fd) with open(path, "w") as output: output.write("value: 42\n") result = pdosq.read_yaml_file(path) assert result['value'] == 42 pdosq.safe_remove(path) assert pdosq.read_yaml_file(path, default=None) is None assert pdosq.read_yaml_file(path, default={}) == {}