text:
from __future__ import print_function

from collections import OrderedDict
import sys

from ros_buildfarm.common import get_binarydeb_job_name
from ros_buildfarm.common import get_debian_package_name
from ros_buildfarm.common import get_default_node_label
from ros_buildfarm.common import get_github_project_url
from ros_buildfarm.common import get_node_label
from ros_buildfarm.common import get_release_binary_view_name
from ros_buildfarm.common import get_release_job_prefix
from ros_buildfarm.common import get_release_source_view_name
from ros_buildfarm.common import get_release_view_name
from ros_buildfarm.common \
    import get_repositories_and_script_generating_key_files
from ros_buildfarm.common import get_sourcedeb_job_name
from ros_buildfarm.common import get_system_architecture
from ros_buildfarm.common import JobValidationError
from ros_buildfarm.common import write_groovy_script_and_configs
from ros_buildfarm.config import get_distribution_file
from ros_buildfarm.config import get_index as get_config_index
from ros_buildfarm.config import get_release_build_files
from ros_buildfarm.git import get_repository
from ros_buildfarm.templates import expand_template

from rosdistro import get_distribution_cache
from rosdistro import get_index

def configure_release_jobs(
        config_url, rosdistro_name, release_build_name, groovy_script=None,
        dry_run=False, whitelist_package_names=None):
    """
    Configure all Jenkins release jobs.

    L{configure_release_job} will be invoked for every released package and
    target which matches the build file criteria.

    Additionally a job to import Debian packages into the Debian repository is
    created.
    """
    config = get_config_index(config_url)
    build_files = get_release_build_files(config, rosdistro_name)
    build_file = build_files[release_build_name]

    index = get_index(config.rosdistro_index_url)

    # get targets
    platforms = []
    for os_name in build_file.targets.keys():
        for os_code_name in build_file.targets[os_name].keys():
            platforms.append((os_name, os_code_name))
    print('The build file contains the following targets:')
    for os_name, os_code_name in platforms:
        print('  - %s %s: %s' % (os_name, os_code_name, ', '.join(
            build_file.targets[os_name][os_code_name])))

    dist_file = get_distribution_file(index, rosdistro_name, build_file)
    if not dist_file:
        print('No distribution file matches the build file')
        return

    pkg_names = dist_file.release_packages.keys()
    filtered_pkg_names = build_file.filter_packages(pkg_names)
    explicitly_ignored_pkg_names = set(pkg_names) - set(filtered_pkg_names)
    if explicitly_ignored_pkg_names:
        print(('The following packages are being %s because of ' +
               'white-/blacklisting:') %
              ('ignored' if build_file.skip_ignored_packages else 'disabled'))
        for pkg_name in sorted(explicitly_ignored_pkg_names):
            print('  -', pkg_name)

    dist_cache = get_distribution_cache(index, rosdistro_name)

    if explicitly_ignored_pkg_names:
        # get direct dependencies from distro cache for each package
        direct_dependencies = {}
        for pkg_name in pkg_names:
            direct_dependencies[pkg_name] = _get_direct_dependencies(
                pkg_name, dist_cache, pkg_names) or set([])

        # find recursive downstream deps for all explicitly ignored packages
        ignored_pkg_names = set(explicitly_ignored_pkg_names)
        while True:
            implicitly_ignored_pkg_names = _get_downstream_package_names(
                ignored_pkg_names, direct_dependencies)
            if implicitly_ignored_pkg_names - ignored_pkg_names:
                ignored_pkg_names |= implicitly_ignored_pkg_names
                continue
            break
        implicitly_ignored_pkg_names = \
            ignored_pkg_names - explicitly_ignored_pkg_names

        if implicitly_ignored_pkg_names:
            print(('The following packages are being %s because their ' +
                   'dependencies are being ignored:') % ('ignored'
                  if build_file.skip_ignored_packages else 'disabled'))
            for pkg_name in sorted(implicitly_ignored_pkg_names):
                print('  -', pkg_name)
            filtered_pkg_names = \
                set(filtered_pkg_names) - implicitly_ignored_pkg_names

    # all further configuration will be handled by either the Jenkins API
    # or by a generated groovy script
    jenkins = False
    if groovy_script is None:
        from ros_buildfarm.jenkins import connect
        jenkins = connect(config.jenkins_url)

    all_view_configs = {}
    all_job_configs = OrderedDict()

    job_name, job_config = configure_import_package_job(
        config_url, rosdistro_name, release_build_name,
        config=config, build_file=build_file, jenkins=jenkins, dry_run=dry_run)
    if not jenkins:
        all_job_configs[job_name] = job_config

    job_name, job_config = configure_sync_packages_to_main_job(
        config_url, rosdistro_name, release_build_name,
        config=config, build_file=build_file, jenkins=jenkins, dry_run=dry_run)
    if not jenkins:
        all_job_configs[job_name] = job_config

    for os_name, os_code_name in platforms:
        for arch in sorted(build_file.targets[os_name][os_code_name]):
            job_name, job_config = configure_sync_packages_to_testing_job(
                config_url, rosdistro_name, release_build_name,
                os_code_name, arch,
                config=config, build_file=build_file, jenkins=jenkins,
                dry_run=dry_run)
            if not jenkins:
                all_job_configs[job_name] = job_config

    targets = []
    for os_name, os_code_name in platforms:
        targets.append((os_name, os_code_name, 'source'))
        for arch in build_file.targets[os_name][os_code_name]:
            targets.append((os_name, os_code_name, arch))
    views = configure_release_views(
        jenkins, rosdistro_name, release_build_name, targets,
        dry_run=dry_run)
    if not jenkins:
        all_view_configs.update(views)
    groovy_data = {
        'dry_run': dry_run,
        'expected_num_views': len(views),
    }

    # binary jobs must be generated in topological order
    from catkin_pkg.package import parse_package_string
    from ros_buildfarm.common import topological_order_packages
    pkgs = {}
    for pkg_name in pkg_names:
        if pkg_name not in dist_cache.release_package_xmls:
            print("Skipping package '%s': no released package.xml in cache" %
                  (pkg_name), file=sys.stderr)
            continue
        pkg_xml = dist_cache.release_package_xmls[pkg_name]
        pkg = parse_package_string(pkg_xml)
        pkgs[pkg_name] = pkg
    ordered_pkg_tuples = topological_order_packages(pkgs)

    other_build_files = [v for k, v in build_files.items() if k != release_build_name]

    all_source_job_names = []
    all_binary_job_names = []
    for pkg_name in [p.name for _, p in ordered_pkg_tuples]:
        if whitelist_package_names:
            if pkg_name not in whitelist_package_names:
                print("Skipping package '%s' not in the explicitly passed list" %
                      pkg_name, file=sys.stderr)
                continue

        pkg = dist_file.release_packages[pkg_name]
        repo_name = pkg.repository_name
        repo = dist_file.repositories[repo_name]
        is_disabled = pkg_name not in filtered_pkg_names
        if is_disabled and build_file.skip_ignored_packages:
            print("Skipping ignored package '%s' in repository '%s'" %
                  (pkg_name, repo_name), file=sys.stderr)
            continue
        if not repo.release_repository:
            print(("Skipping package '%s' in repository '%s': no release " +
                   "section") % (pkg_name, repo_name), file=sys.stderr)
            continue
        if not repo.release_repository.version:
            print(("Skipping package '%s' in repository '%s': no release " +
                   "version") % (pkg_name, repo_name), file=sys.stderr)
            continue

        for os_name, os_code_name in platforms:
            other_build_files_same_platform = []
            for other_build_file in other_build_files:
                if os_name not in other_build_file.targets:
                    continue
                if os_code_name not in other_build_file.targets[os_name]:
                    continue
                other_build_files_same_platform.append(other_build_file)

            try:
                source_job_names, binary_job_names, job_configs = \
                    configure_release_job(
                        config_url, rosdistro_name, release_build_name,
                        pkg_name, os_name, os_code_name,
                        config=config, build_file=build_file,
                        index=index, dist_file=dist_file,
                        dist_cache=dist_cache,
                        jenkins=jenkins, views=views,
                        generate_import_package_job=False,
                        generate_sync_packages_jobs=False,
                        is_disabled=is_disabled,
                        other_build_files_same_platform=other_build_files_same_platform,
                        groovy_script=groovy_script,
                        dry_run=dry_run)
                all_source_job_names += source_job_names
                all_binary_job_names += binary_job_names
                if groovy_script is not None:
                    print('Configuration for jobs: ' +
                          ', '.join(source_job_names + binary_job_names))
                    for source_job_name in source_job_names:
                        all_job_configs[source_job_name] = job_configs[source_job_name]
                    for binary_job_name in binary_job_names:
                        all_job_configs[binary_job_name] = job_configs[binary_job_name]
            except JobValidationError as e:
                print(e.message, file=sys.stderr)

    groovy_data['expected_num_jobs'] = len(all_job_configs)
    groovy_data['job_prefixes_and_names'] = {}

    # with an explicit list of packages we don't delete obsolete jobs
    if not whitelist_package_names:
        # delete obsolete binary jobs
        for os_name, os_code_name in platforms:
            for arch in build_file.targets[os_name][os_code_name]:
                binary_view = get_release_binary_view_name(
                    rosdistro_name, release_build_name,
                    os_name, os_code_name, arch)
                binary_job_prefix = '%s__' % binary_view

                excluded_job_names = set([
                    j for j in all_binary_job_names
                    if j.startswith(binary_job_prefix)])
                if groovy_script is None:
                    print("Removing obsolete binary jobs with prefix '%s'" %
                          binary_job_prefix)
                    from ros_buildfarm.jenkins import remove_jobs
                    remove_jobs(
                        jenkins, binary_job_prefix, excluded_job_names,
                        dry_run=dry_run)
                else:
                    binary_key = 'binary_%s_%s_%s' % \
                        (os_name, os_code_name, arch)
                    groovy_data['job_prefixes_and_names'][binary_key] = \
                        (binary_job_prefix, excluded_job_names)

        # delete obsolete source jobs
        # requires knowledge about all other release build files
        for os_name, os_code_name in platforms:
            other_source_job_names = []
            # get source job names for all other release build files
            for other_release_build_name in [
                    k for k in build_files.keys() if k != release_build_name]:
                other_build_file = build_files[other_release_build_name]
                other_dist_file = get_distribution_file(
                    index, rosdistro_name, other_build_file)
                if not other_dist_file:
                    continue

                if os_name not in other_build_file.targets or \
                        os_code_name not in other_build_file.targets[os_name]:
                    continue

                if other_build_file.skip_ignored_packages:
                    filtered_pkg_names = other_build_file.filter_packages(
                        pkg_names)
                else:
                    filtered_pkg_names = pkg_names
                for pkg_name in sorted(filtered_pkg_names):
                    pkg = other_dist_file.release_packages[pkg_name]
                    repo_name = pkg.repository_name
                    repo = other_dist_file.repositories[repo_name]
                    if not repo.release_repository:
                        continue
                    if not repo.release_repository.version:
                        continue

                    other_job_name = get_sourcedeb_job_name(
                        rosdistro_name, other_release_build_name,
                        pkg_name, os_name, os_code_name)
                    other_source_job_names.append(other_job_name)

            source_view_prefix = get_release_source_view_name(
                rosdistro_name, os_name, os_code_name)
            source_job_prefix = '%s__' % source_view_prefix
            excluded_job_names = set([
                j for j in (all_source_job_names + other_source_job_names)
                if j.startswith(source_job_prefix)])
            if groovy_script is None:
                print("Removing obsolete source jobs with prefix '%s'" %
                      source_job_prefix)
                from ros_buildfarm.jenkins import remove_jobs
                remove_jobs(
                    jenkins, source_job_prefix, excluded_job_names,
                    dry_run=dry_run)
            else:
                source_key = 'source_%s_%s' % (os_name, os_code_name)
                groovy_data['job_prefixes_and_names'][source_key] = (
                    source_job_prefix, excluded_job_names)

    if groovy_script is not None:
        print(
            "Writing groovy script '%s' to reconfigure %d views and %d jobs" %
            (groovy_script, len(all_view_configs), len(all_job_configs)))
        content = expand_template(
            'snippet/reconfigure_jobs.groovy.em', groovy_data)
        write_groovy_script_and_configs(
            groovy_script, content, all_job_configs,
            view_configs=all_view_configs)

