text:
"""
.. module:: burpui.api.client
    :platform: Unix
    :synopsis: Burp-UI client api module.

.. moduleauthor:: Ziirish <hi+burpui@ziirish.me>

"""
import os
import re

from . import api, cache_key, force_refresh
from ..engines.server import BUIServer  # noqa
from .custom import fields, Resource
from ..decorators import browser_cache
from ..ext.cache import cache
from ..exceptions import BUIserverException

from flask_restx.marshalling import marshal
from flask_restx import inputs
from flask import current_app, request
from flask_login import current_user

bui = current_app  # type: BUIServer
ns = api.namespace("client", "Client methods")

node_fields = ns.model(
    "ClientTree",
    {
        "date": fields.DateTime(
            required=True,
            dt_format="iso8601",
            description="Human representation of the backup date",
        ),
        "gid": fields.Integer(required=True, description="gid owner of the node"),
        "inodes": fields.Integer(required=True, description="Inodes of the node"),
        "mode": fields.String(
            required=True, description='Human readable mode. Example: "drwxr-xr-x"'
        ),
        "name": fields.String(required=True, description="Node name"),
        "title": fields.SafeString(
            required=True, description="Node name (alias)", attribute="name"
        ),
        "fullname": fields.String(required=True, description="Full name of the Node"),
        "key": fields.String(
            required=True,
            description="Full name of the Node (alias)",
            attribute="fullname",
        ),
        "parent": fields.String(required=True, description="Parent node name"),
        "size": fields.String(
            required=True, description='Human readable size. Example: "12.0KiB"'
        ),
        "type": fields.String(required=True, description='Node type. Example: "d"'),
        "uid": fields.Integer(required=True, description="uid owner of the node"),
        "selected": fields.Boolean(
            required=False, description="Is path selected", default=False
        ),
        "lazy": fields.Boolean(
            required=False,
            description="Do the children have been loaded during this"
            + " request or not",
            default=True,
        ),
        "folder": fields.Boolean(required=True, description="Is it a folder"),
        "expanded": fields.Boolean(
            required=False, description="Should we expand the node", default=False
        ),
        # Cannot use nested on own
        "children": fields.Raw(required=False, description="List of children"),
    },
)

@ns.route(
    "/browse/<name>/<int:backup>",
    "/<server>/browse/<name>/<int:backup>",
    endpoint="client_tree",
)
@ns.doc(
    params={
        "server": "Which server to collect data from when in" + " multi-agent mode",
        "name": "Client name",
        "backup": "Backup number",
    },
)
class ClientTree(Resource):
    """The :class:`burpui.api.client.ClientTree` resource allows you to
    retrieve a list of files in a given backup.

    This resource is part of the :mod:`burpui.api.client` module.

    An optional ``GET`` parameter called ``serverName`` is supported when
    running in multi-agent mode.
    A mandatory ``GET`` parameter called ``root`` is used to know what path we
    are working on.
    """

    parser = ns.parser()
    parser.add_argument(
        "serverName", help="Which server to collect data from when in multi-agent mode"
    )
    parser.add_argument(
        "root",
        help="Root path to expand. You may specify several of them",
        action="append",
    )
    parser.add_argument(
        "recursive",
        type=inputs.boolean,
        help="Returns the whole tree instead of just the sub-tree",
        nullable=True,
        required=False,
        default=False,
    )
    parser.add_argument(
        "selected",
        type=inputs.boolean,
        help="Make the returned path selected at load time. Only works"
        + " if 'recursive' is True",
        nullable=True,
        required=False,
        default=False,
    )
    parser.add_argument(
        "init",
        type=inputs.boolean,
        help="First call to load the root of the tree",
        nullable=True,
        required=False,
        default=False,
    )

    @cache.cached(timeout=3600, key_prefix=cache_key, unless=force_refresh)
    @ns.marshal_list_with(node_fields, code=200, description="Success")
    @ns.expect(parser)
    @ns.doc(
        responses={
            "403": "Insufficient permissions",
            "500": "Internal failure",
        },
    )
    @browser_cache(3600)
    def get(self, server=None, name=None, backup=None):
        """Returns a list of 'nodes' under a given path

        **GET** method provided by the webservice.

        The *JSON* returned is:
        ::

            [
              {
                "date": "2015-05-21 14:54:49",
                "gid": "0",
                "inodes": "173",
                "selected": false,
                "expanded": false,
                "children": [],
                "mode": "drwxr-xr-x",
                "name": "/",
                "key": "/",
                "title": "/",
                "fullname": "/",
                "parent": "",
                "size": "12.0KiB",
                "type": "d",
                "uid": "0"
              }
            ]

        The output is filtered by the :mod:`burpui.misc.acl` module so that you
        only see stats about the clients you are authorized to.

        :param server: Which server to collect data from when in multi-agent
                       mode
        :type server: str

        :param name: The client we are working on
        :type name: str

        :param backup: The backup we are working on
        :type backup: int

        :returns: The *JSON* described above.
        """
        args = self.parser.parse_args()
        server = server or args["serverName"]
        json = []
        if not name or not backup:
            return json

        root_list = sorted(args["root"]) if args["root"] else []
        root_loaded = False
        paths_loaded = []
        to_select_list = []

        if (
            not current_user.is_anonymous
            and not current_user.acl.is_admin()
            and not current_user.acl.is_client_allowed(name, server)
        ):
            self.abort(403, "Sorry, you are not allowed to view this client")

        from_cookie = None
        if args["init"] and not root_list:
            from_cookie = request.cookies.get("fancytree-1-expanded", "")
            if from_cookie:
                args["recursive"] = True
                _root = bui.client.get_tree(name, backup, agent=server)
                root_list = [x["name"] for x in _root]
                for path in from_cookie.split("~"):
                    if not path.endswith("/"):
                        path += "/"
                    if path not in root_list:
                        root_list.append(path)
                root_list = sorted(root_list)

        try:
            root_list_clean = []

            for root in root_list:
                if args["recursive"]:
                    path = ""
                    # fetch the root first if not already loaded
                    if not root_loaded:
                        part = bui.client.get_tree(name, backup, level=0, agent=server)
                        root_loaded = True
                    else:
                        part = []
                    root = root.rstrip("/")
                    to_select = root.rsplit("/", 1)
                    if not to_select[0]:
                        to_select[0] = "/"
                    if len(to_select) == 1:
                        # special case we want to select '/'
                        to_select = ("", "/")
                    if not root:
                        root = "/"
                    to_select_list.append(to_select)
                    root_list_clean.append(root)
                    paths = root.split("/")
                    for level, sub in enumerate(paths, start=1):
                        path = os.path.join(path, sub)
                        if not path:
                            path = "/"
                        if path in paths_loaded:
                            continue
                        temp = bui.client.get_tree(
                            name, backup, path, level, agent=server
                        )
                        paths_loaded.append(path)
                        part += temp
                else:
                    part = bui.client.get_tree(name, backup, root, agent=server)
                json += part

            if args["selected"]:
                for entry in json:
                    for parent, fold in to_select_list:
                        if entry["parent"] == parent and entry["name"] == fold:
                            entry["selected"] = True
                            break
                    if entry["parent"] in root_list_clean:
                        entry["selected"] = True

            if not root_list:
                json = bui.client.get_tree(name, backup, agent=server)
                if args["selected"]:
                    for entry in json:
                        if not entry["parent"]:
                            entry["selected"] = True

            if args["recursive"]:
                tree = {}
                rjson = []
                roots = []
                for entry in json:
                    # /!\ after marshalling, 'fullname' will be 'key'
                    tree[entry["fullname"]] = marshal(entry, node_fields)

                for key, entry in tree.items():
                    parent = entry["parent"]
                    if not entry["children"]:
                        entry["children"] = None
                    if parent:
                        node = tree[parent]
                        if not node["children"]:
                            node["children"] = []
                        node["children"].append(entry)
                        if node["folder"]:
                            node["lazy"] = False
                            node["expanded"] = True
                    else:
                        roots.append(entry["key"])

                for fullname in roots:
                    rjson.append(tree[fullname])

                json = rjson
            else:
                for entry in json:
                    entry["children"] = None
                    if not entry["folder"]:
                        entry["lazy"] = False

        except BUIserverException as e:
            self.abort(500, str(e))
        return json

@ns.route(
    "/browseall/<name>/<int:backup>",
    "/<server>/browseall/<name>/<int:backup>",
    endpoint="client_tree_all",
)
@ns.doc(
    params={
        "server": "Which server to collect data from when in" + " multi-agent mode",
        "name": "Client name",
        "backup": "Backup number",
    },
)
class ClientTreeAll(Resource):
    """The :class:`burpui.api.client.ClientTreeAll` resource allows you to
    retrieve a list of all the files in a given backup.

    This resource is part of the :mod:`burpui.api.client` module.

    An optional ``GET`` parameter called ``serverName`` is supported when
    running in multi-agent mode.
    """

    parser = ns.parser()
    parser.add_argument(
        "serverName", help="Which server to collect data from when in multi-agent mode"
    )

    @cache.cached(timeout=3600, key_prefix=cache_key, unless=force_refresh)
    @ns.marshal_list_with(node_fields, code=200, description="Success")
    @ns.expect(parser)
    @ns.doc(
        responses={
            "403": "Insufficient permissions",
            "405": "Method not allowed",
            "500": "Internal failure",
        },
    )
    @browser_cache(3600)
    def get(self, server=None, name=None, backup=None):
        """Returns a list of all 'nodes' of a given backup

        **GET** method provided by the webservice.

        The *JSON* returned is:
        ::

            [
              {
                "date": "2015-05-21 14:54:49",
                "gid": "0",
                "inodes": "173",
                "selected": false,
                "expanded": false,
                "children": [],
                "mode": "drwxr-xr-x",
                "name": "/",
                "key": "/",
                "title": "/",
                "fullname": "/",
                "parent": "",
                "size": "12.0KiB",
                "type": "d",
                "uid": "0"
              }
            ]

        The output is filtered by the :mod:`burpui.misc.acl` module so that you
        only see stats about the clients you are authorized to.

        :param server: Which server to collect data from when in multi-agent
                       mode
        :type server: str

        :param name: The client we are working on
        :type name: str

        :param backup: The backup we are working on
        :type backup: int

        :returns: The *JSON* described above.
        """
        args = self.parser.parse_args()
        server = server or args["serverName"]

        if not bui.client.get_attr("batch_list_supported", False, server):
            self.abort(405, "Sorry, the requested backend does not support this method")

        if (
            not current_user.is_anonymous
            and not current_user.acl.is_admin()
            and not current_user.acl.is_client_allowed(name, server)
        ):
            self.abort(403, "Sorry, you are not allowed to view this client")

        try:
            json = self._get_tree_all(name, backup, server)
        except BUIserverException as e:
            self.abort(500, str(e))
        return json

    @staticmethod
    def _get_tree_all(name, backup, server):
        json = bui.client.get_tree(name, backup, "*", agent=server)
        tree = {}
        rjson = []
        roots = []

        def __expand_json(js):
            res = {}
            for entry in js:
                # /!\ after marshalling, 'fullname' will be 'key'
                res[entry["fullname"]] = marshal(entry, node_fields)
            return res

        tree = __expand_json(json)

        # TODO: we can probably improve this at some point
        redo = True
        while redo:
            redo = False
            for key, entry in tree.items().copy():
                parent = entry["parent"]
                if not entry["children"]:
                    entry["children"] = None
                if parent:
                    if parent not in tree:
                        parent2 = parent
                        last = False
                        while parent not in tree and not last:
                            if not parent2:
                                last = True
                            json = bui.client.get_tree(
                                name, backup, parent2, agent=server
                            )
                            if parent2 == "/":
                                parent2 = ""
                            else:
                                parent2 = os.path.dirname(parent2)
                            tree2 = __expand_json(json)
                            tree.update(tree2)
                        roots = []
                        redo = True
                        break
                    node = tree[parent]
                    if not node["children"]:
                        node["children"] = []
                    elif entry in node["children"]:
                        continue
                    node["children"].append(entry)
                    if node["folder"]:
                        node["lazy"] = False
                        node["expanded"] = False
                else:
                    roots.append(entry["key"])

        for fullname in roots:
            rjson.append(tree[fullname])

        return rjson

