File: print_js_deps.py

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (95 lines) | stat: -rwxr-xr-x 2,839 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python

# Copyright 2015 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
'''Print the dependency tree for a JavaScript module.

Given one or more root directories, specified by -r options and one top-level
file, walk the dependency tree and print all modules encountered.
A module is only expanded once; on a second encounter, its dependencies
are represented by a line containing the characters '[...]' as a short-hand.
'''

import optparse
import os
import sys

from jsbundler import ReadSources


def Die(message):
  '''Prints an error message and exit the program.'''
  print >> sys.stderr, message
  sys.exit(1)


def CreateOptionParser():
  parser = optparse.OptionParser(description=__doc__)
  parser.usage = '%prog [options] <top_level_file>'
  parser.add_option(
      '-r',
      '--root',
      dest='roots',
      action='append',
      default=[],
      metavar='ROOT',
      help='Roots of directory trees to scan for sources. '
      'If none specified, all of ChromeVox and closure sources '
      'are scanned.')
  return parser


def DefaultRoots():
  script_dir = os.path.dirname(os.path.abspath(__file__))
  source_root_dir = os.path.join(script_dir, *[os.path.pardir] * 7)
  return [
      os.path.relpath(os.path.join(script_dir, os.path.pardir)),
      os.path.relpath(
          os.path.join(source_root_dir, 'chrome', 'third_party', 'chromevox',
                       'third_party', 'closure-library', 'closure'))
  ]


def WalkDeps(sources, start_source):

  def Walk(source, depth):
    indent = '  ' * depth
    if source.GetInPath() in expanded and len(source.requires) > 0:
      print '%s[...]' % indent
      return
    expanded.add(source.GetInPath())
    for require in source.requires:
      if not require in providers:
        Die('%s not provided, required by %s' % (require, source.GetInPath()))
      require_source = providers[require]
      print '%s%s (%s)' % (indent, require, require_source.GetInPath())
      Walk(require_source, depth + 1)

  # Create a map from provided module names to source objects.
  providers = {}
  expanded = set()
  for source in sources.values():
    for provide in source.provides:
      if provide in providers:
        Die('%s provided multiple times' % provide)
      providers[provide] = source

  print '(%s)' % start_source.GetInPath()
  Walk(start_source, 1)


def main():
  parser = CreateOptionParser()
  options, args = parser.parse_args()
  if len(args) != 1:
    Die('Exactly one top-level source file must be specified.')
  start_path = args[0]
  roots = options.roots or DefaultRoots()
  sources = ReadSources(roots=roots, source_files=[start_path])
  start_source = sources[start_path]
  WalkDeps(sources, start_source)


if __name__ == '__main__':
  main()