text:
import os
import subprocess
import glob
import argparse
from argparse import ArgumentError
import logging as _logging
import multiprocessing
import re
import sys
import inspect
import threading
import webbrowser
from functools import partial

from snakemake.workflow import Workflow
from snakemake.exceptions import print_exception
from snakemake.logging import setup_logger, logger
from snakemake.version import __version__
from snakemake.io import load_configfile
from snakemake.shell import shell

__author__ = "Johannes Köster"

def snakemake(snakefile,
    listrules=False,
    list_target_rules=False,
    cores=1,
    nodes=1,
    resources=dict(),
    config=dict(),
    configfile=None,
    config_args=None,
    workdir=None,
    targets=None,
    dryrun=False,
    touch=False,
    forcetargets=False,
    forceall=False,
    forcerun=[],
    prioritytargets=[],
    stats=None,
    printreason=False,
    printshellcmds=False,
    printdag=False,
    printrulegraph=False,
    printd3dag=False,
    nocolor=False,
    quiet=False,
    keepgoing=False,
    cluster=None,
    drmaa=None,
    jobname="snakejob.{rulename}.{jobid}.sh",
    immediate_submit=False,
    standalone=False,
    ignore_ambiguity=False,
    snakemakepath=None,
    lock=True,
    unlock=False,
    cleanup_metadata=None,
    force_incomplete=False,
    ignore_incomplete=False,
    list_version_changes=False,
    list_code_changes=False,
    list_input_changes=False,
    list_params_changes=False,
    list_resources=False,
    summary=False,
    detailed_summary=False,
    latency_wait=3,
    benchmark_repeats=1,
    wait_for_files=None,
    print_compilation=False,
    debug=False,
    notemp=False,
    nodeps=False,
    keep_target_files=False,
    allowed_rules=None,
    jobscript=None,
    timestamp=False,
    greedyness=None,
    overwrite_shellcmd=None,
    updated_files=None,
    log_handler=None,
    keep_logger=False):
    """Run snakemake on a given snakefile.

    This function provides access to the whole snakemake functionality. It is not thread-safe.

    Args:
        snakefile (str):            the path to the snakefile
        listrules (bool):           list rules (default False)
        list_target_rules (bool):   list target rules (default False)
        cores (int):                the number of provided cores (ignored when using cluster support) (default 1)
        nodes (int):                the number of provided cluster nodes (ignored without cluster support) (default 1)
        resources (dict):           provided resources, a dictionary assigning integers to resource names, e.g. {gpu=1, io=5} (default {})
        config (dict):              override values for workflow config
        workdir (str):              path to working directory (default None)
        targets (list):             list of targets, e.g. rule or file names (default None)
        dryrun (bool):              only dry-run the workflow (default False)
        touch (bool):               only touch all output files if present (default False)
        forcetargets (bool):        force given targets to be re-created (default False)
        forceall (bool):            force all output files to be re-created (default False)
        forcerun (list):             list of files and rules that shall be re-created/re-executed (default [])
        prioritytargets (list):     list of targets that shall be run with maximum priority (default [])
        stats (str):                path to file that shall contain stats about the workflow execution (default None)
        printreason (bool):         print the reason for the execution of each job (default false)
        printshellcmds (bool):      print the shell command of each job (default False)
        printdag (bool):            print the dag in the graphviz dot language (default False)
        printrulegraph (bool):      print the graph of rules in the graphviz dot language (default False)
        printd3dag (bool):          print a D3.js compatible JSON representation of the DAG (default False)
        nocolor (bool):             do not print colored output (default False)
        quiet (bool):               do not print any default job information (default False)
        keepgoing (bool):           keep goind upon errors (default False)
        cluster (str):              submission command of a cluster or batch system to use, e.g. qsub (default None)
        drmaa (str):                if not None use DRMAA for cluster support, str specifies native args passed to the cluster when submitting a job
        jobname (str):              naming scheme for cluster job scripts (default "snakejob.{rulename}.{jobid}.sh")
        immediate_submit (bool):    immediately submit all cluster jobs, regardless of dependencies (default False)
        standalone (bool):          kill all processes very rudely in case of failure (do not use this if you use this API) (default False)
        ignore_ambiguity (bool):    ignore ambiguous rules and always take the first possible one (default False)
        snakemakepath (str):        path to the snakemake executable (default None)
        lock (bool):                lock the working directory when executing the workflow (default True)
        unlock (bool):              just unlock the working directory (default False)
        cleanup_metadata (bool):    just cleanup metadata of output files (default False)
        force_incomplete (bool):    force the re-creation of incomplete files (default False)
        ignore_incomplete (bool):   ignore incomplete files (default False)
        list_version_changes (bool): list output files with changed rule version (default False)
        list_code_changes (bool):   list output files with changed rule code (default False)
        list_input_changes (bool):  list output files with changed input files (default False)
        list_params_changes (bool): list output files with changed params (default False)
        summary (bool):             list summary of all output files and their status (default False)
        latency_wait (int):         how many seconds to wait for an output file to appear after the execution of a job, e.g. to handle filesystem latency (default 3)
        benchmark_repeats (int):    number of repeated runs of a job if declared for benchmarking (default 1)
        wait_for_files (list):      wait for given files to be present before executing the workflow
        list_resources (bool):      list resources used in the workflow (default False)
        summary (bool):             list summary of all output files and their status (default False). If no option  is specified a basic summary will be ouput. If 'detailed' is added as an option e.g --summary detailed, extra info about the input and shell commands will be included
        detailed_summary (bool):    list summary of all input and output files and their status (default False)
        print_compilation (bool):   print the compilation of the snakefile (default False)
        debug (bool):               show additional debug output (default False)
        notemp (bool):              ignore temp file flags, e.g. do not delete output files marked as temp after use (default False)
        nodeps (bool):              ignore dependencies (default False)
        keep_target_files (bool):   Do not adjust the paths of given target files relative to the working directory.
        allowed_rules (set):        Restrict allowed rules to the given set. If None or empty, all rules are used.
        jobscript (str):            path to a custom shell script template for cluster jobs (default None)
        timestamp (bool):           print time stamps in front of any output (default False)
        greedyness (float):         set the greedyness of scheduling. This value between 0 and 1 determines how careful jobs are selected for execution. The default value (0.5 if prioritytargets are used, 1.0 else) provides the best speed and still acceptable scheduling quality.
        overwrite_shellcmd (str):   a shell command that shall be executed instead of those given in the workflow. This is for debugging purposes only.
        updated_files(list):        a list that will be filled with the files that are updated or created during the workflow execution
        log_handler (function):      redirect snakemake output to this custom log handler, a function that takes a log message dictionary (see below) as its only argument (default None). The log message dictionary for the log handler has to following entries:

            :level:
                the log level ("info", "error", "debug", "progress", "job_info")

            :level="info", "error" or "debug":
                :msg:
                    the log message
            :level="progress":
                :done:
                    number of already executed jobs

                :total:
                    number of total jobs

            :level="job_info":
                :input:
                    list of input files of a job

                :output:
                    list of output files of a job

                :log:
                    path to log file of a job

                :local:
                    whether a job is executed locally (i.e. ignoring cluster)

                :msg:
                    the job message

                :reason:
                    the job reason

                :priority:
                    the job priority

                :threads:
                    the threads of the job

    Returns:
        bool:   True if workflow execution was successful.

    """

    if updated_files is None:
        updated_files = list()

    if cluster:
        cores = sys.maxsize
    else:
        nodes = sys.maxsize

    if not keep_logger:
        setup_logger(handler=log_handler, quiet=quiet, printreason=printreason, printshellcmds=printshellcmds, nocolor=nocolor, stdout=dryrun, debug=debug, timestamp=timestamp)

    if greedyness is None:
         greedyness = 0.5 if prioritytargets else 1.0
    else:
        if not (0 <= greedyness <= 1.0):
            logger.error("Error: greedyness must be a float between 0 and 1.")
            return False

    if not os.path.exists(snakefile):
        logger.error("Error: Snakefile \"{}\" not present.".format(snakefile))
        return False
    snakefile = os.path.abspath(snakefile)

    if cluster and (drmaa is not None):
        raise ValueError("cluster and drmaa args are mutually exclusive")

    overwrite_config = dict()
    if configfile:
        overwrite_config.update(load_configfile(configfile))
    if config:
        overwrite_config.update(config)

    if workdir:
        olddir = os.getcwd()
        if not os.path.exists(workdir):
            logger.info("Creating specified working directory {}.".format(workdir))
            os.makedirs(workdir)
        workdir = os.path.abspath(workdir)
        os.chdir(workdir)
    workflow = Workflow(
        snakefile=snakefile, snakemakepath=snakemakepath,
        jobscript=jobscript, overwrite_shellcmd=overwrite_shellcmd,
        overwrite_config=overwrite_config, overwrite_workdir=workdir,
        overwrite_configfile=configfile,
        config_args=config_args
    )

    if standalone:
        try:
            # set the process group
            os.setpgrp()
        except:
            # ignore: if it does not work we can still work without it
            pass

    success = True
    try:
        workflow.include(
            snakefile,
            overwrite_first_rule=True,
            print_compilation=print_compilation
        )
        workflow.check()

        if not print_compilation:
            if listrules:
                workflow.list_rules()
            elif list_target_rules:
                workflow.list_rules(only_targets=True)
            elif list_resources:
                workflow.list_resources()
            else:
                    # if not printdag and not printrulegraph:
                    # handle subworkflows
                    subsnakemake = partial(
                        snakemake,
                        cores=cores,
                        nodes=nodes,
                        resources=resources,
                        dryrun=dryrun,
                        touch=touch,
                        printreason=printreason,
                        printshellcmds=printshellcmds,
                        nocolor=nocolor,
                        quiet=quiet,
                        keepgoing=keepgoing,
                        cluster=cluster,
                        drmaa=drmaa,
                        jobname=jobname,
                        immediate_submit=immediate_submit,
                        standalone=standalone,
                        ignore_ambiguity=ignore_ambiguity,
                        snakemakepath=snakemakepath,
                        lock=lock,
                        unlock=unlock,
                        cleanup_metadata=cleanup_metadata,
                        force_incomplete=force_incomplete,
                        ignore_incomplete=ignore_incomplete,
                        latency_wait=latency_wait,
                        benchmark_repeats=benchmark_repeats,
                        debug=debug,
                        notemp=notemp,
                        nodeps=nodeps,
                        jobscript=jobscript,
                        timestamp=timestamp,
                        greedyness=greedyness,
                        overwrite_shellcmd=overwrite_shellcmd,
                        keep_logger=True
                    )
                    success = workflow.execute(
                        targets=targets, dryrun=dryrun, touch=touch,
                        cores=cores, nodes=nodes, forcetargets=forcetargets,
                        forceall=forceall, forcerun=forcerun,
                        prioritytargets=prioritytargets, quiet=quiet,
                        keepgoing=keepgoing, printshellcmds=printshellcmds,
                        printreason=printreason, printrulegraph=printrulegraph,
                        printdag=printdag, cluster=cluster, jobname=jobname,
                        drmaa=drmaa,
                        printd3dag=printd3dag,
                        immediate_submit=immediate_submit,
                        ignore_ambiguity=ignore_ambiguity,
                        stats=stats,
                        force_incomplete=force_incomplete,
                        ignore_incomplete=ignore_incomplete,
                        list_version_changes=list_version_changes,
                        list_code_changes=list_code_changes,
                        list_input_changes=list_input_changes,
                        list_params_changes=list_params_changes,
                        summary=summary,
                        latency_wait=latency_wait,
                        benchmark_repeats=benchmark_repeats,
                        wait_for_files=wait_for_files,
                        detailed_summary=detailed_summary,
                        nolock=not lock,
                        unlock=unlock,
                        resources=resources,
                        notemp=notemp,
                        nodeps=nodeps,
                        keep_target_files=keep_target_files,
                        cleanup_metadata=cleanup_metadata,
                        subsnakemake=subsnakemake,
                        updated_files=updated_files,
                        allowed_rules=allowed_rules,
                        greedyness=greedyness
                    )

    # BrokenPipeError is not present in Python 3.2, so lets wait until everbody uses > 3.2
    #except BrokenPipeError:
        # ignore this exception and stop. It occurs if snakemake output is piped into less and less quits before reading the whole output.
        # in such a case, snakemake shall stop scheduling and quit with error 1
    #    success = False
    except (Exception, BaseException) as ex:
        print_exception(ex, workflow.linemaps)
        success = False
    if workdir:
        os.chdir(olddir)
    if workflow.persistence:
        workflow.persistence.unlock()
    if not keep_logger:
        logger.cleanup()
    return success