def _get_downstream_package_names(pkg_names, dependencies):
    downstream_pkg_names = set([])
    for pkg_name, deps in dependencies.items():
        if deps.intersection(pkg_names):
            downstream_pkg_names.add(pkg_name)
    return downstream_pkg_names

# Configure a Jenkins release job which consists of
# - a source deb job
# - N binary debs, one for each archicture
def configure_release_job(
        config_url, rosdistro_name, release_build_name,
        pkg_name, os_name, os_code_name,
        config=None, build_file=None,
        index=None, dist_file=None, dist_cache=None,
        jenkins=None, views=None,
        generate_import_package_job=True,
        generate_sync_packages_jobs=True,
        is_disabled=False, other_build_files_same_platform=None,
        groovy_script=None,
        filter_arches=None,
        dry_run=False):
    """
    Configure a Jenkins release job.

    The following jobs are created for each package:
    - M source jobs, one for each OS node name
    - M * N binary jobs, one for each combination of OS code name and arch
    """
    if config is None:
        config = get_config_index(config_url)
    if build_file is None:
        build_files = get_release_build_files(config, rosdistro_name)
        build_file = build_files[release_build_name]

    if index is None:
        index = get_index(config.rosdistro_index_url)
    if dist_file is None:
        dist_file = get_distribution_file(index, rosdistro_name, build_file)
        if not dist_file:
            raise JobValidationError(
                'No distribution file matches the build file')

    pkg_names = dist_file.release_packages.keys()

    if pkg_name not in pkg_names:
        raise JobValidationError(
            "Invalid package name '%s' " % pkg_name +
            'choose one of the following: ' + ', '.join(sorted(pkg_names)))

    pkg = dist_file.release_packages[pkg_name]
    repo_name = pkg.repository_name
    repo = dist_file.repositories[repo_name]

    if not repo.release_repository:
        raise JobValidationError(
            "Repository '%s' has no release section" % repo_name)

    if not repo.release_repository.version:
        raise JobValidationError(
            "Repository '%s' has no release version" % repo_name)

    if os_name not in build_file.targets.keys():
        raise JobValidationError(
            "Invalid OS name '%s' " % os_name +
            'choose one of the following: ' +
            ', '.join(sorted(build_file.targets.keys())))

    if os_code_name not in build_file.targets[os_name].keys():
        raise JobValidationError(
            "Invalid OS code name '%s' " % os_code_name +
            'choose one of the following: ' +
            ', '.join(sorted(build_file.targets[os_name].keys())))

    if dist_cache is None and \
            (build_file.notify_maintainers or
             build_file.abi_incompatibility_assumed):
        dist_cache = get_distribution_cache(index, rosdistro_name)
    if jenkins is None:
        from ros_buildfarm.jenkins import connect
        jenkins = connect(config.jenkins_url)
    if views is None:
        targets = []
        targets.append((os_name, os_code_name, 'source'))
        for arch in build_file.targets[os_name][os_code_name]:
            targets.append((os_name, os_code_name, arch))
        configure_release_views(
            jenkins, rosdistro_name, release_build_name, targets,
            dry_run=dry_run)

    if generate_import_package_job:
        configure_import_package_job(
            config_url, rosdistro_name, release_build_name,
            config=config, build_file=build_file, jenkins=jenkins,
            dry_run=dry_run)

    if generate_sync_packages_jobs:
        configure_sync_packages_to_main_job(
            config_url, rosdistro_name, release_build_name,
            config=config, build_file=build_file, jenkins=jenkins,
            dry_run=dry_run)
        for arch in build_file.targets[os_name][os_code_name]:
            configure_sync_packages_to_testing_job(
                config_url, rosdistro_name, release_build_name,
                os_code_name, arch,
                config=config, build_file=build_file, jenkins=jenkins,
                dry_run=dry_run)

    source_job_names = []
    binary_job_names = []
    job_configs = {}

    # sourcedeb job
    # since sourcedeb jobs are potentially being shared across multiple build
    # files the configuration has to take all of them into account in order to
    # generate a job which all build files agree on
    source_job_name = get_sourcedeb_job_name(
        rosdistro_name, release_build_name,
        pkg_name, os_name, os_code_name)

    # while the package is disabled in the current build file
    # it might be used by sibling build files
    is_source_disabled = is_disabled
    if is_source_disabled and other_build_files_same_platform:
        # check if sourcedeb job is used by any other build file with the same platform
        for other_build_file in other_build_files_same_platform:
            if other_build_file.filter_packages([pkg_name]):
                is_source_disabled = False
                break

    job_config = _get_sourcedeb_job_config(
        config_url, rosdistro_name, release_build_name,
        config, build_file, os_name, os_code_name,
        pkg_name, repo_name, repo.release_repository, dist_cache=dist_cache,
        is_disabled=is_source_disabled,
        other_build_files_same_platform=other_build_files_same_platform)
    # jenkinsapi.jenkins.Jenkins evaluates to false if job count is zero
    if isinstance(jenkins, object) and jenkins is not False:
        from ros_buildfarm.jenkins import configure_job
        configure_job(jenkins, source_job_name, job_config, dry_run=dry_run)
    source_job_names.append(source_job_name)
    job_configs[source_job_name] = job_config

    dependency_names = []
    if build_file.abi_incompatibility_assumed:
        dependency_names = _get_direct_dependencies(
            pkg_name, dist_cache, pkg_names)
        # if dependencies are not yet available in rosdistro cache
        # skip binary jobs
        if dependency_names is None:
            print(("Skipping binary jobs for package '%s' because it is not " +
                   "yet in the rosdistro cache") % pkg_name, file=sys.stderr)
            return source_job_names, binary_job_names, job_configs

    # binarydeb jobs
    for arch in build_file.targets[os_name][os_code_name]:
        if filter_arches and arch not in filter_arches:
            continue

        job_name = get_binarydeb_job_name(
            rosdistro_name, release_build_name,
            pkg_name, os_name, os_code_name, arch)

        upstream_job_names = [source_job_name] + [
            get_binarydeb_job_name(
                rosdistro_name, release_build_name,
                dependency_name, os_name, os_code_name, arch)
            for dependency_name in dependency_names]

        job_config = _get_binarydeb_job_config(
            config_url, rosdistro_name, release_build_name,
            config, build_file, os_name, os_code_name, arch,
            pkg_name, repo_name, repo.release_repository,
            dist_cache=dist_cache, upstream_job_names=upstream_job_names,
            is_disabled=is_disabled)
        # jenkinsapi.jenkins.Jenkins evaluates to false if job count is zero
        if isinstance(jenkins, object) and jenkins is not False:
            configure_job(jenkins, job_name, job_config, dry_run=dry_run)
        binary_job_names.append(job_name)
        job_configs[job_name] = job_config

    return source_job_names, binary_job_names, job_configs

def configure_release_views(
        jenkins, rosdistro_name, release_build_name, targets, dry_run=False):
    from ros_buildfarm.jenkins import configure_view
    views = {}

    for os_name, os_code_name, arch in targets:
        view_name = get_release_view_name(
            rosdistro_name, release_build_name, os_name, os_code_name,
            arch)
        if arch == 'source':
            include_regex = '%s__.+__%s_%s__source' % \
                (view_name, os_name, os_code_name)
        else:
            include_regex = '%s__.+__%s_%s_%s__binary' % \
                (view_name, os_name, os_code_name, arch)
        views[view_name] = configure_view(
            jenkins, view_name, include_regex=include_regex,
            template_name='dashboard_view_all_jobs.xml.em', dry_run=dry_run)

    return views

def _get_direct_dependencies(pkg_name, dist_cache, pkg_names):
    from catkin_pkg.package import parse_package_string
    if pkg_name not in dist_cache.release_package_xmls:
        return None
    pkg_xml = dist_cache.release_package_xmls[pkg_name]
    pkg = parse_package_string(pkg_xml)
    depends = set([
        d.name for d in (
            pkg.buildtool_depends +
            pkg.build_depends +
            pkg.buildtool_export_depends +
            pkg.build_export_depends +
            pkg.exec_depends +
            pkg.test_depends)
        if d.name in pkg_names])
    return depends

def _get_sourcedeb_job_config(
        config_url, rosdistro_name, release_build_name,
        config, build_file, os_name, os_code_name,
        pkg_name, repo_name, release_repository, dist_cache=None,
        is_disabled=False, other_build_files_same_platform=None):
    template_name = 'release/sourcedeb_job.xml.em'

    repository_args, script_generating_key_files = \
        get_repositories_and_script_generating_key_files(build_file=build_file)

    sourcedeb_files = [
        'sourcedeb/*.debian.tar.gz',
        'sourcedeb/*.debian.tar.xz',
        'sourcedeb/*.dsc',
        'sourcedeb/*.orig.tar.gz',
        'sourcedeb/*_source.buildinfo',
        'sourcedeb/*_source.changes',
    ]

    # collect notify emails from all build files with the job enabled
    notify_emails = set(build_file.notify_emails)
    if other_build_files_same_platform:
        for other_build_file in other_build_files_same_platform:
            if other_build_file.filter_packages([pkg_name]):
                notify_emails.update(other_build_file.notify_emails)

    # notify maintainers if any build file (with the job enabled) requests it
    notify_maintainers = build_file.notify_maintainers
    if other_build_files_same_platform:
        for other_build_file in other_build_files_same_platform:
            if other_build_file.filter_packages([pkg_name]):
                if other_build_file.notify_maintainers:
                    notify_maintainers = True

    maintainer_emails = _get_maintainer_emails(dist_cache, pkg_name) \
        if notify_maintainers \
        else set([])

    job_data = {
        'github_url': get_github_project_url(release_repository.url),

        'job_priority': build_file.jenkins_source_job_priority,
        'node_label': get_node_label(
            build_file.jenkins_source_job_label,
            get_default_node_label('%s_%s' % (
                rosdistro_name, 'sourcedeb'))),

        'disabled': is_disabled,

        'ros_buildfarm_repository': get_repository(),

        'script_generating_key_files': script_generating_key_files,

        'rosdistro_index_url': config.rosdistro_index_url,
        'rosdistro_name': rosdistro_name,
        'release_build_name': release_build_name,
        'pkg_name': pkg_name,
        'os_name': os_name,
        'os_code_name': os_code_name,
        'arch': get_system_architecture(),
        'repository_args': repository_args,

        'sourcedeb_files': sourcedeb_files,

        'import_package_job_name': get_import_package_job_name(rosdistro_name),
        'debian_package_name': get_debian_package_name(
            rosdistro_name, pkg_name),

        'notify_emails': notify_emails,
        'maintainer_emails': maintainer_emails,
        'notify_maintainers': notify_maintainers,

        'timeout_minutes': build_file.jenkins_source_job_timeout,

        'credential_id': build_file.upload_credential_id,

        'git_ssh_credential_id': config.git_ssh_credential_id,
    }
    job_config = expand_template(template_name, job_data)
    return job_config

