text:
import sys
import time
import argparse
import gzip
import cPickle as pickle

from prettytable import PrettyTable
import numpy as np
import theano
import theano.tensor as T

from utils import load_embedding_iterator
from nn import get_activation_by_name, create_optimization_updates
from nn import Layer, EmbeddingLayer, LSTM, GRU, RCNN, Dropout, apply_dropout
from nn.evaluation import evaluate_average

import myio
from myio import say
from evaluation import Evaluation

class Model:
    def __init__(self, args, embedding_layer, weights=None):
        self.args = args
        self.embedding_layer = embedding_layer
        self.padding_id = embedding_layer.vocab_map["<padding>"]
        self.bos_id = embedding_layer.vocab_map["<s>"]
        self.eos_id = embedding_layer.vocab_map["</s>"]
        self.weights = weights

    def ready(self):
        args = self.args
        weights = self.weights

        # len(source) * batch
        idxs = self.idxs = T.imatrix()

        # len(target) * batch
        idys = self.idys = T.imatrix()
        idts = idys[:-1]
        idgs = idys[1:]

        dropout = self.dropout = theano.shared(np.float64(args.dropout).astype(
                            theano.config.floatX))

        embedding_layer = self.embedding_layer

        activation = get_activation_by_name(args.activation)
        n_d = self.n_d = args.hidden_dim
        n_e = self.n_e = embedding_layer.n_d
        n_V = self.n_V = embedding_layer.n_V

        if args.layer.lower() == "rcnn":
            LayerType = RCNN
        elif args.layer.lower() == "lstm":
            LayerType = LSTM
        elif args.layer.lower() == "gru":
            LayerType = GRU

        depth = self.depth = args.depth
        layers = self.layers = [ ]
        for i in range(depth*2):
            if LayerType != RCNN:
                feature_layer = LayerType(
                        n_in = n_e if i/2 == 0 else n_d,
                        n_out = n_d,
                        activation = activation
                    )
            else:
                feature_layer = LayerType(
                        n_in = n_e if i/2 == 0 else n_d,
                        n_out = n_d,
                        activation = activation,
                        order = args.order,
                        mode = args.mode,
                        has_outgate = args.outgate
                    )
            layers.append(feature_layer)

        self.output_layer = output_layer = Layer(
                n_in = n_d,
                n_out = n_V,
                activation = T.nnet.softmax,
            )

        # feature computation starts here

        # (len*batch)*n_e
        xs_flat = embedding_layer.forward(idxs.ravel())
        xs_flat = apply_dropout(xs_flat, dropout)
        if weights is not None:
            xs_w = weights[idxs.ravel()].dimshuffle((0,'x'))
            xs_flat = xs_flat * xs_w
        # len*batch*n_e
        xs = xs_flat.reshape((idxs.shape[0], idxs.shape[1], n_e))

        # (len*batch)*n_e
        xt_flat = embedding_layer.forward(idts.ravel())
        xt_flat = apply_dropout(xt_flat, dropout)
        if weights is not None:
            xt_w = weights[idts.ravel()].dimshuffle((0,'x'))
            xt_flat = xt_flat * xt_w
        # len*batch*n_e
        xt = xt_flat.reshape((idts.shape[0], idts.shape[1], n_e))

        prev_hs = xs
        prev_ht = xt
        for i in range(depth):
            # len*batch*n_d
            hs = layers[i*2].forward_all(prev_hs, return_c=True)
            ht = layers[i*2+1].forward_all(prev_ht, hs[-1])
            hs = hs[:,:,-n_d:]
            ht = ht[:,:,-n_d:]
            prev_hs = hs
            prev_ht = ht
            prev_hs = apply_dropout(hs, dropout)
            prev_ht = apply_dropout(ht, dropout)

        self.p_y_given_x = output_layer.forward(prev_ht.reshape(
                                (xt_flat.shape[0], n_d)
                            ))

        h_final = hs[-1]
        self.scores2 = -(h_final[1:]-h_final[0]).norm(2,axis=1)
        h_final = self.normalize_2d(h_final)
        self.scores = T.dot(h_final[1:], h_final[0])

        # (len*batch)
        nll = T.nnet.categorical_crossentropy(
                        self.p_y_given_x,
                        idgs.ravel()
                    )
        nll = nll.reshape(idgs.shape)
        self.nll = nll
        self.mask = mask = T.cast(T.neq(idgs, self.padding_id), theano.config.floatX)
        nll = T.sum(nll*mask, axis=0)

        #layers.append(embedding_layer)
        layers.append(output_layer)
        params = [ ]
        for l in self.layers:
            params += l.params
        self.params = params
        say("num of parameters: {}\n".format(
            sum(len(x.get_value(borrow=True).ravel()) for x in params)
        ))

        l2_reg = None
        for p in params:
            if l2_reg is None:
                l2_reg = p.norm(2)
            else:
                l2_reg = l2_reg + p.norm(2)
        l2_reg = l2_reg * args.l2_reg
        self.loss = T.mean(nll)
        self.cost = self.loss + l2_reg

    def train(self, ids_corpus, train, dev=None, test=None, heldout=None):
        args = self.args
        dropout_prob = np.float64(args.dropout).astype(theano.config.floatX)
        batch_size = args.batch_size
        padding_id = self.padding_id
        bos_id = self.bos_id
        eos_id = self.eos_id

        #train_batches = myio.create_batches(ids_corpus, train, batch_size, padding_id, args.loss)

        updates, lr, gnorm = create_optimization_updates(
                cost = self.cost,
                params = self.params,
                lr = args.learning_rate,
                method = args.learning
            )[:3]

        train_func = theano.function(
                inputs = [ self.idxs, self.idys ],
                outputs = [ self.cost, self.loss, gnorm ],
                updates = updates
            )

        eval_func = theano.function(
                inputs = [ self.idxs ],
                #outputs = self.scores2
                outputs = self.scores
            )

        nll_func = theano.function(
                inputs = [ self.idxs, self.idys ],
                outputs = [ self.nll, self.mask ]
            )

        say("\tp_norm: {}\n".format(
                self.get_pnorm_stat()
            ))

        result_table = PrettyTable(["Epoch", "dev MAP", "dev MRR", "dev P@1", "dev P@5"] +
                                    ["tst MAP", "tst MRR", "tst P@1", "tst P@5"])

        unchanged = 0
        best_dev = -1
        dev_MAP = dev_MRR = dev_P1 = dev_P5 = 0
        test_MAP = test_MRR = test_P1 = test_P5 = 0
        heldout_PPL = -1

        start_time = 0
        max_epoch = args.max_epoch
        for epoch in xrange(max_epoch):
            unchanged += 1
            if unchanged > 8: break

            start_time = time.time()

            train_batches = myio.create_batches(ids_corpus, train, batch_size,
                                    padding_id, bos_id, eos_id, auto_encode=True)
            N =len(train_batches)

            train_cost = 0.0
            train_loss = 0.0
            train_loss2 = 0.0
            for i in xrange(N):
                # get current batch
                t1, b1, t2 = train_batches[i]

                if args.use_title:
                    idxs, idys = myio.create_one_batch(t1, t2, padding_id)
                    cur_cost, cur_loss, grad_norm = train_func(idxs, idys)
                    train_cost += cur_cost
                    train_loss += cur_loss
                    train_loss2 += cur_loss / idys.shape[0]

                if args.use_body:
                    idxs, idys = myio.create_one_batch(b1, t2, padding_id)
                    cur_cost, cur_loss, grad_norm = train_func(idxs, idys)
                    train_cost += cur_cost
                    train_loss += cur_loss
                    train_loss2 += cur_loss / idys.shape[0]

                if i % 10 == 0:
                    say("\r{}/{}".format(i,N))

                if i == N-1:
                    self.dropout.set_value(0.0)

                    if dev is not None:
                        dev_MAP, dev_MRR, dev_P1, dev_P5 = self.evaluate(dev, eval_func)
                    if test is not None:
                        test_MAP, test_MRR, test_P1, test_P5 = self.evaluate(test, eval_func)
                    if heldout is not None:
                        heldout_PPL = self.evaluate_perplexity(heldout, nll_func)

                    if dev_MRR > best_dev:
                        unchanged = 0
                        best_dev = dev_MRR
                        result_table.add_row(
                            [ epoch ] +
                            [ "%.2f" % x for x in [ dev_MAP, dev_MRR, dev_P1, dev_P5 ] +
                                        [ test_MAP, test_MRR, test_P1, test_P5 ] ]
                        )
                        if args.model:
                            self.save_model(args.model+".pkl.gz")

                    dropout_p = np.float64(args.dropout).astype(
                                theano.config.floatX)
                    self.dropout.set_value(dropout_p)

                    say("\r\n\n")
                    say( ( "Epoch {}\tcost={:.3f}\tloss={:.3f} {:.3f}\t" \
                        +"\tMRR={:.2f},{:.2f}\tPPL={:.1f}\t|g|={:.3f}\t[{:.3f}m]\n" ).format(
                            epoch,
                            train_cost / (i+1),
                            train_loss / (i+1),
                            train_loss2 / (i+1),
                            dev_MRR,
                            best_dev,
                            heldout_PPL,
                            float(grad_norm),
                            (time.time()-start_time)/60.0
                    ))
                    say("\tp_norm: {}\n".format(
                            self.get_pnorm_stat()
                        ))

                    say("\n")
                    say("{}".format(result_table))
                    say("\n")

    def get_pnorm_stat(self):
        lst_norms = [ ]
        for p in self.params:
            vals = p.get_value(borrow=True)
            l2 = np.linalg.norm(vals)
            lst_norms.append("{:.3f}".format(l2))
        return lst_norms

    def normalize_2d(self, x, eps=1e-8):
        # x is batch*d
        # l2 is batch*1
        l2 = x.norm(2,axis=1).dimshuffle((0,'x'))
        return x/(l2+eps)

    def normalize_3d(self, x, eps=1e-8):
        # x is len*batch*d
        # l2 is len*batch*1
        l2 = x.norm(2,axis=2).dimshuffle((0,1,'x'))
        return x/(l2+eps)

    def average_without_padding(self, x, ids, eps=1e-8):
        # len*batch*1
        mask = T.neq(ids, self.padding_id).dimshuffle((0,1,'x'))
        mask = T.cast(mask, theano.config.floatX)
        # batch*d
        s = T.sum(x*mask,axis=0) / (T.sum(mask,axis=0)+eps)
        return s

    def evaluate(self, data, eval_func):
        res = [ ]
        for t, b, labels in data:
            idts, idbs = myio.create_one_batch(t, b, self.padding_id)
            scores = eval_func(idts)
            #assert len(scores) == len(labels)
            ranks = (-scores).argsort()
            ranked_labels = labels[ranks]
            res.append(ranked_labels)
        e = Evaluation(res)
        MAP = e.MAP()*100
        MRR = e.MRR()*100
        P1 = e.Precision(1)*100
        P5 = e.Precision(5)*100
        return MAP, MRR, P1, P5

    def evaluate_perplexity(self, data, nll_func):
        nll_preds = [ ]
        nll_masks = [ ]
        for idbs, idts in data:
            nll, mask = nll_func(idbs, idts)
            assert nll.shape == mask.shape
            nll_preds.append(nll)
            nll_masks.append(mask)
        avg_nll = evaluate_average(
                    predictions = nll_preds,
                    masks = nll_masks
                )
        return np.exp(avg_nll)

    def save_model(self, path):
        args = self.args
        lst_params = [ ]
        for i in range(args.depth):
            lst_params.append(self.layers[i*2].params)
        with gzip.open(path,"w") as fout:
            pickle.dump(
                    { "d": args.hidden_dim,
                      "layer_type": args.layer,
                      "args": args,
                      "params": lst_params },
                    fout,
                    protocol = pickle.HIGHEST_PROTOCOL
                )
        say(" \tmodel saved.\n")

