<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import os <EOL> import re <EOL> import sys <EOL> from subprocess import Popen , PIPE , check_call , check_output <EOL> from urllib import urlopen <EOL> from gh_api import ( <EOL> get_issues_list , <EOL> get_pull_request , <EOL> get_pull_request_files , <EOL> is_pull_request , <EOL> get_milestone_id , <EOL> ) <EOL> from pandas import Series <EOL> def find_rejects ( root = '<STR_LIT:.>' ) : <EOL> for dirname , dirs , files in os . walk ( root ) : <EOL> for fname in files : <EOL> if fname . endswith ( '<STR_LIT>' ) : <EOL> yield os . path . join ( dirname , fname ) <EOL> def get_current_branch ( ) : <EOL> branches = check_output ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> for branch in branches . splitlines ( ) : <EOL> if branch . startswith ( '<STR_LIT:*>' ) : <EOL> return branch [ <NUM_LIT:1> : ] . strip ( ) <EOL> def backport_pr ( branch , num , project = '<STR_LIT>' ) : <EOL> current_branch = get_current_branch ( ) <EOL> if branch != current_branch : <EOL> check_call ( [ '<STR_LIT>' , '<STR_LIT>' , branch ] ) <EOL> check_call ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> pr = get_pull_request ( project , num , auth = True ) <EOL> files = get_pull_request_files ( project , num , auth = True ) <EOL> patch_url = pr [ '<STR_LIT>' ] <EOL> title = pr [ '<STR_LIT:title>' ] <EOL> description = pr [ '<STR_LIT:body>' ] <EOL> fname = "<STR_LIT>" % num <EOL> if os . path . exists ( fname ) : <EOL> print ( "<STR_LIT>" . format ( ** locals ( ) ) ) <EOL> with open ( fname ) as f : <EOL> patch = f . read ( ) <EOL> else : <EOL> req = urlopen ( patch_url ) <EOL> patch = req . read ( ) <EOL> msg = "<STR_LIT>" % ( num , title ) + '<STR_LIT>' + description <EOL> check = Popen ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , stdin = PIPE ) <EOL> a , b = check . communicate ( patch ) <EOL> if check . returncode : <EOL> print ( "<STR_LIT>" . format ( ** locals ( ) ) ) <EOL> print ( "<STR_LIT>" . format ( ** locals ( ) ) ) <EOL> print ( "<STR_LIT>" . format ( ** locals ( ) ) ) <EOL> if not os . path . exists ( fname ) : <EOL> with open ( fname , '<STR_LIT:wb>' ) as f : <EOL> f . write ( patch ) <EOL> return <NUM_LIT:1> <EOL> p = Popen ( [ '<STR_LIT>' , '<STR_LIT>' ] , stdin = PIPE ) <EOL> a , b = p . communicate ( patch ) <EOL> filenames = [ f [ '<STR_LIT:filename>' ] for f in files ] <EOL> check_call ( [ '<STR_LIT>' , '<STR_LIT>' ] + filenames ) <EOL> check_call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , msg ] ) <EOL> print ( "<STR_LIT>" % num ) <EOL> print ( ) <EOL> print ( msg ) <EOL> print ( ) <EOL> if branch != current_branch : <EOL> check_call ( [ '<STR_LIT>' , '<STR_LIT>' , current_branch ] ) <EOL> return <NUM_LIT:0> <EOL> backport_re = re . compile ( r"<STR_LIT>" ) <EOL> def already_backported ( branch , since_tag = None ) : <EOL> """<STR_LIT>""" <EOL> if since_tag is None : <EOL> since_tag = check_output ( [ '<STR_LIT>' , '<STR_LIT>' , branch , '<STR_LIT>' ] ) . decode ( '<STR_LIT:utf8>' ) . strip ( ) <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' % ( since_tag , branch ) , '<STR_LIT>' ] <EOL> lines = check_output ( cmd ) . decode ( '<STR_LIT:utf8>' ) <EOL> return set ( int ( num ) for num in backport_re . findall ( lines ) ) <EOL> def should_backport ( labels = None , milestone = None ) : <EOL> """<STR_LIT>""" <EOL> if labels is None and milestone is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> elif labels is not None and milestone is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if labels is not None : <EOL> issues = get_issues_list ( "<STR_LIT>" , <EOL> labels = labels , <EOL> state = '<STR_LIT>' , <EOL> auth = True , <EOL> ) <EOL> else : <EOL> milestone_id = get_milestone_id ( "<STR_LIT>" , milestone , <EOL> auth = True ) <EOL> issues = get_issues_list ( "<STR_LIT>" , <EOL> milestone = milestone_id , <EOL> state = '<STR_LIT>' , <EOL> auth = True , <EOL> ) <EOL> should_backport = [ ] <EOL> merged_dates = [ ] <EOL> for issue in issues : <EOL> if not is_pull_request ( issue ) : <EOL> continue <EOL> pr = get_pull_request ( "<STR_LIT>" , issue [ '<STR_LIT>' ] , <EOL> auth = True ) <EOL> if not pr [ '<STR_LIT>' ] : <EOL> print ( "<STR_LIT>" % pr [ '<STR_LIT>' ] ) <EOL> continue <EOL> if pr [ '<STR_LIT>' ] not in should_backport : <EOL> merged_dates . append ( pr [ '<STR_LIT>' ] ) <EOL> should_backport . append ( pr [ '<STR_LIT>' ] ) <EOL> return Series ( merged_dates , index = should_backport ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if len ( sys . argv ) < <NUM_LIT:2> : <EOL> print ( __doc__ ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if len ( sys . argv ) < <NUM_LIT:3> : <EOL> branch = sys . argv [ <NUM_LIT:1> ] <EOL> already = already_backported ( branch ) <EOL> should = should_backport ( milestone = "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> to_backport = [ ] <EOL> if already : <EOL> should = should . ix [ set ( should . index ) . difference ( already ) ] <EOL> should . sort ( ) <EOL> for pr , date in should . iteritems ( ) : <EOL> print ( pr ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> sys . exit ( backport_pr ( sys . argv [ <NUM_LIT:1> ] , int ( sys . argv [ <NUM_LIT:2> ] ) ) ) </s>
