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