Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions mig/shared/fileio.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
Expand All @@ -20,7 +20,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Check warning on line 23 in mig/shared/fileio.py

View workflow job for this annotation

GitHub Actions / Style check python and annotate

line too long (81 > 80 characters)
#
# -- END_HEADER ---
#
Expand All @@ -43,7 +43,7 @@
# NOTE: We expose optimized walk function directly for ease and efficiency.
# Requires stand-alone scandir module on python 2 whereas the native os
# functions are built-in and optimized similarly on python 3+
slow_walk, slow_listdir = False, False

Check failure on line 46 in mig/shared/fileio.py

View workflow job for this annotation

GitHub Actions / Style check python and annotate

unused variable 'slow_listdir' (60% confidence)
if sys.version_info[0] > 2:
from os import walk, listdir
else:
Expand All @@ -51,12 +51,12 @@
from distutils.version import StrictVersion
from scandir import walk, listdir, __version__ as scandir_version
if StrictVersion(scandir_version) < StrictVersion("1.3"):
# Important os.walk compatibility utf8 fixes were not added until 1.3

Check warning on line 54 in mig/shared/fileio.py

View workflow job for this annotation

GitHub Actions / Style check python and annotate

line too long (81 > 80 characters)
raise ImportError(
"scandir version is too old: fall back to os.walk")
except ImportError as err:
# print("DEBUG: not using scandir: %s" % err)
slow_walk = slow_listdir = True

Check failure on line 59 in mig/shared/fileio.py

View workflow job for this annotation

GitHub Actions / Style check python and annotate

unused variable 'slow_listdir' (60% confidence)
walk = os.walk
listdir = os.listdir

Expand All @@ -71,7 +71,7 @@
exit(1)


def _auto_adjust_mode(data, mode):

Check failure on line 74 in mig/shared/fileio.py

View workflow job for this annotation

GitHub Actions / Style check python and annotate

unused function '_auto_adjust_mode' (60% confidence)
"""Select suitable file open mode based on string type of data. I.e. whether
to use binary or text mode depending on data in bytes or unicode format.
"""
Expand Down Expand Up @@ -127,7 +127,7 @@
if offset > 0:
try:
filehandle.seek(offset)
except:

Check warning on line 130 in mig/shared/fileio.py

View workflow job for this annotation

GitHub Actions / Style check python and annotate

do not use bare 'except'
filehandle.seek(0, 2)
file_size = filehandle.tell()
for _ in range(offset - file_size):
Expand All @@ -150,7 +150,9 @@
return False

# TODO: toggle default force_string here when we have auto mode select?


def write_chunk(path, chunk, offset, logger, mode='r+b', force_string=True):

Check failure on line 155 in mig/shared/fileio.py

View workflow job for this annotation

GitHub Actions / Style check python and annotate

unused function 'write_chunk' (60% confidence)
"""Wrapper to handle writing of chunks with offset to path.
Creates file first if it doesn't already exist.
"""
Expand All @@ -176,7 +178,7 @@
old_umask = os.umask(umask)

# TODO: enable this again once throuroughly tested and assured py2+3 safe
#mode = _auto_adjust_mode(content, mode)
# mode = _auto_adjust_mode(content, mode)