def _get_binarydeb_job_config(
        config_url, rosdistro_name, release_build_name,
        config, build_file, os_name, os_code_name, arch,
        pkg_name, repo_name, release_repository,
        dist_cache=None, upstream_job_names=None,
        is_disabled=False):
    template_name = 'release/binarydeb_job.xml.em'

    repository_args, script_generating_key_files = \
        get_repositories_and_script_generating_key_files(build_file=build_file)
    repository_args.append(
        '--target-repository ' + build_file.target_repository)

    binarydeb_files = [
        'binarydeb/*.changes',
        'binarydeb/*.deb',
    ]

    sync_to_testing_job_name = [get_sync_packages_to_testing_job_name(
        rosdistro_name, os_code_name, arch)]

    maintainer_emails = _get_maintainer_emails(dist_cache, pkg_name) \
        if build_file.notify_maintainers \
        else set([])

    job_data = {
        'github_url': get_github_project_url(release_repository.url),

        'job_priority': build_file.jenkins_binary_job_priority,
        'node_label': get_node_label(
            build_file.jenkins_binary_job_label,
            get_default_node_label('%s_%s_%s' % (
                rosdistro_name, 'binarydeb', release_build_name))),

        'disabled': is_disabled,

        'upstream_projects': upstream_job_names,

        'ros_buildfarm_repository': get_repository(),

        'script_generating_key_files': script_generating_key_files,

        'rosdistro_index_url': config.rosdistro_index_url,
        'rosdistro_name': rosdistro_name,
        'release_build_name': release_build_name,
        'pkg_name': pkg_name,
        'os_name': os_name,
        'os_code_name': os_code_name,
        'arch': arch,
        'repository_args': repository_args,

        'append_timestamp': build_file.abi_incompatibility_assumed,

        'binarydeb_files': binarydeb_files,

        'import_package_job_name': get_import_package_job_name(rosdistro_name),
        'debian_package_name': get_debian_package_name(
            rosdistro_name, pkg_name),

        'child_projects': sync_to_testing_job_name,

        'notify_emails': build_file.notify_emails,
        'maintainer_emails': maintainer_emails,
        'notify_maintainers': build_file.notify_maintainers,

        'timeout_minutes': build_file.jenkins_binary_job_timeout,

        'credential_id': build_file.upload_credential_id,
    }
    job_config = expand_template(template_name, job_data)
    return job_config

def configure_import_package_job(
        config_url, rosdistro_name, release_build_name,
        config=None, build_file=None, jenkins=None, dry_run=False):
    if config is None:
        config = get_config_index(config_url)
    if build_file is None:
        build_files = get_release_build_files(config, rosdistro_name)
        build_file = build_files[release_build_name]
    if jenkins is None:
        from ros_buildfarm.jenkins import connect
        jenkins = connect(config.jenkins_url)

    job_name = get_import_package_job_name(rosdistro_name)
    job_config = _get_import_package_job_config(build_file)

    # jenkinsapi.jenkins.Jenkins evaluates to false if job count is zero
    if isinstance(jenkins, object) and jenkins is not False:
        from ros_buildfarm.jenkins import configure_job
        configure_job(jenkins, job_name, job_config, dry_run=dry_run)
    return (job_name, job_config)

def get_import_package_job_name(rosdistro_name):
    view_name = get_release_job_prefix(rosdistro_name)
    return '%s_import-package' % view_name

def _get_import_package_job_config(build_file):
    template_name = 'release/import_package_job.xml.em'
    job_data = {
        'target_queue': build_file.target_queue,
        'abi_incompatibility_assumed': build_file.abi_incompatibility_assumed,
        'notify_emails': build_file.notify_emails,
    }
    job_config = expand_template(template_name, job_data)
    return job_config

def configure_sync_packages_to_testing_job(
        config_url, rosdistro_name, release_build_name, os_code_name, arch,
        config=None, build_file=None, jenkins=None, dry_run=False):
    if config is None:
        config = get_config_index(config_url)
    if build_file is None:
        build_files = get_release_build_files(config, rosdistro_name)
        build_file = build_files[release_build_name]
    if jenkins is None:
        from ros_buildfarm.jenkins import connect
        jenkins = connect(config.jenkins_url)

    job_name = get_sync_packages_to_testing_job_name(
        rosdistro_name, os_code_name, arch)
    job_config = _get_sync_packages_to_testing_job_config(
        config_url, rosdistro_name, release_build_name, os_code_name, arch,
        config, build_file)

    # jenkinsapi.jenkins.Jenkins evaluates to false if job count is zero
    if isinstance(jenkins, object) and jenkins is not False:
        from ros_buildfarm.jenkins import configure_job
        configure_job(jenkins, job_name, job_config, dry_run=dry_run)
    return (job_name, job_config)

def get_sync_packages_to_testing_job_name(
        rosdistro_name, os_code_name, arch):
    view_name = get_release_job_prefix(rosdistro_name)
    return '%s_sync-packages-to-testing_%s_%s' % \
        (view_name, os_code_name, arch)

def _get_sync_packages_to_testing_job_config(
        config_url, rosdistro_name, release_build_name, os_code_name, arch,
        config, build_file):
    template_name = 'release/sync_packages_to_testing_job.xml.em'

    repository_args, script_generating_key_files = \
        get_repositories_and_script_generating_key_files(build_file=build_file)

    job_data = {
        'ros_buildfarm_repository': get_repository(),

        'script_generating_key_files': script_generating_key_files,

        'config_url': config_url,
        'rosdistro_name': rosdistro_name,
        'release_build_name': release_build_name,
        'os_code_name': os_code_name,
        'arch': arch,
        'repository_args': repository_args,

        'notify_emails': build_file.notify_emails,
    }
    job_config = expand_template(template_name, job_data)
    return job_config

def configure_sync_packages_to_main_job(
        config_url, rosdistro_name, release_build_name,
        config=None, build_file=None, jenkins=None, dry_run=False):
    if config is None:
        config = get_config_index(config_url)
    if build_file is None:
        build_files = get_release_build_files(config, rosdistro_name)
        build_file = build_files[release_build_name]
    if jenkins is None:
        from ros_buildfarm.jenkins import connect
        jenkins = connect(config.jenkins_url)

    job_name = get_sync_packages_to_main_job_name(
        rosdistro_name)
    job_config = _get_sync_packages_to_main_job_config(
        rosdistro_name, build_file)

    # jenkinsapi.jenkins.Jenkins evaluates to false if job count is zero
    if isinstance(jenkins, object) and jenkins is not False:
        from ros_buildfarm.jenkins import configure_job
        configure_job(jenkins, job_name, job_config, dry_run=dry_run)
    return (job_name, job_config)

def get_sync_packages_to_main_job_name(rosdistro_name):
    view_name = get_release_job_prefix(rosdistro_name)
    return '%s_sync-packages-to-main' % view_name

def _get_sync_packages_to_main_job_config(rosdistro_name, build_file):
    template_name = 'release/sync_packages_to_main_job.xml.em'
    job_data = {
        'rosdistro_name': rosdistro_name,

        'notify_emails': build_file.notify_emails,
    }
    job_config = expand_template(template_name, job_data)
    return job_config

def _get_maintainer_emails(dist_cache, pkg_name):
    maintainer_emails = set([])
    # add maintainers listed in latest release to recipients
    if dist_cache and pkg_name in dist_cache.release_package_xmls:
        from catkin_pkg.package import parse_package_string
        pkg_xml = dist_cache.release_package_xmls[pkg_name]
        pkg = parse_package_string(pkg_xml)
        for m in pkg.maintainers:
            maintainer_emails.add(m.email)
    return maintainer_emails


text:
/* OFF */
#mainBigMenu a.myMessages{width:114px; height:84px; background: url(../include_img/layout/3/bt_messagesOFF.png) no-repeat;}
* html #mainBigMenu a.myMessages{background:none;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../include_img/layout/3/bt_messagesOFF.png', sizingMethod='crop');}

#mainBigMenu a.myNablife {width:98px; height:84px; background: url(../include_img/layout/3/bt_nablifeOFF.png) no-repeat;}
* html #mainBigMenu a.myNablife{background:none;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../include_img/layout/3/bt_nablifeOFF.png', sizingMethod='crop');}

#mainBigMenu a.myTerrier {width:114px; height:84px; background: url(../include_img/layout/3/bt_terrierOFF.png) no-repeat;}
* html #mainBigMenu a.myTerrier{background:none;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../include_img/layout/3/bt_terrierOFF.png', sizingMethod='crop');}

#mainBigMenu a.myTools {width:81px; height:84px; background: url(../include_img/layout/3/bt_outilsOFF.png) no-repeat;}
* html #mainBigMenu a.myTools{background:none;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../include_img/layout/3/bt_outilsOFF.png', sizingMethod='crop');}

#mainBigMenu a.myHelp  {width:65px; height:84px; background: url(../include_img/layout/3/bt_aideOFF.png) no-repeat;}
* html #mainBigMenu a.myHelp{background:none;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../include_img/layout/3/bt_aideOFF.png', sizingMethod='crop');}

/* ON */
#mainBigMenu a.myMessagesON{width:114px; height:84px; background: url(../include_img/layout/3/bt_messagesON.png) no-repeat;}
* html #mainBigMenu a.myMessagesON{background:none;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../include_img/layout/3/bt_messagesON.png', sizingMethod='crop');}

#mainBigMenu a.myNablifeON {width:98px; height:84px; background: url(../include_img/layout/3/bt_nablifeON.png) no-repeat;}
* html #mainBigMenu a.myNablifeON{background:none;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../include_img/layout/3/bt_nablifeON.png', sizingMethod='crop');}

#mainBigMenu a.myTerrierON {width:114px; height:84px; background: url(../include_img/layout/3/bt_terrierON.png) no-repeat;}
* html #mainBigMenu a.myTerrierON{background:none;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../include_img/layout/3/bt_terrierON.png', sizingMethod='crop');}

#mainBigMenu a.myToolsON {width:81px; height:84px; background: url(../include_img/layout/3/bt_outilsON.png) no-repeat;}
* html #mainBigMenu a.myToolsON{background:none;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../include_img/layout/3/bt_outilsON.png', sizingMethod='crop');}

#mainBigMenu a.myHelpON  {width:65px; height:84px; background: url(../include_img/layout/3/bt_aideON.png) no-repeat;}
* html #mainBigMenu a.myHelpON {background:none;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../include_img/layout/3/bt_aideON.png', sizingMethod='crop');}

redpajama_set_name: RedPajamaGithub


text:
package utils.utils;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 黄艳武 on 2016/3/23.;
 * 对于用户输入的判断
 */
public class InputUtils {
    /***
     * @return 获取一段字符中, 中文字符串的长度和其他字符串的长度
     */
    public static int[] getChinesLetterCountAndOther(String input) {
        if (TextUtils.isEmpty(input)) return new int[]{0, 0};
        String temp = null;
        Pattern p = Pattern.compile("[\u4E00-\u9FA5]+");
        Matcher m = p.matcher(input);
        int count = 0;
        while (m.find()) {
            temp = m.group(0);
            count += temp.length();
        }
        return new int[]{count, input.length() - count};
    }

