1#!/usr/bin/python
2# Copyright (c) 2011 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 list of team members for the root individual."""
7
8
9import commands
10import json
11import logging
12import optparse
13import os
14import sys
15
16
17def ParseArgs(argv):
18  """Get input and output file."""
19  base_dir = os.path.realpath(os.path.abspath(os.path.join(
20      os.getcwd(), os.path.dirname(__file__))))
21
22  parser = optparse.OptionParser()
23  parser.add_option('-r', '--root-person', help='initial person to follow',
24                    dest='rootperson', default=None)
25  parser.add_option('-o', '--output-file', help='output file (json)',
26                    dest='outputfile', default="kernel-team.json")
27  parser.add_option('-p', '--print-test', help='print the map as a test',
28                    dest='printtest', action='store_true', default=False)
29  parser.add_option('-v', '--verbose', help='Show more output',
30                    dest='verbose', action='store_true', default=False)
31  options, args = parser.parse_args()
32
33  if not options.rootperson:
34    parser.error('--root-person must be supplied')
35
36  logging_level = logging.INFO
37  if options.verbose:
38    logging_level = logging.DEBUG
39
40  logging.basicConfig(level=logging_level)
41
42  return options, args, base_dir
43
44
45def SearchOnePerson(person):
46  """Run a command to get details for one person."""
47  found = []
48  if person:
49    command = 'f %s | grep Reportees' % person
50    logging.debug(command)
51    find_result = commands.getoutput(command)
52    if find_result:
53      found = find_result.split(' ')[2:]
54      logging.debug(found)
55  return found
56
57
58def FindTeamMembers(root_person):
59  """Recursively iteratea through the list of team members until done.
60
61  Expect the root_person to have at least 1 report but not needed.
62  """
63  remaining = [root_person]
64  extracted = [root_person]
65  while remaining:
66    found = SearchOnePerson(remaining.pop(0))
67    if found:
68      remaining += found
69      extracted += found
70
71  return extracted
72
73
74def WriteJson(outputfile, extracted):
75  """Write output in json format."""
76  f = open(outputfile, 'w')
77  json.dump(extracted, f)
78  f.close()
79
80
81def PrintJson(jsonfile):
82  """Read the json file and format-print its contents as a test."""
83  f = open(jsonfile, 'r')
84  team_list = json.load(f)
85  f.close()
86  for t in sorted(team_list):
87    logging.info('%s', t)
88
89
90def main(argv):
91  """Can generate tables, plots and email."""
92  options, args, base_dir = ParseArgs(argv)
93
94  logging.info('Using %s as root.', options.rootperson)
95  logging.info('Using output file: %s', options.outputfile)
96
97  team = FindTeamMembers(options.rootperson)
98  if team:
99    WriteJson(options.outputfile, team)
100  if options.printtest:
101    PrintJson(options.outputfile)
102
103
104if __name__ == '__main__':
105  main(sys.argv)
106