<commit_before># Copyright 2016-2019 <NAME>
# Copyright 2019 Open Source Robotics Foundation, Inc.
# Licensed under the Apache License, Version 2.0

import argparse
from collections import OrderedDict
import os
from pathlib import Path
import sys


FORMAT_STR_COMMENT_LINE = None
FORMAT_STR_SET_ENV_VAR = None
FORMAT_STR_USE_ENV_VAR = None
FORMAT_STR_INVOKE_SCRIPT = None

DSV_TYPE_PREPEND_NON_DUPLICATE = 'prepend-non-duplicate'
DSV_TYPE_PREPEND_NON_DUPLICATE_IF_EXISTS = 'prepend-non-duplicate-if-exists'
DSV_TYPE_SOURCE = 'source'


def main(argv=sys.argv[1:]):  # noqa: D103
    global FORMAT_STR_COMMENT_LINE
    global FORMAT_STR_SET_ENV_VAR
    global FORMAT_STR_USE_ENV_VAR
    global FORMAT_STR_INVOKE_SCRIPT

    parser = argparse.ArgumentParser(
        description='Output shell commands for the packages in topological '
                    'order')
    parser.add_argument(
        'primary_extension',
        help='The file extension of the primary shell')
    parser.add_argument(
        'additional_extension', nargs='?',
        help='The additional file extension to be considered')
    args = parser.parse_args(argv)

    if args.primary_extension == 'sh':
        FORMAT_STR_COMMENT_LINE = '# {comment}'
        FORMAT_STR_SET_ENV_VAR = 'export {name}="{value}"'
        FORMAT_STR_USE_ENV_VAR = '${name}'
        FORMAT_STR_INVOKE_SCRIPT = 'AMENT_CURRENT_PREFIX="{prefix}" ' \
            '_ament_prefix_sh_source_script "{script_path}"'
    elif args.primary_extension == 'bat':
        FORMAT_STR_COMMENT_LINE = ':: {comment}'
        FORMAT_STR_SET_ENV_VAR = 'set "{name}={value}"'
        FORMAT_STR_USE_ENV_VAR = '%{name}%'
        FORMAT_STR_INVOKE_SCRIPT = \
            'call:_ament_prefix_bat_call_script "{script_path}"'
    else:
        assert False, 'Unknown primary extension: ' + args.primary_extension

    packages = get_packages(Path(__file__).parent)

    ordered_packages = order_packages(packages)
    for pkg_name in ordered_packages:
        if _include_comments():
            print(
                FORMAT_STR_COMMENT_LINE.format_map(
                    {'comment': 'Package: ' + pkg_name}))
        prefix = os.path.abspath(os.path.dirname(__file__))
        for line in get_commands(
            pkg_name, prefix, args.primary_extension,
            args.additional_extension
        ):
            print(line)


def get_packages(prefix_path):
    """
    Find packages based on ament resource files created during installation.

    :param Path prefix_path: The install prefix path of all packages
    :returns: A mapping from the package name to the set of runtime
      dependencies
    :rtype: dict
    """
    packages = {}
    # since importing ament_index_python isn't feasible here the following
    # constant must match ament_index_python.constants.RESOURCE_INDEX_SUBFOLDER
    subdirectory = 'share/ament_index/resource_index/packages'
    # return if workspace is empty
    if not (prefix_path / subdirectory).is_dir():
        return packages
    # find all files in the subdirectory
    for p in (prefix_path / subdirectory).iterdir():
        if not p.is_file():
            continue
        if p.name.startswith('.'):
            continue
        add_package_runtime_dependencies(p, packages)

    # remove unknown dependencies
    pkg_names = set(packages.keys())
    for k in packages.keys():
        packages[k] = {d for d in packages[k] if d in pkg_names}

    return packages


def add_package_runtime_dependencies(path, packages):
    """
    Check the path and if it exists extract the packages runtime dependencies.

    :param Path path: The resource file containing the runtime dependencies
    :param dict packages: A mapping from package names to the sets of runtime
      dependencies to add to
    """
    dependencies = set()
    marker_file = path.parents[1] / 'package_run_dependencies' / path.name
    if marker_file.exists():
        content = marker_file.read_text()
        dependencies = set(content.split(os.pathsep) if content else [])
    packages[marker_file.name] = dependencies


