<s> import codecs <EOL> import os <EOL> import subprocess <EOL> from git_upstream . lib . utils import GitMixin <EOL> from git_upstream . log import LogDedentMixin <EOL> REBASE_EDITOR_SCRIPT = "<STR_LIT>" <EOL> REBASE_EDITOR_TODO = "<STR_LIT>" <EOL> TODO_EPILOGUE = """<STR_LIT>""" <EOL> class RebaseEditor ( GitMixin , LogDedentMixin ) : <EOL> def __init__ ( self , interactive = False , * args , ** kwargs ) : <EOL> self . _interactive = interactive <EOL> super ( RebaseEditor , self ) . __init__ ( ) <EOL> self . _editor = REBASE_EDITOR_SCRIPT <EOL> if interactive == '<STR_LIT>' : <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> self . _editor = "<STR_LIT>" % self . editor <EOL> @ property <EOL> def editor ( self ) : <EOL> return self . _editor <EOL> def _write_todo ( self , commits , * args , ** kwargs ) : <EOL> todo_file = os . path . join ( self . repo . git_dir , REBASE_EDITOR_TODO ) <EOL> if os . path . exists ( todo_file ) : <EOL> os . remove ( todo_file ) <EOL> if not os . path . exists ( os . path . dirname ( todo_file ) ) : <EOL> os . mkdir ( os . path . dirname ( todo_file ) ) <EOL> onto = kwargs . get ( '<STR_LIT>' , None ) <EOL> for idx , arg in enumerate ( args ) : <EOL> if arg . startswith ( "<STR_LIT>" ) : <EOL> onto = arg [ <NUM_LIT:7> : ] or args [ idx + <NUM_LIT:1> ] <EOL> break <EOL> root = None <EOL> with codecs . open ( todo_file , "<STR_LIT:w>" , "<STR_LIT:utf-8>" ) as todo : <EOL> for commit in commits : <EOL> if not root : <EOL> root = commit . parents [ <NUM_LIT:0> ] <EOL> subject = commit . message . splitlines ( ) [ <NUM_LIT:0> ] <EOL> todo . write ( "<STR_LIT>" % ( self . _shorten ( commit ) , subject ) ) <EOL> if not root : <EOL> todo . write ( "<STR_LIT>" ) <EOL> todo . write ( TODO_EPILOGUE % <EOL> { '<STR_LIT>' : "<STR_LIT>" % ( self . _shorten ( root ) , <EOL> self . _shorten ( commit ) ) , <EOL> '<STR_LIT>' : self . _shorten ( onto or root ) } ) <EOL> return todo_file <EOL> def _shorten ( self , commit ) : <EOL> if not commit : <EOL> return "<STR_LIT>" <EOL> return self . git . rev_parse ( commit , short = True ) <EOL> def _set_editor ( self , editor ) : <EOL> if self . git_sequence_editor : <EOL> self . _saveeditor = self . git_sequence_editor <EOL> if self . _interactive == '<STR_LIT>' : <EOL> os . environ [ '<STR_LIT>' ] = self . _saveeditor <EOL> os . environ [ '<STR_LIT>' ] = editor <EOL> else : <EOL> self . _saveeditor = self . git_editor <EOL> if self . _interactive == '<STR_LIT>' : <EOL> os . environ [ '<STR_LIT>' ] = self . _saveeditor <EOL> os . environ [ '<STR_LIT>' ] = editor <EOL> def _unset_editor ( self ) : <EOL> for var in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if os . environ . get ( '<STR_LIT>' + var , None ) : <EOL> del os . environ [ '<STR_LIT>' + var ] <EOL> if os . environ . get ( var , None ) : <EOL> os . environ [ var ] = self . _saveeditor <EOL> break <EOL> def run ( self , commits , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> todo_file = self . _write_todo ( commits , * args , ** kwargs ) <EOL> if self . _interactive : <EOL> user_editor = self . git_sequence_editor or self . git_editor <EOL> status = subprocess . call ( "<STR_LIT>" % ( user_editor , todo_file ) , <EOL> shell = True ) <EOL> if status : <EOL> return status , None , "<STR_LIT>" <EOL> editor = "<STR_LIT>" % ( self . editor , todo_file ) <EOL> self . _set_editor ( editor ) <EOL> try : <EOL> if self . _interactive == '<STR_LIT>' : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> cmd . extend ( self . git . transform_kwargs ( ** kwargs ) ) <EOL> cmd . extend ( args ) <EOL> return subprocess . call ( cmd ) , None , None <EOL> else : <EOL> return self . git . rebase ( interactive = True , with_exceptions = False , <EOL> with_extended_output = True , * args , <EOL> ** kwargs ) <EOL> finally : <EOL> os . remove ( todo_file ) <EOL> self . _unset_editor ( ) <EOL> @ property <EOL> def git_sequence_editor ( self ) : <EOL> return os . environ . get ( '<STR_LIT>' , <EOL> self . git . config ( "<STR_LIT>" , <EOL> with_exceptions = False ) ) <EOL> @ property <EOL> def git_editor ( self ) : <EOL> return os . environ . get ( "<STR_LIT>" , <EOL> self . git . var ( "<STR_LIT>" , <EOL> with_exceptions = False ) ) </s>
