~openerp-community/openobject-addons/trunk-addons-community

« back to all changes in this revision

Viewing changes to backup_system/_utils.py

  • Committer: Dukai Gábor
  • Date: 2010-08-12 12:28:58 UTC
  • Revision ID: gdukai@gmail.com-20100812122858-uq34gxr1zvxa5pzb
[ADD] backup_system: a system backup framework to run python scripts and have an OpenERP interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Utility functions for copying and archiving files and directory trees.
 
2
 
 
3
XXX The functions here don't copy the resource fork or other metadata on Mac.
 
4
 
 
5
Copied from python 2.7
 
6
"""
 
7
 
 
8
import os
 
9
 
 
10
try:
 
11
    from pwd import getpwnam
 
12
except ImportError:
 
13
    getpwnam = None
 
14
 
 
15
try:
 
16
    from grp import getgrnam
 
17
except ImportError:
 
18
    getgrnam = None
 
19
    
 
20
class ExecError(EnvironmentError):
 
21
    """Raised when a command could not be executed"""
 
22
 
 
23
def _get_gid(name):
 
24
    """Returns a gid, given a group name."""
 
25
    if getgrnam is None or name is None:
 
26
        return None
 
27
    try:
 
28
        result = getgrnam(name)
 
29
    except KeyError:
 
30
        result = None
 
31
    if result is not None:
 
32
        return result[2]
 
33
    return None
 
34
 
 
35
def _get_uid(name):
 
36
    """Returns an uid, given a user name."""
 
37
    if getpwnam is None or name is None:
 
38
        return None
 
39
    try:
 
40
        result = getpwnam(name)
 
41
    except KeyError:
 
42
        result = None
 
43
    if result is not None:
 
44
        return result[2]
 
45
    return None
 
46
 
 
47
def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
 
48
                  owner=None, group=None, logger=None):
 
49
    """Create a (possibly compressed) tar file from all the files under
 
50
    'base_dir'.
 
51
 
 
52
    'compress' must be "gzip" (the default), "bzip2", or None.
 
53
 
 
54
    'owner' and 'group' can be used to define an owner and a group for the
 
55
    archive that is being built. If not provided, the current owner and group
 
56
    will be used.
 
57
 
 
58
    The output tar file will be named 'base_dir' +  ".tar", possibly plus
 
59
    the appropriate compression extension (".gz", or ".bz2").
 
60
 
 
61
    Returns the output filename.
 
62
    """
 
63
    tar_compression = {'gzip': 'gz', 'bzip2': 'bz2', None: ''}
 
64
    compress_ext = {'gzip': '.gz', 'bzip2': '.bz2'}
 
65
 
 
66
    # flags for compression program, each element of list will be an argument
 
67
    if compress is not None and compress not in compress_ext.keys():
 
68
        raise ValueError, \
 
69
              ("bad value for 'compress': must be None, 'gzip' or 'bzip2'")
 
70
 
 
71
    archive_name = base_name + '.tar' + compress_ext.get(compress, '')
 
72
    archive_dir = os.path.dirname(archive_name)
 
73
 
 
74
    if not os.path.exists(archive_dir):
 
75
        if logger is not None:
 
76
            logger.info("creating %s" % archive_dir)
 
77
        if not dry_run:
 
78
            os.makedirs(archive_dir)
 
79
 
 
80
 
 
81
    # creating the tarball
 
82
    import _tarfile27 as tarfile  # late import so Python build itself doesn't break
 
83
 
 
84
    if logger is not None:
 
85
        logger.info('Creating tar archive')
 
86
 
 
87
    uid = _get_uid(owner)
 
88
    gid = _get_gid(group)
 
89
 
 
90
    def _set_uid_gid(tarinfo):
 
91
        if gid is not None:
 
92
            tarinfo.gid = gid
 
93
            tarinfo.gname = group
 
94
        if uid is not None:
 
95
            tarinfo.uid = uid
 
96
            tarinfo.uname = owner
 
97
        return tarinfo
 
98
 
 
99
    if not dry_run:
 
100
        tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress])
 
101
        try:
 
102
            tar.add(base_dir, filter=_set_uid_gid)
 
103
        finally:
 
104
            tar.close()
 
105
 
 
106
    return archive_name
 
107
 
 
108
def _call_external_zip(base_dir, zip_filename, verbose=False, dry_run=False):
 
109
    # XXX see if we want to keep an external call here
 
110
    if verbose:
 
111
        zipoptions = "-r"
 
112
    else:
 
113
        zipoptions = "-rq"
 
114
    from distutils.errors import DistutilsExecError
 
115
    from distutils.spawn import spawn
 
116
    try:
 
117
        spawn(["zip", zipoptions, zip_filename, base_dir], dry_run=dry_run)
 
118
    except DistutilsExecError:
 
119
        # XXX really should distinguish between "couldn't find
 
120
        # external 'zip' command" and "zip failed".
 
121
        raise ExecError, \
 
122
            ("unable to create zip file '%s': "
 
123
            "could neither import the 'zipfile' module nor "
 
124
            "find a standalone zip utility") % zip_filename
 
125
 
 
126
def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
 
127
    """Create a zip file from all the files under 'base_dir'.
 