@ns.route(
    "/report/<name>",
    "/<server>/report/<name>",
    "/report/<name>/<int:backup>",
    "/<server>/report/<name>/<int:backup>",
    endpoint="client_report",
)
@ns.doc(
    params={
        "server": "Which server to collect data from when in multi-agent" + " mode",
        "name": "Client name",
        "backup": "Backup number",
    },
)
class ClientReport(Resource):
    """The :class:`burpui.api.client.ClientStats` resource allows you to
    retrieve a report on a given backup for a given client.

    This resource is part of the :mod:`burpui.api.client` module.

    An optional ``GET`` parameter called ``serverName`` is supported when
    running in multi-agent mode.
    """

    parser = ns.parser()
    parser.add_argument(
        "serverName", help="Which server to collect data from when in multi-agent mode"
    )
    report_tpl_fields = ns.model(
        "ClientReportTpl",
        {
            "changed": fields.Integer(
                required=True, description="Number of changed files", default=0
            ),
            "deleted": fields.Integer(
                required=True, description="Number of deleted files", default=0
            ),
            "new": fields.Integer(
                required=True, description="Number of new files", default=0
            ),
            "scanned": fields.Integer(
                required=True, description="Number of scanned files", default=0
            ),
            "total": fields.Integer(
                required=True, description="Total number of files", default=0
            ),
            "unchanged": fields.Integer(
                required=True, description="Number of scanned files", default=0
            ),
        },
    )
    report_fields = ns.model(
        "ClientReport",
        {
            "dir": fields.Nested(report_tpl_fields, required=True),
            "duration": fields.Integer(
                required=True, description="Backup duration in seconds"
            ),
            "efs": fields.Nested(report_tpl_fields, required=True),
            "encrypted": fields.Boolean(
                required=True, description="Is the backup encrypted"
            ),
            "end": fields.DateTime(
                dt_format="iso8601",
                required=True,
                description="Timestamp of the end date of the backup",
            ),
            "files": fields.Nested(report_tpl_fields, required=True),
            "files_enc": fields.Nested(report_tpl_fields, required=True),
            "hardlink": fields.Nested(report_tpl_fields, required=True),
            "meta": fields.Nested(report_tpl_fields, required=True),
            "meta_enc": fields.Nested(report_tpl_fields, required=True),
            "number": fields.Integer(required=True, description="Backup number"),
            "received": fields.Integer(required=True, description="Bytes received"),
            "softlink": fields.Nested(report_tpl_fields, required=True),
            "special": fields.Nested(report_tpl_fields, required=True),
            "start": fields.DateTime(
                dt_format="iso8601",
                required=True,
                description="Timestamp of the beginning of the backup",
            ),
            "totsize": fields.Integer(
                required=True, description="Total size of the backup"
            ),
            "vssfooter": fields.Nested(report_tpl_fields, required=True),
            "vssfooter_enc": fields.Nested(report_tpl_fields, required=True),
            "vssheader": fields.Nested(report_tpl_fields, required=True),
            "vssheader_enc": fields.Nested(report_tpl_fields, required=True),
            "windows": fields.Boolean(
                required=True, description="Is the client a windows system"
            ),
        },
    )

    @cache.cached(timeout=1800, key_prefix=cache_key, unless=force_refresh)
    @ns.marshal_with(report_fields, code=200, description="Success")
    @ns.expect(parser)
    @ns.doc(
        responses={
            "403": "Insufficient permissions",
            "500": "Internal failure",
        },
    )
    @browser_cache(1800)
    def get(self, server=None, name=None, backup=None):
        """Returns a global report of a given backup/client

        **GET** method provided by the webservice.

        The *JSON* returned is:
        ::

            {
              "dir": {
                "changed": 0,
                "deleted": 0,
                "new": 394,
                "scanned": 394,
                "total": 394,
                "unchanged": 0
              },
              "duration": 5,
              "efs": {
                "changed": 0,
                "deleted": 0,
                "new": 0,
                "scanned": 0,
                "total": 0,
                "unchanged": 0
              },
              "encrypted": true,
              "end": 1422189124,
              "files": {
                "changed": 0,
                "deleted": 0,
                "new": 0,
                "scanned": 0,
                "total": 0,
                "unchanged": 0
              },
              "files_enc": {
                "changed": 0,
                "deleted": 0,
                "new": 1421,
                "scanned": 1421,
                "total": 1421,
                "unchanged": 0
              },
              "hardlink": {
                "changed": 0,
                "deleted": 0,
                "new": 0,
                "scanned": 0,
                "total": 0,
                "unchanged": 0
              },
              "meta": {
                "changed": 0,
                "deleted": 0,
                "new": 0,
                "scanned": 0,
                "total": 0,
                "unchanged": 0
              },
              "meta_enc": {
                "changed": 0,
                "deleted": 0,
                "new": 0,
                "scanned": 0,
                "total": 0,
                "unchanged": 0
              },
              "number": 1,
              "received": 1679304,
              "softlink": {
                "changed": 0,
                "deleted": 0,
                "new": 1302,
                "scanned": 1302,
                "total": 1302,
                "unchanged": 0
              },
              "special": {
                "changed": 0,
                "deleted": 0,
                "new": 0,
                "scanned": 0,
                "total": 0,
                "unchanged": 0
              },
              "start": 1422189119,
              "total": {
                "changed": 0,
                "deleted": 0,
                "new": 3117,
                "scanned": 3117,
                "total": 3117,
                "unchanged": 0
              },
              "totsize": 5345361,
              "vssfooter": {
                "changed": 0,
                "deleted": 0,
                "new": 0,
                "scanned": 0,
                "total": 0,
                "unchanged": 0
              },
              "vssfooter_enc": {
                "changed": 0,
                "deleted": 0,
                "new": 0,
                "scanned": 0,
                "total": 0,
                "unchanged": 0
              },
              "vssheader": {
                "changed": 0,
                "deleted": 0,
                "new": 0,
                "scanned": 0,
                "total": 0,
                "unchanged": 0
              },
              "vssheader_enc": {
                "changed": 0,
                "deleted": 0,
                "new": 0,
                "scanned": 0,
                "total": 0,
                "unchanged": 0
              },
              "windows": "false"
            }

        The output is filtered by the :mod:`burpui.misc.acl` module so that you
        only see stats about the clients you are authorized to.

        :param server: Which server to collect data from when in multi-agent
                       mode
        :type server: str

        :param name: The client we are working on
        :type name: str

        :param backup: The backup we are working on
        :type backup: int

        :returns: The *JSON* described above.
        """
        server = server or self.parser.parse_args()["serverName"]
        json = []
        if not name:
            err = [[1, "No client defined"]]
            self.abort(400, err)

        if (
            not current_user.is_anonymous
            and not current_user.acl.is_admin()
            and not current_user.acl.is_client_allowed(name, server)
        ):
            self.abort(403, "You don't have rights to view this client report")
        if backup:
            try:
                json = bui.client.get_backup_logs(backup, name, agent=server)
            except BUIserverException as exp:
                self.abort(500, str(exp))
        else:
            try:
                json = bui.client.get_backup_logs(-1, name, agent=server)
            except BUIserverException as exp:
                self.abort(500, str(exp))
        return json

    @api.disabled_on_demo()
    @ns.marshal_with(report_fields, code=202, description="Success")
    @ns.expect(parser)
    @ns.doc(
        responses={
            "400": "Missing arguments",
            "403": "Insufficient permissions",
            "500": "Internal failure",
        },
    )
    def delete(self, name, backup, server=None):
        """Deletes a given backup from the server

        **DELETE** method provided by the webservice.

        The access is filtered by the :mod:`burpui.misc.acl` module so that you
        can only delete backups you have access to.

        :param server: Which server to collect data from when in multi-agent
                       mode
        :type server: str

        :param name: The client we are working on
        :type name: str

        :param backup: The backup we are working on
        :type backup: int
        """
        server = server or self.parser.parse_args()["serverName"]
        if not name:
            err = [[1, "No client defined"]]
            self.abort(400, err)

        if (
            not current_user.is_anonymous
            and not current_user.acl.is_admin()
            and (
                not current_user.acl.is_moderator()
                or current_user.acl.is_moderator()
                and not current_user.acl.is_client_rw(name, server)
            )
        ):
            self.abort(403, "You don't have rights on this client")

        msg = bui.client.delete_backup(name, backup, server)
        if msg:
            self.abort(500, msg)
        bui.audit.logger.info(
            f"requested the deletion of backup {backup} for {name}", server=server
        )
        return 202, ""