def main(args):
    raw_corpus = myio.read_corpus(args.corpus)
    embedding_layer = myio.create_embedding_layer(
                raw_corpus,
                n_d = args.hidden_dim,
                cut_off = args.cut_off,
                embs = load_embedding_iterator(args.embeddings) if args.embeddings else None
            )
    ids_corpus = myio.map_corpus(raw_corpus, embedding_layer)
    say("vocab size={}, corpus size={}\n".format(
            embedding_layer.n_V,
            len(raw_corpus)
        ))
    padding_id = embedding_layer.vocab_map["<padding>"]
    bos_id = embedding_layer.vocab_map["<s>"]
    eos_id = embedding_layer.vocab_map["</s>"]

    if args.reweight:
        weights = myio.create_idf_weights(args.corpus, embedding_layer)

    if args.dev:
        dev = myio.read_annotations(args.dev, K_neg=20, prune_pos_cnt=-1)
        dev = myio.create_eval_batches(ids_corpus, dev, padding_id)
    if args.test:
        test = myio.read_annotations(args.test, K_neg=20, prune_pos_cnt=-1)
        test = myio.create_eval_batches(ids_corpus, test, padding_id)

    if args.heldout:
        with open(args.heldout) as fin:
            heldout_ids = fin.read().split()
        heldout_corpus = dict((id, ids_corpus[id]) for id in heldout_ids if id in ids_corpus)
        train_corpus = dict((id, ids_corpus[id]) for id in ids_corpus
                                                if id not in heldout_corpus)
        heldout = myio.create_batches(heldout_corpus, [ ], args.batch_size,
                    padding_id, bos_id, eos_id, auto_encode=True)
        heldout = [ myio.create_one_batch(b1, t2, padding_id) for t1, b1, t2 in heldout ]
        say("heldout examples={}\n".format(len(heldout_corpus)))

    if args.train:
        model = Model(args, embedding_layer,
                      weights=weights if args.reweight else None)

        start_time = time.time()
        train = myio.read_annotations(args.train)
        if not args.use_anno: train = [ ]
        train_batches = myio.create_batches(ids_corpus, train, args.batch_size,
                    model.padding_id, model.bos_id, model.eos_id, auto_encode=True)
        say("{} to create batches\n".format(time.time()-start_time))

        model.ready()
        model.train(
                ids_corpus if not args.heldout else train_corpus,
                train,
                dev if args.dev else None,
                test if args.test else None,
                heldout if args.heldout else None
            )

if __name__ == "__main__":
    argparser = argparse.ArgumentParser(sys.argv[0])
    argparser.add_argument("--corpus",
            type = str
        )
    argparser.add_argument("--train",
            type = str,
            default = ""
        )
    argparser.add_argument("--test",
            type = str,
            default = ""
        )
    argparser.add_argument("--dev",
            type = str,
            default = ""
        )
    argparser.add_argument("--heldout",
            type = str,
            default = ""
        )
    argparser.add_argument("--embeddings",
            type = str,
            default = ""
        )
    argparser.add_argument("--hidden_dim", "-d",
            type = int,
            default = 200
        )
    argparser.add_argument("--learning",
            type = str,
            default = "adam"
        )
    argparser.add_argument("--learning_rate",
            type = float,
            default = 0.001
        )
    argparser.add_argument("--l2_reg",
            type = float,
            default = 0.00001
        )
    argparser.add_argument("--activation", "-act",
            type = str,
            default = "tanh"
        )
    argparser.add_argument("--batch_size",
            type = int,
            default = 256
        )
    argparser.add_argument("--depth",
            type = int,
            default = 1
        )
    argparser.add_argument("--dropout",
            type = float,
            default = 0.0
        )
    argparser.add_argument("--max_epoch",
            type = int,
            default = 50
        )
    argparser.add_argument("--cut_off",
            type = int,
            default = 1
        )
    argparser.add_argument("--max_seq_len",
            type = int,
            default = 100
        )
    argparser.add_argument("--normalize",
            type = int,
            default = 1
        )
    argparser.add_argument("--reweight",
            type = int,
            default = 1
        )
    argparser.add_argument("--order",
            type = int,
            default = 2
        )
    argparser.add_argument("--layer",
            type = str,
            default = "rcnn"
        )
    argparser.add_argument("--mode",
            type = int,
            default = 1
        )
    argparser.add_argument("--outgate",
            type = int,
            default = 0
        )
    argparser.add_argument("--model",
            type = str,
            default = ""
        )
    argparser.add_argument("--use_title",
            type = int,
            default = 1
        )
    argparser.add_argument("--use_body",
            type = int,
            default = 1
        )
    argparser.add_argument("--use_anno",
            type = int,
            default = 1
        )
    args = argparser.parse_args()
    assert args.use_title or args.use_body
    print args
    print ""
    main(args)


text:
using System.Collections.Generic;
using System.Text.Json;
using Azure.Core;

namespace Azure.ResourceManager.SecurityInsights.Models
{
    public partial class MetadataDependencies : IUtf8JsonSerializable
    {
        void IUtf8JsonSerializable.Write(Utf8JsonWriter writer)
        {
            writer.WriteStartObject();
            if (Optional.IsDefined(ContentId))
            {
                writer.WritePropertyName("contentId");
                writer.WriteStringValue(ContentId);
            }
            if (Optional.IsDefined(Kind))
            {
                writer.WritePropertyName("kind");
                writer.WriteStringValue(Kind.Value.ToString());
            }
            if (Optional.IsDefined(Version))
            {
                writer.WritePropertyName("version");
                writer.WriteStringValue(Version);
            }
            if (Optional.IsDefined(Name))
            {
                writer.WritePropertyName("name");
                writer.WriteStringValue(Name);
            }
            if (Optional.IsDefined(Operator))
            {
                writer.WritePropertyName("operator");
                writer.WriteStringValue(Operator.Value.ToString());
            }
            if (Optional.IsCollectionDefined(Criteria))
            {
                writer.WritePropertyName("criteria");
                writer.WriteStartArray();
                foreach (var item in Criteria)
                {
                    writer.WriteObjectValue(item);
                }
                writer.WriteEndArray();
            }
            writer.WriteEndObject();
        }

