<s> import logging <EOL> import io <EOL> import re <EOL> from plumbum . cmd import git <EOL> log = logging . getLogger ( __name__ ) <EOL> def write_new_changelog ( repo_url , filename , content_lines , dry_run = True ) : <EOL> heading_and_newline = '<STR_LIT>' % repo_url <EOL> with io . open ( filename , '<STR_LIT>' ) as f : <EOL> existing = f . readlines ( ) <EOL> output = existing [ <NUM_LIT:2> : ] <EOL> output . insert ( <NUM_LIT:0> , '<STR_LIT:\n>' ) <EOL> for index , line in enumerate ( content_lines ) : <EOL> output . insert ( <NUM_LIT:0> , content_lines [ len ( content_lines ) - index - <NUM_LIT:1> ] ) <EOL> output . insert ( <NUM_LIT:0> , heading_and_newline ) <EOL> output = '<STR_LIT>' . join ( output ) <EOL> if not dry_run : <EOL> with io . open ( filename , '<STR_LIT>' ) as f : <EOL> f . write ( output ) <EOL> else : <EOL> log . info ( '<STR_LIT>' , '<STR_LIT>' . join ( content_lines ) ) <EOL> def replace_sha_with_commit_link ( repo_url , git_log_content ) : <EOL> git_log_content = git_log_content . split ( '<STR_LIT:\n>' ) <EOL> for index , line in enumerate ( git_log_content ) : <EOL> sha1_re = re . match ( r'<STR_LIT>' , line ) <EOL> if sha1_re : <EOL> sha1 = sha1_re . group ( ) <EOL> new_line = line . replace ( <EOL> sha1 , <EOL> '<STR_LIT>' % ( sha1 , repo_url , sha1 ) <EOL> ) <EOL> log . debug ( '<STR_LIT>' , line , new_line ) <EOL> git_log_content [ index ] = new_line <EOL> return git_log_content <EOL> def generate_changelog ( context ) : <EOL> """<STR_LIT>""" <EOL> changelog_content = [ <EOL> '<STR_LIT>' % ( <EOL> context . new_version , context . repo_url , <EOL> context . current_version , context . new_version , <EOL> ) <EOL> ] <EOL> git_log_content = None <EOL> git_log = '<STR_LIT>' . split ( '<STR_LIT:U+0020>' ) <EOL> try : <EOL> git_log_tag = git_log + [ '<STR_LIT>' % context . current_version ] <EOL> git_log_content = git ( git_log_tag ) <EOL> log . debug ( '<STR_LIT>' % git_log_content ) <EOL> except : <EOL> log . warn ( '<STR_LIT>' ) <EOL> git_log_content = git ( git_log ) <EOL> git_log_content = replace_sha_with_commit_link ( context . repo_url , git_log_content ) <EOL> if git_log_content : <EOL> [ <EOL> changelog_content . append ( '<STR_LIT>' % line ) <EOL> if line else line <EOL> for line in git_log_content [ : - <NUM_LIT:1> ] <EOL> ] <EOL> write_new_changelog ( <EOL> context . repo_url , <EOL> '<STR_LIT>' , <EOL> changelog_content , <EOL> dry_run = context . dry_run <EOL> ) <EOL> log . info ( '<STR_LIT>' ) <EOL> context . changelog_content = changelog_content </s>
