text:
import warnings
from typing import Callable, Dict, Optional, Sequence, Tuple, Union

from google.api_core import grpc_helpers
from google.api_core import operations_v1
from google.api_core import gapic_v1
import google.auth  # type: ignore
from google.auth import credentials as ga_credentials  # type: ignore
from google.auth.transport.grpc import SslCredentials  # type: ignore

import grpc  # type: ignore

from google.cloud.dialogflow_v2.types import document
from google.cloud.dialogflow_v2.types import document as gcd_document
from google.cloud.location import locations_pb2  # type: ignore
from google.longrunning import operations_pb2
from google.longrunning import operations_pb2  # type: ignore
from .base import DocumentsTransport, DEFAULT_CLIENT_INFO

class DocumentsGrpcTransport(DocumentsTransport):
    """gRPC backend transport for Documents.

    Service for managing knowledge
    [Documents][google.cloud.dialogflow.v2.Document].

    This class defines the same methods as the primary client, so the
    primary client can load the underlying transport implementation
    and call it.

    It sends protocol buffers over the wire using gRPC (which is built on
    top of HTTP/2); the ``grpcio`` package must be installed.
    """

    _stubs: Dict[str, Callable]

    def __init__(
        self,
        *,
        host: str = "dialogflow.googleapis.com",
        credentials: ga_credentials.Credentials = None,
        credentials_file: str = None,
        scopes: Sequence[str] = None,
        channel: grpc.Channel = None,
        api_mtls_endpoint: str = None,
        client_cert_source: Callable[[], Tuple[bytes, bytes]] = None,
        ssl_channel_credentials: grpc.ChannelCredentials = None,
        client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
        quota_project_id: Optional[str] = None,
        client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
        always_use_jwt_access: Optional[bool] = False,
        api_audience: Optional[str] = None,
    ) -> None:
        """Instantiate the transport.

        Args:
            host (Optional[str]):
                 The hostname to connect to.
            credentials (Optional[google.auth.credentials.Credentials]): The
                authorization credentials to attach to requests. These
                credentials identify the application to the service; if none
                are specified, the client will attempt to ascertain the
                credentials from the environment.
                This argument is ignored if ``channel`` is provided.
            credentials_file (Optional[str]): A file with credentials that can
                be loaded with :func:`google.auth.load_credentials_from_file`.
                This argument is ignored if ``channel`` is provided.
            scopes (Optional(Sequence[str])): A list of scopes. This argument is
                ignored if ``channel`` is provided.
            channel (Optional[grpc.Channel]): A ``Channel`` instance through
                which to make calls.
            api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint.
                If provided, it overrides the ``host`` argument and tries to create
                a mutual TLS channel with client SSL credentials from
                ``client_cert_source`` or application default SSL credentials.
            client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]):
                Deprecated. A callback to provide client SSL certificate bytes and
                private key bytes, both in PEM format. It is ignored if
                ``api_mtls_endpoint`` is None.
            ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials
                for the grpc channel. It is ignored if ``channel`` is provided.
            client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]):
                A callback to provide client certificate bytes and private key bytes,
                both in PEM format. It is used to configure a mutual TLS channel. It is
                ignored if ``channel`` or ``ssl_channel_credentials`` is provided.
            quota_project_id (Optional[str]): An optional project to use for billing
                and quota.
            client_info (google.api_core.gapic_v1.client_info.ClientInfo):
                The client info used to send a user-agent string along with
                API requests. If ``None``, then default info will be used.
                Generally, you only need to set this if you're developing
                your own client library.
            always_use_jwt_access (Optional[bool]): Whether self signed JWT should
                be used for service account credentials.

        Raises:
          google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport
              creation failed for any reason.
          google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials``
              and ``credentials_file`` are passed.
        """
        self._grpc_channel = None
        self._ssl_channel_credentials = ssl_channel_credentials
        self._stubs: Dict[str, Callable] = {}
        self._operations_client: Optional[operations_v1.OperationsClient] = None

        if api_mtls_endpoint:
            warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning)
        if client_cert_source:
            warnings.warn("client_cert_source is deprecated", DeprecationWarning)

        if channel:
            # Ignore credentials if a channel was passed.
            credentials = False
            # If a channel was explicitly provided, set it.
            self._grpc_channel = channel
            self._ssl_channel_credentials = None

        else:
            if api_mtls_endpoint:
                host = api_mtls_endpoint

                # Create SSL credentials with client_cert_source or application
                # default SSL credentials.
                if client_cert_source:
                    cert, key = client_cert_source()
                    self._ssl_channel_credentials = grpc.ssl_channel_credentials(
                        certificate_chain=cert, private_key=key
                    )
                else:
                    self._ssl_channel_credentials = SslCredentials().ssl_credentials

            else:
                if client_cert_source_for_mtls and not ssl_channel_credentials:
                    cert, key = client_cert_source_for_mtls()
                    self._ssl_channel_credentials = grpc.ssl_channel_credentials(
                        certificate_chain=cert, private_key=key
                    )

        # The base transport sets the host, credentials and scopes
        super().__init__(
            host=host,
            credentials=credentials,
            credentials_file=credentials_file,
            scopes=scopes,
            quota_project_id=quota_project_id,
            client_info=client_info,
            always_use_jwt_access=always_use_jwt_access,
            api_audience=api_audience,
        )

        if not self._grpc_channel:
            self._grpc_channel = type(self).create_channel(
                self._host,
                # use the credentials which are saved
                credentials=self._credentials,
                # Set ``credentials_file`` to ``None`` here as
                # the credentials that we saved earlier should be used.
                credentials_file=None,
                scopes=self._scopes,
                ssl_credentials=self._ssl_channel_credentials,
                quota_project_id=quota_project_id,
                options=[
                    ("grpc.max_send_message_length", -1),
                    ("grpc.max_receive_message_length", -1),
                ],
            )

        # Wrap messages. This must be done after self._grpc_channel exists
        self._prep_wrapped_messages(client_info)

    @classmethod
    def create_channel(
        cls,
        host: str = "dialogflow.googleapis.com",
        credentials: ga_credentials.Credentials = None,
        credentials_file: str = None,
        scopes: Optional[Sequence[str]] = None,
        quota_project_id: Optional[str] = None,
        **kwargs,
    ) -> grpc.Channel:
        """Create and return a gRPC channel object.
        Args:
            host (Optional[str]): The host for the channel to use.
            credentials (Optional[~.Credentials]): The
                authorization credentials to attach to requests. These
                credentials identify this application to the service. If
                none are specified, the client will attempt to ascertain
                the credentials from the environment.
            credentials_file (Optional[str]): A file with credentials that can
                be loaded with :func:`google.auth.load_credentials_from_file`.
                This argument is mutually exclusive with credentials.
            scopes (Optional[Sequence[str]]): A optional list of scopes needed for this
                service. These are only used when credentials are not specified and
                are passed to :func:`google.auth.default`.
            quota_project_id (Optional[str]): An optional project to use for billing
                and quota.
            kwargs (Optional[dict]): Keyword arguments, which are passed to the
                channel creation.
        Returns:
            grpc.Channel: A gRPC channel object.

        Raises:
            google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials``
              and ``credentials_file`` are passed.
        """

        return grpc_helpers.create_channel(
            host,
            credentials=credentials,
            credentials_file=credentials_file,
            quota_project_id=quota_project_id,
            default_scopes=cls.AUTH_SCOPES,
            scopes=scopes,
            default_host=cls.DEFAULT_HOST,
            **kwargs,
        )

    @property
    def grpc_channel(self) -> grpc.Channel:
        """Return the channel designed to connect to this service."""
        return self._grpc_channel

    @property
    def operations_client(self) -> operations_v1.OperationsClient:
        """Create the client designed to process long-running operations.

        This property caches on the instance; repeated calls return the same
        client.
        """
        # Quick check: Only create a new client if we do not already have one.
        if self._operations_client is None:
            self._operations_client = operations_v1.OperationsClient(self.grpc_channel)

        # Return the client from cache.
        return self._operations_client

    @property
    def list_documents(
        self,
    ) -> Callable[[document.ListDocumentsRequest], document.ListDocumentsResponse]:
        r"""Return a callable for the list documents method over gRPC.

        Returns the list of all documents of the knowledge
        base.

        Returns:
            Callable[[~.ListDocumentsRequest],
                    ~.ListDocumentsResponse]:
                A function that, when called, will call the underlying RPC
                on the server.
        """
        # Generate a "stub function" on-the-fly which will actually make
        # the request.
        # gRPC handles serialization and deserialization, so we just need
        # to pass in the functions for each.
        if "list_documents" not in self._stubs:
            self._stubs["list_documents"] = self.grpc_channel.unary_unary(
                "/google.cloud.dialogflow.v2.Documents/ListDocuments",
                request_serializer=document.ListDocumentsRequest.serialize,
                response_deserializer=document.ListDocumentsResponse.deserialize,
            )
        return self._stubs["list_documents"]

    @property
    def get_document(
        self,
    ) -> Callable[[document.GetDocumentRequest], document.Document]:
        r"""Return a callable for the get document method over gRPC.

        Retrieves the specified document.

        Returns:
            Callable[[~.GetDocumentRequest],
                    ~.Document]:
                A function that, when called, will call the underlying RPC
                on the server.
        """
        # Generate a "stub function" on-the-fly which will actually make
        # the request.
        # gRPC handles serialization and deserialization, so we just need
        # to pass in the functions for each.
        if "get_document" not in self._stubs:
            self._stubs["get_document"] = self.grpc_channel.unary_unary(
                "/google.cloud.dialogflow.v2.Documents/GetDocument",
                request_serializer=document.GetDocumentRequest.serialize,
                response_deserializer=document.Document.deserialize,
            )
        return self._stubs["get_document"]

    @property
    def create_document(
        self,
    ) -> Callable[[gcd_document.CreateDocumentRequest], operations_pb2.Operation]:
        r"""Return a callable for the create document method over gRPC.

        Creates a new document.

        This method is a `long-running
        operation <https://cloud.google.com/dialogflow/cx/docs/how/long-running-operation>`__.
        The returned ``Operation`` type has the following
        method-specific fields:

        -  ``metadata``:
           [KnowledgeOperationMetadata][google.cloud.dialogflow.v2.KnowledgeOperationMetadata]
        -  ``response``: [Document][google.cloud.dialogflow.v2.Document]

        Returns:
            Callable[[~.CreateDocumentRequest],
                    ~.Operation]:
                A function that, when called, will call the underlying RPC
                on the server.
        """
        # Generate a "stub function" on-the-fly which will actually make
        # the request.
        # gRPC handles serialization and deserialization, so we just need
        # to pass in the functions for each.
        if "create_document" not in self._stubs:
            self._stubs["create_document"] = self.grpc_channel.unary_unary(
                "/google.cloud.dialogflow.v2.Documents/CreateDocument",
                request_serializer=gcd_document.CreateDocumentRequest.serialize,
                response_deserializer=operations_pb2.Operation.FromString,
            )
        return self._stubs["create_document"]

    @property
    def import_documents(
        self,
    ) -> Callable[[document.ImportDocumentsRequest], operations_pb2.Operation]:
        r"""Return a callable for the import documents method over gRPC.

        Creates documents by importing data from external sources.
        Dialogflow supports up to 350 documents in each request. If you
        try to import more, Dialogflow will return an error.

        This method is a `long-running
        operation <https://cloud.google.com/dialogflow/cx/docs/how/long-running-operation>`__.
        The returned ``Operation`` type has the following
        method-specific fields:

        -  ``metadata``:
           [KnowledgeOperationMetadata][google.cloud.dialogflow.v2.KnowledgeOperationMetadata]
        -  ``response``:
           [ImportDocumentsResponse][google.cloud.dialogflow.v2.ImportDocumentsResponse]

        Returns:
            Callable[[~.ImportDocumentsRequest],
                    ~.Operation]:
                A function that, when called, will call the underlying RPC
                on the server.
        """
        # Generate a "stub function" on-the-fly which will actually make
        # the request.
        # gRPC handles serialization and deserialization, so we just need
        # to pass in the functions for each.
        if "import_documents" not in self._stubs:
            self._stubs["import_documents"] = self.grpc_channel.unary_unary(
                "/google.cloud.dialogflow.v2.Documents/ImportDocuments",
                request_serializer=document.ImportDocumentsRequest.serialize,
                response_deserializer=operations_pb2.Operation.FromString,
            )
        return self._stubs["import_documents"]

    @property
    def delete_document(
        self,
    ) -> Callable[[document.DeleteDocumentRequest], operations_pb2.Operation]:
        r"""Return a callable for the delete document method over gRPC.

        Deletes the specified document.

        This method is a `long-running
        operation <https://cloud.google.com/dialogflow/cx/docs/how/long-running-operation>`__.
        The returned ``Operation`` type has the following
        method-specific fields:

        -  ``metadata``:
           [KnowledgeOperationMetadata][google.cloud.dialogflow.v2.KnowledgeOperationMetadata]
        -  ``response``: An `Empty
           message <https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#empty>`__

        Returns:
            Callable[[~.DeleteDocumentRequest],
                    ~.Operation]:
                A function that, when called, will call the underlying RPC
                on the server.
        """
        # Generate a "stub function" on-the-fly which will actually make
        # the request.
        # gRPC handles serialization and deserialization, so we just need
        # to pass in the functions for each.
        if "delete_document" not in self._stubs:
            self._stubs["delete_document"] = self.grpc_channel.unary_unary(
                "/google.cloud.dialogflow.v2.Documents/DeleteDocument",
                request_serializer=document.DeleteDocumentRequest.serialize,
                response_deserializer=operations_pb2.Operation.FromString,
            )
        return self._stubs["delete_document"]

    @property
    def update_document(
        self,
    ) -> Callable[[gcd_document.UpdateDocumentRequest], operations_pb2.Operation]:
        r"""Return a callable for the update document method over gRPC.

        Updates the specified document.

        This method is a `long-running
        operation <https://cloud.google.com/dialogflow/cx/docs/how/long-running-operation>`__.
        The returned ``Operation`` type has the following
        method-specific fields:

        -  ``metadata``:
           [KnowledgeOperationMetadata][google.cloud.dialogflow.v2.KnowledgeOperationMetadata]
        -  ``response``: [Document][google.cloud.dialogflow.v2.Document]

        Returns:
            Callable[[~.UpdateDocumentRequest],
                    ~.Operation]:
                A function that, when called, will call the underlying RPC
                on the server.
        """
        # Generate a "stub function" on-the-fly which will actually make
        # the request.
        # gRPC handles serialization and deserialization, so we just need
        # to pass in the functions for each.
        if "update_document" not in self._stubs:
            self._stubs["update_document"] = self.grpc_channel.unary_unary(
                "/google.cloud.dialogflow.v2.Documents/UpdateDocument",
                request_serializer=gcd_document.UpdateDocumentRequest.serialize,
                response_deserializer=operations_pb2.Operation.FromString,
            )
        return self._stubs["update_document"]

    @property
    def reload_document(
        self,
    ) -> Callable[[document.ReloadDocumentRequest], operations_pb2.Operation]:
        r"""Return a callable for the reload document method over gRPC.

        Reloads the specified document from its specified source,
        content_uri or content. The previously loaded content of the
        document will be deleted. Note: Even when the content of the
        document has not changed, there still may be side effects
        because of internal implementation changes.

        This method is a `long-running
        operation <https://cloud.google.com/dialogflow/cx/docs/how/long-running-operation>`__.
        The returned ``Operation`` type has the following
        method-specific fields:

        -  ``metadata``:
           [KnowledgeOperationMetadata][google.cloud.dialogflow.v2.KnowledgeOperationMetadata]
        -  ``response``: [Document][google.cloud.dialogflow.v2.Document]

        Note: The ``projects.agent.knowledgeBases.documents`` resource
        is deprecated; only use ``projects.knowledgeBases.documents``.

        Returns:
            Callable[[~.ReloadDocumentRequest],
                    ~.Operation]:
                A function that, when called, will call the underlying RPC
                on the server.
        """
        # Generate a "stub function" on-the-fly which will actually make
        # the request.
        # gRPC handles serialization and deserialization, so we just need
        # to pass in the functions for each.
        if "reload_document" not in self._stubs:
            self._stubs["reload_document"] = self.grpc_channel.unary_unary(
                "/google.cloud.dialogflow.v2.Documents/ReloadDocument",
                request_serializer=document.ReloadDocumentRequest.serialize,
                response_deserializer=operations_pb2.Operation.FromString,
            )
        return self._stubs["reload_document"]

    @property
    def export_document(
        self,
    ) -> Callable[[document.ExportDocumentRequest], operations_pb2.Operation]:
        r"""Return a callable for the export document method over gRPC.

        Exports a smart messaging candidate document into the specified
        destination.

        This method is a `long-running
        operation <https://cloud.google.com/dialogflow/cx/docs/how/long-running-operation>`__.
        The returned ``Operation`` type has the following
        method-specific fields:

        -  ``metadata``:
           [KnowledgeOperationMetadata][google.cloud.dialogflow.v2.KnowledgeOperationMetadata]
        -  ``response``: [Document][google.cloud.dialogflow.v2.Document]

        Returns:
            Callable[[~.ExportDocumentRequest],
                    ~.Operation]:
                A function that, when called, will call the underlying RPC
                on the server.
        """
        # Generate a "stub function" on-the-fly which will actually make
        # the request.
        # gRPC handles serialization and deserialization, so we just need
        # to pass in the functions for each.
        if "export_document" not in self._stubs:
            self._stubs["export_document"] = self.grpc_channel.unary_unary(
                "/google.cloud.dialogflow.v2.Documents/ExportDocument",
                request_serializer=document.ExportDocumentRequest.serialize,
                response_deserializer=operations_pb2.Operation.FromString,
            )
        return self._stubs["export_document"]

    def close(self):
        self.grpc_channel.close()

    @property
    def cancel_operation(
        self,
    ) -> Callable[[operations_pb2.CancelOperationRequest], None]:
        r"""Return a callable for the cancel_operation method over gRPC."""
        # Generate a "stub function" on-the-fly which will actually make
        # the request.
        # gRPC handles serialization and deserialization, so we just need
        # to pass in the functions for each.
        if "cancel_operation" not in self._stubs:
            self._stubs["cancel_operation"] = self.grpc_channel.unary_unary(
                "/google.longrunning.Operations/CancelOperation",
                request_serializer=operations_pb2.CancelOperationRequest.SerializeToString,
                response_deserializer=None,
            )
        return self._stubs["cancel_operation"]

    @property
    def get_operation(
        self,
    ) -> Callable[[operations_pb2.GetOperationRequest], operations_pb2.Operation]:
        r"""Return a callable for the get_operation method over gRPC."""
        # Generate a "stub function" on-the-fly which will actually make
        # the request.
        # gRPC handles serialization and deserialization, so we just need
        # to pass in the functions for each.
        if "get_operation" not in self._stubs:
            self._stubs["get_operation"] = self.grpc_channel.unary_unary(
                "/google.longrunning.Operations/GetOperation",
                request_serializer=operations_pb2.GetOperationRequest.SerializeToString,
                response_deserializer=operations_pb2.Operation.FromString,
            )
        return self._stubs["get_operation"]

    @property
    def list_operations(
        self,
    ) -> Callable[
        [operations_pb2.ListOperationsRequest], operations_pb2.ListOperationsResponse
    ]:
        r"""Return a callable for the list_operations method over gRPC."""
        # Generate a "stub function" on-the-fly which will actually make
        # the request.
        # gRPC handles serialization and deserialization, so we just need
        # to pass in the functions for each.
        if "list_operations" not in self._stubs:
            self._stubs["list_operations"] = self.grpc_channel.unary_unary(
                "/google.longrunning.Operations/ListOperations",
                request_serializer=operations_pb2.ListOperationsRequest.SerializeToString,
                response_deserializer=operations_pb2.ListOperationsResponse.FromString,
            )
        return self._stubs["list_operations"]

    @property
    def list_locations(
        self,
    ) -> Callable[
        [locations_pb2.ListLocationsRequest], locations_pb2.ListLocationsResponse
    ]:
        r"""Return a callable for the list locations method over gRPC."""
        # Generate a "stub function" on-the-fly which will actually make
        # the request.
        # gRPC handles serialization and deserialization, so we just need
        # to pass in the functions for each.
        if "list_locations" not in self._stubs:
            self._stubs["list_locations"] = self.grpc_channel.unary_unary(
                "/google.cloud.location.Locations/ListLocations",
                request_serializer=locations_pb2.ListLocationsRequest.SerializeToString,
                response_deserializer=locations_pb2.ListLocationsResponse.FromString,
            )
        return self._stubs["list_locations"]

    @property
    def get_location(
        self,
    ) -> Callable[[locations_pb2.GetLocationRequest], locations_pb2.Location]:
        r"""Return a callable for the list locations method over gRPC."""
        # Generate a "stub function" on-the-fly which will actually make
        # the request.
        # gRPC handles serialization and deserialization, so we just need
        # to pass in the functions for each.
        if "get_location" not in self._stubs:
            self._stubs["get_location"] = self.grpc_channel.unary_unary(
                "/google.cloud.location.Locations/GetLocation",
                request_serializer=locations_pb2.GetLocationRequest.SerializeToString,
                response_deserializer=locations_pb2.Location.FromString,
            )
        return self._stubs["get_location"]

    @property
    def kind(self) -> str:
        return "grpc"