        internal static MetadataDependencies DeserializeMetadataDependencies(JsonElement element)
        {
            Optional<string> contentId = default;
            Optional<SecurityInsightsKind> kind = default;
            Optional<string> version = default;
            Optional<string> name = default;
            Optional<Operator> @operator = default;
            Optional<IList<MetadataDependencies>> criteria = default;
            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("contentId"))
                {
                    contentId = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("kind"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    kind = new SecurityInsightsKind(property.Value.GetString());
                    continue;
                }
                if (property.NameEquals("version"))
                {
                    version = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("name"))
                {
                    name = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("operator"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    @operator = new Operator(property.Value.GetString());
                    continue;
                }
                if (property.NameEquals("criteria"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    List<MetadataDependencies> array = new List<MetadataDependencies>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        array.Add(DeserializeMetadataDependencies(item));
                    }
                    criteria = array;
                    continue;
                }
            }
            return new MetadataDependencies(contentId.Value, Optional.ToNullable(kind), version.Value, name.Value, Optional.ToNullable(@operator), Optional.ToList(criteria));
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
{-# LANGUAGE DataKinds         #-}
{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds         #-}
{-# LANGUAGE TypeFamilies      #-}
{-# LANGUAGE TypeOperators     #-}

import           Control.Monad.IO.Class            (liftIO)
import           Data.Aeson
import           Data.Bool
import           Data.Char                         (toUpper)
import qualified Data.List                         as L
import           Data.Monoid
import           Data.Proxy
import           Data.Text                         hiding (head, length, map,
                                                    null, toUpper)
import qualified Data.Text                         as T
import qualified Data.Text.IO                      as T
import           GHC.Generics
import           Servant.Server.Internal.SnapShims
import           Snap.Core
import           Snap.Http.Server

import           Servant
import           Servant.Server
-- import           Snap.Util.FileServe
import           API
import           Snap

-- * Example

-- | A greet message data type
newtype Greet = Greet { _msg :: Text }
  deriving (Generic, Show)

instance FromJSON Greet
instance ToJSON Greet

testApi :: Proxy API
testApi = Proxy

data App = App

-- Server-side handlers.
--
-- There's one handler per endpoint, which, just like in the type
-- that represents the API, are glued together using :<|>.
--
-- Each handler runs in the 'ExceptT ServantErr IO' monad.
server :: Server API (Handler App App)
server = return () :<|> return 100 :<|> sayhi :<|> dbl :<|> multi :<|> qna :<|> serveDirectory "static"
  where sayhi :: Maybe Text -> [Text] -> Bool -> Handler App App Text
        sayhi nm greetings withGusto = case nm of
          Nothing -> return ("Sorry, who are you?" :: Text)
          Just n  -> do
           let modifier  = bool id T.toUpper withGusto
               greetPart
                 | null greetings        = "Hi, "
                 | length greetings == 1 = L.head greetings <> ", "
                 | otherwise             = T.intercalate ", " (L.init greetings)
                                       <> ", and " <> L.last greetings <> ", "
           return . modifier $ greetPart <> n
        dbl x = if x `elem` [4,13]
                then throwError $ err500 { errBody = "No unlucky numbers please" }
                else return $ x * 2
        multi = return . bool "Box unchecked" "Box Checked"
        qna q = do
          liftIO $ do
            putStrLn $ "qna got: " ++ show q
            T.putStrLn $ unQuestion q
          return $ Answer $ unQuestion q

-- Turn the server into a WAI app. 'serve' is provided by servant,
-- more precisely by the Servant.Server module.
test :: Handler App App ()
test = serveSnap testApi server

initApp :: SnapletInit App App
initApp = makeSnaplet "myapp" "example" Nothing $ do
  addRoutes [("", test)
            ,("", serveDirectory "static")
            ]
  return App

-- Put this all to work!
main :: IO ()
main = serveSnaplet mempty initApp


text:
using DevToolsProtocolTest = DevToolsProtocolTestBase;
using testing::AllOf;
using testing::Contains;
using testing::Eq;
using testing::Not;

namespace {

IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest,
                       VisibleSecurityStateChangedNeutralState) {
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GURL("about:blank")));
  EXPECT_TRUE(content::WaitForLoadStop(web_contents()));

  Attach();
  SendCommandAsync("Security.enable");
  base::Value::Dict params =
      WaitForNotification("Security.visibleSecurityStateChanged", true);

  std::string* security_state =
      params.FindStringByDottedPath("visibleSecurityState.securityState");
  ASSERT_TRUE(security_state);
  EXPECT_EQ(std::string("neutral"), *security_state);
  EXPECT_FALSE(params.FindStringByDottedPath(
      "visibleSecurityState.certificateSecurityState"));
  EXPECT_FALSE(
      params.FindStringByDottedPath("visibleSecurityState.safetyTipInfo"));
  const base::Value* security_state_issue_ids =
      params.FindByDottedPath("visibleSecurityState.securityStateIssueIds");
  EXPECT_TRUE(std::find(security_state_issue_ids->GetListDeprecated().begin(),
                        security_state_issue_ids->GetListDeprecated().end(),
                        base::Value("scheme-is-not-cryptographic")) !=
              security_state_issue_ids->GetListDeprecated().end());
}

IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest, CreateDeleteContext) {
  AttachToBrowserTarget();
  for (int i = 0; i < 2; i++) {
    const base::Value::Dict* result =
        SendCommandSync("Target.createBrowserContext");
    std::string context_id = *result->FindString("browserContextId");

    base::Value::Dict params;
    params.Set("url", "about:blank");
    params.Set("browserContextId", context_id);
    SendCommandSync("Target.createTarget", std::move(params));

    params = base::Value::Dict();
    params.Set("browserContextId", context_id);
    SendCommandSync("Target.disposeBrowserContext", std::move(params));
  }
}

IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest,
                       NewTabPageInCreatedContextDoesNotCrash) {
  AttachToBrowserTarget();
  const base::Value::Dict* result =
      SendCommandSync("Target.createBrowserContext");
  std::string context_id = *result->FindString("browserContextId");

  base::Value::Dict params;
  params.Set("url", chrome::kChromeUINewTabURL);
  params.Set("browserContextId", context_id);
  content::WebContentsAddedObserver observer;
  SendCommandSync("Target.createTarget", std::move(params));
  content::WebContents* wc = observer.GetWebContents();
  ASSERT_TRUE(content::WaitForLoadStop(wc));
  EXPECT_EQ(chrome::kChromeUINewTabURL, wc->GetLastCommittedURL().spec());

  // Should not crash by this point.
}

IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest,
                       InputDispatchEventsToCorrectTarget) {
  Attach();

  std::string setup_logging = R"(
      window.logs = [];
      ['dragenter', 'keydown', 'mousedown', 'mouseenter', 'mouseleave',
       'mousemove', 'mouseout', 'mouseover', 'mouseup', 'click', 'touchcancel',
       'touchend', 'touchmove', 'touchstart',
      ].forEach((event) =>
        window.addEventListener(event, (e) => logs.push(e.type)));)";
  content::WebContents* target_web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();

  ui_test_utils::NavigateToURLWithDisposition(
      browser(), GURL("about:blank"), WindowOpenDisposition::NEW_FOREGROUND_TAB,
      ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
  content::WebContents* other_web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  EXPECT_TRUE(
      content::EvalJs(target_web_contents, setup_logging).error.empty());
  EXPECT_TRUE(content::EvalJs(other_web_contents, setup_logging).error.empty());

  base::Value::Dict params;
  params.Set("button", "left");
  params.Set("clickCount", 1);
  params.Set("x", 100);
  params.Set("y", 250);
  params.Set("clickCount", 1);

  params.Set("type", "mousePressed");
  SendCommandSync("Input.dispatchMouseEvent", params.Clone());

  params.Set("type", "mouseMoved");
  params.Set("y", 270);
  SendCommandSync("Input.dispatchMouseEvent", params.Clone());

  params.Set("type", "mouseReleased");
  SendCommandSync("Input.dispatchMouseEvent", std::move(params));

  params = base::Value::Dict();
  params.Set("x", 100);
  params.Set("y", 250);
  params.Set("type", "dragEnter");
  params.SetByDottedPath("data.dragOperationsMask", 1);
  params.SetByDottedPath("data.items", base::Value::List());
  SendCommandSync("Input.dispatchDragEvent", std::move(params));

  params = base::Value::Dict();
  params.Set("x", 100);
  params.Set("y", 250);
  SendCommandSync("Input.synthesizeTapGesture", std::move(params));

  params = base::Value::Dict();
  params.Set("type", "keyDown");
  params.Set("key", "a");
  SendCommandSync("Input.dispatchKeyEvent", std::move(params));

  content::EvalJsResult main_target_events =
      content::EvalJs(target_web_contents, "logs.join(' ')");
  content::EvalJsResult other_target_events =
      content::EvalJs(other_web_contents, "logs.join(' ')");
  // mouse events might happen in the other_target if the real mouse pointer
  // happens to be over the browser window
  EXPECT_THAT(
      base::SplitString(main_target_events.ExtractString(), " ",
                        base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL),
      AllOf(Contains("mouseover"), Contains("mousedown"), Contains("mousemove"),
            Contains("mouseup"), Contains("click"), Contains("dragenter"),
            Contains("keydown")));
  EXPECT_THAT(base::SplitString(other_target_events.ExtractString(), " ",
                                base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL),
              AllOf(Not(Contains("click")), Not(Contains("dragenter")),
                    Not(Contains("keydown"))));
}

IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest,
                       NoInputEventsSentToBrowserWhenDisallowed) {
  SetIsTrusted(false);
  Attach();

  base::Value::Dict params;
  params.Set("type", "rawKeyDown");
  params.Set("key", "F12");
  params.Set("windowsVirtualKeyCode", 123);
  params.Set("nativeVirtualKeyCode", 123);
  SendCommandSync("Input.dispatchKeyEvent", std::move(params));

  EXPECT_EQ(nullptr, DevToolsWindow::FindDevToolsWindow(agent_host_.get()));
}

IN_PROC_BROWSER_TEST_F(
    DevToolsProtocolTest,
    NoPendingUrlShownWhenAttachedToBrowserInitiatedFailedNavigation) {
  GURL url("invalid.scheme:for-sure");
  ui_test_utils::AllBrowserTabAddedWaiter tab_added_waiter;

  content::WebContents* web_contents =
      browser()->OpenURL(content::OpenURLParams(
          url, content::Referrer(), WindowOpenDisposition::NEW_FOREGROUND_TAB,
          ui::PAGE_TRANSITION_TYPED, false));
  tab_added_waiter.Wait();
  // WaitForLoadStop() checks for the existence of the last committed
  // NavigationEntry, which will only be there if we have initial
  // NavigationEntries.
  ASSERT_EQ(WaitForLoadStop(web_contents),
            blink::features::IsInitialNavigationEntryEnabled());
  content::NavigationController& navigation_controller =
      web_contents->GetController();
  content::NavigationEntry* pending_entry =
      navigation_controller.GetPendingEntry();
  ASSERT_NE(nullptr, pending_entry);
  EXPECT_EQ(url, pending_entry->GetURL());

  EXPECT_EQ(pending_entry, navigation_controller.GetVisibleEntry());
  agent_host_ = content::DevToolsAgentHost::GetOrCreateFor(web_contents);
  agent_host_->AttachClient(this);
  SendCommandSync("Page.enable");

  // Ensure that a failed pending entry is cleared when the DevTools protocol
  // attaches, so that any modified page content is not attributed to the failed
  // URL. (crbug/1192417)
  EXPECT_EQ(nullptr, navigation_controller.GetPendingEntry());
  if (blink::features::IsInitialNavigationEntryEnabled()) {
    EXPECT_EQ(GURL(""), navigation_controller.GetVisibleEntry()->GetURL());
  } else {
    EXPECT_EQ(nullptr, navigation_controller.GetVisibleEntry());
  }
}

IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest,
                       NoPendingUrlShownForPageNavigateFromChromeExtension) {
  GURL url("https://example.com");
  // DevTools protocol use cases that have an initiator origin (e.g., for
  // extensions) should use renderer-initiated navigations and be subject to URL
  // spoof defenses.
  SetNavigationInitiatorOrigin(
      url::Origin::Create(GURL("chrome-extension://abc123/")));

  // Attach DevTools and start a navigation but don't wait for it to finish.
  Attach();
  SendCommandSync("Page.enable");
  base::Value::Dict params;
  params.Set("url", url.spec());
  SendCommandAsync("Page.navigate", std::move(params));
  content::NavigationController& navigation_controller =
      web_contents()->GetController();
  content::NavigationEntry* pending_entry =
      navigation_controller.GetPendingEntry();
  ASSERT_NE(nullptr, pending_entry);
  EXPECT_EQ(url, pending_entry->GetURL());

  // Attaching the DevTools protocol to the initial empty document of a new tab
  // should prevent the pending URL from being visible, since the protocol
  // allows modifying the initial empty document in a way that could be useful
  // for URL spoofs.
  EXPECT_NE(pending_entry, navigation_controller.GetVisibleEntry());
  EXPECT_NE(nullptr, navigation_controller.GetPendingEntry());
  EXPECT_EQ(GURL("about:blank"),
            navigation_controller.GetVisibleEntry()->GetURL());
}

using DevToolsProtocolTest_AppId = DevToolsProtocolTest;

IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest_AppId, ReturnsManifestAppId) {
  ASSERT_TRUE(embedded_test_server()->Start());
  const GURL url(embedded_test_server()->GetURL(
      "/banners/manifest_test_page.html?manifest=manifest_with_id.json"));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  Attach();

  const base::Value::Dict* result = SendCommandSync("Page.getAppId");
  EXPECT_EQ(*result->FindString("appId"),
            embedded_test_server()->GetURL("/some_id"));
}

IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest_AppId,
                       ReturnsStartUrlAsManifestAppIdIfNotSet) {
  ASSERT_TRUE(embedded_test_server()->Start());
  const GURL url(
      embedded_test_server()->GetURL("/web_apps/no_service_worker.html"));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  Attach();

  const base::Value::Dict* result = SendCommandSync("Page.getAppId");
  EXPECT_EQ(*result->FindString("appId"),
            embedded_test_server()->GetURL("/web_apps/no_service_worker.html"));
  EXPECT_EQ(*result->FindString("recommendedId"),
            "/web_apps/no_service_worker.html");
}

IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest_AppId, ReturnsNoAppIdIfNoManifest) {
  ASSERT_TRUE(embedded_test_server()->Start());
  const GURL url(embedded_test_server()->GetURL("/empty.html"));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  Attach();

  const base::Value::Dict* result = SendCommandSync("Page.getAppId");
  EXPECT_FALSE(result->Find("appId"));
  EXPECT_FALSE(result->Find("recommendedId"));
}

IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest, VisibleSecurityStateSecureState) {
  net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS);
  https_server.ServeFilesFromSourceDirectory(GetChromeTestDataDir());
  ASSERT_TRUE(https_server.Start());

  ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
      browser(), https_server.GetURL("/title1.html"), 1);
  content::NavigationEntry* entry =
      web_contents()->GetController().GetLastCommittedEntry();
  ASSERT_TRUE(entry);

  // Extract SSL status data from the navigation entry.
  scoped_refptr<net::X509Certificate> page_cert = entry->GetSSL().certificate;
  ASSERT_TRUE(page_cert);

  int ssl_version =
      net::SSLConnectionStatusToVersion(entry->GetSSL().connection_status);
  const char* page_protocol;
  net::SSLVersionToString(&page_protocol, ssl_version);

  const char* page_key_exchange_str;
  const char* page_cipher;
  const char* page_mac;
  bool is_aead;
  bool is_tls13;
  uint16_t page_cipher_suite =
      net::SSLConnectionStatusToCipherSuite(entry->GetSSL().connection_status);
  net::SSLCipherSuiteToStrings(&page_key_exchange_str, &page_cipher, &page_mac,
                               &is_aead, &is_tls13, page_cipher_suite);
  std::string page_key_exchange;
  if (page_key_exchange_str)
    page_key_exchange = page_key_exchange_str;

  const char* page_key_exchange_group =
      SSL_get_curve_name(entry->GetSSL().key_exchange_group);

  std::string page_subject_name;
  std::string page_issuer_name;
  double page_valid_from = 0.0;
  double page_valid_to = 0.0;
  if (entry->GetSSL().certificate) {
    page_subject_name = entry->GetSSL().certificate->subject().common_name;
    page_issuer_name = entry->GetSSL().certificate->issuer().common_name;
    page_valid_from = entry->GetSSL().certificate->valid_start().ToDoubleT();
    page_valid_to = entry->GetSSL().certificate->valid_expiry().ToDoubleT();
  }

  std::string page_certificate_network_error;
  if (net::IsCertStatusError(entry->GetSSL().cert_status)) {
    page_certificate_network_error = net::ErrorToString(
        net::MapCertStatusToNetError(entry->GetSSL().cert_status));
  }

  bool page_certificate_has_weak_signature =
      (entry->GetSSL().cert_status & net::CERT_STATUS_WEAK_SIGNATURE_ALGORITHM);

  bool page_certificate_has_sha1_signature_present =
      (entry->GetSSL().cert_status & net::CERT_STATUS_SHA1_SIGNATURE_PRESENT);

  int status = net::ObsoleteSSLStatus(entry->GetSSL().connection_status,
                                      entry->GetSSL().peer_signature_algorithm);
  bool page_modern_ssl = status == net::OBSOLETE_SSL_NONE;
  bool page_obsolete_ssl_protocol = status & net::OBSOLETE_SSL_MASK_PROTOCOL;
  bool page_obsolete_ssl_key_exchange =
      status & net::OBSOLETE_SSL_MASK_KEY_EXCHANGE;
  bool page_obsolete_ssl_cipher = status & net::OBSOLETE_SSL_MASK_CIPHER;
  bool page_obsolete_ssl_signature = status & net::OBSOLETE_SSL_MASK_SIGNATURE;

  Attach();
  SendCommandAsync("Security.enable");
  auto has_certificate = [](const base::Value::Dict& params) {
    return params.FindListByDottedPath(
               "visibleSecurityState.certificateSecurityState.certificate") !=
           nullptr;
  };
  base::Value::Dict params =
      WaitForMatchingNotification("Security.visibleSecurityStateChanged",
                                  base::BindRepeating(has_certificate));

  // Verify that the visibleSecurityState payload matches the SSL status data.
  std::string* security_state =
      params.FindStringByDottedPath("visibleSecurityState.securityState");
  ASSERT_TRUE(security_state);
  ASSERT_EQ(std::string("secure"), *security_state);

  base::Value* certificate_security_state =
      params.FindByDottedPath("visibleSecurityState.certificateSecurityState");
  ASSERT_TRUE(certificate_security_state);

  std::string* protocol =
      certificate_security_state->FindStringPath("protocol");
  ASSERT_TRUE(protocol);
  ASSERT_EQ(*protocol, page_protocol);

  std::string* key_exchange =
      certificate_security_state->FindStringPath("keyExchange");
  ASSERT_TRUE(key_exchange);
  ASSERT_EQ(*key_exchange, page_key_exchange);

  std::string* key_exchange_group =
      certificate_security_state->FindStringPath("keyExchangeGroup");
  if (key_exchange_group) {
    ASSERT_EQ(*key_exchange_group, page_key_exchange_group);
  }

  std::string* mac = certificate_security_state->FindStringPath("mac");
  if (mac) {
    ASSERT_EQ(*mac, page_mac);
  }

  std::string* cipher = certificate_security_state->FindStringPath("cipher");
  ASSERT_TRUE(cipher);
  ASSERT_EQ(*cipher, page_cipher);

  std::string* subject_name =
      certificate_security_state->FindStringPath("subjectName");
  ASSERT_TRUE(subject_name);
  ASSERT_EQ(*subject_name, page_subject_name);

  std::string* issuer = certificate_security_state->FindStringPath("issuer");
  ASSERT_TRUE(issuer);
  ASSERT_EQ(*issuer, page_issuer_name);

  auto valid_from = certificate_security_state->FindDoublePath("validFrom");
  ASSERT_TRUE(valid_from);
  ASSERT_EQ(*valid_from, page_valid_from);

  auto valid_to = certificate_security_state->FindDoublePath("validTo");
  ASSERT_TRUE(valid_to);
  ASSERT_EQ(*valid_to, page_valid_to);

  std::string* certificate_network_error =
      certificate_security_state->FindStringPath("certificateNetworkError");
  if (certificate_network_error) {
    ASSERT_EQ(*certificate_network_error, page_certificate_network_error);
  }

  auto certificate_has_weak_signature =
      certificate_security_state->FindBoolPath("certificateHasWeakSignature");
  ASSERT_TRUE(certificate_has_weak_signature);
  ASSERT_EQ(*certificate_has_weak_signature,
            page_certificate_has_weak_signature);

  auto certificate_has_sha1_signature_present =
      certificate_security_state->FindBoolPath("certificateHasSha1Signature");
  ASSERT_TRUE(certificate_has_sha1_signature_present);
  ASSERT_EQ(*certificate_has_sha1_signature_present,
            page_certificate_has_sha1_signature_present);

  auto modern_ssl = certificate_security_state->FindBoolPath("modernSSL");
  ASSERT_TRUE(modern_ssl);
  ASSERT_EQ(*modern_ssl, page_modern_ssl);

  auto obsolete_ssl_protocol =
      certificate_security_state->FindBoolPath("obsoleteSslProtocol");
  ASSERT_TRUE(obsolete_ssl_protocol);
  ASSERT_EQ(*obsolete_ssl_protocol, page_obsolete_ssl_protocol);

  auto obsolete_ssl_key_exchange =
      certificate_security_state->FindBoolPath("obsoleteSslKeyExchange");
  ASSERT_TRUE(obsolete_ssl_key_exchange);
  ASSERT_EQ(*obsolete_ssl_key_exchange, page_obsolete_ssl_key_exchange);

  auto obsolete_ssl_cipher =
      certificate_security_state->FindBoolPath("obsoleteSslCipher");
  ASSERT_TRUE(obsolete_ssl_cipher);
  ASSERT_EQ(*obsolete_ssl_cipher, page_obsolete_ssl_cipher);

  auto obsolete_ssl_signature =
      certificate_security_state->FindBoolPath("obsoleteSslSignature");
  ASSERT_TRUE(obsolete_ssl_signature);
  ASSERT_EQ(*obsolete_ssl_signature, page_obsolete_ssl_signature);

  const base::Value* certificate_value =
      certificate_security_state->FindListPath("certificate");
  std::vector<std::string> der_certs;
  for (const auto& cert : certificate_value->GetListDeprecated()) {
    std::string decoded;
    ASSERT_TRUE(base::Base64Decode(cert.GetString(), &decoded));
    der_certs.push_back(decoded);
  }
  std::vector<base::StringPiece> cert_string_piece;
  for (const auto& str : der_certs) {
    cert_string_piece.push_back(str);
  }

  // Check that the certificateSecurityState.certificate matches.
  net::SHA256HashValue page_cert_chain_fingerprint =
      page_cert->CalculateChainFingerprint256();
  scoped_refptr<net::X509Certificate> certificate =
      net::X509Certificate::CreateFromDERCertChain(cert_string_piece);
  ASSERT_TRUE(certificate);
  EXPECT_EQ(page_cert_chain_fingerprint,
            certificate->CalculateChainFingerprint256());
  const base::Value* security_state_issue_ids =
      params.FindByDottedPath("visibleSecurityState.securityStateIssueIds");
  EXPECT_EQ(security_state_issue_ids->GetListDeprecated().size(), 0u);

  EXPECT_FALSE(params.FindByDottedPath("visibleSecurityState.safetyTipInfo"));
}

IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest,
                       AutomationOverrideShowsAndRemovesInfoBar) {
  Attach();
  auto* manager = infobars::ContentInfoBarManager::FromWebContents(
      browser()->tab_strip_model()->GetActiveWebContents());
  {
    base::Value::Dict params;
    params.Set("enabled", true);
    SendCommandSync("Emulation.setAutomationOverride", std::move(params));
  }
  EXPECT_EQ(static_cast<int>(manager->infobar_count()), 1);
  {
    base::Value::Dict params;
    params.Set("enabled", false);
    SendCommandSync("Emulation.setAutomationOverride", std::move(params));
  }
  EXPECT_EQ(static_cast<int>(manager->infobar_count()), 0);
}

IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest,
                       AutomationOverrideAddsOneInfoBarOnly) {
  Attach();
  auto* manager = infobars::ContentInfoBarManager::FromWebContents(
      browser()->tab_strip_model()->GetActiveWebContents());
  {
    base::Value::Dict params;
    params.Set("enabled", true);
    SendCommandSync("Emulation.setAutomationOverride", std::move(params));
  }
  EXPECT_EQ(static_cast<int>(manager->infobar_count()), 1);
  {
    base::Value::Dict params;
    params.Set("enabled", true);
    SendCommandSync("Emulation.setAutomationOverride", std::move(params));
  }
  EXPECT_EQ(static_cast<int>(manager->infobar_count()), 1);
}

IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest, UntrustedClient) {
  std::unique_ptr<base::Value::Dict> params(new base::Value::Dict());
  SetIsTrusted(false);
  Attach();
  EXPECT_FALSE(SendCommandSync("HeapProfiler.enable"));  // Implemented in V8
  EXPECT_FALSE(SendCommandSync("LayerTree.enable"));     // Implemented in blink
  EXPECT_FALSE(SendCommandSync(
      "Memory.prepareForLeakDetection"));        // Implemented in content
  EXPECT_FALSE(SendCommandSync("Cast.enable"));  // Implemented in content
}

class ExtensionProtocolTest : public DevToolsProtocolTest {
 protected:
  void SetUpOnMainThread() override {
    DevToolsProtocolTest::SetUpOnMainThread();
    Profile* profile = browser()->profile();
    extension_service_ =
        extensions::ExtensionSystem::Get(profile)->extension_service();
    extension_registry_ = extensions::ExtensionRegistry::Get(profile);
  }

  content::WebContents* web_contents() override {
    return background_web_contents_;
  }

  const extensions::Extension* LoadExtension(base::FilePath extension_path) {
    extensions::TestExtensionRegistryObserver observer(extension_registry_);
    ExtensionTestMessageListener activated_listener("WORKER_ACTIVATED");
    extensions::UnpackedInstaller::Create(extension_service_)
        ->Load(extension_path);
    observer.WaitForExtensionLoaded();
    const extensions::Extension* extension = nullptr;
    for (const auto& enabled_extension :
         extension_registry_->enabled_extensions()) {
      if (enabled_extension->path() == extension_path) {
        extension = enabled_extension.get();
        break;
      }
    }
    CHECK(extension) << "Failed to find loaded extension " << extension_path;
    auto* process_manager =
        extensions::ProcessManager::Get(browser()->profile());
    if (extensions::BackgroundInfo::IsServiceWorkerBased(extension)) {
      EXPECT_TRUE(activated_listener.WaitUntilSatisfied());
      auto worker_ids =
          process_manager->GetServiceWorkersForExtension(extension->id());
      CHECK_EQ(1lu, worker_ids.size());
    } else {
      extensions::ExtensionHost* host =
          process_manager->GetBackgroundHostForExtension(extension->id());
      background_web_contents_ = host->host_contents();
    }

    return extension;
  }

  void ReloadExtension(const std::string extension_id) {
    extensions::TestExtensionRegistryObserver observer(extension_registry_);
    extension_service_->ReloadExtension(extension_id);
    observer.WaitForExtensionLoaded();
  }

 private:
  extensions::ExtensionService* extension_service_;
  extensions::ExtensionRegistry* extension_registry_;
  content::WebContents* background_web_contents_;
};

IN_PROC_BROWSER_TEST_F(ExtensionProtocolTest, ReloadTracedExtension) {
  base::FilePath extension_path =
      base::PathService::CheckedGet(chrome::DIR_TEST_DATA)
          .AppendASCII("devtools")
          .AppendASCII("extensions")
          .AppendASCII("simple_background_page");
  auto* extension = LoadExtension(extension_path);
  ASSERT_TRUE(extension);
  Attach();
  ReloadExtension(extension->id());
  base::Value::Dict params;
  params.Set("categories", "-*");
  SendCommandSync("Tracing.start", std::move(params));
  SendCommandAsync("Tracing.end");
  WaitForNotification("Tracing.tracingComplete", true);
}

IN_PROC_BROWSER_TEST_F(ExtensionProtocolTest,
                       DISABLED_ReloadServiceWorkerExtension) {
  base::FilePath extension_path =
      base::PathService::CheckedGet(chrome::DIR_TEST_DATA)
          .AppendASCII("devtools")
          .AppendASCII("extensions")
          .AppendASCII("service_worker");
  std::string extension_id;
  {
    // `extension` is stale after reload.
    auto* extension = LoadExtension(extension_path);
    ASSERT_THAT(extension, testing::NotNull());
    extension_id = extension->id();
  }
  AttachToBrowserTarget();
  const base::Value::Dict* result = SendCommandSync("Target.getTargets");

  std::string target_id;
  base::Value ext_target;
  for (const auto& target : *result->FindList("targetInfos")) {
    if (*target.FindStringKey("type") == "service_worker") {
      ext_target = target.Clone();
      break;
    }
  }
  {
    base::Value::Dict params;
    params.Set("targetId", *ext_target.FindStringKey("targetId"));
    params.Set("waitForDebuggerOnStart", false);
    SendCommandSync("Target.autoAttachRelated", std::move(params));
  }
  ReloadExtension(extension_id);
  base::Value::Dict attached =
      WaitForNotification("Target.attachedToTarget", true);
  base::Value* targetInfo = attached.Find("targetInfo");
  ASSERT_THAT(targetInfo, testing::NotNull());
  EXPECT_THAT(*targetInfo, base::test::DictionaryHasValue(
                               "type", base::Value("service_worker")));
  EXPECT_THAT(*targetInfo, base::test::DictionaryHasValue(
                               "url", *ext_target.FindKey("url")));
  EXPECT_THAT(attached.FindBool("waitingForDebugger"),
              testing::Optional(false));

  {
    base::Value::Dict params;
    params.Set("targetId", *targetInfo->FindStringKey("targetId"));
    params.Set("waitForDebuggerOnStart", false);
    SendCommandSync("Target.autoAttachRelated", std::move(params));
  }
  auto detached = WaitForNotification("Target.detachedFromTarget", true);
  EXPECT_THAT(*detached.FindString("sessionId"), Eq("sessionId"));
}

}  // namespace


text:
package org.jvending.masa.plugin.lint;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.jvending.masa.CommandExecutor;
import org.jvending.masa.ExecutionException;
import org.jvending.masa.MasaUtil;

/**
 *
 * @goal verify
 *
 * @phase process-sources
 */
public class LintMojo
    extends AbstractMojo
{

    /**
     * The maven project.
     *
     * @parameter expression="${project}"
     */
    public MavenProject project;

    /**
     * @parameter expression="${session}
     */
    public MavenSession session;

    /**
     * @parameter
     */
    public File config;

    /**
     * @parameter
     */
    public String disable;

    /**
     * @parameter
     */
    public String enable;

    /**
     * @parameter
     */
    public String check;

    /**
     * @parameter default-value="false"
     */
    public boolean disableConfigFile;

    /**
     * @parameter default-value="true"
     */
    public boolean generateHtml;

    /**
     * @parameter default-value="false"
     */
    public boolean generateXml;

    /**
     * @parameter default-value="false"
     */
    public boolean noWarn;

    /**
     * @parameter default-value="true"
     */
    public boolean quiet;

    /**
     * @parameter default-value="false"
     */
    public boolean noLines;

    /**
     * @parameter default-value="false"
     */
    public boolean fullPath;

    private File target;

    public void execute()
        throws MojoExecutionException
    {

        if ( !generateHtml && !generateXml )
        {
            throw new MojoExecutionException(
                                              "Must specify generateXml and/or generateHtml parameters as true. Check your plugin configuration." );
        }

        target = new File( project.getBuild().getDirectory(), "lint" );
        if ( !target.exists() )
        {
            target.mkdirs();
        }

        /*
        try
        {
            copyResourceFilesToTargetDir();
            copySourceFilesToTargetDir();
        }
        catch ( IOException e )
        {
            throw new MojoExecutionException( "Failed to copy source files to target directory.", e );
        }
*/
        if ( generateHtml )
        {
            List<String> commands = new ArrayList<String>();
            commands.add( "--html" );
            commands.add( new File( target, "index.html" ).getAbsolutePath() );
            executeLint( commands );
        }

        if ( generateXml )
        {
            List<String> commands = new ArrayList<String>();
            commands.add( "--xml" );
            commands.add( new File( target, "lint-report.xml" ).getAbsolutePath() );
            executeLint( commands );
        }
    }

    private void executeLint( Collection<String> args )
        throws MojoExecutionException
    {
        CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor();
        executor.setLogger( getLog() );

        List<String> commands = new ArrayList<String>();

        if ( getLintFile().exists() && !disableConfigFile )
        {
            commands.add( "--config" );
            commands.add( getLintFile().getAbsolutePath() );
        }

        if ( noLines )
        {
            commands.add( "--nolines" );
        }

        if ( quiet )
        {
            commands.add( "--quiet" );
        }

        if ( noWarn )
        {
            commands.add( "--nowarn" );
        }

        if ( fullPath )
        {
            commands.add( "--fullpath" );
        }

        if ( disable != null )
        {
            commands.add( "--disable" );
            commands.add( disable );
        }

        if ( enable != null )
        {
            commands.add( "--enable" );
            commands.add( enable );
        }

        if ( check != null )
        {
            commands.add( "--check" );
            commands.add( check );
        }

        commands.addAll( args );
        commands.add( project.getBasedir().getAbsolutePath() );

        //commands.add( target.getAbsolutePath() );

        String lintCommand = MasaUtil.getToolnameWithPath( session, project, "lint" );
        getLog().info( lintCommand + ":" + commands.toString() );
        try
        {
            executor.executeCommand( lintCommand, commands, project.getBasedir(), false );
        }
        catch ( ExecutionException e )
        {
            throw new MojoExecutionException( "", e );
        }
    }

    private File getLintFile()
    {
        return ( config != null && config.exists() ) ? config : new File( project.getBasedir(), "lint.xml" );
    }

    private void copyResourceFilesToTargetDir()
        throws IOException
    {
        for ( Resource res : (List<Resource>) project.getResources() )
        {
            File resDir = new File( res.getDirectory() );
            FileUtils.copyDirectory( resDir, new File( target, resDir.getName() ) );
        }

        File manifest = new File(project.getBasedir(), "AndroidManifest.xml");
        FileUtils.copyFileToDirectory( manifest, target );
    }

    private void copySourceFilesToTargetDir()
        throws IOException
    {
        for ( String src : (List<String>) project.getCompileSourceRoots() )
        {
            File srcDir = new File( src );
            FileUtils.copyDirectory( srcDir, new File( target, srcDir.getName() ) );
        }

        File srcDir = new File( project.getBuild().getDirectory(), "classes" );
        FileUtils.copyDirectoryStructure( srcDir, new File( target, "classes" ) );

        srcDir = new File( project.getBuild().getDirectory(), "gen-mvn" );
        FileUtils.copyDirectoryStructure( srcDir, new File( target, "classes" ) );
    }
}


