diff options
author | Matthias Maennich <maennich@google.com> | 2021-04-01 14:01:42 +0100 |
---|---|---|
committer | Matthias Maennich <maennich@google.com> | 2021-04-01 13:45:52 +0000 |
commit | 8396c7a2782711b7392588d51f29ba2628a3d6ae (patch) | |
tree | c213168128e566cd9638bb80e15ea9748e9a0d2c /synchronize_owners | |
parent | af40665c7bfae1feff1a3200e1fccfcd45f5b563 (diff) | |
download | build-8396c7a2782711b7392588d51f29ba2628a3d6ae.tar.gz |
add synchronize_owners utility
This helper synchronizes OWNERS directives between a main branch and a
tracking branch. In the directories in which the main branch has an
OWNERS file, the tracking branch's equivalent OWNERS file will have a
directive to include the main branch file. Existing OWNERS file contents
are preserved by prepending the include directive if absent.
Example usage (executed in an common-android-multi repo checkout):
$ build/synchronize_owners common-mainline/ android-mainline common12-5.10/
Bug: 184248201
Signed-off-by: Matthias Maennich <maennich@google.com>
Change-Id: I41b0e0e8ff20b45ef1f1e8641e1107af95bc7c85
Diffstat (limited to 'synchronize_owners')
-rwxr-xr-x | synchronize_owners | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/synchronize_owners b/synchronize_owners new file mode 100755 index 00000000..2fffe3ee --- /dev/null +++ b/synchronize_owners @@ -0,0 +1,75 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2021 The Android Open main Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import argparse +import os +import pathlib +import sys + + +def process_owners(main_dir, main_branch, tracking_dir): + # walk the tree ... + for subdir, _, files in os.walk(main_dir): + for file in files: + # ... and for each OWNERS file in the main ... + if file == 'OWNERS': + rel_dir = pathlib.Path(subdir[len(main_dir.name) + 1:]) + owners = rel_dir / file + + target = tracking_dir / owners + comment_line = (f'# include OWNERS from the authoritative ' + f'{main_branch} branch\n') + include_line = f'include kernel/common:{main_branch}:/{owners}\n' + + # ... either add a new file with include directive ... + if not target.is_file(): + with open(target, 'w') as file: + file.writelines([comment_line, include_line]) + + # ... or ensure the include directive is in the file + else: + with open(target) as file: + contents = file.readlines() + if include_line not in contents: + with open(target, 'w') as file: + file.writelines([comment_line, include_line, '\n'] + contents) + + +def is_dir(dirname): + result = pathlib.Path(dirname) + if not result.is_dir(): + raise argparse.ArgumentTypeError(f'{dirname} is not a directory') + else: + return result + + +def main(): + parser = argparse.ArgumentParser() + + parser.add_argument( + 'main_dir', type=is_dir, help='main directory to synchronize from') + parser.add_argument('main_branch', help='main branch name'), + parser.add_argument( + 'tracking_dir', type=is_dir, help='tracking directory to synchronize to') + + args = parser.parse_args() + + process_owners(args.main_dir, args.main_branch, args.tracking_dir) + + +if __name__ == '__main__': + sys.exit(main()) |