def is_executable(mode):
"""
Check if mode is executable
Mode can be specified in octal or as an int.
"""
if isinstance(mode, str) and mode.startswith('0o'):
mode = int(mode, 8)
ux, gx, ox = stat.S_IXUSR, stat.S_IXGRP, stat.S_IXOTH
return ((mode & ux) or (mode & gx) or (mode & ox)) > 0
python类S_IXUSR的实例源码
def is_executable_file(path):
"""Checks that path is an executable regular file, or a symlink towards one.
This is roughly ``os.path isfile(path) and os.access(path, os.X_OK)``.
"""
# follow symlinks,
fpath = os.path.realpath(path)
if not os.path.isfile(fpath):
# non-files (directories, fifo, etc.)
return False
mode = os.stat(fpath).st_mode
if (sys.platform.startswith('sunos')
and os.getuid() == 0):
# When root on Solaris, os.X_OK is True for *all* files, irregardless
# of their executability -- instead, any permission bit of any user,
# group, or other is fine enough.
#
# (This may be true for other "Unix98" OS's such as HP-UX and AIX)
return bool(mode & (stat.S_IXUSR |
stat.S_IXGRP |
stat.S_IXOTH))
return os.access(fpath, os.X_OK)
def add_execution_right(file_path):
current_permissions = stat.S_IMODE(os.lstat(file_path).st_mode)
os.chmod(file_path, current_permissions | stat.S_IXUSR)
def install_le_auto(contents, install_path):
"""Install some given source code as the letsencrypt-auto script at the
root level of a virtualenv.
:arg contents: The contents of the built letsencrypt-auto script
:arg install_path: The path where to install the script
"""
with open(install_path, 'w') as le_auto:
le_auto.write(contents)
chmod(install_path, S_IRUSR | S_IXUSR)
def set_le_script_version(venv_dir, version):
"""Tell the letsencrypt script to report a certain version.
We actually replace the script with a dummy version that knows only how to
print its version.
"""
letsencrypt_path = join(venv_dir, 'bin', 'letsencrypt')
with open(letsencrypt_path, 'w') as script:
script.write("#!/usr/bin/env python\n"
"from sys import stderr\n"
"stderr.write('letsencrypt %s\\n')" % version)
chmod(letsencrypt_path, S_IRUSR | S_IXUSR)
def setup_permissions(path):
file_stat = os.stat(path)
os.chmod(path, file_stat.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
def assert_mode_644(mode):
"""Verify given mode is 644"""
assert (mode & stat.S_IROTH) and (mode & stat.S_IRGRP)
assert (mode & stat.S_IWUSR) and (mode & stat.S_IRUSR) and not (mode & stat.S_IXUSR)
def assert_mode_755(mode):
"""Verify given mode is 755"""
assert (mode & stat.S_IROTH) and (mode & stat.S_IRGRP) and (mode & stat.S_IXOTH) and (mode & stat.S_IXGRP)
assert (mode & stat.S_IWUSR) and (mode & stat.S_IRUSR) and (mode & stat.S_IXUSR)
def _isexecutable(cmd):
if os.path.isfile(cmd):
mode = os.stat(cmd)[stat.ST_MODE]
if mode & stat.S_IXUSR or mode & stat.S_IXGRP or mode & stat.S_IXOTH:
return True
return False
def _create_executable_script(self, script_name, content):
script_path = os.path.join(self._paths.venv,
'{}.bash'.format(script_name))
with open(script_path, 'w') as f:
f.write(content)
st = os.stat(script_path)
os.chmod(script_path, st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
def _has_updown_scripts(path, warn=True):
"""
Checks the existence of the up/down scripts and its
exec bit if applicable.
:param path: the path to be checked
:type path: str
:param warn: whether we should log the absence
:type warn: bool
:rtype: bool
"""
is_file = os.path.isfile(path)
if warn and not is_file:
log.error('Could not find up/down script %s. '
'Might produce DNS leaks.' % (path,))
# XXX check if applies in win
is_exe = False
try:
is_exe = (stat.S_IXUSR & os.stat(path)[stat.ST_MODE] != 0)
except OSError as e:
log.warn("%s" % (e,))
if warn and not is_exe:
log.error('Up/down script %s is not executable. '
'Might produce DNS leaks.' % (path,))
return is_file and is_exe
def _isexecutable(cmd):
if os.path.isfile(cmd):
mode = os.stat(cmd)[stat.ST_MODE]
if mode & stat.S_IXUSR or mode & stat.S_IXGRP or mode & stat.S_IXOTH:
return True
return False
def __init__(self):
# create cdrom dir
self.cdrom_dir_tmp = TemporaryDirectory()
self.tmp_dir = TemporaryDirectory()
self.cdrom_iso_tmp = None
# give qemu permission to execute and read in this directory
os.chmod(self.tmp_dir.name, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
stat.S_IROTH | stat.S_IWOTH | stat.S_IXOTH)
self.cdrom_dir = self.cdrom_dir_tmp.name
self.cdrom_iso_tmp = None
def run(self):
"""Runs the default installer and sets rights on log directory."""
install.run(self)
# Make argosd user owned of log directory
uid = getpwnam('argosd').pw_uid
gid = getpwnam('argosd').pw_gid
os.chown(settings.LOG_PATH, uid, gid)
# User has full access, others can read
mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | \
stat.S_IRGRP | stat.S_IROTH
os.chmod(settings.LOG_PATH, mode)
def file_group_executable(path):
"""Check if a file should be group executable"""
mode = path.stat().mode
if stat.S_ISDIR(mode):
return
if bool(stat.S_IXUSR & mode) and not bool(stat.S_IXGRP & mode):
return 'PROB_FILE_NOT_GRPEXEC'
def _create_wrappers(files_to_wrap, destination_dir, run_in_template_filename,
template_func,
inline=False, command=''):
os.path.exists(destination_dir) or os.makedirs(destination_dir)
with open(run_in_template_filename, 'r') as f:
run_in_template = template_func(f.read())
if not inline:
run_in_filename = os.path.join(destination_dir,
'run-in' + get_wrapper_extension())
with open(run_in_filename, 'w') as f:
f.write(run_in_template.replace('__COMMAND__', command))
os.chmod(run_in_filename,
os.stat(run_in_filename).st_mode | stat.S_IXUSR)
for filename in files_to_wrap:
basename = os.path.basename(filename)
if basename == 'run-in' + get_wrapper_extension():
continue
destination_filename = get_wrapper_full_path(destination_dir, basename)
if inline:
content = run_in_template.replace('__COMMAND__',
command + filename + ' ')
else:
content = get_wrapper_template().format(
command=command,
run_in_file=run_in_filename,
wrapped_file=filename)
with open(destination_filename, 'w') as f:
f.write(content)
os.chmod(destination_filename,
os.stat(destination_filename).st_mode | stat.S_IXUSR)
def _environ_from_activate(activate_script, tmpdir):
activate_file = tmpdir.join('environ-from-activate') + \
get_wrapper_extension()
activate_file.write('''%s
python -c "from os import environ; print(dict(environ))"
''' % activate_script)
activate_file.chmod(activate_file.stat().mode | stat.S_IXUSR)
output = subprocess.check_output(str(activate_file))
environ_from_activate = eval(output)
return environ_from_activate
def _create_executable_file(filepath):
if sys.platform == 'win32':
filepath = filepath.new(ext='bat')
filepath.write('')
if sys.platform != 'win32':
filepath.chmod(filepath.stat().mode | stat.S_IXUSR)
return filepath
def statinfo(st):
return {
'mode' : "%04o" % stat.S_IMODE(st.st_mode),
'isdir' : stat.S_ISDIR(st.st_mode),
'ischr' : stat.S_ISCHR(st.st_mode),
'isblk' : stat.S_ISBLK(st.st_mode),
'isreg' : stat.S_ISREG(st.st_mode),
'isfifo' : stat.S_ISFIFO(st.st_mode),
'islnk' : stat.S_ISLNK(st.st_mode),
'issock' : stat.S_ISSOCK(st.st_mode),
'uid' : st.st_uid,
'gid' : st.st_gid,
'size' : st.st_size,
'inode' : st.st_ino,
'dev' : st.st_dev,
'nlink' : st.st_nlink,
'atime' : st.st_atime,
'mtime' : st.st_mtime,
'ctime' : st.st_ctime,
'wusr' : bool(st.st_mode & stat.S_IWUSR),
'rusr' : bool(st.st_mode & stat.S_IRUSR),
'xusr' : bool(st.st_mode & stat.S_IXUSR),
'wgrp' : bool(st.st_mode & stat.S_IWGRP),
'rgrp' : bool(st.st_mode & stat.S_IRGRP),
'xgrp' : bool(st.st_mode & stat.S_IXGRP),
'woth' : bool(st.st_mode & stat.S_IWOTH),
'roth' : bool(st.st_mode & stat.S_IROTH),
'xoth' : bool(st.st_mode & stat.S_IXOTH),
'isuid' : bool(st.st_mode & stat.S_ISUID),
'isgid' : bool(st.st_mode & stat.S_ISGID),
}
def is_executable(path):
'''is the given path executable?
Limitations:
* Does not account for FSACLs.
* Most times we really want to know "Can the current user execute this
file" This function does not tell us that, only if an execute bit is set.
'''
# These are all bitfields so first bitwise-or all the permissions we're
# looking for, then bitwise-and with the file's mode to determine if any
# execute bits are set.
return ((stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) & os.stat(path)[stat.ST_MODE])