def order_packages(packages):
    """
    Order packages topologically.

    :param dict packages: A mapping from package name to the set of runtime
      dependencies
    :returns: The package names
    :rtype: list
    """
    # select packages with no dependencies in alphabetical order
    to_be_ordered = list(packages.keys())
    ordered = []
    while to_be_ordered:
        pkg_names_without_deps = [
            name for name in to_be_ordered if not packages[name]]
        if not pkg_names_without_deps:
            reduce_cycle_set(packages)
            raise RuntimeError(
                'Circular dependency between: ' + ', '.join(sorted(packages)))<commit_msg>fix reading the package run dependencies (#93)<commit_after># Copyright 2016-2019 <NAME>
# Copyright 2019 Open Source Robotics Foundation, Inc.
# Licensed under the Apache License, Version 2.0

import argparse
from collections import OrderedDict
import os
from pathlib import Path
import sys


FORMAT_STR_COMMENT_LINE = None
FORMAT_STR_SET_ENV_VAR = None
FORMAT_STR_USE_ENV_VAR = None
FORMAT_STR_INVOKE_SCRIPT = None

DSV_TYPE_PREPEND_NON_DUPLICATE = 'prepend-non-duplicate'
DSV_TYPE_PREPEND_NON_DUPLICATE_IF_EXISTS = 'prepend-non-duplicate-if-exists'
DSV_TYPE_SOURCE = 'source'


def main(argv=sys.argv[1:]):  # noqa: D103
    global FORMAT_STR_COMMENT_LINE
    global FORMAT_STR_SET_ENV_VAR
    global FORMAT_STR_USE_ENV_VAR
    global FORMAT_STR_INVOKE_SCRIPT

    parser = argparse.ArgumentParser(
        description='Output shell commands for the packages in topological '
                    'order')
    parser.add_argument(
        'primary_extension',
        help='The file extension of the primary shell')
    parser.add_argument(
        'additional_extension', nargs='?',
        help='The additional file extension to be considered')
    args = parser.parse_args(argv)

    if args.primary_extension == 'sh':
        FORMAT_STR_COMMENT_LINE = '# {comment}'
        FORMAT_STR_SET_ENV_VAR = 'export {name}="{value}"'
        FORMAT_STR_USE_ENV_VAR = '${name}'
        FORMAT_STR_INVOKE_SCRIPT = 'AMENT_CURRENT_PREFIX="{prefix}" ' \
            '_ament_prefix_sh_source_script "{script_path}"'
    elif args.primary_extension == 'bat':
        FORMAT_STR_COMMENT_LINE = ':: {comment}'
        FORMAT_STR_SET_ENV_VAR = 'set "{name}={value}"'
        FORMAT_STR_USE_ENV_VAR = '%{name}%'
        FORMAT_STR_INVOKE_SCRIPT = \
            'call:_ament_prefix_bat_call_script "{script_path}"'
    else:
        assert False, 'Unknown primary extension: ' + args.primary_extension

    packages = get_packages(Path(__file__).parent)

    ordered_packages = order_packages(packages)
    for pkg_name in ordered_packages:
        if _include_comments():
            print(
                FORMAT_STR_COMMENT_LINE.format_map(
                    {'comment': 'Package: ' + pkg_name}))
        prefix = os.path.abspath(os.path.dirname(__file__))
        for line in get_commands(
            pkg_name, prefix, args.primary_extension,
            args.additional_extension
        ):
            print(line)


def get_packages(prefix_path):
    """
    Find packages based on ament resource files created during installation.

    :param Path prefix_path: The install prefix path of all packages
    :returns: A mapping from the package name to the set of runtime
      dependencies
    :rtype: dict
    """
    packages = {}
    # since importing ament_index_python isn't feasible here the following
    # constant must match ament_index_python.constants.RESOURCE_INDEX_SUBFOLDER
    subdirectory = 'share/ament_index/resource_index/packages'
    # return if workspace is empty
    if not (prefix_path / subdirectory).is_dir():
        return packages
    # find all files in the subdirectory
    for p in (prefix_path / subdirectory).iterdir():
        if not p.is_file():
            continue
        if p.name.startswith('.'):
            continue
        add_package_runtime_dependencies(p, packages)

    # remove unknown dependencies
    pkg_names = set(packages.keys())
    for k in packages.keys():
        packages[k] = {d for d in packages[k] if d in pkg_names}

    return packages


def add_package_runtime_dependencies(path, packages):
    """
    Check the path and if it exists extract the packages runtime dependencies.

    :param Path path: The resource file containing the runtime dependencies
    :param dict packages: A mapping from package names to the sets of runtime
      dependencies to add to
    """
    dependencies = set()
    marker_file = path.parents[1] / 'package_run_dependencies' / path.name
    if marker_file.exists():
        content = marker_file.read_text()
        dependencies = set(content.split(';') if content else [])
    packages[marker_file.name] = dependencies


def order_packages(packages):
    """
    Order packages topologically.

    :param dict packages: A mapping from package name to the set of runtime
      dependencies
    :returns: The package names
    :rtype: list
    """
    # select packages with no dependencies in alphabetical order
    to_be_ordered = list(packages.keys())
    ordered = []
    while to_be_ordered:
        pkg_names_without_deps = [
            name for name in to_be_ordered if not packages[name]]
        if not pkg_names_without_deps:
            reduce_cycle_set(packages)
            raise RuntimeError(
                'Circular dependency between: ' + ', '.join(sorted(packages)))
{'fraction_non_alphanumeric': 0.08217884130982368, 'fraction_numerical': 0.0041981528127623844, 'mean_word_length': 3.096732588134136, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2765040', 'n_tokens_mistral': 2826, 'n_tokens_neox': 2692, 'n_words': 745}