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 create_url_opener_from_args, find_gerrit_name, query_change_lists, get_patch 30) 31 32def _parse_args(): 33 """Parse command line options.""" 34 parser = argparse.ArgumentParser() 35 36 parser.add_argument('query', help='Change list query string') 37 parser.add_argument('-g', '--gerrit', help='Gerrit review URL') 38 39 parser.add_argument('--gitcookies', 40 default=os.path.expanduser('~/.gitcookies'), 41 help='Gerrit cookie file') 42 parser.add_argument('--limits', default=1000, 43 help='Max number of change lists') 44 45 return parser.parse_args() 46 47 48def main(): 49 """Main function""" 50 args = _parse_args() 51 52 if not args.gerrit: 53 try: 54 args.gerrit = find_gerrit_name() 55 # pylint: disable=bare-except 56 except: 57 print('gerrit instance not found, use [-g GERRIT]') 58 sys.exit(1) 59 60 # Query change lists 61 url_opener = create_url_opener_from_args(args) 62 change_lists = query_change_lists( 63 url_opener, args.gerrit, args.query, args.limits) 64 65 # Download patch files 66 num_changes = len(change_lists) 67 num_changes_width = len(str(num_changes)) 68 for i, change in enumerate(change_lists, start=1): 69 print('{:>{}}/{} | {} {}'.format( 70 i, num_changes_width, num_changes, change['_number'], 71 change['subject'])) 72 73 patch_file = get_patch(url_opener, args.gerrit, change['id']) 74 with open('{}.patch'.format(change['_number']), 'wb') as output_file: 75 output_file.write(patch_file) 76 77if __name__ == '__main__': 78 main() 79