__all__ = ("DocumentsGrpcTransport",)


text:
import logging
import time
from datetime import datetime, timedelta

from django.conf import settings
from django.core.management.base import BaseCommand
from django.db import connection, transaction

from kitsune.questions.models import Question, QuestionMappingType
from kitsune.search.es_utils import ES_EXCEPTIONS, get_documents
from kitsune.search.tasks import index_task

log = logging.getLogger('k.cron')

class Command(BaseCommand):
    help = "Archive all questions that were created over 180 days ago."

    def handle(self, **options):
        # Set up logging so it doesn't send Ricky email.
        logging.basicConfig(level=logging.ERROR)

        # Get a list of ids of questions we're going to go change. We need
        # a list of ids so that we can feed it to the update, but then
        # also know what we need to update in the index.
        days_180 = datetime.now() - timedelta(days=180)
        q_ids = list(
            Question.objects.filter(is_archived=False)
            .filter(created__lte=days_180)
            .values_list('id', flat=True))

        if q_ids:
            log.info('Updating %d questions', len(q_ids))

            sql = """
                UPDATE questions_question
                SET is_archived = 1
                WHERE id IN (%s)
                """ % ','.join(map(str, q_ids))

            cursor = connection.cursor()
            cursor.execute(sql)
            if not transaction.get_connection().in_atomic_block:
                transaction.commit()

            if settings.ES_LIVE_INDEXING:
                try:
                    # So... the first time this runs, it'll handle 160K
                    # questions or so which stresses everything. Thus we
                    # do it in chunks because otherwise this won't work.
                    #
                    # After we've done this for the first time, we can nix
                    # the chunking code.

                    from kitsune.search.utils import chunked
                    for chunk in chunked(q_ids, 100):

                        # Fetch all the documents we need to update.
                        es_docs = get_documents(QuestionMappingType, chunk)

                        log.info('Updating %d index documents', len(es_docs))

                        documents = []

                        # For each document, update the data and stick it
                        # back in the index.
                        for doc in es_docs:
                            doc[u'question_is_archived'] = True
                            doc[u'indexed_on'] = int(time.time())
                            documents.append(doc)

                        QuestionMappingType.bulk_index(documents)

                except ES_EXCEPTIONS:
                    # Something happened with ES, so let's push index
                    # updating into an index_task which retries when it
                    # fails because of ES issues.
                    index_task.delay(QuestionMappingType, q_ids)

