def update_versions(version_map, target_file):

    newlines = []
    repl_open, repl_thisline, file_changed = False, False, False
    print('processing: ' + target_file)
    try:
        with open(target_file, encoding='utf-8') as f:
            for line in f:
                repl_thisline = repl_open
                match = version_update_marker.search(line)
                if match:
                    module_name, version_type = match.group(1), match.group(2)
                    repl_thisline = True
                else:	
                    match = version_update_start_marker.search(line)	
                    if match:	
                        module_name, version_type = match.group(1), match.group(2)	
                        repl_open, repl_thisline = True, True	
                    else:	
                        match = version_update_end_marker.search(line)	
                        if match:	
                            repl_open, repl_thisline = False, False
                
                if repl_thisline:
                    # If the module isn't found then just continue. This can
                    # happen if we're going through and replacing only library
                    # or only external dependency versions
                    if module_name not in version_map:
                        newlines.append(line)
                        continue
                    module = version_map[module_name]
                    new_version = ''
                    if version_type == 'current':
                        try:
                            new_version = module.current
                        except AttributeError:
                            raise ValueError('Module: {0} does not have a current version.\nFile={1}\nLine={2}'.format(module_name, target_file, line))
                    elif version_type == 'dependency':
                        new_version = module.dependency
                    else:
                        raise ValueError('Invalid version type: {} for module: {}.\nFile={}\nLine={}'.format(version_type, module_name, target_file, line))

                    newline = re.sub(version_regex_str_no_anchor, new_version, line)
                    newlines.append(newline)
                    file_changed = True
                else:
                    newlines.append(line)

                if not repl_open:
                    module_name, version_type = '', ''

        if file_changed:
            with open(target_file, 'w', encoding='utf-8') as f:
                for line in newlines:
                    f.write(line)
    except Exception as e:
        print("Unexpected exception: " + str(e))
Esempio n. 2
0
def update_versions(update_type, version_map, ext_dep_map, target_file, skip_readme, auto_version_increment, library_array):

    newlines = []
    repl_open, repl_thisline, file_changed, is_include = False, False, False, False
    print('processing: ' + target_file)
    try:
        with open(target_file, encoding='utf-8') as f:
            for line in f:
                is_include = False
                repl_thisline = repl_open
                match = version_update_marker.search(line)
                if match and not target_file.endswith('.md'):
                    module_name, version_type = match.group(1), match.group(2)
                    repl_thisline = True
                elif include_update_marker.search(line):
                    match = include_update_marker.search(line)
                    module_name, version_type = match.group(1), match.group(2)
                    repl_thisline = True
                    is_include = True
                else:
                    match = version_update_start_marker.search(line)
                    if match:
                        module_name, version_type = match.group(1), match.group(2)
                        # only update the version in the MD file if the module is in the list or the list is empty
                        if len(library_array) == 0 or module_name in library_array:
                            repl_open, repl_thisline = True, True
                        else:
                            repl_open, repl_thisline = False, False
                    else:
                        match = version_update_end_marker.search(line)
                        if match:
                            repl_open, repl_thisline = False, False

                if repl_thisline:
                    # If the module isn't found then just continue. This can happen if we're going through and updating
                    # library versions for one track and tag entry is for another track or if we're only updating
                    # external_dependency versions.
                    if module_name not in version_map and (version_type == 'current' or version_type == 'dependency'):
                        newlines.append(line)
                        continue
                    new_version = ''
                    if version_type == 'current':
                        try:
                            module = version_map[module_name]
                            new_version = module.current
                            newline = re.sub(version_regex_str_no_anchor, new_version, line)
                        except AttributeError:
                            # This can happen when a dependency is an unreleased_ or beta_ dependency and the tag is current instead of dependency
                            raise ValueError('Module: {0} does not have a current version.\nFile={1}\nLine={2}'.format(module_name, target_file, line))
                    elif version_type == 'dependency':
                        try:
                            module = version_map[module_name]
                            new_version = module.dependency
                            newline = re.sub(version_regex_str_no_anchor, new_version, line)
                        except AttributeError:
                            # This should never happen unless the version file is malformed
                            raise ValueError('Module: {0} does not have a dependency version.\nFile={1}\nLine={2}'.format(module_name, target_file, line))
                    elif version_type == 'external_dependency':
                        # The external dependency map will be empty if the update type is library
                        if update_type == UpdateType.library:
                            newlines.append(line)
                            continue
                        if is_include:
                            try:
                                module = ext_dep_map.get(module_name)
                                if module:
                                    new_include_version = module.string_for_allowlist_include()
                                    newline = re.sub(external_dependency_include_regex, new_include_version, line)
                            except AttributeError:
                                raise ValueError('Module: {0} does not have an external dependency version.\nFile={1}\nLine={2}'.format(module_name, target_file, line))
                        else:
                            try:
                                module = ext_dep_map.get(module_name)
                                if module:
                                    new_version = module.external_dependency
                                    newline = re.sub(external_dependency_version_regex, new_version, line)
                            except AttributeError:
                                raise ValueError('Module: {0} does not have an external dependency version.\nFile={1}\nLine={2}'.format(module_name, target_file, line))
                    else:
                        raise ValueError('Invalid version type: {} for module: {}.\nFile={}\nLine={}'.format(version_type, module_name, target_file, line))

                    newlines.append(newline)
                    if line != newline:
                        file_changed = True
                else:
                    newlines.append(line)

                if not repl_open:
                    module_name, version_type = '', ''

        if file_changed:
            with open(target_file, 'w', encoding='utf-8') as f:
                for line in newlines:
                    f.write(line)

            # If the pom file changed check and see if we need to add a version line to the Changelog
            file_name = os.path.basename(target_file)
            if ((auto_version_increment or not skip_readme) and (file_name.startswith('pom.') and file_name.endswith('.xml'))):
                update_changelog(target_file, auto_version_increment, library_array)

    except Exception as e:
        print("Unexpected exception: " + str(e))
        traceback.print_exc(file=sys.stderr)
