[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>
This commit is contained in:
Xavier Morel
2019-10-30 11:02:41 +00:00
parent 8ef1be2e42
commit 355e360973
4 changed files with 26 additions and 36 deletions
@@ -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)
+2 -2
View File
@@ -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:
+21 -30
View File
@@ -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):
+1 -2
View File
@@ -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})