Browse Source

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

From: @liangyongxiong1024
Reviewed-by: @ouwenchang,@yelihua
Signed-off-by: @yelihua
pull/1313/MERGE
mindspore-ci-bot Gitee 4 years ago
parent
commit
c7b30146a7
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."""

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

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

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

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

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


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

@@ -15,6 +15,7 @@
"""Hook module."""
import os
import stat
import threading
from importlib import import_module
@@ -132,10 +133,8 @@ def init(workspace='', config='', **kwargs):
Raises:
FileSystemPermissionError, if workspace is not allowed to access or available.
"""
permissions = os.R_OK | os.W_OK | os.X_OK
# set umask to 0o077
os.umask(permissions << 3 | permissions)
os.umask(stat.S_IRWXG | stat.S_IRWXO)
# assign argument values into environment
if workspace:
@@ -151,7 +150,7 @@ def init(workspace='', config='', **kwargs):
settings.refresh()
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))
else:
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(dfn):
os.remove(dfn)
os.chmod(sfn, stat.S_IREAD)
os.chmod(sfn, stat.S_IRUSR)
os.rename(sfn, dfn)

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

# 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:
if self.backupCount > 0:
@@ -60,11 +60,11 @@ class MultiCompatibleRotatingFileHandler(RotatingFileHandler):
if os.path.exists(dfn):
os.remove(dfn)

os.chmod(self.baseFilename, stat.S_IREAD)
os.chmod(self.baseFilename, stat.S_IRUSR)
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:
self.stream = self._open()
@@ -75,7 +75,7 @@ class MultiCompatibleRotatingFileHandler(RotatingFileHandler):

def _open(self):
"""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)
return new_log

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

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(
filename=os.path.join(logfile_dir, '{}.{}.log'.format(log_name, settings.PORT)),


Loading…
Cancel
Save