Esempio n. 3
0
def update_versions(update_type, version_map, ext_dep_map, target_file):

    newlines = []
    repl_open, repl_thisline, file_changed = False, False, False
    print('processing: ' + target_file)
    try:
        with open(target_file, encoding='utf-8') as f:
            for line in f:
                repl_thisline = repl_open
                match = version_update_marker.search(line)
                if match and not target_file.endswith('.md'):
                    module_name, version_type = match.group(1), match.group(2)
                    repl_thisline = True
                else:
                    match = version_update_start_marker.search(line)
                    if match:
                        module_name, version_type = match.group(1), match.group(2)
                        repl_open, repl_thisline = True, True
                    else:
                        match = version_update_end_marker.search(line)
                        if match:
                            repl_open, repl_thisline = False, False

                if repl_thisline:
                    # If the module isn't found then just continue. This can happen if we're going through and updating
                    # library versions for one track and tag entry is for another track or if we're only updating 
                    # external_dependency versions.
                    if module_name not in version_map and (version_type == 'current' or version_type == 'dependency'):
                        newlines.append(line)
                        continue
                    new_version = ''
                    if version_type == 'current':
                        try:
                            module = version_map[module_name]
                            new_version = module.current
                            newline = re.sub(version_regex_str_no_anchor, new_version, line)
                        except AttributeError:
                            # This can happen when a dependency is an unreleased_ dependency and the tag is current instead of dependency
                            raise ValueError('Module: {0} does not have a current version.\nFile={1}\nLine={2}'.format(module_name, target_file, line))
                    elif version_type == 'dependency':
                        try:
                            module = version_map[module_name]
                            new_version = module.dependency
                            newline = re.sub(version_regex_str_no_anchor, new_version, line)
                        except AttributeError:
                            # This should never happen unless the version file is malformed
                            raise ValueError('Module: {0} does not have a dependency version.\nFile={1}\nLine={2}'.format(module_name, target_file, line))
                    elif version_type == 'external_dependency':
                        # The external dependency map will be empty if the update type is library
                        if update_type == UpdateType.library:
                            newlines.append(line)
                            continue
                        try:
                            module = ext_dep_map[module_name]
                            new_version = module.external_dependency
                            newline = re.sub(external_dependency_version_regex, new_version, line)
                        except AttributeError:
                            raise ValueError('Module: {0} does not have an external dependency version.\nFile={1}\nLine={2}'.format(module_name, target_file, line))
                    else:
                        raise ValueError('Invalid version type: {} for module: {}.\nFile={}\nLine={}'.format(version_type, module_name, target_file, line))

                    newlines.append(newline)
                    file_changed = True
                else:
                    newlines.append(line)

                if not repl_open:
                    module_name, version_type = '', ''

        if file_changed:
            with open(target_file, 'w', encoding='utf-8') as f:
                for line in newlines:
                    f.write(line)
    except Exception as e:
        print("Unexpected exception: " + str(e))
        traceback.print_exc(file=sys.stderr)