aboutsummaryrefslogtreecommitdiff
path: root/hwpack/better_tarfile.py
blob: a0ce9b68328dd83c606d40d9cdb2a00957f94b87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from contextlib import contextmanager
from StringIO import StringIO
from tarfile import DIRTYPE, TarFile as StandardTarFile, TarInfo

"""Improvements to the standard library's tarfile module.

In particular this module provides a tarfile.TarFile subclass that aids
in adding paths to the tarfile that aren't present on the filesystem,
with the ability to specify file content as strings, and provide
default values for the mtime, uid, etc. of the created paths.
"""

@contextmanager
def writeable_tarfile(backing_file, mode="w", **kwargs):
    """A context manager to get a writeable better tarfile.

    :param backing_file: a file object to write the tarfile contents
        to.
    :param mode: the mode to open the tarfile with. Default is
        "w".
    :param kwargs: other keyword arguments to pass to the TarFile
        constructor.
    """
    tf = TarFile.open(mode=mode, fileobj=backing_file, **kwargs)
    try:
        yield tf
    finally:
        tf.close()


class TarFile(StandardTarFile):
    """An improvement to tarfile that can add paths not on the filesystem.

    With the standard tarfile implementation adding paths that are not
    present on the filesystem is convoluted. This subclass adds methods
    to create paths in the tarfile that are not present on the filesystem.

    In addition, it can take constructor parameters to set the defaults
    of various attributes of the paths that it adds.
    """

    def __init__(self, *args, **kwargs):
        """Create a TarFile.

        :param default_mtime: the default mtime to create paths with,
            an int or None to use the stdlib default.
        :param default_uid: the default user id to set as the owner of
            created paths, an int or None to use the stdlib default.
        :param default_gid: the default group id to set as the owner of
            created paths, an int or None to use the stdlib default.
        :param default_uname: the default user name to set as the owner
            of created paths, a string, or None to use the stdlib default.
        :param default_gname: the default group name ot set as the owner
            of created paths, a string, or None to use the stdlib default.
        """
        self.default_mtime = kwargs.pop("default_mtime", None)
        self.default_uid = kwargs.pop("default_uid", None)
        self.default_gid = kwargs.pop("default_gid", None)
        self.default_uname = kwargs.pop("default_uname", None)
        self.default_gname = kwargs.pop("default_gname", None)
        super(TarFile, self).__init__(*args, **kwargs)

    def _set_defaults(self, tarinfo):
        if self.default_mtime is not None:
            tarinfo.mtime = self.default_mtime
        if self.default_uid is not None:
            tarinfo.uid = self.default_uid
        if self.default_gid is not None:
            tarinfo.gid = self.default_gid
        if self.default_uname is not None:
            tarinfo.uname = self.default_uname
        if self.default_gname is not None:
            tarinfo.gname = self.default_gname

    def create_file_from_string(self, filename, content):
        """Create a file with the contents passed as a string.

        :param filename: the path to put the file at inside the
            tarfile.
        :param content: the content to put in the created file.
        """
        tarinfo = TarInfo(name=filename)
        tarinfo.size = len(content)
        self._set_defaults(tarinfo)
        fileobj = StringIO(content)
        self.addfile(tarinfo, fileobj=fileobj)

    def create_dir(self, path):
        """Create a directory within the tarfile.

        :param path: the path to put the directory at.
        """
        tarinfo = TarInfo(name=path)
        tarinfo.type = DIRTYPE
        tarinfo.mode = 0755
        self._set_defaults(tarinfo)
        self.addfile(tarinfo)