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 | |
| 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>
| -rw-r--r-- | addons/base_import_module/models/ir_module.py | 4 | ||||
| -rw-r--r-- | odoo/service/db.py | 4 | ||||
| -rw-r--r-- | odoo/tools/osutil.py | 51 | ||||
| -rw-r--r-- | odoo/tools/translate.py | 3 |
4 files changed, 26 insertions, 36 deletions
diff --git a/addons/base_import_module/models/ir_module.py b/addons/base_import_module/models/ir_module.py index eda9562fd65..a9d064ad23a 100644 --- a/addons/base_import_module/models/ir_module.py +++ b/addons/base_import_module/models/ir_module.py @@ -5,6 +5,7 @@ import logging import lxml import os import sys +import tempfile import zipfile from os.path import join as opj @@ -13,7 +14,6 @@ from odoo import api, fields, models, _ from odoo.exceptions import UserError from odoo.modules import load_information_from_description_file from odoo.tools import convert_file, exception_to_unicode -from odoo.tools.osutil import tempdir _logger = logging.getLogger(__name__) @@ -123,7 +123,7 @@ class IrModule(models.Model): if zf.file_size > MAX_FILE_SIZE: raise UserError(_("File '%s' exceed maximum allowed file size") % zf.filename) - with tempdir() as module_dir: + with tempfile.TemporaryDirectory() as module_dir: import odoo.modules.module as module try: odoo.addons.__path__.append(module_dir) diff --git a/odoo/service/db.py b/odoo/service/db.py index b3c90bd99a0..58708ee2e2c 100644 --- a/odoo/service/db.py +++ b/odoo/service/db.py @@ -216,7 +216,7 @@ def dump_db(db_name, stream, backup_format='zip'): cmd.append(db_name) if backup_format == 'zip': - with odoo.tools.osutil.tempdir() as dump_dir: + with tempfile.TemporaryDirectory() as dump_dir: filestore = odoo.tools.config.filestore(db_name) if os.path.exists(filestore): shutil.copytree(filestore, os.path.join(dump_dir, 'filestore')) @@ -266,7 +266,7 @@ def restore_db(db, dump_file, copy=False): _create_empty_database(db) filestore_path = None - with odoo.tools.osutil.tempdir() as dump_dir: + with tempfile.TemporaryDirectory() as dump_dir: if zipfile.is_zipfile(dump_file): # v8 format with zipfile.ZipFile(dump_file, 'r') as z: 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): diff --git a/odoo/tools/translate.py b/odoo/tools/translate.py index 370e6cb361f..e185f9c809c 100644 --- a/odoo/tools/translate.py +++ b/odoo/tools/translate.py @@ -24,7 +24,6 @@ from lxml import etree, html import odoo from . import config, pycompat from .misc import file_open, get_iso_codes, SKIPPED_ELEMENT_TYPES -from .osutil import walksymlinks _logger = logging.getLogger(__name__) @@ -1006,7 +1005,7 @@ def trans_generate(lang, modules, cr): for (path, recursive) in path_list: _logger.debug("Scanning files of modules at %s", path) - for root, dummy, files in walksymlinks(path): + for root, dummy, files in os.walk(path, followlinks=True): for fname in fnmatch.filter(files, '*.py'): babel_extract_terms(fname, path, root, extract_keywords={'_': None, '_lt': None}) |