text:
package griffon.core;

import griffon.core.addon.AddonManager;
import griffon.core.artifact.ArtifactManager;
import griffon.core.controller.ActionManager;
import griffon.core.env.ApplicationPhase;
import griffon.core.event.EventRouter;
import griffon.core.i18n.MessageSource;
import griffon.core.injection.Injector;
import griffon.core.mvc.MVCGroupManager;
import griffon.core.resources.ResourceHandler;
import griffon.core.resources.ResourceInjector;
import griffon.core.resources.ResourceResolver;
import griffon.core.threading.UIThreadManager;
import griffon.core.view.WindowManager;
import org.slf4j.Logger;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Locale;
import java.util.Map;

/**
 * Defines the basic contract of a Griffon application.

 *
 * @author Danno Ferrin
 * @author Andres Almiray
 * @since 0.0.0
 */
public interface GriffonApplication extends Observable {
    String PROPERTY_LOCALE = "locale";
    String PROPERTY_PHASE = "phase";

    @Nonnull
    Object createApplicationContainer(@Nonnull Map<String, Object> attributes);

    @Nonnull
    ApplicationClassLoader getApplicationClassLoader();

    @Nonnull
    Configuration getConfiguration();

    @Nonnull
    UIThreadManager getUIThreadManager();

    @Nonnull
    EventRouter getEventRouter();

    @Nonnull
    ArtifactManager getArtifactManager();

    @Nonnull
    ActionManager getActionManager();

    @Nonnull
    AddonManager getAddonManager();

    @Nonnull
    MVCGroupManager getMvcGroupManager();

    @Nonnull
    MessageSource getMessageSource();

    @Nonnull
    ResourceResolver getResourceResolver();

    @Nonnull
    ResourceHandler getResourceHandler();

    @Nonnull
    ResourceInjector getResourceInjector();

    @Nonnull
    Injector<?> getInjector();

    @Nonnull
    Context getContext();

    @Nonnull
    <W> WindowManager<W> getWindowManager();

    // --== Lifecycle ==--

    /**
     * Executes the 'Initialize' life cycle phase.
     */
    void initialize();

    /**
     * Executes the 'Startup' life cycle phase.
     */
    void startup();

    /**
     * Executes the 'Ready' life cycle phase.
     */
    void ready();

    /**
     * Executes the 'Shutdown' life cycle phase.
     *
     * @return false if the shutdown sequence was aborted
     */
    boolean shutdown();

    /**
     * Queries any available ShutdownHandlers.
     *
     * @return true if the shutdown sequence can proceed, false otherwise
     */
    boolean canShutdown();

    /**
     * Registers a ShutdownHandler on this application
     *
     * @param handler the shutdown handler to be registered; null and/or
     *                duplicated values should be ignored
     */
    void addShutdownHandler(@Nonnull ShutdownHandler handler);

    /**
     * Removes a ShutdownHandler from this application
     *
     * @param handler the shutdown handler to be removed; null and/or
     *                duplicated values should be ignored
     */
    void removeShutdownHandler(@Nonnull ShutdownHandler handler);

    // --== Properties ==--

    /**
     * Gets the application locale.
     *
     * @return the current Locale used by the application. Never returns null.
     */
    @Nonnull
    Locale getLocale();

    /**
     * Sets the application locale.

     * This is a bound property.
     *
     * @param locale the Locale value to use
     */
    void setLocale(@Nonnull Locale locale);

    /**
     * Sets the application locale.

     * This is a bound property.
     *
     * @param locale a literal representation of a Locale
     */
    void setLocaleAsString(@Nullable String locale);

    /**
     * Returns the current phase.
     *
     * @return returns the current ApplicationPhase. Never returns null.
     */
    @Nonnull
    ApplicationPhase getPhase();

    /**
     * Returns the arguments set on the command line (if any).

     *
     * @return an array of command line arguments. Never returns null.
     */
    @Nonnull
    String[] getStartupArgs();

    /**
     * Returns a Logger instance suitable for this application.
     *
     * @return a Logger instance.
     */
    @Nonnull
    Logger getLog();
}


text:
import { Dispatch, Fragment, SetStateAction, useEffect, useState } from 'react';

import { format, isValid } from 'date-fns';
import { c } from 'ttag';

import { CryptoProxy, PublicKeyReference } from '@proton/crypto';
import downloadFile from '@proton/shared/lib/helpers/downloadFile';
import { getKnowledgeBaseUrl } from '@proton/shared/lib/helpers/url';
import { dateLocale } from '@proton/shared/lib/i18n';
import { ContactPublicKeyModel } from '@proton/shared/lib/interfaces';
import { getFormattedAlgorithmNames } from '@proton/shared/lib/keys';
import { getVerifyingKeys } from '@proton/shared/lib/keys/publicKeys';
import move from '@proton/utils/move';
import uniqueBy from '@proton/utils/uniqueBy';

import { Badge, ContactKeyWarningIcon, DropdownActions, Table, TableBody, TableRow } from '../../../components';
import { classnames } from '../../../helpers';
import useActiveBreakpoint from '../../../hooks/useActiveBreakpoint';

interface Props {
    model: ContactPublicKeyModel;
    setModel: Dispatch<SetStateAction<ContactPublicKeyModel | undefined>>;
}

type LocalKeyModel = {
    publicKey: PublicKeyReference;
    armoredPublicKey: string;
    fingerprint: string;
    algo: string;
    creationTime: Date;
    expirationTime: any;
    isPrimary?: boolean;
    isWKD: boolean;
    isExpired: boolean;
    isRevoked: boolean;
    isTrusted: boolean;
    isObsolete: boolean;
    isCompromised: boolean;
    supportsEncryption: boolean;
    isUploaded: boolean;
    canBePrimary?: boolean;
    canBeTrusted: boolean;
    canBeUntrusted: boolean;
};