def parse_resources(args):
    resources = dict()
    if args.resources is not None:
        valid = re.compile("[a-zA-Z_]\w*$")
        for res in args.resources:
            try:
                res, val = res.split("=")
            except ValueError:
                raise ValueError("Resources have to be defined as name=value pairs.")
            if not valid.match(res):
                raise ValueError("Resource definition must start with a valid identifier.")
            try:
                val = int(val)
            except ValueError:
                raise ValueError("Resource definiton must contain an integer after the identifier.")
            if res == "_cores":
                raise ValueError("Resource _cores is already defined internally. Use a different name.")
            resources[res] = val
    return resources

def parse_config(args):
    parsers = [int, float, eval, str]
    config = dict()
    if args.config is not None:
        valid = re.compile("[a-zA-Z_]\w*$")
        for entry in args.config:
            try:
                key, val = entry.split("=", 1)
            except ValueError:
                raise ValueError("Config entries have to be defined as name=value pairs.")
            if not valid.match(key):
                raise ValueError("Config entry must start with a valid identifier.")
            v = None
            for parser in parsers:
                try:
                    v = parser(val)
                    break
                except:
                    pass
            assert v is not None
            config[key] = v
    return config

def get_argument_parser():
    parser = argparse.ArgumentParser(
        description="Snakemake is a Python based language and execution "
            "environment for GNU Make-like workflows.")

    parser.add_argument(
        "target", nargs="*", default=None,
        help="Targets to build. May be rules or files.")
    parser.add_argument(
        "--snakefile", "-s", metavar="FILE",
        default="Snakefile", help="The workflow definition in a snakefile.")
    parser.add_argument(
        "--gui", nargs="?", const="8000", metavar="PORT", type=int,
        help="Serve an HTML based user interface to the given port "
        "(default: 8000). If possible, a browser window is opened.")
    parser.add_argument(
        "--cores", "--jobs", "-j", action="store", default=1,
        const=multiprocessing.cpu_count(), nargs="?", metavar="N", type=int,
        help=(
            "Use at most N cores in parallel (default: 1). "
            "If N is omitted, the limit is set to the number of "
            "available cores."))
    parser.add_argument(
        "--resources", "--res", nargs="*", metavar="NAME=INT",
        help=(
            "Define additional resources that shall constrain the scheduling "
            "analogously to threads (see above). A resource is defined as "
            "a name and an integer value. E.g. --resources gpu=1. Rules can "
            "use resources by defining the resource keyword, e.g. "
            "resources: gpu=1. If now two rules require 1 of the resource "
            "'gpu' they won't be run in parallel by the scheduler."))
    parser.add_argument(
        "--config", nargs="*", metavar="KEY=VALUE",
        help=(
            "Set or overwrite values in the workflow config object. "
            "The workflow config object is accessible as variable config inside "
            "the workflow. Default values can be set by providing a JSON file "
            "(see Documentation)."))
    parser.add_argument(
        "--configfile", metavar="JSON_FILE",
        help=(
            "Specify or overwrite the config file of the workflow (see the docs). "
            "Values specified in JSON format are available in the global config "
            "dictionary inside the workflow."
        )
    )
    parser.add_argument(
        "--list", "-l", action="store_true",
        help="Show availiable rules in given Snakefile.")
    parser.add_argument(
        "--list-target-rules", "--lt", action="store_true",
        help="Show available target rules in given Snakefile.")
    parser.add_argument(
        "--directory", "-d", metavar="DIR", action="store",
        help=(
            "Specify working directory (relative paths in "
            "the snakefile will use this as their origin)."))
    parser.add_argument(
        "--dryrun", "-n", action="store_true",
        help="Do not execute anything.")
    parser.add_argument(
        "--printshellcmds", "-p", action="store_true",
        help="Print out the shell commands that will be executed.")
    parser.add_argument(
        "--dag", action="store_true",
        help="Do not execute anything and print the directed "
            "acyclic graph of jobs in the dot language. Recommended "
            "use on Unix systems: snakemake --dag | dot | display")
    parser.add_argument(
        "--rulegraph", action="store_true",
        help="Do not execute anything and print the dependency graph "
            "of rules in the dot language. This will be less "
            "crowded than above DAG of jobs, but also show less information. "
            "Note that each rule is displayed once, hence the displayed graph will be "
            "cyclic if a rule appears in several steps of the workflow. "
            "Use this if above option leads to a DAG that is too large. "
            "Recommended use on Unix systems: snakemake --rulegraph | dot | display")
    parser.add_argument(
        "--d3dag", action="store_true",
        help="Print the DAG in D3.js compatible JSON format.")
    parser.add_argument(
        "--summary", "-S", action="store_true",
        help="Print a summary of all files created by the workflow. The "
        "has the following columns: filename, modification time, "
        "rule version, status, plan.\n"
        "Thereby rule version contains the version"
        "the file was created with (see the version keyword of rules), and "
        "status denotes whether the file is missing, its input files are "
        "newer or if version or implementation of the rule changed since "
        "file creation. Finally the last column denotes whether the file "
        "will be updated or created during the next workflow execution.")
    parser.add_argument(
        "--detailed-summary", "-D", action="store_true",
        help="Print a summary of all files created by the workflow. The "
        "has the following columns: filename, modification time, "
        "rule version, input file(s), shell command, status, plan.\n"
        "Thereby rule version contains the version"
        "the file was created with (see the version keyword of rules), and "
        "status denotes whether the file is missing, its input files are "
        "newer or if version or implementation of the rule changed since "
        "file creation. The input file and shell command columns are self"
        "explanatory. Finally the last column denotes whether the file "
        "will be updated or created during the next workflow execution.")
    parser.add_argument(
        "--touch", "-t", action="store_true",
        help=(
            "Touch output files (mark them up to date without really "
            "changing them) instead of running their commands. This is "
            "used to pretend that the rules were executed, in order to "
            "fool future invocations of snakemake. Fails if a file does "
            "not yet exist."))
    parser.add_argument(
        "--keep-going", "-k", action="store_true",
        help="Go on with independent jobs if a job fails.")
    parser.add_argument(
        "--force", "-f", action="store_true",
        help=(
            "Force the execution of the selected target or the first rule "
            "regardless of already created output."))
    parser.add_argument(
        "--forceall", "-F", action="store_true",
        help=(
            "Force the execution of the selected (or the first) rule and "
            "all rules it is dependent on regardless of already created "
            "output."))
    parser.add_argument(
        "--forcerun", "-R", nargs="+", metavar="TARGET",
        help=(
            "Force the re-execution or creation of the given rules or files."
            " Use this option if you changed a rule and want to have all its "
            "output in your workflow updated."))
    parser.add_argument(
        "--prioritize", "-P", nargs="+", metavar="TARGET",
        help=
            ("Tell the scheduler to assign creation of given targets "
            "(and all their dependencies) highest priority. (EXPERIMENTAL)"))
    parser.add_argument(
        "--allow-ambiguity", "-a", action="store_true",
        help=(
            "Don't check for ambiguous rules and simply use the first if "
            "several can produce the same file. This allows the user to "
            "prioritize rules by their order in the snakefile."))
    # TODO extend below description to explain the wildcards that can be used
    parser.add_argument(
        "--cluster", "-c", metavar="CMD",
        help=(
            "Execute snakemake rules with the given submit command, "
            "e.g. qsub. Snakemake compiles jobs into scripts that are "
            "submitted to the cluster with the given command, once all input "
            "files for a particular job are present.\n"
            "The submit command can be decorated to make it aware of certain job properties (input, output, params, wildcards, log, threads and dependencies (see the argument below)), e.g.:\n"
            "$ snakemake --cluster 'qsub -pe threaded {threads}'."))
    parser.add_argument(
        "--drmaa", nargs="?", const="", metavar="ARGS",
        help="Execute snakemake on a cluster accessed via DRMAA, "
            "Snakemake compiles jobs into scripts that are "
            "submitted to the cluster with the given command, once all input "
            "files for a particular job are present. ARGS can be used to "
            "specify options of the underlying cluster system, "
            "thereby using the job properties input, output, params, wildcards, log, "
            "threads and dependencies, e.g.: "
            "--drmaa ' -pe threaded {threads}'. Note that ARGS must be given in quotes and "
            "with a leading whitespace.")
    parser.add_argument(
        "--immediate-submit", "--is", action="store_true",
        help=(
            "Immediately submit all jobs to the cluster instead of waiting "
            "for present input files. This will fail, unless you make "
            "the cluster aware of job dependencies, e.g. via:\n"
            "$ snakemake --cluster 'sbatch --dependency {dependencies}.\n"
            "Assuming that your submit script (here sbatch) outputs the generated job id to the first stdout line, {dependencies} will be filled with space separated job ids this job depends on."))
    parser.add_argument(
        "--jobscript", "--js", metavar="SCRIPT",
        help="Provide a custom job script for submission to the cluster. "
            "The default script resides as 'jobscript.sh' in the "
            "installation directory.")
    parser.add_argument(
        "--jobname", "--jn", default="snakejob.{rulename}.{jobid}.sh", metavar="NAME",
        help="Provide a custom name for the jobscript that is submitted to the cluster (see --cluster)."
        "NAME is \"snakejob.{rulename}.{jobid}.sh\" per default. The wildcard {jobid} has to be present in the name.")
    parser.add_argument(
        "--reason", "-r", action = "store_true",
        help="Print the reason for each executed rule.")
    parser.add_argument(
        "--stats", metavar="FILE",
        help="Write stats about Snakefile execution in JSON format to the given file.")
    parser.add_argument(
        "--nocolor", action = "store_true",
        help="Do not use a colored output.")
    parser.add_argument(
        "--quiet", "-q", action = "store_true",
        help="Do not output any progress or rule information.")
    parser.add_argument(
        "--nolock", action="store_true",
        help="Do not lock the working directory")
    parser.add_argument(
        "--unlock", action="store_true",
        help="Remove a lock on the working directory.")
    parser.add_argument(
        "--cleanup-metadata", "--cm", nargs="*", metavar="FILE",
        help="Cleanup the metadata "
        "of given files. That means that snakemake removes any tracked "
        "version info, and any marks that files are incomplete.")
    parser.add_argument(
        "--rerun-incomplete", "--ri", action="store_true", help="Re-run all "
        "jobs the output of which is recognized as incomplete.")
    parser.add_argument(
        "--ignore-incomplete", "--ii", action="store_true", help="Ignore "
        "any incomplete jobs.")
    parser.add_argument(
        "--list-version-changes", "--lv", action="store_true",
        help="List all output files that have been created with "
        "a different version (as determined by the version keyword).")
    parser.add_argument(
        "--list-code-changes", "--lc", action="store_true",
        help="List all output files for which the rule body (run or shell) have changed "
        "in the Snakefile.")
    parser.add_argument(
        "--list-input-changes", "--li", action="store_true",
        help="List all output files for which the defined input files have changed "
        "in the Snakefile (e.g. new input files were added in the rule definition or files were renamed). For listing input file modification in the filesystem, use --summary.")
    parser.add_argument(
        "--list-params-changes", "--lp", action="store_true",
        help="List all output files for which the defined params have changed "
        "in the Snakefile.")
    parser.add_argument(
        "--latency-wait", "--output-wait", "-w", type=int, default=5, metavar="SECONDS",
        help="Wait given seconds if an output file of a job is not present after "
        "the job finished. This helps if your filesystem "
        "suffers from latency (default 5).")
    parser.add_argument(
        "--wait-for-files", nargs="*", metavar="FILE", help="Wait --latency-wait seconds for these "
        "files to be present before executing the workflow. "
        "This option is used internally to handle filesystem latency in cluster "
        "environments.")
    parser.add_argument(
        "--benchmark-repeats", type=int, default=1, metavar="N",
        help="Repeat a job N times if marked for benchmarking (default 1)."
    )
    parser.add_argument(
        "--notemp", "--nt", action="store_true",
        help="Ignore temp() declarations. This is useful when running only "
        "a part of the workflow, since temp() would lead to deletion of "
        "probably needed files by other parts of the workflow."
        )
    parser.add_argument(
        "--keep-target-files", action="store_true",
        help="Do not adjust the paths of given target files relative to the working directory.")
    parser.add_argument(
        "--allowed-rules", nargs="+",
        help="Only use given rules. If omitted, all rules in Snakefile are used.")
    parser.add_argument(
        '--timestamp', '-T', action='store_true',
        help='Add a timestamp to all logging output')
    parser.add_argument(
        "--greedyness", type=float, default=None, help="Set the greedyness of scheduling. This value between 0 and 1 determines how careful jobs are selected for execution. The default value (1.0) provides the best speed and still acceptable scheduling quality.")
    parser.add_argument(
        "--print-compilation", action="store_true",
        help="Print the python representation of the workflow.")
    parser.add_argument(
        "--overwrite-shellcmd",
        help="Provide a shell command that shall be executed instead of those given in the workflow. "
        "This is for debugging purposes only.")
    parser.add_argument(
        "--debug", action="store_true", help="Print debugging output.")
    parser.add_argument(
        "--profile", metavar="FILE", help="Profile Snakemake and write the output to FILE. This requires yappi to be installed.")
    parser.add_argument(
        "--bash-completion", action="store_true", help="Output code to register bash completion for snakemake. Put the following in your .bashrc (including the accents): `snakemake --bash-completion` or issue it in an open terminal session.")
    parser.add_argument(
        "--version", "-v", action="version", version=__version__)
    return parser