128
 
 
129
    The output zip file will be named 'base_dir' + ".zip".  Uses either the
 
130
    "zipfile" Python module (if available) or the InfoZIP "zip" utility
 
131
    (if installed and found on the default search path).  If neither tool is
 
132
    available, raises ExecError.  Returns the name of the output zip
 
133
    file.
 
134
    """
 
135
    zip_filename = base_name + ".zip"
 
136
    archive_dir = os.path.dirname(base_name)
 
137
 
 
138
    if not os.path.exists(archive_dir):
 
139
        if logger is not None:
 
140
            logger.info("creating %s", archive_dir)
 
141
        if not dry_run:
 
142
            os.makedirs(archive_dir)
 
143
 
 
144
    # If zipfile module is not available, try spawning an external 'zip'
 
145
    # command.
 
146
    try:
 
147
        import zipfile
 
148
    except ImportError:
 
149
        zipfile = None
 
150
 
 
151
    if zipfile is None:
 
152
        _call_external_zip(base_dir, zip_filename, verbose, dry_run)
 
153
    else:
 
154
        if logger is not None:
 
155
            logger.info("creating '%s' and adding '%s' to it",
 
156
                        zip_filename, base_dir)
 
157
 
 
158
        if not dry_run:
 
159
            zip = zipfile.ZipFile(zip_filename, "w",
 
160
                                  compression=zipfile.ZIP_DEFLATED)
 
161
 
 
162
            for dirpath, dirnames, filenames in os.walk(base_dir):
 
163
                for name in filenames:
 
164
                    path = os.path.normpath(os.path.join(dirpath, name))
 
165
                    if os.path.isfile(path):
 
166
                        zip.write(path, path)
 
167
                        if logger is not None:
 
168
                            logger.info("adding '%s'", path)
 
169
            zip.close()
 
170
 
 
171
    return zip_filename
 
172
 
 
173
_ARCHIVE_FORMATS = {
 
174
    'gztar': (_make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),
 
175
    'bztar': (_make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"),
 
176
    'tar':   (_make_tarball, [('compress', None)], "uncompressed tar file"),
 
177
    'zip':   (_make_zipfile, [],"ZIP file")
 
178
    }
 
179
 
 
180
def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
 
181
                 dry_run=0, owner=None, group=None, logger=None):
 
182
    """Create an archive file (eg. zip or tar).
 
183
 
 
184
    'base_name' is the name of the file to create, minus any format-specific
 
185
    extension; 'format' is the archive format: one of "zip", "tar", "bztar"
 
186
    or "gztar".
 
187
 
 
188
    'root_dir' is a directory that will be the root directory of the
 
189
    archive; ie. we typically chdir into 'root_dir' before creating the
 
190
    archive.  'base_dir' is the directory where we start archiving from;
 
191
    ie. 'base_dir' will be the common prefix of all files and
 
192
    directories in the archive.  'root_dir' and 'base_dir' both default
 
193
    to the current directory.  Returns the name of the archive file.
 
194
 
 
195
    'owner' and 'group' are used when creating a tar archive. By default,
 
196
    uses the current owner and group.
 
197
    """
 
198
    save_cwd = os.getcwd()
 
199
    if root_dir is not None:
 
200
        if logger is not None:
 
201
            logger.debug("changing into '%s'", root_dir)
 
202
        base_name = os.path.abspath(base_name)
 
203
        if not dry_run:
 
204
            os.chdir(root_dir)
 
205
 
 
206
    if base_dir is None:
 
207
        base_dir = os.curdir
 
208
 
 
209
    kwargs = {'dry_run': dry_run, 'logger': logger}
 
210
 
 
211
    try:
 
212
        format_info = _ARCHIVE_FORMATS[format]
 
213
    except KeyError:
 
214
        raise ValueError, "unknown archive format '%s'" % format
 
215
 
 
216
    func = format_info[0]
 
217
    for arg, val in format_info[1]:
 
218
        kwargs[arg] = val
 
219
 
 
220
    if format != 'zip':
 
221
        kwargs['owner'] = owner
 
222
        kwargs['group'] = group
 
223
 
 
224
    try:
 
225
        filename = func(base_name, base_dir, **kwargs)
 
226
    finally:
 
227
        if root_dir is not None:
 
228
            if logger is not None:
 
229
                logger.debug("changing back to '%s'", save_cwd)
 
230
            os.chdir(save_cwd)
 
231
 
 
232
    return filename