本文整理汇总了Python中os.F_OK属性的典型用法代码示例。如果您正苦于以下问题:Python os.F_OK属性的具体用法?Python os.F_OK怎么用?Python os.F_OK使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类os
的用法示例。
在下文中一共展示了os.F_OK属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import os [as 别名]
# 或者: from os import F_OK [as 别名]
def __init__(self, dir, file_template=_default_file_template,
truncate_slug_length=40,
version_locations=None,
sourceless=False, output_encoding="utf-8"):
self.dir = dir
self.file_template = file_template
self.version_locations = version_locations
self.truncate_slug_length = truncate_slug_length or 40
self.sourceless = sourceless
self.output_encoding = output_encoding
self.revision_map = revision.RevisionMap(self._load_revisions)
if not os.access(dir, os.F_OK):
raise util.CommandError("Path doesn't exist: %r. Please use "
"the 'init' command to create a new "
"scripts folder." % dir)
示例2: write_script
# 需要导入模块: import os [as 别名]
# 或者: from os import F_OK [as 别名]
def write_script(
scriptdir, rev_id, content, encoding='ascii', sourceless=False):
old = scriptdir.revision_map.get_revision(rev_id)
path = old.path
content = textwrap.dedent(content)
if encoding:
content = content.encode(encoding)
with open(path, 'wb') as fp:
fp.write(content)
pyc_path = util.pyc_file_from_path(path)
if os.access(pyc_path, os.F_OK):
os.unlink(pyc_path)
script = Script._from_path(scriptdir, path)
old = scriptdir.revision_map.get_revision(script.revision)
if old.down_revision != script.down_revision:
raise Exception("Can't change down_revision "
"on a refresh operation.")
scriptdir.revision_map.add_revision(script, _replace=True)
if sourceless:
make_sourceless(path)
示例3: read_file
# 需要导入模块: import os [as 别名]
# 或者: from os import F_OK [as 别名]
def read_file(location):
"""Returns the contents of a file or None on error. The error is printed to
stderr.
Args:
location (str): The location of the file to be read.
"""
location = os.path.expandvars(location)
if not os.access(location, os.F_OK):
eprint('Configuration file', location, 'not found')
return None
try:
with open(location, 'r') as file:
return file.read()
except PermissionError:
eprint('Cannot read configuration file', location, '(permission)')
return None
示例4: GetCacheDir
# 需要导入模块: import os [as 别名]
# 或者: from os import F_OK [as 别名]
def GetCacheDir(session):
"""Returns the path of a usable cache directory."""
cache_dir = session.GetParameter("cache_dir")
if cache_dir == None:
return cache_dir
cache_dir = os.path.expandvars(cache_dir)
if not cache_dir:
raise io_manager.IOManagerError(
"Local profile cache is not configured - "
"add a cache_dir parameter to ~/.rekallrc.")
# Cache dir may be specified relative to the home directory.
cache_dir = os.path.join(config.GetHomeDir(session), cache_dir)
if not os.access(cache_dir, os.F_OK | os.R_OK | os.W_OK | os.X_OK):
try:
os.makedirs(cache_dir)
except (IOError, OSError):
raise io_manager.IOManagerError(
"Unable to create or access cache directory %s" % cache_dir)
return cache_dir
示例5: setTmpDir
# 需要导入模块: import os [as 别名]
# 或者: from os import F_OK [as 别名]
def setTmpDir(self):
"""Set the tmpDir attribute to a reasonnable location for a temporary
directory"""
if os.name != 'nt':
# On unix use /tmp by default
self.tmpDir = os.environ.get("TMPDIR", "/tmp")
self.tmpDir = os.environ.get("TMP", self.tmpDir)
else:
# On Windows use the current directory
self.tmpDir = os.environ.get("TMPDIR", "")
self.tmpDir = os.environ.get("TMP", self.tmpDir)
self.tmpDir = os.environ.get("TEMP", self.tmpDir)
if not os.path.isdir(self.tmpDir):
self.tmpDir = ""
elif not os.access(self.tmpDir, os.F_OK + os.W_OK):
self.tmpDir = ""
示例6: _create_test_db
# 需要导入模块: import os [as 别名]
# 或者: from os import F_OK [as 别名]
def _create_test_db(self, verbosity, autoclobber, keepdb=False):
test_database_name = self._get_test_db_name()
if keepdb:
return test_database_name
if not self.connection.is_in_memory_db(test_database_name):
# Erase the old test database
if verbosity >= 1:
print("Destroying old test database '%s'..." % self.connection.alias)
if os.access(test_database_name, os.F_OK):
if not autoclobber:
confirm = input(
"Type 'yes' if you would like to try deleting the test "
"database '%s', or 'no' to cancel: " % test_database_name
)
if autoclobber or confirm == 'yes':
try:
os.remove(test_database_name)
except Exception as e:
sys.stderr.write("Got an error deleting the old test database: %s\n" % e)
sys.exit(2)
else:
print("Tests cancelled.")
sys.exit(1)
return test_database_name
示例7: _popen
# 需要导入模块: import os [as 别名]
# 或者: from os import F_OK [as 别名]
def _popen(command, args):
import os
path = os.environ.get("PATH", os.defpath).split(os.pathsep)
path.extend(('/sbin', '/usr/sbin'))
for dir in path:
executable = os.path.join(dir, command)
if (os.path.exists(executable) and
os.access(executable, os.F_OK | os.X_OK) and
not os.path.isdir(executable)):
break
else:
return None
# LC_ALL to ensure English output, 2>/dev/null to prevent output on
# stderr (Note: we don't have an example where the words we search for
# are actually localized, but in theory some system could do so.)
cmd = 'LC_ALL=C %s %s 2>/dev/null' % (executable, args)
return os.popen(cmd)
示例8: check_permissions
# 需要导入模块: import os [as 别名]
# 或者: from os import F_OK [as 别名]
def check_permissions(self, device_id):
if not os.access(self.devices[device_id]['path'], os.F_OK | os.R_OK | os.X_OK):
return False
if not self.check_file_permissions(self.device_file(device_id, 'alternate_modes')):
return False
if not self.check_file_permissions(self.device_file(device_id, 'range')):
return False
if not self.check_file_permissions(self.device_file(device_id, 'combine_pedals')):
return False
if not self.check_file_permissions(self.device_file(device_id, 'gain')):
return False
if not self.check_file_permissions(self.device_file(device_id, 'autocenter')):
return False
if not self.check_file_permissions(self.device_file(device_id, 'spring_level')):
return False
if not self.check_file_permissions(self.device_file(device_id, 'damper_level')):
return False
if not self.check_file_permissions(self.device_file(device_id, 'friction_level')):
return False
if not self.check_file_permissions(self.device_file(device_id, 'ffb_leds')):
return False
if not self.check_file_permissions(self.device_file(device_id, 'peak_ffb_level')):
return False
return True
示例9: is_storage_device
# 需要导入模块: import os [as 别名]
# 或者: from os import F_OK [as 别名]
def is_storage_device(name):
"""Return True if the given name refers to a root device (e.g.
"sda", "nvme0n1") as opposed to a logical partition (e.g. "sda1",
"nvme0n1p1"). If name is a virtual device (e.g. "loop1", "ram")
return True.
"""
# Readapted from iostat source code, see:
# https://github.com/sysstat/sysstat/blob/
# 97912938cd476645b267280069e83b1c8dc0e1c7/common.c#L208
# Some devices may have a slash in their name (e.g. cciss/c0d0...).
name = name.replace('/', '!')
including_virtual = True
if including_virtual:
path = "/sys/block/%s" % name
else:
path = "/sys/block/%s/device" % name
return os.access(path, os.F_OK)
示例10: _clone_test_db
# 需要导入模块: import os [as 别名]
# 或者: from os import F_OK [as 别名]
def _clone_test_db(self, suffix, verbosity, keepdb=False):
source_database_name = self.connection.settings_dict['NAME']
target_database_name = self.get_test_db_clone_settings(suffix)['NAME']
# Forking automatically makes a copy of an in-memory database.
if not self.is_in_memory_db(source_database_name):
# Erase the old test database
if os.access(target_database_name, os.F_OK):
if keepdb:
return
if verbosity >= 1:
print("Destroying old test database for alias %s..." % (
self._get_database_display_str(verbosity, target_database_name),
))
try:
os.remove(target_database_name)
except Exception as e:
sys.stderr.write("Got an error deleting the old test database: %s\n" % e)
sys.exit(2)
try:
shutil.copy(source_database_name, target_database_name)
except Exception as e:
sys.stderr.write("Got an error cloning the test database: %s\n" % e)
sys.exit(2)
示例11: get_writable_file
# 需要导入模块: import os [as 别名]
# 或者: from os import F_OK [as 别名]
def get_writable_file(self,existing_only=0):
""" Return the name of the first writable catalog file.
Its parent directory must also be writable. This is so that
compiled modules can be written to the same directory.
"""
# note: both file and its parent directory must be writeable
if existing_only:
files = self.get_existing_files()
else:
files = self.get_catalog_files()
# filter for (file exists and is writable) OR directory is writable
def file_test(x):
from os import access, F_OK, W_OK
return (access(x,F_OK) and access(x,W_OK) or
access(os.path.dirname(x),W_OK))
writable = filter(file_test,files)
if writable:
file = writable[0]
else:
file = None
return file
示例12: can_write_dir
# 需要导入模块: import os [as 别名]
# 或者: from os import F_OK [as 别名]
def can_write_dir(path):
"""Determines if the directory at path can be written to by the current process.
If the directory doesn't exist, determines if it can be created and thus written to.
N.B.: This is a best-effort check only that uses permission heuristics and does not actually test
that the directory can be written to with and writes.
:param str path: The directory path to test.
:return: `True` if the given path is a directory that can be written to by the current process.
:rtype boo:
"""
while not os.access(path, os.F_OK):
parent_path = os.path.dirname(path)
if not parent_path or (parent_path == path):
# We've recursed up to the root without success, which shouldn't happen,
return False
path = parent_path
return os.path.isdir(path) and os.access(path, os.R_OK | os.W_OK | os.X_OK)
示例13: which
# 需要导入模块: import os [as 别名]
# 或者: from os import F_OK [as 别名]
def which(
command: str, *, return_bool: bool = False, raise_error: bool = False, raise_msg: str = None, env: str = None
) -> Union[bool, None, str]:
"""Test to see if a command is available.
Returns
-------
str or None
By default, returns command path if command found or `None` if not.
Environment is $PATH or `os.pathsep`-separated `env`, less any None values.
bool
When `return_bool=True`, returns whether or not found.
Raises
------
ModuleNotFoundError
When `raises_error=True` and command not found. Raises generic message plus any `raise_msg`.
"""
if env is None:
lenv = {"PATH": os.pathsep + os.environ.get("PATH", "") + os.path.dirname(sys.executable)}
else:
lenv = {"PATH": os.pathsep.join([os.path.abspath(x) for x in env.split(os.pathsep) if x != ""])}
lenv = {k: v for k, v in lenv.items() if v is not None}
ans = shutil.which(command, mode=os.F_OK | os.X_OK, path=lenv["PATH"])
if raise_error and ans is None:
raise ModuleNotFoundError(
f"Command '{command}' not found in envvar PATH.{' ' + raise_msg if raise_msg else ''}"
)
if return_bool:
return bool(ans)
else:
return ans
示例14: _clean_check
# 需要导入模块: import os [as 别名]
# 或者: from os import F_OK [as 别名]
def _clean_check(cmd, target):
"""
Run the command to download target.
If the command fails, clean up before re-raising the error.
"""
try:
subprocess.check_call(cmd)
except subprocess.CalledProcessError:
if os.access(target, os.F_OK):
os.unlink(target)
raise
示例15: _clean_check
# 需要导入模块: import os [as 别名]
# 或者: from os import F_OK [as 别名]
def _clean_check(cmd, target):
"""
Run the command to download target. If the command fails, clean up before
re-raising the error.
"""
try:
subprocess.check_call(cmd)
except subprocess.CalledProcessError:
if os.access(target, os.F_OK):
os.unlink(target)
raise