<s> from __future__ import print_function <EOL> import datetime <EOL> import docutils . core <EOL> import os <EOL> import re <EOL> from catkin_pkg . changelog_generator import FORTHCOMING_LABEL <EOL> def _replace_version ( package_str , new_version ) : <EOL> """<STR_LIT>""" <EOL> new_package_str , number_of_subs = re . subn ( '<STR_LIT>' , '<STR_LIT>' % new_version , package_str ) <EOL> if number_of_subs != <NUM_LIT:1> : <EOL> raise RuntimeError ( '<STR_LIT>' % ( number_of_subs ) ) <EOL> return new_package_str <EOL> def _check_for_version_comment ( package_str , new_version ) : <EOL> """<STR_LIT>""" <EOL> version_tag = '<STR_LIT>' % new_version <EOL> pattern = '<STR_LIT>' % ( re . escape ( version_tag ) , re . escape ( '<STR_LIT>' ) , re . escape ( '<STR_LIT>' ) ) <EOL> comment = re . search ( pattern , package_str ) <EOL> if comment : <EOL> comment = comment . group ( <NUM_LIT:1> ) <EOL> return comment <EOL> def update_versions ( paths , new_version ) : <EOL> """<STR_LIT>""" <EOL> files = { } <EOL> for path in paths : <EOL> package_path = os . path . join ( path , '<STR_LIT>' ) <EOL> with open ( package_path , '<STR_LIT:r>' ) as f : <EOL> package_str = f . read ( ) <EOL> try : <EOL> new_package_str = _replace_version ( package_str , new_version ) <EOL> comment = _check_for_version_comment ( new_package_str , new_version ) <EOL> if comment : <EOL> print ( '<STR_LIT>' % ( path , comment ) ) <EOL> except RuntimeError as rue : <EOL> raise RuntimeError ( '<STR_LIT>' % ( package_path , str ( rue ) ) ) <EOL> files [ package_path ] = new_package_str <EOL> for package_path , new_package_str in files . items ( ) : <EOL> with open ( package_path , '<STR_LIT:w>' ) as f : <EOL> f . write ( new_package_str ) <EOL> def get_forthcoming_label ( rst ) : <EOL> document = docutils . core . publish_doctree ( rst ) <EOL> forthcoming_label = None <EOL> for child in document . children : <EOL> title = None <EOL> if isinstance ( child , docutils . nodes . subtitle ) : <EOL> title = child <EOL> elif isinstance ( child , docutils . nodes . section ) : <EOL> section = child <EOL> if len ( section . children ) > <NUM_LIT:0> and isinstance ( section . children [ <NUM_LIT:0> ] , docutils . nodes . title ) : <EOL> title = section . children [ <NUM_LIT:0> ] <EOL> if title and len ( title . children ) > <NUM_LIT:0> and isinstance ( title . children [ <NUM_LIT:0> ] , docutils . nodes . Text ) : <EOL> title_text = title . children [ <NUM_LIT:0> ] . rawsource <EOL> if FORTHCOMING_LABEL . lower ( ) in title_text . lower ( ) : <EOL> if forthcoming_label : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> forthcoming_label = title_text <EOL> return forthcoming_label <EOL> def update_changelog_sections ( changelogs , new_version ) : <EOL> new_changelog_data = { } <EOL> new_label = '<STR_LIT>' % ( new_version , datetime . date . today ( ) . isoformat ( ) ) <EOL> for pkg_name , ( changelog_path , changelog , forthcoming_label ) in changelogs . items ( ) : <EOL> data = rename_section ( changelog . rst , forthcoming_label , new_label ) <EOL> new_changelog_data [ changelog_path ] = data <EOL> for changelog_path , data in new_changelog_data . items ( ) : <EOL> with open ( changelog_path , '<STR_LIT:w>' ) as f : <EOL> f . write ( data ) <EOL> def rename_section ( data , old_label , new_label ) : <EOL> valid_section_characters = '<STR_LIT>' <EOL> def replace_section ( match ) : <EOL> section_char = match . group ( <NUM_LIT:2> ) [ <NUM_LIT:0> ] <EOL> return new_label + '<STR_LIT:\n>' + section_char * len ( new_label ) <EOL> pattern = '<STR_LIT>' + re . escape ( old_label ) + '<STR_LIT>' + re . escape ( valid_section_characters ) + '<STR_LIT>' <EOL> data , count = re . subn ( pattern , replace_section , data , flags = re . MULTILINE ) <EOL> if count == <NUM_LIT:0> : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> if count > <NUM_LIT:1> : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> return data </s>
