<s> from __future__ import print_function , unicode_literals <EOL> import logging <EOL> import subprocess <EOL> from rbtools . api . client import RBClient <EOL> from rbtools . api . errors import APIError , ServerInterfaceError <EOL> SUBMITTED = '<STR_LIT>' <EOL> class HookError ( Exception ) : <EOL> pass <EOL> def get_api ( server_url , username , password ) : <EOL> """<STR_LIT>""" <EOL> api_client = RBClient ( server_url , username = username , password = password ) <EOL> try : <EOL> api_root = api_client . get_root ( ) <EOL> except ServerInterfaceError as e : <EOL> raise HookError ( '<STR_LIT>' <EOL> % ( server_url , e ) ) <EOL> except APIError as e : <EOL> raise HookError ( '<STR_LIT>' % e ) <EOL> return api_client , api_root <EOL> def execute ( command ) : <EOL> """<STR_LIT>""" <EOL> process = subprocess . Popen ( command , stdout = subprocess . PIPE ) <EOL> output = process . communicate ( ) [ <NUM_LIT:0> ] . strip ( ) <EOL> if process . returncode : <EOL> logging . warning ( '<STR_LIT>' , command ) <EOL> return None <EOL> return output <EOL> def initialize_logging ( ) : <EOL> """<STR_LIT>""" <EOL> root = logging . getLogger ( ) <EOL> handler = logging . StreamHandler ( ) <EOL> handler . setFormatter ( logging . Formatter ( '<STR_LIT>' ) ) <EOL> handler . setLevel ( logging . WARNING ) <EOL> root . addHandler ( handler ) <EOL> def get_review_request_id ( regex , commit_message ) : <EOL> """<STR_LIT>""" <EOL> match = regex . search ( commit_message ) <EOL> return ( match and int ( match . group ( '<STR_LIT:id>' ) ) ) or <NUM_LIT:0> <EOL> def get_review_request ( review_request_id , api_root ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> review_request = api_root . get_review_request ( <EOL> review_request_id = review_request_id ) <EOL> except APIError as e : <EOL> raise HookError ( '<STR_LIT>' % e ) <EOL> return review_request <EOL> def close_review_request ( server_url , username , password , review_request_id , <EOL> description ) : <EOL> """<STR_LIT>""" <EOL> api_client , api_root = get_api ( server_url , username , password ) <EOL> review_request = get_review_request ( review_request_id , api_root ) <EOL> if review_request . status == SUBMITTED : <EOL> logging . warning ( '<STR_LIT>' , <EOL> review_request_id , SUBMITTED ) <EOL> return <EOL> if description : <EOL> review_request = review_request . update ( status = SUBMITTED , <EOL> description = description ) <EOL> else : <EOL> review_request = review_request . update ( status = SUBMITTED ) <EOL> print ( '<STR_LIT>' % <EOL> ( review_request_id , review_request . status ) ) <EOL> def get_review_request_approval ( server_url , username , password , <EOL> review_request_id ) : <EOL> """<STR_LIT>""" <EOL> api_client , api_root = get_api ( server_url , username , password ) <EOL> review_request = get_review_request ( review_request_id , api_root ) <EOL> return review_request . approved , review_request . approval_failure </s>
