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