redpajama_set_name: RedPajamaGithub


text:
if [ "$UID" -ne 0 ]; then
	echo "Please run as root."
	exit
fi

usage() {
	echo "Usage: $0 -n <container> -c <command>"
	echo "Example: $0 -n richard -c apt-get update"
	exit 1
}

# Parse options
while getopts ":n:c:" o; do
	case "${o}" in
		n)
			opt_container=${OPTARG}
			;;
		c)
			opt_command=${OPTARG}
			;;
		*)
			usage
			;;
	esac
done
shift $((OPTIND-1))

if [ -z "${opt_container}" ] || [ -z "${opt_command}" ]; then
	usage
fi

if [ "$opt_container" != "all" ]; then
	# Check the container is valid
	found=0
	for container in $(lxc-ls)
	do
		if [ "$opt_container" = "$container" ]; then
			found=1
		fi
	done
	if [ $found -eq 0 ]; then
		echo "Container does not exist!"
		exit 1
	fi
fi

if [ "$opt_container" = "all" ]; then
	echo "Running command '$opt_command' in *all* containers..."
	for container in $(lxc-ls)
	do
		if [ "$container" != "ubuntu-base" ] && [ "$container" != "ubuntu-lamp" ]; then
			echo "Running command '$opt_command' in '$container'..."
			lxc-attach -n "$container" -- $opt_command
		fi
	done
else
	echo "Running command '$opt_command' in '$opt_container'..."
	lxc-attach -n "$opt_container" -- $opt_command
fi

echo "Done."


text:
package org.kuali.continuity.admin.main.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.IntegerFieldDef;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;

public class Dependency extends SimpleGridViewer {

	// Setup specific for Dependencies
	final TextBox textBox = new TextBox();
	final ListBox type = new ListBox();
	int[] gcsize = { 100, 40, 300 };
	public ColumnConfig typeCol = new ColumnConfig("Type", "description", gcsize[0],
			true);
	public ColumnConfig orderColumn = new ColumnConfig("Order", "order",
			gcsize[1], true);
	public ColumnConfig dependencyCol = new ColumnConfig("Dependency", "name",
			gcsize[2], true);

	ColumnModel columnModel = new ColumnModel(new ColumnConfig[] { typeCol,
			orderColumn, dependencyCol });

	public void onModuleLoad() {
		// Top level info

		itemService= (ItemServiceAsync) GWT.create(ItemService.class);

		Element screenelement = DOM.getElementById("itemdata");
		Element formelement = DOM.getElementById("formelement");
		Element pluralelement = DOM.getElementById("plural");
		String plural = pluralelement.getTitle();

		String pageInfo = screenelement.getTitle();
		String pageTitle = screenelement.getTitle();
		String proxyUrl = "./items";
		String formItemName = pageTitle;
		itemRootName = pageInfo;
		// Grid
		RecordDef recordDef = new RecordDef(new FieldDef[] {
				new StringFieldDef("id"), new StringFieldDef("name"),
				new IntegerFieldDef("order"), new StringFieldDef("description") });

		super.onModuleLoad(plural, proxyUrl, columnModel, recordDef);
		this.formController=dependencyFormController;

		// Buttons

		this.setButtonNames(pageTitle);

		// Heading
		this.setHeadingName(plural + " Setup");

		// Form Data
		textBox.setMaxLength(50);
		textBox.setWidth("335px");
		flexForm.addFormItem(formItemName, textBox);
		type.setVisibleItemCount(1);

		type.addItem("--Select--","2");
		type.addItem("Upstream","1");
		type.addItem("Downstream","0");
		flexForm.addFormItem("Type", type);
		flexForm.addFormItem("Order", orderBox);

      	// set token
      	String token = Cookies.getCookie(CSRF_TOKEN);
      	csrfToken.setValue(token);
      	flexForm.registerWidget(csrfToken);

      	gridPanel.setCls(".myclass");
		itemRootName=itemRootName.toLowerCase();
	}

	public AsyncCallback selectItemCallback = new AsyncCallback() {
		public void onSuccess(Object result) {
			// Get Item from result and load form.

			// Window.alert("We got an item here."+item.getName());
			dependencyFormController.formLoad(result);
			// textBox.setText(item.getName());
			// orderBox.setText(item.getOrder());
			// //Load item fields.
		}

		public void onFailure(Throwable e) {
			String directLoginCookie = Cookies.getCookie(KUALI_DIRECTLOGIN_COOKIE_KEY);
			String shibbolethLoginCookie = Cookies.getCookie(SHIBBOLETH_LOGIN_IDP_ID);

			if (directLoginCookie != null || shibbolethLoginCookie != null){
				redirect(getWelcomeUrl());
			} else {
				redirect(getLogoutUrlWithParm(SESSION_EXPIRED_ERROR));
			}
		}

	};

	SimpleFormController dependencyFormController = new SimpleFormController() {
		public boolean formLoad(Object result) {
			DependencyItem item = (DependencyItem) result;
			textBox.setText(item.getName());
			orderBox.setText(item.getOrder());
			String typestr = item.getDescription();
			int typeIndex=1;
			if (typestr.equals("Downstream")) {
				typeIndex=2;
			}
			type.setSelectedIndex(typeIndex);

			return true;
		}

		public NamedItem validateSave(NamedItem namedItem) {
            DependencyItem di = (DependencyItem) namedItem;
            di.setName(textBox.getText());
            int tind=type.getSelectedIndex();
            int itype = Integer.parseInt(type.getValue(tind));
            di.setType(itype);
            di.setCsrfToken(csrfToken.getValue());
            return (NamedItem) di;
		}

		Item saveFields() {

			Item item = this.validate();
			if (item != null) {
				item.setId(currentId);

			}
			return item;
		}

		public Item validate() {
			DependencyItem item = new DependencyItem();
			boolean ret = true;
			int order = 0;
			String excp = "";
			try {
				// This check can be removed.  All these are orderedTypes
				if (checkForGetMethod(item, "getOrder")) {
					String orderStr = orderBox.getText();
					if (orderStr.trim().isEmpty()) orderStr = "999";  // max 3 digit int
					item.setOrder(orderStr);
					order = Integer.parseInt(item.getOrder().trim());
//					if (order == 0) {
//						ret = false;
//						setError("Order must be a numeric value. " + excp);
//					}
				}

				// dependency type
				String typeString = type.getValue(type.getSelectedIndex());
				if (typeString.isEmpty() || typeString.equals("2")) {
					ret = false;
					setError("Type selection required");
				} else {
					item = (DependencyItem) validateSave((NamedItem) item);
					item.setId(currentId);
				}

			//	item.setName(textBox.getText());
			} catch (NumberFormatException e) {
				ret = false;
				setError("Order must be a numeric value. " + excp);
			} catch (Exception e) {
				excp = e.toString();

			}
			ret = ret && validation();
			showErr(!ret);
			if (!ret) {
				item = null;
			}
			return item;

		}

		public boolean delete(String itemRootName, AsyncCallback updateItemCallback) {
			Item item = saveFields();
			itemService.deleteItem(itemRootName, item, updateItemCallback);
			return true;
		}

		public boolean insert(String itemRootName, AsyncCallback updateItemCallback) {
			Item item = saveFields();
			if (item != null) {
				itemService.insertItem(itemRootName, item, updateItemCallback);
			}
			return true;
		}

		public boolean update(String itemRootName, AsyncCallback updateItemCallback) {

			Item item = saveFields();

			if (item != null) {
				itemService.updateItem(itemRootName, item, updateItemCallback);
			}
			return true;
		}

		public void getItem(String itemRootName, String id) {
			itemService.getItem(itemRootName, id, selectItemCallback);
		}

		public boolean gridDisplayEdit() {

			return true;

		}

	};

}


text:
require 'spec_helper'