    /**
     * 汉字为两个字符,英语为一个字符
     */
    public static int getLetterCount(String input) {
        int[] counts = getChinesLetterCountAndOther(input);
        return counts[0] * 2 + counts[1];
    }

    public interface SimpleTextWatcher {
        public void onTextChanged(CharSequence s, int start, int before, int count);
    }

    /****
     * @return 添加一个建议的监听事件
     */
    public static void addSimpleTextWatcher(EditText editText, @NonNull final SimpleTextWatcher simpleListener) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (simpleListener != null)
                    simpleListener.onTextChanged(s, start, before, count);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    /****
     * @param maxSize 输入汉字的个数
     * @return 添加一个建议的监听事件
     */
    public static void addSimpleTextWatcher(EditText editText, final Context context, final int maxSize) {
        addSimpleTextWatcher(editText, context, maxSize, "不能超过" + maxSize + "个汉字哦...", "输入的字符超过限制...");
    }

    /****
     * @param maxSize 输入汉字的个数
     * @return 添加一个建议的监听事件
     */
    public static void addSimpleTextWatcher(EditText editText, final Context context, final int maxSize, final String tip1, final String tips2) {
        addSimpleTextWatcher(editText, new InputUtils.SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > maxSize) {
                    int[] counts = InputUtils.getChinesLetterCountAndOther(s.toString().trim());
                    int totalCount = counts[0] * 2 + counts[1];
                    if (counts[0] > maxSize && counts[0] == s.length()) {
                        ToastUtil.show(context, tip1);
                    } else if (totalCount > maxSize * 2) {
                        ToastUtil.show(context, tips2);
                    }
                }
            }
        });
    }

    /***
     * @return 输入小数点的时候,自动0.并且输入小数的时候只能输入两位
     */
    public static void addWatcher(final EditText ev_input_weight, final onTextListener onTextListener) {
        ev_input_weight.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setDecimalLastTwo(ev_input_weight);
                if (onTextListener != null)
                    onTextListener.onFinishDeal(ev_input_weight.getText().toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    /***
     * @return 限制输入只能输入到小数点后两位
     */
    public static void setDecimalLastTwo(EditText decimalLastTwo) {
        String text = decimalLastTwo.getText().toString();
        if (decimalLastTwo.hasFocus() && text.contains(".") && !text.endsWith(".")) {
            int position = text.lastIndexOf(".");
            if (text.length() - position > 3)
                decimalLastTwo.setText(text.substring(0, position + 3));
        }
    }

    /***
     * 输入是否超出了限制
     *
     * @return 最大字符, 汉字算两个, 英文算一个
     */
    public static boolean isSurpassLismit(String s, int maxCount) {
        if (!TextUtils.isEmpty(s)) {
            int[] counts = InputUtils.getChinesLetterCountAndOther(s.toString().trim());
            if (counts == null || counts.length <= 0) {
                return false;
            }
            int totalCount = counts[0] * 2 + counts[1];//这里表示的是汉字*2+其他*1=总字符的长度
            if (totalCount > maxCount) {//18表示的是字符的长度
                return true;
            }
        }
        return false;
    }

    /**
     * @return 超出了多少个字符, 返回的是字符, 汉子算两个字符
     */
    public static int surpassLitmitCount(String s, int maxCount) {
        int[] counts = InputUtils.getChinesLetterCountAndOther(s);
        if (counts == null || counts.length <= 0) {
            return 0;
        }
        int totalCount = counts[0] * 2 + counts[1];//这里表示的是汉字*2+其他*1=总字符的长度
        return totalCount - maxCount;
    }

    /**
     * 汉字算两个, 英文算一个
     *
     * @return 返回限定的文本数量:汉字和英文数量加起来为maxCount
     */
    public static String getMaxString(String s, int maxCount) {
        if (TextUtils.isEmpty(s) || !InputUtils.isSurpassLismit(s, maxCount))
            return s;//为空或者没超出限定都直接返回

        int count = 0;
        Pattern p = Pattern.compile("[\u4E00-\u9FA5]+");
        char[] characters = s.toCharArray();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < characters.length; i++) {
            if (p.matcher(String.valueOf(characters[i])).matches()) {//是汉字
                count += 2;
            } else {
                count += 1;
            }
            if (count > maxCount) break;
            stringBuilder.append(characters[i]);
        }
        return stringBuilder.toString();
    }

    /**
     * @param nickName
     */
    public static boolean isInputEmoj(String nickName) {
        for (int i = 0; i < nickName.length(); i++) {
            if (EmojFilters.isEmojiCharacter(nickName.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    public interface onTextListener {
        public void onFinishDeal(String price);
    }

    public static String replaceCharacterWitch(String text, String replaceChar) {
        if (TextUtils.isEmpty(text)) return text;
        String string = "";
        for (int i = 0; i < text.length(); i++) {
            string += replaceChar;
        }
        return string;
    }

}


text:
var express = require('express');
var app = express();

var path = require('path');

app.get('/',function(request,response){

	response.sendFile(path.join(__dirname +'/public'+'/index.html'));

});

app.listen(3005);


text:
<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string name="app_name">Documents Demo</string>
	<string name="hint">Editor #%d</string>
	<string name="split">Fork</string>
	<string name="add">Add Before</string>
	<string name="remove">Remove</string>
	<string name="swap">Swap</string>
  <string name="launch">Launch</string>
  <string name="editor_activity_title_prefix">"Editing "</string>

</resources>


text:
CUDA_ROOT = /usr/local/cuda
ARCH = sm_13

CC = gcc-4.4
CXX = gcc-4.4
NVCC = $(CUDA_ROOT)/bin/nvcc
# NVCC = g++

INCLUDES += -I$(BOOST_ROOT) -I$(THRUST_ROOT) -I$(CUDA_ROOT)/include -I../../../../..

NVCCFLAGS = -O3 $(INCLUDES) -arch $(ARCH) --compiler-bindir=/usr/bin/g++-4.4  -Xcompiler -fopenmp -DTHRUST_DEVICE_BACKEND=THRUST_DEVICE_BACKEND_OMP
# NVCCFLAGS = -O3 $(INCLUDES) -arch $(ARCH) --compiler-bindir=/usr/bin/g++-4.3 -Xcompiler -fopenmp -DTHRUST_DEVICE_BACKEND=THRUST_DEVICE_BACKEND_OMP

# NVCCFLAGS = -O3 $(INCLUDES) -arch $(ARCH) --compiler-bindir=/usr/bin/g++-4.3

#--compiler-bindir=/usr/bin/g++-4.4
#-Xcompiler -fopenmp -DTHRUST_DEVICE_BACKEND=THRUST_DEVICE_BACKEND_OMP
# NVCCFLAGS = -O3 $(INCLUDES) -arch $(ARCH) --compiler-bindir=/usr/bin/g++-4.3
#-Xcompiler -fopenmp -DTHRUST_DEVICE_BACKEND=THRUST_DEVICE_BACKEND_OMP

LDLIBS = -lcudart -lgomp
LDFLAGS = -L$(CUDA_ROOT)/lib64

%.co : %.cu
	$(NVCC) $(NVCCFLAGS) -c $< -o $@

all : phase_oscillator_chain phase_oscillator_ensemble lorenz_parameters relaxation

phase_oscillator_chain.co : phase_oscillator_chain.cu
phase_oscillator_chain : phase_oscillator_chain.co
	$(CC) -o phase_oscillator_chain $(LDFLAGS) $(LDLIBS) phase_oscillator_chain.co

phase_oscillator_ensemble.co : phase_oscillator_ensemble.cu
phase_oscillator_ensemble : phase_oscillator_ensemble.co
	$(CC) -o phase_oscillator_ensemble $(LDFLAGS) $(LDLIBS) phase_oscillator_ensemble.co

lorenz_parameters : lorenz_parameters.co
	$(CC) -o lorenz_parameters $(LDFLAGS) $(LDLIBS) lorenz_parameters.co
lorenz_parameters.co : lorenz_parameters.cu

relaxation : relaxation.co
	$(CC) -o relaxation $(LDFLAGS) $(LDLIBS) relaxation.co
relaxation.co : relaxation.cu

clean :
	-rm *~ *.o *.co phase_oscillator_chain phase_oscillator_ensemble lorenz_parameters relaxation


text:
<?xml version="1.0" encoding="utf-8"?>
<Peach xmlns="http://phed.org/2008/Peach" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://phed.org/2008/Peach ../../../../../peach/peach.xsd">

	<!-- Import defaults for Peach instance -->
	<Include ns="default" src="file:defaults.xml" />

	<!-- Create a simple data template containing a single string -->
	<DataModel name="TheDataModel">
	</DataModel>

	<StateModel name="TheStateModel" initialState="Initial">
		<State name="Initial">
			<Action name="SendData" type="output">
				<DataModel ref="TheDataModel" />
			</Action>
		</State>
	</StateModel>

	<Agent name="LocalAgent" location="http://127.0.0.1:9000">
		<Monitor class="test.TestStopOnFirst" />
	</Agent>

	<Test name="TheTest">
		<Agent ref="LocalAgent"/>
		<StateModel ref="TheStateModel" />
		<Publisher class="tcp.Tcp">
			<Param name="host" value="127.0.0.1"/>
			<Param name="port" value="9001"/>
		</Publisher>
	</Test>

	<Run name="DefaultRun">
		<Test ref="TheTest" />

        <Logger class="logger.Filesystem">
            <Param name="path" value="c:\peach\logtest" />
        </Logger>
	</Run>

</Peach>
<!-- end -->


text:
<?php

/**
 * Product options default type block
 *
 * @category   Mage
 * @package    Mage_Catalog
 * @author     Magento Core Team <core@magentocommerce.com>
 */
class Mage_Catalog_Block_Product_View_Options_Type_Default
    extends Mage_Catalog_Block_Product_View_Options_Abstract
{

}


text:
#ifndef __PSCI_COMPAT_H__
#define __PSCI_COMPAT_H__

#include <arch.h>
#include <platform_def.h>

#ifndef __ASSEMBLY__
/*
 * The below declarations are to enable compatibility for the platform ports
 * using the old platform interface and psci helpers.
 */
#define PLAT_MAX_PWR_LVL	PLATFORM_MAX_AFFLVL
#define PLAT_NUM_PWR_DOMAINS	PLATFORM_NUM_AFFS

/*******************************************************************************
 * PSCI affinity related constants. An affinity instance could
 * be present or absent physically to cater for asymmetric topologies.
 ******************************************************************************/
#define PSCI_AFF_ABSENT		0x0
#define PSCI_AFF_PRESENT	0x1

#define PSCI_STATE_ON		0x0
#define PSCI_STATE_OFF		0x1
#define PSCI_STATE_ON_PENDING	0x2
#define PSCI_STATE_SUSPEND	0x3

/*
 * Using the compatibility platform interfaces means that the local states
 * used in psci_power_state_t need to only convey whether its power down
 * or standby state. The onus is on the platform port to do the right thing
 * including the state coordination in case multiple power down states are
 * involved. Hence if we assume 3 generic states viz, run, standby and
 * power down, we can assign 1 and 2 to standby and power down respectively.
 */
#define PLAT_MAX_RET_STATE	1
#define PLAT_MAX_OFF_STATE	2

/*
 * Macro to represent invalid affinity level within PSCI.
 */
#define PSCI_INVALID_DATA -1

#define psci_get_pstate_afflvl(pstate)		psci_get_pstate_pwrlvl(pstate)

/*
 * This array stores the 'power_state' requests of each CPU during
 * CPU_SUSPEND and SYSTEM_SUSPEND which will be populated by the
 * compatibility layer when appropriate platform hooks are invoked.
 */
extern unsigned int psci_power_state_compat[PLATFORM_CORE_COUNT];

/*******************************************************************************
 * Structure populated by platform specific code to export routines which
 * perform common low level pm functions
 ******************************************************************************/
typedef struct plat_pm_ops {
	void (*affinst_standby)(unsigned int power_state);
	int (*affinst_on)(unsigned long mpidr,
			  unsigned long sec_entrypoint,
			  unsigned int afflvl,
			  unsigned int state);
	void (*affinst_off)(unsigned int afflvl, unsigned int state);
	void (*affinst_suspend)(unsigned long sec_entrypoint,
			       unsigned int afflvl,
			       unsigned int state);
	void (*affinst_on_finish)(unsigned int afflvl, unsigned int state);
	void (*affinst_suspend_finish)(unsigned int afflvl,
				      unsigned int state);
	void (*system_off)(void) __dead2;
	void (*system_reset)(void) __dead2;
	int (*validate_power_state)(unsigned int power_state);
	int (*validate_ns_entrypoint)(unsigned long ns_entrypoint);
	unsigned int (*get_sys_suspend_power_state)(void);
} plat_pm_ops_t;

/*******************************************************************************
 * Function & Data prototypes to enable compatibility for older platform ports
 ******************************************************************************/
int psci_get_suspend_stateid_by_mpidr(unsigned long);
int psci_get_suspend_stateid(void);
int psci_get_suspend_powerstate(void);
unsigned int psci_get_max_phys_off_afflvl(void);
int psci_get_suspend_afflvl(void);

#endif /* ____ASSEMBLY__ */
#endif /* __PSCI_COMPAT_H__ */


text:
package org.testcontainers.containers;

import lombok.Getter;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
import org.testcontainers.utility.ComparableVersion;
import org.testcontainers.utility.DockerImageName;

import java.util.Collections;
import java.util.Optional;
import java.util.Set;

/**
 * Testcontainers implementation for InfluxDB.
 */
public class InfluxDBContainer<SELF extends InfluxDBContainer<SELF>> extends GenericContainer<SELF> {

    public static final Integer INFLUXDB_PORT = 8086;

    private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("influxdb");

    private static final String DEFAULT_TAG = "1.4.3";

    @Deprecated
    public static final String VERSION = DEFAULT_TAG;

    private static final int NO_CONTENT_STATUS_CODE = 204;

    @Getter
    private String username = "test-user";

    @Getter
    private String password = "test-password";

    /**
     * Properties of InfluxDB 1.x
     */
    private boolean authEnabled = true;

    private String admin = "admin";

    private String adminPassword = "password";

    @Getter
    private String database;

    /**
     * Properties of InfluxDB 2.x
     */
    @Getter
    private String bucket = "test-bucket";

    @Getter
    private String organization = "test-org";

    @Getter
    private Optional<String> retention = Optional.empty();

    @Getter
    private Optional<String> adminToken = Optional.empty();

    private final boolean isAtLeastMajorVersion2;

    /**
     * @deprecated use {@link InfluxDBContainer(DockerImageName)} instead
     */
    @Deprecated
    public InfluxDBContainer() {
        this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_TAG));
    }

    /**
     * @deprecated use {@link InfluxDBContainer(DockerImageName)} instead
     */
    @Deprecated
    public InfluxDBContainer(final String version) {
        this(DEFAULT_IMAGE_NAME.withTag(version));
    }

    public InfluxDBContainer(final DockerImageName dockerImageName) {
        super(dockerImageName);
        dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);

        this.waitStrategy =
            new HttpWaitStrategy()
                .forPath("/ping")
                .withBasicCredentials(this.username, this.password)
                .forStatusCode(NO_CONTENT_STATUS_CODE);

        this.isAtLeastMajorVersion2 =
            new ComparableVersion(dockerImageName.getVersionPart()).isGreaterThanOrEqualTo("2.0.0");
        addExposedPort(INFLUXDB_PORT);
    }

    /**
     * Sets the InfluxDB environment variables based on the version
     */
    @Override
    protected void configure() {
        if (this.isAtLeastMajorVersion2) {
            configureInfluxDBV2();
        } else {
            configureInfluxDBV1();
        }
    }

    /**
     * Sets the InfluxDB 2.x environment variables
     *
     * @see  InfluxDB Dockerhub  for full documentation on InfluxDB's
     * envrinoment variables
     */
    private void configureInfluxDBV2() {
        addEnv("DOCKER_INFLUXDB_INIT_MODE", "setup");

        addEnv("DOCKER_INFLUXDB_INIT_USERNAME", this.username);
        addEnv("DOCKER_INFLUXDB_INIT_PASSWORD", this.password);

        addEnv("DOCKER_INFLUXDB_INIT_ORG", this.organization);
        addEnv("DOCKER_INFLUXDB_INIT_BUCKET", this.bucket);

        this.retention.ifPresent(ret -> addEnv("DOCKER_INFLUXDB_INIT_RETENTION", ret));
        this.adminToken.ifPresent(token -> addEnv("DOCKER_INFLUXDB_INIT_ADMIN_TOKEN", token));
    }

    /**
     * Sets the InfluxDB 1.x environment variables
     */
    private void configureInfluxDBV1() {
        addEnv("INFLUXDB_USER", this.username);
        addEnv("INFLUXDB_USER_PASSWORD", this.password);

        addEnv("INFLUXDB_HTTP_AUTH_ENABLED", String.valueOf(this.authEnabled));

        addEnv("INFLUXDB_ADMIN_USER", this.admin);
        addEnv("INFLUXDB_ADMIN_PASSWORD", this.adminPassword);

        addEnv("INFLUXDB_DB", this.database);
    }

    @Override
    public Set<Integer> getLivenessCheckPortNumbers() {
        return Collections.singleton(getMappedPort(INFLUXDB_PORT));
    }

    /**
     * Set user for InfluxDB
     *
     * @param username The username to set for the system's initial super-user
     * @return a reference to this container instance
     */
    public InfluxDBContainer<SELF> withUsername(final String username) {
        this.username = username;
        return this;
    }

    /**
     * Set password for InfluxDB
     *
     * @param password The password to set for the system's initial super-user
     * @return a reference to this container instance
     */
    public InfluxDBContainer<SELF> withPassword(final String password) {
        this.password = password;
        return this;
    }

    /**
     * Determines if authentication should be enabled or not
     *
     * @param authEnabled Enables authentication.
     * @return a reference to this container instance
     */
    public InfluxDBContainer<SELF> withAuthEnabled(final boolean authEnabled) {
        this.authEnabled = authEnabled;
        return this.self();
    }

    /**
     * Sets the admin user
     *
     * @param admin The name of the admin user to be created. If this is unset, no admin user is created.
     * @return a reference to this container instance
     */
    public InfluxDBContainer<SELF> withAdmin(final String admin) {
        this.admin = admin;
        return this.self();
    }

    /**
     * Sets the admin password
     *
     * @param adminPassword The password for the admin user. If this is unset, a random password is generated and
     * printed to standard out.
     * @return a reference to this container instance
     */
    public InfluxDBContainer<SELF> withAdminPassword(final String adminPassword) {
        this.adminPassword = adminPassword;
        return this.self();
    }

    /**
     * Initializes database with given name
     *
     * @param database name of the database.
     * @return a reference to this container instance
     */
    public InfluxDBContainer<SELF> withDatabase(final String database) {
        this.database = database;
        return this.self();
    }

    /**
     * Sets the organization name
     *
     * @param organization The organization for the initial setup of influxDB.
     * @return a reference to this container instance
     */
    public InfluxDBContainer<SELF> withOrganization(final String organization) {
        this.organization = organization;
        return this;
    }

    /**
     * Initializes bucket with given name
     *
     * @param bucket name of the bucket.
     * @return a reference to this container instance
     */
    public InfluxDBContainer<SELF> withBucket(final String bucket) {
        this.bucket = bucket;
        return this;
    }

    /**
     * Sets the retention in days
     *
     * @param retention days bucket will retain data (0 is infinite, default is 0).
     * @return a reference to this container instance
     */
    public InfluxDBContainer<SELF> withRetention(final String retention) {
        this.retention = Optional.of(retention);
        return this;
    }

    /**
     * Sets the admin token
     *
     * @param adminToken Authentication token to associate with the admin user.
     * @return a reference to this container instance
     */
    public InfluxDBContainer<SELF> withAdminToken(final String adminToken) {
        this.adminToken = Optional.of(adminToken);
        return this;
    }

    /**
     * @return a url to InfluxDB
     */
    public String getUrl() {
        return "http://" + getHost() + ":" + getMappedPort(INFLUXDB_PORT);
    }

    /**
     * @return a InfluxDB client for InfluxDB 1.x.
     * @deprecated Use the new InfluxDB client library.
     */
    @Deprecated
    public InfluxDB getNewInfluxDB() {
        final InfluxDB influxDB = InfluxDBFactory.connect(getUrl(), this.username, this.password);
        influxDB.setDatabase(this.database);
        return influxDB;
    }
}


text:
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.protocol.dict;

import java.util.*;

import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.util.*;

import org.jitsi.service.resources.*;
import org.osgi.framework.*;

/**
 * Loads the Dict provider factory and registers its services in the OSGI
 * bundle context.
 *
 * @author ROTH Damien
 * @author LITZELMANN Cedric
 */
public class DictActivator
    implements BundleActivator
{
    private static final Logger logger = Logger.getLogger(DictActivator.class);

    /**
     * The currently valid bundle context.
     */
    private static BundleContext bundleContext = null;

    private ServiceRegistration dictPpFactoryServReg = null;
    private static ProtocolProviderFactoryDictImpl
                                 dictProviderFactory = null;

    private static ResourceManagementService resourceService;

    /**
     * Called when this bundle is started. In here we'll export the
     * dict ProtocolProviderFactory implementation so that it could be
     * possible to register accounts with it in SIP Communicator.
     *
     * @param context The execution context of the bundle being started.
     * @throws Exception If this method throws an exception, this bundle is
     *   marked as stopped and the Framework will remove this bundle's
     *   listeners, unregister all services registered by this bundle, and
     *   release all services used by this bundle.
     */
    public void start(BundleContext context)
        throws Exception
    {
        bundleContext = context;

        Hashtable<String,String> hashtable = new Hashtable<String,String>();
        hashtable.put(ProtocolProviderFactory.PROTOCOL, ProtocolNames.DICT);

        dictProviderFactory = new ProtocolProviderFactoryDictImpl();

        //reg the dict provider factory.
        dictPpFactoryServReg = context.registerService(
                    ProtocolProviderFactory.class.getName(),
                    dictProviderFactory,
                    hashtable);

        if (logger.isInfoEnabled())
            logger.info("DICT protocol implementation [STARTED].");
    }

    /**
     * Returns a reference to the bundle context that we were started with.
     * @return a reference to the BundleContext instance that we were started
     * witn.
     */
    public static BundleContext getBundleContext()
    {
        return bundleContext;
    }

    /**
     * Called when this bundle is stopped so the Framework can perform the
     * bundle-specific activities necessary to stop the bundle.
     *
     * @param context The execution context of the bundle being stopped.
     * @throws Exception If this method throws an exception, the bundle is
     *   still marked as stopped, and the Framework will remove the bundle's
     *   listeners, unregister all services registered by the bundle, and
     *   release all services used by the bundle.
     */
    public void stop(BundleContext context)
        throws Exception
    {

        dictProviderFactory.stop();
        dictPpFactoryServReg.unregister();

        if (logger.isInfoEnabled())
            logger.info("DICT protocol implementation [STOPPED].");
    }

    /**
     * Returns a reference to the protocol provider factory that we have
     * registered.
     * @return a reference to the <tt>ProtocolProviderFactoryDictImpl</tt>
     * instance that we have registered from this package.
     */
    public static ProtocolProviderFactoryDictImpl getProtocolProviderFactory()
    {
        return dictProviderFactory;
    }

    /**
     * Returns the <tt>ResourceManagementService</tt>.
     *
     * @return the <tt>ResourceManagementService</tt>.
     */
    public static ResourceManagementService getResources()
    {
        if (resourceService == null)
        {
            ServiceReference serviceReference = bundleContext
                .getServiceReference(ResourceManagementService.class.getName());

            if(serviceReference == null)
                return null;

            resourceService = (ResourceManagementService) bundleContext
                .getService(serviceReference);
        }

        return resourceService;
    }
}


text:
#include <aws/securityhub/model/RuleGroupSourceStatelessRuleMatchAttributesSources.h>
#include <aws/core/utils/json/JsonSerializer.h>

#include <utility>

using namespace Aws::Utils::Json;
using namespace Aws::Utils;

namespace Aws
{
namespace SecurityHub
{
namespace Model
{

RuleGroupSourceStatelessRuleMatchAttributesSources::RuleGroupSourceStatelessRuleMatchAttributesSources() :
    m_addressDefinitionHasBeenSet(false)
{
}

RuleGroupSourceStatelessRuleMatchAttributesSources::RuleGroupSourceStatelessRuleMatchAttributesSources(JsonView jsonValue) :
    m_addressDefinitionHasBeenSet(false)
{
  *this = jsonValue;
}

RuleGroupSourceStatelessRuleMatchAttributesSources& RuleGroupSourceStatelessRuleMatchAttributesSources::operator =(JsonView jsonValue)
{
  if(jsonValue.ValueExists("AddressDefinition"))
  {
    m_addressDefinition = jsonValue.GetString("AddressDefinition");

    m_addressDefinitionHasBeenSet = true;
  }

  return *this;
}

JsonValue RuleGroupSourceStatelessRuleMatchAttributesSources::Jsonize() const
{
  JsonValue payload;

  if(m_addressDefinitionHasBeenSet)
  {
   payload.WithString("AddressDefinition", m_addressDefinition);

  }

  return payload;
}

} // namespace Model
} // namespace SecurityHub
} // namespace Aws


text:
<?php

declare(strict_types=1);

namespace Inowas\Tool\Model\Event;

use Inowas\Common\Id\UserId;
use Inowas\Tool\Model\ToolId;
use Inowas\Tool\Model\ToolType;
use Prooph\EventSourcing\AggregateChanged;

/** @noinspection LongInheritanceChainInspection */
class ToolInstanceWasCreated extends AggregateChanged
{

    /** @var ToolId */
    private $id;

    /** @var UserId */
    private $userId;

    /** @var  ToolType */
    private $type;

    /** @noinspection MoreThanThreeArgumentsInspection
     * @param ToolId $id
     * @param UserId $userId
     * @param ToolType $type
     * @return ToolInstanceWasCreated
     */
    public static function withParameters(
        ToolId $id,
        UserId $userId,
        ToolType $type
    ): ToolInstanceWasCreated
    {
        /** @var ToolInstanceWasCreated $event */
        $event = self::occur($id->toString(),[
            'user_id' => $userId->toString(),
            'type' => $type->toString()
        ]);

        $event->id = $id;
        $event->userId = $userId;
        $event->type = $type;

        return $event;
    }

    public function id(): ToolId
    {
        if ($this->id === null){
            $this->id = ToolId::fromString($this->aggregateId());
        }

        return $this->id;
    }

    public function userId(): UserId
    {
        if ($this->userId === null){
            $this->userId = UserId::fromString($this->payload['user_id']);
        }

        return $this->userId;
    }

    public function type(): ToolType
    {
        if ($this->type === null){
            $this->type = ToolType::fromString($this->payload['type']);
        }

        return $this->type;
    }
}


text:
/**
 * ag-grid - Advanced Data Grid / Data Table supporting Javascript / React / AngularJS / Web Components
 * @version v3.3.2
 * @link http://www.ag-grid.com/
 * @license MIT
 */
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var vElement_1 = require("./vElement");
var VWrapperElement = (function (_super) {
    __extends(VWrapperElement, _super);
    function VWrapperElement(wrappedElement) {
        _super.call(this);
        this.wrappedElement = wrappedElement;
    }
    VWrapperElement.prototype.toHtmlString = function () {
        return '<span v_element_id="' + this.getId() + '"></span>';
    };
    VWrapperElement.prototype.elementAttached = function (element) {
        var parent = element.parentNode;
        parent.insertBefore(this.wrappedElement, element);
        parent.removeChild(element);
    };
    return VWrapperElement;
})(vElement_1.default);
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = VWrapperElement;


text:
'use strict';

angular.module('app')

.controller('CustomerImportCtrl', function($scope, $state, $filter, $stateParams, $localStorage, CustomerSvc, FieldSvc) {
    var o = $scope,
        translator = {};
    o.itemsByPage = 4;
    o.csvTemplate = [];
    o.csvHeader = [];
    FieldSvc.get().then(function(res) {
        if (res.data.length > 0) {
            var obj = {};
            var fields = res.data[0].fields;
            for (var i in fields) {
                var x = fields[i];
                translator[x.label] = x.id;
                o.csvHeader.push(x.label);
                if (x.type === "date")
                    obj[x.label] = $filter('date')(new Date(), 'yyyy/MM/dd');
                else
                    obj[x.label] = "text";
            }
            o.csvTemplate.push(obj)
        }
    });

    o.upload = function() {
        var temp = angular.copy(o.displayed);
        var data = [];
        for (var i in temp) {
            var obj = {};
            for (var j in temp[i]) {
                obj[translator[j]] = temp[i][j];
                obj.companyId = $localStorage.company.id;
            }
            data.push(obj);
        }
        CustomerSvc.create(data).then(function(res) {
			toaster.pop('success', 'Unggah Data Pelanggan', 'Berhasil mengunggah data pelanggan');
            $state.go('customer');
        }, function(res) {
			toaster.pop('error', 'Unggah Data Pelanggan', 'Gagal mengunggah data pelanggan');
        })
    }
});


text:
<?php

namespace Doctrine\DBAL\Driver\OCI8;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Schema\OracleSchemaManager;

/**
 * A Doctrine DBAL driver for the Oracle OCI8 PHP extensions.
 *
 * @author Roman Borschel <roman@code-factory.org>
 * @since 2.0
 */
class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver
{
    /**
     * {@inheritdoc}
     */
    public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
    {
        try {
            return new OCI8Connection(
                $username,
                $password,
                $this->_constructDsn($params),
                isset($params['charset']) ? $params['charset'] : null,
                isset($params['sessionMode']) ? $params['sessionMode'] : OCI_DEFAULT,
                isset($params['persistent']) ? $params['persistent'] : false
            );
        } catch (OCI8Exception $e) {
            throw DBALException::driverException($this, $e);
        }
    }

    /**
     * Constructs the Oracle DSN.
     *
     * @param array $params
     *
     * @return string The DSN.
     */
    protected function _constructDsn(array $params)
    {
        $dsn = '';
        if (isset($params['host']) && $params['host'] != '') {
            $dsn .= '(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)' .
                   '(HOST=' . $params['host'] . ')';

            if (isset($params['port'])) {
                $dsn .= '(PORT=' . $params['port'] . ')';
            } else {
                $dsn .= '(PORT=1521)';
            }

            $serviceName = $params['dbname'];

            if ( ! empty($params['servicename'])) {
                $serviceName = $params['servicename'];
            }

            $service = 'SID=' . $serviceName;
            $pooled   = '';

            if (isset($params['service']) && $params['service'] == true) {
                $service = 'SERVICE_NAME=' . $serviceName;
            }

            if (isset($params['pooled']) && $params['pooled'] == true) {
                $pooled = '(SERVER=POOLED)';
            }

            $dsn .= '))(CONNECT_DATA=(' . $service . ')' . $pooled . '))';
        } else {
            $dsn .= $params['dbname'];
        }

        return $dsn;
    }

    /**
     * {@inheritdoc}
     */
    public function getDatabasePlatform()
    {
        return new OraclePlatform();
    }

    /**
     * {@inheritdoc}
     */
    public function getSchemaManager(Connection $conn)
    {
        return new OracleSchemaManager($conn);
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'oci8';
    }

    /**
     * {@inheritdoc}
     */
    public function getDatabase(Connection $conn)
    {
        $params = $conn->getParams();

        return $params['user'];
    }

    /**
     * {@inheritdoc}
     */
    public function convertExceptionCode(\Exception $exception)
    {
        switch ($exception->getCode()) {
            case '1':
            case '2299':
            case '38911':
                return DBALException::ERROR_DUPLICATE_KEY;

            case '904':
                return DBALException::ERROR_BAD_FIELD_NAME;

            case '918':
            case '960':
                return DBALException::ERROR_NON_UNIQUE_FIELD_NAME;

            case '923':
                return DBALException::ERROR_SYNTAX;

            case '942':
                return DBALException::ERROR_UNKNOWN_TABLE;

            case '955':
                return DBALException::ERROR_TABLE_ALREADY_EXISTS;

            case '1017':
            case '12545':
                return DBALException::ERROR_ACCESS_DENIED;

            case '1400':
                return DBALException::ERROR_NOT_NULL;

            case '2292':
                return DBALException::ERROR_FOREIGN_KEY_CONSTRAINT;
        }

        return 0;
    }
}


text:
replacer
========

Replacer is a simple console application that does two way text replacement based on configuration files.

#Usage
The application is run via the following command:
```
./replacer {options-file.json} {destination}
```

The options file contains the replacement rules for a given replacement operation.

```
{
  "Brand": "SampleBrand",
  "Token": "%%",
  "DefaultDirection": "reverse",
  "Filters": [
    { "Type": "Directory", "filterPattern" : "test" },
    { "Type": "Directory", "filterPattern" : "code" },
    { "Type": "File", "filterPattern" : "cs" },
    { "Type": "File", "filterPattern" : "txt" }
  ],
  "Values": [
    { "Source": "CompanyName", "value": "Mega Corp" },
    { "Source": "Copyright", "value": "Copyright 2012.  All Rights Reserved." }
  ]
}
```

**Brand**

The brand is used purely for display purposes at this time.

**Token**

The token represents the start and end blocks for a given replacement.  When the above configuration file is used, `%%CompanyName%%` gets replaced with `Mega Corp`.

**DefaultDirection**

There are two valid options:

1. forward
2. reverse

Forward does the replacement as you would expect.  `%%CompanyName%%` gets replaced with `Mega Corp`.  Reverse attempts to undo the replacement.  It searches for all instances of `Mega Corp` and inserts `%%CompanyName%%`.

Reverse direction can cause issues when the replacement value (Mega Corp in this case) isn't very distinct or multiple source rules have the same value.

**Filters**

There are two types of filters:

1. Directory
2. File

Directory filters are exclusion based.  `{ "Type": "Directory", "filterPattern" : "test" }` will exclude any directory named `test`.

File filters are inclusive.  If the filter pattern is set to `"filterPattern" : "cs"`, only files with a `.cs` extension will be replaced.  In the above example, all `.cs` and `.txt` files will have replacements made except for directories named `test` or `code`.

**Values**

The values are the actual replacement rules to be run.  The above example will replace the `%%CompanyName%%` and `%%Copyright%%` placeholders with their respective values.


text:
module.exports = {
	port: 8888,
	secrets: {
		cookie: 'Super Secret Cookie String',
		session: 'Super Secret Session String'
	},
	devMode: true
};


text:
import setuptools

setuptools.setup(
  name = 'pykafka',
  version = '2.4.3',
  license = 'MIT',
  description = open('README.md').read(),
  author = "Datadog, Inc.",
  author_email = "packages@datadoghq.com",
  url = 'https://github.com/datadog/pykafka',
  platforms = 'any',
  packages = ['kafka'],
  zip_safe = True,
  verbose = False,
)


text:
import React, { Component } from "react";

import { ModalBody } from "backstage-modal";
import ModalPluginItem from "./ModalPluginItem";

export default class ModalPluginList extends Component {
  constructor(props) {
    super(props);
    this.modalClose = this.modalClose.bind(this);
    this.onChange = this.onChange.bind(this);
  }

  modalClose() {
    this.props.toggleModalVisibility();
  }

  onChange() {
    this.props.toggleModalVisibility();
    this.props.onChange(...arguments);
  }

  render() {
    return (
      <ModalBody>
        <ModalPluginItem
          toggleModalVisibility={this.modalClose}
          plugins={this.props.plugins}
          onChange={this.onChange}
          editorState={this.props.editorState}
        />
      </ModalBody>
    );
  }
}


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<title>application Class Hierarchy</title>
<meta name="date" content="2015-04-17">
<link rel="stylesheet" type="text/css" href="../stylesheet.css" title="Style">

JavaScript is disabled on your browser.

Skip navigation links

Package

Class

Use

Tree

Deprecated

Index

Help

Prev

Next

Frames

No Frames

All Classes

Hierarchy For Package application

Class Hierarchy

java.lang.Object

application.AABB

javafx.application.Application

application.MainApp

application.Camera

application.Config

application.ConfigController

application.ConfigSaver

application.CustomObject

application.FPSSystem

application.MainGUIController

application.MainSimulation

application.ConfigCreatorSim (implements java.lang.Runnable)

application.Material

application.ObjectInit

application.ObjImporter

application.Position

application.Vertex

application.Renderer (implements javax.media.opengl.GLEventListener)

application.Rotation

java.lang.Thread (implements java.lang.Runnable)

application.PhysicsSimulation

application.ConfigSimulation

application.TriangularFace

Skip navigation links

Package

Class

Use

Tree

Deprecated

Index

Help

Prev

Next

Frames

No Frames

All Classes


text:
package downwardapi

import (
	"encoding/json"
	"fmt"
	"os"
	"strconv"

	"k8s.io/test-infra/prow/kube"
)

// JobSpec is the full downward API that we expose to
// jobs that realize a ProwJob. We will provide this
// data to jobs with environment variables in two ways:
//  - the full spec, in serialized JSON in one variable
//  - individual fields of the spec in their own variables
type JobSpec struct {
	Type      kube.ProwJobType `json:"type,omitempty"`
	Job       string           `json:"job,omitempty"`
	BuildId   string           `json:"buildid,omitempty"`
	ProwJobId string           `json:"prowjobid,omitempty"`

	Refs kube.Refs `json:"refs,omitempty"`

	// we need to keep track of the agent until we
	// migrate everyone away from using the $BUILD_NUMBER
	// environment variable
	agent kube.ProwJobAgent
}

func NewJobSpec(spec kube.ProwJobSpec, buildId, prowJobId string) JobSpec {
	refs := kube.Refs{}
	if spec.Refs != nil {
		refs = *spec.Refs
	}

	return JobSpec{
		Type:      spec.Type,
		Job:       spec.Job,
		BuildId:   buildId,
		ProwJobId: prowJobId,
		Refs:      refs,
		agent:     spec.Agent,
	}
}

// ResolveSpecFromEnv will determine the Refs being
// tested in by parsing Prow environment variable contents
func ResolveSpecFromEnv() (*JobSpec, error) {
	specEnv, ok := os.LookupEnv(JobSpecEnv)
	if !ok {
		return nil, fmt.Errorf("$%s unset", JobSpecEnv)
	}

	spec := &JobSpec{}
	if err := json.Unmarshal([]byte(specEnv), spec); err != nil {
		return nil, fmt.Errorf("malformed $%s: %v", JobSpecEnv, err)
	}

	return spec, nil
}

const (
	JobNameEnv   = "JOB_NAME"
	JobSpecEnv   = "JOB_SPEC"
	JobTypeEnv   = "JOB_TYPE"
	ProwJobIdEnv = "PROW_JOB_ID"

	BuildIdEnv = "BUILD_ID"
	// Deprecated, will be removed in the future.
	ProwBuildIdEnv = "BUILD_NUMBER"
	// Deprecated, will be removed in the future.
	JenkinsBuildIdEnv = "buildId"

	RepoOwnerEnv   = "REPO_OWNER"
	RepoNameEnv    = "REPO_NAME"
	PullBaseRefEnv = "PULL_BASE_REF"
	PullBaseShaEnv = "PULL_BASE_SHA"
	PullRefsEnv    = "PULL_REFS"
	PullNumberEnv  = "PULL_NUMBER"
	PullPullShaEnv = "PULL_PULL_SHA"
)

// EnvForSpec returns a mapping of environment variables
// to their values that should be available for a job spec
func EnvForSpec(spec JobSpec) (map[string]string, error) {
	env := map[string]string{
		JobNameEnv:   spec.Job,
		BuildIdEnv:   spec.BuildId,
		ProwJobIdEnv: spec.ProwJobId,
		JobTypeEnv:   string(spec.Type),
	}

	// for backwards compatibility, we provide the build ID
	// in both $BUILD_ID and $BUILD_NUMBER for Prow agents
	// and in both $buildId and $BUILD_NUMBER for Jenkins
	if spec.agent == kube.KubernetesAgent {
		env[ProwBuildIdEnv] = spec.BuildId
	} else if spec.agent == kube.JenkinsAgent {
		env[JenkinsBuildIdEnv] = spec.BuildId
	}

	raw, err := json.Marshal(spec)
	if err != nil {
		return env, fmt.Errorf("failed to marshal job spec: %v", err)
	}
	env[JobSpecEnv] = string(raw)

	if spec.Type == kube.PeriodicJob {
		return env, nil
	}
	env[RepoOwnerEnv] = spec.Refs.Org
	env[RepoNameEnv] = spec.Refs.Repo
	env[PullBaseRefEnv] = spec.Refs.BaseRef
	env[PullBaseShaEnv] = spec.Refs.BaseSHA
	env[PullRefsEnv] = spec.Refs.String()

	if spec.Type == kube.PostsubmitJob || spec.Type == kube.BatchJob {
		return env, nil
	}
	env[PullNumberEnv] = strconv.Itoa(spec.Refs.Pulls[0].Number)
	env[PullPullShaEnv] = spec.Refs.Pulls[0].SHA
	return env, nil
}

func EnvForType(jobType kube.ProwJobType) []string {
	baseEnv := []string{JobNameEnv, JobSpecEnv, JobTypeEnv, ProwJobIdEnv, BuildIdEnv, ProwBuildIdEnv, JenkinsBuildIdEnv}
	refsEnv := []string{RepoOwnerEnv, RepoNameEnv, PullBaseRefEnv, PullBaseShaEnv, PullRefsEnv}
	pullEnv := []string{PullNumberEnv, PullPullShaEnv}

	switch jobType {
	case kube.PeriodicJob:
		return baseEnv
	case kube.PostsubmitJob, kube.BatchJob:
		return append(baseEnv, refsEnv...)
	case kube.PresubmitJob:
		return append(append(baseEnv, refsEnv...), pullEnv...)
	default:
		return []string{}
	}
}


text:
package main

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"github.com/bradfitz/gomemcache/memcache"
)

