本文整理汇总了Python中os.umask方法的典型用法代码示例。如果您正苦于以下问题:Python os.umask方法的具体用法?Python os.umask怎么用?Python os.umask使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.umask方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: drop_privileges
# 需要导入模块: import os [as 别名]
# 或者: from os import umask [as 别名]
def drop_privileges():
from certidude import config
import pwd
_, _, uid, gid, gecos, root, shell = pwd.getpwnam("certidude")
restricted_groups = []
restricted_groups.append(gid)
# PAM needs access to /etc/shadow
if config.AUTHENTICATION_BACKENDS == {"pam"}:
import grp
name, passwd, num, mem = grp.getgrnam("shadow")
click.echo("Adding current user to shadow group due to PAM authentication backend")
restricted_groups.append(num)
os.setgroups(restricted_groups)
os.setgid(gid)
os.setuid(uid)
click.echo("Switched %s (pid=%d) to user %s (uid=%d, gid=%d); member of groups %s" %
(getproctitle(), os.getpid(), "certidude", os.getuid(), os.getgid(), ", ".join([str(j) for j in os.getgroups()])))
os.umask(0o007)
示例2: __init__
# 需要导入模块: import os [as 别名]
# 或者: from os import umask [as 别名]
def __init__(self, cfg):
old_umask = os.umask(cfg.umask)
fdir = cfg.worker_tmp_dir
if fdir and not os.path.isdir(fdir):
raise RuntimeError("%s doesn't exist. Can't create workertmp." % fdir)
fd, name = tempfile.mkstemp(prefix="wgunicorn-", dir=fdir)
# allows the process to write to the file
util.chown(name, cfg.uid, cfg.gid)
os.umask(old_umask)
# unlink the file so we don't leak tempory files
try:
if not IS_CYGWIN:
util.unlink(name)
self._tmp = os.fdopen(fd, 'w+b', 1)
except:
os.close(fd)
raise
self.spinner = 0
示例3: write_option
# 需要导入模块: import os [as 别名]
# 或者: from os import umask [as 别名]
def write_option(option, value):
"""
Write an option to disk -- doesn't handle config reloading
"""
# deny rwx to Group and World -- don't bother storing the returned old mask
# value, since we'll never restore it in the CLI anyway
# do this on every call to ensure that we're always consistent about it
os.umask(0o077)
conf = get_config_obj()
# add the section if absent
if CONF_SECTION_NAME not in conf:
conf[CONF_SECTION_NAME] = {}
conf[CONF_SECTION_NAME][option] = value
conf.write()
示例4: become_daemon
# 需要导入模块: import os [as 别名]
# 或者: from os import umask [as 别名]
def become_daemon(our_home_dir='.', out_log=None, err_log=None, umask=0o022):
"""
If we're not running under a POSIX system, just simulate the daemon
mode by doing redirections and directory changing.
"""
os.chdir(our_home_dir)
os.umask(umask)
sys.stdin.close()
sys.stdout.close()
sys.stderr.close()
if err_log:
sys.stderr = open(err_log, 'a', buffering)
else:
sys.stderr = NullDevice()
if out_log:
sys.stdout = open(out_log, 'a', buffering)
else:
sys.stdout = NullDevice()
示例5: _openUploadFile
# 需要导入模块: import os [as 别名]
# 或者: from os import umask [as 别名]
def _openUploadFile(self, buildId, suffix):
(packageResultPath, packageResultFile) = self._getPath(buildId, suffix)
if os.path.isfile(packageResultFile):
raise ArtifactExistsError()
# open temporary file in destination directory
if not os.path.isdir(packageResultPath):
if self.__dirMode is not None:
oldMask = os.umask(~self.__dirMode & 0o777)
try:
os.makedirs(packageResultPath, exist_ok=True)
finally:
if self.__dirMode is not None:
os.umask(oldMask)
return LocalArchiveUploader(
NamedTemporaryFile(dir=packageResultPath, delete=False),
self.__fileMode,
packageResultFile)
示例6: test_execute_bit_not_copied
# 需要导入模块: import os [as 别名]
# 或者: from os import umask [as 别名]
def test_execute_bit_not_copied(self):
# Issue 6070: under posix .pyc files got their execute bit set if
# the .py file had the execute bit set, but they aren't executable.
oldmask = os.umask(022)
sys.path.insert(0, os.curdir)
try:
fname = TESTFN + os.extsep + "py"
f = open(fname, 'w').close()
os.chmod(fname, (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH |
stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH))
__import__(TESTFN)
fn = fname + 'c'
if not os.path.exists(fn):
fn = fname + 'o'
if not os.path.exists(fn):
self.fail("__import__ did not result in creation of "
"either a .pyc or .pyo file")
s = os.stat(fn)
self.assertEqual(stat.S_IMODE(s.st_mode),
stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
finally:
os.umask(oldmask)
remove_files(TESTFN)
unload(TESTFN)
del sys.path[0]
示例7: test_permissions_after_flush
# 需要导入模块: import os [as 别名]
# 或者: from os import umask [as 别名]
def test_permissions_after_flush(self):
# See issue #5346
# Make the mailbox world writable. It's unlikely that the new
# mailbox file would have these permissions after flush(),
# because umask usually prevents it.
mode = os.stat(self._path).st_mode | 0o666
os.chmod(self._path, mode)
self._box.add(self._template % 0)
i = self._box.add(self._template % 1)
# Need to remove one message to make flush() create a new file
self._box.remove(i)
self._box.flush()
self.assertEqual(os.stat(self._path).st_mode, mode)
示例8: test_file_perms
# 需要导入模块: import os [as 别名]
# 或者: from os import umask [as 别名]
def test_file_perms(self):
# From bug #3228, we want to verify that the mailbox file isn't executable,
# even if the umask is set to something that would leave executable bits set.
# We only run this test on platforms that support umask.
try:
old_umask = os.umask(0077)
self._box.close()
os.unlink(self._path)
self._box = mailbox.mbox(self._path, create=True)
self._box.add('')
self._box.close()
finally:
os.umask(old_umask)
st = os.stat(self._path)
perms = st.st_mode
self.assertFalse((perms & 0111)) # Execute bits should all be off.
示例9: test_dumbdbm_creation_mode
# 需要导入模块: import os [as 别名]
# 或者: from os import umask [as 别名]
def test_dumbdbm_creation_mode(self):
try:
old_umask = os.umask(0002)
f = dumbdbm.open(_fname, 'c', 0637)
f.close()
finally:
os.umask(old_umask)
expected_mode = 0635
if os.name != 'posix' or sys.platform == 'cli':
# Windows and IronPython only support setting the read-only attribute.
# This shouldn't fail, but doesn't work like Unix either.
expected_mode = 0666
import stat
st = os.stat(_fname + '.dat')
self.assertEqual(stat.S_IMODE(st.st_mode), expected_mode)
st = os.stat(_fname + '.dir')
self.assertEqual(stat.S_IMODE(st.st_mode), expected_mode)
示例10: chmod_plus_x
# 需要导入模块: import os [as 别名]
# 或者: from os import umask [as 别名]
def chmod_plus_x(file):
import os
import stat
umask = os.umask(0)
os.umask(umask)
st = os.stat(file)
os.chmod(file, st.st_mode | ((stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) & ~umask))
示例11: __init__
# 需要导入模块: import os [as 别名]
# 或者: from os import umask [as 别名]
def __init__(self, bus, umask=None, uid=None, gid=None):
SimplePlugin.__init__(self, bus)
self.finalized = False
self.uid = uid
self.gid = gid
self.umask = umask
示例12: umask
# 需要导入模块: import os [as 别名]
# 或者: from os import umask [as 别名]
def umask(self):
"""The default permission mode for newly created files and directories.
Usually expressed in octal format, for example, ``0644``.
Availability: Unix, Windows.
"""
return self._umask
示例13: temp_umask
# 需要导入模块: import os [as 别名]
# 或者: from os import umask [as 别名]
def temp_umask(umask):
"""Context manager that temporarily sets the process umask."""
oldmask = os.umask(umask)
try:
yield
finally:
os.umask(oldmask)
示例14: _daemonize
# 需要导入模块: import os [as 别名]
# 或者: from os import umask [as 别名]
def _daemonize(self):
try:
pid = os.fork() # 第一次fork,生成子进程,脱离父进程
if pid > 0:
sys.exit(0) # 退出主进程
except OSError as e:
sys.stderr.write('fork #1 failed: %d (%s)\n' % (e.errno, e.strerror))
sys.exit(1)
if self.cd:
os.chdir("/") # 修改工作目录
os.setsid() # 设置新的会话连接
os.umask(0) # 重新设置文件创建权限
try:
pid = os.fork() # 第二次fork,禁止进程打开终端
if pid > 0:
sys.exit(0)
except OSError as e:
sys.stderr.write('fork #2 failed: %d (%s)\n' % (e.errno, e.strerror))
sys.exit(1)
# 重定向文件描述符
sys.stdout.flush()
sys.stderr.flush()
# with open(self.stdin, 'r') as si, open(self.stdout, 'a+') as so, open(self.stderr, 'ab+', 0) as se:
si = open(self.stdin, 'r')
so = open(self.stdout, 'a+')
se = open(self.stderr, 'ab+', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
# 注册退出函数,根据文件pid判断是否存在进程
atexit.register(self.delpid)
pid = str(os.getpid())
with open(self.pidfile, 'w+') as f:
f.write('%s\n' % pid)
# file(self.pidfile, 'w+').write('%s\n' % pid)
示例15: get_process_umask
# 需要导入模块: import os [as 别名]
# 或者: from os import umask [as 别名]
def get_process_umask():
result = os.umask(0o22)
os.umask(result)
return result