<commit_before>#------------------------------------------------------------------------------
# Copyright (c) 2008, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD license
# available at http://www.enthought.com/licenses/BSD.txt and may be
# redistributed only under the conditions described in the aforementioned
# license.
#
# <NAME>
#------------------------------------------------------------------------------

# Enstaller imports.
from pkg_resources import compose_version_string, parse_version


def get_upgrade_str(name, version, upgrade=True):
    """
    Given a package name and version, return a requirement string in
    pkg_resources.Requirement format that can be used to retrieve
    an upgrade to that particular package version.  By default,
    the string returned would be an upgrade(i.e. change in major/minor
    version number), but if 'upgrade' is set to False, it will return a string
    that would constitute an update(i.e. change in patch/build level version
    number).
    """
    
    # Split up the version string into a list so we can determine
    # upgrades in the major/minor version parts.  Also check to see
    # if the package has our build number tag.
    if version[-2:] == "_s" or version[-2:] == "-s":
        version = version[:-2]

    # Find out the length of the version string as split by '.' and the length of what we consider
    # the major part of the version.
    version_len = len(version.split('.'))
    major_len = len(version.split('.')) // 2
    
    # Retrieve the version_parts from pkg_resources.parse_version.
    # Separate the major part from the version_parts, which signifies
    # the difference between the parts of the version that represent major/minor level upgrades
    # and patch/build level updates.  Also retrieve the last entry of the major part
    # so that we can increment it to determine our upgrade version number.
    version_parts = parse_version(version)
    major_part = version_parts[:major_len]
    end_part = major_part[-1]
    
    # Convert the end of the major part to an integer or it's ASCII code and then
    # increment it and convert it back to a string to be placed back in the major part
    # of the version.
    try:
        end_part = int(end_part)
    except:
        end_part = ord(end_part[-1])
    end_part = str(end_part+1)
    
    # If there is only one item in the major_part tuple, then the upgrade version is just
    # the end_part, which is the major_part incremented.  Otherwise, we need to convert the
    # major_part tuple into a list so that we can remove its last entry and then append our
    # incremented end_part and retrieve an upgrade version from that.
    if len(major_part) == 1:
        upgrade_version = end_part
    else:
        major_parts = list(major_part[:-1])
        major_parts.append(end_part)
        major_parts = tuple(major_parts)
        upgrade_version = compose_version_string(major_parts)
        
    # Calculate the requirement string based on whether we are doing an upgrade
    # or an update.
    if upgrade:
        req_str = "%s>=%s" % (name, upgrade_version)
    else:
        req_str = "%s>%s, <%s" % (name, version, upgrade_version)
    <commit_msg>Modifying upgrade algorithm to correctly generate the requirement string if the egg has a post-install flag.<commit_after>#------------------------------------------------------------------------------
# Copyright (c) 2008, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD license
# available at http://www.enthought.com/licenses/BSD.txt and may be
# redistributed only under the conditions described in the aforementioned
# license.
#
# <NAME>
#------------------------------------------------------------------------------

# Enstaller imports.
from pkg_resources import compose_version_string, parse_version


def get_upgrade_str(name, version, upgrade=True):
    """
    Given a package name and version, return a requirement string in
    pkg_resources.Requirement format that can be used to retrieve
    an upgrade to that particular package version.  By default,
    the string returned would be an upgrade(i.e. change in major/minor
    version number), but if 'upgrade' is set to False, it will return a string
    that would constitute an update(i.e. change in patch/build level version
    number).
    """
    
    # Split up the version string into a list so we can determine
    # upgrades in the major/minor version parts.  Also check to see
    # if the package has our build number tag.
    orig_version = version
    if version[-2:] == "-s":
        version = version[:-2]

    # Find out the length of the version string as split by '.' and the length of what we consider
    # the major part of the version.
    version_len = len(version.split('.'))
    major_len = len(version.split('.')) // 2
    
    # Retrieve the version_parts from pkg_resources.parse_version.
    # Separate the major part from the version_parts, which signifies
    # the difference between the parts of the version that represent major/minor level upgrades
    # and patch/build level updates.  Also retrieve the last entry of the major part
    # so that we can increment it to determine our upgrade version number.
    version_parts = parse_version(version)
    major_part = version_parts[:major_len]
    end_part = major_part[-1]
    
    # Convert the end of the major part to an integer or it's ASCII code and then
    # increment it and convert it back to a string to be placed back in the major part
    # of the version.
    try:
        end_part = int(end_part)
    except:
        end_part = ord(end_part[-1])
    end_part = str(end_part+1)
    
    # If there is only one item in the major_part tuple, then the upgrade version is just
    # the end_part, which is the major_part incremented.  Otherwise, we need to convert the
    # major_part tuple into a list so that we can remove its last entry and then append our
    # incremented end_part and retrieve an upgrade version from that.
    if len(major_part) == 1:
        upgrade_version = end_part
    else:
        major_parts = list(major_part[:-1])
        major_parts.append(end_part)
        major_parts = tuple(major_parts)
        upgrade_version = compose_version_string(major_parts)
        
    # Calculate the requirement string based on whether we are doing an upgrade
    # or an update.
    if upgrade:
        req_str = "%s>=%s" % (name, upgrade_version)
    else:
        req_str = "%s>%s, <%s" % (name, orig_version, upgrade_version)
    
{'fraction_non_alphanumeric': 0.11368073284276918, 'fraction_numerical': 0.0037543174650848474, 'mean_word_length': 2.572961373390558, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7350339', 'n_tokens_mistral': 1796, 'n_tokens_neox': 1732, 'n_words': 770}