type User struct {
	UserId    int    `json:"user_id,omitempty"`
	UserName  string `json:"user_name"`
	UserLogin string `json:"user_login,omitempty"`
	UserToken string `json:"user_token,omitempty"`
}

func NewMemcachedApi() *MemcachedApi {

	return &MemcachedApi{
		memcache: memcache.New("127.0.0.1:3000"),
	}
}

type MemcachedApi struct {
	memcache *memcache.Client
}

func (api *MemcachedApi) GetUserById(userId int) (*memcache.Item, error) {

	return api.memcache.Get(command("GetUserById", userId))
}

func (api *MemcachedApi) GetUserByTwoParams(login string, userId int) (*memcache.Item, error) {

	return api.memcache.Get(command("GetUserByTwoParams", login, userId))
}

func (api *MemcachedApi) GetAuthUser(token string) (*memcache.Item, error) {

	return api.memcache.Get(command("GetAuthUser", token))
}

func (api *MemcachedApi) Cast() (*memcache.Item, error) {

	return api.memcache.Get(command("Cast", 42, 3.14159265359, "Hello"))
}

func (api *MemcachedApi) MultiGet() (map[string]*memcache.Item, error) {

	return api.memcache.GetMulti([]string{
		command("GetUserById", 42),
		command("GetAuthUser", "token"),
	},
	)
}