describe "package agent" do
  before do
    agent_file = File.join([File.dirname(__FILE__), "../agent/puppet-package.rb"])
    @agent = MCollective::Test::LocalAgentTest.new("package", :agent_file => agent_file).plugin
  end
  after :all do
    MCollective::PluginManager.clear
  end

  describe "#yum_clean" do
    it "should fail if /usr/bin/yum doesn't exist" do
      File.expects(:exist?).with("/usr/bin/yum").returns(false)
      result = @agent.call(:yum_clean)
      result.should be_aborted_error
      result[:statusmsg].should == "Cannot find yum at /usr/bin/yum"
    end

    it "should succeed if run method returns 0" do
      File.expects(:exist?).with("/usr/bin/yum").returns(true)
      @agent.config.expects(:pluginconf).returns({"package.yum_clean_mode" => "all"})
      @agent.expects(:run).with("/usr/bin/yum clean all", :stdout => :output, :chomp => true).returns(0)

      result = @agent.call(:yum_clean)
      result.should be_successful
      result.should have_data_items(:exitcode => 0)
    end

    it "should fail if the run method doesn't return 0" do
      File.expects(:exist?).with("/usr/bin/yum").returns(true)
      @agent.expects(:run).with("/usr/bin/yum clean all", :stdout => :output, :chomp => true).returns(1)
      @agent.config.expects(:pluginconf).returns({"package.yum_clean_mode" => "all"})
      result = @agent.call(:yum_clean)
      result.should be_aborted_error
      result.should have_data_items(:exitcode => 1)
    end

    it "should default to 'all' mode" do
      File.expects(:exist?).with("/usr/bin/yum").returns(true)
      @agent.config.expects(:pluginconf).returns({})
      @agent.expects(:run).with("/usr/bin/yum clean all", :stdout => :output, :chomp => true).returns(0)

      result = @agent.call(:yum_clean)
      result.should be_successful
      result.should have_data_items(:exitcode => 0)
    end

    it "should support a configured mode" do
      File.expects(:exist?).with("/usr/bin/yum").returns(true)
      @agent.config.expects(:pluginconf).returns({"package.yum_clean_mode" => "headers"})
      @agent.expects(:run).with("/usr/bin/yum clean headers", :stdout => :output, :chomp => true).returns(0)

      result = @agent.call(:yum_clean)
      result.should be_successful
      result.should have_data_items(:exitcode => 0)
    end

    it "should support configured modes" do
      ["all", "headers", "packages", "metadata", "dbcache", "plugins", "expire-cache"].each do |mode|
        File.expects(:exist?).with("/usr/bin/yum").returns(true)
        @agent.config.expects(:pluginconf).returns({"package.yum_clean_mode" => "all"})
        @agent.expects(:run).with("/usr/bin/yum clean #{mode}", :stdout => :output, :chomp => true).returns(0)

        result = @agent.call(:yum_clean, :mode => mode)
        result.should be_successful
        result.should have_data_items(:exitcode => 0)
      end
    end
  end

  describe "#apt_update" do
    it "should fail if /usr/bin/apt-get doesn't exist" do
      File.expects(:exist?).with("/usr/bin/apt-get").returns(false)
      result = @agent.call(:apt_update)
      result.should be_aborted_error
      result[:statusmsg].should == "Cannot find apt-get at /usr/bin/apt-get"
    end

    it "should succeed if the agent responds to 'run' and the run method returns 0" do
      File.expects(:exist?).with("/usr/bin/apt-get").returns(true)
      @agent.expects(:run).with("/usr/bin/apt-get update", :stdout => :output, :chomp => true).returns(0)
      result = @agent.call(:apt_update)
      result.should have_data_items(:exitcode => 0)
      result.should be_successful
    end

    it "should fail if the agent responds to 'run' and the run method doesn't return 0" do
      File.expects(:exist?).with("/usr/bin/apt-get").returns(true)
      @agent.expects(:run).with("/usr/bin/apt-get update", :stdout => :output, :chomp => true).returns(1)
      result = @agent.call(:apt_update)
      result.should have_data_items(:exitcode => 1)
      result.should be_aborted_error
    end
  end

  describe "#checkupdates" do
    it "should fail if neither /usr/bin/yum or /usr/bin/apt-get are present" do
      File.expects(:exist?).with("/usr/bin/yum").returns(false)
      File.expects(:exist?).with("/usr/bin/apt-get").returns(false)
      result = @agent.call(:checkupdates)
      result.should be_aborted_error
      result[:statusmsg].should == "Cannot find a compatible package system to check updates for"
    end

    it "should call yum_checkupdates if /usr/bin/yum exists" do
      File.expects(:exist?).with("/usr/bin/yum").returns(true)
      @agent.expects(:yum_checkupdates_action).returns(true)
      result = @agent.call(:checkupdates)
      result.should be_true
      result.should have_data_items(:package_manager=>"yum")
    end

    it "should call apt_checkupdates if /usr/bin/apt-get exists" do
      File.expects(:exist?).with("/usr/bin/yum").returns(false)
      File.expects(:exist?).with("/usr/bin/apt-get").returns(true)
      @agent.expects(:apt_checkupdates_action).returns(true)
      result = @agent.call(:checkupdates)
      result.should have_data_items(:package_manager=>"apt")
      result.should be_true
    end
  end

  describe "#yum_checkupdates" do
    it "should fail if /usr/bin/yum does not exist" do
      File.expects(:exist?).with("/usr/bin/yum").returns(false)
      result = @agent.call(:yum_checkupdates)
      result.should be_aborted_error
      result[:statusmsg].should == "Cannot find yum at /usr/bin/yum"
    end

    it "should succeed if it responds to run and there are packages to update" do
      File.expects(:exist?).with("/usr/bin/yum").returns(true)
      @agent.expects(:run).with("/usr/bin/yum -q check-update", :stdout => :output, :chomp => true).returns(100)
      @agent.expects(:do_yum_outdated_packages)
      result = @agent.call(:yum_checkupdates)
      result.should be_successful
      result.should have_data_items(:outdated_packages=>nil, :exitcode=>100)
    end
  end

  describe "#apt_checkupdates" do
    it "should fail if /usr/bin/apy-get does not exist" do
      File.expects(:exist?).with("/usr/bin/apt-get").returns(false)
      result = @agent.call(:apt_checkupdates)
      result.should be_aborted_error
      result[:statusmsg].should == "Cannot find apt at /usr/bin/apt-get"
    end

    it "should succeed if it responds to run and returns exit code of 0" do
      @agent.stubs("reply").returns({:output => "Inst emacs23 [23.1+1-4ubuntu7] (23.1+1-4ubuntu7.1 Ubuntu:10.04/lucid-updates) []", :exitcode => 0})

      File.expects(:exist?).with("/usr/bin/apt-get").returns(true)
      @agent.expects(:run).with("/usr/bin/apt-get --simulate dist-upgrade", :stdout => :output, :chomp => true).returns(0)

      result = @agent.call(:apt_checkupdates)
      result.should be_successful

    end

    it "should fail if it responds to 'run' but returns an error code that is not 0" do
      File.expects(:exist?).with("/usr/bin/apt-get").returns(true)
      @agent.expects(:run).with("/usr/bin/apt-get --simulate dist-upgrade", :stdout => :output, :chomp => true).returns(1)
      result = @agent.call(:apt_checkupdates)
      result.should be_aborted_error
      result.should have_data_items(:outdated_packages=>[], :exitcode=>1)
    end
  end

  describe "#do_pkg_action" do
    before(:each) do
      @puppet_type = mock
      @puppet_type.stubs(:clear)
      @puppet_package = mock
    end

    describe "#install" do
      it "should install if the package is absent" do
        [:absent, :purged].each do |status|
          Puppet::Type.stubs(:type).with(:package).returns(@puppet_type)

          @puppet_type.expects(:new).with(:name => "package").returns(@puppet_package)
          @puppet_package.expects(:provider).returns(@puppet_package)
          @puppet_package.expects(:install).returns(0)
          @puppet_package.expects(:properties).twice.returns({:ensure => status})
          @puppet_package.expects(:flush)

          result = @agent.call(:install, :package => "package")
          result.should be_successful
          result.should have_data_items(:ensure => status.to_s, :output => 0)
        end
      end

      it "should not install if the package is present" do
        Puppet::Type.stubs(:type).with(:package).returns(@puppet_type)

        @puppet_type.expects(:new).with(:name => "package").returns(@puppet_package)
        @puppet_package.expects(:provider).returns(@puppet_package)
        @puppet_package.expects(:properties).twice.returns({:ensure => "123"})
        @puppet_package.expects(:flush)

        result = @agent.call(:install, :package => "package")
        result.should be_successful
        result.should have_data_items(:ensure => "123", :output => "")
      end
    end

    describe "#update" do
      it "should update unless the package is absent" do
        Puppet::Type.stubs(:type).with(:package).returns(@puppet_type)

        @puppet_type.expects(:new).with(:name => "package").returns(@puppet_package)
        @puppet_package.expects(:provider).returns(@puppet_package)
        @puppet_package.expects(:update).returns(0)
        @puppet_package.expects(:properties).twice.returns({:ensure => :not_absent})
        @puppet_package.expects(:flush)

        result = @agent.call(:update, :package => "package")
        result.should be_successful
        result.should have_data_items(:ensure => "not_absent", :output => 0)
      end

      it "should not update if the package is not installed" do
        [:absent, :purged].each do |status|
          Puppet::Type.stubs(:type).with(:package).returns(@puppet_type)

          @puppet_type.expects(:new).with(:name => "package").returns(@puppet_package)
          @puppet_package.expects(:provider).returns(@puppet_package)
          @puppet_package.expects(:properties).twice.returns({:ensure => status})
          @puppet_package.expects(:flush)

          result = @agent.call(:update, :package => "package")
          result.should be_successful
          result.should have_data_items(:ensure => status.to_s, :output => "")
        end
      end
    end

    describe "#uninstall" do
      it "should uninstall if the package is present" do
        Puppet::Type.stubs(:type).with(:package).returns(@puppet_type)

        @puppet_type.expects(:new).with(:name => "package").returns(@puppet_package)
        @puppet_package.expects(:provider).returns(@puppet_package)
        @puppet_package.expects(:uninstall).returns(0)
        @puppet_package.expects(:properties).twice.returns({:ensure => :not_absent})
        @puppet_package.expects(:flush)

        result = @agent.call(:uninstall, :package => "package")
        result.should be_successful
        result.should have_data_items(:ensure => "not_absent", :output=>0)
      end

      it "should not uninstall if the package is absent" do
        [:absent, :purged].each do |status|
          Puppet::Type.stubs(:type).with(:package).returns(@puppet_type)

          @puppet_type.expects(:new).with(:name => "package").returns(@puppet_package)
          @puppet_package.expects(:provider).returns(@puppet_package)
          @puppet_package.expects(:properties).twice.returns({:ensure => status})
          @puppet_package.expects(:flush)

          result = @agent.call(:uninstall, :package => "package")
          result.should be_successful
          result.should have_data_items(:ensure => status.to_s, :output => "")
        end
      end
    end

    describe "#status" do
      it "should return the status of the package" do
        Puppet::Type.stubs(:type).with(:package).returns(@puppet_type)

        @puppet_type.expects(:new).with(:name => "package").returns(@puppet_package)
        @puppet_package.expects(:provider).returns(@puppet_package)
        @puppet_package.expects(:flush)
        @puppet_package.expects(:properties).returns({:status => "rspec"})

        result = @agent.call(:status, :package => "package")
        result.should be_successful
        result.should have_data_items(:status => "rspec", :output=>"")
      end
    end
    describe "#purge" do
      it "should run purge on the package object" do
        Puppet::Type.stubs(:type).with(:package).returns(@puppet_type)

        @puppet_type.expects(:new).with(:name => "package").returns(@puppet_package)
        @puppet_package.expects(:provider).returns(@puppet_package)
        @puppet_package.expects(:flush)
        @puppet_package.expects(:purge).returns("Purged")
        @puppet_package.expects(:properties)

        result = @agent.call(:purge, :package => "package")
        result.should be_successful
        result.should have_data_items(:output=>"Purged")
      end
    end
    describe "#Exceptions" do
      it "should fail if exception is raised" do
        Puppet::Type.stubs(:type).raises("Exception")
        result = @agent.call(:install, :package => "package")
        result.should be_aborted_error
        result[:statusmsg].should == "Exception"
      end
    end
  end
  describe "#do_yum_outdated_packages" do
    it "should not do anything with obsoleted packages" do
      File.expects(:exist?).with("/usr/bin/yum").returns(true)
      @agent.expects(:run).with("/usr/bin/yum -q check-update", :stdout => :output, :chomp => true).returns(100)
      @agent.stubs(:reply).returns(:output => "Obsoleting")

      result = @agent.call(:yum_checkupdates)
      result.should be_successful
    end

    it "should return packages which need to be updated" do
      File.expects(:exist?).with("/usr/bin/yum").returns(true)
      @agent.expects(:run).with("/usr/bin/yum -q check-update", :stdout => :output, :chomp => true).returns(100)
      @agent.stubs(:reply).returns(:output => "Package version repo", :outdated_packages => "foo")

      result = @agent.call(:yum_checkupdates)
      result.should be_successful
    end
  end
end


text:
//---------------------------------------------------------------------------//

//---------------------------------------------------------------------------//
/*!
 * \file   DTK_IntrepidCell.cpp
 * \author Stuart Slattery
 * \brief  Manager for intrepid cell-level operations.
 */
//---------------------------------------------------------------------------//

#include "DTK_DBC.hpp"

#include "DTK_IntrepidCell.hpp"

#include <Teuchos_as.hpp>

