# Path: ros_buildfarm/argument.py
# def add_argument_package_dependencies(parser):
#     parser.add_argument(
#         '--package-dependencies',
#         action='store_true',
#         help='Also include recursive dependencies of packages specified in '
#              '--package-names.')
# 
# Path: ros_buildfarm/argument.py
# def add_argument_package_names(parser, optional=False):
#     parser.add_argument(
#         '--package-names',
#         nargs='*' if optional else '+',
#         help='A space separated list of package names')
# 
# Path: ros_buildfarm/argument.py
# def add_argument_repos_file_urls(parser):
#     parser.add_argument(
#         '--repos-file-urls', nargs='*', metavar='URL',
#         help='URLs of repos files to import with vcs.')
# 
# Path: ros_buildfarm/argument.py
# def add_argument_repository_names(parser, optional=False):
#     parser.add_argument(
#         '--repository-names',
#         nargs='*' if optional else '+',
#         help='A space separated list of repository names')
# 
# 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_test_branch(parser):
#     parser.add_argument(
#         '--test-branch', default=None,
#         help='Branch to attempt to checkout before doing batch job.')
# 
# 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: ros_buildfarm/vcs.py
# def export_repositories(source_space, check=True):
#     cmd = ['vcs', 'export', '--exact', source_space]
#     subprocess.run(cmd, check=check)
# 
# def import_repositories(source_space, repository_file, target_branch):
#     cmd = ['vcs', 'import', source_space, '--force', '--retry', '5', '--input', repository_file]
#     subprocess.check_call(cmd)
# 
#     if target_branch:
#         cmd = ['vcs', 'custom', source_space, '--git', '--args', 'checkout', '-b', '__ci_default']
#         subprocess.check_call(cmd)
# 
#         cmd = ['vcs', 'custom', source_space, '--args', 'checkout',
#                '-b', target_branch, '--track', 'origin/%s' % target_branch]
#         subprocess.call(cmd)
# 
#         cmd = ['vcs', 'custom', source_space, '--git', '--args', 'merge', '__ci_default']
#         subprocess.check_call(cmd)
# 
# Path: ros_buildfarm/workspace.py
# def ensure_workspace_exists(workspace_root):
#     # ensure that workspace exists
#     assert os.path.exists(workspace_root), \
#         "Workspace root '%s' does not exist" % workspace_root
#     source_space = os.path.join(workspace_root, 'src')
#     assert os.path.exists(source_space), \
#         "Source space '%s' does not exist" % source_space


import argparse
import os
import sys
import yaml
from urllib.request import urlretrieve
from ros_buildfarm.argument import add_argument_package_dependencies
from ros_buildfarm.argument import add_argument_package_names
from ros_buildfarm.argument import add_argument_repos_file_urls
from ros_buildfarm.argument import add_argument_repository_names
from ros_buildfarm.argument import add_argument_rosdistro_name
from ros_buildfarm.argument import add_argument_test_branch
from ros_buildfarm.common import Scope
from ros_buildfarm.vcs import export_repositories, import_repositories
from ros_buildfarm.workspace import ensure_workspace_exists
from rosdistro import get_cached_distribution
from rosdistro import get_index
from rosdistro import get_index_url
from rosdistro.dependency_walker import DependencyWalker
                        pkg_name,
                        ['buildtool', 'buildtool_export', 'build', 'build_export', 'run', 'test'],
                        ros_packages_only=True)
                additional_package_names.difference_update(args.package_names)
                args.package_names.extend(additional_package_names)
            for pkg_name in args.package_names or ():
                pkg = dist.release_packages[pkg_name]
                repo = dist.repositories[pkg.repository_name]
                rel_repo = repo.release_repository
                repo_data = {
                    'type': 'git',
                    'url': rel_repo.url,
                    'version': rel_repo.tags['release'].format_map({
                        'package': pkg_name,
                        'version': rel_repo.version,
                    }),
                }
                data[pkg_name] = repo_data
            repos_file = os.path.join(args.workspace_root, 'repositories-from-rosdistro.repos')
            with open(repos_file, 'w') as h:
                h.write(yaml.safe_dump({'repositories': data}, default_flow_style=False))
            repos_files.append(repos_file)

    with Scope('SUBSECTION', 'fetch repos files(s)'):
        for index, repos_file_url in enumerate(args.repos_file_urls):
            repos_file = os.path.join(args.workspace_root, '%d.repos' % index)
            print('Fetching \'%s\' to \'%s\'' % (repos_file_url, repos_file))
            urlretrieve(repos_file_url, repos_file)
            repos_files += [repos_file]


