# Path: ros_buildfarm/argument.py
# def add_argument_os_code_name(parser):
#     parser.add_argument(
#         'os_code_name',
#         help='An OS code name from the build file')
# 
# Path: ros_buildfarm/argument.py
# def add_argument_os_name(parser):
#     parser.add_argument(
#         'os_name',
#         help='An OS name from the build file')
# 
# Path: ros_buildfarm/argument.py
# def add_argument_output_dir(parser, required=False):
#     parser.add_argument(
#         '--output-dir',
#         default=os.curdir,
#         required=required,
#         help='The output directory')
# 
# Path: ros_buildfarm/argument.py
# def add_argument_package_selection_args(parser):
#     return parser.add_argument(
#         '--package-selection-args', nargs=argparse.REMAINDER,
#         help='Package selection arguments passed to colcon '
#              'to specify which packages should be built and tested.')
# 
# Path: ros_buildfarm/argument.py
# def add_argument_rosdistro_name(parser, action=None):
#     parser.add_argument(
#         'rosdistro_name',
#         action=action,
#         help="The name of the ROS distro from the index")
# 
# Path: ros_buildfarm/argument.py
# def add_argument_skip_rosdep_keys(parser):
#     parser.add_argument(
#         '--skip-rosdep-keys', nargs='*',
#         help='The specified rosdep keys will be ignored, i.e. not resolved '
#              'and not installed.')
# 
# Path: ros_buildfarm/colcon.py
# def locate_packages(
#     source_space, packages_select=None, packages_up_to=None,
#     packages_above_depth=None, extra_args=None
# ):
#     cmd = ['colcon', 'list', '--base-paths', source_space]
#     if packages_select:
#         cmd.append('--packages-select')
#         cmd.extend(packages_select)
#     if packages_up_to:
#         cmd.append('--packages-up-to')
#         cmd.extend(packages_up_to)
#     if packages_above_depth:
#         cmd.append('--packages-above-depth')
#         cmd.extend(packages_above_depth)
#     if extra_args:
#         cmd.extend(extra_args)
# 
#     output = subprocess.check_output(cmd).decode()
# 
#     packages = {}
#     for line in output.splitlines():
#         # Format is: NAME PATH (TYPE)
#         name_path = line.rsplit(None, 1)[0]
#         name, path = name_path.split(None, 1)
#         packages[name] = path
# 
#     return packages
# 
# Path: ros_buildfarm/common.py
# def get_binary_package_versions(apt_cache, debian_pkg_names):
#     versions = {}
#     for debian_pkg_name in debian_pkg_names:
#         pkg = apt_cache[debian_pkg_name]
#         versions[debian_pkg_name] = max(pkg.versions).version
#     return versions
# 
# Path: ros_buildfarm/common.py
# class Scope(object):
# 
#     def __init__(self, scope_name, description):  # noqa: D107
#         global next_scope_id
#         self.scope_name = scope_name
#         self.description = description
#         self.scope_id = next_scope_id
#         next_scope_id += 1
# 
#     def __enter__(self):
#         if os.environ.get('TRAVIS') == 'true':
#             print('travis_fold:start:scope%d' % self.scope_id)
#         print('# BEGIN %s: %s' % (self.scope_name, self.description))
# 
#     def __exit__(self, type, value, traceback):
#         print('# END %s' % self.scope_name)
#         if os.environ.get('TRAVIS') == 'true':
#             print('travis_fold:end:scope%d' % self.scope_id)


import argparse
import os
import sys
from pathlib import Path
from apt import Cache
from catkin_pkg.packages import find_packages
from ros_buildfarm.argument import add_argument_os_code_name
from ros_buildfarm.argument import add_argument_os_name
from ros_buildfarm.argument import add_argument_output_dir
from ros_buildfarm.argument import add_argument_package_selection_args
from ros_buildfarm.argument import add_argument_rosdistro_name
from ros_buildfarm.argument import add_argument_skip_rosdep_keys
from ros_buildfarm.colcon import locate_packages
from ros_buildfarm.common import get_binary_package_versions
from ros_buildfarm.common import Scope
from rosdep2 import create_default_installer_context
from rosdep2.catkin_support import get_catkin_view
from rosdep2.catkin_support import resolve_for_os

def main(argv=sys.argv[1:]):
    parser = argparse.ArgumentParser(
        description='Lists available binary packages and versions which are'
                    'needed to satisfy rosdep keys for ROS packages in the workspace')

    # Positional
    add_argument_rosdistro_name(parser)
    add_argument_os_name(parser)
    add_argument_os_code_name(parser)

    add_argument_output_dir(parser)
    add_argument_package_selection_args(parser)
    add_argument_skip_rosdep_keys(parser)
    parser.add_argument(
        '--package-root',
        nargs='+',
        help='The path to the directory containing packages')
    args = parser.parse_args(argv)

    workspace_root = args.package_root[-1]
    os.chdir(workspace_root)

    with Scope('SUBSECTION', 'mark packages with IGNORE files'):
        all_packages = locate_packages(workspace_root)
        selected_packages = all_packages
        if args.package_selection_args:
            print(
                'Using package selection arguments:',
                args.package_selection_args)

