def html_to_png(html):
# Use a temp file in the current working directory so that wkhtmltoimage handles relative URLs properly
temp_file = '.temp.html'
with open(temp_file, 'w') as f:
f.write(html.encode('utf-8'))
command = ['wkhtmltoimage']
if not find_executable(command[0]):
raise ImportError('%s not found' % command[0])
command += ['-f', 'png'] # format output as PNG
command += ['--zoom', '2'] # retina image
command += ['--width', '750'] # viewport 750px wide
command += [temp_file] # read from stdin
command += ['-'] # write to stdout
wkhtml_process = Popen(command, stdout=PIPE, stderr=PIPE)
(output, err) = wkhtml_process.communicate()
os.remove(temp_file)
image = Image.open(BytesIO(output))
image = set_transparent_pixel(image)
return image
python类find_executable()的实例源码
def __init__(self, port:int, auth_token:str, subdomain:str):
"""Build ngrok HTTP tunnel
Assume ngrok version 2.x. A temporary ngrok configuration file is created where the ngrok token is stored for running the command.
:param port: localhost port forwarded through tunnel
:param auth_token: Ngrok auth token to use
:param subdomain: Use this subdomain for the tunnelling
"""
assert find_executable("ngrok"), "ngrok command must be installed, see https://ngrok.com/"
self.port = port
self.auth_token = auth_token
self.subdomain = subdomain
self.ngrok = None
self.config_file_handle, self.config_file = tempfile.mkstemp()
def run_cmake(arg=""):
"""
Forcing to run cmake
"""
if ds.find_executable('cmake') is None:
print "CMake is required to build zql"
print "Please install cmake version >= 2.8 and re-run setup"
sys.exit(-1)
print "Configuring zql build with CMake.... "
cmake_args = arg
try:
build_dir = op.join(op.split(__file__)[0], 'build')
dd.mkpath(build_dir)
os.chdir("build")
ds.spawn(['cmake', '..'] + cmake_args.split())
ds.spawn(['make', 'clean'])
ds.spawn(['make'])
os.chdir("..")
except ds.DistutilsExecError:
print "Error while running cmake"
print "run 'setup.py build --help' for build options"
print "You may also try editing the settings in CMakeLists.txt file and re-running setup"
sys.exit(-1)
def _use_header(new_header):
"""
Should _adjust_header use the replaced header?
On non-windows systems, always use. On
Windows systems, only use the replaced header if it resolves
to an executable on the system.
"""
clean_header = new_header[2:-1].strip('"')
return sys.platform != 'win32' or find_executable(clean_header)
def _use_header(new_header):
"""
Should _adjust_header use the replaced header?
On non-windows systems, always use. On
Windows systems, only use the replaced header if it resolves
to an executable on the system.
"""
clean_header = new_header[2:-1].strip('"')
return sys.platform != 'win32' or find_executable(clean_header)
def get_versions():
""" Try to find out the versions of gcc and ld.
If not possible it returns None for it.
"""
from distutils.version import StrictVersion
from distutils.spawn import find_executable
import re
gcc_exe = find_executable('gcc')
if gcc_exe:
out = os.popen(gcc_exe + ' -dumpversion','r')
try:
out_string = out.read()
finally:
out.close()
result = re.search('(\d+\.\d+\.\d+)',out_string)
if result:
gcc_version = StrictVersion(result.group(1))
else:
gcc_version = None
else:
gcc_version = None
# EMX ld has no way of reporting version number, and we use GCC
# anyway - so we can link OMF DLLs
ld_version = None
return (gcc_version, ld_version)
def get_editor():
# Get the editor from the environment. Prefer VISUAL to EDITOR
editor = os.environ.get('VISUAL') or os.environ.get('EDITOR')
if editor:
return editor
# None found in the environment. Fallback to platform-specific defaults.
for ed in get_default_editors():
path = find_executable(ed)
if path is not None:
return path
raise EditorError("Unable to find a viable editor on this system."
"Please consider setting your %s variable" % get_platform_editor_var())
def _use_header(new_header):
"""
Should _adjust_header use the replaced header?
On non-windows systems, always use. On
Windows systems, only use the replaced header if it resolves
to an executable on the system.
"""
clean_header = new_header[2:-1].strip('"')
return sys.platform != 'win32' or find_executable(clean_header)
def _use_header(new_header):
"""
Should _adjust_header use the replaced header?
On non-windows systems, always use. On
Windows systems, only use the replaced header if it resolves
to an executable on the system.
"""
clean_header = new_header[2:-1].strip('"')
return sys.platform != 'win32' or find_executable(clean_header)
def _use_header(new_header):
"""
Should _adjust_header use the replaced header?
On non-windows systems, always use. On
Windows systems, only use the replaced header if it resolves
to an executable on the system.
"""
clean_header = new_header[2:-1].strip('"')
return sys.platform != 'win32' or find_executable(clean_header)
def path():
doc = "the path to the exeutable"
def fget(self):
if not self._path is None:
return self._path
self._path = find_executable(self.name)
if self._path is None:
msg = "Could not find executable:{executable}".format(executable=self.name)
log.error(msg)
raise ExecutableNotFound(msg)
return self._path
return locals()
def _use_header(new_header):
"""
Should _adjust_header use the replaced header?
On non-windows systems, always use. On
Windows systems, only use the replaced header if it resolves
to an executable on the system.
"""
clean_header = new_header[2:-1].strip('"')
return sys.platform != 'win32' or find_executable(clean_header)
def get_versions():
""" Try to find out the versions of gcc and ld.
If not possible it returns None for it.
"""
from distutils.version import StrictVersion
from distutils.spawn import find_executable
import re
gcc_exe = find_executable('gcc')
if gcc_exe:
out = os.popen(gcc_exe + ' -dumpversion','r')
try:
out_string = out.read()
finally:
out.close()
result = re.search('(\d+\.\d+\.\d+)',out_string)
if result:
gcc_version = StrictVersion(result.group(1))
else:
gcc_version = None
else:
gcc_version = None
# EMX ld has no way of reporting version number, and we use GCC
# anyway - so we can link OMF DLLs
ld_version = None
return (gcc_version, ld_version)
def test_run(self):
pkg_dir, dist = self.create_dist()
cmd = build_clib(dist)
foo_c = os.path.join(pkg_dir, 'foo.c')
self.write_file(foo_c, 'int main(void) { return 1;}\n')
cmd.libraries = [('foo', {'sources': [foo_c]})]
build_temp = os.path.join(pkg_dir, 'build')
os.mkdir(build_temp)
cmd.build_temp = build_temp
cmd.build_clib = build_temp
# before we run the command, we want to make sure
# all commands are present on the system
# by creating a compiler and checking its executables
from distutils.ccompiler import new_compiler
from distutils.sysconfig import customize_compiler
compiler = new_compiler()
customize_compiler(compiler)
for ccmd in compiler.executables.values():
if ccmd is None:
continue
if find_executable(ccmd[0]) is None:
self.skipTest('The %r command is not found' % ccmd[0])
# this should work
cmd.run()
# let's check the result
self.assertIn('libfoo.a', os.listdir(build_temp))
def finalize_options(self):
self.root = os.path.abspath(os.path.dirname(__file__))
self.thrift = find_executable('thrift1')
if self.thrift is None:
self.thrift = find_executable('thrift')
def _use_header(new_header):
"""
Should _adjust_header use the replaced header?
On non-windows systems, always use. On
Windows systems, only use the replaced header if it resolves
to an executable on the system.
"""
clean_header = new_header[2:-1].strip('"')
return sys.platform != 'win32' or find_executable(clean_header)
def _use_header(new_header):
"""
Should _adjust_header use the replaced header?
On non-windows systems, always use. On
Windows systems, only use the replaced header if it resolves
to an executable on the system.
"""
clean_header = new_header[2:-1].strip('"')
return sys.platform != 'win32' or find_executable(clean_header)
def _check_dependencies(self):
not_found = []
for executable in REQUIRED_EXEC:
if not find_executable(executable):
not_found.append(executable)
if not_found:
raise CommandError('The following executables are required: %s, missing: %s' % (REQUIRED_EXEC, not_found))
def add_image(self, image, pool, cmd=None):
shortimage = os.path.basename(image).split('?')[0]
if pool is not None:
pool = [p['path'] for p in self._pool_info() if p['name'] == pool]
if pool:
poolpath = pool[0]
else:
print("Pool not found. Leaving....")
return
downloadcmd = 'curl -Lo %s -f %s/%s' % (shortimage, poolpath, image)
os.system(downloadcmd)
if cmd is not None and find_executable('virt-customize') is not None:
cmd = "virt-customize -a %s/%s %s" % (poolpath, image, cmd)
os.system(cmd)
return {'result': 'success'}