blob: 0114100b418be5a64dad70a5e16ff407d7f810a7 [file] [log] [blame]
#!/usr/bin/env python2.7
# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import argparse
import os
import re
import sys
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
FUCHSIA_ROOT = os.path.dirname( # $root
os.path.dirname( # scripts
SCRIPT_DIR)) # unification
def main():
parser = argparse.ArgumentParser(
description='Finds all OWNERS of given paths')
parser.add_argument('paths', nargs='+')
args = parser.parse_args()
owner = args.owner
abspaths = [os.path.abspath(path) for path in args.paths]
perfile_exp = re.compile('^\s*per-file ([^\s=]*)\s*=\s*' + owner)
# Find all OWNERS files
path_to_owners = {}
for path in abspaths:
dir = path if os.path.isdir(path) else os.path.dirname(path)
dir = os.path.abspath(dir)
while (os.path.exists(dir) and
os.path.commonprefix([dir, FUCHSIA_ROOT]) == FUCHSIA_ROOT):
owners_path = os.path.join(dir, 'OWNERS')
if os.path.exists(owners_path):
path_to_owners[path] = owners_path
dir = os.path.dirname(dir)
# Parse all OWNERS files
owned = set()
for path, owners in path_to_owners.iteritems():
with open(owners) as f:
for line in f.readlines():
if line.strip().startswith(owner):
match = perfile_exp.match(line)
if match:
filename = os.path.abspath(
if filename in abspaths:
# Print owned files
for owned_path in sorted(owned):
print os.path.relpath(owned_path)
return 0
if __name__ == '__main__':