func (api *MemcachedApi) ReturnError() (*memcache.Item, error) {

	return api.memcache.Get(command("ReturnError"))
}

func (api *MemcachedApi) NotFoundmethod() (*memcache.Item, error) {

	return api.memcache.Get(command("notFoundmethod"))
}

func (api *MemcachedApi) GetUserWhereIdIn() (*memcache.Item, error) {

	return api.memcache.Get(command("GetUserWhereIdIn", []int{1, 2, 3, 4}))
}

func (api *MemcachedApi) SetUser() error {

	value, _ := json.Marshal(&User{UserId: 42, UserName: "New User", UserLogin: "new_login"})

	return api.memcache.Set(&memcache.Item{Key: "SetUser", Value: value})
}

func (api *MemcachedApi) Increment(delta uint64) (uint64, error) {

	return api.memcache.Increment("Increment", delta)
}

func (api *MemcachedApi) Decrement(delta uint64) (uint64, error) {

	return api.memcache.Decrement("Decrement", delta)
}

func (api *MemcachedApi) Delete() error {

	return api.memcache.Delete(command("Delete", 42))
}

func command(method string, params ...interface{}) string {

	jsonParams, _ := json.Marshal(params)

	return fmt.Sprintf("%s:%s", method, base64.StdEncoding.EncodeToString(jsonParams))
}