def main():
    parser = get_argument_parser()
    args = parser.parse_args()

    if args.bash_completion:
        print("complete -C snakemake-bash-completion snakemake")
        sys.exit(0)

    snakemakepath = sys.argv[0]

    try:
        resources = parse_resources(args)
        config = parse_config(args)
    except ValueError as e:
        print(e, file=sys.stderr)
        print("", file=sys.stderr)
        parser.print_help()
        sys.exit(1)

    if args.cluster and args.drmaa:
        print("--cluster and --drmaa are mutually exclusive", file=sys.stderr)
        parser.print_help()
        sys.exit(1)

    if args.profile:
        import yappi
        yappi.start()

    _snakemake = partial(snakemake, args.snakefile, snakemakepath=snakemakepath)

    if args.gui is not None:
        try:
            import snakemake.gui as gui
        except ImportError:
            print(
                "Error: GUI needs Flask to be installed. Install "
                "with easy_install or contact your administrator.",
                file=sys.stderr)
            sys.exit(1)

        _logging.getLogger("werkzeug").setLevel(_logging.ERROR)
        gui.register(_snakemake, args)
        url = "http://127.0.0.1:{}".format(args.gui)
        print("Listening on {}.".format(url), file=sys.stderr)
        def open_browser():
            try:
                webbrowser.open(url)
            except:
                pass
        print("Open this address in your browser to access the GUI.", file=sys.stderr)
        threading.Timer(0.5, open_browser).start()
        success = True
        try:
            gui.app.run(debug=False, threaded=True, port=args.gui)
        except (KeyboardInterrupt, SystemExit):
            # silently close
            pass
    else:
        success = _snakemake(
            listrules=args.list,
            list_target_rules=args.list_target_rules,
            cores=args.cores,
            nodes=args.cores,
            resources=resources,
            config=config,
            configfile=args.configfile,
            config_args=args.config,
            workdir=args.directory,
            targets=args.target,
            dryrun=args.dryrun,
            printshellcmds=args.printshellcmds,
            printreason=args.reason,
            printdag=args.dag,
            printrulegraph=args.rulegraph,
            printd3dag=args.d3dag,
            touch=args.touch,
            forcetargets=args.force,
            forceall=args.forceall,
            forcerun=args.forcerun,
            prioritytargets=args.prioritize,
            stats=args.stats,
            nocolor=args.nocolor,
            quiet=args.quiet,
            keepgoing=args.keep_going,
            cluster=args.cluster,
            drmaa=args.drmaa,
            jobname=args.jobname,
            immediate_submit=args.immediate_submit,
            standalone=True,
            ignore_ambiguity=args.allow_ambiguity,
            snakemakepath=snakemakepath,
            lock=not args.nolock,
            unlock=args.unlock,
            cleanup_metadata=args.cleanup_metadata,
            force_incomplete=args.rerun_incomplete,
            ignore_incomplete=args.ignore_incomplete,
            list_version_changes=args.list_version_changes,
            list_code_changes=args.list_code_changes,
            list_input_changes=args.list_input_changes,
            list_params_changes=args.list_params_changes,
            summary=args.summary,
            detailed_summary=args.detailed_summary,
            print_compilation=args.print_compilation,
            debug=args.debug,
            jobscript=args.jobscript,
            notemp=args.notemp,
            timestamp=args.timestamp,
            greedyness=args.greedyness,
            overwrite_shellcmd=args.overwrite_shellcmd,
            latency_wait=args.latency_wait,
            benchmark_repeats=args.benchmark_repeats,
            wait_for_files=args.wait_for_files,
            keep_target_files=args.keep_target_files,
            allowed_rules=args.allowed_rules)

    if args.profile:
        with open(args.profile, "w") as out:
            profile = yappi.get_func_stats()
            profile.sort("totaltime")
            profile.print_all(out=out)

    sys.exit(0 if success else 1)

