def update_version_number(ctx, bump=None, ignore_prerelease=False):
    """
    Update version number.

    Args:
        bump (str): bump level
        ignore_prerelease (bool): ignore the fact that our new version is a
            prerelease, or issue a warning

    Returns a two semantic_version objects (the old version and the current
    version).
    """
    if bump is not None and bump.lower() not in VALID_BUMPS:
        print(textwrap.fill("[{}WARN{}] bump level, as provided on command "
                            "line, is invalid. Trying value given in "
                            "configuration file. Valid values are {}."
                            .format(WARNING_COLOR, RESET_COLOR,
                                    VALID_BUMPS_STR),
                            width=text.get_terminal_size().columns - 1,
                            subsequent_indent=' '*7))
        bump = None
    else:
        update_level = bump

    # Find current version
    temp_file = Path(ctx.releaser.version).resolve().parent / ("~" + Path(ctx.releaser.version).name)
    with temp_file.open(mode='w', encoding='utf-8') as g:
        with Path(ctx.releaser.version).resolve().open(mode='r', encoding='utf-8') as f:
            for line in f:
                version_matches = bare_version_re.match(line)
                if version_matches:
                    bare_version_str = version_matches.groups(0)[0]
                    if semantic_version.validate(bare_version_str):
                        old_version = Version(bare_version_str)
                        print("{}Current version is {}".format(" "*4,
                                                               old_version))
                    else:
                        old_version = Version.coerce(bare_version_str)
                        ans = text.query_yes_quit("{}I think the version is {}."
                                                   " Use it?".format(" "*4,
                                                                     old_version),
                                                   default="yes")
                        if ans == text.Answers.QUIT:
                            exit('[{}ERROR{}] Please set an initial version '
                                 'number to continue.'.format(ERROR_COLOR,
                                                              RESET_COLOR))

                    # if bump level not defined by command line options
                    if bump is None:
                        try:
                            update_level = ctx.releaser.version_bump
                        except AttributeError:
                            print("[{}WARN{}] bump level not defined in "
                                  "configuration. Use key "
                                  "'releaser.version_bump'"
                                  .format(WARNING_COLOR, RESET_COLOR))
                            print(textwrap.fill("{}Valid bump levels are: "
                                                "{}. Or use 'quit' to exit."
                                                .format(" "*7,
                                                        VALID_BUMPS_STR),
                                                width=text.get_terminal_size().columns - 1,
                                                subsequent_indent=' '*7))
                            my_input = input("What bump level to use? ")
                            if my_input.lower() in ["quit", "q", "exit", "y"]:
                                sys.exit(0)
                            elif my_input.lower() not in VALID_BUMPS:
                                exit("[{}ERROR{}] invalid bump level provided. "
                                     "Exiting...".format(ERROR_COLOR, RESET_COLOR))
                            else:
                                update_level = my_input

                    # Determine new version number
                    if update_level is None or update_level.lower() in ['none']:
                        update_level = None
                    elif update_level is not None:
                        update_level = update_level.lower()

                    if update_level in ['breaking', 'major']:
                        current_version = old_version.next_major()
                    elif update_level in ['feature', 'minor']:
                        current_version = old_version.next_minor()
                    elif update_level in ['bugfix', 'patch']:
                        current_version = old_version.next_patch()
                    elif update_level in ['dev', 'development', 'prerelease']:
                        if not old_version.prerelease:
                            current_version = old_version.next_patch()
                            current_version.prerelease = ('dev', )
                        else:
                            current_version = old_version
                    elif update_level is None:
                        # don't update version
                        current_version = old_version
                    else:
                        exit('[{}ERROR{}] Cannot update version in {} mode'
                             .format(ERROR_COLOR, RESET_COLOR, update_level))

                    # warn on pre-release versions
                    if current_version.prerelease and not ignore_prerelease:
                        ans = text.query_yes_quit("[{}WARN{}] Current version "
                                                  "is a pre-release version. "
                                                  "Continue anyway?"
                                                  .format(WARNING_COLOR,
                                                          RESET_COLOR),
                                                  default="quit")
                        if ans == text.Answers.QUIT:
                            sys.exit(1)

                    print("{}New version is     {}".format(" "*4,
                                                           current_version))

                    # Update version number
                    line = '__version__ = "{}"\n'.format(current_version)
                print(line, file=g, end="")
        #print('', file=g)  # add a blank line at the end of the file
    shutil.copyfile(str(temp_file), str(Path(ctx.releaser.version).resolve()))
    os.remove(str(temp_file))
    return(old_version, current_version)
