def plist_save(filename, data, binary = False):
import plistlib
if not binary:
plistlib.writePlist(data, filename)
return 0
import warnings
warnings.filterwarnings("ignore")
tmpname = os.tempnam(None, 'plist.')
plistlib.writePlist(data, tmpname)
plutil('-convert', 'binary1', '-o', filename, tmpname)
os.remove(tmpname)
return 0
#----------------------------------------------------------------------
# testing case
#----------------------------------------------------------------------
python类tempnam()的实例源码
def get_temp_file(keep=False, autoext=""):
f = os.tempnam("","scapy")
if not keep:
conf.temp_files.append(f+autoext)
return f
def get_temp_file(keep=False, autoext=""):
f = os.tempnam("","scapy")
if not keep:
conf.temp_files.append(f+autoext)
return f
def get_temp_file(keep=False, autoext=""):
f = os.tempnam("","scapy")
if not keep:
conf.temp_files.append(f+autoext)
return f
def mkstemp(dir):
for _ in range(5):
name = os.tempnam(dir)
try:
fd = os.open(name, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0600)
except OSError, why:
if why.errno != EEXIST:
raise
else:
return fd, name
raise RuntimeError('Failed to get temp file after 5 attempts')
def get_temp_file(keep=False, autoext=""):
f = os.tempnam("","scapy")
if not keep:
conf.temp_files.append(f+autoext)
return f
def test_tempnam(self):
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
r"test_os$")
warnings.filterwarnings("ignore", "tempnam", DeprecationWarning)
self.check_tempfile(os.tempnam())
name = os.tempnam(test_support.TESTFN)
self.check_tempfile(name)
name = os.tempnam(test_support.TESTFN, "pfx")
self.assertTrue(os.path.basename(name)[:3] == "pfx")
self.check_tempfile(name)
def test_tempnam(self):
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
r"test_os$")
warnings.filterwarnings("ignore", "tempnam", DeprecationWarning)
self.check_tempfile(os.tempnam())
name = os.tempnam(test_support.TESTFN)
self.check_tempfile(name)
name = os.tempnam(test_support.TESTFN, "pfx")
self.assertTrue(os.path.basename(name)[:3] == "pfx")
self.check_tempfile(name)
def get_temp_file(keep=False, autoext=""):
f = os.tempnam("","scapy")
if not keep:
conf.temp_files.append(f+autoext)
return f
def get_temp_file(keep=False, autoext=""):
f = os.tempnam("","scapy")
if not keep:
conf.temp_files.append(f+autoext)
return f
def get_temp_file(keep=False, autoext=""):
f = os.tempnam("","scapy")
if not keep:
conf.temp_files.append(f+autoext)
return f
def get_temp_file(keep=False, autoext=""):
f = os.tempnam("","scapy")
if not keep:
conf.temp_files.append(f+autoext)
return f
def test_tempnam(self):
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
r"test_os$")
warnings.filterwarnings("ignore", "tempnam", DeprecationWarning)
self.check_tempfile(os.tempnam())
name = os.tempnam(test_support.TESTFN)
self.check_tempfile(name)
name = os.tempnam(test_support.TESTFN, "pfx")
self.assertTrue(os.path.basename(name)[:3] == "pfx")
self.check_tempfile(name)
def test_tempnam(self):
if not hasattr(os, "tempnam"):
return
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
r"test_os$")
warnings.filterwarnings("ignore", "tempnam", DeprecationWarning)
self.check_tempfile(os.tempnam())
name = os.tempnam(test_support.TESTFN)
self.check_tempfile(name)
name = os.tempnam(test_support.TESTFN, "pfx")
self.assertTrue(os.path.basename(name)[:3] == "pfx")
self.check_tempfile(name)
def plist_load(filename):
import plistlib
fp = open(filename, 'rb')
content = fp.read(8)
fp.close()
if content == 'bplist00':
import warnings
warnings.filterwarnings("ignore")
tmpname = os.tempnam(None, 'plist.')
plutil('-convert', 'xml1', '-o', tmpname, filename)
data = plistlib.readPlist(tmpname)
os.remove(tmpname)
return data
data = plistlib.readPlist(filename)
return data
def get_temp_file(keep=False, autoext=""):
f = os.tempnam("","scapy")
if not keep:
conf.temp_files.append(f+autoext)
return f
def mkstemp(dir):
for _ in range(5):
name = os.tempnam(dir)
try:
fd = os.open(name, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0600)
except OSError, why:
if why.errno != EEXIST:
raise
else:
return fd, name
raise RuntimeError('Failed to get temp file after 5 attempts')
def tempfile(path, prefix, ext):
"""Generates a a temp filename 'path/prefix_XXXXXX.ext'
DEPRECATED: use this instead
fd, filename = temporaryfile.mkstemp(ext, prefix)
os.close(fd)
"""
import warnings
warnings.filterwarnings("ignore", ".*", RuntimeWarning)
filename = os.tempnam(path, "____")
filename = filename.replace("____", prefix) + ext
warnings.filterwarnings("default", ".*", RuntimeWarning)
return filename
def mkstemp(dir):
for _ in range(5):
name = os.tempnam(dir)
try:
fd = os.open(name, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0600)
except OSError, why:
if why.errno != EEXIST:
raise
else:
return fd, name
raise RuntimeError('Failed to get temp file after 5 attempts')
def do_graph(graph,prog=None,format=None,target=None,type=None,string=None,options=None):
"""do_graph(graph, prog=conf.prog.dot, format="svg",
target="| conf.prog.display", options=None, [string=1]):
string: if not None, simply return the graph string
graph: GraphViz graph description
format: output type (svg, ps, gif, jpg, etc.), passed to dot's "-T" option
target: filename or redirect. Defaults pipe to Imagemagick's display program
prog: which graphviz program to use
options: options to be passed to prog"""
if format is None:
if WINDOWS:
format = "png" # use common format to make sure a viewer is installed
else:
format = "svg"
if string:
return graph
if type is not None:
format=type
if prog is None:
prog = conf.prog.dot
start_viewer=False
if target is None:
if WINDOWS:
tempfile = os.tempnam("", "scapy") + "." + format
target = "> %s" % tempfile
start_viewer = True
else:
target = "| %s" % conf.prog.display
if format is not None:
format = "-T %s" % format
w,r = os.popen2("%s %s %s %s" % (prog,options or "", format or "", target))
w.write(graph)
w.close()
if start_viewer:
# Workaround for file not found error: We wait until tempfile is written.
waiting_start = time.time()
while not os.path.exists(tempfile):
time.sleep(0.1)
if time.time() - waiting_start > 3:
warning("Temporary file '%s' could not be written. Graphic will not be displayed." % tempfile)
break
else:
if conf.prog.display == conf.prog._default:
os.startfile(tempfile)
else:
subprocess.Popen([conf.prog.display, tempfile])
def do_graph(graph,prog=None,format=None,target=None,type=None,string=None,options=None):
"""do_graph(graph, prog=conf.prog.dot, format="svg",
target="| conf.prog.display", options=None, [string=1]):
string: if not None, simply return the graph string
graph: GraphViz graph description
format: output type (svg, ps, gif, jpg, etc.), passed to dot's "-T" option
target: filename or redirect. Defaults pipe to Imagemagick's display program
prog: which graphviz program to use
options: options to be passed to prog"""
if format is None:
if WINDOWS:
format = "png" # use common format to make sure a viewer is installed
else:
format = "svg"
if string:
return graph
if type is not None:
format=type
if prog is None:
prog = conf.prog.dot
start_viewer=False
if target is None:
if WINDOWS:
tempfile = os.tempnam("", "scapy") + "." + format
target = "> %s" % tempfile
start_viewer = True
else:
target = "| %s" % conf.prog.display
if format is not None:
format = "-T %s" % format
w,r = os.popen2("%s %s %s %s" % (prog,options or "", format or "", target))
w.write(graph)
w.close()
if start_viewer:
# Workaround for file not found error: We wait until tempfile is written.
waiting_start = time.time()
while not os.path.exists(tempfile):
time.sleep(0.1)
if time.time() - waiting_start > 3:
warning("Temporary file '%s' could not be written. Graphic will not be displayed." % tempfile)
break
else:
if conf.prog.display == conf.prog._default:
os.startfile(tempfile)
else:
subprocess.Popen([conf.prog.display, tempfile])
def do_graph(graph,prog=None,format=None,target=None,type=None,string=None,options=None):
"""do_graph(graph, prog=conf.prog.dot, format="svg",
target="| conf.prog.display", options=None, [string=1]):
string: if not None, simply return the graph string
graph: GraphViz graph description
format: output type (svg, ps, gif, jpg, etc.), passed to dot's "-T" option
target: filename or redirect. Defaults pipe to Imagemagick's display program
prog: which graphviz program to use
options: options to be passed to prog"""
if format is None:
if WINDOWS:
format = "png" # use common format to make sure a viewer is installed
else:
format = "svg"
if string:
return graph
if type is not None:
format=type
if prog is None:
prog = conf.prog.dot
start_viewer=False
if target is None:
if WINDOWS:
tempfile = os.tempnam("", "scapy") + "." + format
target = "> %s" % tempfile
start_viewer = True
else:
target = "| %s" % conf.prog.display
if format is not None:
format = "-T %s" % format
w,r = os.popen2("%s %s %s %s" % (prog,options or "", format or "", target))
w.write(graph)
w.close()
if start_viewer:
# Workaround for file not found error: We wait until tempfile is written.
waiting_start = time.time()
while not os.path.exists(tempfile):
time.sleep(0.1)
if time.time() - waiting_start > 3:
warning("Temporary file '%s' could not be written. Graphic will not be displayed." % tempfile)
break
else:
if conf.prog.display == conf.prog._default:
os.startfile(tempfile)
else:
subprocess.Popen([conf.prog.display, tempfile])
def do_graph(graph,prog=None,format=None,target=None,type=None,string=None,options=None):
"""do_graph(graph, prog=conf.prog.dot, format="svg",
target="| conf.prog.display", options=None, [string=1]):
string: if not None, simply return the graph string
graph: GraphViz graph description
format: output type (svg, ps, gif, jpg, etc.), passed to dot's "-T" option
target: filename or redirect. Defaults pipe to Imagemagick's display program
prog: which graphviz program to use
options: options to be passed to prog"""
if format is None:
if WINDOWS:
format = "png" # use common format to make sure a viewer is installed
else:
format = "svg"
if string:
return graph
if type is not None:
format=type
if prog is None:
prog = conf.prog.dot
start_viewer=False
if target is None:
if WINDOWS:
tempfile = os.tempnam("", "scapy") + "." + format
target = "> %s" % tempfile
start_viewer = True
else:
target = "| %s" % conf.prog.display
if format is not None:
format = "-T %s" % format
w,r = os.popen2("%s %s %s %s" % (prog,options or "", format or "", target))
w.write(graph)
w.close()
if start_viewer:
# Workaround for file not found error: We wait until tempfile is written.
waiting_start = time.time()
while not os.path.exists(tempfile):
time.sleep(0.1)
if time.time() - waiting_start > 3:
warning("Temporary file '%s' could not be written. Graphic will not be displayed." % tempfile)
break
else:
if conf.prog.display == conf.prog._default:
os.startfile(tempfile)
else:
subprocess.Popen([conf.prog.display, tempfile])
def do_graph(graph,prog=None,format=None,target=None,type=None,string=None,options=None):
"""do_graph(graph, prog=conf.prog.dot, format="svg",
target="| conf.prog.display", options=None, [string=1]):
string: if not None, simply return the graph string
graph: GraphViz graph description
format: output type (svg, ps, gif, jpg, etc.), passed to dot's "-T" option
target: filename or redirect. Defaults pipe to Imagemagick's display program
prog: which graphviz program to use
options: options to be passed to prog"""
if format is None:
if WINDOWS:
format = "png" # use common format to make sure a viewer is installed
else:
format = "svg"
if string:
return graph
if type is not None:
format=type
if prog is None:
prog = conf.prog.dot
start_viewer=False
if target is None:
if WINDOWS:
tempfile = os.tempnam("", "scapy") + "." + format
target = "> %s" % tempfile
start_viewer = True
else:
target = "| %s" % conf.prog.display
if format is not None:
format = "-T %s" % format
w,r = os.popen2("%s %s %s %s" % (prog,options or "", format or "", target))
w.write(graph)
w.close()
if start_viewer:
# Workaround for file not found error: We wait until tempfile is written.
waiting_start = time.time()
while not os.path.exists(tempfile):
time.sleep(0.1)
if time.time() - waiting_start > 3:
warning("Temporary file '%s' could not be written. Graphic will not be displayed." % tempfile)
break
else:
if conf.prog.display == conf.prog._default:
os.startfile(tempfile)
else:
subprocess.Popen([conf.prog.display, tempfile])
def do_graph(graph,prog=None,format=None,target=None,type=None,string=None,options=None):
"""do_graph(graph, prog=conf.prog.dot, format="svg",
target="| conf.prog.display", options=None, [string=1]):
string: if not None, simply return the graph string
graph: GraphViz graph description
format: output type (svg, ps, gif, jpg, etc.), passed to dot's "-T" option
target: filename or redirect. Defaults pipe to Imagemagick's display program
prog: which graphviz program to use
options: options to be passed to prog"""
if format is None:
if WINDOWS:
format = "png" # use common format to make sure a viewer is installed
else:
format = "svg"
if string:
return graph
if type is not None:
format=type
if prog is None:
prog = conf.prog.dot
start_viewer=False
if target is None:
if WINDOWS:
tempfile = os.tempnam("", "scapy") + "." + format
target = "> %s" % tempfile
start_viewer = True
else:
target = "| %s" % conf.prog.display
if format is not None:
format = "-T %s" % format
w,r = os.popen2("%s %s %s %s" % (prog,options or "", format or "", target))
w.write(graph)
w.close()
if start_viewer:
# Workaround for file not found error: We wait until tempfile is written.
waiting_start = time.time()
while not os.path.exists(tempfile):
time.sleep(0.1)
if time.time() - waiting_start > 3:
warning("Temporary file '%s' could not be written. Graphic will not be displayed." % tempfile)
break
else:
if conf.prog.display == conf.prog._default:
os.startfile(tempfile)
else:
subprocess.Popen([conf.prog.display, tempfile])
def do_graph(graph,prog=None,format=None,target=None,type=None,string=None,options=None):
"""do_graph(graph, prog=conf.prog.dot, format="svg",
target="| conf.prog.display", options=None, [string=1]):
string: if not None, simply return the graph string
graph: GraphViz graph description
format: output type (svg, ps, gif, jpg, etc.), passed to dot's "-T" option
target: filename or redirect. Defaults pipe to Imagemagick's display program
prog: which graphviz program to use
options: options to be passed to prog"""
if format is None:
if WINDOWS:
format = "png" # use common format to make sure a viewer is installed
else:
format = "svg"
if string:
return graph
if type is not None:
format=type
if prog is None:
prog = conf.prog.dot
start_viewer=False
if target is None:
if WINDOWS:
tempfile = os.tempnam("", "scapy") + "." + format
target = "> %s" % tempfile
start_viewer = True
else:
target = "| %s" % conf.prog.display
if format is not None:
format = "-T %s" % format
w,r = os.popen2("%s %s %s %s" % (prog,options or "", format or "", target))
w.write(graph)
w.close()
if start_viewer:
# Workaround for file not found error: We wait until tempfile is written.
waiting_start = time.time()
while not os.path.exists(tempfile):
time.sleep(0.1)
if time.time() - waiting_start > 3:
warning("Temporary file '%s' could not be written. Graphic will not be displayed." % tempfile)
break
else:
if conf.prog.display == conf.prog._default:
os.startfile(tempfile)
else:
subprocess.Popen([conf.prog.display, tempfile])
def do_graph(graph,prog=None,format=None,target=None,type=None,string=None,options=None):
"""do_graph(graph, prog=conf.prog.dot, format="svg",
target="| conf.prog.display", options=None, [string=1]):
string: if not None, simply return the graph string
graph: GraphViz graph description
format: output type (svg, ps, gif, jpg, etc.), passed to dot's "-T" option
target: filename or redirect. Defaults pipe to Imagemagick's display program
prog: which graphviz program to use
options: options to be passed to prog"""
if format is None:
if WINDOWS:
format = "png" # use common format to make sure a viewer is installed
else:
format = "svg"
if string:
return graph
if type is not None:
format=type
if prog is None:
prog = conf.prog.dot
start_viewer=False
if target is None:
if WINDOWS:
tempfile = os.tempnam("", "scapy") + "." + format
target = "> %s" % tempfile
start_viewer = True
else:
target = "| %s" % conf.prog.display
if format is not None:
format = "-T %s" % format
w,r = os.popen2("%s %s %s %s" % (prog,options or "", format or "", target))
w.write(graph)
w.close()
if start_viewer:
# Workaround for file not found error: We wait until tempfile is written.
waiting_start = time.time()
while not os.path.exists(tempfile):
time.sleep(0.1)
if time.time() - waiting_start > 3:
warning("Temporary file '%s' could not be written. Graphic will not be displayed." % tempfile)
break
else:
if conf.prog.display == conf.prog._default:
os.startfile(tempfile)
else:
subprocess.Popen([conf.prog.display, tempfile])
def do_graph(graph,prog=None,format=None,target=None,type=None,string=None,options=None):
"""do_graph(graph, prog=conf.prog.dot, format="svg",
target="| conf.prog.display", options=None, [string=1]):
string: if not None, simply return the graph string
graph: GraphViz graph description
format: output type (svg, ps, gif, jpg, etc.), passed to dot's "-T" option
target: filename or redirect. Defaults pipe to Imagemagick's display program
prog: which graphviz program to use
options: options to be passed to prog"""
if format is None:
if WINDOWS:
format = "png" # use common format to make sure a viewer is installed
else:
format = "svg"
if string:
return graph
if type is not None:
format=type
if prog is None:
prog = conf.prog.dot
start_viewer=False
if target is None:
if WINDOWS:
tempfile = os.tempnam("", "scapy") + "." + format
target = "> %s" % tempfile
start_viewer = True
else:
target = "| %s" % conf.prog.display
if format is not None:
format = "-T %s" % format
w,r = os.popen2("%s %s %s %s" % (prog,options or "", format or "", target))
w.write(graph)
w.close()
if start_viewer:
# Workaround for file not found error: We wait until tempfile is written.
waiting_start = time.time()
while not os.path.exists(tempfile):
time.sleep(0.1)
if time.time() - waiting_start > 3:
warning("Temporary file '%s' could not be written. Graphic will not be displayed." % tempfile)
break
else:
if conf.prog.display == conf.prog._default:
os.startfile(tempfile)
else:
subprocess.Popen([conf.prog.display, tempfile])
def commit(self, uci):
"""
Commint uci package to openwrt.
"""
buffer = StringIO()
buffer.write('uci import {package} <<UCI\n'.format(
package=uci.package
))
uci.dump(buffer)
buffer.write('\nUCI\n')
# command = buffer.getvalue()
with settings(shell=self.WRT_SHELL, abort_exception=UCIError):
buffer.seek(0)
chunk = buffer.read(WRITE_CHUNK_SIZE)
if len(chunk) < WRITE_CHUNK_SIZE:
j.sal.process.execute(chunk)
return
# Write chunks into file
tmp = os.tempnam()
try:
j.sal.process.execute(
'echo -n > {tmp} "{chunk}"'.format(
tmp=tmp,
chunk=chunk
)
)
while True:
chunk = buffer.read(WRITE_CHUNK_SIZE)
if not chunk:
break
j.sal.process.execute(
'echo -n >> {tmp} "{chunk}"'.format(
tmp=tmp,
chunk=chunk
)
)
j.sal.process.execute('chmod +x %s' % tmp)
j.sal.process.execute(tmp)
finally:
j.sal.process.execute('rm -f %s')