def bash_completion(snakefile="Snakefile"):
    if not len(sys.argv) >= 2:
        print("Calculate bash completion for snakemake. This tool shall not be invoked by hand.")
        sys.exit(1)

    prefix = sys.argv[2]

    if prefix.startswith("-"):
        opts = [
            action.option_strings[0] for action in get_argument_parser()._actions
            if action.option_strings and action.option_strings[0].startswith(prefix)]
        print(*opts, sep="\n")
    else:
        files = glob.glob("{}*".format(prefix))
        if files:
            print(*files, sep="\n")
        elif os.path.exists(snakefile):
            workflow = Workflow(snakefile=snakefile, snakemakepath="snakemake")
            workflow.include(snakefile)

            workflow_files = sorted(set(
                file for file in workflow.concrete_files
                if file.startswith(prefix)))
            if workflow_files:
                print(*workflow_files, sep="\n")

            rules = [
                rule.name for rule in workflow.rules
                if rule.name.startswith(prefix)]
            if rules:
                print(*rules, sep="\n")
    sys.exit(0)


text:
require 'pathname'
require 'vagrant-vmware-appcatalyst/plugin'

module VagrantPlugins
  module AppCatalyst
    lib_path = Pathname.new(File.expand_path('../vagrant-vmware-appcatalyst', __FILE__))
    autoload :Action, lib_path.join('action')
    autoload :Errors, lib_path.join('errors')

    # This returns the path to the source of this plugin.
    #
    # @return [Pathname]
    def self.source_root
      @source_root ||= Pathname.new(File.expand_path('../../', __FILE__))
    end
  end
end

redpajama_set_name: RedPajamaGithub


text:
module.exports = function (grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        //resolve bower dependencies and place in appropriate location
        clean: {
            cleanDist: ['dist', 'output']
        },
        //Static Analysis
        jshint: {
            options: {
                jshintrc: true,
                force: true,
                reporter: 'checkstyle',
                //Dont put within sub-folder otherwise jenkins-checkstyle plugin
                //cannot find source
                reporterOutput: 'jshint_checkstyle.xml'
            },
            all: ['src/app/js/*.js', 'src/app/js/**/*.js']
        },
        copy: {
            copyUnoptimised: {
                files: [
                    {
                        expand: true,
                        cwd: 'src',
                        src: ['**', '!app/css/**', '!app/js/**', '!app/libs**', '!app/index.html'],
                        dest: 'dist'
                    }
                ]
            }
        },
        bower: {
            install: {
                options: {
                    targetDir: 'dist/app/libs',
                    copy: true,
                    install: true
                }
            }
        },
        concat: {
            options: {
                banner: '/*! Enter own license data here */'
            },
            projectConcat: {
                src: ['src/app/js/*.js'],
                dest: 'dist/app/js/phonecat.min.js'
            },
            projectCssConcat: {
                src: ['src/app/css/*.css'],
                dest: 'dist/app/css/combined.min.css'
            }
        },
        uglify: {
            minifyProject: {
                options: {
                    preserveComments: 'some'
                },
                files: {
                    'dist/app/js/phonecat.min.js': ['dist/app/js/phonecat.min.js']
                }
            }
        },
        cssmin: {
            minifyProjectCss: {
                options: {
                    banner: '/* Add own license Banne for CSS */'
                },
                files: {
                    'dist/app/css/combined.min.css': ['dist/app/css/combined.min.css']
                }
            }
        },
        processhtml: {
            dist: {
                files: {
                    'dist/app/index.html': ['src/app/index.html']
                }
            }
        },
        //Startup a shell and start the Xvfb server (provides display on :99)
        shell: {
            xvfb: {
                command: 'Xvfb :99 -ac -screen 0 1600x1200x24',
                options: {
                    async: true
                }
            }
        },
        //export the DISPLAY environment variable so headless tests can connect to display
        env: {
            xvfb: {
                DISPLAY: ':99'
            }
        },
        //Unit test run (Runs once as opposed to watch as its a CI build)
        karma: {
            unit: {
                configFile: 'dist/test/karma.conf.js',
                singleRun: true
            }
        },
        //start a simple web server hosting the app to test
        //(Required for protractor to run against)
        connect: {
            deployForTest: {
                options: {
                    base: 'dist/'
                }
            }
        },
        //Run E2E tests
        protractor: {
            options: {
                keepAlive: true, // If false, the grunt process stops when the test fails.
                args: {
                }
            },
            e2e: {// Grunt requires at least one target to run so you can simply put 'all: {}' here too.
                options: {
                    configFile: "dist/test/protractor-conf.js", // Target-specific config file
                    args: {}
                }
            }
        },
        yslow_test: {
            options: {
                info: "grade",
                format: "junit",
                urls: ['http://localhost:8000/app/index.html','http://localhost:8000/app/index.html#/phones/nexus-s'],
                reports: ['output/yslow/yslow.xml']
            },
            your_target: {
                files: []
            }
        },
        compress: {
            main: {
                options: {
                    archive: 'output/<%= pkg.name %>-<%= pkg.version %>.war',
                    mode: 'zip'
                },
                files: [
                    {expand: true, cwd: 'dist/app', src: ['**'], filter: 'isFile'}, // includes files in path
                ]
            }
        }
    });
    grunt.loadNpmTasks('grunt-bower-task');
    grunt.loadNpmTasks('grunt-karma');
    grunt.loadNpmTasks('grunt-contrib-connect');
    grunt.loadNpmTasks('grunt-protractor-runner');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-env');
    grunt.loadNpmTasks('grunt-shell-spawn');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-processhtml');
    grunt.loadNpmTasks('grunt-contrib-compress');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-yslow-test');

    grunt.registerTask('optimise', ['concat', 'uglify', 'cssmin', 'processhtml']);
    grunt.registerTask('build', ['clean', 'bower', 'copy', 'optimise']);
    //For use in a CI environment (Start a shell and setup the environment)
    grunt.registerTask('headlessStart', ['shell:xvfb', 'env:xvfb']);
    grunt.registerTask('headlessFinish', ['shell:xvfb:kill']);
    grunt.registerTask('unitTest', ['karma']);
    grunt.registerTask('headless-unitTest', ['headlessStart', 'unitTest', 'headlessFinish']);
    grunt.registerTask('integrationTest', ['connect', 'protractor']);
    grunt.registerTask('headless-integrationTest', ['headlessStart', 'integrationTest', 'headlessFinish']);
    //Static analysis
    grunt.registerTask('analyse', ['jshint']);
    //build to run in dev environment (Windowing already setup)
    grunt.registerTask('devBuild', ['build', 'unitTest', 'integrationTest', 'yslow_test', 'analyse']);
    //build to run in headless environment (Jenkins)
    grunt.registerTask('ciBuild', ['build', 'headlessStart', 'unitTest', 'integrationTest', 'yslow_test', 'headlessFinish', 'analyse']);
    //leave default as non CI build so its easy for devs to run
    grunt.registerTask('default', ['devBuild']);
    grunt.registerTask('package', ['compress']);
};


