本文整理汇总了Python中tempfile.mkstemp函数的典型用法代码示例。如果您正苦于以下问题:Python mkstemp函数的具体用法?Python mkstemp怎么用?Python mkstemp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了mkstemp函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compile_and_load
def compile_and_load(src_files, obj_files=[], cc="g++", flags=["O3", "Wall"], includes=[], links=[], defs=[]):
""" Compile and load a shared object from a source file
\param src_files list of source fiels (eg. ['~/src/foo.cc'])
\param cc the path to the c++ compiler
\param obj_files name of files to compile (eg. ['~/obj/foo.o'])
\param flags list of falgs for the compiler (eg. ['O3', 'Wall'])
\param includes list of directories to include (eg. ['~/includes/'])
\param links list of libraries to link with (eg. ['pthread', 'gtest'])
\param defs list of names to define with -D (eg. ['ENABLE_FOO'])
\return (lib, fin) link to the library and a function to call to close the library
"""
__, obj_name = tempfile.mkstemp(suffix=".o", dir=TEMP_DIR)
os.close(__)
__, lib_name = tempfile.mkstemp(suffix=".so", dir=TEMP_DIR)
os.close(__)
compile_bin(obj_name, src_files, cc=cc, flags=flags, includes=includes, links=links, defs=defs, lib=True)
# add the newly compiled object file to the list of objects for the lib
obj_files = list(obj_files)
obj_files.append(obj_name)
compile_so(lib_name, obj_files, cc=cc, links=links)
def finalize():
if os.path.exists(obj_name):
os.unlink(obj_name)
if os.path.exists(lib_name):
os.unlink(lib_name)
try:
lib = ctypes.CDLL(lib_name)
return lib, finalize
except OSError:
print "Failed link with library, source files:"
print ", ".join(src_files)
raise
示例2: _execute
def _execute(cmds):
import subprocess
stderrPath = tempfile.mkstemp()[1]
stdoutPath = tempfile.mkstemp()[1]
stderrFile = open(stderrPath, "w")
stdoutFile = open(stdoutPath, "w")
# get the os.environ
env = _makeEnviron()
# make a string of escaped commands
cmds = subprocess.list2cmdline(cmds)
# go
popen = subprocess.Popen(cmds, stderr=stderrFile, stdout=stdoutFile, env=env, shell=True)
popen.wait()
# get the output
stderrFile.close()
stdoutFile.close()
stderrFile = open(stderrPath, "r")
stdoutFile = open(stdoutPath, "r")
stderr = stderrFile.read()
stdout = stdoutFile.read()
stderrFile.close()
stdoutFile.close()
# trash the temp files
os.remove(stderrPath)
os.remove(stdoutPath)
# done
return stderr, stdout
示例3: print_xcf
def print_xcf(self, filename_or_obj, *args, **kwargs):
"Writes the figure to a GIMP XCF image file"
# If filename_or_obj is a file-like object we need a temporary file for
# GIMP's output too...
if is_string(filename_or_obj):
out_temp_handle, out_temp_name = None, filename_or_obj
else:
out_temp_handle, out_temp_name = tempfile.mkstemp(suffix='.xcf')
try:
# Create a temporary file and write the "layer" to it as a PNG
in_temp_handle, in_temp_name = tempfile.mkstemp(suffix='.png')
try:
FigureCanvasAgg.print_png(self, in_temp_name, *args, **kwargs)
run_gimp_script(
SINGLE_LAYER_SCRIPT.format(
input=quote_string(in_temp_name),
output=quote_string(out_temp_name)))
finally:
os.close(in_temp_handle)
os.unlink(in_temp_name)
finally:
if out_temp_handle:
os.close(out_temp_handle)
# If we wrote the XCF to a temporary file, write its content to
# the file-like object we were given (the copy is chunked as
# XCF files can get pretty big)
with open(out_temp_name, 'rb') as source:
for chunk in iter(lambda: source.read(131072), ''):
filename_or_obj.write(chunk)
os.unlink(out_temp_name)
示例4: test_pipeline_with_temp
def test_pipeline_with_temp(self):
input_f = tempfile.mkstemp(suffix='capsul_input.txt')
os.close(input_f[0])
input_name = input_f[1]
open(input_name, 'w').write('this is my input data\n')
output_f = tempfile.mkstemp(suffix='capsul_output.txt')
os.close(output_f[0])
output_name = output_f[1]
#os.unlink(output_name)
try:
self.pipeline.input_image = input_name
self.pipeline.output_image = output_name
# run sequentially
self.pipeline()
# test
self.assertTrue(os.path.exists(output_name))
self.assertEqual(open(input_name).read(), open(output_name).read())
finally:
try:
os.unlink(input_name)
except: pass
try:
os.unlink(output_name)
except: pass
示例5: take_screenshot
def take_screenshot(self, filename):
"""Take a screenshot and save it to 'filename'"""
# make sure filename is unicode
filename = ensure_unicode(filename, "utf-8")
if get_platform() == "windows":
# use win32api to take screenshot
# create temp file
f, imgfile = tempfile.mkstemp(u".bmp", filename)
os.close(f)
mswin.screenshot.take_screenshot(imgfile)
else:
# use external app for screen shot
screenshot = self.get_external_app("screen_shot")
if screenshot is None or screenshot.prog == "":
raise Exception(_("You must specify a Screen Shot program in Application Options"))
# create temp file
f, imgfile = tempfile.mkstemp(".png", filename)
os.close(f)
proc = subprocess.Popen([screenshot.prog, imgfile])
if proc.wait() != 0:
raise OSError("Exited with error")
if not os.path.exists(imgfile):
# catch error if image is not created
raise Exception(_("The screenshot program did not create the necessary image file '%s'") % imgfile)
return imgfile
示例6: run_shell_cmd
def run_shell_cmd(cmd, echo=True):
"""
Run a command in a sub-shell, capturing stdout and stderr
to temporary files that are then read.
"""
_, stdout_f = tempfile.mkstemp()
_, stderr_f = tempfile.mkstemp()
print("Running command")
print(cmd)
p = subprocess.Popen(
'{} >{} 2>{}'.format(cmd, stdout_f, stderr_f), shell=True)
p.wait()
with open(stdout_f) as f:
stdout = f.read()
os.remove(stdout_f)
with open(stderr_f) as f:
stderr = f.read()
os.remove(stderr_f)
if echo:
print("stdout:")
print(stdout)
print("stderr:")
print(stderr)
return stdout, stderr
示例7: test_cache
def test_cache(self):
"""Test the caching mechanism in the reporter."""
length = random.randint(1, 30)
exit_code = random.randint(0, 3)
threshold = random.randint(0, 10)
message = ''.join(random.choice(string.printable) for x in range(length))
message = message.rstrip()
(handle, filename) = tempfile.mkstemp()
os.unlink(filename)
os.close(handle)
reporter = NagiosReporter('test_cache', filename, threshold, self.nagios_user)
nagios_exit = [NAGIOS_EXIT_OK, NAGIOS_EXIT_WARNING, NAGIOS_EXIT_CRITICAL, NAGIOS_EXIT_UNKNOWN][exit_code]
reporter.cache(nagios_exit, message)
(handle, output_filename) = tempfile.mkstemp()
os.close(handle)
try:
old_stdout = sys.stdout
buffer = StringIO.StringIO()
sys.stdout = buffer
reporter_test = NagiosReporter('test_cache', filename, threshold, self.nagios_user)
reporter_test.report_and_exit()
except SystemExit, err:
line = buffer.getvalue().rstrip()
sys.stdout = old_stdout
buffer.close()
self.assertTrue(err.code == nagios_exit[0])
self.assertTrue(line == "%s %s" % (nagios_exit[1], message))
示例8: cmpIgProfReport
def cmpIgProfReport(outdir,file1,file2,IgProfMemOpt=""):
(tfile1, tfile2) = ("", "")
try:
# don't make temp files in /tmp because it's never bloody big enough
(th1, tfile1) = tmp.mkstemp(prefix=os.path.join(outdir,"igprofRegressRep."))
(th2, tfile2) = tmp.mkstemp(prefix=os.path.join(outdir,"igprofRegressRep."))
os.close(th1)
os.close(th2)
os.remove(tfile1)
os.remove(tfile2)
ungzip2(file1,tfile1)
ungzip2(file2,tfile2)
perfreport(1,tfile1,tfile2,outdir,IgProfMemOpt)
os.remove(tfile1)
os.remove(tfile2)
except OSError as detail:
raise PerfReportErr("WARNING: The OS returned the following error when comparing %s and %s\n%s" % (file1,file2,str(detail)))
if os.path.exists(tfile1):
os.remove(tfile1)
if os.path.exists(tfile2):
os.remove(tfile2)
except IOError as detail:
raise PerfReportErr("IOError: When comparing %s and %s using temporary files %s and %s. Error message:\n%s" % (file1,file2,tfile1,tfile2,str(detail)))
if os.path.exists(tfile1):
os.remove(tfile1)
if os.path.exists(tfile2):
os.remove(tfile2)
示例9: shellScriptInWindow
def shellScriptInWindow(c,script):
if sys.platform == 'darwin':
#@ << write script to temporary MacOS file >>
#@+node:ekr.20040915105758.22:<< write script to temporary MacOS file >>
handle, path = tempfile.mkstemp(text=True)
directory = c.frame.openDirectory
script = ("cd %s\n" % directory) + script + '\n' + ("rm -f %s\n" % path)
os.write(handle, script)
os.close(handle)
os.chmod(path, 0700)
#@nonl
#@-node:ekr.20040915105758.22:<< write script to temporary MacOS file >>
#@nl
os.system("open -a /Applications/Utilities/Terminal.app " + path)
elif sys.platform == 'win32':
g.es("shellScriptInWindow not ready for Windows",color='red')
else:
#@ << write script to temporary Unix file >>
#@+node:ekr.20040915105758.25:<< write script to temporary Unix file >>
handle, path = tempfile.mkstemp(text=True)
directory = c.frame.openDirectory
script = ("cd %s\n" % directory) + script + '\n' + ("rm -f %s\n" % path)
os.write(handle, script)
os.close(handle)
os.chmod(path, 0700)
#@nonl
#@-node:ekr.20040915105758.25:<< write script to temporary Unix file >>
#@nl
os.system("xterm -e sh " + path)
示例10: tempfilter
def tempfilter(s, cmd):
'''filter string S through a pair of temporary files with CMD.
CMD is used as a template to create the real command to be run,
with the strings INFILE and OUTFILE replaced by the real names of
the temporary files generated.'''
inname, outname = None, None
try:
infd, inname = tempfile.mkstemp(prefix='hg-filter-in-')
fp = os.fdopen(infd, 'wb')
fp.write(s)
fp.close()
outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-')
os.close(outfd)
cmd = cmd.replace('INFILE', inname)
cmd = cmd.replace('OUTFILE', outname)
code = os.system(cmd)
if sys.platform == 'OpenVMS' and code & 1:
code = 0
if code:
raise Abort(_("command '%s' failed: %s") %
(cmd, explain_exit(code)))
return open(outname, 'rb').read()
finally:
try:
if inname:
os.unlink(inname)
except:
pass
try:
if outname:
os.unlink(outname)
except:
pass
示例11: svg_formatter
def svg_formatter(data, format) :
""" Generate a logo in Scalable Vector Graphics (SVG) format.
Requires the program 'pdf2svg' be installed.
"""
pdf = pdf_formatter(data, format)
try:
command = find_command('pdf2svg')
except EnvironmentError:
raise EnvironmentError("Scalable Vector Graphics (SVG) format requires the program 'pdf2svg'. "
"Cannot find 'pdf2svg' on search path.")
import tempfile
fpdfi, fname_pdf = tempfile.mkstemp(suffix=".pdf")
fsvgi, fname_svg = tempfile.mkstemp(suffix=".svg")
try:
fpdf2 = open(fname_pdf, 'w')
if sys.version_info[0] >= 3:
fpdf2.buffer.write(pdf)
else:
fpdf2.write(pdf)
fpdf2.seek(0)
args = [command, fname_pdf, fname_svg]
p = Popen(args)
(out,err) = p.communicate()
fsvg = open(fname_svg)
return fsvg.read().encode()
finally:
os.remove(fname_svg)
os.remove(fname_pdf)
示例12: dump_to_file
def dump_to_file(blurb, exit_fpr):
"""
Dump the given blurb to a randomly generated file which contains exit_fpr.
This function is useful to save data obtained from bad exit relays to file
for later analysis.
"""
if analysis_dir is None:
fd, file_name = tempfile.mkstemp(prefix="%s_" % exit_fpr)
else:
try:
os.makedirs(analysis_dir)
except OSError as err:
if err.errno != errno.EEXIST:
raise
fd, file_name = tempfile.mkstemp(prefix="%s_" % exit_fpr,
dir=analysis_dir)
try:
with open(file_name, "w") as fd:
fd.write(blurb)
except IOError as err:
log.warning("Couldn't write to \"%s\": %s" % (file_name, err))
return None
log.debug("Wrote %d-length blurb to file \"%s\"." %
(len(blurb), file_name))
return file_name
示例13: _inject_admin_password_into_fs
def _inject_admin_password_into_fs(admin_passwd, fs):
"""Set the root password to admin_passwd
admin_password is a root password
fs is the path to the base of the filesystem into which to inject
the key.
This method modifies the instance filesystem directly,
and does not require a guest agent running in the instance.
"""
# The approach used here is to copy the password and shadow
# files from the instance filesystem to local files, make any
# necessary changes, and then copy them back.
LOG.debug(_("Inject admin password fs=%(fs)s "
"admin_passwd=ha-ha-not-telling-you") %
locals())
admin_user = 'root'
fd, tmp_passwd = tempfile.mkstemp()
os.close(fd)
fd, tmp_shadow = tempfile.mkstemp()
os.close(fd)
passwd_path = os.path.join('etc', 'passwd')
shadow_path = os.path.join('etc', 'shadow')
passwd_data = fs.read_file(passwd_path)
shadow_data = fs.read_file(shadow_path)
new_shadow_data = _set_passwd(admin_user, admin_passwd,
passwd_data, shadow_data)
fs.replace_file(shadow_path, new_shadow_data)
示例14: change_admin_password
def change_admin_password(self, password):
root_logger.debug("Changing admin password")
dirname = config_dirname(self.serverid)
dmpwdfile = ""
admpwdfile = ""
try:
(dmpwdfd, dmpwdfile) = tempfile.mkstemp(dir='/var/lib/ipa')
os.write(dmpwdfd, self.dm_password)
os.close(dmpwdfd)
(admpwdfd, admpwdfile) = tempfile.mkstemp(dir='/var/lib/ipa')
os.write(admpwdfd, password)
os.close(admpwdfd)
args = ["/usr/bin/ldappasswd", "-h", self.fqdn,
"-ZZ", "-x", "-D", str(DN(('cn', 'Directory Manager'))),
"-y", dmpwdfile, "-T", admpwdfile,
str(DN(('uid', 'admin'), ('cn', 'users'), ('cn', 'accounts'), self.suffix))]
try:
env = { 'LDAPTLS_CACERTDIR':os.path.dirname(CACERT),
'LDAPTLS_CACERT':CACERT }
ipautil.run(args, env=env)
root_logger.debug("ldappasswd done")
except ipautil.CalledProcessError, e:
print "Unable to set admin password", e
root_logger.debug("Unable to set admin password %s" % e)
finally:
if os.path.isfile(dmpwdfile):
os.remove(dmpwdfile)
if os.path.isfile(admpwdfile):
os.remove(admpwdfile)
示例15: testParseMaincfg
def testParseMaincfg(self):
""" Test parsing of different broker_module declarations """
path = "/var/lib/nagios/rw/livestatus" # Path to the livestatus socket
# Test plain setup with no weird arguments
fd, filename = tempfile.mkstemp()
os.write(fd, 'broker_module=./livestatus.o /var/lib/nagios/rw/livestatus')
status = pynag.Parsers.mk_livestatus(nagios_cfg_file=filename)
self.assertEqual(path, status.livestatus_socket_path)
os.close(fd)
# Test what happens if arguments are provided
fd, filename = tempfile.mkstemp()
os.write(fd, 'broker_module=./livestatus.o /var/lib/nagios/rw/livestatus hostgroups=t')
status = pynag.Parsers.mk_livestatus(nagios_cfg_file=filename)
self.assertEqual(path, status.livestatus_socket_path)
os.close(fd)
# Test what happens if arguments are provided before and after file socket path
fd, filename = tempfile.mkstemp()
os.write(fd, 'broker_module=./livestatus.o num_client_threads=20 /var/lib/nagios/rw/livestatus hostgroups=t')
status = pynag.Parsers.mk_livestatus(nagios_cfg_file=filename)
self.assertEqual(path, status.livestatus_socket_path)
os.close(fd)
# Test what happens if livestatus socket path cannot be found
try:
fd, filename = tempfile.mkstemp()
os.write(fd, 'broker_module=./livestatus.o num_client_threads=20')
status = pynag.Parsers.mk_livestatus(nagios_cfg_file=filename)
self.assertEqual(path, status.livestatus_socket_path)
os.close(fd)
self.assertEqual(True, "Above could should have raised exception")
except pynag.Parsers.ParserError:
pass