retval = _write_chunk(path, content, offset=0, logger=logger, mode=mode,
make_parent=make_parent, create_file=False,
Expand All @@ -188,7 +190,7 @@
return retval


def write_file_lines(lines, path, logger, mode='w', make_parent=True, umask=None):

Check failure on line 193 in mig/shared/fileio.py

View workflow job for this annotation

GitHub Actions / Style check python and annotate

unused function 'write_file_lines' (60% confidence)

Check warning on line 193 in mig/shared/fileio.py

View workflow job for this annotation

GitHub Actions / Style check python and annotate

line too long (82 > 80 characters)
"""Wrapper to handle writing of lines of content to path"""
return write_file(''.join(lines), path, logger, mode, make_parent, umask)

Expand All @@ -210,7 +212,7 @@
return content


def read_file_lines(path, logger, mode='r'):

Check failure on line 215 in mig/shared/fileio.py

View workflow job for this annotation

GitHub Actions / Style check python and annotate

unused function 'read_file_lines' (60% confidence)
"""Wrapper to handle reading lines of content from path"""
contents = read_file(path, logger, mode)
if contents is None:
Expand All @@ -224,7 +226,7 @@
return lines


def read_head_lines(path, lines, logger, mode='r'):

Check failure on line 229 in mig/shared/fileio.py

View workflow job for this annotation

GitHub Actions / Style check python and annotate

unused function 'read_head_lines' (60% confidence)
"""Read first lines from path"""
if not logger:
logger = null_logger("dummy")
Expand All @@ -249,7 +251,7 @@
# NOTE: last line is likely truncated when read like this
while pos < size and contents.count(b'\n') < lines:
pos = head_fd.tell()
# logger.debug("read %db at pos %d from %r" % (step_size, pos, path))

Check warning on line 254 in mig/shared/fileio.py

View workflow job for this annotation

GitHub Actions / Style check python and annotate

line too long (81 > 80 characters)
contents += head_fd.read(step_size)
step_size *= 2
head_fd.close()
Expand All @@ -263,7 +265,7 @@
return out_lines[:lines]


def read_tail_lines(path, lines, logger, mode='r'):

Check failure on line 268 in mig/shared/fileio.py

View workflow job for this annotation

GitHub Actions / Style check python and annotate

unused function 'read_tail_lines' (60% confidence)
"""Read last lines from path"""
if not logger:
logger = null_logger("dummy")
Expand Down Expand Up @@ -302,7 +304,7 @@
return out_lines[-lines:]


def get_file_size(path, logger):

Check failure on line 307 in mig/shared/fileio.py

View workflow job for this annotation

GitHub Actions / Style check python and annotate

unused function 'get_file_size' (60% confidence)
"""Wrapper to handle getsize of path"""
if not logger:
logger = null_logger("dummy")
Expand All @@ -311,7 +313,7 @@
return os.path.getsize(path)
except Exception as err:
logger.error("could not get size for %r: %s" % (path, err))
result = -1
return -1


def delete_file(path, logger, allow_broken_symlink=False, allow_missing=False):
Expand Down Expand Up @@ -497,6 +499,7 @@
"""Write message to notify home"""
if not logger:
logger = null_logger("dummy")
filepath = 'UNDEFINED'
try:
(filedescriptor, filepath) = make_temp_file(
suffix='.%s' % time.time(),
Expand Down Expand Up @@ -752,7 +755,7 @@
argument decides if any symlinks in path are expanded before this check
and it is on by default.
"""
return _check_access(path, os.O_RDONLY, parent_dir, follow_symlink)
return _check_access(path, os.R_OK, parent_dir, follow_symlink)


def check_write_access(path, parent_dir=False, follow_symlink=True):
Expand All @@ -762,8 +765,7 @@
decides if any symlinks in path are expanded before this check and it is
on by default.
"""
# IMPORTANT: we need to use RDWR rather than WRONLY here.
return _check_access(path, os.O_RDWR, parent_dir, follow_symlink)
return _check_access(path, os.W_OK, parent_dir, follow_symlink)


def make_temp_file(suffix='', prefix='tmp', dir=None, text=False):
Expand All @@ -787,8 +789,8 @@
os.write(filehandle, force_utf8(contents))
os.close(filehandle)
except Exception as exc:
_logger.error("failed to write tempfile %r: %s" % (tmpname, exc))
tmpname = None
_logger.error("failed to write tempfile %r: %s" % (tmpname, exc))
return tmpname


Expand Down Expand Up @@ -884,7 +886,7 @@
# Clean up
try:
lock_handle.close()
except:

Check warning on line 889 in mig/shared/fileio.py

View workflow job for this annotation

GitHub Actions / Style check python and annotate

do not use bare 'except'
pass
# If non-blocking flock gave up an IOError will be raised and the
# exception will have an errno attribute set to EACCES or EAGAIN.
Expand Down Expand Up @@ -928,7 +930,7 @@
if close:
try:
lock_handle.close()
except:

Check warning on line 933 in mig/shared/fileio.py

View workflow job for this annotation

GitHub Actions / Style check python and annotate

do not use bare 'except'
pass


Expand Down Expand Up @@ -1019,7 +1021,7 @@
chroot_exceptions.append(os.path.abspath(configuration.resource_home))
if configuration.site_enable_seafile and configuration.seafile_mount:
chroot_exceptions.append(os.path.abspath(configuration.seafile_mount))
# Allow access to mig_system_storage used for merging multiple storage backends

Check warning on line 1024 in mig/shared/fileio.py

View workflow job for this annotation

GitHub Actions / Style check python and annotate

line too long (83 > 80 characters)
chroot_exceptions.append(os.path.abspath(configuration.mig_system_storage))
return chroot_exceptions

Expand Down