text:
def decompressSize(compressed, recurse):
    index = 0
    size = 0

    while "(" in compressed[index:]:
        markerLoc = compressed[index:].find("(")
        closeLoc = compressed[index:].find(")")
        size += markerLoc
        marker = compressed[(index+markerLoc+1):(index+closeLoc)]
        markerString = "".join(marker)
        splitMarker = markerString.split("x")
        length = int(splitMarker[0])
        multiplier = int(splitMarker[1])
        substring = compressed[(index+closeLoc+1):(index+closeLoc+1+length)]
        if recurse and "(" in substring:
            substringSize = decompressSize(substring, recurse)
        else:
            substringSize = len(substring)
        size += (substringSize * multiplier)
        index += closeLoc + 1 + length
    size += len(compressed[index:])
    return size

raw = input("? ")
print decompressSize(raw, False)
print decompressSize(raw, True)


text:
#pragma checksum "C:\Users\holge\Source\Repos\Draw2\Draw\Draw\View\Adorner.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "13EE4DAF7B9A55BCB0E79CA86A9DFB3E"
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Draw.View
{
    partial class Adorner :
        global::Windows.UI.Xaml.Controls.UserControl,
        global::Windows.UI.Xaml.Markup.IComponentConnector,
        global::Windows.UI.Xaml.Markup.IComponentConnector2
    {
        /// <summary>
        /// Connect()
        /// </summary>
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks"," 14.0.0.0")]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public void Connect(int connectionId, object target)
        {
            switch(connectionId)
            {
            case 1:
                {
                    this.rootGrid = (global::Windows.UI.Xaml.Controls.Grid)(target);
                }
                break;
            case 2:
                {
                    this.TopLeft = (global::Windows.UI.Xaml.Shapes.Rectangle)(target);
                }
                break;
            case 3:
                {
                    this.TopCenter = (global::Windows.UI.Xaml.Shapes.Rectangle)(target);
                }
                break;
            case 4:
                {
                    this.TopRight = (global::Windows.UI.Xaml.Shapes.Rectangle)(target);
                }
                break;
            case 5:
                {
                    this.CenterLeft = (global::Windows.UI.Xaml.Shapes.Rectangle)(target);
                }
                break;
            case 6:
                {
                    this.CenterRight = (global::Windows.UI.Xaml.Shapes.Rectangle)(target);
                }
                break;
            case 7:
                {
                    this.BottomLeft = (global::Windows.UI.Xaml.Shapes.Rectangle)(target);
                }
                break;
            case 8:
                {
                    this.BottomCenter = (global::Windows.UI.Xaml.Shapes.Rectangle)(target);
                }
                break;
            case 9:
                {
                    this.BottomRight = (global::Windows.UI.Xaml.Shapes.Rectangle)(target);
                }
                break;
            case 10:
                {
                    this.TurnHandle = (global::Windows.UI.Xaml.Shapes.Ellipse)(target);
                }
                break;
            default:
                break;
            }
            this._contentLoaded = true;
        }

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks"," 14.0.0.0")]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public global::Windows.UI.Xaml.Markup.IComponentConnector GetBindingConnector(int connectionId, object target)
        {
            global::Windows.UI.Xaml.Markup.IComponentConnector returnValue = null;
            return returnValue;
        }
    }
}


text:
<link rel="import" href="../../../bower_components/polymer/polymer.html">

<link rel="import" href="../../../bower_components/things-ajax/things-ajax.html">

<link rel="import" href="../../things-layout-view/things-layout-mercury.html">
<link rel="import" href="../../things-meta-view/things-diy-based-grid.html">
<link rel="import" href="../../things-meta-view/things-diy-based-form.html">

<link rel="import" href="things-term-common-grid.html">
<link rel="import" href="things-lot-based-transaction-behavior.html">

<!--
  OI - FINISH LOT 트랜잭션
-->

<dom-module id="things-term-finish-lot">
  <template>
    <style>
      :host {
        display: block;
        @apply(--layout-vertical);
        @apply(--layout-flex);
      }
      things-layout-mercury {
        @apply(--layout-flex);
      }
    </style>

    <things-ajax
      id="validate-lot"
      resource-action="create"
      resource-url="lot_trxs/VALIDATE/transaction"
      last-response="{{lot}}">
    </things-ajax>

    <things-ajax
      id="transaction"
      resource-action="create"
      resource-url="[[transactionUrl]]">
    </things-ajax>

    <things-layout-mercury
      left-top-content-title="[[inputTitle]]"
      left-bottom-content-title="[[lotInfoTitle]]"
      right-content-title="[[lotListTitle]]">
      <things-diy-based-form
        left-top
        id="input-form"
        class="flex"
        on-keypress="handleKeyPress"
        diy-form-name="OI-Comment"
        buttons="[[buttons]]">
      </things-diy-based-form>

      <things-diy-based-form
        left-bottom
        id="lot-info-form"
        class="flex"
        diy-form-name="OI-LotInfo">
      </things-diy-based-form>

      <things-term-common-grid
        right
        id="latest-lot-grid"
        class="flex"
        query-fields="[[latestLotQueryFields]]"
        sort-fields="[[lastestLotSortFields]]">
      </things-term-common-grid>
    </things-layout-mercury>
  </template>

  <script>
    Polymer({
      is: 'things-term-finish-lot',

      behaviors: [
        Things.LotBasedTransactionBehavior
      ],

      properties: {
        /**
         * Title Of Lot Information
         */
        lotInfoTitle: {
          type: String,
          value: function() {
            return Things.DataGlobal.t('title.LotInformation')
          }
        },

        /**
         * Title Of Input
         */
        inputTitle: {
          type: String,
          value: function() {
            return Things.DataGlobal.t('title.Input')
          }
        },

        /**
         * Title Of Lot List
         */
        lotListTitle: {
          type: String,
          value: function() {
            return Things.DataGlobal.t('title.LotList')
          }
        },

        lastestLotSortFields: {
          type: Array,
          value: function() {
            return [{field : 'updatedAt', ascending : false}]
          }
        }
      },

      observers: [
        'lotListConditionsChanged(orderId,operationId)'
      ],

      listeners: {
        'process-btn-tap' : 'lotTransaction',
        'transaction.things-ajax-response' : 'transactionResponsed',
        'things-grid-configured' : 'latestLotGridConfigured'
      },

      /**
       * Life Cycle - Ready
       */
      ready: function() {
        this.transaction = 'FINISH';
      },

      /**
       * Latest Lot List 검색 조건 변경시
       *
       * @param {String} orderId
       * @param {String} operationId
       */
      lotListConditionsChanged: function(orderId, operationId) {
        this.latestLotQueryFields = [ {
          name: 'work_order_id',
          value: orderId
        }, {
          name: 'operation_id',
          value: operationId
        }, {
          name: 'state',
          value: 'PROCESSING'
        } ];

        this.refreshLatestLots();
      },

      /**
       * Latest Lot Grid 변경시
       */
      latestLotGridConfigured: function(event) {
        this.refreshLatestLots();
      },

      /**
       * Form List를 리턴
       */
      getForms: function() {
        return [this.getLotInfoForm(), this.getInputForm()];
      },

      /**
       * Lot Information Form
       */
      getLotInfoForm: function() {
        return this.$['lot-info-form'];
      },

      /**
       * Input Form
       */
      getInputForm: function() {
        return this.$['input-form'];
      },

      /**
       * Latest Lot List Grid
       */
      getLatestLotGrid: function() {
        return this.$['latest-lot-grid'];
      },

      /**
       * Validation Lot Service
       */
      getValidateLotService: function() {
        return this.$['validate-lot'];
      },

      /**
       * Transaction Lot Service
       */
      getTransactionLotService: function() {
        return this.$['transaction'];
      },

      /**
       * Get Lot No.
       */
      getLotNo: function() {
        var inputData = this.getInputForm().getForm().serializeMyForm();
        return inputData.name;
      },

      /**
       * Lot Transaction
       */
      lotTransaction: function() {
        var inputData = this.getInputForm().getForm().serializeMyForm();
        var lotData = this.getLotInfoForm().getForm().serializeMyForm();
        var tranAjax = this.getTransactionLotService();

        tranAjax.body = {
          lot_id : lotData.id,
          work_order_id : this.orderId,
          current_operation_id : this.operationId,
          comments : inputData.comments
        };

        tranAjax.generateRequest();
      },

      /**
       * 트랜잭션 성공시
       *
       * @param {Object} event
       */
      transactionResponsed: function(event) {
        if(!this.checkTransactionResponse(event)) {
          return;
        }

        this.resetForm();
        this.refreshLatestLots();
        this.fire('iron-signal', {name : 'refresh-wip'});
      }
    });
  </script>