#include <Intrepid_CellTools.hpp>
#include <Intrepid_DefaultCubatureFactory.hpp>
#include <Intrepid_FunctionSpaceTools.hpp>
#include <Intrepid_Types.hpp>

namespace DataTransferKit
{
//---------------------------------------------------------------------------//
/*!
 * \brief Constructor.
 */
IntrepidCell::IntrepidCell( const shards::CellTopology &cell_topology,
                            const unsigned degree )
    : d_topology( cell_topology )
    , d_cub_points( 0, 0 )
    , d_cub_weights( 0 )
    , d_jacobian( 0, 0, 0, 0 )
    , d_jacobian_det( 0, 0 )
    , d_weighted_measures( 0, 0 )
    , d_physical_ip_coordinates( 0, 0, 0 )
{
    Intrepid::DefaultCubatureFactory<Scalar, MDArray> cub_factory;
    d_cubature = cub_factory.create( d_topology, degree );

    unsigned num_cub_points = d_cubature->getNumPoints();
    unsigned cub_dim = cell_topology.getDimension();

    d_cub_points.resize( num_cub_points, cub_dim );
    d_cub_weights.resize( num_cub_points );
    d_cubature->getCubature( d_cub_points, d_cub_weights );
}

//---------------------------------------------------------------------------//
/*!
 * \brief Destructor.
 */
IntrepidCell::~IntrepidCell() { /* ... */}

//---------------------------------------------------------------------------//
/*!
 * \brief Given physical coordinates for the cell nodes (Cell,Node,Dim),
 * assign them to the cell without allocating internal data.
 */
void IntrepidCell::setCellNodeCoordinates( const MDArray &cell_node_coords )
{
    d_cell_node_coords = cell_node_coords;
}

//---------------------------------------------------------------------------//
/*!
 * \brief Given physical coordinates for the cell nodes (Cell,Node,Dim),
 * allocate the state of the cell object.
 */
void IntrepidCell::allocateCellState( const MDArray &cell_node_coords )
{
    // Store the cell node coords as the current state.
    setCellNodeCoordinates( cell_node_coords );

    // Get required dimensions.
    int num_cells = d_cell_node_coords.dimension( 0 );
    int num_ip = d_cub_points.dimension( 0 );
    int space_dim = d_cub_points.dimension( 1 );

    // Resize arrays.
    d_jacobian.resize( num_cells, num_ip, space_dim, space_dim );
    d_jacobian_det.resize( num_cells, num_ip );
    d_weighted_measures.resize( num_cells, num_ip );
    d_physical_ip_coordinates.resize( num_cells, num_ip, space_dim );
}

//---------------------------------------------------------------------------//
/*!
 * \brief Update the state of the cell object for the current cell node
 * coordinates.
 */
void IntrepidCell::updateCellState()
{
    // Compute the Jacobian.
    Intrepid::CellTools<Scalar>::setJacobian( d_jacobian, d_cub_points,
                                              d_cell_node_coords, d_topology );

    // Compute the determinant of the Jacobian.
    Intrepid::CellTools<Scalar>::setJacobianDet( d_jacobian_det, d_jacobian );

    // Compute the cell measures.
    Intrepid::FunctionSpaceTools::computeCellMeasure<Scalar>(
        d_weighted_measures, d_jacobian_det, d_cub_weights );

    // Compute physical frame integration point coordinates.
    Intrepid::CellTools<Scalar>::mapToPhysicalFrame(
        d_physical_ip_coordinates, d_cub_points, d_cell_node_coords,
        d_topology );
}

//---------------------------------------------------------------------------//
/*!
 * \brief Free function for updating the cell state for a new set of
 * physical cells in a single call.
 */
void IntrepidCell::updateState( IntrepidCell &intrepid_cell,
                                const MDArray &cell_node_coords )
{
    intrepid_cell.allocateCellState( cell_node_coords );
    intrepid_cell.updateCellState();
}

//---------------------------------------------------------------------------//
/*!
 * \brief Given a set of coordinates in the physical frame of the cell, map
 * them to the reference frame of the cell.
 */
void IntrepidCell::mapToCellReferenceFrame( const MDArray &physical_coords,
                                            MDArray &reference_coords )
{
    DTK_REQUIRE( 2 == physical_coords.rank() );
    DTK_REQUIRE( 2 == reference_coords.rank() );
    Intrepid::CellTools<Scalar>::mapToReferenceFrame(
        reference_coords, physical_coords, d_cell_node_coords, d_topology, 0 );
}

//---------------------------------------------------------------------------//
/*!
 * \brief Given a set of coordinates in the reference frame of the cell, map
 * them to the physical frame.
 */
void IntrepidCell::mapToCellPhysicalFrame( const MDArray &parametric_coords,
                                           MDArray &physical_coords )
{
    DTK_REQUIRE( 2 == parametric_coords.rank() );
    DTK_REQUIRE( 3 == physical_coords.rank() );
    DTK_REQUIRE( parametric_coords.dimension( 1 ) ==
                 Teuchos::as<int>( d_topology.getDimension() ) );
    DTK_REQUIRE( physical_coords.dimension( 0 ) ==
                 d_cell_node_coords.dimension( 0 ) );
    DTK_REQUIRE( physical_coords.dimension( 1 ) ==
                 parametric_coords.dimension( 0 ) );
    DTK_REQUIRE( physical_coords.dimension( 2 ) ==
                 Teuchos::as<int>( d_topology.getDimension() ) );

    Intrepid::CellTools<Scalar>::mapToPhysicalFrame(
        physical_coords, parametric_coords, d_cell_node_coords, d_topology );
}

//---------------------------------------------------------------------------//
/*!
 * \brief Determine if a point given in parametric coordinates is inside of the
 * reference cell.
 */
bool IntrepidCell::pointInReferenceCell( const MDArray &reference_point,
                                         const double tolerance )
{
    return Intrepid::CellTools<Scalar>::checkPointsetInclusion(
        reference_point, d_topology, tolerance );
}

//---------------------------------------------------------------------------//
/*!
 * \brief Determine if a point given in physical coordinates is inside of the
 * phyiscal cell.
 */
bool IntrepidCell::pointInPhysicalCell( const MDArray &point,
                                        const double tolerance )
{
    MDArray reference_point( point );
    reference_point.initialize();
    mapToCellReferenceFrame( point, reference_point );
    return pointInReferenceCell( reference_point, tolerance );
}

//---------------------------------------------------------------------------//
/*!
 * \brief Get the number of cells in the current state.
 */
int IntrepidCell::getNumCells() const
{
    return d_weighted_measures.dimension( 0 );
}

//---------------------------------------------------------------------------//
/*!
 * \brief Get the number of cell points.
 */
int IntrepidCell::getNumIntegrationPoints() const
{
    return d_cub_points.dimension( 0 );
}

//---------------------------------------------------------------------------//
/*!
 * \brief Get the spatial dimension.
 */
int IntrepidCell::getSpatialDimension() const
{
    return d_cub_points.dimension( 1 );
}

//---------------------------------------------------------------------------//
/*!
 * \brief Get the cell measures (Cell). cell_measures must all ready be
 * allocated.
 */
void IntrepidCell::getCellMeasures( MDArray &cell_measures ) const
{
    DTK_REQUIRE( 1 == cell_measures.rank() );
    DTK_REQUIRE( cell_measures.dimension( 0 ) ==
                 d_weighted_measures.dimension( 0 ) );

    MDArray dofs( d_cell_node_coords.dimension( 0 ),
                  d_cub_weights.dimension( 0 ) );
    dofs.initialize( 1.0 );
    integrate( dofs, cell_measures );
}

//---------------------------------------------------------------------------//
// Get the physical cell point coordinates in each cell
// (Cell,IP,Dim).
void IntrepidCell::getPhysicalIntegrationCoordinates(
    MDArray &physical_ip_coordinates ) const
{
    physical_ip_coordinates = d_physical_ip_coordinates;
}

//---------------------------------------------------------------------------//
/*!
 * \brief Given DOFs at the quadrature points {(Cell,Node) for scalar fields,
 * (Cell,Node,VecDim) for vector fields, and (Cell,Node,TensDim1,TensDim2) for
 * tensor fields.} perform the numerical integration in each cell by
 * contracting them with the weighted measures.
 */
void IntrepidCell::integrate( const MDArray &dofs, MDArray &integrals ) const
{
    Intrepid::FunctionSpaceTools::integrate<Scalar>(
        integrals, dofs, d_weighted_measures, Intrepid::COMP_BLAS );
}

//---------------------------------------------------------------------------//

} // end namespace DataTransferKit

//---------------------------------------------------------------------------//
// end DTK_IntrepidCell.cpp
//---------------------------------------------------------------------------//


text:
#include "ameba_soc.h"

struct gdma_memcopy_s {
	u32 ch_num;
	volatile u32 dma_done;
	GDMA_InitTypeDef GDMA_InitStruct;
};

struct gdma_memcopy_s gdma_memcpy;
//xSemaphoreHandle dma_memcpy_sema = NULL;

IMAGE2_RAM_TEXT_SECTION
static void memcpy_gdma_int(void *pData)
{
	/* To avoid gcc warnings */
	( void ) pData;

	//portBASE_TYPE taskWoken = pdFALSE;

	/* Clear Pending ISR */
	GDMA_ClearINT(0, gdma_memcpy.ch_num);
	GDMA_Cmd(0, gdma_memcpy.ch_num, DISABLE);

	gdma_memcpy.dma_done = 1;
	//xSemaphoreGiveFromISR(dma_memcpy_sema, &taskWoken);
	//portEND_SWITCHING_ISR(taskWoken);
}

IMAGE2_RAM_TEXT_SECTION
void memcpy_gdma_init(void)
{
	gdma_memcpy.dma_done = 1;
	gdma_memcpy.ch_num = GDMA_ChnlAlloc(0, (IRQ_FUN)memcpy_gdma_int, 0, 10);

	GDMA_StructInit(&(gdma_memcpy.GDMA_InitStruct));
	gdma_memcpy.GDMA_InitStruct.GDMA_ChNum = gdma_memcpy.ch_num;
	gdma_memcpy.GDMA_InitStruct.GDMA_Index = 0;
	gdma_memcpy.GDMA_InitStruct.GDMA_IsrType = (TransferType|ErrType);

	gdma_memcpy.GDMA_InitStruct.GDMA_SrcMsize = MsizeEight;
	gdma_memcpy.GDMA_InitStruct.GDMA_DstMsize = MsizeEight;

	//vSemaphoreCreateBinary(dma_memcpy_sema);
	//xSemaphoreTake(dma_memcpy_sema, portMAX_DELAY);
}

IMAGE2_RAM_TEXT_SECTION
static inline u32 memcpy_use_cpu(void *dest, void *src, u32 size)
{
	/* To avoid gcc warnings */
	( void ) dest;
	( void ) src;

	if (size < 128) {
		return TRUE;
	}

	if (gdma_memcpy.dma_done == 0) {
		return TRUE;
	}

	return FALSE;
}

