本文整理匯總了Python中site.py方法的典型用法代碼示例。如果您正苦於以下問題:Python site.py方法的具體用法?Python site.py怎麽用?Python site.py使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類site
的用法示例。
在下文中一共展示了site.py方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: has_system_site_packages
# 需要導入模塊: import site [as 別名]
# 或者: from site import py [as 別名]
def has_system_site_packages(interpreter):
# TODO: unit-test
system_site_packages = check_output((
interpreter,
'-c',
# stolen directly from virtualenv's site.py
"""\
import site, os.path
print(
0
if os.path.exists(
os.path.join(os.path.dirname(site.__file__), 'no-global-site-packages.txt')
) else
1
)"""
))
system_site_packages = int(system_site_packages)
assert system_site_packages in (0, 1)
return bool(system_site_packages)
示例2: pip_faster
# 需要導入模塊: import site [as 別名]
# 或者: from site import py [as 別名]
def pip_faster(venv_path, pip_command, install, bootstrap_deps):
"""install and run pip-faster"""
# activate the virtualenv
execfile_(venv_executable(venv_path, 'activate_this.py'))
# disable a useless warning
# FIXME: ensure a "true SSLContext" is available
from os import environ
environ['PIP_DISABLE_PIP_VERSION_CHECK'] = '1'
# we always have to run the bootstrap, because the presense of an
# executable doesn't imply the right version. pip is able to validate the
# version in the fastpath case quickly anyway.
run(('pip', 'install') + bootstrap_deps)
run(pip_command + install)
示例3: write_script
# 需要導入模塊: import site [as 別名]
# 或者: from site import py [as 別名]
def write_script(self, script_name, contents, mode="t", blockers=()):
"""Write an executable file to the scripts directory"""
self.delete_blockers( # clean up old .py/.pyw w/o a script
[os.path.join(self.script_dir, x) for x in blockers]
)
log.info("Installing %s script to %s", script_name, self.script_dir)
target = os.path.join(self.script_dir, script_name)
self.add_output(target)
mask = current_umask()
if not self.dry_run:
ensure_directory(target)
if os.path.exists(target):
os.unlink(target)
with open(target, "w" + mode) as f:
f.write(contents)
chmod(target, 0o777 - mask)
示例4: unpack_and_compile
# 需要導入模塊: import site [as 別名]
# 或者: from site import py [as 別名]
def unpack_and_compile(self, egg_path, destination):
to_compile = []
to_chmod = []
def pf(src, dst):
if dst.endswith('.py') and not src.startswith('EGG-INFO/'):
to_compile.append(dst)
elif dst.endswith('.dll') or dst.endswith('.so'):
to_chmod.append(dst)
self.unpack_progress(src, dst)
return not self.dry_run and dst or None
unpack_archive(egg_path, destination, pf)
self.byte_compile(to_compile)
if not self.dry_run:
for f in to_chmod:
mode = ((os.stat(f)[stat.ST_MODE]) | 0o555) & 0o7755
chmod(f, mode)
示例5: write_script
# 需要導入模塊: import site [as 別名]
# 或者: from site import py [as 別名]
def write_script(self, script_name, contents, mode="t", blockers=()):
"""Write an executable file to the scripts directory"""
self.delete_blockers( # clean up old .py/.pyw w/o a script
[os.path.join(self.script_dir,x) for x in blockers])
log.info("Installing %s script to %s", script_name, self.script_dir)
target = os.path.join(self.script_dir, script_name)
self.add_output(target)
mask = current_umask()
if not self.dry_run:
ensure_directory(target)
if os.path.exists(target):
os.unlink(target)
f = open(target,"w"+mode)
f.write(contents)
f.close()
chmod(target, 0o777-mask)
示例6: unpack_and_compile
# 需要導入模塊: import site [as 別名]
# 或者: from site import py [as 別名]
def unpack_and_compile(self, egg_path, destination):
to_compile = []
to_chmod = []
def pf(src, dst):
if dst.endswith('.py') and not src.startswith('EGG-INFO/'):
to_compile.append(dst)
elif dst.endswith('.dll') or dst.endswith('.so'):
to_chmod.append(dst)
self.unpack_progress(src,dst)
return not self.dry_run and dst or None
unpack_archive(egg_path, destination, pf)
self.byte_compile(to_compile)
if not self.dry_run:
for f in to_chmod:
mode = ((os.stat(f)[stat.ST_MODE]) | 0o555) & 0o7755
chmod(f, mode)
示例7: write_script
# 需要導入模塊: import site [as 別名]
# 或者: from site import py [as 別名]
def write_script(self, script_name, contents, mode="t", blockers=()):
"""Write an executable file to the scripts directory"""
self.delete_blockers( # clean up old .py/.pyw w/o a script
[os.path.join(self.script_dir, x) for x in blockers]
)
log.info("Installing %s script to %s", script_name, self.script_dir)
target = os.path.join(self.script_dir, script_name)
self.add_output(target)
if self.dry_run:
return
mask = current_umask()
ensure_directory(target)
if os.path.exists(target):
os.unlink(target)
with open(target, "w" + mode) as f:
f.write(contents)
chmod(target, 0o777 - mask)
示例8: install
# 需要導入模塊: import site [as 別名]
# 或者: from site import py [as 別名]
def install(setup_py, prefix, lib_dir, bin_dir):
__file__ = setup_py
with getattr(tokenize, "open", open)(setup_py) as f:
code = f.read().replace("\\r\\n", "\n")
if os.path.exists(os.path.join(lib_dir, "site.py")):
# Remove the custom site.py for editable install.
# It will be added back after installation is done.
os.remove(os.path.join(lib_dir, "site.py"))
sys.argv[1:] = [
"develop",
"--install-dir={0}".format(lib_dir),
"--no-deps",
"--prefix={0}".format(prefix),
"--script-dir={0}".format(bin_dir),
"--site-dirs={0}".format(lib_dir),
]
if os.path.normpath(lib_dir) not in site.getsitepackages():
# Patches the script writer to inject library path
easy_install.ScriptWriter.template = easy_install.ScriptWriter.template.replace(
"import sys",
"import sys\nsys.path.insert(0, {0!r})".format(lib_dir.replace("\\", "/")),
)
exec(compile(code, __file__, "exec"))
示例9: write_script
# 需要導入模塊: import site [as 別名]
# 或者: from site import py [as 別名]
def write_script(self, script_name, contents, mode="t", blockers=()):
"""Write an executable file to the scripts directory"""
self.delete_blockers( # clean up old .py/.pyw w/o a script
[os.path.join(self.script_dir, x) for x in blockers]
)
log.info("Installing %s script to %s", script_name, self.script_dir)
target = os.path.join(self.script_dir, script_name)
self.add_output(target)
mask = current_umask()
if not self.dry_run:
ensure_directory(target)
if os.path.exists(target):
os.unlink(target)
f = open(target, "w" + mode)
f.write(contents)
f.close()
chmod(target, 0o777 - mask)
示例10: exec_scratch_virtualenv
# 需要導入模塊: import site [as 別名]
# 或者: from site import py [as 別名]
def exec_scratch_virtualenv(args):
"""
goals:
- get any random site-packages off of the pythonpath
- ensure we can import virtualenv
- ensure that we're not using the interpreter that we may need to delete
- idempotency: do nothing if the above goals are already met
"""
scratch = Scratch()
if not exists(scratch.python):
run(('virtualenv', scratch.venv))
if not exists(join(scratch.src, 'virtualenv.py')):
scratch_python = venv_python(scratch.venv)
# TODO: do we allow user-defined override of which version of virtualenv to install?
tmp = scratch.src + '.tmp'
run((scratch_python, '-m', 'pip.__main__', 'install', 'virtualenv', '--target', tmp))
from os import rename
rename(tmp, scratch.src)
import sys
from os.path import realpath
# We want to compare the paths themselves as sometimes sys.path is the same
# as scratch.venv, but with a suffix of bin/..
if realpath(sys.prefix) != realpath(scratch.venv):
# TODO-TEST: sometimes we would get a stale version of venv-update
exec_((scratch.python, dotpy(__file__)) + args) # never returns
# TODO-TEST: the original venv-update's directory was on sys.path (when using symlinking)
sys.path[0] = scratch.src
示例11: install_eggs
# 需要導入模塊: import site [as 別名]
# 或者: from site import py [as 別名]
def install_eggs(self, spec, dist_filename, tmpdir):
# .egg dirs or files are already built, so just return them
if dist_filename.lower().endswith('.egg'):
return [self.install_egg(dist_filename, tmpdir)]
elif dist_filename.lower().endswith('.exe'):
return [self.install_exe(dist_filename, tmpdir)]
# Anything else, try to extract and build
setup_base = tmpdir
if os.path.isfile(dist_filename) and not dist_filename.endswith('.py'):
unpack_archive(dist_filename, tmpdir, self.unpack_progress)
elif os.path.isdir(dist_filename):
setup_base = os.path.abspath(dist_filename)
if (setup_base.startswith(tmpdir) # something we downloaded
and self.build_directory and spec is not None):
setup_base = self.maybe_move(spec, dist_filename, setup_base)
# Find the setup.py file
setup_script = os.path.join(setup_base, 'setup.py')
if not os.path.exists(setup_script):
setups = glob(os.path.join(setup_base, '*', 'setup.py'))
if not setups:
raise DistutilsError(
"Couldn't find a setup script in %s" %
os.path.abspath(dist_filename)
)
if len(setups) > 1:
raise DistutilsError(
"Multiple setup scripts in %s" %
os.path.abspath(dist_filename)
)
setup_script = setups[0]
# Now run it, and return the result
if self.editable:
log.info(self.report_editable(spec, setup_script))
return []
else:
return self.build_and_install(setup_script, setup_base)
示例12: _update_zipimporter_cache
# 需要導入模塊: import site [as 別名]
# 或者: from site import py [as 別名]
def _update_zipimporter_cache(normalized_path, cache, updater=None):
"""
Update zipimporter cache data for a given normalized path.
Any sub-path entries are processed as well, i.e. those corresponding to zip
archives embedded in other zip archives.
Given updater is a callable taking a cache entry key and the original entry
(after already removing the entry from the cache), and expected to update
the entry and possibly return a new one to be inserted in its place.
Returning None indicates that the entry should not be replaced with a new
one. If no updater is given, the cache entries are simply removed without
any additional processing, the same as if the updater simply returned None.
"""
for p in _collect_zipimporter_cache_entries(normalized_path, cache):
# N.B. pypy's custom zipimport._zip_directory_cache implementation does
# not support the complete dict interface:
# * Does not support item assignment, thus not allowing this function
# to be used only for removing existing cache entries.
# * Does not support the dict.pop() method, forcing us to use the
# get/del patterns instead. For more detailed information see the
# following links:
# https://github.com/pypa/setuptools/issues/202#issuecomment-202913420
# https://bitbucket.org/pypy/pypy/src/dd07756a34a41f674c0cacfbc8ae1d4cc9ea2ae4/pypy/module/zipimport/interp_zipimport.py#cl-99
old_entry = cache[p]
del cache[p]
new_entry = updater and updater(p, old_entry)
if new_entry is not None:
cache[p] = new_entry
示例13: is_python_script
# 需要導入模塊: import site [as 別名]
# 或者: from site import py [as 別名]
def is_python_script(script_text, filename):
"""Is this text, as a whole, a Python script? (as opposed to shell/bat/etc.
"""
if filename.endswith('.py') or filename.endswith('.pyw'):
return True # extension says it's Python
if is_python(script_text, filename):
return True # it's syntactically valid Python
if script_text.startswith('#!'):
# It begins with a '#!' line, so check if 'python' is in it somewhere
return 'python' in script_text.splitlines()[0].lower()
return False # Not any Python I can recognize
示例14: _get_script_args
# 需要導入模塊: import site [as 別名]
# 或者: from site import py [as 別名]
def _get_script_args(cls, type_, name, header, script_text):
"For Windows, add a .py extension"
ext = dict(console='.pya', gui='.pyw')[type_]
if ext not in os.environ['PATHEXT'].lower().split(';'):
warnings.warn("%s not listed in PATHEXT; scripts will not be "
"recognized as executables." % ext, UserWarning)
old = ['.pya', '.py', '-script.py', '.pyc', '.pyo', '.pyw', '.exe']
old.remove(ext)
header = cls._adjust_header(type_, header)
blockers = [name + x for x in old]
yield name + ext, header + script_text, 't', blockers
示例15: _get_script_args
# 需要導入模塊: import site [as 別名]
# 或者: from site import py [as 別名]
def _get_script_args(cls, type_, name, header, script_text):
"For Windows, add a .py extension"
ext = dict(console='.pya', gui='.pyw')[type_]
if ext not in os.environ['PATHEXT'].lower().split(';'):
msg = (
"{ext} not listed in PATHEXT; scripts will not be "
"recognized as executables."
).format(**locals())
warnings.warn(msg, UserWarning)
old = ['.pya', '.py', '-script.py', '.pyc', '.pyo', '.pyw', '.exe']
old.remove(ext)
header = cls._adjust_header(type_, header)
blockers = [name + x for x in old]
yield name + ext, header + script_text, 't', blockers