@ns.route("/stats/<name>", "/<server>/stats/<name>", endpoint="client_stats")
@ns.doc(
    params={
        "server": "Which server to collect data from when in multi-agent" + " mode",
        "name": "Client name",
    },
)
class ClientStats(Resource):
    """The :class:`burpui.api.client.ClientReport` resource allows you to
    retrieve a list of backups for a given client.

    This resource is part of the :mod:`burpui.api.client` module.

    An optional ``GET`` parameter called ``serverName`` is supported when
    running in multi-agent mode.
    """

    parser = ns.parser()
    parser.add_argument(
        "serverName", help="Which server to collect data from when in multi-agent mode"
    )
    client_fields = ns.model(
        "ClientStats",
        {
            "number": fields.Integer(required=True, description="Backup number"),
            "received": fields.Integer(required=True, description="Bytes received"),
            "size": fields.Integer(required=True, description="Total size"),
            "encrypted": fields.Boolean(
                required=True, description="Is the backup encrypted"
            ),
            "deletable": fields.Boolean(
                required=True, description="Is the backup deletable"
            ),
            "date": fields.DateTime(
                required=True,
                dt_format="iso8601",
                description="Human representation of the backup date",
            ),
        },
    )

    @cache.cached(timeout=1800, key_prefix=cache_key, unless=force_refresh)
    @ns.marshal_list_with(client_fields, code=200, description="Success")
    @ns.expect(parser)
    @ns.doc(
        responses={
            "403": "Insufficient permissions",
            "500": "Internal failure",
        },
    )
    @browser_cache(1800)
    def get(self, server=None, name=None):
        """Returns a list of backups for a given client

        **GET** method provided by the webservice.

        The *JSON* returned is:
        ::

            [
              {
                "date": "2015-01-25 13:32:00",
                "deletable": true,
                "encrypted": true,
                "received": 123,
                "size": 1234,
                "number": 1
              },
            ]

        The output is filtered by the :mod:`burpui.misc.acl` module so that you
        only see stats about the clients you are authorized to.

        :param server: Which server to collect data from when in multi-agent
                       mode
        :type server: str

        :param name: The client we are working on
        :type name: str

        :returns: The *JSON* described above.
        """
        server = server or self.parser.parse_args()["serverName"]
        try:
            if (
                not current_user.is_anonymous
                and not current_user.acl.is_admin()
                and not current_user.acl.is_client_allowed(name, server)
            ):
                self.abort(403, "Sorry, you cannot access this client")
            json = bui.client.get_client(name, agent=server)
        except BUIserverException as exp:
            self.abort(500, str(exp))
        return json

@ns.route("/labels/<name>", "/<server>/labels/<name>", endpoint="client_labels")
@ns.doc(
    params={
        "server": "Which server to collect data from when in multi-agent" + " mode",
        "name": "Client name",
    },
)
class ClientLabels(Resource):
    """The :class:`burpui.api.client.ClientLabels` resource allows you to
    retrieve the labels of a given client.

    This resource is part of the :mod:`burpui.api.client` module.

    An optional ``GET`` parameter called ``serverName`` is supported when
    running in multi-agent mode.
    """

    parser = ns.parser()
    parser.add_argument(
        "serverName", help="Which server to collect data from when in multi-agent mode"
    )
    parser.add_argument("clientName", help="Client name")
    labels_fields = ns.model(
        "ClientLabels",
        {
            "labels": fields.List(fields.String, description="List of labels"),
        },
    )

    @cache.cached(timeout=1800, key_prefix=cache_key, unless=force_refresh)
    @ns.marshal_list_with(labels_fields, code=200, description="Success")
    @ns.expect(parser)
    @ns.doc(
        responses={
            "403": "Insufficient permissions",
            "500": "Internal failure",
        },
    )
    @browser_cache(1800)
    def get(self, server=None, name=None):
        """Returns the labels of a given client

        **GET** method provided by the webservice.

        The *JSON* returned is:
        ::

            {
              "labels": [
                "label1",
                "label2"
              ]
            }

        The output is filtered by the :mod:`burpui.misc.acl` module so that you
        only see stats about the clients you are authorized to.

        :param server: Which server to collect data from when in multi-agent
                       mode
        :type server: str

        :param name: The client we are working on
        :type name: str

        :returns: The *JSON* described above.
        """
        try:
            if (
                not current_user.is_anonymous
                and not current_user.acl.is_admin()
                and not current_user.acl.is_client_allowed(name, server)
            ):
                self.abort(403, "Sorry, you cannot access this client")
            labels = self._get_labels(name, server)
        except BUIserverException as exp:
            self.abort(500, str(exp))
        return {"labels": labels}

    @staticmethod
    def _get_labels(client, server=None):
        key = "labels-{}-{}".format(client, server)
        ret = cache.cache.get(key)
        if ret is not None:
            return ret
        labels = bui.client.get_client_labels(client, agent=server)
        ret = []
        ignore = re.compile("|".join(bui.ignore_labels)) if bui.ignore_labels else None
        reformat = (
            [(re.compile(regex), replace) for regex, replace in bui.format_labels]
            if bui.format_labels
            else []
        )
        for label in labels:
            if bui.ignore_labels and ignore.search(label):
                continue
            tmp_label = label
            for regex, replace in reformat:
                tmp_label = regex.sub(replace, tmp_label)
            ret.append(tmp_label)
        cache.cache.set(key, ret, 1800)
        return ret

@ns.route(
    "/running",
    "/running/<name>",
    "/<server>/running",
    "/<server>/running/<name>",
    endpoint="client_running_status",
)
@ns.doc(
    params={
        "server": "Which server to collect data from when in multi-agent" + " mode",
        "name": "Client name",
    },
)
class ClientRunningStatus(Resource):
    """The :class:`burpui.api.client.ClientRunningStatus` resource allows you to
    retrieve the running status of a given client.

    This resource is part of the :mod:`burpui.api.client` module.

    An optional ``GET`` parameter called ``serverName`` is supported when
    running in multi-agent mode.
    """

    parser = ns.parser()
    parser.add_argument(
        "serverName", help="Which server to collect data from when in multi-agent mode"
    )
    parser.add_argument("clientName", help="Client name")
    running_fields = ns.model(
        "ClientRunningStatus",
        {
            "state": fields.LocalizedString(required=True, description="Running state"),
            "percent": fields.Integer(
                required=False, description="Backup progress in percent", default=-1
            ),
            "phase": fields.String(
                required=False, description="Backup phase", default=None
            ),
            "last": fields.DateTime(
                required=False, dt_format="iso8601", description="Date of last backup"
            ),
        },
    )

    @ns.marshal_list_with(running_fields, code=200, description="Success")
    @ns.expect(parser)
    @ns.doc(
        responses={
            "403": "Insufficient permissions",
            "500": "Internal failure",
        },
    )
    def get(self, server=None, name=None):
        """Returns the running status of a given client

        **GET** method provided by the webservice.

        The *JSON* returned is:
        ::

            {
              "state": "running",
              "percent": 42,
              "phase": "2",
              "last": "now"
            }

        The output is filtered by the :mod:`burpui.misc.acl` module so that you
        only see stats about the clients you are authorized to.

        :param server: Which server to collect data from when in multi-agent
                       mode
        :type server: str

        :param name: The client we are working on
        :type name: str

        :returns: The *JSON* described above.
        """
        args = self.parser.parse_args()
        server = server or args["serverName"]
        name = name or args["clientName"]
        try:
            if (
                not current_user.is_anonymous
                and not current_user.acl.is_admin()
                and not current_user.acl.is_client_allowed(name, server)
            ):
                self.abort(403, "Sorry, you cannot access this client")
            json = bui.client.get_client_status(name, agent=server)
        except BUIserverException as exp:
            self.abort(500, str(exp))
        return json


text:
from __future__ import (
    absolute_import, division, print_function, unicode_literals)

from sqlalchemy import event
from sqlalchemy.engine import Engine
from builtins import *

@event.listens_for(Engine, 'connect')
def set_sqlite_pragma(dbapi_connection, connection_record):
    if 'sqlite3' in str(dbapi_connection.cursor):
        """Enable foreign key constraints for SQLite."""
        cursor = dbapi_connection.cursor()
        cursor.execute("PRAGMA foreign_keys=ON")
        cursor.close()

redpajama_set_name: RedPajamaGithub


text:
/*
 * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
 */

#ifndef __GTK_CURVE_H__
#define __GTK_CURVE_H__

#include <gdk/gdk.h>
#include <gtk/gtkdrawingarea.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#define GTK_TYPE_CURVE                  (gtk_curve_get_type ())
#define GTK_CURVE(obj)                  (GTK_CHECK_CAST ((obj), GTK_TYPE_CURVE, GtkCurve))
#define GTK_CURVE_CLASS(klass)          (GTK_CHECK_CLASS_CAST ((klass), GTK_TYPE_CURVE, GtkCurveClass))
#define GTK_IS_CURVE(obj)               (GTK_CHECK_TYPE ((obj), GTK_TYPE_CURVE))
#define GTK_IS_CURVE_CLASS(klass)       (GTK_CHECK_CLASS_TYPE ((klass), GTK_TYPE_CURVE))
#define GTK_CURVE_GET_CLASS(obj)        (GTK_CHECK_GET_CLASS ((obj), GTK_TYPE_CURVE, GtkCurveClass))

typedef struct _GtkCurve	GtkCurve;
typedef struct _GtkCurveClass	GtkCurveClass;

struct _GtkCurve
{
  GtkDrawingArea graph;

  gint cursor_type;
  gfloat min_x;
  gfloat max_x;
  gfloat min_y;
  gfloat max_y;
  GdkPixmap *pixmap;
  GtkCurveType curve_type;
  gint height;                  /* (cached) graph height in pixels */
  gint grab_point;              /* point currently grabbed */
  gint last;

  /* (cached) curve points: */
  gint num_points;
  GdkPoint *point;

  /* control points: */
  gint num_ctlpoints;           /* number of control points */
  gfloat (*ctlpoint)[2];        /* array of control points */
};

struct _GtkCurveClass
{
  GtkDrawingAreaClass parent_class;

  void (* curve_type_changed) (GtkCurve *curve);
};

GtkType		gtk_curve_get_type	(void);
GtkWidget*	gtk_curve_new		(void);
void		gtk_curve_reset		(GtkCurve *curve);
void		gtk_curve_set_gamma	(GtkCurve *curve, gfloat gamma);
void		gtk_curve_set_range	(GtkCurve *curve,
					 gfloat min_x, gfloat max_x,
					 gfloat min_y, gfloat max_y);
void		gtk_curve_get_vector	(GtkCurve *curve,
					 int veclen, gfloat vector[]);
void		gtk_curve_set_vector	(GtkCurve *curve,
					 int veclen, gfloat vector[]);
void		gtk_curve_set_curve_type (GtkCurve *curve, GtkCurveType type);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __GTK_CURVE_H__ */


text:
module.exports = {
  pages: ['index', 'about', { name: 'demo', independent: true }],
  chunks: {
    vendor: true,
    common: true,
  }
}


text:
package example.repo;

import example.model.Customer143;

import java.util.List;

import org.springframework.data.repository.CrudRepository;

public interface Customer143Repository extends CrudRepository<Customer143, Long> {

	List<Customer143> findByLastName(String lastName);
}


text:
package org.dspace.app.xmlui.aspect.discovery;

import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;

import org.apache.cocoon.caching.CacheableProcessingComponent;
import org.apache.excalibur.source.SourceValidity;
import org.apache.excalibur.source.impl.validity.NOPValidity;
import org.dspace.app.xmlui.cocoon.AbstractDSpaceTransformer;
import org.dspace.app.xmlui.utils.UIException;
import org.dspace.app.xmlui.wing.Message;
import org.dspace.app.xmlui.wing.WingException;
import org.dspace.app.xmlui.wing.element.Body;
import org.dspace.app.xmlui.wing.element.Division;
import org.dspace.app.xmlui.wing.element.PageMeta;
import org.dspace.app.xmlui.wing.element.Para;
import org.dspace.authorize.AuthorizeException;
import org.dspace.core.ConfigurationManager;
import org.xml.sax.SAXException;