IMAGE2_RAM_TEXT_SECTION
int memcpy_gdma(void *dest, void *src, u32 size)
{
	u32 size_4byte = size & ~(0x03);
	u32 left = size & (0x03);

	if (memcpy_use_cpu(dest, src, size) == TRUE) {
		_memcpy(dest, src, size);

		return 0;
	}
	gdma_memcpy.dma_done = 0;

	if (((u32)(src)& 0x03) || ((u32)(dest)& 0x03)) {
		gdma_memcpy.GDMA_InitStruct.GDMA_SrcDataWidth = TrWidthOneByte;
		gdma_memcpy.GDMA_InitStruct.GDMA_DstDataWidth = TrWidthOneByte;
		gdma_memcpy.GDMA_InitStruct.GDMA_BlockSize = size;
	} else {
		/* 4-bytes aligned, move 4 bytes each transfer */
		gdma_memcpy.GDMA_InitStruct.GDMA_SrcDataWidth = TrWidthFourBytes;
		gdma_memcpy.GDMA_InitStruct.GDMA_DstDataWidth = TrWidthFourBytes;
		gdma_memcpy.GDMA_InitStruct.GDMA_BlockSize = size_4byte >> 2;

		if (left != 0) {
			char *dst0 = (char *) dest + size_4byte;
			char *src0 = (char *) src + size_4byte;
			while (left--){
				*dst0++ = *src0++;
			}
		}
	}

	gdma_memcpy.GDMA_InitStruct.GDMA_SrcAddr = (u32)(src);
	gdma_memcpy.GDMA_InitStruct.GDMA_DstAddr = (u32)(dest);
	GDMA_Init(0, gdma_memcpy.ch_num, &(gdma_memcpy.GDMA_InitStruct));
	GDMA_Cmd(0, gdma_memcpy.ch_num, ENABLE);

	//xSemaphoreTake(dma_memcpy_sema, portMAX_DELAY);
	while (gdma_memcpy.dma_done == 0);

	return 0;
}


text:
import numpy as np

from rllab import spaces
from rllab.core.serializable import Serializable
from rllab.envs.base import Step
from rllab.envs.proxy_env import ProxyEnv
from rllab.misc import tensor_utils
from rllab.misc.overrides import overrides
from rllab.envs.normalized_env import NormalizedEnv
from sandbox.snn4hrl.envs.mujoco.maze.fast_maze_env import FastMazeEnv
from sandbox.snn4hrl.policies.hier_multi_mlp_policy import GaussianMLPPolicy_multi_hier
from sandbox.snn4hrl.sampler.utils import rollout  # this is a different rollout (option of no reset)

class HierarchizedMultiPoliEnv(ProxyEnv, Serializable):
    def __init__(
            self,
            env,
            time_steps_agg=1,
            discrete_actions=True,
            pkl_paths=None,
            json_paths=None,
            npz_paths=None,
            animate=False,
            keep_rendered_rgb=False,
    ):
        """
        :param env: Env to wrap, should have same robot characteristics than env where the policies where pretrained on
        :param time_steps_agg: Time-steps during which one of the policies is executed without external action possible
        :param discrete_actions: whether the policy are applied alone or with a linear combination
        :param pkl_paths: list of paths to pickled pre-training experiment that includes the pre-trained policy
        :param json_paths: list of paths to json of the pre-training experiment. Requires npz_paths of the policy params
        :param npz_paths: only required when using json_paths
        :param keep_rendered_rgb: the returned frac_paths include all rgb images (for plotting video after)
        """
        Serializable.quick_init(self, locals())
        ProxyEnv.__init__(self, env)
        self.time_steps_agg = time_steps_agg
        self.discrete_actions = discrete_actions
        self.animate = animate
        self.keep_rendered_rgb = keep_rendered_rgb
        if json_paths:
            self.low_policy_selector_dim = len(json_paths)
        elif pkl_paths:
            self.low_policy_selector_dim = len(pkl_paths)
        else:
            raise Exception("No path no file given")

        self.low_policy = GaussianMLPPolicy_multi_hier(
            env_spec=env.spec,
            env=env,
            pkl_paths=pkl_paths,
            json_paths=json_paths,
            npz_paths=npz_paths,
            trainable_old=False,
            external_selector=True,
        )

    @property
    @overrides
    def action_space(self):
        selector_dim = self.low_policy_selector_dim
        if self.discrete_actions:
            return spaces.Discrete(selector_dim)  # the action is now just a selection
        else:
            ub = 1e6 * np.ones(selector_dim)
            return spaces.Box(-1 * ub, ub)

    @overrides
    def step(self, action):
        action = self.action_space.flatten(action)
        with self.low_policy.fix_selector(action):
            # print("The hier action is prefixed selector: {}".format(self.low_policy.pre_fix_selector))
            if isinstance(self.wrapped_env, FastMazeEnv):
                with self.wrapped_env.blank_maze():
                    frac_path = rollout(self.wrapped_env, self.low_policy, max_path_length=self.time_steps_agg,
                                        reset_start_rollout=False, keep_rendered_rgbs=self.keep_rendered_rgb,
                                        animated=self.animate, speedup=1000)
                next_obs = self.wrapped_env.get_current_obs()
            elif isinstance(self.wrapped_env, NormalizedEnv) and isinstance(self.wrapped_env.wrapped_env, FastMazeEnv):
                with self.wrapped_env.wrapped_env.blank_maze():
                    frac_path = rollout(self.wrapped_env, self.low_policy, max_path_length=self.time_steps_agg,
                                        reset_start_rollout=False, keep_rendered_rgbs=self.keep_rendered_rgb,
                                        animated=self.animate, speedup=1000)
                next_obs = self.wrapped_env.wrapped_env.get_current_obs()
            else:
                frac_path = rollout(self.wrapped_env, self.low_policy, max_path_length=self.time_steps_agg,
                                    reset_start_rollout=False, keep_rendered_rgbs=self.keep_rendered_rgb,
                                    animated=self.animate, speedup=1000)
                next_obs = frac_path['observations'][-1]
            reward = np.sum(frac_path['rewards'])
            terminated = frac_path['terminated'][-1]
            done = self.time_steps_agg > len(
                frac_path['observations']) or terminated  # if the rollout was not maximal it was "done"
            # it would be better to add an extra flagg to this rollout to check if it was done in the last step
            last_agent_info = dict((k, val[-1]) for k, val in frac_path['agent_infos'].items())
            last_env_info = dict((k, val[-1]) for k, val in frac_path['env_infos'].items())
        # print("finished step of {}, with cummulated reward of: {}".format(len(frac_path['observations']), reward))
        if done:
            # if done I need to PAD the tensor so there is no mismatch. Pad with the last elem
            full_path = tensor_utils.pad_tensor_dict(frac_path, self.time_steps_agg, mode='last')
        else:
            full_path = frac_path

        return Step(next_obs, reward, done,
                    last_env_info=last_env_info, last_agent_info=last_agent_info, full_path=full_path)
        # the last kwargs will all go to env_info, so path['env_info']['full_path'] gives a dict with the full path!

    @overrides
    def log_diagnostics(self, paths, *args, **kwargs):
        # to use the visualization I need to append all paths
        expanded_paths = [tensor_utils.flatten_first_axis_tensor_dict(path['env_infos']['full_path']) for path in paths]
        self.wrapped_env.log_diagnostics(expanded_paths, *args, **kwargs)

    def __str__(self):
        return "Hierarchized: %s" % self._wrapped_env

hierarchize_multi = HierarchizedMultiPoliEnv


text:
package com.intellij.ui.popup;

import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupAdapter;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.LightweightWindowEvent;
import com.intellij.ui.ListenerUtil;
import com.intellij.ui.ScreenUtil;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.util.ui.UIUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author max
 */
public class FramelessNotificationPopup {
  private JComponent myContent;
  public static final Dimension myPreferredContentSize = new Dimension(300, 100);
  private JBPopup myPopup;
  private int myTimerTick;
  private Color myBackgroud;
  private final boolean myUseDefaultPreferredSize;
  private final static int FADE_IN_TICKS = 60;
  private final static int SHOW_TIME_TICKS = FADE_IN_TICKS + 300;
  private final static int FADE_OUT_TICKS = SHOW_TIME_TICKS + 60;

  private final ActionListener myFadeTracker = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      Window popupWindow = SwingUtilities.windowForComponent(myContent);
      if (popupWindow != null) {
        myTimerTick++;
        if (myTimerTick < FADE_IN_TICKS) {
          popupWindow.setLocation(popupWindow.getLocation().x,  popupWindow.getLocation().y - 2);
        }
        else if (myTimerTick > FADE_OUT_TICKS) {
          myPopup.cancel();
          myFadeInTimer.stop();
        }
        else if (myTimerTick > SHOW_TIME_TICKS) {
          popupWindow.setLocation(popupWindow.getLocation().x,  popupWindow.getLocation().y + 2);
        }
      }
    }
  };
  private final Timer myFadeInTimer = new Timer(10, myFadeTracker);
  private ActionListener myActionListener;

  public FramelessNotificationPopup(final JComponent owner, final JComponent content, Color backgroud) {
    this(owner, content, backgroud, true, null);
  }

  public FramelessNotificationPopup(final JComponent owner, final JComponent content, Color backgroud, boolean useDefaultPreferredSize, final ActionListener listener) {
    myBackgroud = backgroud;
    myUseDefaultPreferredSize = useDefaultPreferredSize;
    myContent = new ContentComponent(content);

    myActionListener = listener;

    myPopup = JBPopupFactory.getInstance().createComponentPopupBuilder(myContent, null)
      .setForceHeavyweight(true)
      .setRequestFocus(false)
      .setResizable(false)
      .setMovable(true)
      .setLocateWithinScreenBounds(false)
      .setAlpha(0.2f).addListener(new JBPopupAdapter() {
      public void onClosed(LightweightWindowEvent event) {
        if (myFadeInTimer.isRunning()) {
          myFadeInTimer.stop();
        }
        myFadeInTimer.removeActionListener(myFadeTracker);
      }
    })
      .createPopup();
    final Point p = RelativePoint.getSouthEastOf(owner).getScreenPoint();
    Rectangle screen = ScreenUtil.getScreenRectangle(p.x, p.y);

    final Point initial = new Point(screen.x + screen.width - myContent.getPreferredSize().width - 50,
                                    screen.y + screen.height - 5);

    myPopup.showInScreenCoordinates(owner, initial);

    myFadeInTimer.setRepeats(true);
    myFadeInTimer.start();
  }

  public JBPopup getPopup() {
    return myPopup;
  }

  private class ContentComponent extends JPanel {
    private final MouseAdapter myMouseListener;

    public ContentComponent(JComponent content) {
      super(new BorderLayout());
      add(content, BorderLayout.CENTER);
      setBackground(myBackgroud);

      myMouseListener = new MouseAdapter() {
        @Override
        public void mouseClicked(final MouseEvent e) {
          if (e.getButton() == MouseEvent.BUTTON3) {
            myPopup.cancel();
          } else if (UIUtil.isActionClick(e)) {
            if (myActionListener != null) {
              myActionListener.actionPerformed(new ActionEvent(FramelessNotificationPopup.this, ActionEvent.ACTION_PERFORMED, null));
            }
          }
        }

        @Override
        public void mouseEntered(MouseEvent e) {
          if (myFadeInTimer.isRunning()) {
            myFadeInTimer.stop();
          }
        }

        @Override
        public void mouseExited(MouseEvent e) {
          if (!myFadeInTimer.isRunning()) {
            myFadeInTimer.start();
          }
        }
      };
    }

    @Override
    public void addNotify() {
      super.addNotify();
      ListenerUtil.addMouseListener(this, myMouseListener);
    }

    @Override
    public void removeNotify() {
      super.removeNotify();
      ListenerUtil.removeMouseListener(this, myMouseListener);
    }

    public Dimension getPreferredSize() {
      if (myUseDefaultPreferredSize) {
        return myPreferredContentSize;
      }
      return super.getPreferredSize();
    }
  }

}