# Path: ros_buildfarm/argument.py
# def add_argument_package_dependencies(parser):
#     parser.add_argument(
#         '--package-dependencies',
#         action='store_true',
#         help='Also include recursive dependencies of packages specified in '
#              '--package-names.')
# 
# Path: ros_buildfarm/argument.py
# def add_argument_package_names(parser, optional=False):
#     parser.add_argument(
#         '--package-names',
#         nargs='*' if optional else '+',
#         help='A space separated list of package names')
# 
# Path: ros_buildfarm/argument.py
# def add_argument_repos_file_urls(parser):
#     parser.add_argument(
#         '--repos-file-urls', nargs='*', metavar='URL',
#         help='URLs of repos files to import with vcs.')
# 
# Path: ros_buildfarm/argument.py
# def add_argument_repository_names(parser, optional=False):
#     parser.add_argument(
#         '--repository-names',
#         nargs='*' if optional else '+',
#         help='A space separated list of repository names')
# 
# 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_test_branch(parser):
#     parser.add_argument(
#         '--test-branch', default=None,
#         help='Branch to attempt to checkout before doing batch job.')
# 
# 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: ros_buildfarm/vcs.py
# def export_repositories(source_space, check=True):
#     cmd = ['vcs', 'export', '--exact', source_space]
#     subprocess.run(cmd, check=check)
# 
# def import_repositories(source_space, repository_file, target_branch):
#     cmd = ['vcs', 'import', source_space, '--force', '--retry', '5', '--input', repository_file]
#     subprocess.check_call(cmd)
# 
#     if target_branch:
#         cmd = ['vcs', 'custom', source_space, '--git', '--args', 'checkout', '-b', '__ci_default']
#         subprocess.check_call(cmd)
# 
#         cmd = ['vcs', 'custom', source_space, '--args', 'checkout',
#                '-b', target_branch, '--track', 'origin/%s' % target_branch]
#         subprocess.call(cmd)
# 
#         cmd = ['vcs', 'custom', source_space, '--git', '--args', 'merge', '__ci_default']
#         subprocess.check_call(cmd)
# 
# Path: ros_buildfarm/workspace.py
# def ensure_workspace_exists(workspace_root):
#     # ensure that workspace exists
#     assert os.path.exists(workspace_root), \
#         "Workspace root '%s' does not exist" % workspace_root
#     source_space = os.path.join(workspace_root, 'src')
#     assert os.path.exists(source_space), \
#         "Source space '%s' does not exist" % source_space


# Path: scripts/ci/create_workspace.py
import argparse
import os
import sys
import yaml
from urllib.request import urlretrieve
from ros_buildfarm.argument import add_argument_package_dependencies
from ros_buildfarm.argument import add_argument_package_names
from ros_buildfarm.argument import add_argument_repos_file_urls
from ros_buildfarm.argument import add_argument_repository_names
from ros_buildfarm.argument import add_argument_rosdistro_name
from ros_buildfarm.argument import add_argument_test_branch
from ros_buildfarm.common import Scope
from ros_buildfarm.vcs import export_repositories, import_repositories
from ros_buildfarm.workspace import ensure_workspace_exists
from rosdistro import get_cached_distribution
from rosdistro import get_index
from rosdistro import get_index_url
from rosdistro.dependency_walker import DependencyWalker

                        pkg_name,
                        ['buildtool', 'buildtool_export', 'build', 'build_export', 'run', 'test'],
                        ros_packages_only=True)
                additional_package_names.difference_update(args.package_names)
                args.package_names.extend(additional_package_names)
            for pkg_name in args.package_names or ():
                pkg = dist.release_packages[pkg_name]
                repo = dist.repositories[pkg.repository_name]
                rel_repo = repo.release_repository
                repo_data = {
                    'type': 'git',
                    'url': rel_repo.url,
                    'version': rel_repo.tags['release'].format_map({
                        'package': pkg_name,
                        'version': rel_repo.version,
                    }),
                }
                data[pkg_name] = repo_data
            repos_file = os.path.join(args.workspace_root, 'repositories-from-rosdistro.repos')
            with open(repos_file, 'w') as h:
                h.write(yaml.safe_dump({'repositories': data}, default_flow_style=False))
            repos_files.append(repos_file)

    with Scope('SUBSECTION', 'fetch repos files(s)'):
        for index, repos_file_url in enumerate(args.repos_file_urls):
            repos_file = os.path.join(args.workspace_root, '%d.repos' % index)
            print('Fetching \'%s\' to \'%s\'' % (repos_file_url, repos_file))
            urlretrieve(repos_file_url, repos_file)
            repos_files += [repos_file]


    with Scope('SUBSECTION', 'import repositories'):