</dom-module>


text:
import hashlib

from bson import json_util
from flask import Response

def sha1_string(s):
    sha1_checksum = hashlib.sha1()
    sha1_checksum.update(s)
    return sha1_checksum.hexdigest()

def force_unicode(text):
    if text is None:
        return u''

    if isinstance(text, unicode):
        return text

    try:
        text = unicode(text, 'utf-8')
    except UnicodeDecodeError:
        text = unicode(text, 'latin1')
    except TypeError:
        text = unicode(text)
    return text

def force_utf8(text):
    return str(force_unicode(text).encode('utf8'))

class InvalidUsage(Exception):
    def __init__(self, message, status_code=400, payload=None):
        Exception.__init__(self)
        self.message = message
        self.status_code = status_code
        self.payload = payload

    def to_dict(self):
        rv = dict(self.payload or ())
        rv['message'] = self.message
        return rv

def assert_if(condition, error_message, status_code=400):
    if not condition:
        raise InvalidUsage(error_message, status_code=status_code)

def jsonify(data):
    return Response(json_util.dumps(data),
                    mimetype='application/json')


text:
'use strict'

module.exports = {
  name: 'take_attendance_callback',
  execute: function (bot, msg) {
    this._facilitator.takeAttendanceCallback(bot, msg).catch((err) => console.error(err))
  }
}


text:
set -o errexit
set -o nounset
set -o pipefail

readonly SCRIPT_ROOT=$(cd $(dirname ${BASH_SOURCE})/.. && pwd)
echo "SCRIPT_ROOT ${SCRIPT_ROOT}"
readonly GO111MODULE="on"
readonly GOFLAGS="-mod=mod"
readonly GOPATH="$(mktemp -d)"

export GO111MODULE GOFLAGS GOPATH

# Even when modules are enabled, the code-generator tools always write to
# a traditional GOPATH directory, so fake on up to point to the current
# workspace.
mkdir -p "$GOPATH/src/k8s.io/cloud-provider-gcp"
ln -s "${SCRIPT_ROOT}" "$GOPATH/src/k8s.io/cloud-provider-gcp/crd"

# mkdir -p "$GOPATH/src/k8s.io"
# ln -s "${SCRIPT_ROOT}" "$GOPATH/src/k8s.io/cloud-provider-gcp"
echo " GOPATH/src/k8s.io/cloud-provider-gcp/crd ${GOPATH}/src/k8s.io/cloud-provider-gcp/crd"

readonly REPO_BASE=k8s.io/cloud-provider-gcp/crd
readonly OUTPUT_BASE_PKG=${REPO_BASE}/client
readonly APIS_BASE_PKG=${REPO_BASE}/apis
readonly CLIENTSET_NAME=versioned
readonly CLIENTSET_PKG_NAME=clientset

if [[ "${VERIFY_CODEGEN:-}" == "true" ]]; then
  echo "Running in verification mode"
  readonly VERIFY_FLAG="--verify-only"
fi

readonly COMMON_FLAGS="${VERIFY_FLAG:-} --go-header-file ${SCRIPT_ROOT}/hack/boilerplate.go.txt"

generate_config() {
  local crd_name version apis_pkg
  crd_name=${1}
  version=${2}
  apis_pkg=${APIS_BASE_PKG}/${1}/${2}

  echo "Performing code generation for ${crd_name} CRD"
  echo "Generating deepcopy functions and CRD artifacts"
  go run sigs.k8s.io/controller-tools/cmd/controller-gen \
          object:headerFile=${SCRIPT_ROOT}/hack/boilerplate.go.txt \
          crd:crdVersions=$version \
          paths=${apis_pkg}/... \
          output:crd:artifacts:config=${SCRIPT_ROOT}/config/crds
}

