1#!/usr/bin/env python3 2 3# 4# Copyright (C) 2018 The Android Open Source Project 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19"""A command line utility to download multiple patch files of change lists from 20Gerrit.""" 21 22from __future__ import print_function 23 24import argparse 25import os 26import sys 27 28from gerrit import ( 29 add_common_parse_args, create_url_opener_from_args, find_gerrit_name, 30 normalize_gerrit_name, query_change_lists, get_patch 31) 32 33def _parse_args(): 34 """Parse command line options.""" 35 parser = argparse.ArgumentParser() 36 add_common_parse_args(parser) 37 return parser.parse_args() 38 39 40def main(): 41 """Main function""" 42 args = _parse_args() 43 44 if args.gerrit: 45 args.gerrit = normalize_gerrit_name(args.gerrit) 46 else: 47 try: 48 args.gerrit = find_gerrit_name() 49 # pylint: disable=bare-except 50 except: 51 print('gerrit instance not found, use [-g GERRIT]') 52 sys.exit(1) 53 54 # Query change lists 55 url_opener = create_url_opener_from_args(args) 56 change_lists = query_change_lists( 57 url_opener, args.gerrit, args.query, args.start, args.limits) 58 59 # Download patch files 60 num_changes = len(change_lists) 61 num_changes_width = len(str(num_changes)) 62 for i, change in enumerate(change_lists, start=1): 63 print('{:>{}}/{} | {} {}'.format( 64 i, num_changes_width, num_changes, change['_number'], 65 change['subject'])) 66 67 patch_file = get_patch(url_opener, args.gerrit, change['id']) 68 with open('{}.patch'.format(change['_number']), 'wb') as output_file: 69 output_file.write(patch_file) 70 71if __name__ == '__main__': 72 main() 73