diff options
| author | Xavier Morel <xmo@odoo.com> | 2019-10-30 11:02:41 +0000 |
|---|---|---|
| committer | Xavier Morel <xmo@odoo.com> | 2019-10-30 11:02:41 +0000 |
| commit | 355e3609732bdc1ef0563edadb8634a322d7d5af (patch) | |
| tree | f4ddc3978ddb5fd8567e3cd6dcbc45a6cd6d116c /odoo/tools/osutil.py | |
| parent | 8ef1be2e42f3c060db61a1a5d6b463a1cf1a1fd5 (diff) | |
[IMP] core: mark a few osutil functions as deprecated
* walksymlinks is useless, os.walk got followlinks in 2.6
* tempdir is redundant with tempfile.TemporaryDirectory added in 3.2
* listdir(recursive=False) is just os.listdir (I guess recursive=True
is somewhat useful)
* zip_dir is *not* redundant with shutil.make_archive:
- zip_dir handles the archived root differently
- zip_dir sorts files, make_archive sorts directories
- make_archive explicitly adds entries for directories (including
empty), sorts directories, doesn't sort files; zip_dir sorts
files (including with a custom key function) but ignores directories
- zip_dir filters out a bunch of trash files
closes odoo/odoo#39573
Signed-off-by: Christophe Simonis <chs@odoo.com>
Diffstat (limited to 'odoo/tools/osutil.py')
| -rw-r--r-- | odoo/tools/osutil.py | 51 |
1 files changed, 21 insertions, 30 deletions
diff --git a/odoo/tools/osutil.py b/odoo/tools/osutil.py index 27d4abd34a8..7ab7a8ced57 100644 --- a/odoo/tools/osutil.py +++ b/odoo/tools/osutil.py @@ -4,56 +4,43 @@ """ Some functions related to the os and os.path module """ - from contextlib import contextmanager +import logging import os from os.path import join as opj -import shutil import tempfile import zipfile -if os.name == 'nt': - import ctypes - import win32service as ws - import win32serviceutil as wsu +_logger = logging.getLogger(__name__) def listdir(dir, recursive=False): - """Allow to recursively get the file listing""" + """Allow to recursively get the file listing following symlinks, returns + paths relative to the provided `dir` except completely broken if the symlink + it follows leaves `dir`... + """ + if not recursive: + _logger.getChild('listdir').warning("Deprecated: just call os.listdir...") dir = os.path.normpath(dir) if not recursive: return os.listdir(dir) res = [] - for root, dirs, files in walksymlinks(dir): - root = root[len(dir)+1:] - res.extend([opj(root, f) for f in files]) + for root, _, files in os.walk(dir, followlinks=True): + r = os.path.relpath(root, dir) + # FIXME: what should happen if root is outside dir? + yield from (opj(r, f) for f in files) return res def walksymlinks(top, topdown=True, onerror=None): - """ - same as os.walk but follow symlinks - attention: all symlinks are walked before all normal directories - """ - for dirpath, dirnames, filenames in os.walk(top, topdown, onerror): - if topdown: - yield dirpath, dirnames, filenames - - symlinks = (dirname for dirname in dirnames if os.path.islink(os.path.join(dirpath, dirname))) - for s in symlinks: - for x in walksymlinks(os.path.join(dirpath, s), topdown, onerror): - yield x - - if not topdown: - yield dirpath, dirnames, filenames + _logger.getChild('walksymlinks').warning("Deprecated: use os.walk(followlinks=True) instead") + return os.walk(top, topdown=topdown, onerror=onerror, followlinks=True) @contextmanager def tempdir(): - tmpdir = tempfile.mkdtemp() - try: - yield tmpdir - finally: - shutil.rmtree(tmpdir) + _logger.getChild('tempdir').warning("Deprecated: use tempfile.TemporaryDirectory") + with tempfile.TemporaryDirectory() as d: + yield d def zip_dir(path, stream, include_dir=True, fnct_sort=None): # TODO add ignore list """ @@ -82,6 +69,10 @@ if os.name != 'nt': getppid = os.getppid is_running_as_nt_service = lambda: False else: + import ctypes + import win32service as ws + import win32serviceutil as wsu + # based on http://mail.python.org/pipermail/python-win32/2007-June/006174.html _TH32CS_SNAPPROCESS = 0x00000002 class _PROCESSENTRY32(ctypes.Structure): |
