本文整理匯總了Python中tempfile._get_default_tempdir方法的典型用法代碼示例。如果您正苦於以下問題:Python tempfile._get_default_tempdir方法的具體用法?Python tempfile._get_default_tempdir怎麽用?Python tempfile._get_default_tempdir使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tempfile
的用法示例。
在下文中一共展示了tempfile._get_default_tempdir方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: upx_unpack
# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import _get_default_tempdir [as 別名]
def upx_unpack(self, seed=None):
# dump bytez to a temporary file
tmpfilename = os.path.join(
tempfile._get_default_tempdir(), next(tempfile._get_candidate_names()))
with open(tmpfilename, 'wb') as outfile:
outfile.write(self.bytez)
with open(os.devnull, 'w') as DEVNULL:
retcode = subprocess.call(
['upx', tmpfilename, '-d', '-o', tmpfilename + '_unpacked'], stdout=DEVNULL, stderr=DEVNULL)
os.unlink(tmpfilename)
if retcode == 0: # sucessfully unpacked
with open(tmpfilename + '_unpacked', 'rb') as result:
self.bytez = result.read()
os.unlink(tmpfilename + '_unpacked')
return self.bytez
示例2: test_barracuda_converter
# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import _get_default_tempdir [as 別名]
def test_barracuda_converter():
path_prefix = os.path.dirname(os.path.abspath(__file__))
tmpfile = os.path.join(
tempfile._get_default_tempdir(), next(tempfile._get_candidate_names()) + ".nn"
)
# make sure there are no left-over files
if os.path.isfile(tmpfile):
os.remove(tmpfile)
tf2bc.convert(path_prefix + "/BasicLearning.pb", tmpfile)
# test if file exists after conversion
assert os.path.isfile(tmpfile)
# currently converter produces small output file even if input file is empty
# 100 bytes is high enough to prove that conversion was successful
assert os.path.getsize(tmpfile) > 100
# cleanup
os.remove(tmpfile)
示例3: wfuzz_me_test_generator_previous_session
# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import _get_default_tempdir [as 別名]
def wfuzz_me_test_generator_previous_session(prev_session_cli, next_session_cli, expected_list):
def test(self):
temp_name = next(tempfile._get_candidate_names())
defult_tmp_dir = tempfile._get_default_tempdir()
filename = os.path.join(defult_tmp_dir, temp_name)
# first session
with wfuzz.get_session(prev_session_cli) as s:
ret_list = [x.eval(x._description) if x._description else x.description for x in s.fuzz(save=filename)]
# second session wfuzzp as payload
with wfuzz.get_session(next_session_cli.replace("$$PREVFILE$$", filename)) as s:
ret_list = [x.eval(x._description) if x._description else x.description for x in s.fuzz()]
self.assertEqual(sorted(ret_list), sorted(expected_list))
return test
示例4: upx_pack
# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import _get_default_tempdir [as 別名]
def upx_pack(self, seed=None):
# tested with UPX 3.91
random.seed(seed)
tmpfilename = os.path.join(
tempfile._get_default_tempdir(), next(tempfile._get_candidate_names()))
# dump bytez to a temporary file
with open(tmpfilename, 'wb') as outfile:
outfile.write(self.bytez)
options = ['--force', '--overlay=copy']
compression_level = random.randint(1, 9)
options += ['-{}'.format(compression_level)]
# --exact
# compression levels -1 to -9
# --overlay=copy [default]
# optional things:
# --compress-exports=0/1
# --compress-icons=0/1/2/3
# --compress-resources=0/1
# --strip-relocs=0/1
options += ['--compress-exports={}'.format(random.randint(0, 1))]
options += ['--compress-icons={}'.format(random.randint(0, 3))]
options += ['--compress-resources={}'.format(random.randint(0, 1))]
options += ['--strip-relocs={}'.format(random.randint(0, 1))]
with open(os.devnull, 'w') as DEVNULL:
retcode = subprocess.call(
['upx'] + options + [tmpfilename, '-o', tmpfilename + '_packed'], stdout=DEVNULL, stderr=DEVNULL)
os.unlink(tmpfilename)
if retcode == 0: # successfully packed
with open(tmpfilename + '_packed', 'rb') as infile:
self.bytez = infile.read()
os.unlink(tmpfilename + '_packed')
return self.bytez
示例5: get_new_temp_file_path
# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import _get_default_tempdir [as 別名]
def get_new_temp_file_path(extension):
tmp_dir = tempfile._get_default_tempdir()
tmp_name = next(tempfile._get_candidate_names())
tmp_file = os.path.join(tmp_dir, tmp_name + "." + extension)
return tmp_file
示例6: test_no_files_left_behind
# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import _get_default_tempdir [as 別名]
def test_no_files_left_behind(self):
# use a private empty directory
our_temp_directory = tempfile.mkdtemp()
try:
# force _get_default_tempdir() to consider our empty directory
def our_candidate_list():
return [our_temp_directory]
with support.swap_attr(tempfile, "_candidate_tempdir_list",
our_candidate_list):
# verify our directory is empty after _get_default_tempdir()
tempfile._get_default_tempdir()
self.assertEqual(os.listdir(our_temp_directory), [])
def raise_OSError(*args, **kwargs):
raise OSError(-1)
with support.swap_attr(io, "open", raise_OSError):
# test again with failing io.open()
with self.assertRaises(IOError) as cm:
tempfile._get_default_tempdir()
self.assertEqual(cm.exception.errno, errno.ENOENT)
self.assertEqual(os.listdir(our_temp_directory), [])
def bad_writer(*args, **kwargs):
fp = orig_open(*args, **kwargs)
fp.write = raise_OSError
return fp
with support.swap_attr(io, "open", bad_writer) as orig_open:
# test again with failing write()
with self.assertRaises(IOError) as cm:
tempfile._get_default_tempdir()
self.assertEqual(cm.exception.errno, errno.ENOENT)
self.assertEqual(os.listdir(our_temp_directory), [])
finally:
shutil.rmtree(our_temp_directory)
示例7: test_no_files_left_behind
# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import _get_default_tempdir [as 別名]
def test_no_files_left_behind(self):
# use a private empty directory
our_temp_directory = tempfile.mkdtemp()
try:
# force _get_default_tempdir() to consider our empty directory
def our_candidate_list():
return [our_temp_directory]
with support.swap_attr(tempfile, "_candidate_tempdir_list",
our_candidate_list):
# verify our directory is empty after _get_default_tempdir()
tempfile._get_default_tempdir()
self.assertEqual(os.listdir(our_temp_directory), [])
def raise_OSError(*args, **kwargs):
raise OSError(-1)
with support.swap_attr(io, "open", raise_OSError):
# test again with failing io.open()
with self.assertRaises(IOError) as cm:
tempfile._get_default_tempdir()
self.assertEqual(cm.exception.errno, errno.ENOENT)
self.assertEqual(os.listdir(our_temp_directory), [])
open = io.open
def bad_writer(*args, **kwargs):
fp = open(*args, **kwargs)
fp.write = raise_OSError
return fp
with support.swap_attr(io, "open", bad_writer):
# test again with failing write()
with self.assertRaises(IOError) as cm:
tempfile._get_default_tempdir()
self.assertEqual(cm.exception.errno, errno.ENOENT)
self.assertEqual(os.listdir(our_temp_directory), [])
finally:
shutil.rmtree(our_temp_directory)
示例8: render_point_cloud
# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import _get_default_tempdir [as 別名]
def render_point_cloud(point_cloud, cfg):
"""
Wraps the call to blender to render the image
"""
cfg = edict(cfg)
temp_dir = tempfile._get_default_tempdir()
temp_name = next(tempfile._get_candidate_names())
in_file = f"{temp_dir}/{temp_name}.npz"
point_cloud_save = np.reshape(point_cloud, (1, -1, 3))
np.savez(in_file, point_cloud_save)
temp_name = next(tempfile._get_candidate_names())
out_file = f"{temp_dir}/{temp_name}.png"
args = build_command_line_args([["in_file", in_file],
["out_file", out_file],
["vis_azimuth", cfg.vis_azimuth],
["vis_elevation", cfg.vis_elevation],
["vis_dist", cfg.vis_dist],
["cycles_samples", cfg.render_cycles_samples],
["like_train_data", True],
["voxels", False],
["colored_subsets", False],
["image_size", cfg.render_image_size]],
as_string=False)
full_args = [blender_exec, "--background", "-P", python_script, "--"] + args
subprocess.check_call(full_args, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
image = imageio.imread(out_file)
os.remove(in_file)
os.remove(out_file)
return image
示例9: generate_tmp_filename
# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import _get_default_tempdir [as 別名]
def generate_tmp_filename(extension):
return tempfile._get_default_tempdir() + "/" + next(tempfile._get_candidate_names()) + "." + extension
示例10: test_wanted_dirs
# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import _get_default_tempdir [as 別名]
def test_wanted_dirs(self):
# _candidate_tempdir_list contains the expected directories
# Make sure the interesting environment variables are all set.
with support.EnvironmentVarGuard() as env:
for envname in 'TMPDIR', 'TEMP', 'TMP':
dirname = os.getenv(envname)
if not dirname:
env[envname] = os.path.abspath(envname)
cand = tempfile._candidate_tempdir_list()
for envname in 'TMPDIR', 'TEMP', 'TMP':
dirname = os.getenv(envname)
if not dirname: raise ValueError
self.assertIn(dirname, cand)
try:
dirname = os.getcwd()
except (AttributeError, OSError):
dirname = os.curdir
self.assertIn(dirname, cand)
# Not practical to try to verify the presence of OS-specific
# paths in this list.
# We test _get_default_tempdir some more by testing gettempdir.
示例11: test_no_files_left_behind
# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import _get_default_tempdir [as 別名]
def test_no_files_left_behind(self):
# use a private empty directory
with tempfile.TemporaryDirectory() as our_temp_directory:
# force _get_default_tempdir() to consider our empty directory
def our_candidate_list():
return [our_temp_directory]
with support.swap_attr(tempfile, "_candidate_tempdir_list",
our_candidate_list):
# verify our directory is empty after _get_default_tempdir()
tempfile._get_default_tempdir()
self.assertEqual(os.listdir(our_temp_directory), [])
def raise_OSError(*args, **kwargs):
raise OSError()
with support.swap_attr(io, "open", raise_OSError):
# test again with failing io.open()
with self.assertRaises(FileNotFoundError):
tempfile._get_default_tempdir()
self.assertEqual(os.listdir(our_temp_directory), [])
open = io.open
def bad_writer(*args, **kwargs):
fp = open(*args, **kwargs)
fp.write = raise_OSError
return fp
with support.swap_attr(io, "open", bad_writer):
# test again with failing write()
with self.assertRaises(FileNotFoundError):
tempfile._get_default_tempdir()
self.assertEqual(os.listdir(our_temp_directory), [])
示例12: mktemppath
# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import _get_default_tempdir [as 別名]
def mktemppath():
try:
path = os.path.join(
tempfile._get_default_tempdir(), next(tempfile._get_candidate_names())
)
yield path
finally:
try:
os.unlink(path)
except OSError as e:
current_app.logger.debug("No file {0}".format(path))
示例13: test_file_loading1
# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import _get_default_tempdir [as 別名]
def test_file_loading1(self):
data = self.data[:1000]
directory = tempfile._get_default_tempdir()
filename = next(tempfile._get_candidate_names())
filename = directory + os.sep + filename + ".txt"
np.savetxt(filename, data)
consumer = ChainConsumer()
consumer.add_chain(filename)
summary = consumer.analysis.get_summary()
actual = np.array(list(summary.values())[0])
assert np.abs(actual[1] - 5.0) < 0.5
示例14: test_file_loading2
# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import _get_default_tempdir [as 別名]
def test_file_loading2(self):
data = self.data[:1000]
directory = tempfile._get_default_tempdir()
filename = next(tempfile._get_candidate_names())
filename = directory + os.sep + filename + ".npy"
np.save(filename, data)
consumer = ChainConsumer()
consumer.add_chain(filename)
summary = consumer.analysis.get_summary()
actual = np.array(list(summary.values())[0])
assert np.abs(actual[1] - 5.0) < 0.5
示例15: process
# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import _get_default_tempdir [as 別名]
def process(self, fuzzresult):
temp_name = next(tempfile._get_candidate_names())
defult_tmp_dir = tempfile._get_default_tempdir()
filename = os.path.join(defult_tmp_dir, temp_name + ".png")
subprocess.call(['cutycapt', '--url=%s' % pipes.quote(fuzzresult.url), '--out=%s' % filename])
self.add_result("Screnshot taken, output at %s" % filename)