<s> from __future__ import absolute_import <EOL> from __future__ import unicode_literals <EOL> import collections <EOL> import contextlib <EOL> import shutil <EOL> import subprocess <EOL> import tempfile <EOL> from git_code_debt . util . iter import chunk_iter <EOL> from git_code_debt . util . subprocess import cmd_output <EOL> Commit = collections . namedtuple ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT:date>' ] ) <EOL> COMMIT_FORMAT = '<STR_LIT>' <EOL> class RepoParser ( object ) : <EOL> def __init__ ( self , git_repo ) : <EOL> self . git_repo = git_repo <EOL> self . tempdir = None <EOL> @ contextlib . contextmanager <EOL> def repo_checked_out ( self ) : <EOL> assert not self . tempdir <EOL> self . tempdir = tempfile . mkdtemp ( suffix = '<STR_LIT>' ) <EOL> try : <EOL> subprocess . check_call ( <EOL> ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> self . git_repo , self . tempdir , <EOL> ) , <EOL> stdout = None , <EOL> ) <EOL> yield <EOL> finally : <EOL> shutil . rmtree ( self . tempdir ) <EOL> self . tempdir = None <EOL> def get_commit ( self , sha ) : <EOL> output = cmd_output ( <EOL> '<STR_LIT>' , '<STR_LIT>' , COMMIT_FORMAT , sha , <EOL> cwd = self . tempdir , <EOL> ) <EOL> sha , date = output . splitlines ( ) [ : <NUM_LIT:2> ] <EOL> return Commit ( sha , int ( date ) ) <EOL> def get_commits ( self , since_sha = None ) : <EOL> """<STR_LIT>""" <EOL> assert self . tempdir <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , COMMIT_FORMAT ] <EOL> if since_sha : <EOL> commits = [ self . get_commit ( since_sha ) ] <EOL> cmd . append ( '<STR_LIT>' . format ( since_sha ) ) <EOL> else : <EOL> commits = [ ] <EOL> cmd . append ( '<STR_LIT>' ) <EOL> output = cmd_output ( * cmd , cwd = self . tempdir ) <EOL> for sha , date in chunk_iter ( output . splitlines ( ) , <NUM_LIT:2> ) : <EOL> commits . append ( Commit ( sha , int ( date ) ) ) <EOL> return commits <EOL> def get_original_commit ( self , sha ) : <EOL> assert self . tempdir <EOL> output = cmd_output ( <EOL> '<STR_LIT>' , '<STR_LIT>' , sha , cwd = self . tempdir , encoding = None , <EOL> ) <EOL> return output <EOL> def get_commit_diff ( self , previous_sha , sha ) : <EOL> assert self . tempdir <EOL> output = cmd_output ( <EOL> '<STR_LIT>' , '<STR_LIT>' , previous_sha , sha , cwd = self . tempdir , encoding = None , <EOL> ) <EOL> return output </s>