func main() {

	api := NewMemcachedApi()

	item, _ := api.GetUserById(42)

	fmt.Println(string(item.Value))

	item, _ = api.GetUserByTwoParams("Login", 42)

	fmt.Println(string(item.Value))

	item, _ = api.GetAuthUser("token")

	fmt.Println(string(item.Value))

	item, _ = api.Cast()

	fmt.Println(string(item.Value))

	item, _ = api.NotFoundmethod()

	fmt.Println(string(item.Value))

	items, _ := api.MultiGet()

	for k, item := range items {

		fmt.Println(k, string(item.Value))
	}

	item, _ = api.ReturnError()

	fmt.Println(string(item.Value))

	item, _ = api.GetUserWhereIdIn()

	fmt.Println(string(item.Value))

	fmt.Println(api.SetUser())

	for i := 1; i <= 5; i++ {

		newValue, _ := api.Increment(uint64(i))

		fmt.Println(newValue)
	}

	for i := 1; i <= 5; i++ {

		newValue, _ := api.Decrement(uint64(i))

		fmt.Println(newValue)
	}

	fmt.Println(api.Delete())
}


text:
package it.unimi.di.law.warc.tool;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// RELEASE-STATUS: DIST

import com.martiansoftware.jsap.FlaggedOption;
import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPException;
import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.Parameter;
import com.martiansoftware.jsap.SimpleJSAP;
import com.martiansoftware.jsap.UnflaggedOption;

