Browse Source

replace built-in function open() with os.open() to set file permission

pull/1313/head
liangyongxiong 4 years ago
parent
commit
e7be4b221c
3 changed files with 14 additions and 19 deletions
  1. +3
    -4
      mindinsight/utils/command.py
  2. +3
    -4
      mindinsight/utils/hook.py
  3. +8
    -11
      mindinsight/utils/log.py

+ 3
- 4
mindinsight/utils/command.py View File

@@ -14,8 +14,9 @@
# ============================================================================ # ============================================================================
"""Command module.""" """Command module."""


import sys
import os import os
import sys
import stat
import argparse import argparse
from importlib import import_module from importlib import import_module


@@ -91,10 +92,8 @@ def main():
console.error('Python version should be at least 3.7') console.error('Python version should be at least 3.7')
sys.exit(1) sys.exit(1)


permissions = os.R_OK | os.W_OK | os.X_OK

# set umask to 0o077 # set umask to 0o077
os.umask(permissions << 3 | permissions)
os.umask(stat.S_IRWXG | stat.S_IRWXO)


parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog='mindinsight', prog='mindinsight',


+ 3
- 4
mindinsight/utils/hook.py View File

@@ -15,6 +15,7 @@
"""Hook module.""" """Hook module."""
import os import os
import stat
import threading import threading
from importlib import import_module from importlib import import_module
@@ -132,10 +133,8 @@ def init(workspace='', config='', **kwargs):
Raises: Raises:
FileSystemPermissionError, if workspace is not allowed to access or available. FileSystemPermissionError, if workspace is not allowed to access or available.
""" """
permissions = os.R_OK | os.W_OK | os.X_OK
# set umask to 0o077 # set umask to 0o077
os.umask(permissions << 3 | permissions)
os.umask(stat.S_IRWXG | stat.S_IRWXO)
# assign argument values into environment # assign argument values into environment
if workspace: if workspace:
@@ -151,7 +150,7 @@ def init(workspace='', config='', **kwargs):
settings.refresh() settings.refresh()
if os.path.exists(settings.WORKSPACE): if os.path.exists(settings.WORKSPACE):
if not os.access(settings.WORKSPACE, permissions):
if not os.access(settings.WORKSPACE, os.R_OK | os.W_OK | os.X_OK):
raise FileSystemPermissionError('Workspace {} not allowed to access'.format(workspace)) raise FileSystemPermissionError('Workspace {} not allowed to access'.format(workspace))
else: else:
try: try:


+ 8
- 11
mindinsight/utils/log.py View File

@@ -38,7 +38,7 @@ class MultiCompatibleRotatingFileHandler(RotatingFileHandler):
if os.path.exists(sfn): if os.path.exists(sfn):
if os.path.exists(dfn): if os.path.exists(dfn):
os.remove(dfn) os.remove(dfn)
os.chmod(sfn, stat.S_IREAD)
os.chmod(sfn, stat.S_IRUSR)
os.rename(sfn, dfn) os.rename(sfn, dfn)


def doRollover(self): def doRollover(self):
@@ -48,8 +48,8 @@ class MultiCompatibleRotatingFileHandler(RotatingFileHandler):
self.stream = None self.stream = None


# Attain an exclusive lock with blocking mode by `fcntl` module. # Attain an exclusive lock with blocking mode by `fcntl` module.
with open(self.baseFilename, 'a') as file_pointer:
fcntl.lockf(file_pointer.fileno(), fcntl.LOCK_EX)
with os.open(self.baseFilename, os.O_APPEND | os.O_CREAT, mode=stat.S_IRUSR | stat.S_IWUSR) as fd:
fcntl.lockf(fd, fcntl.LOCK_EX)


try: try:
if self.backupCount > 0: if self.backupCount > 0:
@@ -60,11 +60,11 @@ class MultiCompatibleRotatingFileHandler(RotatingFileHandler):
if os.path.exists(dfn): if os.path.exists(dfn):
os.remove(dfn) os.remove(dfn)


os.chmod(self.baseFilename, stat.S_IREAD)
os.chmod(self.baseFilename, stat.S_IRUSR)
self.rotate(self.baseFilename, dfn) self.rotate(self.baseFilename, dfn)


with open(self.baseFilename, 'a'):
os.chmod(self.baseFilename, stat.S_IREAD | stat.S_IWRITE)
fd = os.open(self.baseFilename, os.O_APPEND | os.O_CREAT, mode=stat.S_IRUSR | stat.S_IWUSR)
os.close(fd)


if not self.delay: if not self.delay:
self.stream = self._open() self.stream = self._open()
@@ -75,7 +75,7 @@ class MultiCompatibleRotatingFileHandler(RotatingFileHandler):


def _open(self): def _open(self):
"""Open the current base file with the (original) mode and encoding.""" """Open the current base file with the (original) mode and encoding."""
new_log = open(self.baseFilename, self.mode, encoding=self.encoding)
new_log = super()._open()
os.chmod(self.baseFilename, stat.S_IREAD | stat.S_IWRITE) os.chmod(self.baseFilename, stat.S_IREAD | stat.S_IWRITE)
return new_log return new_log


@@ -218,10 +218,7 @@ def setup_logger(sub_module, log_name, **kwargs):
'backupCount should be int type and > 0.') 'backupCount should be int type and > 0.')


logfile_dir = os.path.join(settings.WORKSPACE, 'log', sub_module) logfile_dir = os.path.join(settings.WORKSPACE, 'log', sub_module)

permissions = os.R_OK | os.W_OK | os.X_OK
mode = permissions << 6
os.makedirs(logfile_dir, mode=mode, exist_ok=True)
os.makedirs(logfile_dir, mode=stat.S_IRWXU, exist_ok=True)


logfile_handler = MultiCompatibleRotatingFileHandler( logfile_handler = MultiCompatibleRotatingFileHandler(
filename=os.path.join(logfile_dir, '{}.{}.log'.format(log_name, settings.PORT)), filename=os.path.join(logfile_dir, '{}.{}.log'.format(log_name, settings.PORT)),


Loading…
Cancel
Save