diff options
author | Tudor Florea <tudor.florea@enea.com> | 2014-10-16 03:05:19 +0200 |
---|---|---|
committer | Tudor Florea <tudor.florea@enea.com> | 2014-10-16 03:05:19 +0200 |
commit | c527fd1f14c27855a37f2e8ac5346ce8d940ced2 (patch) | |
tree | bb002c1fdf011c41dbd2f0927bed23ecb5f83c97 /scripts/oe-pkgdata-util | |
download | poky-c527fd1f14c27855a37f2e8ac5346ce8d940ced2.tar.gz |
initial commit for Enea Linux 4.0-140929daisy-140929
Migrated from the internal git server on the daisy-enea-point-release branch
Signed-off-by: Tudor Florea <tudor.florea@enea.com>
Diffstat (limited to 'scripts/oe-pkgdata-util')
-rwxr-xr-x | scripts/oe-pkgdata-util | 332 |
1 files changed, 332 insertions, 0 deletions
diff --git a/scripts/oe-pkgdata-util b/scripts/oe-pkgdata-util new file mode 100755 index 0000000000..a373116b2c --- /dev/null +++ b/scripts/oe-pkgdata-util | |||
@@ -0,0 +1,332 @@ | |||
1 | #!/usr/bin/env python | ||
2 | |||
3 | # OpenEmbedded pkgdata utility | ||
4 | # | ||
5 | # Written by: Paul Eggleton <paul.eggleton@linux.intel.com> | ||
6 | # | ||
7 | # Copyright 2012-2013 Intel Corporation | ||
8 | # | ||
9 | # This program is free software; you can redistribute it and/or modify | ||
10 | # it under the terms of the GNU General Public License version 2 as | ||
11 | # published by the Free Software Foundation. | ||
12 | # | ||
13 | # This program is distributed in the hope that it will be useful, | ||
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | # GNU General Public License for more details. | ||
17 | # | ||
18 | # You should have received a copy of the GNU General Public License along | ||
19 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
20 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
21 | # | ||
22 | |||
23 | import sys | ||
24 | import os | ||
25 | import os.path | ||
26 | import fnmatch | ||
27 | import re | ||
28 | import optparse | ||
29 | from collections import defaultdict | ||
30 | |||
31 | def glob(args, usage, debug=False): | ||
32 | if len(args) < 3: | ||
33 | usage() | ||
34 | sys.exit(1) | ||
35 | |||
36 | pkgdata_dir = args[0] | ||
37 | pkglist_file = args[1] | ||
38 | globs = args[2].split() | ||
39 | |||
40 | if not os.path.exists(pkgdata_dir): | ||
41 | print('ERROR: Unable to find pkgdata directory %s' % pkgdata_dir) | ||
42 | sys.exit(1) | ||
43 | |||
44 | if not os.path.exists(pkglist_file): | ||
45 | print('ERROR: Unable to find package list file %s' % pkglist_file) | ||
46 | sys.exit(1) | ||
47 | |||
48 | skipregex = re.compile("-locale-|^locale-base-|-dev$|-doc$|-dbg$|-staticdev$|^kernel-module-") | ||
49 | |||
50 | mappedpkgs = set() | ||
51 | with open(pkglist_file, 'r') as f: | ||
52 | for line in f: | ||
53 | fields = line.rstrip().split() | ||
54 | if not fields: | ||
55 | continue | ||
56 | pkg = fields[0] | ||
57 | # We don't care about other args (used to need the package architecture but the | ||
58 | # new pkgdata structure avoids the need for that) | ||
59 | |||
60 | # Skip packages for which there is no point applying globs | ||
61 | if skipregex.search(pkg): | ||
62 | if debug: | ||
63 | print("%s -> !!" % pkg) | ||
64 | continue | ||
65 | |||
66 | # Skip packages that already match the globs, so if e.g. a dev package | ||
67 | # is already installed and thus in the list, we don't process it any further | ||
68 | # Most of these will be caught by skipregex already, but just in case... | ||
69 | already = False | ||
70 | for g in globs: | ||
71 | if fnmatch.fnmatchcase(pkg, g): | ||
72 | already = True | ||
73 | break | ||
74 | if already: | ||
75 | if debug: | ||
76 | print("%s -> !" % pkg) | ||
77 | continue | ||
78 | |||
79 | # Define some functions | ||
80 | def revpkgdata(pkgn): | ||
81 | return os.path.join(pkgdata_dir, "runtime-reverse", pkgn) | ||
82 | def fwdpkgdata(pkgn): | ||
83 | return os.path.join(pkgdata_dir, "runtime", pkgn) | ||
84 | def readpn(pkgdata_file): | ||
85 | pn = "" | ||
86 | with open(pkgdata_file, 'r') as f: | ||
87 | for line in f: | ||
88 | if line.startswith("PN:"): | ||
89 | pn = line.split(': ')[1].rstrip() | ||
90 | return pn | ||
91 | def readrenamed(pkgdata_file): | ||
92 | renamed = "" | ||
93 | pn = os.path.basename(pkgdata_file) | ||
94 | with open(pkgdata_file, 'r') as f: | ||
95 | for line in f: | ||
96 | if line.startswith("PKG_%s:" % pn): | ||
97 | renamed = line.split(': ')[1].rstrip() | ||
98 | return renamed | ||
99 | |||
100 | # Main processing loop | ||
101 | for g in globs: | ||
102 | mappedpkg = "" | ||
103 | # First just try substitution (i.e. packagename -> packagename-dev) | ||
104 | newpkg = g.replace("*", pkg) | ||
105 | revlink = revpkgdata(newpkg) | ||
106 | if os.path.exists(revlink): | ||
107 | mappedpkg = os.path.basename(os.readlink(revlink)) | ||
108 | fwdfile = fwdpkgdata(mappedpkg) | ||
109 | if os.path.exists(fwdfile): | ||
110 | mappedpkg = readrenamed(fwdfile) | ||
111 | if not os.path.exists(fwdfile + ".packaged"): | ||
112 | mappedpkg = "" | ||
113 | else: | ||
114 | revlink = revpkgdata(pkg) | ||
115 | if os.path.exists(revlink): | ||
116 | # Check if we can map after undoing the package renaming (by resolving the symlink) | ||
117 | origpkg = os.path.basename(os.readlink(revlink)) | ||
118 | newpkg = g.replace("*", origpkg) | ||
119 | fwdfile = fwdpkgdata(newpkg) | ||
120 | if os.path.exists(fwdfile): | ||
121 | mappedpkg = readrenamed(fwdfile) | ||
122 | else: | ||
123 | # That didn't work, so now get the PN, substitute that, then map in the other direction | ||
124 | pn = readpn(revlink) | ||
125 | newpkg = g.replace("*", pn) | ||
126 | fwdfile = fwdpkgdata(newpkg) | ||
127 | if os.path.exists(fwdfile): | ||
128 | mappedpkg = readrenamed(fwdfile) | ||
129 | if not os.path.exists(fwdfile + ".packaged"): | ||
130 | mappedpkg = "" | ||
131 | else: | ||
132 | # Package doesn't even exist... | ||
133 | if debug: | ||
134 | print "%s is not a valid package!" % (pkg) | ||
135 | break | ||
136 | |||
137 | if mappedpkg: | ||
138 | if debug: | ||
139 | print "%s (%s) -> %s" % (pkg, g, mappedpkg) | ||
140 | mappedpkgs.add(mappedpkg) | ||
141 | else: | ||
142 | if debug: | ||
143 | print "%s (%s) -> ?" % (pkg, g) | ||
144 | |||
145 | if debug: | ||
146 | print "------" | ||
147 | |||
148 | print("\n".join(mappedpkgs)) | ||
149 | |||
150 | def read_value(args, usage, debug=False): | ||
151 | if len(args) < 3: | ||
152 | usage() | ||
153 | sys.exit(1) | ||
154 | |||
155 | pkgdata_dir = args[0] | ||
156 | var = args[1] | ||
157 | packages = args[2].split() | ||
158 | |||
159 | if not os.path.exists(pkgdata_dir): | ||
160 | print('ERROR: Unable to find pkgdata directory %s' % pkgdata_dir) | ||
161 | sys.exit(1) | ||
162 | |||
163 | def readvar(pkgdata_file, var): | ||
164 | val = "" | ||
165 | with open(pkgdata_file, 'r') as f: | ||
166 | for line in f: | ||
167 | if line.startswith(var + ":"): | ||
168 | val = line.split(': ')[1].rstrip() | ||
169 | return val | ||
170 | |||
171 | if debug: | ||
172 | print "read-value('%s', '%s' '%s'" % (pkgdata_dir, var, packages) | ||
173 | for package in packages: | ||
174 | pkg_split = package.split('_') | ||
175 | pkg_name = pkg_split[0] | ||
176 | if debug: | ||
177 | print "package: '%s'" % pkg_name | ||
178 | revlink = os.path.join(pkgdata_dir, "runtime-reverse", pkg_name) | ||
179 | if debug: | ||
180 | print(revlink) | ||
181 | if os.path.exists(revlink): | ||
182 | mappedpkg = os.path.basename(os.readlink(revlink)) | ||
183 | qvar = var | ||
184 | if qvar == "PKGSIZE": | ||
185 | # append packagename | ||
186 | qvar = "%s_%s" % (var, mappedpkg) | ||
187 | # PKGSIZE is now in bytes, but we we want it in KB | ||
188 | pkgsize = (int(readvar(revlink, qvar)) + 1024 // 2) // 1024 | ||
189 | print("%d" % pkgsize) | ||
190 | else: | ||
191 | print(readvar(revlink, qvar)) | ||
192 | |||
193 | def lookup_pkg(args, usage, debug=False): | ||
194 | if len(args) < 2: | ||
195 | usage() | ||
196 | sys.exit(1) | ||
197 | |||
198 | pkgdata_dir = args[0] | ||
199 | pkgs = args[1].split() | ||
200 | |||
201 | if not os.path.exists(pkgdata_dir): | ||
202 | print('ERROR: Unable to find pkgdata directory %s' % pkgdata_dir) | ||
203 | sys.exit(1) | ||
204 | |||
205 | mappings = defaultdict(list) | ||
206 | for pkg in pkgs: | ||
207 | pkgfile = os.path.join(pkgdata_dir, 'runtime', pkg) | ||
208 | if os.path.exists(pkgfile): | ||
209 | with open(pkgfile, 'r') as f: | ||
210 | for line in f: | ||
211 | fields = line.rstrip().split(': ') | ||
212 | if fields[0] == 'PKG_%s' % pkg: | ||
213 | mappings[pkg].append(fields[1]) | ||
214 | break | ||
215 | if len(mappings) < len(pkgs): | ||
216 | missing = list(set(pkgs) - set(mappings.keys())) | ||
217 | sys.stderr.write("ERROR: the following packages could not be found: %s\n" % ', '.join(missing)) | ||
218 | sys.exit(1) | ||
219 | |||
220 | items = [] | ||
221 | for pkg in pkgs: | ||
222 | items.extend(mappings.get(pkg, [])) | ||
223 | print '\n'.join(items) | ||
224 | |||
225 | def lookup_recipe(args, usage, debug=False): | ||
226 | if len(args) < 2: | ||
227 | usage() | ||
228 | sys.exit(1) | ||
229 | |||
230 | pkgdata_dir = args[0] | ||
231 | pkgs = args[1].split() | ||
232 | |||
233 | if not os.path.exists(pkgdata_dir): | ||
234 | print('ERROR: Unable to find pkgdata directory %s' % pkgdata_dir) | ||
235 | sys.exit(1) | ||
236 | |||
237 | mappings = defaultdict(list) | ||
238 | for pkg in pkgs: | ||
239 | pkgfile = os.path.join(pkgdata_dir, 'runtime-reverse', pkg) | ||
240 | if os.path.exists(pkgfile): | ||
241 | with open(pkgfile, 'r') as f: | ||
242 | for line in f: | ||
243 | fields = line.rstrip().split(': ') | ||
244 | if fields[0] == 'PN': | ||
245 | mappings[pkg].append(fields[1]) | ||
246 | break | ||
247 | if len(mappings) < len(pkgs): | ||
248 | missing = list(set(pkgs) - set(mappings.keys())) | ||
249 | sys.stderr.write("ERROR: the following packages could not be found: %s\n" % ', '.join(missing)) | ||
250 | sys.exit(1) | ||
251 | |||
252 | items = [] | ||
253 | for pkg in pkgs: | ||
254 | items.extend(mappings.get(pkg, [])) | ||
255 | print '\n'.join(items) | ||
256 | |||
257 | def find_path(args, usage, debug=False): | ||
258 | if len(args) < 2: | ||
259 | usage() | ||
260 | sys.exit(1) | ||
261 | |||
262 | pkgdata_dir = args[0] | ||
263 | targetpath = args[1] | ||
264 | |||
265 | if not os.path.exists(pkgdata_dir): | ||
266 | print('ERROR: Unable to find pkgdata directory %s' % pkgdata_dir) | ||
267 | sys.exit(1) | ||
268 | |||
269 | import json | ||
270 | import fnmatch | ||
271 | |||
272 | for root, dirs, files in os.walk(os.path.join(pkgdata_dir, 'runtime')): | ||
273 | for fn in files: | ||
274 | with open(os.path.join(root,fn)) as f: | ||
275 | for line in f: | ||
276 | if line.startswith('FILES_INFO:'): | ||
277 | val = line.split(':', 1)[1].strip() | ||
278 | dictval = json.loads(val) | ||
279 | for fullpth in dictval.keys(): | ||
280 | if fnmatch.fnmatchcase(fullpth, targetpath): | ||
281 | print("%s: %s" % (fn, fullpth)) | ||
282 | break | ||
283 | |||
284 | |||
285 | def main(): | ||
286 | parser = optparse.OptionParser( | ||
287 | usage = '''%prog [options] <command> <arguments> | ||
288 | |||
289 | Available commands: | ||
290 | glob <pkgdatadir> <pkglistfile> "<globs>" | ||
291 | expand one or more glob expressions over the packages listed in | ||
292 | pkglistfile (one package per line) | ||
293 | lookup-pkg <pkgdatadir> "<recipe-pkgs>" | ||
294 | look up the specified recipe-space package name(s) to see what the | ||
295 | final runtime package name is (e.g. eglibc becomes libc6) | ||
296 | lookup-recipe <pkgdatadir> "<pkgs>" | ||
297 | look up the specified package(s) to see which recipe they were | ||
298 | produced by | ||
299 | find-path <pkgdatadir> <path> | ||
300 | find the package providing the specified path (wildcards * ? allowed) | ||
301 | read-value <pkgdatadir> <value-name> "<pkgs>" | ||
302 | read the named value from the pkgdata files for the specified | ||
303 | packages''') | ||
304 | |||
305 | parser.add_option("-d", "--debug", | ||
306 | help = "Report all SRCREV values, not just ones where AUTOREV has been used", | ||
307 | action="store_true", dest="debug", default=False) | ||
308 | |||
309 | options, args = parser.parse_args(sys.argv) | ||
310 | args = args[1:] | ||
311 | |||
312 | if len(args) < 1: | ||
313 | parser.print_help() | ||
314 | sys.exit(1) | ||
315 | |||
316 | if args[0] == "glob": | ||
317 | glob(args[1:], parser.print_help, options.debug) | ||
318 | elif args[0] == "lookup-pkg": | ||
319 | lookup_pkg(args[1:], parser.print_help, options.debug) | ||
320 | elif args[0] == "lookup-recipe": | ||
321 | lookup_recipe(args[1:], parser.print_help, options.debug) | ||
322 | elif args[0] == "find-path": | ||
323 | find_path(args[1:], parser.print_help, options.debug) | ||
324 | elif args[0] == "read-value": | ||
325 | read_value(args[1:], parser.print_help, options.debug) | ||
326 | else: | ||
327 | parser.print_help() | ||
328 | sys.exit(1) | ||
329 | |||
330 | |||
331 | if __name__ == "__main__": | ||
332 | main() | ||