OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Extract dependency tree out of emerge and make it accessible and useful.""" |
| 7 |
| 8 import optparse |
| 9 import pprint |
| 10 import re |
| 11 import shutil |
| 12 import subprocess |
| 13 import sys |
| 14 import tempfile |
| 15 import time |
| 16 |
| 17 class ParseException(Exception): |
| 18 def __init__(self, reason): |
| 19 Exception.__init__(self) |
| 20 self.reason = reason |
| 21 |
| 22 def __str__(self): |
| 23 return self.reason |
| 24 |
| 25 |
| 26 def GetDepLinesFromPortage(options, packages): |
| 27 """Get dependency lines out of emerge. |
| 28 |
| 29 This calls emerge -p --debug and extracts the 'digraph' lines which detail |
| 30 the dependencies." |
| 31 """ |
| 32 # Use a temporary directory for $ROOT, so that emerge will consider all |
| 33 # packages regardless of current build status. |
| 34 temp_dir = tempfile.mkdtemp() |
| 35 |
| 36 emerge = 'emerge' |
| 37 if options.board: |
| 38 emerge += '-' + options.board |
| 39 cmdline = [emerge, '-p', '--debug', '--root=' + temp_dir] |
| 40 if not options.build_time: |
| 41 cmdline.append('--root-deps=rdeps') |
| 42 cmdline += packages |
| 43 |
| 44 # Store output in a temp file as it is too big for a unix pipe. |
| 45 stderr_buffer = tempfile.TemporaryFile() |
| 46 |
| 47 depsproc = subprocess.Popen(cmdline, stderr=stderr_buffer, |
| 48 stdout=open('/dev/null', 'w'), bufsize=64*1024) |
| 49 depsproc.wait() |
| 50 |
| 51 subprocess.check_call(['sudo', 'rm', '-rf', temp_dir]) |
| 52 |
| 53 assert(depsproc.returncode==0) |
| 54 |
| 55 stderr_buffer.seek(0) |
| 56 lines = [] |
| 57 output = False |
| 58 for line in stderr_buffer: |
| 59 stripped = line.rstrip() |
| 60 if output: |
| 61 lines.append(stripped) |
| 62 if stripped == 'digraph:': |
| 63 output = True |
| 64 |
| 65 if not output: |
| 66 raise ParseException('Could not find digraph in output from emerge.') |
| 67 |
| 68 return lines |
| 69 |
| 70 |
| 71 def ParseDepLines(lines): |
| 72 """Parse the dependency lines into a dependency tree. |
| 73 |
| 74 This parses the digraph lines, extract the information and builds the |
| 75 dependency tree (doubly-linked)." |
| 76 """ |
| 77 # The digraph output looks like this: |
| 78 |
| 79 # hard-host-depends depends on |
| 80 # ('ebuild', '/tmp/root', 'dev-lang/swig-1.3.36', 'merge') depends on |
| 81 # ('ebuild', '/tmp/root', 'dev-lang/perl-5.8.8-r8', 'merge') (buildtime) |
| 82 # ('binary', '/tmp/root', 'sys-auth/policykit-0.9-r1', 'merge') depends on |
| 83 # ('binary', '/tmp/root', 'x11-misc/xbitmaps-1.1.0', 'merge') (no children) |
| 84 |
| 85 re_deps = re.compile(r'(?P<indent>\W*)\(\'(?P<package_type>\w+)\',' |
| 86 r' \'(?P<destination>[\w/\.-]+)\',' |
| 87 r' \'(?P<category>[\w\+-]+)/(?P<package_name>[\w\+-]+)-' |
| 88 r'(?P<version>\d+[\w\.-]*)\', \'(?P<action>\w+)\'\)' |
| 89 r' (?P<dep_type>(depends on|\(.*\)))') |
| 90 re_seed_deps = re.compile(r'(?P<package_name>[\w\+/-]+) depends on') |
| 91 # Packages that fail the previous regex should match this one and be noted as |
| 92 # failure. |
| 93 re_failed = re.compile(r'.*depends on.*') |
| 94 |
| 95 deps_map = {} |
| 96 |
| 97 current_package = None |
| 98 for line in lines: |
| 99 deps_match = re_deps.match(line) |
| 100 if deps_match: |
| 101 package_name = deps_match.group('package_name') |
| 102 category = deps_match.group('category') |
| 103 indent = deps_match.group('indent') |
| 104 action = deps_match.group('action') |
| 105 dep_type = deps_match.group('dep_type') |
| 106 version = deps_match.group('version') |
| 107 |
| 108 # Pretty print what we've captured. |
| 109 full_package_name = '%s/%s-%s' % (category, package_name, version) |
| 110 |
| 111 try: |
| 112 package_info = deps_map[full_package_name] |
| 113 except KeyError: |
| 114 package_info = { |
| 115 'deps': set(), |
| 116 'rev_deps': set(), |
| 117 'name': package_name, |
| 118 'category': category, |
| 119 'version': version, |
| 120 'full_name': full_package_name, |
| 121 'action': action, |
| 122 } |
| 123 deps_map[full_package_name] = package_info |
| 124 |
| 125 if not indent: |
| 126 if dep_type == 'depends on': |
| 127 current_package = package_info |
| 128 else: |
| 129 current_package = None |
| 130 else: |
| 131 if not current_package: |
| 132 raise ParseException('Found a dependency without parent:\n' + line) |
| 133 if dep_type == 'depend on': |
| 134 raise ParseException('Found extra levels of dependencies:\n' + line) |
| 135 current_package['deps'].add(full_package_name) |
| 136 package_info['rev_deps'].add(current_package['full_name']) |
| 137 |
| 138 else: |
| 139 seed_match = re_seed_deps.match(line) |
| 140 if seed_match: |
| 141 package_name = seed_match.group('package_name') |
| 142 |
| 143 try: |
| 144 current_package = deps_map[package_name] |
| 145 except KeyError: |
| 146 current_package = { |
| 147 'deps': set(), |
| 148 'rev_deps': set(), |
| 149 'name': package_name, |
| 150 'category': '', |
| 151 'version': '', |
| 152 'full_name': package_name, |
| 153 'action': 'seed', |
| 154 } |
| 155 deps_map[package_name] = current_package |
| 156 |
| 157 else: |
| 158 # Is this a package that failed to match our huge regex? |
| 159 failed_match = re_failed.match(line) |
| 160 if failed_match: |
| 161 raise ParseException('Couldn\'t understand line:\n' + line) |
| 162 |
| 163 return deps_map |
| 164 |
| 165 |
| 166 def main(): |
| 167 parser = optparse.OptionParser(usage='usage: %prog [options] package1 ...') |
| 168 parser.add_option('-b', '--board', |
| 169 help='The board to extract dependencies from.') |
| 170 parser.add_option('-B', '--build-time', action='store_true', |
| 171 dest='build_time', |
| 172 help='Also extract build-time dependencies.') |
| 173 parser.add_option('-o', '--output', default=None, |
| 174 help='Output file.') |
| 175 (options, packages) = parser.parse_args() |
| 176 if not packages: |
| 177 parser.print_usage() |
| 178 sys.exit(1) |
| 179 |
| 180 lines = GetDepLinesFromPortage(options, packages) |
| 181 deps_map = ParseDepLines(lines) |
| 182 output = pprint.pformat(deps_map) |
| 183 if options.output: |
| 184 output_file = open(options.output, 'w') |
| 185 output_file.write(output) |
| 186 output_file.close() |
| 187 else: |
| 188 print output |
| 189 |
| 190 |
| 191 if __name__ == '__main__': |
| 192 main() |
OLD | NEW |