OLD | NEW |
1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 # | 4 # |
5 # Adapted from portage/getbinpkg.py -- Portage binary-package helper functions | 5 # Adapted from portage/getbinpkg.py -- Portage binary-package helper functions |
6 # Copyright 2003-2004 Gentoo Foundation | 6 # Copyright 2003-2004 Gentoo Foundation |
7 # Distributed under the terms of the GNU General Public License v2 | 7 # Distributed under the terms of the GNU General Public License v2 |
8 | 8 |
9 import operator | 9 import operator |
10 import os | 10 import os |
11 import tempfile | 11 import tempfile |
12 import time | 12 import time |
| 13 import urllib |
13 import urllib2 | 14 import urllib2 |
14 | 15 |
15 class PackageIndex(object): | 16 class PackageIndex(object): |
16 """A parser for the Portage Packages index file. | 17 """A parser for the Portage Packages index file. |
17 | 18 |
18 The Portage Packages index file serves to keep track of what packages are | 19 The Portage Packages index file serves to keep track of what packages are |
19 included in a tree. It contains the following sections: | 20 included in a tree. It contains the following sections: |
20 1) The header. The header tracks general key/value pairs that don't apply | 21 1) The header. The header tracks general key/value pairs that don't apply |
21 to any specific package. E.g., it tracks the base URL of the packages | 22 to any specific package. E.g., it tracks the base URL of the packages |
22 file, and the number of packages included in the file. The header is | 23 file, and the number of packages included in the file. The header is |
(...skipping 22 matching lines...) Expand all Loading... |
45 """Populate db with SHA1 -> URL mapping for packages. | 46 """Populate db with SHA1 -> URL mapping for packages. |
46 | 47 |
47 Args: | 48 Args: |
48 db: Dictionary to populate with SHA1 -> URL mapping for packages. | 49 db: Dictionary to populate with SHA1 -> URL mapping for packages. |
49 """ | 50 """ |
50 | 51 |
51 uri = self.header['URI'] | 52 uri = self.header['URI'] |
52 for pkg in self.packages: | 53 for pkg in self.packages: |
53 cpv, sha1 = pkg['CPV'], pkg.get('SHA1') | 54 cpv, sha1 = pkg['CPV'], pkg.get('SHA1') |
54 if sha1: | 55 if sha1: |
55 path = pkg.get('PATH', cpv + '.tbz2') | 56 path = pkg.get('PATH', urllib.quote(cpv + '.tbz2')) |
56 db[sha1] = '%s/%s' % (uri.rstrip('/'), path) | 57 db[sha1] = '%s/%s' % (uri.rstrip('/'), path) |
57 | 58 |
58 def _ReadPkgIndex(self, pkgfile): | 59 def _ReadPkgIndex(self, pkgfile): |
59 """Read a list of key/value pairs from the Packages file into a dictionary. | 60 """Read a list of key/value pairs from the Packages file into a dictionary. |
60 | 61 |
61 Both header entries and package entries are lists of key/value pairs, so | 62 Both header entries and package entries are lists of key/value pairs, so |
62 they can both be read by this function. Entries can be terminated by empty | 63 they can both be read by this function. Entries can be terminated by empty |
63 lines or by the end of the file. | 64 lines or by the end of the file. |
64 | 65 |
65 This function will read lines from the specified file until it encounters | 66 This function will read lines from the specified file until it encounters |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 194 |
194 Args: | 195 Args: |
195 base_uri: Base URI for all packages in the file. We set | 196 base_uri: Base URI for all packages in the file. We set |
196 self.header['URI'] to this value, so all packages must live under | 197 self.header['URI'] to this value, so all packages must live under |
197 this directory. | 198 this directory. |
198 path_prefix: Path prefix to use for all current packages in the file. | 199 path_prefix: Path prefix to use for all current packages in the file. |
199 This will be added to the beginning of the path for every package. | 200 This will be added to the beginning of the path for every package. |
200 """ | 201 """ |
201 self.header['URI'] = base_uri | 202 self.header['URI'] = base_uri |
202 for pkg in self.packages: | 203 for pkg in self.packages: |
203 pkg['PATH'] = '%s/%s' % (path_prefix.rstrip('/'), pkg['CPV'] + '.tbz2') | 204 path = urllib.quote(pkg['CPV'] + '.tbz2') |
| 205 pkg['PATH'] = '%s/%s' % (path_prefix.rstrip('/'), path) |
204 | 206 |
205 def Write(self, pkgfile): | 207 def Write(self, pkgfile): |
206 """Write a packages file to disk. | 208 """Write a packages file to disk. |
207 | 209 |
208 If 'modified' flag is set, the TIMESTAMP and PACKAGES fields in the header | 210 If 'modified' flag is set, the TIMESTAMP and PACKAGES fields in the header |
209 will be updated before writing to disk. | 211 will be updated before writing to disk. |
210 | 212 |
211 Args: | 213 Args: |
212 pkgfile: A python file object. | 214 pkgfile: A python file object. |
213 """ | 215 """ |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 package_path: Directory containing Packages file. | 300 package_path: Directory containing Packages file. |
299 | 301 |
300 Returns: | 302 Returns: |
301 A PackageIndex object. | 303 A PackageIndex object. |
302 """ | 304 """ |
303 packages_file = file(os.path.join(package_path, 'Packages')) | 305 packages_file = file(os.path.join(package_path, 'Packages')) |
304 pkgindex = PackageIndex() | 306 pkgindex = PackageIndex() |
305 pkgindex.Read(packages_file) | 307 pkgindex.Read(packages_file) |
306 packages_file.close() | 308 packages_file.close() |
307 return pkgindex | 309 return pkgindex |
OLD | NEW |