/**
 * Adds a searchbox on the dspace home page
 *
 * @author Kevin Van de Velde (kevin at atmire dot com)
 * @author Mark Diggory (markd at atmire dot com)
 * @author Ben Bosman (ben at atmire dot com)
 *
 * Front page search has been removed
 */
public class SiteViewer extends AbstractDSpaceTransformer implements CacheableProcessingComponent
{
    /** Language Strings */

    public static final Message T_dspace_home =
        message("xmlui.general.dspace_home");

    private static final Message T_head =
        message("xmlui.ArtifactBrowser.FrontPageSearch.head");

    private static final Message T_para1 =
        message("xmlui.ArtifactBrowser.FrontPageSearch.para1");

    private static final Message T_go =
        message("xmlui.general.go");

    /**
     * Generate the unique caching key.
     * This key must be unique inside the space of this component.
     */
    public Serializable getKey()
    {
       return "1";
    }

    /**
     * Generate the cache validity object.
     */
    public SourceValidity getValidity()
    {
        return NOPValidity.SHARED_INSTANCE;
    }

    /**
     * Add a page title and trail links.
     */
    public void addPageMeta(PageMeta pageMeta) throws SAXException,
            WingException, UIException, SQLException, IOException,
            AuthorizeException
    {
    	pageMeta.addMetadata("title").addContent(T_dspace_home);
    	pageMeta.addTrailLink(contextPath, T_dspace_home);

        // Add RSS links if available
        String formats = ConfigurationManager.getProperty("webui.feed.formats");
		if ( formats != null )
		{
			for (String format : formats.split(","))
			{
				// Remove the protocol number, i.e. just list 'rss' or' atom'
				String[] parts = format.split("_");
				if (parts.length < 1)
                {
                    continue;
                }

				String feedFormat = parts[0].trim()+"+xml";

				String feedURL = contextPath+"/feed/"+format.trim()+"/site";
				pageMeta.addMetadata("feed", feedFormat).addContent(feedURL);
			}
		}
    }

    public void addBody(Body body) throws SAXException, WingException,
            UIException, SQLException, IOException, AuthorizeException
    {
//        Division search =
//        	body.addInteractiveDivision("front-page-search",contextPath+"/discover",Division.METHOD_GET,"primary");
//
//        search.setHead(T_head);
//
//        search.addPara(T_para1);
//
//        Para fields = search.addPara();
//        fields.addText("query");
//        fields.addHidden("location").setValue("l2");
//        fields.addButton("submit").setValue(T_go);
//        fields.addFigure("/themes/Dryad/images/help.png", "/searching", "search-help");
    }
}


text:
//The MIT License(MIT)
//
//Copyright(c) 2016 universalappfactory
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.

//Copyright(c) 2016 universalappfactory

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.

using SharpEnd.Pcl.Application.Contracts.Storage;
using System.Threading.Tasks;
using Windows.Storage;
using System;
using System.IO;

namespace Sharpend.UAP.Storage
{
    public static class FileExtensions
    {
        public enum FileCreation
        {
            DoNotCreateFile = 0,
            CreateIfNotExist = 1
        }

        public static async Task<StorageFile> ToStorageFile(this IFile file, FileCreation fileCreation)
        {
            if (fileCreation == FileCreation.CreateIfNotExist)
            {
                var storageFolder = await StorageFolder.GetFolderFromPathAsync(file.Folder.FullPath);

                var storageItem = await storageFolder.TryGetItemAsync(file.Filename);
                if (storageItem == null)
                {
                    return await storageFolder.CreateFileAsync(file.Filename);
                }
            }

            return await StorageFile.GetFileFromPathAsync(file.Fullname);
        }

        private static async Task<string> GetKnownLibraryFullPathAsync(KnownLibraryId libraryId)
        {
            var musicLibrary = await StorageLibrary.GetLibraryAsync(libraryId);
            return musicLibrary.SaveFolder.Path;
        }

        private static async Task<string> GetKnownLibraryFullPathAsync(KnownLibraryId libaryId, string filename)
        {
            return Path.Combine(await GetKnownLibraryFullPathAsync(libaryId), filename);
        }

        private static string GetKnownLibraryFullPath(KnownLibraryId libraryId)
        {
            var t = Task<string>.Run(async () =>
            {
                return await GetKnownLibraryFullPathAsync(libraryId);
            });
            t.Wait();
            return t.Result;
        }

        private static string GetFullPath(IFile file, KnownLibraryId libraryId)
        {
            var result = Path.Combine(GetKnownLibraryFullPath(libraryId), file.Fullname.TrimStart('/', '\\'));
            return result.Replace('/', '\\');
        }

        private static string GetFullPath(IFolder folder, KnownLibraryId libraryId)
        {
            var result = Path.Combine(GetKnownLibraryFullPath(libraryId), folder.FullPath.TrimStart('/', '\\'));
            return result.Replace('/', '\\');
        }

        private static string GetFullPath(StorageFolder storageFolder, IFolder folder)
        {
            var result = Path.Combine(storageFolder.Path, folder.FullPath.TrimStart('/', '\\'));
            return result.Replace('/', '\\');
        }

        private static string GetFullPath(StorageFolder storageFolder, IFile file)
        {
            var result = Path.Combine(storageFolder.Path, file.Fullname.TrimStart('/', '\\'));
            return result.Replace('/', '\\');
        }

        /// <summary>
        /// returns the full path of this file with the windows music library as root prefix
        /// usually this is something like c:\users\username\Music
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string MusicFolderFullName(this IFile file)
        {
            return GetFullPath(file, KnownLibraryId.Music);
        }

        /// <summary>
        /// returns the full path of this file with the windows music library as root prefix
        /// usually this is something like c:\users\username\Music
        /// </summary>
        /// <param name="folder"></param>
        /// <returns></returns>
        public static string MusicFolderFullname(this IFolder folder)
        {
            return GetFullPath(folder, KnownLibraryId.Music);
        }

        /// <summary>
        /// returns the full path of this file with the windows videos library as root prefix
        /// usually this is something like c:\users\username\Videos
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string VideosFolderFullname(this IFile file)
        {
            return GetFullPath(file, KnownLibraryId.Videos);
        }

        /// <summary>
        /// returns the full path of this file with the windows pictures library as root prefix
        /// usually this is something like c:\users\username\Pictures
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string PicturesFolderFullName(this IFile file)
        {
            return GetFullPath(file, KnownLibraryId.Pictures);
        }

        /// <summary>
        /// returns the full path of this file with the windows pictures library as root prefix
        /// usually this is something like c:\users\username\Pictures
        /// </summary>
        /// <param name="folder"></param>
        /// <returns></returns>
        public static string PicturesFolderFullname(this IFolder folder)
        {
            return GetFullPath(folder, KnownLibraryId.Pictures);
        }

        /// <summary>
        /// returns the full path of this file with the windows videos library as root prefix
        /// usually this is something like c:\users\username\Videos
        /// </summary>
        /// <param name="folder"></param>
        /// <returns></returns>
        public static string VideosFolderFullname(this IFolder folder)
        {
            return GetFullPath(folder, KnownLibraryId.Videos);
        }

        /// <summary>
        /// returns appdata cuurent local folder fullname for this folder
        /// </summary>
        /// <param name="folder"></param>
        /// <returns></returns>
        public static string LocalFolderFullname(this IFolder folder)
        {
            return GetFullPath(ApplicationData.Current.LocalFolder, folder);
        }

        /// <summary>
        /// returns appdata current local folder fullname for this file
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string LocalFolderFullname(this IFile file)
        {
            return GetFullPath(ApplicationData.Current.LocalFolder, file);
        }

        /// <summary>
        /// returns appdata cuurent temp folder fullname for this folder
        /// </summary>
        /// <param name="folder"></param>
        /// <returns></returns>
        public static string TempFolderFullname(this IFolder folder)
        {
            return GetFullPath(ApplicationData.Current.TemporaryFolder, folder);
        }

        /// <summary>
        /// returns appdata current temp folder fullname for this file
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string TempFolderFullname(this IFile file)
        {
            return GetFullPath(ApplicationData.Current.TemporaryFolder, file);
        }
        /* Hint for Documents
         *
         * https://docs.microsoft.com/en-us/uwp/api/windows.storage.knownfolders
         * The Documents library is not intended for general use. For more info, see App capability declarations.
         *
         */

    }
}


text:
'*******************************************************************************************'
'                                                                                '
' Download Free Evaluation Version From:     https://bytescout.com/download/web-installer   '
'                                                                                '
' Also available as Web API! Get free API Key https://app.pdf.co/signup                     '
'                                                                                '
' Copyright © 2017-2020 ByteScout, Inc. All rights reserved.                                '
' https://www.bytescout.com                                                                 '
' https://www.pdf.co                                                                        '
'*******************************************************************************************'

Imports System.IO
Imports System.Net
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

' Visit Knowledgebase for adding Text Macros to PDF
' https://apidocs.pdf.co/kb/Fill%20PDF%20and%20Add%20Text%20or%20Images%20to%20PDF/macros