const ContactKeysTable = ({ model, setModel }: Props) => {
    const [keys, setKeys] = useState<LocalKeyModel[]>([]);
    const { isNarrow, isTinyMobile } = useActiveBreakpoint();

    const { emailAddress } = model;
    const totalApiKeys = model.publicKeys.apiKeys.length;

    /**
     * Extract keys info from model.publicKeys to define table body
     */
    const parse = async () => {
        const allKeys = model.isPGPInternal
            ? [...model.publicKeys.apiKeys]
            : [...model.publicKeys.apiKeys, ...model.publicKeys.pinnedKeys];
        const uniqueKeys = uniqueBy(allKeys, (publicKey) => publicKey.getFingerprint());
        const parsedKeys = await Promise.all(
            uniqueKeys.map(async (publicKey, index) => {
                const armoredPublicKey = await CryptoProxy.exportPublicKey({
                    key: publicKey,
                    format: 'armored',
                });
                const fingerprint = publicKey.getFingerprint();
                const creationTime = publicKey.getCreationTime();
                const expirationTime = await publicKey.getExpirationTime();
                const algoInfos = [
                    publicKey.getAlgorithmInfo(),
                    ...publicKey.subkeys.map((subkey) => subkey.getAlgorithmInfo()),
                ];
                const algo = getFormattedAlgorithmNames(algoInfos);
                const isExpired = await CryptoProxy.isExpiredKey({ key: publicKey });
                const isRevoked = await CryptoProxy.isRevokedKey({ key: publicKey });
                const isTrusted = model.trustedFingerprints.has(fingerprint);
                const supportsEncryption = model.encryptionCapableFingerprints.has(fingerprint);
                const isObsolete = model.obsoleteFingerprints.has(fingerprint);
                const isCompromised = model.compromisedFingerprints.has(fingerprint);
                const isPrimary =
                    !index &&
                    supportsEncryption &&
                    !isObsolete &&
                    !isCompromised &&
                    (totalApiKeys ? true : model.encrypt);
                const isWKD = model.isPGPExternal && index < totalApiKeys;
                const isUploaded = index >= totalApiKeys;
                const canBePrimary =
                    !!index &&
                    supportsEncryption &&
                    !isObsolete &&
                    !isCompromised &&
                    (index < totalApiKeys ? isTrusted : !totalApiKeys && model.encrypt);
                const canBeTrusted = !isTrusted && !isUploaded && !isCompromised;
                const canBeUntrusted = isTrusted && !isUploaded;
                return {
                    publicKey,
                    armoredPublicKey,
                    fingerprint,
                    algo,
                    creationTime,
                    expirationTime,
                    isPrimary,
                    isWKD,
                    isExpired,
                    isRevoked,
                    isTrusted,
                    supportsEncryption,
                    isObsolete,
                    isCompromised,
                    isUploaded,
                    canBePrimary,
                    canBeTrusted,
                    canBeUntrusted,
                };
            })
        );
        setKeys(parsedKeys);
    };

    useEffect(() => {
        void parse();
    }, [model.publicKeys, model.trustedFingerprints, model.encrypt]);

    return (

                    <th scope="col" className="text-ellipsis" title={c('Table header').t`Fingerprint`}>{c(
                        'Table header'
                    ).t`Fingerprint`}
                    {!isNarrow && (
                        <th scope="col" className="text-ellipsis" title={c('Table header').t`Created`}>{c(
                            'Table header'
                        ).t`Created`}
                    )}
                    {!isTinyMobile && (
                        <th scope="col" className="w10 text-ellipsis" title={c('Table header').t`Expires`}>{c(
                            'Table header'
                        ).t`Expires`}
                    )}
                    {!isNarrow && (
                        <th scope="col" className="text-ellipsis" title={c('Table header').t`Type`}>{c('Table header')
                            .t`Type`}
                    )}
                    <th scope="col" className="text-ellipsis w15" title={c('Table header').t`Status`}>{c('Table header')
                        .t`Status`}
                    <th
                        scope="col"
                        className={classnames(['text-ellipsis', isNarrow && 'w40'])}
                        title={c('Table header').t`Actions`}
                    >{c('Table header').t`Actions`}

            <TableBody>
                {keys.map(
                    ({
                        fingerprint,
                        algo,
                        creationTime,
                        expirationTime,
                        isPrimary,
                        isWKD,
                        publicKey,
                        armoredPublicKey,
                        isExpired,
                        isRevoked,
                        isTrusted,
                        supportsEncryption,
                        isObsolete,
                        isCompromised,
                        isUploaded,
                        canBePrimary,
                        canBeTrusted,
                        canBeUntrusted,
                    }) => {
                        const creation = new Date(creationTime);
                        const expiration = new Date(expirationTime);
                        const untrustKeyText = c('PGP Key info').t`We recommend that you "untrust" this key.`;
                        const obsoleteTooltipText = c('PGP Key info')
                            .t`${emailAddress} has marked this key as obsolete. This key can only be used for signature verification.`;
                        let compromisedTooltipText = c('PGP Key info')
                            .t`${emailAddress} has marked this key as compromised. This key cannot be used neither for encryption nor for signature verification.`;
                        if (isTrusted) {
                            compromisedTooltipText += ' ' + untrustKeyText;
                        }

                        const list = [
                            {
                                text: c('Action').t`Download`,
                                onClick: () => {
                                    const blob = new Blob([armoredPublicKey], {
                                        type: 'text/plain',
                                    });
                                    const filename = `publickey - ${model.emailAddress} - 0x${fingerprint
                                        .slice(0, 8)
                                        .toUpperCase()}.asc`;

                                    downloadFile(blob, filename);
                                },
                            },
                        ];
                        if (canBePrimary) {
                            list.push({
                                text: c('Action').t`Use for sending`,
                                onClick: () => {
                                    const apiKeyIndex = model.publicKeys.apiKeys.findIndex(
                                        (key) => key.getFingerprint() === fingerprint
                                    );
                                    const pinnedKeyIndex = model.publicKeys.pinnedKeys.findIndex(
                                        (key) => key.getFingerprint() === fingerprint
                                    );
                                    const reOrderedApiKeys =
                                        apiKeyIndex !== -1
                                            ? move(model.publicKeys.apiKeys, apiKeyIndex, 0)
                                            : model.publicKeys.apiKeys;
                                    const reOrderedPinnedKeys =
                                        pinnedKeyIndex !== -1
                                            ? move(model.publicKeys.pinnedKeys, pinnedKeyIndex, 0)
                                            : model.publicKeys.pinnedKeys;
                                    setModel({
                                        ...model,
                                        publicKeys: {
                                            apiKeys: reOrderedApiKeys,
                                            pinnedKeys: reOrderedPinnedKeys,
                                            verifyingPinnedKeys: getVerifyingKeys(
                                                reOrderedApiKeys,
                                                model.compromisedFingerprints
                                            ),
                                        },
                                    });
                                },
                            });
                        }
                        if (canBeTrusted) {
                            list.push({
                                text: c('Action').t`Trust`,
                                onClick: () => {
                                    const trustedFingerprints = new Set(model.trustedFingerprints);
                                    trustedFingerprints.add(fingerprint);
                                    const trustedKey = model.publicKeys.apiKeys.find(
                                        (key) => key.getFingerprint() === fingerprint
                                    );
                                    if (trustedKey) {
                                        setModel({
                                            ...model,
                                            publicKeys: {
                                                ...model.publicKeys,
                                                pinnedKeys: [...model.publicKeys.pinnedKeys, trustedKey],
                                                verifyingPinnedKeys: [
                                                    ...model.publicKeys.verifyingPinnedKeys,
                                                    trustedKey,
                                                ],
                                            },
                                            trustedFingerprints,
                                        });
                                    }
                                },
                            });
                        }

                        if (canBeUntrusted) {
                            list.push({
                                text: c('Action').t`Untrust`,
                                onClick: () => {
                                    const trustedFingerprints = new Set(model.trustedFingerprints);
                                    trustedFingerprints.delete(fingerprint);
                                    const pinnedKeys = model.publicKeys.pinnedKeys.filter(
                                        (key) => key.getFingerprint() !== fingerprint
                                    );
                                    const verifyingPinnedKeys = model.publicKeys.verifyingPinnedKeys.filter(
                                        (key) => key.getFingerprint() !== fingerprint
                                    );
                                    setModel({
                                        ...model,
                                        publicKeys: {
                                            ...model.publicKeys,
                                            pinnedKeys,
                                            verifyingPinnedKeys,
                                        },
                                        trustedFingerprints,
                                    });
                                },
                            });
                        }
                        if (isUploaded) {
                            list.push({
                                text: c('Action').t`Remove`,
                                onClick: () => {
                                    const trustedFingerprints = new Set(model.trustedFingerprints);
                                    const encryptionCapableFingerprints = new Set(model.encryptionCapableFingerprints);
                                    trustedFingerprints.delete(fingerprint);
                                    encryptionCapableFingerprints.delete(fingerprint);
                                    setModel({
                                        ...model,
                                        trustedFingerprints,
                                        encryptionCapableFingerprints,
                                        publicKeys: {
                                            ...model.publicKeys,
                                            pinnedKeys: model.publicKeys.pinnedKeys.filter(
                                                (publicKey) => publicKey.getFingerprint() !== fingerprint
                                            ),
                                            verifyingPinnedKeys: model.publicKeys.verifyingPinnedKeys.filter(
                                                (publicKey) => publicKey.getFingerprint() !== fingerprint
                                            ),
                                        },
                                    });
                                },
                            });
                        }

                        const cells = [

                                <ContactKeyWarningIcon
                                    className="mr0-5 flex-item-noshrink flex-item-centered-vert"
                                    publicKey={publicKey}
                                    emailAddress={model.emailAddress}
                                    isInternal={model.isPGPInternal}
                                    supportsEncryption={supportsEncryption}
                                />
                                {fingerprint}

,
                            !isNarrow && (isValid(creation) ? format(creation, 'PP', { locale: dateLocale }) : '-'),
                            !isTinyMobile &&
                                (isValid(expiration) ? format(expiration, 'PP', { locale: dateLocale }) : '-'),
                            !isNarrow && algo,
                            <Fragment key={fingerprint}>
                                {isPrimary ? <Badge type="primary">{c('Key badge').t`Primary`}</Badge> : null}
                                {isObsolete && !isCompromised ? (
                                    <Badge
                                        type="warning"
                                        url={getKnowledgeBaseUrl('/download-public-private-key')}
                                        tooltip={obsoleteTooltipText}
                                    >
                                        {c('Key badge').t`Obsolete`}
                                    </Badge>
                                ) : null}
                                {isCompromised ? (
                                    <Badge
                                        type="error"
                                        url={getKnowledgeBaseUrl('/download-public-private-key')}
                                        tooltip={compromisedTooltipText}
                                    >
                                        {c('Key badge').t`Compromised`}
                                    </Badge>
                                ) : null}
                                {isWKD ? <Badge type="origin">{c('Key badge').t`WKD`}</Badge> : null}
                                {isTrusted ? <Badge type="success">{c('Key badge').t`Trusted`}</Badge> : null}
                                {isRevoked ? <Badge type="error">{c('Key badge').t`Revoked`}</Badge> : null}
                                {isExpired ? <Badge type="error">{c('Key badge').t`Expired`}</Badge> : null}
                            </Fragment>,
                            <DropdownActions key={fingerprint} size="small" list={list} />,
                        ].filter(Boolean);
                        return <TableRow key={fingerprint} cells={cells} />;
                    }
                )}
            </TableBody>

    );
};

export default ContactKeysTable;


text:
import os
import re
import sys
import mpq
from hearthstone import enums

EXTRACT = [
	"Hearthstone.exe",
	"manifest-cards.csv",
	"Data/PlayErrors.xml",
	"Data/cards.unity3d",
	"Data/cards0.unity3d",
	"Data/cards1.unity3d",
	"Data/cardxml0.unity3d",
	"Data/Win/cardxml0.unity3d",
	"Data/Win/cards0.unity3d",
	"Data/Win/cards1.unity3d",
	"Data/Win/cards2.unity3d",
	"Hearthstone_Data/mainData",
	"Hearthstone_Data/Managed/Assembly-CSharp.dll",
	"Hearthstone_Data/Managed/Assembly-CSharp-firstpass.dll",
	"DBF/ACHIEVE.xml",
	"DBF/ADVENTURE.xml",
	"DBF/ADVENTURE_DATA.xml",
	"DBF/ADVENTURE_MISSION.xml",
	"DBF/ADVENTURE_MODE.xml",
	"DBF/BANNER.xml",
	"DBF/BOARD.xml",
	"DBF/BOOSTER.xml",
	"DBF/CARD.xml",
	"DBF/CARD_BACK.xml",
	"DBF/FIXED_REWARD.xml",
	"DBF/FIXED_REWARD_ACTION.xml",
	"DBF/FIXED_REWARD_MAP.xml",
	"DBF/HERO.xml",
	"DBF/SCENARIO.xml",
	"DBF/SEASON.xml",
	"DBF/WING.xml",
]

STRINGS = [
	"CREDITS_2014.txt",
	"CREDITS_2015.txt",
	"GAMEPLAY_AUDIO.txt",
	"GAMEPLAY.txt",
	"GLOBAL.txt",
	"GLUE.txt",
	"MISSION_AUDIO.txt",
	"PRESENCE.txt",
	"TUTORIAL_AUDIO.txt",
	"TUTORIAL.txt",
]

MPQ_REGEX = re.compile(r"hs-(\d+)-(\d+)-Win-final.MPQ")

