<s> from __future__ import unicode_literals <EOL> import logging <EOL> import os <EOL> import re <EOL> from rbtools . clients import SCMClient , RepositoryInfo <EOL> from rbtools . clients . errors import TooManyRevisionsError <EOL> from rbtools . utils . checks import check_install <EOL> from rbtools . utils . diffs import filter_diff , normalize_patterns <EOL> from rbtools . utils . process import execute <EOL> USING_PARENT_PREFIX = '<STR_LIT>' <EOL> class BazaarClient ( SCMClient ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> supports_diff_exclude_patterns = True <EOL> can_branch = True <EOL> INDEX_FILE_RE = re . compile ( b"<STR_LIT>" ) <EOL> BRANCH_REGEX = ( <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' ) <EOL> REVISION_SEPARATOR_REGEX = re . compile ( r'<STR_LIT>' ) <EOL> def get_repository_info ( self ) : <EOL> """<STR_LIT>""" <EOL> if not check_install ( [ '<STR_LIT>' , '<STR_LIT>' ] ) : <EOL> logging . debug ( '<STR_LIT>' ) <EOL> return None <EOL> bzr_info = execute ( [ '<STR_LIT>' , '<STR_LIT:info>' ] , ignore_errors = True ) <EOL> if '<STR_LIT>' in bzr_info : <EOL> repository_info = None <EOL> else : <EOL> branch_match = re . search ( self . BRANCH_REGEX , bzr_info , re . MULTILINE ) <EOL> path = branch_match . group ( '<STR_LIT>' ) <EOL> if path == '<STR_LIT:.>' : <EOL> path = os . getcwd ( ) <EOL> repository_info = RepositoryInfo ( <EOL> path = path , <EOL> base_path = '<STR_LIT:/>' , <EOL> supports_parent_diffs = True ) <EOL> return repository_info <EOL> def parse_revision_spec ( self , revisions = [ ] ) : <EOL> """<STR_LIT>""" <EOL> n_revs = len ( revisions ) <EOL> result = { } <EOL> if n_revs == <NUM_LIT:0> : <EOL> result [ '<STR_LIT>' ] = self . _get_revno ( ) <EOL> result [ '<STR_LIT>' ] = self . _get_revno ( '<STR_LIT>' ) <EOL> elif n_revs == <NUM_LIT:1> or n_revs == <NUM_LIT:2> : <EOL> if n_revs == <NUM_LIT:1> : <EOL> revisions = self . REVISION_SEPARATOR_REGEX . split ( revisions [ <NUM_LIT:0> ] ) <EOL> n_revs = len ( revisions ) <EOL> if n_revs == <NUM_LIT:1> : <EOL> result [ '<STR_LIT>' ] = self . _get_revno ( '<STR_LIT>' + revisions [ <NUM_LIT:0> ] ) <EOL> result [ '<STR_LIT>' ] = self . _get_revno ( revisions [ <NUM_LIT:0> ] ) <EOL> elif n_revs == <NUM_LIT:2> : <EOL> result [ '<STR_LIT>' ] = self . _get_revno ( revisions [ <NUM_LIT:0> ] ) <EOL> result [ '<STR_LIT>' ] = self . _get_revno ( revisions [ <NUM_LIT:1> ] ) <EOL> else : <EOL> raise TooManyRevisionsError <EOL> else : <EOL> raise TooManyRevisionsError <EOL> if self . options . parent_branch : <EOL> result [ '<STR_LIT>' ] = result [ '<STR_LIT>' ] <EOL> result [ '<STR_LIT>' ] = self . _get_revno ( <EOL> '<STR_LIT>' % self . options . parent_branch ) <EOL> return result <EOL> def _get_revno ( self , revision_spec = None ) : <EOL> command = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> if revision_spec : <EOL> command += [ '<STR_LIT>' , revision_spec ] <EOL> result = execute ( command ) . strip ( ) . split ( '<STR_LIT:\n>' ) <EOL> if len ( result ) == <NUM_LIT:1> : <EOL> return '<STR_LIT>' + result [ <NUM_LIT:0> ] <EOL> elif len ( result ) == <NUM_LIT:2> and result [ <NUM_LIT:0> ] . startswith ( USING_PARENT_PREFIX ) : <EOL> branch = result [ <NUM_LIT:0> ] [ len ( USING_PARENT_PREFIX ) : ] <EOL> return '<STR_LIT>' % ( result [ <NUM_LIT:1> ] , branch ) <EOL> def diff ( self , revisions , include_files = [ ] , exclude_patterns = [ ] , <EOL> extra_args = [ ] ) : <EOL> """<STR_LIT>""" <EOL> exclude_patterns = normalize_patterns ( exclude_patterns , <EOL> self . get_repository_info ( ) . path ) <EOL> diff = self . _get_range_diff ( revisions [ '<STR_LIT>' ] , revisions [ '<STR_LIT>' ] , <EOL> include_files , exclude_patterns ) <EOL> if '<STR_LIT>' in revisions : <EOL> parent_diff = self . _get_range_diff ( <EOL> revisions [ '<STR_LIT>' ] , revisions [ '<STR_LIT>' ] , include_files , <EOL> exclude_patterns ) <EOL> else : <EOL> parent_diff = None <EOL> return { <EOL> '<STR_LIT>' : diff , <EOL> '<STR_LIT>' : parent_diff , <EOL> } <EOL> def _get_range_diff ( self , base , tip , include_files , exclude_patterns = [ ] ) : <EOL> """<STR_LIT>""" <EOL> diff_cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' % ( base , tip ) ] + include_files <EOL> diff = execute ( diff_cmd , ignore_errors = True , log_output_on_error = False , <EOL> split_lines = True , results_unicode = False ) <EOL> if diff : <EOL> if exclude_patterns : <EOL> diff = filter_diff ( diff , self . INDEX_FILE_RE , exclude_patterns , <EOL> base_dir = self . get_repository_info ( ) . path ) <EOL> return b'<STR_LIT>' . join ( diff ) <EOL> else : <EOL> return None <EOL> def get_raw_commit_message ( self , revisions ) : <EOL> log_cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' % ( revisions [ '<STR_LIT>' ] , revisions [ '<STR_LIT>' ] ) ] <EOL> lines = execute ( log_cmd + [ '<STR_LIT>' ] , <EOL> ignore_errors = True , split_lines = True ) <EOL> n_revs = len ( lines ) - <NUM_LIT:1> <EOL> lines = execute ( log_cmd + [ '<STR_LIT>' , '<STR_LIT>' , str ( n_revs ) ] , <EOL> ignore_errors = True , split_lines = True ) <EOL> message = [ ] <EOL> for line in lines : <EOL> if line . startswith ( '<STR_LIT:\t>' ) : <EOL> message . append ( line [ <NUM_LIT:1> : ] ) <EOL> elif not line . strip ( ) : <EOL> message . append ( line ) <EOL> return '<STR_LIT>' . join ( message ) . strip ( ) <EOL> def get_current_branch ( self ) : <EOL> """<STR_LIT>""" <EOL> return execute ( [ '<STR_LIT>' , '<STR_LIT>' ] , ignore_errors = True ) . strip ( ) </s>
