<s> from __future__ import division <EOL> """<STR_LIT>""" <EOL> __credits__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> import importlib <EOL> import sys <EOL> import os <EOL> import re <EOL> from datetime import datetime , date <EOL> from pyqi . util import pyqi_system_call <EOL> from pyqi . core . command import ( Command , CommandIn , CommandOut , <EOL> ParameterCollection ) <EOL> class MakeRelease ( Command ) : <EOL> BriefDescription = "<STR_LIT>" <EOL> LongDescription = "<STR_LIT>" <EOL> CommandIns = ParameterCollection ( [ <EOL> CommandIn ( Name = '<STR_LIT>' , DataType = str , <EOL> Description = '<STR_LIT>' , <EOL> Required = True ) , <EOL> CommandIn ( Name = '<STR_LIT>' , DataType = bool , <EOL> Description = '<STR_LIT>' , Required = False , <EOL> Default = False ) <EOL> ] ) <EOL> CommandOuts = ParameterCollection ( [ ] ) <EOL> RealRun = False <EOL> _date_clean_re = re . compile ( r'<STR_LIT>' ) <EOL> def _parse_changelog ( self , pkg_name ) : <EOL> with open ( '<STR_LIT>' ) as f : <EOL> lineiter = iter ( f ) <EOL> for line in lineiter : <EOL> match = re . search ( '<STR_LIT>' % pkg_name , line . strip ( ) ) <EOL> if match is None : <EOL> continue <EOL> length = len ( match . group ( <NUM_LIT:1> ) ) <EOL> version = match . group ( <NUM_LIT:1> ) . strip ( ) <EOL> if lineiter . next ( ) . count ( '<STR_LIT:->' ) != len ( match . group ( <NUM_LIT:0> ) ) : <EOL> continue <EOL> while <NUM_LIT:1> : <EOL> change_info = lineiter . next ( ) . strip ( ) <EOL> if change_info : <EOL> break <EOL> match = re . search ( r'<STR_LIT>' , <EOL> change_info ) <EOL> if match is None : <EOL> continue <EOL> datestr = match . group ( <NUM_LIT:1> ) <EOL> return version , self . _parse_date ( datestr ) <EOL> def _bump_version ( self , version ) : <EOL> try : <EOL> parts = map ( int , version . split ( '<STR_LIT:.>' ) ) <EOL> except ValueError : <EOL> self . _fail ( '<STR_LIT>' ) <EOL> parts [ - <NUM_LIT:1> ] += <NUM_LIT:1> <EOL> return '<STR_LIT:.>' . join ( map ( str , parts ) ) <EOL> def _parse_date ( self , string ) : <EOL> string = self . _date_clean_re . sub ( r'<STR_LIT>' , string ) <EOL> return datetime . strptime ( string , '<STR_LIT>' ) <EOL> def _set_filename_version ( self , filename , version_number , pattern ) : <EOL> changed = [ ] <EOL> def inject_version ( match ) : <EOL> before , old , after = match . groups ( ) <EOL> changed . append ( True ) <EOL> return before + version_number + after <EOL> with open ( filename ) as f : <EOL> contents = re . sub ( r"""<STR_LIT>""" % <EOL> pattern , inject_version , f . read ( ) ) <EOL> if not changed : <EOL> self . _fail ( '<STR_LIT>' , pattern , filename ) <EOL> if self . RealRun : <EOL> with open ( filename , '<STR_LIT:w>' ) as f : <EOL> f . write ( contents ) <EOL> def _set_init_version ( self , pkg_name , version ) : <EOL> self . _info ( '<STR_LIT>' , version ) <EOL> self . _set_filename_version ( '<STR_LIT>' % pkg_name , version , <EOL> '<STR_LIT>' ) <EOL> def _set_setup_version ( self , version ) : <EOL> self . _info ( '<STR_LIT>' , version ) <EOL> self . _set_filename_version ( '<STR_LIT>' , version , '<STR_LIT>' ) <EOL> def _set_doc_version ( self , version ) : <EOL> self . _info ( '<STR_LIT>' , version ) <EOL> self . _set_filename_version ( '<STR_LIT>' , version , '<STR_LIT>' ) <EOL> def _build_and_upload ( self ) : <EOL> cmd = [ sys . executable , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> stdout , stderr , retval = pyqi_system_call ( cmd , shell = False , <EOL> dry_run = not self . RealRun ) <EOL> if retval is not <NUM_LIT:0> : <EOL> self . _fail ( "<STR_LIT>" , <EOL> stdout , stderr ) <EOL> def _fail ( self , message , * args ) : <EOL> sys . stderr . write ( '<STR_LIT>' ) <EOL> sys . stderr . write ( message % args ) <EOL> sys . stderr . write ( '<STR_LIT:\n>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def _info ( self , message , * args ) : <EOL> sys . stderr . write ( message % args ) <EOL> sys . stderr . write ( '<STR_LIT:\n>' ) <EOL> def _get_git_tags ( self ) : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> stdout , stderr , retval = pyqi_system_call ( cmd , shell = False , <EOL> dry_run = not self . RealRun ) <EOL> if retval is not <NUM_LIT:0> : <EOL> self . _fail ( "<STR_LIT>" , <EOL> stdout , stderr ) <EOL> return stdout . splitlines ( ) <EOL> def _git_is_clean ( self ) : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> stdout , stderr , retval = pyqi_system_call ( cmd , shell = False ) <EOL> return retval == <NUM_LIT:0> <EOL> def _make_git_commit ( self , message , * args ) : <EOL> message = message % args <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , message ] <EOL> stdout , stderr , retval = pyqi_system_call ( cmd , shell = False , <EOL> dry_run = not self . RealRun ) <EOL> if retval is not <NUM_LIT:0> : <EOL> self . _fail ( "<STR_LIT>" , <EOL> stdout , stderr ) <EOL> def _make_git_tag ( self , tag ) : <EOL> self . _info ( '<STR_LIT>' , tag ) <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , tag ] <EOL> stdout , stderr , retval = pyqi_system_call ( cmd , shell = False , <EOL> dry_run = not self . RealRun ) <EOL> if retval is not <NUM_LIT:0> : <EOL> self . _fail ( "<STR_LIT>" , stdout , <EOL> stderr ) <EOL> def _get_git_branch ( self ) : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> stdout , stderr , retval = pyqi_system_call ( cmd , shell = False ) <EOL> if retval is not <NUM_LIT:0> : <EOL> self . _fail ( "<STR_LIT>" , <EOL> stdout , stderr ) <EOL> return stdout . strip ( ) <EOL> def _git_push_branch ( self ) : <EOL> branch = self . _get_git_branch ( ) <EOL> self . _info ( '<STR_LIT>' , branch ) <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , branch ] <EOL> stdout , stderr , retval = pyqi_system_call ( cmd , shell = False , <EOL> dry_run = not self . RealRun ) <EOL> if retval is not <NUM_LIT:0> : <EOL> self . _fail ( "<STR_LIT>" , <EOL> stdout , stderr , branch ) <EOL> def _git_push_tag ( self , tag ) : <EOL> self . _info ( '<STR_LIT>' , tag ) <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , tag ] <EOL> stdout , stderr , retval = pyqi_system_call ( cmd , shell = False , <EOL> dry_run = not self . RealRun ) <EOL> if retval is not <NUM_LIT:0> : <EOL> self . _fail ( "<STR_LIT>" , <EOL> stdout , stderr , tag ) <EOL> def run ( self , ** kwargs ) : <EOL> pkg_name = kwargs [ '<STR_LIT>' ] <EOL> self . RealRun = kwargs [ '<STR_LIT>' ] <EOL> try : <EOL> pkg_module = importlib . import_module ( pkg_name ) <EOL> except ImportError : <EOL> sys . stderr . write ( "<STR_LIT>" % pkg_name ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> os . chdir ( os . path . join ( os . path . dirname ( pkg_module . __file__ ) , '<STR_LIT:..>' ) ) <EOL> rv = self . _parse_changelog ( pkg_name ) <EOL> if rv is None : <EOL> self . _fail ( '<STR_LIT>' ) <EOL> version , release_date = rv <EOL> dev_version = version + '<STR_LIT>' <EOL> self . _info ( '<STR_LIT>' , <EOL> version , release_date . strftime ( '<STR_LIT>' ) ) <EOL> tags = self . _get_git_tags ( ) <EOL> if version in tags : <EOL> self . _fail ( '<STR_LIT>' , version ) <EOL> if release_date . date ( ) != date . today ( ) : <EOL> self . _fail ( '<STR_LIT>' , <EOL> release_date . strftime ( '<STR_LIT>' ) , <EOL> date . today ( ) ) <EOL> if not self . _git_is_clean ( ) : <EOL> self . _fail ( '<STR_LIT>' ) <EOL> self . _set_init_version ( pkg_name , version ) <EOL> self . _set_setup_version ( version ) <EOL> self . _set_doc_version ( version ) <EOL> self . _make_git_commit ( '<STR_LIT>' , version ) <EOL> self . _make_git_tag ( version ) <EOL> self . _build_and_upload ( ) <EOL> self . _set_init_version ( pkg_name , dev_version ) <EOL> self . _set_setup_version ( dev_version ) <EOL> self . _set_doc_version ( dev_version ) <EOL> self . _make_git_commit ( '<STR_LIT>' , dev_version ) <EOL> self . _git_push_branch ( ) <EOL> self . _git_push_tag ( version ) <EOL> return { } <EOL> CommandConstructor = MakeRelease </s>
