~openerp-dev/openobject-server/trunk-fix-unlink-irmodelfields-chs

« back to all changes in this revision

Viewing changes to openerp/tools/osutil.py

  • Committer: Christophe Simonis
  • Date: 2014-06-05 09:56:14 UTC
  • mfrom: (4955.1.265 openobject-server)
  • Revision ID: chs@openerp.com-20140605095614-y6i30cg4hes0pvxr
mergeĀ upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
Some functions related to the os and os.path module
24
24
"""
25
25
 
 
26
from contextlib import contextmanager
26
27
import os
27
28
from os.path import join as opj
 
29
import shutil
 
30
import tempfile
 
31
import zipfile
28
32
 
29
33
if os.name == 'nt':
30
34
    import ctypes
61
65
        if not topdown:
62
66
            yield dirpath, dirnames, filenames
63
67
 
 
68
@contextmanager
 
69
def tempdir():
 
70
    tmpdir = tempfile.mkdtemp()
 
71
    try:
 
72
        yield tmpdir
 
73
    finally:
 
74
        shutil.rmtree(tmpdir)
 
75
 
 
76
def zip_dir(path, stream, include_dir=True):      # TODO add ignore list
 
77
    path = os.path.normpath(path)
 
78
    len_prefix = len(os.path.dirname(path)) if include_dir else len(path)
 
79
    if len_prefix:
 
80
        len_prefix += 1
 
81
 
 
82
    with zipfile.ZipFile(stream, 'w', compression=zipfile.ZIP_DEFLATED, allowZip64=True) as zipf:
 
83
        for dirpath, dirnames, filenames in os.walk(path):
 
84
            for fname in filenames:
 
85
                bname, ext = os.path.splitext(fname)
 
86
                ext = ext or bname
 
87
                if ext not in ['.pyc', '.pyo', '.swp', '.DS_Store']:
 
88
                    path = os.path.normpath(os.path.join(dirpath, fname))
 
89
                    if os.path.isfile(path):
 
90
                        zipf.write(path, path[len_prefix:])
 
91
 
64
92
 
65
93
if os.name != 'nt':
66
94
    getppid = os.getppid