Module Module1

    ' The authentication key (API Key).
    ' Get your own by registering at https://app.pdf.co
    Const API_KEY As String = "***********************************"

    ' Direct URL of source PDF file.
    ' You can also upload your own file into PDF.co and use it as url. Check "Upload File" samples for code snippets: https://github.com/bytescout/pdf-co-api-samples/tree/master/File%20Upload/
    Const SourceFileUrl As String = "https://bytescout-com.s3.amazonaws.com/files/demo-files/cloud-api/pdf-edit/sample.pdf"

    ' Comma-separated list of page indices (Or ranges) to process. Leave empty for all pages. Example '0,2-5,7-'.
    Const Pages As String = ""

    ' PDF document password. Leave empty for unprotected documents.
    Const Password As String = ""

    ' Destination PDF file name
    Const DestinationFile As String = ".\result.pdf"

    ' Text params

    Private Const Type As String = "annotation"
    Private Const Text As String = "Some notes will go here... Some notes will go here.... Some notes will go here....."
    Private Const FontName As String = "Times New Roman"
    Private Const FontSize As Decimal = 12
    Private Const Color As String = "FF0000"

    Sub Main()

        ' Create standard .NET web client instance
        Dim webClient As WebClient = New WebClient()

        ' Set API Key
        webClient.Headers.Add("x-api-key", API_KEY)

        ' Find Text coordinates to add Annotation
        Dim oCoordinates = FindCoordinates(API_KEY, SourceFileUrl, "Notes")

        Dim X As Integer = oCoordinates.x
        Dim Y As Integer = oCoordinates.y + 25

        ' Set JSON content type
        webClient.Headers.Add("Content-Type", "application/json")

        ' * Add text annotations *
        ' Prepare URL for `PDF Edit` API call
		Dim url As String = "https://api.pdf.co/v1/pdf/edit/add"

        ' Prepare requests params as JSON
        ' See documentation: https://apidocs.pdf.co/04-pdf-add-text-signatures-and-images-to-pdf
        Dim jsonPayload As String = $"{{
    ""name"": ""{Path.GetFileName(DestinationFile)}"",
    ""url"": ""{SourceFileUrl}"",
    ""password"": ""{Password}"",
    ""annotations"": [
        {{
            ""text"": ""{Text}"",
            ""x"": {X},
            ""y"": {Y},
            ""fontname"": ""{FontName}"",
            ""size"": ""{FontSize}"",
            ""color"": ""{Color}"",
            ""pages"": ""{Pages}""
        }}
    ]
}}"

        Try
            ' Execute POST request with JSON payload
            Dim response As String = webClient.UploadString(url, jsonPayload)

            ' Parse JSON response
            Dim json As JObject = JObject.Parse(response)

            If json("error").ToObject(Of Boolean) = False Then

                ' Get URL of generated PDF file
                Dim resultFileUrl As String = json("url").ToString()

                ' Download PDF file
                webClient.DownloadFile(resultFileUrl, DestinationFile)

                Console.WriteLine("Generated PDF file saved as ""{0}"" file.", DestinationFile)

            Else
                Console.WriteLine(json("message").ToString())
            End If

        Catch ex As WebException
            Console.WriteLine(ex.ToString())
        End Try

        webClient.Dispose()

        Console.WriteLine()
        Console.WriteLine("Press any key...")
        Console.ReadKey()

    End Sub

    ''' <summary>
    ''' Find result coordinates
    ''' </summary>
    Function FindCoordinates(ByVal API_KEY As String, ByVal SourceFileUrl As String, ByVal SearchString As String) As ResultCoOrdinates

        Dim oResult As New ResultCoOrdinates()

        ' Create standard .NET web client instance
        Dim webClient As WebClient = New WebClient()

        ' Set API Key
        webClient.Headers.Add("x-api-key", API_KEY)

        ' Set JSON content type
        webClient.Headers.Add("Content-Type", "application/json")

        ' Prepare URL for PDF text search API call.
        ' See documentation: https://apidocs.pdf.co/07-pdf-search-text
        Dim url As String = "https://api.pdf.co/v1/pdf/find"

        ' Prepare requests params as JSON
        ' See documentation: https : //apidocs.pdf.co
        Dim parameters As New Dictionary(Of String, Object)
		parameters.Add("url", SourceFileUrl)
		parameters.Add("searchString", SearchString)

        ' Convert dictionary of params to JSON
        Dim jsonPayload As String = JsonConvert.SerializeObject(parameters)

        Try
            ' Execute POST request with JSON payload
            Dim response As String = webClient.UploadString(url, jsonPayload)

            ' Parse JSON response
            Dim json As JObject = JObject.Parse(response)

            If json("error").ToObject(Of Boolean) = False Then

                Dim item = json("body")(0)

                oResult.x = item("left")
                oResult.y = item("top")
                oResult.width = item("width")
                oResult.height = item("height")

            End If

        Catch ex As WebException
            Console.WriteLine(ex.ToString())
        End Try

        webClient.Dispose()

        Return oResult

    End Function

    Class ResultCoOrdinates

        Public x As Integer
        Public y As Integer
        Public width As Integer
        Public height As Integer

    End Class

End Module


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

# insert_and_wait_for_replication insert data in host and wait all replset members
# applied recent oplog entry
function insert_and_wait_for_replication() {
  local host
  host=$1
  local data
  data=$2

  # Storing document into replset and wait replication to finish
  local script
  script="db.getSiblingDB('test_db').data.insert(${data});
    for (var i = 0; i < 60; i++) {
      var status=rs.status();
      var optime=status.members[0].optime;
      var ok=true;
      for(var j=1; j < status.members.length; j++) {
        if(tojson(optime) != tojson(status.members[j].optime)) {
          ok=false;
        }
      };
      if(ok == true) {
        print('INFO: All members of replicaset are synchronized');
        quit(0);
      }
      sleep(1000);
    }
    print('ERROR: Members of replicaset are not synchronized');
    printjson(rs.status());
    quit(1);"

  mongo "${host}" -u admin -p"${MONGODB_ADMIN_PASSWORD}" <<<"${script}"
}

# wait_replicaset_members waits till replset has specified number of members
function wait_replicaset_members() {
  local host
  host=$1
  local count
  count=$2

  local script
  script="for (var i = 0; i < 60; i++) {
    var ret = rs.status().members.length;
    if (ret == ${count}) {
      print('INFO: Replicaset has expected number of members');
      quit(0);
    }
    sleep(1000);
  }
  print('ERROR: Wrong count of members in replicaset');
  printjson(rs.status());
  quit(1);"

  mongo "${host}" -u admin -p"${MONGODB_ADMIN_PASSWORD}" <<<"${script}"
}


text:
<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: misc_seccode.php 34646 2014-06-17 03:23:15Z nemohou $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

$idhash = isset($_GET['idhash']) && preg_match('/^\w+$/', $_GET['idhash']) ? $_GET['idhash'] : '';
$modid = isset($_GET['modid']) && preg_match('/^[\w:]+$/', $_GET['modid']) ? $_GET['modid'] : '';

if($_GET['action'] == 'update') {

	$message = '';
	$showid = 'seccode_'.$idhash;
	$rand = random(5, 1);
	$htmlcode = '';
	$ani = $_G['setting']['seccodedata']['animator'] ? '_ani' : '';
	if($_G['setting']['seccodedata']['type'] == 2) {
		$htmlcode = extension_loaded('ming') ?
			"$('seccodeswf_$idhash').innerHTML='".lang('core', 'seccode_image'.$ani.'_tips')."' + AC_FL_RunContent('width', '".$_G['setting']['seccodedata']['width']."', 'height', '".$_G['setting']['seccodedata']['height']."', 'src', 'misc.php?mod=seccode&update=$rand&idhash=$idhash','quality', 'high', 'wmode', 'transparent', 'bgcolor', '#ffffff','align', 'middle', 'menu', 'false', 'allowScriptAccess', 'never');" :
			"$('seccodeswf_$idhash').innerHTML='".lang('core', 'seccode_image'.$ani.'_tips')."' + AC_FL_RunContent('width', '".$_G['setting']['seccodedata']['width']."', 'height', '".$_G['setting']['seccodedata']['height']."', 'src', '$_G[siteurl]static/image/seccode/flash/flash2.swf', 'FlashVars', 'sFile=".rawurlencode("$_G[siteurl]misc.php?mod=seccode&update=$rand&idhash=$idhash")."', 'menu', 'false', 'allowScriptAccess', 'never', 'swLiveConnect', 'true', 'wmode', 'transparent');";
		$message = '';
	} elseif($_G['setting']['seccodedata']['type'] == 3) {
		$htmlcode = "$('seccodeswf_$idhash').innerHTML='".lang('core', 'seccode_sound_tips')."' + AC_FL_RunContent('id', 'seccodeplayer_$idhash', 'name', 'seccodeplayer_$idhash', 'width', '0', 'height', '0', 'src', '$_G[siteurl]static/image/seccode/flash/flash1.swf', 'FlashVars', 'sFile=".rawurlencode("$_G[siteurl]misc.php?mod=seccode&update=$rand&idhash=$idhash")."', 'menu', 'false', 'allowScriptAccess', 'never', 'swLiveConnect', 'true', 'wmode', 'transparent');";
		$message = ''.lang('forum/misc', 'seccode_player', array('idhash' => $idhash));
	} else {
		if(!is_numeric($_G['setting']['seccodedata']['type']) && preg_match('/^[\w\d:_]+$/i', $_G['setting']['seccodedata']['type'])) {
			$etype = explode(':', $_G['setting']['seccodedata']['type']);
			if(count($etype) > 1) {
				$codefile = DISCUZ_ROOT.'./source/plugin/'.$etype[0].'/seccode/seccode_'.$etype[1].'.php';
				$class = $etype[1];
			} else {
				$codefile = libfile('seccode/'.$_G['setting']['seccodedata']['type'], 'class');
				$class = $_G['setting']['seccodedata']['type'];
			}
			if(file_exists($codefile)) {
				@include_once $codefile;
				$class = 'seccode_'.$class;
				if(class_exists($class)) {
					$code = new $class();
					if(method_exists($code, 'make')) {
						ob_start();
						$seccode = $code->make($idhash, $modid);
						make_seccode($seccode);
						$message = preg_replace("/\r|\n/", '', ob_get_contents());
						ob_end_clean();
					}
				}
			}
		} else {
			$message = lang('core', 'seccode_image'.$ani.'_tips').'';
		}
	}
	$imemode = $_G['setting']['seccodedata']['type'] != 1 ? 'ime-mode:disabled;' : '';
	$message = str_replace("'", "\'", $message);
	$seclang = lang('forum/misc');
echo <<<EOF
if($('$showid')) {
	if(!$('v$showid')) {
		var sectpl = seccheck_tpl['$idhash'] != '' ? seccheck_tpl['$idhash'].replace(/<hash>/g, 'code$idhash') : '';
		var sectplcode = sectpl != '' ? sectpl.split('<sec>') : Array('
',': ','
','');
		var string = '<input name="seccodehash" type="hidden" value="$idhash" /><input name="seccodemodid" type="hidden" value="$modid" />' + sectplcode[0] + '$seclang[seccode]' + sectplcode[1] + '<input name="seccodeverify" id="seccodeverify_$idhash" type="text" autocomplete="off" style="{$imemode}width:100px" class="txt px vm" onblur="checksec(\'code\', \'$idhash\', 0, null, \'$modid\')" />' +
			' $seclang[seccode_update]' +
			'' +
			sectplcode[2] + '$message' + sectplcode[3];
		evalscript(string);
		$('$showid').innerHTML = string;
	} else {
		var string = '$message';
		evalscript(string);
		$('v$showid').innerHTML = string;
	}
	$htmlcode
}
EOF;

} elseif($_GET['action'] == 'check') {

	include template('common/header_ajax');
	echo check_seccode($_GET['secverify'], $_GET['idhash'], 1, $modid) ? 'succeed' : 'invalid';
	include template('common/footer_ajax');

} else {

	$refererhost = parse_url($_SERVER['HTTP_REFERER']);
	$refererhost['host'] .= !empty($refererhost['port']) ? (':'.$refererhost['port']) : '';

	if($_G['setting']['seccodedata']['type'] < 2 && ($refererhost['host'] != $_SERVER['HTTP_HOST']) || $_G['setting']['seccodedata']['type'] == 2 && !extension_loaded('ming') && $_POST['fromFlash'] != 1 || $_G['setting']['seccodedata']['type'] == 3 && $_GET['fromFlash'] != 1) {
		exit('Access Denied');
	}

	if(is_numeric($_G['setting']['seccodedata']['type']) || !preg_match('/^[\w\d:_]+$/i', $_G['setting']['seccodedata']['type'])) {

		if(IN_MOBILE && in_array($_G['setting']['seccodedata']['type'], array(2, 3))) {
			exit;
		}

		$seccode = make_seccode();

		if(!$_G['setting']['nocacheheaders']) {
			@header("Expires: -1");
			@header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
			@header("Pragma: no-cache");
		}

		require_once libfile('class/seccode');

		$code = new seccode();
		$code->code = $seccode;
		$code->type = $_G['setting']['seccodedata']['type'];
		$code->width = $_G['setting']['seccodedata']['width'];
		$code->height = $_G['setting']['seccodedata']['height'];
		$code->background = $_G['setting']['seccodedata']['background'];
		$code->adulterate = $_G['setting']['seccodedata']['adulterate'];
		$code->ttf = $_G['setting']['seccodedata']['ttf'];
		$code->angle = $_G['setting']['seccodedata']['angle'];
		$code->warping = $_G['setting']['seccodedata']['warping'];
		$code->scatter = $_G['setting']['seccodedata']['scatter'];
		$code->color = $_G['setting']['seccodedata']['color'];
		$code->size = $_G['setting']['seccodedata']['size'];
		$code->shadow = $_G['setting']['seccodedata']['shadow'];
		$code->animator = $_G['setting']['seccodedata']['animator'];
		$code->fontpath = DISCUZ_ROOT.'./static/image/seccode/font/';
		$code->datapath = DISCUZ_ROOT.'./static/image/seccode/';
		$code->includepath = DISCUZ_ROOT.'./source/class/';

		$code->display();

	} else {
		$etype = explode(':', $_G['setting']['seccodedata']['type']);
		if(count($etype) > 1) {
			$codefile = DISCUZ_ROOT.'./source/plugin/'.$etype[0].'/seccode/seccode_'.$etype[1].'.php';
			$class = $etype[1];
		} else {
			$codefile = libfile('seccode/'.$_G['setting']['seccodedata']['type'], 'class');
			$class = $_G['setting']['seccodedata']['type'];
		}
		if(file_exists($codefile)) {
			@include_once $codefile;
			$class = 'seccode_'.$class;
			if(class_exists($class)) {
				make_seccode();
				$code = new $class();
				$image = $code->image($idhash, $modid);
				if($image) {
					dheader('location: '.$image);
				}
			}
		}
	}

}

?>


text:
package test.persistence;

import android.content.Context;
import android.util.Log;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import test.objects.FieldValue;

/**
 * Created by willpassidomo on 3/21/15.
 */
public class Constants {
    public static final String USER_ID = "user_id";
    public static final String PROJECT_ID = "project_id";
    public static final String SITE_VISIT_ID = "site_visit_id";
    public static final String WALK_THROUGH_ID = "walk_through_id";
    public static final String STRING_LIST_TYPE = "string_list_type";
    public static final String NEW_OR_EDIT = "new_or_edit";
    public static final String NEW = "new";
    public static final String EDIT = "edit";
    public static final String EMAIL_ADDRESS = "email_address";

    public static final String ID_TYPE = "type";
    public static final String TYPE_SITEVISIT = "type_site_visit";
    public static final String TYPE_PROJECT = "type_project";

    public static final int FIELD_VALUE_PROJECT = 1;
    public static final int FIELD_VALUE_SITE_VISIT = 2;
    public static final int FIELD_VALUE_USER = 3;

    public static final String INTEGER_TYPE = " INTEGER";
    public static final String TEXT_TYPE = " TEXT";
    public static final String REAL_TYPE = " REAL";
    public static final String BLOB = " BLOB";
    public static final String COMMA_SEP = " , ";

    public static String createTableString(String tableName, String Id, String...columnNames) {
        String createTable = "CREATE TABLE " + tableName + " (" + Id + " PRIMARY KEY";
        if (columnNames.length > 0) {
            createTable += ", ";
            for (int i = 0; i < (columnNames.length - 1); i++) {
                createTable += columnNames[i] + COMMA_SEP;
            }
            createTable += columnNames[columnNames.length - 1];
        }
        createTable += ")";

        return createTable;
    }

    public static String createBridgeTableString(String tableName, String idOne, String idTwo) {
        String createTable = "CREATE TABLE " + tableName + " (" + idOne + COMMA_SEP + idTwo + " PRIMARY KEY) ";
        return createTable;
    }

    public static void storeInitialSiteWalkQuestions(Context context) {
        List<FieldValue> fvs = new ArrayList<>();
        fvs.add(new FieldValue("Temperature", false, false));
        fvs.add(new FieldValue("Weather", false, false));
        fvs.add(new FieldValue("Date", true, false));
        fvs.add(new FieldValue("Time", false, false));
        fvs.add(new FieldValue("Humidity", false, false));

        Storage.storeSiteVisitQuestions(context, fvs);
    }

    public static void storeInitialProjectQuestions(Context context) {
        List<FieldValue> fvs = new ArrayList<>();
        fvs.add(new FieldValue("Contact Name", true, false));
        fvs.add(new FieldValue("Contact Phone #", true, false));
        fvs.add(new FieldValue("Project Estimated Start Date", true, false));
        fvs.add(new FieldValue("Project Actual Start Date", false, false));
        fvs.add(new FieldValue("Project Estimated End Date", true, false));
        fvs.add(new FieldValue("Estiamted Budget", true, false));
        fvs.add(new FieldValue("Coordinates", false, false));
        Storage.storeProjectQuestions(context, fvs);
    }

    public static List<String> getInitialProgresses() {
        List progressList = new LinkedList();
        progressList.add("yet to commence");
        progressList.add("commenced");
        progressList.add("25%");
        progressList.add("In Progress");
        progressList.add("75%");
        progressList.add("Substantially Complete");
        return progressList;
    }

    public static List<String> getInitalTrades() {
        List tradeList = new LinkedList<String>();
        tradeList.add("Demo");
        tradeList.add("Framing");
        tradeList.add("Plumbing- rough");
        tradeList.add("Plumbing- finish");
        tradeList.add("Electical");
        return tradeList;
    }
}


text:
package com.facebook.buck.features.python;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeThat;
import static org.junit.Assume.assumeTrue;

import com.facebook.buck.core.config.BuckConfig;
import com.facebook.buck.core.config.FakeBuckConfig;
import com.facebook.buck.core.model.BuildTargetFactory;
import com.facebook.buck.core.model.UnconfiguredTargetConfiguration;
import com.facebook.buck.core.rules.BuildRuleResolver;
import com.facebook.buck.core.rules.resolver.impl.TestActionGraphBuilder;
import com.facebook.buck.cxx.config.CxxBuckConfig;
import com.facebook.buck.cxx.toolchain.CxxPlatformUtils;
import com.facebook.buck.cxx.toolchain.nativelink.NativeLinkStrategy;
import com.facebook.buck.features.python.PythonBuckConfig.PackageStyle;
import com.facebook.buck.features.python.toolchain.impl.PythonInterpreterFromConfig;
import com.facebook.buck.io.ExecutableFinder;
import com.facebook.buck.io.filesystem.TestProjectFilesystems;
import com.facebook.buck.io.pathformat.PathFormatter;
import com.facebook.buck.testutil.ProcessResult;
import com.facebook.buck.testutil.TemporaryPaths;
import com.facebook.buck.testutil.TestConsole;
import com.facebook.buck.testutil.integration.ProjectWorkspace;
import com.facebook.buck.testutil.integration.TestDataHelper;
import com.facebook.buck.util.CreateSymlinksForTests;
import com.facebook.buck.util.DefaultProcessExecutor;
import com.facebook.buck.util.ProcessExecutor;
import com.facebook.buck.util.ProcessExecutor.Result;
import com.facebook.buck.util.ProcessExecutorParams;
import com.facebook.buck.util.VersionStringComparator;
import com.facebook.buck.util.config.Config;
import com.facebook.buck.util.config.Configs;
import com.facebook.buck.util.environment.Platform;
import com.facebook.buck.util.types.Pair;
import com.facebook.buck.util.unarchive.Unzip;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.hamcrest.Matchers;
import org.hamcrest.comparator.ComparatorMatcherBuilder;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class PythonBinaryIntegrationTest {

  @Parameterized.Parameters(name = "{0}(dir={1}),{2}")
  public static Collection<Object[]> data() {
    ImmutableList.Builder<Object[]> validPermutations = ImmutableList.builder();
    for (PythonBuckConfig.PackageStyle packageStyle : PythonBuckConfig.PackageStyle.values()) {
      for (boolean pexDirectory : new boolean[] {true, false}) {
        if (packageStyle.isInPlace() && pexDirectory) {
          continue;
        }

        for (NativeLinkStrategy linkStrategy : NativeLinkStrategy.values()) {
          validPermutations.add(new Object[] {packageStyle, pexDirectory, linkStrategy});
        }
      }
    }
    return validPermutations.build();
  }

  @Parameterized.Parameter public PythonBuckConfig.PackageStyle packageStyle;

  @Parameterized.Parameter(value = 1)
  public boolean pexDirectory;

  @Parameterized.Parameter(value = 2)
  public NativeLinkStrategy nativeLinkStrategy;

  @Rule public TemporaryPaths tmp = new TemporaryPaths();

  public ProjectWorkspace workspace;

  @Before
  public void setUp() throws IOException {
    workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "python_binary", tmp);
    workspace.setUp();
    String pexFlags = pexDirectory ? "--directory" : "";
    String buckconfigContents =
        Platform.detect().equals(Platform.WINDOWS)
            ? workspace.getFileContents("buckconfig.windows")
            : "";
    buckconfigContents +=
        "[python]\n"
            + "  package_style = "
            + packageStyle.toString().toLowerCase()
            + "\n"
            + "  native_link_strategy = "
            + nativeLinkStrategy.toString().toLowerCase()
            + "\n"
            + "  pex_flags = "
            + pexFlags;
    workspace.writeContentsToPath(buckconfigContents, ".buckconfig");
    PythonBuckConfig config = getPythonBuckConfig();
    assertThat(config.getPackageStyle(), equalTo(packageStyle));
    assertThat(config.getNativeLinkStrategy(), equalTo(nativeLinkStrategy));
  }

  @Test
  public void nonComponentDepsArePreserved() throws IOException {
    workspace.runBuckBuild("//:bin-with-extra-dep").assertSuccess();
    workspace.getBuildLog().assertTargetBuiltLocally("//:extra");
  }

  @Test
  public void executionThroughSymlink() throws IOException, InterruptedException {
    workspace.runBuckBuild("//:bin").assertSuccess();
    String output =
        workspace
            .runBuckCommand("targets", "--show-output", "//:bin")
            .assertSuccess()
            .getStdout()
            .trim();
    Path link = workspace.getPath("link");
    CreateSymlinksForTests.createSymLink(
        link, workspace.getPath(Splitter.on(" ").splitToList(output).get(1)).toAbsolutePath());

    ProcessExecutor.Result result =
        workspace.runCommand(
            new PythonInterpreterFromConfig(getPythonBuckConfig(), new ExecutableFinder())
                .getPythonInterpreterPath()
                .toString(),
            link.toString());
    assertThat(
        result.getStdout().orElse("") + result.getStderr().orElse(""),
        result.getExitCode(),
        equalTo(0));
  }

  @Test
  public void inplaceBinariesWriteCorrectInterpreter() throws IOException {
    assumeThat(
        packageStyle,
        Matchers.in(ImmutableList.of(PackageStyle.INPLACE, PackageStyle.INPLACE_LITE)));

    String expectedPythonPath =
        new PythonInterpreterFromConfig(getPythonBuckConfig(), new ExecutableFinder())
            .getPythonInterpreterPath()
            .toString();

    Path binPath = workspace.buildAndReturnOutput("//:bin");
    workspace.runBuckCommand("run", "//:bin").assertSuccess();

    String firstLine = workspace.getProjectFileSystem().readLines(binPath).get(0);
    assertTrue(firstLine.startsWith(String.format("#!%s", expectedPythonPath)));
  }

  @Test
  public void commandLineArgs() {
    ProcessResult result = workspace.runBuckCommand("run", ":bin", "HELLO WORLD").assertSuccess();
    assertThat(result.getStdout(), containsString("HELLO WORLD"));
  }

  @Test
  public void testOutput() throws Exception {
    workspace.runBuckBuild("//:bin").assertSuccess();

    File output = workspace.getGenPath(BuildTargetFactory.newInstance("//:bin"), "%s.pex").toFile();
    if (pexDirectory) {
      assertTrue(output.isDirectory());
    } else {
      assertTrue(output.isFile());
    }
  }

  public void assumeThatNativeLibsAreSupported() {
    assumeTrue(!Platform.detect().equals(Platform.WINDOWS));
    assumeThat(packageStyle, not(is(PackageStyle.INPLACE_LITE)));
    assumeThat(
        "TODO(8667197): Native libs currently don't work on El Capitan",
        Platform.detect(),
        not(equalTo(Platform.MACOS)));
  }

  @Test
  public void nativeLibraries() {
    assumeThat(packageStyle, equalTo(PythonBuckConfig.PackageStyle.INPLACE));
    assumeThatNativeLibsAreSupported();
    ProcessResult result = workspace.runBuckCommand("run", ":bin-with-native-libs").assertSuccess();
    assertThat(result.getStdout(), containsString("HELLO WORLD"));
  }

  @Test
  public void runFromGenrule() {
    workspace.runBuckBuild(":gen").assertSuccess();
  }

  @Test
  public void arg0IsPreserved() throws IOException {
    workspace.writeContentsToPath("import sys; print(sys.argv[0])", "main.py");
    String arg0 = workspace.runBuckCommand("run", ":bin").assertSuccess().getStdout().trim();
    String output =
        workspace
            .runBuckCommand("targets", "--show-output", "//:bin")
            .assertSuccess()
            .getStdout()
            .trim();
    assertThat(arg0, endsWith(Splitter.on(" ").splitToList(output).get(1)));
  }

  @Test
  public void nativeLibsEnvVarIsPreserved() throws IOException {
    BuildRuleResolver resolver = new TestActionGraphBuilder();

    assumeThatNativeLibsAreSupported();

    String nativeLibsEnvVarName =
        CxxPlatformUtils.build(new CxxBuckConfig(FakeBuckConfig.builder().build()))
            .getLd()
            .resolve(resolver, UnconfiguredTargetConfiguration.INSTANCE)
            .searchPathEnvVar();
    String originalNativeLibsEnvVar = "something";
    workspace.writeContentsToPath(
        String.format("import os; print(os.environ.get('%s'))", nativeLibsEnvVarName),
        "main_with_native_libs.py");

    // Pre-set library path.
    String nativeLibsEnvVar =
        workspace
            .runBuckCommandWithEnvironmentOverridesAndContext(
                workspace.getPath(""),
                Optional.empty(),
                ImmutableMap.of(nativeLibsEnvVarName, originalNativeLibsEnvVar),
                "run",
                ":bin-with-native-libs")
            .assertSuccess()
            .getStdout()
            .trim();
    assertThat(nativeLibsEnvVar, equalTo(originalNativeLibsEnvVar));

    // Empty library path.
    nativeLibsEnvVar =
        workspace
            .runBuckCommandWithEnvironmentOverridesAndContext(
                workspace.getPath(""),
                Optional.empty(),
                ImmutableMap.of(),
                "run",
                ":bin-with-native-libs")
            .assertSuccess()
            .getStdout()
            .trim();
    assertThat(nativeLibsEnvVar, equalTo("None"));
  }

  @Test
  public void sysPathDoesNotIncludeWorkingDir() throws IOException {
    workspace.writeContentsToPath("import sys; print(sys.path[0])", "main.py");
    String sysPath0 = workspace.runBuckCommand("run", ":bin").assertSuccess().getStdout().trim();
    assertThat(sysPath0, not(equalTo("")));
  }

  @Test
  public void binaryIsCachedProperly() throws IOException {
    // Verify that the flow of build, upload to cache, clean, then re-build (and potentially
    // fetching from cache) results in a usable binary.
    workspace.writeContentsToPath("print('hello world')", "main.py");
    workspace.enableDirCache();
    workspace.runBuckBuild(":bin").assertSuccess();
    workspace.runBuckCommand("clean", "--keep-cache").assertSuccess();
    String stdout = workspace.runBuckCommand("run", ":bin").assertSuccess().getStdout().trim();
    assertThat(stdout, equalTo("hello world"));
  }

  @Test
  public void externalPexToolAffectsRuleKey() throws IOException {
    assumeThat(packageStyle, equalTo(PythonBuckConfig.PackageStyle.STANDALONE));

    ProcessResult firstResult =
        workspace.runBuckCommand(
            "targets", "-c", "python.path_to_pex=//:pex_tool", "--show-rulekey", "//:bin");
    String firstRuleKey = firstResult.assertSuccess().getStdout().trim();

    workspace.writeContentsToPath("changes", "pex_tool.sh");

    ProcessResult secondResult =
        workspace.runBuckCommand(
            "targets", "-c", "python.path_to_pex=//:pex_tool", "--show-rulekey", "//:bin");
    String secondRuleKey = secondResult.assertSuccess().getStdout().trim();

    assertThat(secondRuleKey, not(equalTo(firstRuleKey)));
  }

  @Test
  public void multiplePythonHomes() {
    assumeThatNativeLibsAreSupported();
    ProcessResult result =
        workspace.runBuckBuild(
            "-c",
            "python#a.library=//:platform_a",
            "-c",
            "python#b.library=//:platform_b",
            "//:binary_with_extension_a",
            "//:binary_with_extension_b");
    result.assertSuccess();
  }

  @Test
  public void mainModuleNameIsSetProperly() {
    assumeThat(packageStyle, not(is(PythonBuckConfig.PackageStyle.STANDALONE)));
    workspace.runBuckCommand("run", "//:main_module_bin").assertSuccess();
  }

  @Test
  public void disableCachingForPackagedBinaries() throws IOException {
    assumeThat(packageStyle, is(PythonBuckConfig.PackageStyle.STANDALONE));
    workspace.enableDirCache();
    workspace.runBuckBuild("-c", "python.cache_binaries=false", ":bin").assertSuccess();
    workspace.runBuckCommand("clean", "--keep-cache").assertSuccess();
    workspace.runBuckBuild("-c", "python.cache_binaries=false", ":bin").assertSuccess();
    workspace.getBuildLog().assertTargetBuiltLocally("//:bin");
  }

  @Test
  public void standalonePackagePrebuiltLibrariesProperly() throws IOException {
    assumeThat(packageStyle, is(PythonBuckConfig.PackageStyle.STANDALONE));

    workspace.runBuckCommand("run", "//:main_module_with_prebuilt_dep_bin").assertSuccess();
    Path binPath =
        workspace.resolve(
            workspace.getGenPath(
                BuildTargetFactory.newInstance("//:main_module_with_prebuilt_dep_bin"), "%s.pex"));

    ImmutableSet<Path> expectedPaths =
        ImmutableSet.of(
            Paths.get("wheel_package", "my_wheel.py"),
            Paths.get("wheel_package", "__init__.py"),
            Paths.get("wheel_package-0.0.1.dist-info", "DESCRIPTION.rst"),
            Paths.get("lib", "foo", "bar.py"));
    ImmutableSet<Path> expectedAbsentPaths =
        ImmutableSet.of(
            Paths.get(
                ".deps", "wheel_package-0.0.1-py2-none-any.whl", "wheel_package", "my_wheel.py"),
            Paths.get(
                ".deps", "wheel_package-0.0.1-py2-none-any.whl", "wheel_package", "__init__.py"),
            Paths.get(
                ".deps",
                "wheel_package-0.0.1-py2-none-any.whl",
                "wheel_package-0.0.1.dist-info",
                "DESCRIPTION.rst"),
            Paths.get(
                ".deps",
                "wheel_package-0.0.1-py2-none-any.whl",
                "wheel_package-0.0.1.data",
                "data",
                "lib",
                "foo",
                "bar.py"));
    ImmutableSet<Path> paths;
    if (pexDirectory) {
      paths =
          Files.walk(binPath)
              .filter(p -> !p.equals(binPath))
              .map(binPath::relativize)
              .collect(ImmutableSet.toImmutableSet());
    } else {
      paths = Unzip.getZipMembers(binPath);
    }
    assertThat(expectedPaths, everyItem(Matchers.in(paths)));
    assertThat(expectedAbsentPaths, everyItem(not(Matchers.in(paths))));
  }

  @Test
  public void inplacePackagePrebuiltLibrariesProperly() throws IOException {
    assumeThat(packageStyle, is(PackageStyle.INPLACE));

    ProcessResult res = workspace.runBuckCommand("run", "//:main_module_with_prebuilt_dep_bin");
    res.assertSuccess();

    Path linkTreeDir =
        workspace.getGenPath(
            workspace.newBuildTarget("//:main_module_with_prebuilt_dep_bin#link-tree"), "%s");
    Path originalWhlDir =
        workspace.getGenPath(
            BuildTargetFactory.newInstance("//external_sources:whl_dep"), "__%s__extracted");

    ImmutableSet<Path> expectedPaths =
        ImmutableSet.of(
            Paths.get("wheel_package", "my_wheel.py"),
            Paths.get("wheel_package", "__init__.py"),
            Paths.get("wheel_package-0.0.1.dist-info", "DESCRIPTION.rst"),
            Paths.get("lib", "foo", "bar.py"));

    ImmutableSet<Path> expectedAbsentPaths =
        ImmutableSet.of(Paths.get("wheel_package-0.0.1.data", "data"));

    for (Path path : expectedPaths) {
      assertTrue(Files.exists(linkTreeDir.resolve(path)));
      assertTrue(Files.isSameFile(linkTreeDir.resolve(path), originalWhlDir.resolve(path)));
    }
    for (Path path : expectedAbsentPaths) {
      assertFalse(Files.exists(linkTreeDir.resolve(path)));
    }
    ImmutableList<String> links =
        Files.walk(linkTreeDir).map(Path::toString).collect(ImmutableList.toImmutableList());
    assertThat(links, everyItem(not(endsWith(".whl"))));
  }

  @Test
  public void inplaceFailsWhenPrebuiltLibraryConflictsWithOtherInitPy() throws IOException {
    assumeThat(packageStyle, is(PackageStyle.INPLACE));

    assertThat(
        workspace
            .runBuckCommand("run", "//:main_module_with_prebuilt_dep_and_init_conflict_bin")
            .assertFailure()
            .getStderr(),
        Matchers.matchesPattern(
            Pattern.compile(
                ".*found duplicate entries for module \\S+ when creating python package.*",
                Pattern.MULTILINE | Pattern.DOTALL)));
  }

  @Test
  public void inplaceFailsWhenTwoPrebuiltLibrariesConflictWithInitPy() throws IOException {
    assumeThat(packageStyle, is(PackageStyle.INPLACE));

    // Iteration order on whls not guaranteed, but we want to make sure it's the whls conflicting
    String expected = ".*found duplicate entries for module \\S+ when creating python package.*";

    assertThat(
        workspace
            .runBuckCommand("run", "//:main_module_with_prebuilt_dep_and_whl_init_conflict_bin")
            .assertFailure()
            .getStderr(),
        Matchers.matchesPattern(Pattern.compile(expected, Pattern.MULTILINE | Pattern.DOTALL)));
  }

  /**
   * Test a bug where a C/C++ library that is transitively excluded by a `python_library` containing
   * native extensions (in this case, it has to be a 2nd-order dep of the `python_library`) but
   * which is also a direct dependency of another Python rule, causes the node to be processed as
   * both a linkable root and an excluded rule, causing an internal omnibus failure.
   */
  @Test
  public void omnibusExcludedNativeLinkableRoot() {
    assumeThatNativeLibsAreSupported();
    assumeThat(nativeLinkStrategy, is(NativeLinkStrategy.MERGED));
    workspace
        .runBuckCommand("targets", "--show-output", "//omnibus_excluded_root:bin")
        .assertSuccess();
  }

  @Test
  public void depOntoCxxLibrary() {
    workspace
        .runBuckCommand(
            "build",
            "-c",
            "cxx.cxx=//cxx_lib_dep/helpers:cxx",
            "-c",
            "cxx.cxx_type=gcc",
            "//cxx_lib_dep:bin")
        .assertSuccess();
  }

  private PythonBuckConfig getPythonBuckConfig() throws IOException {
    Config rawConfig = Configs.createDefaultConfig(tmp.getRoot());
    BuckConfig buckConfig =
        FakeBuckConfig.builder()
            .setFilesystem(TestProjectFilesystems.createProjectFilesystem(tmp.getRoot()))
            .setSections(rawConfig.getRawConfig())
            .build();
    return new PythonBuckConfig(buckConfig);
  }

  private ImmutableList<String> getDirectlyExecutedPexCommand(String pexPathString)
      throws IOException {
    return Platform.detect().equals(Platform.WINDOWS)
        ? ImmutableList.of(
            new PythonInterpreterFromConfig(getPythonBuckConfig(), new ExecutableFinder())
                .getPythonInterpreterPath()
                .toString(),
            pexPathString)
        : ImmutableList.of(pexPathString);
  }

  @Test
  public void stripsPathEarlyInInplaceBinaries() throws IOException, InterruptedException {
    assumeThat(packageStyle, is(PackageStyle.INPLACE));
    Path pexPath = workspace.buildAndReturnOutput("//pathtest:pathtest");

    DefaultProcessExecutor executor = new DefaultProcessExecutor(new TestConsole());

    ImmutableList<String> command = getDirectlyExecutedPexCommand(pexPath.toString());

    Result ret =
        executor.launchAndExecute(
            ProcessExecutorParams.builder()
                .setDirectory(workspace.resolve("pathtest"))
                .setCommand(command)
                .build());
    Assert.assertEquals(0, ret.getExitCode());
  }

  @Test
  public void preloadDeps() throws IOException, InterruptedException {
    assumeThat(packageStyle, is(PackageStyle.INPLACE));
    assumeThatNativeLibsAreSupported();
    Path pexPath = workspace.buildAndReturnOutput("//preload_deps:bin");

    DefaultProcessExecutor executor = new DefaultProcessExecutor(new TestConsole());

    ImmutableList<String> command = getDirectlyExecutedPexCommand(pexPath.toString());

    Result ret =
        executor.launchAndExecute(
            ProcessExecutorParams.builder()
                .setDirectory(workspace.resolve("pathtest"))
                .setCommand(command)
                .build());
    Assert.assertEquals(0, ret.getExitCode());
  }

  @Test
  public void preloadDepsOrder() throws IOException, InterruptedException {
    assumeThat(packageStyle, is(PackageStyle.INPLACE));
    assumeThatNativeLibsAreSupported();
    DefaultProcessExecutor executor = new DefaultProcessExecutor(new TestConsole());

    for (Pair<String, String> test :
        ImmutableList.of(
            new Pair<>("//preload_deps:bin_a_first", "a\n"),
            new Pair<>("//preload_deps:bin_b_first", "b\n"))) {
      Path pexPath = workspace.buildAndReturnOutput(test.getFirst());

      ImmutableList<String> command = getDirectlyExecutedPexCommand(pexPath.toString());

      Result ret =
          executor.launchAndExecute(
              ProcessExecutorParams.builder()
                  .setDirectory(workspace.resolve("pathtest"))
                  .setCommand(command)
                  .build());
      Assert.assertEquals(
          ret.getStdout().orElse("") + ret.getStderr().orElse(""), 0, ret.getExitCode());
      assertThat(ret.getStdout().orElse(""), equalTo(test.getSecond()));
    }
  }

  @Test
  public void inplaceBinaryUsesInterpreterFlags() throws IOException {
    assumeThat(
        packageStyle,
        Matchers.in(ImmutableList.of(PackageStyle.INPLACE, PackageStyle.INPLACE_LITE)));

    workspace.addBuckConfigLocalOption("python", "inplace_interpreter_flags", "-EsB");
    workspace.runBuckCommand("run", "//:bin").assertSuccess();

    ImmutableList<Path> pycFiles =
        Files.find(
                tmp.getRoot(),
                Integer.MAX_VALUE,
                (path, attr) -> path.getFileName().toString().endsWith(".pyc"))
            .map(path -> tmp.getRoot().relativize(path))
            .collect(ImmutableList.toImmutableList());
    Assert.assertEquals(ImmutableList.of(), pycFiles);

    workspace.removeBuckConfigLocalOption("python", "inplace_interpreter_flags");
    workspace.runBuckCommand("run", "//:bin").assertSuccess();

    // Fall back to using the defaults (-Es), which should write out bytecode
    pycFiles =
        Files.find(
                tmp.getRoot(),
                Integer.MAX_VALUE,
                (path, attr) -> path.getFileName().toString().endsWith(".pyc"))
            .map(path -> tmp.getRoot().relativize(path))
            .collect(ImmutableList.toImmutableList());
    Assert.assertEquals(4, pycFiles.size());
  }

  @Test
  public void compileSources() throws IOException, InterruptedException {
    assumeThat(packageStyle, is(PackageStyle.STANDALONE));
    Path py3 = PythonTestUtils.assumeInterpreter("python3");
    PythonTestUtils.assumeVersion(
        py3,
        Matchers.any(String.class),
        ComparatorMatcherBuilder.comparedBy(new VersionStringComparator())
            .greaterThanOrEqualTo("3.7"));
    Path binPath =
        workspace.buildAndReturnOutput("-c", "python.interpreter=" + py3, "//:bin_compile");
    ImmutableSet<Path> paths =
        pexDirectory
            ? Files.walk(binPath)
                .filter(p -> !p.equals(binPath))
                .map(binPath::relativize)
                .collect(ImmutableSet.toImmutableSet())
            : Unzip.getZipMembers(binPath);
    assertThat(
        paths.stream().map(PathFormatter::pathWithUnixSeparators).collect(Collectors.toList()),
        Matchers.hasItems(
            Matchers.matchesRegex("foo/bar/(__pycache__/)?mod(.cpython-3[0-9])?.pyc"),
            Matchers.matchesRegex("(__pycache__/)?main(.cpython-3[0-9])?.pyc")));
  }
}


text:
class FontArchitectsDaughter < Cask
  version '1.002'
  sha256 '70ad5e06293bd76d2b5e5c52718ddc809085f1be61e610e1c49e6ab872fc3af3'

  url 'https://googlefontdirectory.googlecode.com/hg-history/67342bc472599b4c32201ee4a002fe59a6447a42/ofl/architectsdaughter/ArchitectsDaughter.ttf'
  homepage 'http://www.google.com/fonts/specimen/Architects%20Daughter'

  font 'ArchitectsDaughter.ttf'
end


text:
package org.w3c.domts.level2.core;

import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.w3c.domts.DOMTest.load;

/**
 * The "getElementsByTagNameNS(namespaceURI,localName)" method for a
 * Document should return a new NodeList of all Elements that have a namespace
 * when local name is specified as ' '.
 *

 * Invoke method getElementsByTagNameNS(namespaceURI,localName) on this document
 * with namespaceURI and localName as " ".
 * Method should return a new NodeList of 37 elements.
 *
 * @see http://www.w3.org/TR/DOM-Level-2-Core/core#ID-getElBTNNS
 */

/**
 *
 */

public class getElementsByTagNameNS01 {
    @Test
    void testRun() throws Throwable {
        String namespaceURI = "*";
        String localName = "*";
        Document doc = load("staffNS", false);
        NodeList newList = doc.getElementsByTagNameNS(namespaceURI, localName);

        //Yana: claim is 37, I counted 36
        assertEquals(36, newList.getLength(), "throw_Size");
    }

    /**
     * Gets URI that identifies the test
     *
     * @return uri identifier of test
     */
    public String getTargetURI() {
        return "http://www.w3.org/2001/DOM-Test-Suite/level2/core/getElementsByTagNameNS01";
    }

}


text:
function facebookInit() {
    // Initialize Facebook Connection
    FB.api(
        "/BrewDogHel",
        'GET',
        {"fields":"posts.limit(10)"},
        function (response) {
            if (response && !response.error) {
                console.log("Success....")
                var faceSection = document.getElementById("facebook");
                var node = document.createElement("P");
                var textNode = document.createTextNode("Success...");
                node.appendChild(textNode);
                faceSection.appendChild(node);
            }
        }
    );
};