codegen_for () {
  local crd_name version apis_pkg output_pkg

  if [[ $# != 2 ]]; then
    echo "Usage: codegen_for CRD-NAME VERSION" >&2
    echo "" >&2
    echo "This writes auto generated client methods for CRD-NAME/VERSION" >&2
    return 1
  fi

  crd_name=${1}
  version=${2}
  output_pkg=${OUTPUT_BASE_PKG}/${1}
  apis_pkg=${APIS_BASE_PKG}/${1}/${2}

  generate_config $crd_name $version

  echo "Generating clientset at ${output_pkg}/${CLIENTSET_PKG_NAME}"
  echo "apis_pkg ${apis_pkg}"
  echo "output_pkg/CLIENTSET_PKG_NAME ${output_pkg}/${CLIENTSET_PKG_NAME}"
  go run k8s.io/code-generator/cmd/client-gen \
          --input-base "" \
          --input "${apis_pkg}" \
          --clientset-name "${CLIENTSET_NAME}" \
          --output-package "${output_pkg}/${CLIENTSET_PKG_NAME}" \
          ${COMMON_FLAGS}

  echo "Generating listers at ${output_pkg}/listers"
  go run k8s.io/code-generator/cmd/lister-gen \
          --input-dirs "${apis_pkg}" \
          --output-package "${output_pkg}/listers" \
          ${COMMON_FLAGS}

  echo "Generating informers at ${output_pkg}/informers"
  go run k8s.io/code-generator/cmd/informer-gen \
           --input-dirs "${apis_pkg}" \
           --versioned-clientset-package "${output_pkg}/${CLIENTSET_PKG_NAME}/${CLIENTSET_NAME}" \
           --listers-package "${output_pkg}/listers" \
           --output-package "${output_pkg}/informers" \
           ${COMMON_FLAGS}

  echo "Generating register at ${apis_pkg}"
  go run k8s.io/code-generator/cmd/register-gen \
          --input-dirs "${apis_pkg}" \
          --output-package "${apis_pkg}" \
          ${COMMON_FLAGS}
}

codegen_for network v1
codegen_for gcpfirewall v1beta1


text:
'use strict;'

var io;
var browser;

var lkey = localStorage.lkey;
var privateMode = false;
if (setKey("testme", "test") == false) {
	privateMode = true;
    alert("You browser appears to be in private mode. You will be unable to vote without turning off private mode and reloading.");
}

var debug = _getParameterByName('debug');

document.addEventListener('DOMContentLoaded', function() {

	/* var es_list = document.querySelectorAll('*[data-lang="es"]').remove(); // hardcode for now
	for (var i = 0; i < es_list.length; ++i) {
		es_list[i].remove();
	}
	*/

    var seenEvents = {}; // keep track to prevent dups

	// get the browser details

	BrowserDetect.init();
	browser = {
		browser: BrowserDetect.browser.toLowerCase(),
		version: BrowserDetect.version,
		version: BrowserDetect.version,
		os: BrowserDetect.OS
	};

	if (browser.os.toLowerCase() == "android") {
		browser.browser = "android";
	}
	if (browser.browser.toLowerCase() == "explorer") {
		browser.browser = "ie";
	}
	if (browser.browser.toLowerCase() == "mozilla") {
		if (browser.os.toLowerCase() == "an unknown os") {
			browser.browser = "other";
		}
	}

	switch (browser.browser.toLowerCase()) {
	case "android":
		break;
	case "ie":
		break;
	case "chrome":
		browser.browser = "chrome";
		break;
	case "firefox":
		break;
	case "safari":
		break;
	default:
		browser.browser = "other";
	}

	if (/mobile/i.test(navigator.userAgent)) {
		browser.mobile = true;
	}
	if (/webkit.*mobile/i.test(navigator.userAgent)) {
		browser.mobile_safari = true;
	}

	if (browser.browser == "ie" && browser.version < 9) {
		alert("Sorry, but your need IE 9 or better (or a new Chrome or Firefox) for this app to work properly.");
	}

	// connect
	io = io('/danny', { path: '/node/socket.io'} );

	io.on('connect', function(data) {
        if (!privateMode) {
            lkey = localStorage.lkey ? localStorage.lkey : io.io.engine.id;
            setKey("lkey", lkey);
            io.emit('key', {
                key: lkey,
                browser: browser
            });
        }
    });

    var q1 = document.getElementById("q1");
    q1.addEventListener('keydown', function(ev) {
        if (ev.keyCode == 13) {
            ev.preventDefault();
            io.emit('answer', { answer: q1.value, qgroup: q1.getAttribute('data-qgroup'), key: lkey });
            tulito.toggleOpen(document.getElementById('right-pane-3'));
        }
        return false;
    });

    // admin content sent to us
    io.on('msg', function(content) {

        if (debug) {
            console.log(content);
        }

        if (content['_id'] == undefined) {
            if (debug) {
                console.log(content);
            }
        }
        if (debug) {
            console.log('event id: ' + content._id);
        }

        if (!debug && seenEvents[content._id]) {
            return;
        } // we can get dups if we reconnect, for example.

        for (var key in content) {

            switch (key) {

                case 'title':
                    document.getElementsByTagName('title')[0].innerHTML = content['title'];
                    document.getElementById('title').innerHTML = content['title'];
                    break;

                case 'presenters':
                    var template = document.querySelector('[data-template="presenter"]');
                    for (var i = 0; i < content['presenters'].length; i++) {
                        var newpresenter = template.cloneNode();
                        newpresenter.removeAttribute('data-template');
                        newpresenter.innerHTML = content['presenters'][i];
                        template.parentNode.appendChild(newpresenter);
                        tulito.apply(newpresenter);
                    }
                    break;

                case 'twitters':
                    var twitterEl = document.getElementById('twitter-buttons');
                    var twitters = "";
                    for (var i = 0; i < content['twitters'].length; i++) {
                        twitters += '   @' + content['twitters'][i] + '   ';
                    }
                    twitters += "";
                    twitterEl.innerHTML = twitters;
                    break;

                case 'aside':
                    _setAsideText(content.aside);
                    break;

                case 'hide':
                    for (var i = 0; i < content['hide'].length; i++) {
                        var sel = document.getElementById(content['hide'][i]);
                        tulito._removeClass(sel, 'shown');
                        (function(el) { setTimeout(function() {
                            el.style.display = 'none';
                        }, 1000)})(sel);
                     }
                     break;

                case 'show':
                    for (var i = 0; i < content['show'].length; i++) {
                        var sel = document.getElementById(content['show'][i]);
                        sel.style.display = 'inline';
                        (function(el) { setTimeout(function() {
                            tulito._addClass(sel, 'shown');
                        }, 10)})(sel);
                     }
                     break;

                case 'question-prompt':
                    _promptQuestion(content['question-prompt']);

                    break;

                case 'vote-prompt':
                    _promptVote(content['vote-prompt']);

                    break;

                case 'multiple-votes':
                    for (var i = 0; i < content['multiple-votes'].length; i++) {
                        _promptVote(content['multiple-votes'][i]['vote-prompt']);
                    }
                    break;

                case 'vote-end':
                    var prompt = content['vote-end'];
                    var el = document.getElementById(prompt.el);
                    if (tulito._getCache(el)._opened) {
                        tulito.toggleOpen(el);
                    }
                    if ('aside' in prompt) {
                        _setAsideText(prompt.aside);
                    }

                    // Remove existing buttons.

                    // This is very frustrating with Webjut. It gets up to the remove node
                    // part, then EXITS this entire function. AND resets the io connection.
                    // I gave up. And I moved the sendEvents bool to the very end. I also
                    // moved this removal to vote-end, where before it was in vote-prompt
                    // before adding the new buttons.

                    // I can't explain why this class addition 'removeme' and then a simple
                    // while loop on the old buttons worked where querying off each node's
                    // own classes did not. What I was doing before caused a WebSocket
                    // disconnect each time, too! But no errors at all from Chrome or
                    // Safari. Alas.

                    // IMPORTANT: sometimes a vote-end can happen just before a vote-prompt,
                    // resulting in a removal of buttons that just got added (since we reuse
                    // the panes). So it's critical that a live preso alternative between
                    // panes!
                    removeButtons(el);

                    break;

            }

            seenEvents[content._id] = true;
        }
	});

});

var BrowserDetect = {
	init: function() {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function(data) {
		for (var i = 0; i < data.length; i++) {
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1) return data[i].identity;
			} else if (dataProp) return data[i].identity;
		}
	},
	searchVersion: function(dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
	},
	dataBrowser: [{
		string: navigator.userAgent,
		subString: "Chrome",
		identity: "Chrome"
	}, {
		string: navigator.userAgent,
		subString: "OmniWeb",
		versionSearch: "OmniWeb/",
		identity: "OmniWeb"
	}, {
		string: navigator.vendor,
		subString: "Apple",
		identity: "Safari",
		versionSearch: "Version"
	}, {
		string: navigator.userAgent,
		substring: "Android",
		identity: "Android",
		versionSearch: "Version"
	},
	{
		prop: window.opera,
		identity: "Opera",
		versionSearch: "Version"
	},
	{
		string: navigator.vendor,
		subString: "iCab",
		identity: "iCab"
	}, {
		string: navigator.vendor,
		subString: "KDE",
		identity: "Konqueror"
	}, {
		string: navigator.userAgent,
		subString: "Firefox",
		identity: "Firefox"
	}, {
		string: navigator.vendor,
		subString: "Camino",
		identity: "Camino"
	}, { // for newer Netscapes (6+)
		string: navigator.userAgent,
		subString: "Netscape",
		identity: "Netscape"
	}, {
		string: navigator.userAgent,
		subString: "MSIE",
		identity: "Explorer",
		versionSearch: "MSIE"
	}, {
		string: navigator.userAgent,
		subString: "Gecko",
		identity: "Mozilla",
		versionSearch: "rv"
	}, { // for older Netscapes (4-)
		string: navigator.userAgent,
		subString: "Mozilla",
		identity: "Netscape",
		versionSearch: "Mozilla"
	}],
	dataOS: [{
		string: navigator.userAgent,
		subString: "android",
		identity: "Android"
	}, {
		string: navigator.platform,
		subString: "Win",
		identity: "Windows"
	}, {
		string: navigator.platform,
		subString: "Mac",
		identity: "Mac"
	}, {
		string: navigator.userAgent,
		subString: "iPhone",
		identity: "iPhone/iPod"
	}, {
		string: navigator.platform,
		subString: "Linux",
		identity: "Linux"
	}]

};

function _promptQuestion (prompt) {

    if (debug) {
        console.log(prompt);
    }

    var question = prompt.question;
    var qgroup = prompt.qgroup;
    var el = document.getElementById(prompt.el);
    var qel = el.getElementsByClassName('question-text')[0];

    var q1 = document.getElementById("q1");
    q1.setAttribute('data-qgroup', qgroup);

    if ('color' in prompt) {
        el.style.backgroundColor = prompt.color;
    }

    qel.innerHTML = question;

    if (debug) {
        console.log("opening " + prompt.el + "?");
    }
    // At last Chrome seems to needs this long for transitionEnd to
    // register properly.
    setTimeout(function() { tulito.toggleOpen(el) }, 1000);

}

function _promptVote (prompt) {

    if (debug) {
        console.log(prompt);
    }

    var question = prompt.question;
    var votegroup = prompt.votegroup;
    var el = document.getElementById(prompt.el);
    var qel = el.getElementsByClassName('question-text')[0];
    var q   = el.getElementsByClassName('question')[0];

    tulito._removeClass(qel, 'smaller');
    tulito._removeClass(q, 'smaller');

    // Setup the buttons. This is an important lesson learned. If you need buttons to change their
    // behavior dynamically in their callback, AND you don't want to have to save and read ALL the data
    // dynamically from DOM elements (which seems dumb anyway), you need to clone elements. That's because
    // there's no good way to remove a dynamic callback from an event listener. Period.

    // Add new buttons.
    var button_tmpl = el.querySelector('[data-template="button"]');
    for (var i = 0; i < prompt.options.length; i++) {
        var newButton = button_tmpl.cloneNode();
        newButton.removeAttribute('data-template');
        newButton.style.display = 'normal';
        var option = prompt.options[i];
        newButton.innerHTML = option;
        newButton.setAttribute('data-vote', prompt.options[i]);
        newButton.setAttribute('data-votegroup', prompt.votegroup);

        button_tmpl.parentNode.appendChild(newButton);
        if (debug) {
            // console.log("append:");
            console.log(newButton);
        }

        if ('shape' in prompt) {
            tulito._removeClass(newButton, 'square');
            tulito._removeClass(newButton, 'long');
            tulito._addClass(newButton, prompt.shape);
            tulito._addClass(newButton, 'removeme');
            // tulito._addClass(newButton, 'option' + i);
        }

        Hammer(newButton).on('tap', function(e) {
            io.emit('vote', { key: lkey, votegroup: e.target.getAttribute('data-votegroup'), vote: e.target.getAttribute('data-vote') });
            tulito.toggleOpen(el);
            if ('response' in prompt) {
                _setAsideText(prompt.response);
            }
            removeButtons(e.target);
        });

    }

    if ('color' in prompt) {
        el.style.backgroundColor = LightenDarkenColor(prompt.color, -20);
    }

    if (prompt.options.length > 4) {
        tulito._addClass(qel, 'smaller');
        tulito._addClass(q, 'smaller');
    }
    else if (prompt.options.length > 2) {
        tulito._addClass(qel, 'small');
        tulito._addClass(q, 'smaller');
    }

    qel.innerHTML = question;

    if (debug) {
        console.log("opening " + prompt.el + "?");
    }
    // At last Chrome seems to needs this long for transitionEnd to
    // register properly.
    setTimeout(function() { tulito.toggleOpen(el) }, 1000);

}

function _setAsideText (text) {
    var textel = document.getElementById('main-session-text');
    tulito._addClass(textel, 'fade-out');
    setTimeout(function() {
        textel.innerHTML = text;
    }, 400);
    setTimeout(function() {
        tulito._removeClass(textel, 'fade-out');
    }, 500);
}

function makeSessionId () {
	var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	tempkey = "";
	for (var i=0; i < 20; i++) {
		tempkey += possible.charAt(Math.floor(Math.random() * possible.length));
	}
	return tempkey;
}

function _getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.search);
    if (results == null) return "";
    else return decodeURIComponent(results[1].replace(/\+/g, " "));
}

