fffce99edb1c4862e2cb9d009c7c76454fce9d7e
[osm/SO.git] / rwlaunchpad / plugins / rwlaunchpadtasklet / rift / package / archive.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import io
19 import os
20 import tarfile
21 import time
22
23 from . import package
24
25 class ArchiveError(Exception):
26 pass
27
28
29 def get_size(hdl):
30 """ Get number of bytes of content within file hdl
31 Set the file position to original position before returning
32
33 Returns:
34 Number of bytes in the hdl file object
35 """
36 old_pos = hdl.tell()
37 hdl.seek(0, os.SEEK_END)
38 size = hdl.tell()
39 hdl.seek(old_pos)
40
41 return size
42
43
44 class TarPackageArchive(object):
45 """ This class represents a package stored within a tar.gz archive file """
46 def __init__(self, log, tar_file_hdl, mode="r"):
47 self._log = log
48 self._tar_filehdl = tar_file_hdl
49 self._tar_infos = {}
50
51 self._tarfile = tarfile.open(fileobj=tar_file_hdl, mode=mode)
52
53 self.load_archive()
54
55 @classmethod
56 def from_package(cls, log, pkg, tar_file_hdl):
57 """ Creates a TarPackageArchive from a existing Package
58
59 Arguments:
60 log - logger
61 pkg - a DescriptorPackage instance
62 tar_file_hdl - a writeable file handle to write tar archive data
63
64 Returns:
65 A TarPackageArchive instance
66 """
67
68 def set_common_tarinfo_fields(tar_info):
69 tar_info.uid = os.getuid()
70 tar_info.gid = os.getgid()
71 tar_info.mtime = time.time()
72 tar_info.uname = "rift"
73 tar_info.gname = "rift"
74
75 archive = TarPackageArchive(log, tar_file_hdl, mode='w:gz')
76 for pkg_file in pkg.files:
77 tar_info = tarfile.TarInfo(name=pkg_file)
78 tar_info.type = tarfile.REGTYPE
79 tar_info.mode = pkg.get_file_mode(pkg_file)
80 set_common_tarinfo_fields(tar_info)
81 with pkg.open(pkg_file) as pkg_file_hdl:
82 tar_info.size = get_size(pkg_file_hdl)
83 archive.tarfile.addfile(tar_info, pkg_file_hdl)
84
85 for pkg_dir in pkg.dirs:
86 tar_info = tarfile.TarInfo(name=pkg_dir)
87 tar_info.type = tarfile.DIRTYPE
88 tar_info.mode = 0o775
89 set_common_tarinfo_fields(tar_info)
90 archive.tarfile.addfile(tar_info)
91
92 archive.load_archive()
93 archive.close()
94
95 return archive
96
97 def __repr__(self):
98 return "TarPackageArchive(%s)" % self._tar_filehdl
99
100 def __del__(self):
101 self.close()
102
103 def close(self):
104 """ Close the opened tarfile"""
105 if self._tarfile is not None:
106 self._tarfile.close()
107 self._tarfile = None
108
109 def load_archive(self):
110 self._tar_infos = {info.name: info for info in self._tarfile.getmembers() if info.name}
111
112 @property
113 def tarfile(self):
114 return self._tarfile
115
116 @property
117 def filenames(self):
118 """ The list of file members within the tar file """
119 return [name for name in self._tar_infos if tarfile.TarInfo.isfile(self._tar_infos[name])]
120
121 def open_file(self, rel_file_path):
122 """ Opens a file within the archive as read-only, byte mode.
123
124 Arguments:
125 rel_file_path - The file path within the archive to open
126
127 Returns:
128 A file like object (see tarfile.extractfile())
129
130 Raises:
131 FileNotFoundError - The file could not be found within the archive.
132 ArchiveError - The file could not be opened for some generic reason.
133 """
134 if rel_file_path not in self._tar_infos:
135 raise FileNotFoundError("Could not find %s in tar file", rel_file_path)
136
137 try:
138 return self._tarfile.extractfile(rel_file_path)
139 except tarfile.TarError as e:
140 msg = "Failed to read file {} from tarfile {}: {}".format(
141 rel_file_path, self._tar_filehdl, str(e)
142 )
143 self._log.error(msg)
144 raise ArchiveError(msg) from e
145
146 def create_package(self):
147 """ Creates a Descriptor package from the archive contents """
148 pkg = package.DescriptorPackage.from_package_files(self._log, self.open_file, self.filenames)
149 for pkg_file in self.filenames:
150 pkg.add_file(pkg_file, self._tar_infos[pkg_file].mode)
151
152 return pkg