def extract(mpq, build, extract_to):
	def _extract(path):
		if path not in mpq:
			# print("Skipping %r (not found)" % (path))
			return
		data = mpq.open(path).read()
		if not data:
			print("Skipping %r (empty)" % (path))
			return
		extract_path = os.path.join(extract_to, str(build), path)
		dirname = os.path.dirname(extract_path)
		if not os.path.exists(dirname):
			os.makedirs(dirname)
		print("Writing to %r" % (extract_path))

		with open(extract_path, "wb") as f:
			f.write(data)

	for path in EXTRACT:
		_extract(path)

	for locale in enums.Locale:
		for filename in STRINGS:
			_extract("Strings/%s/%s" % (locale.name, filename))

def get_builds(basepath):
	basepath = os.path.join(basepath, "Updates")
	if not os.path.exists(basepath):
		# No build chain
		return {}
	builds = {}
	for path in os.listdir(basepath):
		sre = MPQ_REGEX.match(path)
		if sre:
			base, build = sre.groups()
			base = int(base)
			if base not in builds:
				builds[base] = []
			builds[base].append(int(build))
	return builds

def get_build_chains(builds):
	chains = []
	for base_build in builds[0]:
		chain = []

		def get_build_chain(build):
			chain.append(build)
			if build in builds:
				return get_build_chain(builds[build][0])
			return chain

		chains.append(get_build_chain(base_build))

	return chains

def extract_plain(path, extract_to, only=[]):
	build = re.search(r"(\d+)", path).groups()[0]
	mpqname = os.path.join(path, "base-Win.MPQ")
	print("Opening: %r" % (mpqname))
	base = mpq.MPQFile(mpqname)
	if only and build not in only:
		return
	extract(base, build, extract_to)

def extract_chain(path, chain, extract_to, only=[]):
	base_build = 0
	mpqname = os.path.join(path, "base-Win.MPQ")
	print("Opening: %r" % (mpqname))
	base = mpq.MPQFile(mpqname)
	for build in chain:
		mpqname = "hs-%i-%i-Win-final.MPQ" % (base_build, build)
		print("Opening: %r" % (mpqname))
		base.patch(os.path.join(path, "Updates", mpqname))
		base_build = build
		if only and build not in only:
			continue
		extract(base, build, extract_to)

def main():
	if len(sys.argv) < 3:
		print("Usage: %s <indir> <outdir>" % (sys.argv[0]))
		exit(1)

	base_path = sys.argv[1]
	extract_to = sys.argv[2]
	filter_builds = [int(x) for x in sys.argv[3:]]
	direct_builds = [3140, 3388, 3749, 4243, 4944]

	for build in direct_builds:
		path = os.path.join(base_path, "%i.direct" % (build))
		builds = get_builds(path)
		extract_plain(path, extract_to, only=filter_builds)
		if builds:
			chains = get_build_chains(builds)
			for chain in chains:
				extract_chain(path, chain, extract_to, only=filter_builds)

if __name__ == "__main__":
	main()


text:
var assert       = require('assert');
var formatOutput = require('../../lib/formatOutput');

describe('#formatOutput', function () {

  it('should return two lines for a correctly formatted result', function () {
    var input = {
      position: {
        x: 2,
        y: 4
      },
      cleaned: 4
    };

    assert.strictEqual(formatOutput(input).split('\n')[0], '2 4');
    assert.strictEqual(formatOutput(input).split('\n')[1], '4');

    var input = {
      position: {
        x: 0,
        y: 0
      },
      cleaned: 0
    };

    assert.strictEqual(formatOutput(input).split('\n')[0], '0 0');
    assert.strictEqual(formatOutput(input).split('\n')[1], '0');
  });

  it('should throw an error for an invalid position input', function () {
    var called = false;
    try {
      formatOutput({
        cleaned: 4
      });
    } catch (e) {
      assert.strictEqual(e.toString(), 'Error: Invalid `result.position`');
      called = true;
    }
    assert(called);
  });

  it('should throw an error for an invalid cleaned input', function () {
    var called = false;
    try {
      formatOutput({
        position: {
          x: 4,
          y: 2
        },
        cleaned: '3' // should be number
      });
    } catch (e) {
      assert.strictEqual(e.toString(), 'Error: Invalid `result.cleaned`');
      called = true;
    }
    assert(called);
  });

});


text:
import os

import migrate
from migrate import exceptions
from oslo_db.sqlalchemy import migration
import six
import sqlalchemy

from keystone.common import sql
import keystone.conf
from keystone import exception
from keystone.i18n import _

CONF = keystone.conf.CONF

#  Different RDBMSs use different schemes for naming the Foreign Key
#  Constraints.  SQLAlchemy does not yet attempt to determine the name
#  for the constraint, and instead attempts to deduce it from the column.
#  This fails on MySQL.
def get_constraints_names(table, column_name):
    fkeys = [fk.name for fk in table.constraints
             if (isinstance(fk, sqlalchemy.ForeignKeyConstraint) and
                 column_name in fk.columns)]
    return fkeys

#  remove_constraints and add_constraints both accept a list of dictionaries
#  that contain:
#  {'table': a sqlalchemy table. The constraint is added to dropped from
#           this table.
#  'fk_column': the name of a column on the above table,  The constraint
#               is added to or dropped from this column
#  'ref_column':a sqlalchemy column object.  This is the reference column
#               for the constraint.
def remove_constraints(constraints):
    for constraint_def in constraints:
        constraint_names = get_constraints_names(constraint_def['table'],
                                                 constraint_def['fk_column'])
        for constraint_name in constraint_names:
            migrate.ForeignKeyConstraint(
                columns=[getattr(constraint_def['table'].c,
                                 constraint_def['fk_column'])],
                refcolumns=[constraint_def['ref_column']],
                name=constraint_name).drop()

def add_constraints(constraints):
    for constraint_def in constraints:

        if constraint_def['table'].kwargs.get('mysql_engine') == 'MyISAM':
            # Don't try to create constraint when using MyISAM because it's
            # not supported.
            continue

        ref_col = constraint_def['ref_column']
        ref_engine = ref_col.table.kwargs.get('mysql_engine')
        if ref_engine == 'MyISAM':
            # Don't try to create constraint when using MyISAM because it's
            # not supported.
            continue

        migrate.ForeignKeyConstraint(
            columns=[getattr(constraint_def['table'].c,
                             constraint_def['fk_column'])],
            refcolumns=[constraint_def['ref_column']]).create()

def rename_tables_with_constraints(renames, constraints, engine):
    """Rename tables with foreign key constraints.

    Tables are renamed after first removing constraints. The constraints are
    replaced after the rename is complete.

    This works on databases that don't support renaming tables that have
    constraints on them (DB2).

    `renames` is a dict, mapping {'to_table_name': from_table, ...}
    """
    if engine.name != 'sqlite':
        # SQLite doesn't support constraints, so nothing to remove.
        remove_constraints(constraints)

    for to_table_name in renames:
        from_table = renames[to_table_name]
        from_table.rename(to_table_name)

    if engine != 'sqlite':
        add_constraints(constraints)

def find_migrate_repo(package=None, repo_name='migrate_repo'):
    package = package or sql
    path = os.path.abspath(os.path.join(
        os.path.dirname(package.__file__), repo_name))
    if os.path.isdir(path):
        return path
    raise exception.MigrationNotProvided(package.__name__, path)

def _sync_common_repo(version):
    abs_path = find_migrate_repo()
    init_version = get_init_version()
    with sql.session_for_write() as session:
        engine = session.get_bind()
        _assert_not_schema_downgrade(version=version)
        migration.db_sync(engine, abs_path, version=version,
                          init_version=init_version, sanity_check=False)

def get_init_version(abs_path=None):
    """Get the initial version of a migrate repository.

    :param abs_path: Absolute path to migrate repository.
    :return:         initial version number or None, if DB is empty.
    """
    if abs_path is None:
        abs_path = find_migrate_repo()

    repo = migrate.versioning.repository.Repository(abs_path)

    # Sadly, Repository has a `latest` but not an `oldest`.
    # The value is a VerNum object which needs to be converted into an int.
    oldest = int(min(repo.versions.versions))

    if oldest < 1:
        return None

    # The initial version is one less
    return oldest - 1

def _assert_not_schema_downgrade(version=None):
    if version is not None:
        try:
            current_ver = int(six.text_type(get_db_version()))
            if int(version) < current_ver:
                raise migration.exception.DbMigrationError(
                    _("Unable to downgrade schema"))
        except exceptions.DatabaseNotControlledError:  # nosec
            # NOTE(morganfainberg): The database is not controlled, this action
            # cannot be a downgrade.
            pass

def sync_database_to_version(version=None):
    _sync_common_repo(version)

def get_db_version():
    with sql.session_for_write() as session:
        return migration.db_version(session.get_bind(),
                                    find_migrate_repo(),
                                    get_init_version())

def print_db_version():
    print(get_db_version())


text:
<?php

namespace User\Form;

use Zend\Form\Form;
use Zend\Form\Element;

class UploadAddSharingForm extends Form
{
    public function __construct()
    {
        parent::__construct('ManageUpload');

        $this->setAttributes(array(
            'class' => 'form-horizontal',
        ));

        $uploadId = new Element\Hidden('uploadId');
        $this->add($uploadId);

        $userId = new Element\Select('userId');
        $userId->setLabel('Choose User:');
        $userId->setLabelAttributes(array(
            'class' => 'control-label',
        ));
        $userId->setAttributes(array(
            'class' => 'form-control',
        ));
        $this->add($userId);

        $submit = new Element\Submit('submit');
        $submit->setAttributes(array(
            'class' => 'btn btn-default',
            'value' => 'Add Sharing',
        ));
        $this->add($submit);
    }
}


text:
class Config:
    draw_coverage = False
    draw_textures = False
    draw_grid = False
    draw_overdraw = False

    draw_texTypes = ['hght', 'mate', 'grass.extm', 'water.extm']
    draw_texType = "hght"

    draw_channels = ['all', 'r', 'g', 'b', 'a']
    draw_channel = "all"

    disable_alpha = False

    @staticmethod
    def NextTexType():
        for i in range(0, len(Config.draw_texTypes)):
            if Config.draw_texTypes[i] == Config.draw_texType:
                Config.draw_texType = Config.draw_texTypes[(i + 1) % len(Config.draw_texTypes)]
                break

    @staticmethod
    def NextChannel():
        for i in range(0, len(Config.draw_channels)):
            if Config.draw_channels[i] == Config.draw_channel:
                Config.draw_channel = Config.draw_channels[(i + 1) % len(Config.draw_channels)]
                break