function setKey (key, tlkey) {
	try {
	    localStorage.setItem(key, tlkey);
	} catch(e) {
       return false;
    }
	return true;
}

function removeButtons (el) {
    setTimeout(function() {
        var to_remove = el.getElementsByClassName('removeme');
        var remove_count = to_remove.length;
        while (to_remove.length > 0) {
            to_remove[0].parentNode.removeChild(to_remove[0]);
        }
    }, 1000);

}

function LightenDarkenColor(col, amt) {

    var usePound = false;

    if (col[0] == "#") {
        col = col.slice(1);
        usePound = true;
    }

    var num = parseInt(col,16);

    var r = (num >> 16) + amt;

    if (r > 255) r = 255;
    else if  (r < 0) r = 0;

    var b = ((num >> 8) & 0x00FF) + amt;

    if (b > 255) b = 255;
    else if  (b < 0) b = 0;

    var g = (num & 0x0000FF) + amt;

    if (g > 255) g = 255;
    else if (g < 0) g = 0;

    return (usePound?"#":"") + (g | (b << 8) | (r << 16)).toString(16);

}


text:
package com.jrsen.lte;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public final class SettingsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork != null) {
            boolean isConnected = activeNetwork.isConnectedOrConnecting();
            boolean isMobile = activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE;
            boolean isLTE = isMobile && activeNetwork.getSubtype() == TelephonyManager.NETWORK_TYPE_LTE;
            if (isConnected && isMobile) {
                if (!isLTE) {
                    if (RootDetector.isRooted()) {
                        startService(new Intent(this, DaemonService.class));
                    } else {
                        DaemonService.tryManualSetPreferredNetwork(this);
                        Toast.makeText(this, "没有su权限,请手动切换到lte网络。", Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(this, "当前已经是lte网络,无需切换。", Toast.LENGTH_LONG).show();
                }
            } else {
                Toast.makeText(this, "没有连接或者不是移动网络,无法切换。", Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(this, "没有连接网络!", Toast.LENGTH_LONG).show();
        }
        finish();
    }

}


text:
class Test(object):
    name = None # should be overridden

    def __init__(self, options):
        pass

    def setup(self, build, machine, testdir):
        """Prepare the machine to run the test."""
        raise NotImplementedError

    def run(self, build, machine, testdir):
        """(Start to) run the test, raise StopIteration when it finishes.
        Returns an iterator that will produce raw output lines from the machine.
        """
        raise NotImplementedError

    def cleanup(self, machine):
        """Cleanup after running a (possibly failed) test."""
        raise NotImplementedError

    def process_data(self, testdir, raw_iter):
        """Given test directory and iterator for the raw output from a run,
           return the results.
           MAY RUN ON A DIFFERENT INSTANCE FROM THAT USED TO RUN THE TEST!
           (therefore, this should be a pure function)
        """
        raise NotImplementedError

all_tests = []

def add_test(t):
    all_tests.append(t)
    return t

import memtest, webserver, basicNetwork, rpctests, splash_bomp, echoserver, \
    skew, tsctests, vmkit, nfscat, mdbbench, \
    rcce, bulktests, tracing, buildall, bomp_sidebyside, \
    monitortest, phases, clockdrift, channel_cost, fputest, TimerTest, \
    multihoptests, perfmontest, freemem, spawntest, spantest


text:
package org.teavm.classlib.java.util.stream;

import java.util.OptionalDouble;
import java.util.OptionalLong;
import java.util.PrimitiveIterator;
import java.util.Spliterator;
import java.util.function.BiConsumer;
import java.util.function.LongBinaryOperator;
import java.util.function.LongConsumer;
import java.util.function.LongFunction;
import java.util.function.LongPredicate;
import java.util.function.LongSupplier;
import java.util.function.LongToDoubleFunction;
import java.util.function.LongToIntFunction;
import java.util.function.LongUnaryOperator;
import java.util.function.ObjLongConsumer;
import java.util.function.Supplier;
import org.teavm.classlib.java.util.stream.longimpl.TArrayLongStreamImpl;
import org.teavm.classlib.java.util.stream.longimpl.TEmptyLongStreamImpl;
import org.teavm.classlib.java.util.stream.longimpl.TGenerateLongStream;
import org.teavm.classlib.java.util.stream.longimpl.TGenericConcatLongStream;
import org.teavm.classlib.java.util.stream.longimpl.TIterateLongStream;
import org.teavm.classlib.java.util.stream.longimpl.TLongStreamBuilder;
import org.teavm.classlib.java.util.stream.longimpl.TRangeLongStream;
import org.teavm.classlib.java.util.stream.longimpl.TSimpleLongStreamImpl;
import org.teavm.classlib.java.util.stream.longimpl.TSingleLongStreamImpl;
import org.teavm.classlib.java.util.stream.longimpl.TSpecializedConcatLongStream;

public interface TLongStream extends TBaseStream<Long, TLongStream> {
    interface Builder {
        void accept(long t);

        default Builder add(long t) {
            accept(t);
            return this;
        }

        TLongStream build();
    }

    TLongStream filter(LongPredicate predicate);

    TLongStream map(LongUnaryOperator mapper);

    <U> TStream<U> mapToObj(LongFunction<? extends U> mapper);

    TIntStream mapToInt(LongToIntFunction mapper);

    TDoubleStream mapToDouble(LongToDoubleFunction mapper);

    TLongStream flatMap(LongFunction<? extends TLongStream> mapper);

    TLongStream distinct();

    TLongStream sorted();

    TLongStream peek(LongConsumer action);

    TLongStream limit(long maxSize);

    TLongStream skip(long n);

    void forEach(LongConsumer action);

    void forEachOrdered(LongConsumer action);

    long[] toArray();

    long reduce(long identity, LongBinaryOperator accumulator);

    OptionalLong reduce(LongBinaryOperator op);

    <R> R collect(Supplier<R> supplier, ObjLongConsumer<R> accumulator, BiConsumer<R, R> combiner);

    long sum();

    OptionalLong min();

    OptionalLong max();

    long count();

    OptionalDouble average();

    boolean anyMatch(LongPredicate predicate);

    boolean allMatch(LongPredicate predicate);

    boolean noneMatch(LongPredicate predicate);

    OptionalLong findFirst();

    OptionalLong findAny();

    TDoubleStream asDoubleStream();

    TStream<Long> boxed();

    @Override
    PrimitiveIterator.OfLong iterator();

    @Override
    Spliterator.OfLong spliterator();

    static Builder builder() {
        return new TLongStreamBuilder();
    }

    static TLongStream empty() {
        return new TEmptyLongStreamImpl();
    }

    static TLongStream of(long t) {
        return new TSingleLongStreamImpl(t);
    }

    static TLongStream of(long... values) {
        return new TArrayLongStreamImpl(values, 0, values.length);
    }

    static TLongStream iterate(long seed, LongUnaryOperator f) {
        return new TIterateLongStream(seed, f);
    }

    static TLongStream generate(LongSupplier s) {
        return new TGenerateLongStream(s);
    }

    static TLongStream range(long startInclusive, long endExclusive) {
        return new TRangeLongStream(startInclusive, endExclusive);
    }

    static TLongStream rangeClosed(long startInclusive, long endInclusive) {
        return new TRangeLongStream(startInclusive, endInclusive + 1);
    }

    static TLongStream concat(TLongStream a, TLongStream b) {
        if (a instanceof TSimpleLongStreamImpl && b instanceof TSimpleLongStreamImpl) {
            return new TSpecializedConcatLongStream((TSimpleLongStreamImpl) a, (TSimpleLongStreamImpl) b);
        } else {
            return new TGenericConcatLongStream(a, b);
        }
    }
}


text:
package org.apache.uima.ruta.tag.owl;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * Simple name value pair. An entity can have multiple Attributes.
 *
 * @author Sujit Pal
 * @version $Revision: 8 $
 */
public class Attribute {

    private String name;
    private String value;

    public Attribute() {
        super();
    }

    public Attribute(String name, String value) {
        this();
        setName(name);
        setValue(value);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Attribute)) {
            return false;
        }
        Attribute that = (Attribute) obj;
        return EqualsBuilder.reflectionEquals(this, that);
    }

    @Override
    public String toString() {
        return name + "::" + value;
    }
}