def update_version_number ( ctx , bump = None , ignore_prerelease = False ) : if bump is not None and bump . lower ( ) not in VALID_BUMPS : print ( textwrap . fill ( "[{}WARN{}] bump level, as provided on command " "line, is invalid. Trying value given in " "configuration file. Valid values are {}." . format ( WARNING_COLOR , RESET_COLOR , VALID_BUMPS_STR ) , width = text . get_terminal_size ( ) . columns - 1 , subsequent_indent = ' ' * 7 ) ) bump = None else : update_level = bump temp_file = Path ( ctx . releaser . version ) . resolve ( ) . parent / ( "~" + Path ( ctx . releaser . version ) . name ) with temp_file . open ( mode = 'w' , encoding = 'utf-8' ) as g : with Path ( ctx . releaser . version ) . resolve ( ) . open ( mode = 'r' , encoding = 'utf-8' ) as f : for line in f : version_matches = bare_version_re . match ( line ) if version_matches : bare_version_str = version_matches . groups ( 0 ) [ 0 ] if semantic_version . validate ( bare_version_str ) : old_version = Version ( bare_version_str ) print ( "{}Current version is {}" . format ( " " * 4 , old_version ) ) else : old_version = Version . coerce ( bare_version_str ) ans = text . query_yes_quit ( "{}I think the version is {}." " Use it?" . format ( " " * 4 , old_version ) , default = "yes" ) if ans == text . Answers . QUIT : exit ( '[{}ERROR{}] Please set an initial version ' 'number to continue.' . format ( ERROR_COLOR , RESET_COLOR ) ) if bump is None : try : update_level = ctx . releaser . version_bump except AttributeError : print ( "[{}WARN{}] bump level not defined in " "configuration. Use key " "'releaser.version_bump'" . format ( WARNING_COLOR , RESET_COLOR ) ) print ( textwrap . fill ( "{}Valid bump levels are: " "{}. Or use 'quit' to exit." . format ( " " * 7 , VALID_BUMPS_STR ) , width = text . get_terminal_size ( ) . columns - 1 , subsequent_indent = ' ' * 7 ) ) my_input = input ( "What bump level to use? " ) if my_input . lower ( ) in [ "quit" , "q" , "exit" , "y" ] : sys . exit ( 0 ) elif my_input . lower ( ) not in VALID_BUMPS : exit ( "[{}ERROR{}] invalid bump level provided. " "Exiting..." . format ( ERROR_COLOR , RESET_COLOR ) ) else : update_level = my_input if update_level is None or update_level . lower ( ) in [ 'none' ] : update_level = None elif update_level is not None : update_level = update_level . lower ( ) if update_level in [ 'breaking' , 'major' ] : current_version = old_version . next_major ( ) elif update_level in [ 'feature' , 'minor' ] : current_version = old_version . next_minor ( ) elif update_level in [ 'bugfix' , 'patch' ] : current_version = old_version . next_patch ( ) elif update_level in [ 'dev' , 'development' , 'prerelease' ] : if not old_version . prerelease : current_version = old_version . next_patch ( ) current_version . prerelease = ( 'dev' , ) else : current_version = old_version elif update_level is None : current_version = old_version else : exit ( '[{}ERROR{}] Cannot update version in {} mode' . format ( ERROR_COLOR , RESET_COLOR , update_level ) ) if current_version . prerelease and not ignore_prerelease : ans = text . query_yes_quit ( "[{}WARN{}] Current version " "is a pre-release version. " "Continue anyway?" . format ( WARNING_COLOR , RESET_COLOR ) , default = "quit" ) if ans == text . Answers . QUIT : sys . exit ( 1 ) print ( "{}New version is     {}" . format ( " " * 4 , current_version ) ) line = '__version__ = "{}"\n' . format ( current_version ) print ( line , file = g , end = "" ) shutil . copyfile ( str ( temp_file ) , str ( Path ( ctx . releaser . version ) . resolve ( ) ) ) os . remove ( str ( temp_file ) ) return ( old_version , current_version )
Update version number.

    Args:
        bump (str): bump level
        ignore_prerelease (bool): ignore the fact that our new version is a
            prerelease, or issue a warning

    Returns a two semantic_version objects (the old version and the current
    version).