15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#!/usr/bin/env python
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Copyright 2014 The Chromium Authors. All rights reserved.
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# found in the LICENSE file.
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)"""Make sure all of the per-file *_messages.h OWNERS are consistent"""
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import os
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import re
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import sys
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def main():
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  file_path = os.path.dirname(__file__);
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  root_dir = os.path.abspath(os.path.join(file_path, '..', '..'))
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  owners = collect_owners(root_dir)
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  all_owners = get_all_owners(owners)
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  print_missing_owners(owners, all_owners)
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return 0
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def collect_owners(root_dir):
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  result = {}
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for root, dirs, files in os.walk(root_dir):
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if "OWNERS" in files:
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      owner_file_path = os.path.join(root, "OWNERS")
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      owner_set = extract_owners_from_file(owner_file_path)
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if owner_set:
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        result[owner_file_path] = owner_set
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return result
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def extract_owners_from_file(owner_file_path):
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  result = set()
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  regexp = re.compile('^per-file.*_messages[^=]*=\s*(.*)@([^#]*)')
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  with open(owner_file_path) as f:
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    for line in f:
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      match = regexp.match(line)
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if match:
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        result.add(match.group(1).strip())
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return result
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def get_all_owners(owner_dict):
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  result = set()
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for key in owner_dict:
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    result = result.union(owner_dict[key])
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return result
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def print_missing_owners(owner_dict, owner_set):
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for key in owner_dict:
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    for owner in owner_set:
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if not owner in owner_dict[key]:
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        print key + " is missing " + owner
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)if '__main__' == __name__:
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  sys.exit(main())
54