<s> """<STR_LIT>""" <EOL> from __future__ import with_statement <EOL> import base64 <EOL> import contextlib <EOL> import httplib <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> import simplejson as json <EOL> import os <EOL> import re <EOL> import subprocess <EOL> import urllib <EOL> EXPORT = '<STR_LIT>' <EOL> GETINFO = '<STR_LIT>' <EOL> MV_BRANCH = '<STR_LIT>' <EOL> RM_BRANCH = '<STR_LIT>' <EOL> SUBMIT = '<STR_LIT>' <EOL> SYNC = '<STR_LIT>' <EOL> CODE_REVIEW = '<STR_LIT>' <EOL> OAUTH2_ARGS = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> REVISION_TEMPLATE = '<STR_LIT>' <EOL> SEND_MAIL_ARG = '<STR_LIT>' <EOL> VCS_ARG = '<STR_LIT>' <EOL> BRANCH = '<STR_LIT>' <EOL> CC = '<STR_LIT>' <EOL> COMMIT_HASH = '<STR_LIT>' <EOL> HOST = '<STR_LIT:host>' <EOL> ISSUE = '<STR_LIT>' <EOL> ISSUE_DESCRIPTION = '<STR_LIT:description>' <EOL> LAST_COMMIT = '<STR_LIT>' <EOL> LAST_SYNCED = '<STR_LIT>' <EOL> PRIVATE = '<STR_LIT>' <EOL> REMOTE = '<STR_LIT>' <EOL> REMOTE_INFO = '<STR_LIT>' <EOL> REVIEW_INFO = '<STR_LIT>' <EOL> RIETVELD_KEY = '<STR_LIT>' <EOL> RIETVELD_KEY_TEMPLATE = RIETVELD_KEY + '<STR_LIT>' <EOL> RIETVELD_KEY_REGEX = '<STR_LIT>' % ( RIETVELD_KEY , ) <EOL> REASON = '<STR_LIT>' <EOL> REVIEWERS = '<STR_LIT>' <EOL> SERVER = '<STR_LIT>' <EOL> STATUS = '<STR_LIT:status>' <EOL> SUBJECT = '<STR_LIT>' <EOL> SYNC_HALTED = '<STR_LIT>' <EOL> CLOSE_ISSUE_TEMPLATE = '<STR_LIT>' <EOL> ISSUE_ARG_TEMPLATE = '<STR_LIT>' <EOL> ISSUE_URI_PATH_TEMPLATE = '<STR_LIT>' <EOL> ISSUE_INFO_ERROR_TEMPLATE = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> MESSAGE = '<STR_LIT:message>' <EOL> PUBLISH_ISSUE_MESSAGE_TEMPLATE = '<STR_LIT>' <EOL> PUBLISH_ISSUE_BASE = { <EOL> '<STR_LIT>' : '<STR_LIT:true>' , <EOL> '<STR_LIT>' : '<STR_LIT:true>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> APPROVAL = '<STR_LIT>' <EOL> BAD_REMOTE_ERROR_TEMPLATE = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> BRANCH_REF_TEMPLATE = '<STR_LIT>' <EOL> COMMIT_HASH_REGEX = re . compile ( '<STR_LIT>' ) <EOL> DESCRIPTION_NEWLINE = '<STR_LIT>' <EOL> FAILED_CLOSE_TEMPLATE = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> FAILED_PUBLISH_TEMPLATE = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> GOOGLE_CODEHOSTING_BAD_URI_TEMPLATE = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> GOOGLE_CODEHOSTING_HG_BAD_REMOTE_TEMPLATE = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> GOOGLE_CODEHOSTING_HG_NO_MAPPING_TEMPLATE = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> INVALID_BRANCH_CHOICE = '<STR_LIT>' <EOL> INVALID_MESSAGE_CHOICE = '<STR_LIT>' <EOL> INVALID_REMOTE_CHOICE = '<STR_LIT>' <EOL> LS_REMOTE_ERROR_TEMPLATE = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> MESSAGES = '<STR_LIT>' <EOL> MESSAGE_CHOICE_PROMPT = '<STR_LIT>' <EOL> MESSAGE_PROMPT_IN_REVIEW = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> MESSAGE_PROMPT_PRE_REVIEW_TEMPLATE = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> MISMATCHED_COMMIT_SUBJECT_TEMPLATE = """<STR_LIT>""" <EOL> NO_BRANCHES_ERROR_TEMPLATE = '<STR_LIT>' <EOL> NO_COMMIT_TEMPLATE = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> NO_REMOTES_ERROR = '<STR_LIT>' <EOL> REMOTE_BRANCH = '<STR_LIT>' <EOL> REMOTE_URL_KEY_TEMPLATE = '<STR_LIT>' <EOL> REMOTE_PROMPT = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> REMOTE_CHOICE_PROMPT = '<STR_LIT>' <EOL> REMOTE_BRANCH_PROMPT = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> REMOTE_BRANCH_CHOICE_PROMPT = '<STR_LIT>' <EOL> SQUASH_COMMIT_TEMPLATE = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> SUBJECT_TOO_LONG_TEMPLATE = '<STR_LIT>' <EOL> TIP_BEHIND_HINT = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> XSRF_TOKEN = '<STR_LIT>' <EOL> XSRF_HEADERS = { '<STR_LIT>' : '<STR_LIT:true>' } <EOL> class GitRvException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def _check_hash ( value ) : <EOL> """<STR_LIT>""" <EOL> if COMMIT_HASH_REGEX . match ( value ) is None : <EOL> raise GitRvException ( '<STR_LIT>' % ( value , ) ) <EOL> def _check_single_line ( value ) : <EOL> """<STR_LIT>""" <EOL> if not value . endswith ( '<STR_LIT:\n>' ) or value . count ( '<STR_LIT:\n>' ) != <NUM_LIT:1> : <EOL> raise GitRvException ( '<STR_LIT>' % ( value , ) ) <EOL> def capture_command ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> proc = subprocess . Popen ( args , stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE ) <EOL> result = proc . wait ( ) <EOL> stdout = proc . stdout . read ( ) <EOL> stderr = proc . stderr . read ( ) <EOL> if not kwargs . get ( '<STR_LIT>' , True ) : <EOL> return ( result , stdout , stderr ) <EOL> if result != <NUM_LIT:0> : <EOL> command = '<STR_LIT:U+0020>' . join ( args ) <EOL> raise GitRvException ( '<STR_LIT>' % ( command , stderr ) ) <EOL> if kwargs . get ( '<STR_LIT>' , True ) : <EOL> _check_single_line ( stdout ) <EOL> return stdout . rstrip ( ) <EOL> def get_current_branch ( ) : <EOL> """<STR_LIT>""" <EOL> return capture_command ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def get_git_root ( ) : <EOL> """<STR_LIT>""" <EOL> return capture_command ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def get_head_commit ( current_branch = None ) : <EOL> """<STR_LIT>""" <EOL> current_branch = current_branch or get_current_branch ( ) <EOL> result = capture_command ( '<STR_LIT>' , '<STR_LIT>' , current_branch ) <EOL> _check_hash ( result ) <EOL> return result <EOL> def get_current_issue ( current_branch = None ) : <EOL> """<STR_LIT>""" <EOL> current_branch = current_branch or get_current_branch ( ) <EOL> rietveld_info = RietveldInfo . from_branch ( branch_name = current_branch ) <EOL> if ( rietveld_info . review_info is None or <EOL> rietveld_info . review_info . issue is None ) : <EOL> raise GitRvException ( '<STR_LIT>' % ( current_branch , ) ) <EOL> return rietveld_info . review_info . issue <EOL> def get_commit_subject ( commit_hash ) : <EOL> """<STR_LIT>""" <EOL> return capture_command ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , commit_hash ) <EOL> def get_commit_message ( commit_hash ) : <EOL> """<STR_LIT>""" <EOL> return capture_command ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> commit_hash , single_line = False ) <EOL> def get_commit_message_parts ( commit_hash = None , current_branch = None ) : <EOL> """<STR_LIT>""" <EOL> commit_hash = commit_hash or get_head_commit ( current_branch = current_branch ) <EOL> commit_subject = get_commit_subject ( commit_hash ) <EOL> if len ( commit_subject ) > <NUM_LIT:100> : <EOL> raise GitRvException ( SUBJECT_TOO_LONG_TEMPLATE % ( commit_subject , ) ) <EOL> commit_message = get_commit_message ( commit_hash ) <EOL> if not commit_message . startswith ( commit_subject ) : <EOL> raise GitRvException ( MISMATCHED_COMMIT_SUBJECT_TEMPLATE % <EOL> ( commit_message , commit_subject ) ) <EOL> commit_description = commit_message . split ( commit_subject , <NUM_LIT:1> ) [ <NUM_LIT:1> ] . lstrip ( ) <EOL> return commit_subject , commit_description <EOL> def user_choice_from_list ( choices , pre_prompt_message , input_message , <EOL> error_message_none , error_message_invalid ) : <EOL> """<STR_LIT>""" <EOL> if len ( choices ) == <NUM_LIT:0> : <EOL> raise GitRvException ( error_message_none ) <EOL> elif len ( choices ) == <NUM_LIT:1> : <EOL> return choices [ <NUM_LIT:0> ] <EOL> else : <EOL> print pre_prompt_message <EOL> options = '<STR_LIT:\n>' . join ( [ '<STR_LIT>' % pair for pair in enumerate ( choices ) ] ) <EOL> print options <EOL> choice = raw_input ( input_message ) . strip ( ) <EOL> if choice in choices : <EOL> return choice <EOL> else : <EOL> try : <EOL> index = int ( choice ) <EOL> return choices [ index ] <EOL> except ( ValueError , TypeError , IndexError ) : <EOL> raise GitRvException ( error_message_invalid % ( choice , ) ) <EOL> def get_commits ( base_commit , head_commit ) : <EOL> """<STR_LIT>""" <EOL> rev_list_arg = '<STR_LIT>' % ( base_commit , head_commit ) <EOL> rev_list_output = capture_command ( <EOL> '<STR_LIT>' , '<STR_LIT>' , rev_list_arg , single_line = False ) <EOL> commits = rev_list_output . split ( '<STR_LIT:\n>' ) if rev_list_output else [ ] <EOL> [ _check_hash ( commit_hash ) for commit_hash in commits ] <EOL> return commits <EOL> def get_user_commit_message_parts ( base_commit , head_commit , remote_branch = None ) : <EOL> """<STR_LIT>""" <EOL> commits = get_commits ( base_commit , head_commit ) <EOL> if len ( commits ) == <NUM_LIT:0> : <EOL> raise GitRvException ( NO_COMMIT_TEMPLATE % ( base_commit , ) ) <EOL> commit_choices = { } <EOL> for commit_hash in commits : <EOL> commit_message_parts = get_commit_message_parts ( commit_hash = commit_hash ) <EOL> single_value = '<STR_LIT>' . join ( commit_message_parts ) <EOL> commit_choices [ single_value ] = commit_message_parts <EOL> error_message_none = '<STR_LIT>' <EOL> if remote_branch is None : <EOL> pre_prompt_message = MESSAGE_PROMPT_IN_REVIEW <EOL> else : <EOL> pre_prompt_message = MESSAGE_PROMPT_PRE_REVIEW_TEMPLATE % remote_branch <EOL> commit_choice = user_choice_from_list ( <EOL> commit_choices . keys ( ) , pre_prompt_message , MESSAGE_CHOICE_PROMPT , <EOL> error_message_none , INVALID_MESSAGE_CHOICE ) <EOL> return commit_choices [ commit_choice ] <EOL> def get_remote ( ) : <EOL> """<STR_LIT>""" <EOL> remote_output = capture_command ( '<STR_LIT>' , '<STR_LIT>' , single_line = False ) <EOL> remotes = remote_output . split ( '<STR_LIT:\n>' ) <EOL> return user_choice_from_list ( remotes , REMOTE_PROMPT , REMOTE_CHOICE_PROMPT , <EOL> NO_REMOTES_ERROR , INVALID_REMOTE_CHOICE ) <EOL> def get_remote_branches_list ( remote ) : <EOL> """<STR_LIT>""" <EOL> branches_output = capture_command ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> remote , single_line = False ) <EOL> split_branches = [ line . split ( '<STR_LIT:\t>' ) for line in branches_output . split ( '<STR_LIT:\n>' ) ] <EOL> branches = { } <EOL> for split in split_branches : <EOL> if len ( split ) != <NUM_LIT:2> : <EOL> bad_content = '<STR_LIT:\n>' . join ( [ <EOL> repr ( '<STR_LIT:\t>' . join ( split ) ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> error_msg = LS_REMOTE_ERROR_TEMPLATE % ( bad_content , ) <EOL> raise GitRvException ( error_msg ) <EOL> commit_hash , head_ref = split <EOL> try : <EOL> _check_hash ( commit_hash ) <EOL> except GitRvException , exc : <EOL> bad_content = exc . message <EOL> error_msg = LS_REMOTE_ERROR_TEMPLATE % ( bad_content , ) <EOL> raise GitRvException ( error_msg ) <EOL> if not head_ref . startswith ( '<STR_LIT>' ) : <EOL> bad_content = ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( head_ref , ) ) <EOL> error_msg = LS_REMOTE_ERROR_TEMPLATE % ( bad_content , ) <EOL> raise GitRvException ( error_msg ) <EOL> branch = head_ref . split ( '<STR_LIT>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> branches [ branch ] = commit_hash <EOL> return branches <EOL> def get_remote_branch ( remote ) : <EOL> """<STR_LIT>""" <EOL> branches = get_remote_branches_list ( remote ) <EOL> no_branches_error = NO_BRANCHES_ERROR_TEMPLATE % ( remote , ) <EOL> remote_branch = user_choice_from_list ( <EOL> branches . keys ( ) , REMOTE_BRANCH_PROMPT , REMOTE_BRANCH_CHOICE_PROMPT , <EOL> no_branches_error , INVALID_BRANCH_CHOICE ) <EOL> commit_hash = branches [ remote_branch ] <EOL> return remote_branch , commit_hash <EOL> def get_remote_url ( remote ) : <EOL> """<STR_LIT>""" <EOL> url_config_key = REMOTE_URL_KEY_TEMPLATE % ( remote , ) <EOL> return capture_command ( '<STR_LIT>' , '<STR_LIT>' , url_config_key ) <EOL> def get_remote_info ( current_branch = None ) : <EOL> """<STR_LIT>""" <EOL> remote = get_remote ( ) <EOL> remote_branch , commit_hash = get_remote_branch ( remote ) <EOL> url = get_remote_url ( remote ) <EOL> if current_branch is not None : <EOL> containing_output = capture_command ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> commit_hash , single_line = False ) <EOL> branches_containing = [ branch [ <NUM_LIT:2> : ] <EOL> for branch in containing_output . split ( '<STR_LIT:\n>' ) ] <EOL> if current_branch not in branches_containing : <EOL> error_message = BAD_REMOTE_ERROR_TEMPLATE % { <EOL> REMOTE : remote , <EOL> REMOTE_BRANCH : remote_branch , <EOL> COMMIT_HASH : commit_hash , <EOL> BRANCH : current_branch , <EOL> } <EOL> raise GitRvException ( error_message ) <EOL> return RemoteInfo ( remote = remote , branch = remote_branch , <EOL> commit_hash = commit_hash , last_synced = commit_hash , url = url ) <EOL> def branch_exists ( branch ) : <EOL> """<STR_LIT>""" <EOL> ref = BRANCH_REF_TEMPLATE % branch <EOL> status_code , _ , _ = capture_command ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , ref , expect_success = False ) <EOL> return status_code == <NUM_LIT:0> <EOL> def _string_type_cast ( value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( value , basestring ) : <EOL> raise GitRvException ( '<STR_LIT>' % ( value , ) ) <EOL> return value <EOL> def _int_type_cast ( value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> value = int ( value ) <EOL> except ( ValueError , TypeError ) : <EOL> raise GitRvException ( '<STR_LIT>' <EOL> % ( value , ) ) <EOL> return value <EOL> def _hash_type_cast ( value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> _string_type_cast ( value ) <EOL> _check_hash ( value ) <EOL> except GitRvException : <EOL> raise GitRvException ( '<STR_LIT>' <EOL> % ( value , ) ) <EOL> return value <EOL> def simple_update_property ( attr_name , can_change = False , <EOL> type_cast_method = _string_type_cast ) : <EOL> """<STR_LIT>""" <EOL> if not attr_name . startswith ( '<STR_LIT:_>' ) : <EOL> raise GitRvException ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( attr_name , ) ) <EOL> def getter ( self ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( self , attr_name , None ) <EOL> def setter ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value = type_cast_method ( value ) <EOL> current_value = getattr ( self , attr_name , None ) <EOL> if current_value is None or can_change : <EOL> setattr ( self , attr_name , value ) <EOL> elif current_value != value : <EOL> raise GitRvException ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( attr_name , current_value ) ) <EOL> return property ( getter , setter ) <EOL> class _MetaBaseRepository ( type ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( cls , name , bases , classdict ) : <EOL> """<STR_LIT>""" <EOL> super ( _MetaBaseRepository , cls ) . __init__ ( name , bases , classdict ) <EOL> cls . REPOSITORY_TYPE_REGISTRY . add ( cls ) <EOL> class RepositoryInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = _MetaBaseRepository <EOL> REPOSITORY_TYPE_REGISTRY = set ( ) <EOL> def __init__ ( self , remote_url , match ) : <EOL> """<STR_LIT>""" <EOL> self . _remote_url = remote_url <EOL> self . populate_from_match ( match ) <EOL> @ classmethod <EOL> def from_remote ( cls , remote_url ) : <EOL> """<STR_LIT>""" <EOL> for klass in cls . REPOSITORY_TYPE_REGISTRY : <EOL> match = klass . match ( remote_url ) <EOL> if match is not None : <EOL> return klass ( remote_url , match ) <EOL> @ classmethod <EOL> def match ( cls , unused_value ) : <EOL> """<STR_LIT>""" <EOL> return <EOL> class GoogleCodehostingRepositoryInfo ( RepositoryInfo ) : <EOL> """<STR_LIT>""" <EOL> GOOGLE_CODEHOSTING_REGEX = re . compile ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> GOOGLE_CODEHOSTING_COMMIT_LINK_TEMPLATE = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def populate_from_match ( self , match ) : <EOL> """<STR_LIT>""" <EOL> if match is None : <EOL> raise GitRvException ( '<STR_LIT>' ) <EOL> try : <EOL> project = match . group ( '<STR_LIT>' ) <EOL> except IndexError : <EOL> raise GitRvException ( '<STR_LIT>' ) <EOL> repository = None <EOL> dot_count = project . count ( '<STR_LIT:.>' ) <EOL> if dot_count == <NUM_LIT:1> : <EOL> project , repository = project . split ( '<STR_LIT:.>' ) <EOL> elif dot_count > <NUM_LIT:1> : <EOL> raise GitRvException ( <EOL> GOOGLE_CODEHOSTING_BAD_URI_TEMPLATE % ( project , ) ) <EOL> self . project = project <EOL> self . repository = repository <EOL> def commit_link ( self , commit_hash ) : <EOL> """<STR_LIT>""" <EOL> query_params = { '<STR_LIT:r>' : commit_hash } <EOL> if self . repository is not None : <EOL> query_params [ '<STR_LIT>' ] = self . repository <EOL> query_string = urllib . urlencode ( query_params ) <EOL> return self . GOOGLE_CODEHOSTING_COMMIT_LINK_TEMPLATE % ( self . project , <EOL> query_string ) <EOL> @ classmethod <EOL> def match ( cls , value ) : <EOL> """<STR_LIT>""" <EOL> return cls . GOOGLE_CODEHOSTING_REGEX . match ( value ) <EOL> class GoogleCodehostingHgRepositoryInfo ( GoogleCodehostingRepositoryInfo ) : <EOL> """<STR_LIT>""" <EOL> GOOGLE_CODEHOSTING_REGEX = re . compile ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def __init__ ( self , remote_url , match ) : <EOL> """<STR_LIT>""" <EOL> super ( GoogleCodehostingHgRepositoryInfo , self ) . __init__ ( <EOL> remote_url , match ) <EOL> self . __add_mapfile_path ( remote_url ) <EOL> def __add_mapfile_path ( self , remote_url ) : <EOL> """<STR_LIT>""" <EOL> if not remote_url . startswith ( '<STR_LIT>' ) : <EOL> raise GitRvException ( <EOL> GOOGLE_CODEHOSTING_HG_BAD_REMOTE_TEMPLATE % ( remote_url , ) ) <EOL> actual_uri = remote_url . split ( '<STR_LIT>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> hgremote_directory = urllib . quote_plus ( actual_uri ) <EOL> project_root = get_git_root ( ) <EOL> self . mapfile_path = os . path . join ( <EOL> project_root , '<STR_LIT>' , '<STR_LIT>' , <EOL> hgremote_directory , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __commit_mapping ( self ) : <EOL> """<STR_LIT>""" <EOL> with open ( self . mapfile_path , '<STR_LIT>' ) as fh : <EOL> content = fh . read ( ) . rstrip ( ) <EOL> return dict ( row . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) for row in content . split ( '<STR_LIT:\n>' ) ) <EOL> def commit_link ( self , commit_hash ) : <EOL> """<STR_LIT>""" <EOL> commit_mapping = self . __commit_mapping ( ) <EOL> if commit_hash not in commit_mapping : <EOL> msg = GOOGLE_CODEHOSTING_HG_NO_MAPPING_TEMPLATE % ( commit_hash , ) <EOL> raise GitRvException ( msg ) <EOL> hg_commit_hash = commit_mapping [ commit_hash ] <EOL> return super ( GoogleCodehostingHgRepositoryInfo , self ) . commit_link ( <EOL> hg_commit_hash ) <EOL> class GithubRepositoryInfo ( RepositoryInfo ) : <EOL> """<STR_LIT>""" <EOL> GITHUB_PATTERN_SUFFIX = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> SIMPLE_PROTOCOL = re . compile ( '<STR_LIT>' + <EOL> GITHUB_PATTERN_SUFFIX ) <EOL> SSH = re . compile ( '<STR_LIT>' + GITHUB_PATTERN_SUFFIX ) <EOL> GITHUB_COMMIT_LINK_TEMPLATE = '<STR_LIT>' <EOL> def populate_from_match ( self , match ) : <EOL> """<STR_LIT>""" <EOL> if match is None : <EOL> raise GitRvException ( '<STR_LIT>' ) <EOL> try : <EOL> self . repository = match . group ( '<STR_LIT>' ) <EOL> self . organization = match . group ( '<STR_LIT>' ) <EOL> except IndexError : <EOL> raise GitRvException ( '<STR_LIT>' ) <EOL> def commit_link ( self , commit_hash ) : <EOL> """<STR_LIT>""" <EOL> return self . GITHUB_COMMIT_LINK_TEMPLATE % ( self . organization , <EOL> self . repository , commit_hash ) <EOL> @ classmethod <EOL> def match ( cls , value ) : <EOL> """<STR_LIT>""" <EOL> return cls . SIMPLE_PROTOCOL . match ( value ) or cls . SSH . match ( value ) <EOL> class _UpdateInfoBase ( object ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for key , value in kwargs . iteritems ( ) : <EOL> if key not in self . _properties : <EOL> raise GitRvException ( '<STR_LIT>' <EOL> % ( key , self . __class__ . __name__ ) ) <EOL> setattr ( self , key , value ) <EOL> def to_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> for prop_name in self . _properties : <EOL> value = getattr ( self , prop_name , None ) <EOL> if value is not None : <EOL> result [ prop_name ] = value <EOL> return result <EOL> def update ( self , other ) : <EOL> """<STR_LIT>""" <EOL> for prop_name in self . _properties : <EOL> other_value = getattr ( other , prop_name , None ) <EOL> if other_value is not None : <EOL> setattr ( self , prop_name , other_value ) <EOL> class RemoteInfo ( _UpdateInfoBase ) : <EOL> """<STR_LIT>""" <EOL> _properties = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:url>' ) <EOL> last_synced = simple_update_property ( '<STR_LIT>' , can_change = True , <EOL> type_cast_method = _hash_type_cast ) <EOL> commit_hash = simple_update_property ( '<STR_LIT>' , can_change = False , <EOL> type_cast_method = _hash_type_cast ) <EOL> remote = simple_update_property ( '<STR_LIT>' , can_change = False ) <EOL> branch = simple_update_property ( '<STR_LIT>' , can_change = False ) <EOL> url = simple_update_property ( '<STR_LIT>' , can_change = False ) <EOL> @ property <EOL> def repository_info ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . url is not None : <EOL> return RepositoryInfo . from_remote ( self . url ) <EOL> @ property <EOL> def remote_branch_ref ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( self . remote , self . branch ) <EOL> @ property <EOL> def head_in_remote_branch ( self ) : <EOL> """<STR_LIT>""" <EOL> return get_head_commit ( self . remote_branch_ref ) <EOL> class ReviewInfo ( _UpdateInfoBase ) : <EOL> """<STR_LIT>""" <EOL> _properties = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:description>' , '<STR_LIT>' ) <EOL> issue = simple_update_property ( '<STR_LIT>' , can_change = False , <EOL> type_cast_method = _int_type_cast ) <EOL> subject = simple_update_property ( '<STR_LIT>' , can_change = True ) <EOL> description = simple_update_property ( '<STR_LIT>' , can_change = True ) <EOL> last_commit = simple_update_property ( '<STR_LIT>' , can_change = True , <EOL> type_cast_method = _hash_type_cast ) <EOL> class RietveldInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> server = simple_update_property ( '<STR_LIT>' , can_change = False ) <EOL> def __init__ ( self , branch_name , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . _branch_name = branch_name <EOL> for key , value in kwargs . iteritems ( ) : <EOL> setattr ( self , key , value ) <EOL> @ classmethod <EOL> def from_branch ( cls , branch_name = None ) : <EOL> """<STR_LIT>""" <EOL> branch_name = branch_name or get_current_branch ( ) <EOL> metadata_key = RIETVELD_KEY_TEMPLATE % ( branch_name , ) <EOL> proc_result , opaque_info , _ = capture_command ( <EOL> '<STR_LIT>' , '<STR_LIT>' , metadata_key , expect_success = False ) <EOL> if proc_result != <NUM_LIT:0> : <EOL> return None <EOL> branch_info = json . loads ( base64 . b64decode ( opaque_info ) ) <EOL> return cls ( branch_name , ** branch_info ) <EOL> @ property <EOL> def key ( self ) : <EOL> """<STR_LIT>""" <EOL> return RIETVELD_KEY_TEMPLATE % ( self . _branch_name , ) <EOL> @ property <EOL> def remote_info ( self ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( self , '<STR_LIT>' , None ) <EOL> @ remote_info . setter <EOL> def remote_info ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , dict ) : <EOL> value = RemoteInfo ( ** value ) <EOL> elif not isinstance ( value , RemoteInfo ) : <EOL> raise GitRvException ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( value , self . key ) ) <EOL> if getattr ( self , '<STR_LIT>' , None ) is None : <EOL> self . _remote_info = value <EOL> else : <EOL> self . _remote_info . update ( value ) <EOL> @ property <EOL> def review_info ( self ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( self , '<STR_LIT>' , None ) <EOL> @ review_info . setter <EOL> def review_info ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , dict ) : <EOL> value = ReviewInfo ( ** value ) <EOL> elif not isinstance ( value , ReviewInfo ) : <EOL> raise GitRvException ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( value , self . key ) ) <EOL> if getattr ( self , '<STR_LIT>' , None ) is None : <EOL> self . _review_info = value <EOL> else : <EOL> self . _review_info . update ( value ) <EOL> def remove_key ( self , key ) : <EOL> """<STR_LIT>""" <EOL> if key not in self . __dict__ : <EOL> return <EOL> del self . __dict__ [ key ] <EOL> self . save ( ) <EOL> @ staticmethod <EOL> def remove ( branch_name = None ) : <EOL> """<STR_LIT>""" <EOL> branch_name = branch_name or get_current_branch ( ) <EOL> metadata_key = RIETVELD_KEY_TEMPLATE % ( branch_name , ) <EOL> capture_command ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> metadata_key , single_line = False ) <EOL> proc_result , _ , _ = capture_command ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> RIETVELD_KEY_REGEX , expect_success = False ) <EOL> if proc_result != <NUM_LIT:0> : <EOL> capture_command ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> RIETVELD_KEY , single_line = False ) <EOL> def to_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> if self . remote_info is not None : <EOL> result [ REMOTE_INFO ] = self . remote_info . to_dict ( ) <EOL> if self . review_info is not None : <EOL> result [ REVIEW_INFO ] = self . review_info . to_dict ( ) <EOL> if self . server is not None : <EOL> result [ SERVER ] = self . server <EOL> for key , value in self . __dict__ . iteritems ( ) : <EOL> if not key . startswith ( '<STR_LIT:_>' ) and value is not None : <EOL> result [ key ] = value <EOL> return result <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> as_dict = self . to_dict ( ) <EOL> opaque_info = base64 . b64encode ( json . dumps ( as_dict ) ) <EOL> result = capture_command ( '<STR_LIT>' , '<STR_LIT>' , self . key , <EOL> opaque_info , single_line = False ) <EOL> if result : <EOL> raise GitRvException ( '<STR_LIT>' % <EOL> ( result , ) ) <EOL> return as_dict <EOL> def in_clean_state ( ) : <EOL> """<STR_LIT>""" <EOL> proc_result , _ , _ = capture_command ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , expect_success = False ) <EOL> return proc_result == <NUM_LIT:0> <EOL> def in_review ( current_branch = None , rietveld_info = None ) : <EOL> """<STR_LIT>""" <EOL> if rietveld_info is None : <EOL> rietveld_info = RietveldInfo . from_branch ( branch_name = current_branch ) <EOL> if rietveld_info is None : <EOL> return False <EOL> return rietveld_info . review_info is not None <EOL> def get_issue_metadata ( issue = None , current_branch = None , server = CODE_REVIEW ) : <EOL> """<STR_LIT>""" <EOL> issue = issue or get_current_issue ( current_branch = current_branch ) <EOL> issue_path = ISSUE_URI_PATH_TEMPLATE % { ISSUE : issue } <EOL> with contextlib . closing ( httplib . HTTPSConnection ( server ) ) as connection : <EOL> connection . request ( '<STR_LIT:GET>' , issue_path ) <EOL> response = connection . getresponse ( ) <EOL> if response . status != <NUM_LIT:200> : <EOL> template_values = { ISSUE : issue , SERVER : server , <EOL> STATUS : response . status , <EOL> REASON : response . reason } <EOL> raise GitRvException ( ISSUE_INFO_ERROR_TEMPLATE % template_values ) <EOL> payload = response . read ( ) <EOL> return json . loads ( payload ) <EOL> def is_current_issue_approved ( issue = None , current_branch = None , <EOL> server = CODE_REVIEW ) : <EOL> """<STR_LIT>""" <EOL> issue_metadata = get_issue_metadata ( <EOL> issue = issue , current_branch = current_branch , server = server ) <EOL> messages = issue_metadata [ MESSAGES ] <EOL> return any ( message . get ( APPROVAL , False ) for message in messages ) <EOL> def update_rietveld_metadata_from_issue ( current_branch = None , <EOL> rietveld_info = None ) : <EOL> """<STR_LIT>""" <EOL> rietveld_info = rietveld_info or RietveldInfo . from_branch ( <EOL> branch_name = current_branch ) <EOL> if rietveld_info is None : <EOL> return ( False , None ) <EOL> server = rietveld_info . server <EOL> review_info = rietveld_info . review_info <EOL> if server is None or review_info is None : <EOL> return ( False , rietveld_info ) <EOL> issue = review_info . issue <EOL> issue_metadata = get_issue_metadata ( issue = issue , server = server ) <EOL> success = False <EOL> if ( REVIEWERS in issue_metadata and CC in issue_metadata and <EOL> SUBJECT in issue_metadata ) : <EOL> rietveld_info . reviewers = issue_metadata [ REVIEWERS ] <EOL> rietveld_info . cc = issue_metadata [ CC ] <EOL> review_info . subject = issue_metadata [ SUBJECT ] <EOL> if ISSUE_DESCRIPTION in issue_metadata : <EOL> if issue_metadata [ ISSUE_DESCRIPTION ] != review_info . subject : <EOL> review_info . description = issue_metadata [ ISSUE_DESCRIPTION ] <EOL> else : <EOL> review_info . description = '<STR_LIT>' <EOL> rietveld_info . save ( ) <EOL> success = True <EOL> return ( success , rietveld_info ) </s>