# Path: ros_buildfarm/argument.py
# def add_argument_os_code_name(parser):
#     parser.add_argument(
#         'os_code_name',
#         help='An OS code name from the build file')
# 
# Path: ros_buildfarm/argument.py
# def add_argument_os_name(parser):
#     parser.add_argument(
#         'os_name',
#         help='An OS name from the build file')
# 
# Path: ros_buildfarm/argument.py
# def add_argument_output_dir(parser, required=False):
#     parser.add_argument(
#         '--output-dir',
#         default=os.curdir,
#         required=required,
#         help='The output directory')
# 
# Path: ros_buildfarm/argument.py
# def add_argument_package_selection_args(parser):
#     return parser.add_argument(
#         '--package-selection-args', nargs=argparse.REMAINDER,
#         help='Package selection arguments passed to colcon '
#              'to specify which packages should be built and tested.')
# 
# Path: ros_buildfarm/argument.py
# def add_argument_rosdistro_name(parser, action=None):
#     parser.add_argument(
#         'rosdistro_name',
#         action=action,
#         help="The name of the ROS distro from the index")
# 
# Path: ros_buildfarm/argument.py
# def add_argument_skip_rosdep_keys(parser):
#     parser.add_argument(
#         '--skip-rosdep-keys', nargs='*',
#         help='The specified rosdep keys will be ignored, i.e. not resolved '
#              'and not installed.')
# 
# Path: ros_buildfarm/colcon.py
# def locate_packages(
#     source_space, packages_select=None, packages_up_to=None,
#     packages_above_depth=None, extra_args=None
# ):
#     cmd = ['colcon', 'list', '--base-paths', source_space]
#     if packages_select:
#         cmd.append('--packages-select')
#         cmd.extend(packages_select)
#     if packages_up_to:
#         cmd.append('--packages-up-to')
#         cmd.extend(packages_up_to)
#     if packages_above_depth:
#         cmd.append('--packages-above-depth')
#         cmd.extend(packages_above_depth)
#     if extra_args:
#         cmd.extend(extra_args)
# 
#     output = subprocess.check_output(cmd).decode()
# 
#     packages = {}
#     for line in output.splitlines():
#         # Format is: NAME PATH (TYPE)
#         name_path = line.rsplit(None, 1)[0]
#         name, path = name_path.split(None, 1)
#         packages[name] = path
# 
#     return packages
# 
# Path: ros_buildfarm/common.py
# def get_binary_package_versions(apt_cache, debian_pkg_names):
#     versions = {}
#     for debian_pkg_name in debian_pkg_names:
#         pkg = apt_cache[debian_pkg_name]
#         versions[debian_pkg_name] = max(pkg.versions).version
#     return versions
# 
# Path: ros_buildfarm/common.py
# class Scope(object):
# 
#     def __init__(self, scope_name, description):  # noqa: D107
#         global next_scope_id
#         self.scope_name = scope_name
#         self.description = description
#         self.scope_id = next_scope_id
#         next_scope_id += 1
# 
#     def __enter__(self):
#         if os.environ.get('TRAVIS') == 'true':
#             print('travis_fold:start:scope%d' % self.scope_id)
#         print('# BEGIN %s: %s' % (self.scope_name, self.description))
# 
#     def __exit__(self, type, value, traceback):
#         print('# END %s' % self.scope_name)
#         if os.environ.get('TRAVIS') == 'true':
#             print('travis_fold:end:scope%d' % self.scope_id)


# Path: scripts/ci/generate_install_lists.py
import argparse
import os
import sys
from pathlib import Path
from apt import Cache
from catkin_pkg.packages import find_packages
from ros_buildfarm.argument import add_argument_os_code_name
from ros_buildfarm.argument import add_argument_os_name
from ros_buildfarm.argument import add_argument_output_dir
from ros_buildfarm.argument import add_argument_package_selection_args
from ros_buildfarm.argument import add_argument_rosdistro_name
from ros_buildfarm.argument import add_argument_skip_rosdep_keys
from ros_buildfarm.colcon import locate_packages
from ros_buildfarm.common import get_binary_package_versions
from ros_buildfarm.common import Scope
from rosdep2 import create_default_installer_context
from rosdep2.catkin_support import get_catkin_view
from rosdep2.catkin_support import resolve_for_os


def main(argv=sys.argv[1:]):
    parser = argparse.ArgumentParser(
        description='Lists available binary packages and versions which are'
                    'needed to satisfy rosdep keys for ROS packages in the workspace')

    # Positional
    add_argument_rosdistro_name(parser)
    add_argument_os_name(parser)
    add_argument_os_code_name(parser)

    add_argument_output_dir(parser)
    add_argument_package_selection_args(parser)
    add_argument_skip_rosdep_keys(parser)
    parser.add_argument(
        '--package-root',
        nargs='+',
        help='The path to the directory containing packages')
    args = parser.parse_args(argv)

    workspace_root = args.package_root[-1]
    os.chdir(workspace_root)

    with Scope('SUBSECTION', 'mark packages with IGNORE files'):
        all_packages = locate_packages(workspace_root)
        selected_packages = all_packages
        if args.package_selection_args:
            print(
                'Using package selection arguments:',
                args.package_selection_args)

            selected_packages = locate_packages(