text:
package org.greencheek.web.filter.memcached;

import com.netflix.hystrix.HystrixCollapser;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.greencheek.web.filter.memcached.client.config.CacheConfigGlobals;
import org.greencheek.web.filter.memcached.domain.CachedResponse;
import org.greencheek.web.filter.memcached.hystrix.commands.BackEndCommand;
import org.greencheek.web.filter.memcached.hystrix.commands.BatchingCacheLookupCommand;
import org.greencheek.web.filter.memcached.hystrix.commands.CacheLookupCommand;
import org.greencheek.web.filter.memcached.hystrix.config.BackendConfig;
import org.greencheek.web.filter.memcached.hystrix.config.BackendConfigBuilder;
import org.greencheek.web.filter.memcached.hystrix.config.CacheLookupConfig;
import org.greencheek.web.filter.memcached.hystrix.config.CacheLookupConfigBuilder;
import org.greencheek.web.filter.memcached.response.BufferedResponseWrapper;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Uses Hystrix  Commands (https://github.com/Netflix/Hystrix), to monitor, and batch, request to memcached
 * Created by dominictootell on 17/05/2014.
 */
public class HystrixPublishToMemcachedFilter extends PublishToMemcachedFilter {

    public static final AtomicInteger filterReferenceCount = new AtomicInteger(0);

    public static final String MEMCACHED_HYSTRIX_CACHE_LOOKUP_EXECUTION_TYPE = "memcached-hystrix-cachelookup-exec-type";
    public static final String MEMCACHED_HYSTRIX_CACHE_LOOKUP_SEMAPHORE_SIZE = "memcached-hystrix-cachelookup-semaphore-size";
    public static final String MEMCACHED_HYSTRIX_CACHE_LOOKUP_TIMEOUT_MILLIS = "memcached-hystrix-cachelookup-timeout-millis";
    public static final String MEMCACHED_HYSTRIX_CACHE_LOOKUP_BATCHING_ENABLED = "memcached-hystrix-cachelookup-batching-enabled";
    public static final String MEMCACHED_HYSTRIX_CACHE_LOOKUP_BATCHING_TIME_MILLIS = "memcached-hystrix-cachelookup-batchingtime-millis";
    public static final String MEMCACHED_HYSTRIX_CACHE_LOOKUP_BATCHING_MAX_SIZE = "memcached-hystrix-cachelookup-batching-maxsize";
    public static final String MEMCACHED_HYSTRIX_CACHE_LOOKUP_THREAD_POOL_SIZE = "memcached-hystrix-cachelookup-threadpool-size";
    public static final String MEMCACHED_HYSTRIX_CACHE_LOOKUP_THREAD_POOL_QUEUESIZE = "memcached-hystrix-cachelookup-threadpool-queuesize";
    public static final String MEMCACHED_HYSTRIX_BACKEND_TRACKING_ENABLED = "memcached-hystrix-backend-tracking-enabled";
    public static final String MEMCACHED_HYSTRIX_BACKEND_SEMAPHORE_SIZE = "memcached-hystrix-backend-semaphore-size";
    public static final String MEMCACHED_HYSTRIX_BACKEND_TIMEOUT_MILLIS = "memcached-hystrix-backend-timeout-millis";

    private CacheLookupConfig cacheLookupConfig;
    private HystrixCommand.Setter cacheLookupSettings;
    private HystrixCommand.Setter backendSettings;
    private HystrixCollapser.Setter batchLookupSettings;
    private BackendConfig backendConfig;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        filterReferenceCount.incrementAndGet();

        CacheLookupConfigBuilder lookupConfigBuilder = new CacheLookupConfigBuilder();
        lookupConfigBuilder.setBatchingTime(filterConfig.getInitParameter(MEMCACHED_HYSTRIX_CACHE_LOOKUP_BATCHING_TIME_MILLIS));
        lookupConfigBuilder.setBatchingMaxSize(filterConfig.getInitParameter(MEMCACHED_HYSTRIX_CACHE_LOOKUP_BATCHING_MAX_SIZE));
        lookupConfigBuilder.useThreadPool(filterConfig.getInitParameter(MEMCACHED_HYSTRIX_CACHE_LOOKUP_EXECUTION_TYPE));
        lookupConfigBuilder.setSemaphoreSize(filterConfig.getInitParameter(MEMCACHED_HYSTRIX_CACHE_LOOKUP_SEMAPHORE_SIZE));
        lookupConfigBuilder.setLookupTimeout(filterConfig.getInitParameter(MEMCACHED_HYSTRIX_CACHE_LOOKUP_TIMEOUT_MILLIS));
        lookupConfigBuilder.setThreadPoolSize(filterConfig.getInitParameter(MEMCACHED_HYSTRIX_CACHE_LOOKUP_THREAD_POOL_SIZE));
        lookupConfigBuilder.setThreadPoolQueueSize(filterConfig.getInitParameter(MEMCACHED_HYSTRIX_CACHE_LOOKUP_THREAD_POOL_QUEUESIZE));
        lookupConfigBuilder.setBatchingEnabled(filterConfig.getInitParameter(MEMCACHED_HYSTRIX_CACHE_LOOKUP_BATCHING_ENABLED));

        BackendConfigBuilder backendConfigBuilder = new BackendConfigBuilder();
        backendConfigBuilder.setBackendHystrixEnabled(CacheConfigGlobals.parseBoolValue(filterConfig.getInitParameter(MEMCACHED_HYSTRIX_BACKEND_TRACKING_ENABLED),true));
        backendConfigBuilder.setSemaphoreSize(filterConfig.getInitParameter(MEMCACHED_HYSTRIX_BACKEND_SEMAPHORE_SIZE));
        backendConfigBuilder.setBackendTimeout(filterConfig.getInitParameter(MEMCACHED_HYSTRIX_BACKEND_TIMEOUT_MILLIS));

        cacheLookupConfig = lookupConfigBuilder.build();
        backendConfig = backendConfigBuilder.build();

        batchLookupSettings = cacheLookupConfig.createCollapserSettings(cacheLookupConfig);
        cacheLookupSettings = cacheLookupConfig.createCacheLookupCommandSettings(cacheLookupConfig);
        backendSettings = BackEndCommand.createBackendSettring(backendConfig);
        super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            super.doFilter(request,response,chain);
        }  finally {
            context.shutdown();
        }
    }

        /**
         * Execute cache lookup
         * @param cacheKey
         */
    @Override
    public CachedResponse executeCacheLookup(String cacheKey) {
        if(cacheLookupConfig.isBatchingEnabled()) {
            return new BatchingCacheLookupCommand(cacheKey, getMemcachedFetchingImpl(), batchLookupSettings, cacheLookupSettings).execute();
        } else {
            return new CacheLookupCommand(cacheLookupSettings,getMemcachedFetchingImpl(),cacheKey).execute();
        }
    }

    /**
     * Post the backend request's completion
     * @param servletRequest
     * @param theResponse
     */
    @Override
    public void postFilter(HttpServletRequest servletRequest,BufferedResponseWrapper theResponse) {
        storeResponseInMemcached(servletRequest, theResponse);
    }

    /**
     * Performs the backend request.
     *
     * @param chain
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    public void doBackEndRequest(FilterChain chain,String cacheKey,HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException
    {
        if(backendConfig.isEnabled()) {
            new BackEndCommand(backendSettings,chain,request,response).execute();
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
        super.destroy();
        shutdown();
    }

    public void shutdown() {
        if(filterReferenceCount.decrementAndGet()==0) {
            com.netflix.hystrix.Hystrix.reset();
        }
    }
}


text:
<!DOCTYPE html>

  <link href="http://gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">

  <meta property="og:title" content="Python Testing" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://dev.im-bot.com/python-testing//" />
<meta property="og:image" content="http://dev.im-bot.com/public/opengraph_logo.png" />
<meta property="og:image:width" content="862" />
<meta property="og:image:height" content="453" />
<meta property="og:description" content="There are difference between 'building it right' and 'building the right thing'. -Python Testing Cookbook by Greg L. TurnquistGood Test Habits  A few test is..." />
<meta property="og:site_name" content="botbotbot" />
<meta property="og:locale" content="th_TH" />

  <title>

      Python Testing · botbotbot

  </title>

  <link rel="stylesheet" href="/public/css/poole.css">
  <link rel="stylesheet" href="/public/css/syntax.css">
  <link rel="stylesheet" href="/public/css/lanyon.css">
  <link rel="stylesheet" href="/public/css/searchbox.css">
  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Serif:400,400italic,700|PT+Sans:400">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">

  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/public/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/public/favicon.ico">

  <link rel="alternate" type="application/rss+xml" title="RSS" href="/feed.xml">

<input type="checkbox" class="sidebar-checkbox" id="sidebar-checkbox">

botbotbot's blog

    Home

          About Me

          Tags

      © 2017. All rights reserved.

            botbotbot
            's blog

  <form action="/search" method="get" id="searchbox">
    <input type="text" id="search-query" placeholder="Search" autocomplete="off" name="q">
  </form>

              Search results

Python Testing

    11 Jul 2014

        Tags:

            python

        ·

There are difference between 'building it right' and 'building the right thing'. -Python Testing Cookbook by Greg L. Turnquist

Good Test Habits

A few test is better than nothing

Coverage isn't everything

make working software not a 100% coverage

Capturing a bug in an automated test

Write Tests for the bug

Add it to your test suite

Fix the bug

Veriry test suite passed

Testing Concept

AAA - Arrange Act Assert

BDD - Give When Then

Tips:

Run All unittest in module:

python -m unittest discover <test_module>

Run nosetests with python3.

Install nose into python3 package with pip3 then you can run nosetests with python3 via nosetests-3.x ex. nosetests-3.4 if your python3 is version 3.4

# ubuntu install python3-pip first
# $ sudo apt-get install python3-ip
$ pip3 install nose
$ nosetests-3.x

Problems using nose in a virtualenv

you can't run nose in system-packages to using packages in virtualenv (sandbox)

Run PDB with nosetest:

from nose.tools import set_trace; set_trace()

Nose Multiprocess

You should make unittest run faster with mocking better than using multi processes.

There are many problem with nose plugin ex. rednose, coverage, xunit.

In worst case more processeses can make your tests slower depends on your tests.

$ nosetests --processes=NUMBER_OF_PROCESSORS --process-timeout <sec>

Plugin Problem:

Rednose - disable rednose by remove –rednose option and NOSE_REDNOSE in enviorment variable

coverage - fix by nose-cov

xunit - fix by nose_xunitmp

Test Coverage

Stmts - statements

Miss - statements that not executed

Cover - (Stmts - Miss)/Stmts * 100

Coverage html

Generate an Pretty HTML report

powerful way to visually inspect the code and see the lines that missing

nosetests --with-coverage --cover-html

Coverage erase

coverage erase
coverage -e
nosetests --with-coverage --cover-erase

Coverage with filter Third-party Libraries out

nosetests --with-coverage --cover-package=<package,package2>
nosetests --with-coverage --cover-package=$(ls | grep '.py$' | sed 's/[.]py$//' | xargs | sed 's/[\ ]/,/g')

Mock

mocking pattern

1.record/replay pattern

  2.action/assert pattern

mock tests behavior

test the method was called or not called by assert_called_with(), etc.

stubs tests state

recording method class and its result

Behavior Driven Development

BDD - Given, When, and Then

BDD Stroy

  -Feature

  – Scenario

  — steps

TDD vs BDD

setUp   ==  Given

call unit ==  Then

assert    ==   When

Lettuce is a Cucumber-like BDD tool built for Python.

Robot Framework - Acceptance Testing

Acceptance testing is block box testing

Customers are usually more interested in what the software does, not how it does it.

Robot Framework uses keywords to define tests, test steps, variables, and other testing components and need to maps Robot Framework keywords to our Python code

Robot Framework uses data-driven test suite

Jekin(Java) - Continuous Integration

nosetests –with-xunit

tests upon commit(trigger),when scheduled

Tools:

Unittests: Nosetests, Rednose, Coverage, nose-progressive

pip install nose rednose coverage
echo "export NOSE_REDNOSE=1" >> .zshrc #enable rednose

Watchdog -
Watching a directory for file changes

pip install watchdog

Tox - multiple environments testing

pip install tox

Testing Python

Python Testing Start Here

Testing in Python: using nose & mocks

Mock - Mocking and Testing Library

An Introduction to Mocking in Python

INTRODUCING BDD

Mock Like a Boss

Beginning Test-Driven Development in Python

Behavior-Driven Development in Python

Testing with mock

Software Testing Course

Patching Mocks in Python

Python Nose: Speed up the runner

Beginning Test-Driven Development in Python

python-test-double

Mocking in Python - How to Bypass Expensive and External Code Within Your Tests

Latest Posts

            JWT
            30 Nov 2016

            Vim Grep then Substitute
            29 Nov 2016

            Code Review
            23 Nov 2016

      Please enable JavaScript to view the comments powered by Disqus.


text:
#ifndef __LINUX_DEBUG_LOCKING_H
#define __LINUX_DEBUG_LOCKING_H
struct task_struct;
#define _RET_IP_ (unsigned long)__builtin_return_address(0)
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
#define DEBUG_LOCKS_WARN_ON(c)  ({   int __ret = 0;     if (unlikely(c)) {   if (debug_locks_off())   WARN_ON(1);   __ret = 1;   }   __ret;  })
#define SMP_DEBUG_LOCKS_WARN_ON(c) do { } while (0)
#define locking_selftest() do { } while (0)
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#endif


text:
require 'intercom/traits/api_resource'

module Intercom
  class Collection
    include Traits::ApiResource
  end
end


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>Y!LDA: Member List</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>

Main Page

Related Pages

Namespaces

Classes

Files

Class List

Class Hierarchy

Class Members

Filter_Sampler Member List
This is the complete list of members for Filter_Sampler, including all inherited members.

Filter_Sampler(Model_Refiner &)	Filter_Sampler

operator()(void *)	Filter_Sampler

~Filter_Sampler()	Filter_Sampler	 [virtual]

<address style="text-align: right;">Generated on Tue Jul 19 11:45:26 2011 for Y!LDA by

 1.6.3 </address>


text:
define(
    'model/article',
    [
        'backbone',
    ],
    function (Backbone) {
        'use strict';

        var ArticleModel = Backbone.Model.extend({
            getTitle: function() {
                return this.get('title');
            },

            getContent: function() {
                return this.get('content');
            }
        });

        return ArticleModel;
    }
);


text:
"""
    DocuSign REST API

    The DocuSign REST API provides you with a powerful, convenient, and simple Web services API for interacting with DocuSign.  # noqa: E501

    OpenAPI spec version: v2.1
    Contact: devcenter@docusign.com
    Generated by: https://github.com/swagger-api/swagger-codegen.git
"""

import pprint
import re  # noqa: F401

import six

class DisplayApplianceInfo(object):
    """NOTE: This class is auto generated by the swagger code generator program.

    Do not edit the class manually.
    """

    """
    Attributes:
      swagger_types (dict): The key is attribute name
                            and the value is attribute type.
      attribute_map (dict): The key is attribute name
                            and the value is json key in definition.
    """
    swagger_types = {
    }

    attribute_map = {
    }

    def __init__(self):  # noqa: E501
        """DisplayApplianceInfo - a model defined in Swagger"""  # noqa: E501
        self.discriminator = None

    def to_dict(self):
        """Returns the model properties as a dict"""
        result = {}

        for attr, _ in six.iteritems(self.swagger_types):
            value = getattr(self, attr)
            if isinstance(value, list):
                result[attr] = list(map(
                    lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
                    value
                ))
            elif hasattr(value, "to_dict"):
                result[attr] = value.to_dict()
            elif isinstance(value, dict):
                result[attr] = dict(map(
                    lambda item: (item[0], item[1].to_dict())
                    if hasattr(item[1], "to_dict") else item,
                    value.items()
                ))
            else:
                result[attr] = value
        if issubclass(DisplayApplianceInfo, dict):
            for key, value in self.items():
                result[key] = value

        return result

    def to_str(self):
        """Returns the string representation of the model"""
        return pprint.pformat(self.to_dict())

    def __repr__(self):
        """For `print` and `pprint`"""
        return self.to_str()

    def __eq__(self, other):
        """Returns true if both objects are equal"""
        if not isinstance(other, DisplayApplianceInfo):
            return False

        return self.__dict__ == other.__dict__

    def __ne__(self, other):
        """Returns true if both objects are not equal"""
        return not self == other


text:
Prebid.js
========

> Setup and manage header bidding advertising partners without writing code or confusing line items. Prebid.js is open source and free.

Many SSPs, bidders, and publishers have all contributed to this project.

Check out the overview and documentation at http://prebid.org.

No more week-long development. Header bidding is made easy by prebid.js :)

**Table of Contents**

- [Prebid.js](#)
	- [Usage](#usage)
		- [Example code](#example-code)
	- [Contribute](#contribute)
		- [Add an Bidder Adapter](#add-an-bidder-adapter)
		- [install](#install)
		- [Build](#build)
		- [Configure](#configure)
		- [Run](#run)

Usage
----------
Download the integration example [here](https://github.com/prebid/Prebid.js/blob/master/integrationExamples/gpt/pbjs_example_gpt.html).

### Example code ###

**Include the prebid.js libraray**
Note that you need to host `prebid.js` locally or on a CDN and update the reference in the code snippet below for `cdn.host.com/prebid.min.js
```javascript
(function() {
        var d = document, pbs = d.createElement('script'), pro = d.location.protocol;
        pbs.type = 'text/javascript';
        pbs.src = ((pro === 'https:') ? 'https' : 'http') + '://cdn.host.com/prebid.min.js';
        var target = document.getElementsByTagName('head')[0];
        target.insertBefore(pbs, target.firstChild);
})();
```

**Setup ad units**
```javascript
pbjs.que.push(function(){
	var adUnits = [{
        code: '{id}',
        sizes: [[300, 250], [300, 600]],
        bids: [
            {
                bidder: 'appnexus',
                params: {
                    placementId: '{id}'
                }
            }
        ]
    }];
	//add the adUnits
    pbjs.addAdUnits(adUnits);
});
```

**Request Bids**
```javascript
pbjs.que.push(function(){
    pbjs.requestBids({
        bidsBackHandler: function(bidResponses) {
            //do stuff when the bids are back
        }
    })
});
```

**See Console Debug Errors during testing**
By default console errors are supressed. To enabled add `?pbjs_debug=true` to the end of the URL for testing.

Contribute
----------

### Add an Bidder Adapter ###
Follow the [guide outlined here](http://prebid.org/bidder-adaptor.html) to add an adapter.

### install ###
	$ sudo npm install

### Build ###
	$ gulp build

### Configure ###
Edit `./integrationExamples/gpt/pbjs_example_gpt.html`

Change `{id}` values appropriately

### Run ###

	$ gulp serve

Navigate to http://localhost:9999/integrationExamples/gpt/pbjs_example_gpt.html to run the example file

### Unit Test In the Browser ###

Navigate to http://localhost:9999/test/spec/runner.html to run the test file.


text:
from webob import exc
from functools import wraps

from heat.common import identifier

def tenant_local(handler):
    '''
    Decorator for a handler method that sets the correct tenant_id in the
    request context.
    '''
    @wraps(handler)
    def handle_stack_method(controller, req, tenant_id, **kwargs):
        req.context.tenant_id = tenant_id
        return handler(controller, req, **kwargs)

    return handle_stack_method

def identified_stack(handler):
    '''
    Decorator for a handler method that passes a stack identifier in place of
    the various path components.
    '''
    @tenant_local
    @wraps(handler)
    def handle_stack_method(controller, req, stack_name, stack_id, **kwargs):
        stack_identity = identifier.HeatIdentifier(req.context.tenant_id,
                                                   stack_name,
                                                   stack_id)
        return handler(controller, req, dict(stack_identity), **kwargs)

    return handle_stack_method

def identified_resource(handler):
    '''
    Decorator for a handler method that passes a resource identifier in place
    of the various path components.
    '''
    @identified_stack
    @wraps(handler)
    def handle_stack_method(controller, stack_identity,
                            resource_name, **kwargs):
        resource_identity = identifier.ResourceIdentifier(stack_identity,
                                                          resource_name)
        return handler(controller, req, dict(resource_identity), **kwargs)

    return handle_stack_method

def make_url(req, identity):
    '''Return the URL for the supplied identity dictionary.'''
    try:
        stack_identity = identifier.HeatIdentifier(**identity)
    except ValueError:
        err_reason = _('Invalid Stack address')
        raise exc.HTTPInternalServerError(err_reason)

    return req.relative_url(stack_identity.url_path(), True)

def make_link(req, identity, relationship='self'):
    '''Return a link structure for the supplied identity dictionary.'''
    return {'href': make_url(req, identity), 'rel': relationship}

def remote_error(ex):
    """
    Map rpc_common.RemoteError exceptions returned by the engine
    to webob exceptions which can be used to return
    properly formatted error responses.
    """

    error_map = {
        'AttributeError': exc.HTTPBadRequest,
        'ValueError': exc.HTTPBadRequest,
        'StackNotFound': exc.HTTPNotFound,
        'ResourceNotFound': exc.HTTPNotFound,
        'ResourceNotAvailable': exc.HTTPNotFound,
        'PhysicalResourceNotFound': exc.HTTPNotFound,
        'InvalidTenant': exc.HTTPForbidden,
        'StackExists': exc.HTTPConflict,
        'StackValidationFailed': exc.HTTPBadRequest,
    }

    Exc = error_map.get(ex.exc_type, exc.HTTPInternalServerError)

    raise Exc(str(ex))


text:
CKEDITOR.plugins.setLang( 'toolbar', 'pt', {
	toolbarCollapse: 'Colapsar Barra',
	toolbarExpand: 'Expandir Barra',
	toolbarGroups: {
		document: 'Document',
		clipboard: 'Clipboard/Undo',
		editing: 'Editing',
		forms: 'Forms',
		basicstyles: 'Basic Styles',
		paragraph: 'Paragraph',
		links: 'Links',
		insert: 'Insert',
		styles: 'Styles',
		colors: 'Colors',
		tools: 'Tools'
	},
	toolbars: 'Editor de barras de ferramentas'
});