import it.unimi.di.law.warc.io.CompressedWarcWriter;
import it.unimi.di.law.warc.io.UncompressedWarcReader;
import it.unimi.di.law.warc.io.WarcReader;
import it.unimi.di.law.warc.io.WarcWriter;
import it.unimi.di.law.warc.io.gzarc.GZIPArchive;
import it.unimi.di.law.warc.records.WarcRecord;
import it.unimi.dsi.fastutil.io.FastBufferedInputStream;
import it.unimi.dsi.fastutil.io.FastBufferedOutputStream;
import it.unimi.dsi.logging.ProgressLogger;

/**
 * A tool to compress a WARC file adding the extra GZIP header fields required
 * by the {@link GZIPArchive} format used by compressed WARC files.
 */
public class WarcCompressor {

    private static final Logger LOGGER = LoggerFactory.getLogger(WarcCompressor.class);

    public static void main(String arg[]) throws IOException, InterruptedException, JSAPException {

	SimpleJSAP jsap = new SimpleJSAP(WarcCompressor.class.getName(),
		"Given a store uncompressed, write a compressed store.",
		new Parameter[] { new FlaggedOption("output", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.REQUIRED, 'o', "output", "The output filename  (- for stdout)."),
		new UnflaggedOption("store", JSAP.STRING_PARSER, JSAP.NOT_REQUIRED, "The name of the store (if omitted, stdin)."),
	});

	JSAPResult jsapResult = jsap.parse(arg);
	if (jsap.messagePrinted()) return;

	final InputStream in = jsapResult.userSpecified("store") ? new FastBufferedInputStream(new FileInputStream(jsapResult.getString("store"))) : System.in;

	final WarcReader reader = new UncompressedWarcReader(in);
	final ProgressLogger pl = new ProgressLogger(LOGGER, 1, TimeUnit.MINUTES, "records");
	final String output = jsapResult.getString("output");

	PrintStream out = "-".equals(output) ? System.out : new PrintStream(new FastBufferedOutputStream(new FileOutputStream(output)), false, "UTF-8");
	final WarcWriter writer = new CompressedWarcWriter(out);

	pl.itemsName = "records";
	pl.displayFreeMemory = true;
	pl.displayLocalSpeed = true;
	pl.start("Scanning...");

	for (long storePosition = 0;; storePosition++) {
	    LOGGER.trace("STOREPOSITION " + storePosition);
	    WarcRecord record = null;
	    try {
		record = reader.read();
	    } catch (Exception e) {
		LOGGER.error("Exception while reading record " + storePosition + " ");
		LOGGER.error(e.getMessage());
		e.printStackTrace();
		continue;
	    }
	    if (record == null)
		break;
	    writer.write(record);
	    pl.lightUpdate();
	}
	pl.done();
	writer.close();
    }

}


text:
package ru.xxlabaza.test.ping.cli;

import com.beust.jcommander.IParameterValidator;
import com.beust.jcommander.ParameterException;
import lombok.val;
import ru.xxlabaza.test.ping.localization.I18nExceptionUtil;

/**
 * Application specific option validator, It guarantees that option
 * will be greater than zero.
 * <p>
 * Producing exception message is localized via program's resource bundle.
 *
 * @author Artem Labazin <xxlabaza@gmail.com>
 * @since 25.06.2017
 */
public class GreaterThanZeroValidator implements IParameterValidator {

    @Override
    public void validate (String name, String value) throws ParameterException {
        val number = Integer.parseInt(value);
        if (number > 0) {
            return;
        }

        val message = I18nExceptionUtil.getMessage("cli.validation.GreaterThanZero", name, value);
        throw new ParameterException(message);
    }
}


text:
package com.sivalabs.bootredisdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootRedisDemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootRedisDemoApplication.class, args);
	}
}


text:
"""Test data purging."""
from datetime import datetime, timedelta
import json

from homeassistant.components import recorder
from homeassistant.components.recorder.const import DATA_INSTANCE
from homeassistant.components.recorder.models import Events, RecorderRuns, States
from homeassistant.components.recorder.purge import purge_old_data
from homeassistant.components.recorder.util import session_scope
from homeassistant.util import dt as dt_util

from .common import wait_recording_done

from tests.async_mock import patch

def test_purge_old_states(hass, hass_recorder):
    """Test deleting old states."""
    hass = hass_recorder()
    _add_test_states(hass)

    # make sure we start with 6 states
    with session_scope(hass=hass) as session:
        states = session.query(States)
        assert states.count() == 6

        # run purge_old_data()
        finished = purge_old_data(hass.data[DATA_INSTANCE], 4, repack=False)
        assert not finished
        assert states.count() == 4

        finished = purge_old_data(hass.data[DATA_INSTANCE], 4, repack=False)
        assert not finished
        assert states.count() == 2

        finished = purge_old_data(hass.data[DATA_INSTANCE], 4, repack=False)
        assert finished
        assert states.count() == 2

def test_purge_old_events(hass, hass_recorder):
    """Test deleting old events."""
    hass = hass_recorder()
    _add_test_events(hass)

    with session_scope(hass=hass) as session:
        events = session.query(Events).filter(Events.event_type.like("EVENT_TEST%"))
        assert events.count() == 6

        # run purge_old_data()
        finished = purge_old_data(hass.data[DATA_INSTANCE], 4, repack=False)
        assert not finished
        assert events.count() == 4

        finished = purge_old_data(hass.data[DATA_INSTANCE], 4, repack=False)
        assert not finished
        assert events.count() == 2

        # we should only have 2 events left
        finished = purge_old_data(hass.data[DATA_INSTANCE], 4, repack=False)
        assert finished
        assert events.count() == 2

def test_purge_method(hass, hass_recorder):
    """Test purge method."""
    hass = hass_recorder()
    service_data = {"keep_days": 4}
    _add_test_events(hass)
    _add_test_states(hass)
    _add_test_recorder_runs(hass)

    # make sure we start with 6 states
    with session_scope(hass=hass) as session:
        states = session.query(States)
        assert states.count() == 6

        events = session.query(Events).filter(Events.event_type.like("EVENT_TEST%"))
        assert events.count() == 6

        recorder_runs = session.query(RecorderRuns)
        assert recorder_runs.count() == 7

        hass.data[DATA_INSTANCE].block_till_done()
        wait_recording_done(hass)

        # run purge method - no service data, use defaults
        hass.services.call("recorder", "purge")
        hass.block_till_done()

        # Small wait for recorder thread
        hass.data[DATA_INSTANCE].block_till_done()
        wait_recording_done(hass)

        # only purged old events
        assert states.count() == 4
        assert events.count() == 4

        # run purge method - correct service data
        hass.services.call("recorder", "purge", service_data=service_data)
        hass.block_till_done()

        # Small wait for recorder thread
        hass.data[DATA_INSTANCE].block_till_done()
        wait_recording_done(hass)

        # we should only have 2 states left after purging
        assert states.count() == 2

        # now we should only have 2 events left
        assert events.count() == 2

        # now we should only have 3 recorder runs left
        assert recorder_runs.count() == 3

        assert not ("EVENT_TEST_PURGE" in (event.event_type for event in events.all()))

        # run purge method - correct service data, with repack
        with patch("homeassistant.components.recorder.purge._LOGGER") as mock_logger:
            service_data["repack"] = True
            hass.services.call("recorder", "purge", service_data=service_data)
            hass.block_till_done()
            hass.data[DATA_INSTANCE].block_till_done()
            wait_recording_done(hass)
            assert (
                mock_logger.debug.mock_calls[5][1][0]
                == "Vacuuming SQL DB to free space"
            )

def _add_test_states(hass):
    """Add multiple states to the db for testing."""
    now = datetime.now()
    five_days_ago = now - timedelta(days=5)
    eleven_days_ago = now - timedelta(days=11)
    attributes = {"test_attr": 5, "test_attr_10": "nice"}

    hass.block_till_done()
    hass.data[DATA_INSTANCE].block_till_done()
    wait_recording_done(hass)

    with recorder.session_scope(hass=hass) as session:
        for event_id in range(6):
            if event_id < 2:
                timestamp = eleven_days_ago
                state = "autopurgeme"
            elif event_id < 4:
                timestamp = five_days_ago
                state = "purgeme"
            else:
                timestamp = now
                state = "dontpurgeme"

            session.add(
                States(
                    entity_id="test.recorder2",
                    domain="sensor",
                    state=state,
                    attributes=json.dumps(attributes),
                    last_changed=timestamp,
                    last_updated=timestamp,
                    created=timestamp,
                    event_id=event_id + 1000,
                )
            )

def _add_test_events(hass):
    """Add a few events for testing."""
    now = datetime.now()
    five_days_ago = now - timedelta(days=5)
    eleven_days_ago = now - timedelta(days=11)
    event_data = {"test_attr": 5, "test_attr_10": "nice"}

    hass.block_till_done()
    hass.data[DATA_INSTANCE].block_till_done()
    wait_recording_done(hass)

    with recorder.session_scope(hass=hass) as session:
        for event_id in range(6):
            if event_id < 2:
                timestamp = eleven_days_ago
                event_type = "EVENT_TEST_AUTOPURGE"
            elif event_id < 4:
                timestamp = five_days_ago
                event_type = "EVENT_TEST_PURGE"
            else:
                timestamp = now
                event_type = "EVENT_TEST"

            session.add(
                Events(
                    event_type=event_type,
                    event_data=json.dumps(event_data),
                    origin="LOCAL",
                    created=timestamp,
                    time_fired=timestamp,
                )
            )

def _add_test_recorder_runs(hass):
    """Add a few recorder_runs for testing."""
    now = datetime.now()
    five_days_ago = now - timedelta(days=5)
    eleven_days_ago = now - timedelta(days=11)

    hass.block_till_done()
    hass.data[DATA_INSTANCE].block_till_done()
    wait_recording_done(hass)

    with recorder.session_scope(hass=hass) as session:
        for rec_id in range(6):
            if rec_id < 2:
                timestamp = eleven_days_ago
            elif rec_id < 4:
                timestamp = five_days_ago
            else:
                timestamp = now

            session.add(
                RecorderRuns(
                    start=timestamp,
                    created=dt_util.utcnow(),
                    end=timestamp + timedelta(days=1),
                )
            )


text:
class BlueLightSpecialCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.string   :email,                :limit => 100
      t.string   :first_name,           :limit => 50
      t.string   :last_name,            :limit => 50
      t.string   :role,                 :limit => 50
      t.string   :encrypted_password,   :limit => 128
      t.string   :salt,                 :limit => 128
      t.string   :remember_token,       :limit => 128
      t.string   :facebook_uid,         :limit => 50
      t.string   :password_reset_token, :limit => 128
      t.timestamps
    end

    add_index :users, :email
    add_index :users, :remember_token
    add_index :users, :facebook_uid
  end

  def self.down
    drop_table :users
  end
end