# Path: ros_buildfarm/common.py
# def get_os_package_name(rosdistro_name, ros_package_name):
#     return '%s%s' % \
#         (get_os_package_name_prefix(rosdistro_name),
#          ros_package_name.replace('_', '-'))
# 
# Path: ros_buildfarm/release_common.py
# def dpkg_parsechangelog(source_dir, fields):
#     cmd = ['dpkg-parsechangelog']
#     output = subprocess.check_output(cmd, cwd=source_dir)
#     values = {}
#     for line in output.decode().splitlines():
#         for field in fields:
#             prefix = '%s: ' % field
#             if line.startswith(prefix):
#                 values[field] = line[len(prefix):]
#     assert len(fields) == len(values.keys())
#     return [values[field] for field in fields]


import os
import subprocess
import sys
import traceback
from time import gmtime, strftime
from ros_buildfarm.common import get_os_package_name
from ros_buildfarm.release_common import dpkg_parsechangelog
        from rosdistro import get_distribution_cache
        from rosdistro import get_index
    from catkin_pkg.package import parse_package
        # Backwards compatibility for #460
        if rosdistro_name not in (
            'indigo', 'jade', 'kinetic', 'lunar', 'ardent')
        else '%s-%s' % (version, strftime('%Y%m%d-%H%M%S%z')),
        '-p',  # preserve directory name
        '-D', distribution,
        '-u', urgency,
        '-m',  # keep maintainer details
        'Append timestamp when binarydeb was built.',
    ]
    print("Invoking '%s' in '%s'" % (' '.join(cmd), source_dir))
    subprocess.check_call(cmd, cwd=source_dir)


def build_binarydeb(rosdistro_name, package_name, sourcepkg_dir, skip_tests=False):
    # ensure that one source subfolder exists
    debian_package_name = get_os_package_name(rosdistro_name, package_name)
    subfolders = _get_package_subfolders(sourcepkg_dir, debian_package_name)
    assert len(subfolders) == 1, subfolders
    source_dir = subfolders[0]

    env = dict(os.environ)
    if skip_tests:
        # There are two settings which documentation suggests for disabling
        # tests in debian builds:
        # This setting should prevent invocation in dh_auto_test
        env['DEB_BUILD_OPTIONS'] = (env.get('DEB_BUILD_OPTIONS', '') + ' nocheck').lstrip()
        # This setting should filter out Build-Depends marked with <!nocheck>
        env['DEB_BUILD_PROFILES'] = (env.get('DEB_BUILD_PROFILES', '') + ' nocheck').lstrip()


# Path: ros_buildfarm/common.py
# def get_os_package_name(rosdistro_name, ros_package_name):
#     return '%s%s' % \
#         (get_os_package_name_prefix(rosdistro_name),
#          ros_package_name.replace('_', '-'))
# 
# Path: ros_buildfarm/release_common.py
# def dpkg_parsechangelog(source_dir, fields):
#     cmd = ['dpkg-parsechangelog']
#     output = subprocess.check_output(cmd, cwd=source_dir)
#     values = {}
#     for line in output.decode().splitlines():
#         for field in fields:
#             prefix = '%s: ' % field
#             if line.startswith(prefix):
#                 values[field] = line[len(prefix):]
#     assert len(fields) == len(values.keys())
#     return [values[field] for field in fields]


# Path: ros_buildfarm/binarydeb_job.py
import os
import subprocess
import sys
import traceback
from time import gmtime, strftime
from ros_buildfarm.common import get_os_package_name
from ros_buildfarm.release_common import dpkg_parsechangelog
        from rosdistro import get_distribution_cache
        from rosdistro import get_index
    from catkin_pkg.package import parse_package

        # Backwards compatibility for #460
        if rosdistro_name not in (
            'indigo', 'jade', 'kinetic', 'lunar', 'ardent')
        else '%s-%s' % (version, strftime('%Y%m%d-%H%M%S%z')),
        '-p',  # preserve directory name
        '-D', distribution,
        '-u', urgency,
        '-m',  # keep maintainer details
        'Append timestamp when binarydeb was built.',
    ]
    print("Invoking '%s' in '%s'" % (' '.join(cmd), source_dir))
    subprocess.check_call(cmd, cwd=source_dir)


def build_binarydeb(rosdistro_name, package_name, sourcepkg_dir, skip_tests=False):
    # ensure that one source subfolder exists
    debian_package_name = get_os_package_name(rosdistro_name, package_name)
    subfolders = _get_package_subfolders(sourcepkg_dir, debian_package_name)
    assert len(subfolders) == 1, subfolders
    source_dir = subfolders[0]

    env = dict(os.environ)
    if skip_tests:
        # There are two settings which documentation suggests for disabling
        # tests in debian builds:
        # This setting should prevent invocation in dh_auto_test
        env['DEB_BUILD_OPTIONS'] = (env.get('DEB_BUILD_OPTIONS', '') + ' nocheck').lstrip()
        # This setting should filter out Build-Depends marked with <!nocheck>
        env['DEB_BUILD_PROFILES'] = (env.get('DEB_BUILD_PROFILES', '') + ' nocheck').lstrip()


    source, version = dpkg_parsechangelog(