2014-01-08 21:33:04 +00:00
|
|
|
# Copyright (C) 2005-2014 Bastian Kleineidam
|
2005-12-17 19:18:08 +00:00
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
2009-07-24 21:58:20 +00:00
|
|
|
# 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.
|
2005-12-17 19:18:08 +00:00
|
|
|
"""
|
|
|
|
|
File and path utilities.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import os
|
2006-05-13 13:41:46 +00:00
|
|
|
import locale
|
2006-02-03 20:42:56 +00:00
|
|
|
import stat
|
2011-01-06 08:52:11 +00:00
|
|
|
import tempfile
|
2012-11-26 17:49:07 +00:00
|
|
|
import importlib
|
2020-03-31 18:46:31 +00:00
|
|
|
from functools import lru_cache
|
2005-12-17 19:18:08 +00:00
|
|
|
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def has_module(name, without_error=True):
|
2008-04-27 11:39:21 +00:00
|
|
|
"""Test if given module can be imported.
|
2013-02-08 20:36:02 +00:00
|
|
|
@param without_error: True if module must not throw any errors when importing
|
2005-12-17 19:18:08 +00:00
|
|
|
@return: flag if import is successful
|
|
|
|
|
@rtype: bool
|
|
|
|
|
"""
|
|
|
|
|
try:
|
2012-11-26 17:49:07 +00:00
|
|
|
importlib.import_module(name)
|
2006-04-03 19:15:07 +00:00
|
|
|
return True
|
2013-02-08 20:36:02 +00:00
|
|
|
except ImportError:
|
2005-12-17 19:18:08 +00:00
|
|
|
return False
|
2013-02-08 20:36:02 +00:00
|
|
|
except Exception:
|
|
|
|
|
# some modules raise errors when intitializing
|
|
|
|
|
return not without_error
|
2005-12-17 19:18:08 +00:00
|
|
|
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def get_mtime(filename):
|
2008-04-27 11:39:21 +00:00
|
|
|
"""Return modification time of filename or zero on errors."""
|
2006-02-03 20:42:56 +00:00
|
|
|
try:
|
2012-11-30 23:05:14 +00:00
|
|
|
return os.path.getmtime(filename)
|
2006-02-03 20:42:56 +00:00
|
|
|
except os.error:
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def get_size(filename):
|
2010-07-29 17:52:26 +00:00
|
|
|
"""Return file size in Bytes, or -1 on error."""
|
|
|
|
|
try:
|
2012-11-30 23:05:14 +00:00
|
|
|
return os.path.getsize(filename)
|
2010-07-29 17:52:26 +00:00
|
|
|
except os.error:
|
|
|
|
|
return -1
|
|
|
|
|
|
|
|
|
|
|
2006-05-13 13:41:46 +00:00
|
|
|
# http://developer.gnome.org/doc/API/2.0/glib/glib-running.html
|
|
|
|
|
if "G_FILENAME_ENCODING" in os.environ:
|
|
|
|
|
FSCODING = os.environ["G_FILENAME_ENCODING"].split(",")[0]
|
|
|
|
|
if FSCODING == "@locale":
|
|
|
|
|
FSCODING = locale.getpreferredencoding()
|
|
|
|
|
elif "G_BROKEN_FILENAMES" in os.environ:
|
|
|
|
|
FSCODING = locale.getpreferredencoding()
|
|
|
|
|
else:
|
|
|
|
|
FSCODING = "utf-8"
|
|
|
|
|
|
2020-05-30 16:01:36 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def path_safe(path):
|
2019-10-05 18:38:57 +00:00
|
|
|
"""Ensure path string is compatible with the platform file system encoding."""
|
2020-06-18 18:27:06 +00:00
|
|
|
if path and not os.path.supports_unicode_filenames:
|
2019-10-05 18:38:57 +00:00
|
|
|
path = path.encode(FSCODING, "replace").decode(FSCODING)
|
2006-05-13 13:41:46 +00:00
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def get_temp_file(mode='r', **kwargs):
|
2011-01-06 08:52:11 +00:00
|
|
|
"""Return tuple (open file object, filename) pointing to a temporary
|
|
|
|
|
file."""
|
|
|
|
|
fd, filename = tempfile.mkstemp(**kwargs)
|
2012-06-10 09:03:43 +00:00
|
|
|
return os.fdopen(fd, mode), filename
|
2011-05-26 05:28:14 +00:00
|
|
|
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def is_tty(fp):
|
2011-05-26 05:28:14 +00:00
|
|
|
"""Check if is a file object pointing to a TTY."""
|
2020-05-30 16:01:36 +00:00
|
|
|
return hasattr(fp, "isatty") and fp.isatty()
|
2012-01-26 19:23:15 +00:00
|
|
|
|
|
|
|
|
|
2020-03-31 18:46:31 +00:00
|
|
|
@lru_cache(128)
|
2012-01-26 19:23:15 +00:00
|
|
|
def is_readable(filename):
|
|
|
|
|
"""Check if file is a regular file and is readable."""
|
|
|
|
|
return os.path.isfile(filename) and os.access(filename, os.R_OK)
|
|
|
|
|
|
|
|
|
|
|
2012-10-15 12:36:10 +00:00
|
|
|
def is_accessable_by_others(filename):
|
2022-09-02 09:20:02 +00:00
|
|
|
"""Check if file is group or world accessible."""
|
2012-11-30 23:05:14 +00:00
|
|
|
mode = os.stat(filename)[stat.ST_MODE]
|
2012-10-15 12:36:10 +00:00
|
|
|
return mode & (stat.S_IRWXG | stat.S_IRWXO)
|
|
|
|
|
|
|
|
|
|
|
2014-02-28 23:12:34 +00:00
|
|
|
def is_writable_by_others(filename):
|
|
|
|
|
"""Check if file or directory is world writable."""
|
|
|
|
|
mode = os.stat(filename)[stat.ST_MODE]
|
|
|
|
|
return mode & stat.S_IWOTH
|