text:
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.rules;

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.actions.Action;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.extra.SpawnInfo;
import com.google.devtools.build.lib.analysis.AbstractConfiguredTarget;
import com.google.devtools.build.lib.analysis.CommandHelper;
import com.google.devtools.build.lib.analysis.FileProvider;
import com.google.devtools.build.lib.analysis.FilesToRunProvider;
import com.google.devtools.build.lib.analysis.LocationExpander;
import com.google.devtools.build.lib.analysis.PseudoAction;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.Runfiles;
import com.google.devtools.build.lib.analysis.RunfilesProvider;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.analysis.actions.FileWriteAction;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction;
import com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.skylarkinterface.Param;
import com.google.devtools.build.lib.skylarkinterface.ParamType;
import com.google.devtools.build.lib.skylarkinterface.SkylarkSignature;
import com.google.devtools.build.lib.syntax.BuiltinFunction;
import com.google.devtools.build.lib.syntax.Environment;
import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.syntax.EvalUtils;
import com.google.devtools.build.lib.syntax.Printer;
import com.google.devtools.build.lib.syntax.Runtime;
import com.google.devtools.build.lib.syntax.SkylarkDict;
import com.google.devtools.build.lib.syntax.SkylarkList;
import com.google.devtools.build.lib.syntax.SkylarkList.MutableList;
import com.google.devtools.build.lib.syntax.SkylarkList.Tuple;
import com.google.devtools.build.lib.syntax.SkylarkNestedSet;
import com.google.devtools.build.lib.syntax.SkylarkSignatureProcessor;
import com.google.devtools.build.lib.syntax.Type;
import com.google.devtools.build.lib.syntax.Type.ConversionException;
import com.google.devtools.build.lib.vfs.PathFragment;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

// TODO(bazel-team): function argument names are often duplicated,
// figure out a nicely readable way to get rid of the duplications.
/**
 * A helper class to provide an easier API for Skylark rule implementations
 * and hide the original Java API. This is experimental code.
 */
public class SkylarkRuleImplementationFunctions {

  // TODO(bazel-team): add all the remaining parameters
  // TODO(bazel-team): merge executable and arguments
  /**
   * A Skylark built-in function to create and register a SpawnAction using a
   * dictionary of parameters:
   * action(
   *     inputs = [input1, input2, ...],
   *     outputs = [output1, output2, ...],
   *     executable = executable,
   *     arguments = [argument1, argument2, ...],
   *     mnemonic = 'Mnemonic',
   *     command = 'command',
   * )
   */
  @SkylarkSignature(
    name = "action",
    doc = "Creates an action that runs an executable or a shell command. You must specify either "
        + "command or executable.\n"
        + "Actions and genrules are very similar, but have different use cases. Actions are "
        + "used inside rules, and genrules are used inside macros. Genrules also have make "
        + "variable expansion.",
    objectType = SkylarkRuleContext.class,
    returnType = Runtime.NoneType.class,
    parameters = {
      @Param(name = "self", type = SkylarkRuleContext.class, doc = "This RuleContext."),
      @Param(
        name = "outputs",
        type = SkylarkList.class,
        generic1 = Artifact.class,
        named = true,
        positional = false,
        doc = "list of the output files of the action"
      ),
      @Param(
        name = "inputs",
        type = SkylarkList.class,
        generic1 = Artifact.class,
        defaultValue = "[]",
        named = true,
        positional = false,
        doc = "list of the input files of the action"
      ),
      @Param(
        name = "executable",
        type = Object.class,
        allowedTypes = {
            @ParamType(type = Artifact.class),
            @ParamType(type = PathFragment.class),
            @ParamType(type = Runtime.NoneType.class),
        },
        defaultValue = "None",
        named = true,
        positional = false,
        doc = "the executable file to be called by the action"
      ),
      @Param(
        name = "arguments",
        type = SkylarkList.class,
        generic1 = String.class,
        defaultValue = "[]",
        named = true,
        positional = false,
        doc = "command line arguments of the action"
      ),
      @Param(
        name = "mnemonic",
        type = String.class,
        noneable = true,
        defaultValue = "None",
        named = true,
        positional = false,
        doc = "a one-word description of the action, e.g. CppCompile or GoLink"
      ),
      @Param(
        name = "command",
        type = Object.class,
        allowedTypes = {
            @ParamType(type = String.class),
            @ParamType(type = SkylarkList.class, generic1 = String.class),
            @ParamType(type = Runtime.NoneType.class),
        },
        defaultValue = "None",
        named = true,
        positional = false,
        doc = "shell command to execute. It is usually preferable to "
            + "use executable instead. "
            + "Arguments are available with $1, $2, etc."
      ),
      @Param(
        name = "progress_message",
        type = String.class,
        noneable = true,
        defaultValue = "None",
        named = true,
        positional = false,
        doc = "progress message to show to the user during the build, "
            + "e.g. \"Compiling foo.cc to create foo.o\""
      ),
      @Param(
        name = "use_default_shell_env",
        type = Boolean.class,
        defaultValue = "False",
        named = true,
        positional = false,
        doc = "whether the action should use the built in shell environment or not"
      ),
      @Param(
        name = "env",
        type = SkylarkDict.class,
        noneable = true,
        defaultValue = "None",
        named = true,
        positional = false,
        doc = "sets the dictionary of environment variables"
      ),
      @Param(
        name = "execution_requirements",
        type = SkylarkDict.class,
        noneable = true,
        defaultValue = "None",
        named = true,
        positional = false,
        doc = "information for scheduling the action. See "
            + "tags for useful keys."
      ),
      @Param(
        name = "input_manifests",
        type = SkylarkDict.class,
        noneable = true,
        defaultValue = "None",
        named = true,
        positional = false,
        doc = "sets the map of input manifests files; "
            + "they are typically generated by resolve_command"
      )
    },
    useLocation = true
  )
  private static final BuiltinFunction createSpawnAction =
      new BuiltinFunction("action") {
        public Runtime.NoneType invoke(
            SkylarkRuleContext ctx,
            SkylarkList outputs,
            SkylarkList inputs,
            Object executableUnchecked,
            SkylarkList arguments,
            Object mnemonicUnchecked,
            Object commandUnchecked,
            Object progressMessage,
            Boolean useDefaultShellEnv,
            Object envUnchecked,
            Object executionRequirementsUnchecked,
            Object inputManifestsUnchecked,
            Location loc)
            throws EvalException, ConversionException {
          SpawnAction.Builder builder = new SpawnAction.Builder();
          // TODO(bazel-team): builder still makes unnecessary copies of inputs, outputs and args.
          boolean hasCommand = commandUnchecked != Runtime.NONE;
          Iterable<Artifact> inputArtifacts = inputs.getContents(Artifact.class, "inputs");
          builder.addInputs(inputArtifacts);
          builder.addOutputs(outputs.getContents(Artifact.class, "outputs"));
          if (hasCommand && arguments.size() > 0) {
            // When we use a shell command, add an empty argument before other arguments.
            //   e.g.  bash -c "cmd" '' 'arg1' 'arg2'
            // bash will use the empty argument as the value of $0 (which we don't care about).
            // arg1 and arg2 will be $1 and $2, as a user exects.
            builder.addArgument("");
          }
          builder.addArguments(arguments.getContents(String.class, "arguments"));
          if (executableUnchecked != Runtime.NONE) {
            if (executableUnchecked instanceof Artifact) {
              Artifact executable = (Artifact) executableUnchecked;
              builder.addInput(executable);
              FilesToRunProvider provider = ctx.getExecutableRunfiles(executable);
              if (provider == null) {
                builder.setExecutable(executable);
              } else {
                builder.setExecutable(provider);
              }
            } else if (executableUnchecked instanceof PathFragment) {
              builder.setExecutable((PathFragment) executableUnchecked);
            } else {
              throw new EvalException(
                  loc,
                  "expected file or PathFragment for "
                      + "executable but got "
                      + EvalUtils.getDataTypeName(executableUnchecked)
                      + " instead");
            }
          }
          if ((commandUnchecked == Runtime.NONE) == (executableUnchecked == Runtime.NONE)) {
            throw new EvalException(
                loc, "You must specify either 'command' or 'executable' argument");
          }
          if (hasCommand) {
            if (commandUnchecked instanceof String) {
              builder.setShellCommand((String) commandUnchecked);
            } else if (commandUnchecked instanceof SkylarkList) {
              SkylarkList commandList = (SkylarkList) commandUnchecked;
              if (commandList.size() < 3) {
                throw new EvalException(loc, "'command' list has to be of size at least 3");
              }
              builder.setShellCommand(commandList.getContents(String.class, "command"));
            } else {
              throw new EvalException(
                  loc,
                  "expected string or list of strings for "
                      + "command instead of "
                      + EvalUtils.getDataTypeName(commandUnchecked));
            }
          }

          // The actual command can refer to an executable from the inputs, which could require
          // some runfiles. Consequently, we add the runfiles of all inputs of this action manually.
          for (Artifact current : inputArtifacts) {
            FilesToRunProvider provider = ctx.getExecutableRunfiles(current);
            if (provider != null) {
              builder.addTool(provider);
            }
          }

          String mnemonic = mnemonicUnchecked == Runtime.NONE
              ? "Generating" : (String) mnemonicUnchecked;
          builder.setMnemonic(mnemonic);
          if (envUnchecked != Runtime.NONE) {
            builder.setEnvironment(
                ImmutableMap.copyOf(
                    SkylarkDict.castSkylarkDictOrNoneToDict(
                        envUnchecked, String.class, String.class, "env")));
          }
          if (progressMessage != Runtime.NONE) {
            builder.setProgressMessage((String) progressMessage);
          }
          if (EvalUtils.toBoolean(useDefaultShellEnv)) {
            builder.useDefaultShellEnvironment();
          }
          if (executionRequirementsUnchecked != Runtime.NONE) {
            builder.setExecutionInfo(
                ImmutableMap.copyOf(
                    SkylarkDict.castSkylarkDictOrNoneToDict(
                        executionRequirementsUnchecked,
                        String.class,
                        String.class,
                        "execution_requirements")));
          }
          if (inputManifestsUnchecked != Runtime.NONE) {
            for (Map.Entry<PathFragment, Artifact> entry :
                     SkylarkDict.castSkylarkDictOrNoneToDict(
                         inputManifestsUnchecked,
                         PathFragment.class,
                         Artifact.class,
                         "input manifest file map")
                     .entrySet()) {
              builder.addInputManifest(entry.getValue(), entry.getKey());
            }
          }
          // Always register the action
          ctx.getRuleContext().registerAction(builder.build(ctx.getRuleContext()));
          return Runtime.NONE;
        }
      };

  @SkylarkSignature(name = "expand_location",
      doc =
      "Expands all $(location ...) templates in the given string by replacing "
      + "$(location //x) with the path of the output file of target //x. "
      + "Expansion only works for labels that point to direct dependencies of this rule or that "
      + "are explicitly listed in the optional argument targets.
"
      + "$(location ...) will cause an error if the referenced target has multiple "
      + "outputs. In this case, please use $(locations ...) since it produces a space-"
      + "separated list of output paths. It can be safely used for a single output file, too.",
      objectType = SkylarkRuleContext.class, returnType = String.class,
      parameters = {
          @Param(name = "self", type = SkylarkRuleContext.class, doc = "this context"),
          @Param(name = "input", type = String.class, doc = "string to be expanded"),
          @Param(name = "targets", type = SkylarkList.class,
              generic1 = AbstractConfiguredTarget.class, defaultValue = "[]",
              doc = "list of targets for additional lookup information"),
      },
      useLocation = true, useEnvironment = true)
  private static final BuiltinFunction expandLocation = new BuiltinFunction("expand_location") {
    @SuppressWarnings("unused")
    public String invoke(SkylarkRuleContext ctx, String input, SkylarkList targets,
        Location loc, Environment env) throws EvalException {
      try {
        return new LocationExpander(ctx.getRuleContext(),
                makeLabelMap(targets.getContents(AbstractConfiguredTarget.class, "targets")), false)
            .expand(input);
      } catch (IllegalStateException ise) {
        throw new EvalException(loc, ise);
      }
    }
  };

  /**
   * Builds a map: Label -> List of files from the given labels
   * @param knownLabels List of known labels
   * @return Immutable map with immutable collections as values
   */
  private static ImmutableMap<Label, ImmutableCollection<Artifact>> makeLabelMap(
      Iterable<AbstractConfiguredTarget> knownLabels) {
    ImmutableMap.Builder<Label, ImmutableCollection<Artifact>> builder = ImmutableMap.builder();

    for (AbstractConfiguredTarget current : knownLabels) {
      builder.put(
          AliasProvider.getDependencyLabel(current),
          ImmutableList.copyOf(current.getProvider(FileProvider.class).getFilesToBuild()));
    }

    return builder.build();
  }

  // TODO(bazel-team): improve this method to be more memory friendly
  @SkylarkSignature(name = "file_action",
      doc = "Creates a file write action.",
      objectType = SkylarkRuleContext.class,
      returnType = Runtime.NoneType.class,
      parameters = {
        @Param(name = "self", type = SkylarkRuleContext.class, doc = "this context"),
        @Param(name = "output", type = Artifact.class, doc = "the output file"),
        @Param(name = "content", type = String.class, doc = "the contents of the file"),
        @Param(name = "executable", type = Boolean.class, defaultValue = "False",
            doc = "whether the output file should be executable (default is False)")})
  private static final BuiltinFunction createFileWriteAction =
      new BuiltinFunction("file_action") {
        public Runtime.NoneType invoke(SkylarkRuleContext ctx,
            Artifact output, String content, Boolean executable)
            throws EvalException, ConversionException {
          FileWriteAction action = new FileWriteAction(
              ctx.getRuleContext().getActionOwner(), output, content, executable);
          ctx.getRuleContext().registerAction(action);
          return Runtime.NONE;
        }
      };

  @SkylarkSignature(name = "empty_action",
      doc =
      "Creates an empty action that neither executes a command nor produces any "
      + "output, but that is useful for inserting 'extra actions'.",
      objectType = SkylarkRuleContext.class, returnType = Runtime.NoneType.class,
      parameters = {
          @Param(name = "self", type = SkylarkRuleContext.class, doc = "this context"),
          @Param(name = "mnemonic", type = String.class, named = true, positional = false,
          doc = "a one-word description of the action, e.g. CppCompile or GoLink"),
          @Param(name = "inputs", type = SkylarkList.class, generic1 = Artifact.class,
              named = true, positional = false,
              defaultValue = "[]", doc = "list of the input files of the action"),
      })
  private static final BuiltinFunction createEmptyAction = new BuiltinFunction("empty_action") {
    @SuppressWarnings("unused")
    public Runtime.NoneType invoke(SkylarkRuleContext ctx, String mnemonic, SkylarkList inputs)
        throws EvalException, ConversionException {
      RuleContext ruleContext = ctx.getRuleContext();
      Action action = new PseudoAction<SpawnInfo>(generateUuid(ruleContext),
          ruleContext.getActionOwner(), convertInputs(inputs), generateDummyOutputs(ruleContext),
          mnemonic, SpawnInfo.spawnInfo, createEmptySpawnInfo());
      ruleContext.registerAction(action);

      return Runtime.NONE;
    }

    private NestedSet<Artifact> convertInputs(SkylarkList inputs) throws EvalException {
      return NestedSetBuilder.<Artifact>compileOrder()
          .addAll(inputs.getContents(Artifact.class, "inputs")).build();
    }

    protected UUID generateUuid(RuleContext ruleContext) {
      return UUID.nameUUIDFromBytes(
          String.format("empty action %s", ruleContext.getLabel())
              .getBytes(StandardCharsets.UTF_8));
    }

    protected ImmutableList<Artifact> generateDummyOutputs(RuleContext ruleContext) {
      return ImmutableList.of(PseudoAction.getDummyOutput(ruleContext));
    }

    protected SpawnInfo createEmptySpawnInfo() {
      return SpawnInfo.newBuilder().build();
    }
  };

  @SkylarkSignature(name = "template_action",
      doc = "Creates a template expansion action.",
      objectType = SkylarkRuleContext.class,
      returnType = TemplateExpansionAction.class,
      parameters = {
        @Param(name = "self", type = SkylarkRuleContext.class, doc = "this context"),
        @Param(name = "template", type = Artifact.class,
            named = true, positional = false,
            doc = "the template file"),
        @Param(name = "output", type = Artifact.class,
            named = true, positional = false,
            doc = "the output file"),
        @Param(name = "substitutions", type = SkylarkDict.class,
            named = true, positional = false,
            doc = "substitutions to make when expanding the template"),
        @Param(name = "executable", type = Boolean.class, defaultValue = "False",
            named = true, positional = false,
            doc = "whether the output file should be executable (default is False)")})
  private static final BuiltinFunction createTemplateAction =
      new BuiltinFunction("template_action", Arrays.<Object>asList(false)) {
        public TemplateExpansionAction invoke(SkylarkRuleContext ctx, Artifact template,
            Artifact output, SkylarkDict<?, ?> substitutionsUnchecked, Boolean executable)
            throws EvalException, ConversionException {
          ImmutableList.Builder<Substitution> substitutionsBuilder = ImmutableList.builder();
          for (Map.Entry<String, String> substitution : substitutionsUnchecked.getContents(
              String.class, String.class, "substitutions").entrySet()) {
            // ParserInputSource.create(Path) uses Latin1 when reading BUILD files, which might
            // contain UTF-8 encoded symbols as part of template substitution.
            // As a quick fix, the substitution values are corrected before being passed on.
            // In the long term, fixing ParserInputSource.create(Path) would be a better approach.
            substitutionsBuilder.add(Substitution.of(
                substitution.getKey(), convertLatin1ToUtf8(substitution.getValue())));
          }
          TemplateExpansionAction action = new TemplateExpansionAction(
              ctx.getRuleContext().getActionOwner(),
              template,
              output,
              substitutionsBuilder.build(),
              executable);
          ctx.getRuleContext().registerAction(action);
          return action;
        }
      };

  /**
   * Returns the proper UTF-8 representation of a String that was erroneously read using Latin1.
   * @param latin1 Input string
   * @return The input string, UTF8 encoded
   */
  private static String convertLatin1ToUtf8(String latin1) {
    return new String(latin1.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
  }

  // TODO(bazel-team): Remove runfile states from Skylark.
  @SkylarkSignature(name = "runfiles",
      doc = "Creates a runfiles object.",
      objectType = SkylarkRuleContext.class,
      returnType = Runfiles.class,
      parameters = {
        @Param(name = "self", type = SkylarkRuleContext.class, doc = "This context."),
        @Param(name = "files", type = SkylarkList.class, generic1 = Artifact.class,
            defaultValue = "[]", doc = "The list of files to be added to the runfiles."),
        // TODO(bazel-team): If we have a memory efficient support for lazy list containing
        // NestedSets we can remove this and just use files = [file] + list(set)
        // Also, allow empty set for init
        @Param(name = "transitive_files", type = SkylarkNestedSet.class, generic1 = Artifact.class,
            noneable = true, defaultValue = "None",
            doc = "The (transitive) set of files to be added to the runfiles."),
        @Param(name = "collect_data", type = Boolean.class, defaultValue = "False",
            doc = "Whether to collect the data "
            + "runfiles from the dependencies in srcs, data and deps attributes."),
        @Param(name = "collect_default", type = Boolean.class, defaultValue = "False",
            doc = "Whether to collect the default "
            + "runfiles from the dependencies in srcs, data and deps attributes."),
        @Param(name = "symlinks", type = SkylarkDict.class, defaultValue = "{}",
            doc = "The map of symlinks to be added to the runfiles, prefixed by workspace name."),
        @Param(name = "root_symlinks", type = SkylarkDict.class, defaultValue = "{}",
            doc = "The map of symlinks to be added to the runfiles.")},
      useLocation = true)
  private static final BuiltinFunction runfiles = new BuiltinFunction("runfiles") {
    public Runfiles invoke(SkylarkRuleContext ctx, SkylarkList files, Object transitiveFiles,
        Boolean collectData, Boolean collectDefault,
        SkylarkDict<?, ?> symlinks, SkylarkDict<?, ?> rootSymlinks,
        Location loc) throws EvalException, ConversionException {
      Runfiles.Builder builder = new Runfiles.Builder(
          ctx.getRuleContext().getWorkspaceName(),
          ctx.getConfiguration().legacyExternalRunfiles());
      boolean checkConflicts = false;
      if (EvalUtils.toBoolean(collectData)) {
        builder.addRunfiles(ctx.getRuleContext(), RunfilesProvider.DATA_RUNFILES);
      }
      if (EvalUtils.toBoolean(collectDefault)) {
        builder.addRunfiles(ctx.getRuleContext(), RunfilesProvider.DEFAULT_RUNFILES);
      }
      if (!files.isEmpty()) {
        builder.addArtifacts(files.getContents(Artifact.class, "files"));
      }
      if (transitiveFiles != Runtime.NONE) {
        builder.addTransitiveArtifacts(((SkylarkNestedSet) transitiveFiles).getSet(Artifact.class));
      }
      if (!symlinks.isEmpty()) {
        // If Skylark code directly manipulates symlinks, activate more stringent validity checking.
        checkConflicts = true;
        for (Map.Entry<String, Artifact> entry : symlinks.getContents(
            String.class, Artifact.class, "symlinks").entrySet()) {
          builder.addSymlink(new PathFragment(entry.getKey()), entry.getValue());
        }
      }
      if (!rootSymlinks.isEmpty()) {
        checkConflicts = true;
        for (Map.Entry<String, Artifact> entry : rootSymlinks.getContents(
            String.class, Artifact.class, "root_symlinks").entrySet()) {
          builder.addRootSymlink(new PathFragment(entry.getKey()), entry.getValue());
        }
      }
      Runfiles runfiles = builder.build();
      if (checkConflicts) {
        runfiles.setConflictPolicy(Runfiles.ConflictPolicy.ERROR);
      }
      return runfiles;
    }
  };

  // TODO(bazel-team): find a better way to typecheck this argument.
  @SuppressWarnings("unchecked")
  private static Map<Label, Iterable<Artifact>> checkLabelDict(
      Map<?, ?> labelDict, Location loc)
      throws EvalException {
    for (Map.Entry<?, ?> entry : labelDict.entrySet()) {
      Object key = entry.getKey();
      if (!(key instanceof Label)) {
        throw new EvalException(
            loc, Printer.format("invalid key %r in 'label_dict'", key));
      }
      Object val = entry.getValue();
      if (!(val instanceof Iterable)) {
        throw new EvalException(
            loc, Printer.format("invalid value %r in 'label_dict'", val));
      }
      for (Object file : (Iterable) val) {
        if (!(file instanceof Artifact)) {
          throw new EvalException(
              loc, Printer.format("invalid value %r in 'label_dict'", val));
        }
      }
    }
    return (Map<Label, Iterable<Artifact>>) labelDict;
  }

  /** suffix of script to be used in case the command is too long to fit on a single line */
  private static final String SCRIPT_SUFFIX = ".script.sh";

  @SkylarkSignature(
    name = "resolve_command",
    doc = "Experimental."
    + "Returns a tuple (inputs, command, input_manifests) of the list of resolved inputs, "
    + "the argv list for the resolved command, and "
    + "the dict mapping locations to runfiles required to run the command, "
    + "all of them suitable for passing as the same-named arguments of the ctx.action method.",
    objectType = SkylarkRuleContext.class,
    returnType = Tuple.class,
    parameters = {
      @Param(name = "self", type = SkylarkRuleContext.class, doc = "this RuleContext"),
      @Param(
        name = "command",
        type = String.class, // string
        defaultValue = "''",
        named = true,
        positional = false,
        doc = "command to resolve"
      ),
      @Param(
        name = "attribute",
        type = String.class, // string
        defaultValue = "None",
        noneable = true,
        named = true,
        positional = false,
        doc = "name of the associated attribute for which to issue an error, or None"
      ),
      @Param(
        name = "expand_locations",
        type = Boolean.class,
        defaultValue = "False",
        named = true,
        positional = false,
        doc = "shall we expand $(location) variables? "
            + "See ctx.expand_location() for more details."
      ),
      @Param(
        name = "make_variables",
        type = SkylarkDict.class, // dict(string, string)
        noneable = true,
        defaultValue = "None",
        named = true,
        positional = false,
        doc = "make variables to expand, or None"
      ),
      @Param(
        name = "tools",
        defaultValue = "[]",
        type = SkylarkList.class,
        generic1 = TransitiveInfoCollection.class,
        named = true,
        positional = false,
        doc = "list of tools (list of targets)"
      ),
      @Param(
        name = "label_dict",
        type = SkylarkDict.class,
        defaultValue = "{}",
        named = true,
        positional = false,
        doc =
            "dictionary of resolved labels and the corresponding list of Files "
        + "(a dict of Label : list of Files)"
      ),
      @Param(
        name = "execution_requirements",
        type = SkylarkDict.class,
        defaultValue = "{}",
        named = true,
        positional = false,
        doc =
            "information for scheduling the action to resolve this command. See "
            + "tags for useful keys."
      ),
    },
    useLocation = true, useEnvironment = true
  )
  private static final BuiltinFunction resolveCommand =
      new BuiltinFunction("resolve_command") {
        @SuppressWarnings("unchecked")
        public Tuple<Object> invoke(
            SkylarkRuleContext ctx,
            String command,
            Object attributeUnchecked,
            Boolean expandLocations,
            Object makeVariablesUnchecked,
            SkylarkList tools,
            SkylarkDict<?, ?> labelDictUnchecked,
            SkylarkDict<?, ?> executionRequirementsUnchecked,
            Location loc,
            Environment env)
            throws ConversionException, EvalException {
          Label ruleLabel = ctx.getLabel();
          Map<Label, Iterable<Artifact>> labelDict = checkLabelDict(labelDictUnchecked, loc);
          // The best way to fix this probably is to convert CommandHelper to Skylark.
          CommandHelper helper = new CommandHelper(
              ctx.getRuleContext(),
              tools.getContents(TransitiveInfoCollection.class, "tools"),
              ImmutableMap.copyOf(labelDict));
          String attribute =
              Type.STRING.convertOptional(attributeUnchecked, "attribute", ruleLabel);
          if (expandLocations) {
            command = helper.resolveCommandAndExpandLabels(
                command, attribute, false, false);
          }
          if (!EvalUtils.isNullOrNone(makeVariablesUnchecked)) {
            Map<String, String> makeVariables = Type.STRING_DICT.convert(
                makeVariablesUnchecked, "make_variables", ruleLabel);
            command = ctx.expandMakeVariables(attribute, command, makeVariables);
          }
          List<Artifact> inputs = new ArrayList<>();
          inputs.addAll(helper.getResolvedTools());

          ImmutableMap<String, String> executionRequirements = ImmutableMap.copyOf(
              SkylarkDict.castSkylarkDictOrNoneToDict(
                    executionRequirementsUnchecked,
                    String.class,
                    String.class,
                    "execution_requirements"));
          List<String> argv =
              helper.buildCommandLine(command, inputs, SCRIPT_SUFFIX, executionRequirements);
          return Tuple.<Object>of(
              new MutableList(inputs, env),
              new MutableList(argv, env),
              helper.getRemoteRunfileManifestMap());
        }
      };

  static {
    SkylarkSignatureProcessor.configureSkylarkFunctions(SkylarkRuleImplementationFunctions.class);
  }
}


text:
package com.ly.java.thrift.TSimpleServer;

import org.apache.thrift.TException;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;

import com.ly.java.thrift.HelloWorld;
import com.ly.java.thrift.HelloWorldImpl;

/**
 * 注册服务端  阻塞式、单线程
 * 简单的单线程服务模型 TSimpleServer
 */
public class HelloTSimpleServer {
	// 注册端口
	public static final int SERVER_PORT = 8083;

	public static void main(String[] args) throws TException {
		//设置处理器
		TProcessor tprocessor = new HelloWorld.Processor<HelloWorld.Iface>(new HelloWorldImpl());
		// 简单的单线程服务模型,阻塞IO
		TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
		TServer.Args tArgs = new TServer.Args(serverTransport);
		tArgs.processor(tprocessor);
		////使用二进制协议
		tArgs.protocolFactory(new TBinaryProtocol.Factory());
		//创建服务器
		TServer server = new TSimpleServer(tArgs);
		System.out.println("HelloServer start....");
		server.serve(); // 启动服务
	}
}

redpajama_set_name: RedPajamaGithub


text:
<!DOCTYPE html>

	<title>refer counter example</title>


text:
<feed version="0.3" xmlns="http://purl.org/atom/ns#">
<entry>
<content type="text/html" mode="escaped"></content>
</entry>
</feed>


text:
package com.petersoboyejo.mobilepassport.activities;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.petersoboyejo.mobilepassport.R;

public class RegisterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
    }
}


text:
package main

import (
	"net/http"
	"os"
	"time"

	"github.com/gorilla/handlers"
)

func middlewareStack(next http.Handler) http.Handler {
	return timeoutHandler(loggingHandler(next))
}

func timeoutHandler(next http.Handler) http.Handler {
	return http.TimeoutHandler(next, 60*time.Second, "Service unavailable")
}

func loggingHandler(next http.Handler) http.Handler {
	return handlers.LoggingHandler(os.Stdout, next)
}


text:
.. py:currentmodule:: NonlinearTMM

Getting started
###############

Installation
************

Installation of NonlinearTMM package is possible through pip or from source
code.

Requirements:

* `Python >3.0 <https://www.python.org/>`_
* C++ compiler
* `Cython <http://cython.org/>`_ for wrapping C++ code to Python
* `Numpy <http://www.numpy.org/>`_ for general arrays
* `Eigency <https://github.com/wouterboomsma/eigency>`_ for Eigen and Numpy array conversion
* `Matplotlib <http://matplotlib.org/>`_ for plotting in examples
* `Pytest <http://doc.pytest.org/>`_ for testing

Tested with Python 3.6, Windows 10 x64, Visual studio C++ 2015.

Dependencies:

* C++ code depends on `Eigen library <http://eigen.tuxfamily.org/>`_ (already included in package)

Installation through pip is done like:

.. code-block:: bash

	pip install NonlinearTMM

Alternatively, it is possible to install the package form the source code by commands

.. code-block:: bash

	python setup.py install

or

.. code-block:: bash

	pip intall .

in the source code folder.

Package structure
*****************

The package has three main classes:

* :class:`Material`
* :class:`TMM`
* :class:`SecondOrderNLTMM`

Class :class:`Material` is responsible to represent the properties of optical
material. Mainly wavelength dependent refractive indices and second-order
suceptibility tensor for nonlinear processes.

Class :class:`TMM` (alias of :class:`NonlinearTMM`) is has all the standard TMM features:

* Both p- and s-polarization
* Arbritarty angle of incidence
* Calculation of reflection, transmission and absorption of plane waves (:any:`GetIntensities <NonlinearTMM.GetIntensities>` and :any:`GetAbsorbedIntensity <NonlinearTMM.GetAbsorbedIntensity>`)
* Calculaion of electric and magnetic fields inside structure (:any:`GetFields <NonlinearTMM.GetFields>` and :any:`GetFields2D <NonlinearTMM.GetFields2D>`)
* Calculation of field enhancement (:any:`GetEnhancement <NonlinearTMM.GetEnhancement>`)
* Sweep over any parameter (:any:`Sweep <NonlinearTMM.Sweep>`)

In addition to those standard features, the class has similar functionality to
work with waves with arbritarty profile (e.g. Gaussian beam). The configuration
of the beam is done through attribute :any:`wave <NonlinearTMM.wave>` (see class :any:`_Wave`).
The interface for the calculations with arbritarty beams is similar to standard TMM:

* Calculation of reflection, transmission and absorption of beams (:any:`WaveGetPowerFlows <NonlinearTMM.WaveGetPowerFlows>`)
* Calculaion of electric and magnetic fields inside structure (:any:`WaveGetFields2D <NonlinearTMM.WaveGetFields2D>`)
* Calculation of field enhancement (:any:`WaveGetEnhancement <NonlinearTMM.WaveGetEnhancement>`)
* Sweep over any parameter (:any:`WaveSweep <NonlinearTMM.WaveSweep>`)

Finally, :class:`SecondOrderNLTMM` class ic capable of calculating second-order
nonlinear processes like second-harmonic generation, sum-frequency generation and
difference frequency generation. This has similar interface as :any:`TMM` - it
supports both the plane waves and beams.

Standard TMM
************

Plane waves example
===================

As an example three layer structure consisting of a prism (z < 0), 50 nm thick silver
film and air is studied. Such kind of structure supports surface plasmon
resonance (SPP) if excited by p-polarized light and is named Kretschmann
configuration. The example code is shown bellow and could be divided into
following steps:

1. Specifying materials refractive indices.
2. Initializing :class:`TMM`, setting params and adding layers.
3. By using :any:`Sweep <NonlinearTMM.Sweep>` calculate the dependence of reflection, transmission and enhancment factor on the angle of incidence.
4. Find the plasmonic resonance by maximum enhancment.
5. Calculate 1D fields at plasmonic resonance by :any:`GetFields <NonlinearTMM.GetFields>`.
6. Calculate 2D fields at plasmonic resonance by :any:`GetFields2D <NonlinearTMM.GetFields2D>`.
7. Plot all results

.. literalinclude:: ../Examples/ExampleTMM.py

The results of the calculations are shown bellow. Indeed there is a sharrp dip
in the reflection (R) near the angle of incidence ca 44 degrees. At the same angle
the field enhancement factor is maximum and is more than 12 times. In the second
the results of the fields calculations at plasmonic resonance is presented. Indeed,
surface wave on the silver-air interface is excited and characteristic pattern of
fields for SPP is visible.

.. image:: images/TMM-example.png

Gaussian wave example
=====================

Previous example was entirely about standard TMM. Now, the calculations are
extended to the beams, in this case Gaussian beam. The steps of the calculations
remain the same, except :class:`_Wave` parameters must be set (:class:`TMM` has
attribute :any:`TMM.wave`). Gaussian beam power is set to 10 mW and waist size
to 10 μm.

.. literalinclude:: ../Examples/ExampleTMMForWaves.py

The results of those calculations are bellow. Despite the fact, that the structure
is the same, the dip in the reflection is different. The reason for this behaviour
is that as the resonances of SPPs are narrow, they also require well collimated
beam to excite them. Also field enhancment is ca 3 times lower, as expected. On
the right side, the electrical field norm is shown. It is clearly visible, that
Gaussian beam is incident form the left, and it gets reflected from the metal film (z = 0).
Part of the energy is transmitted to excite SPPs at the metal-air interface. The
excited SPPs are propagating on the metal film and are absorbe after ca 20 μm of
propagation.

.. image:: images/TMMForWaves-example.png

Second-order nonlinear TMM
**************************

Plane waves example
===================

Will be added in near future.

Gaussian wave example
=====================

Will be added in near future.


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

<title>S-Index</title>
<meta name="date" content="2015-05-22">
<link rel="stylesheet" type="text/css" href="../stylesheet.css" title="Style">

JavaScript is disabled on your browser.

Skip navigation links

Overview

Package

Class

Tree

Deprecated

Index

Help

Prev Letter

Next Letter

Frames

No Frames

All Classes

A C D E F G H K L M N P R S T U V W

S

send() - Method in class MapGenerator.Vrep.VrepSocket

Send initialized data block

sendMap() - Method in class MapGenerator.Vrep.VrepSocket

Sends whole map to Vrep

setDisJointSetID(int) - Method in class MapGenerator.KruskalsAlg.KruskalsNode

setFrom(Node<T>) - Method in class MapGenerator.Graph.Edge

setHeight(int) - Method in class MapGenerator.Generators.RoomsGenerator

setHeight(int) - Method in class MapGenerator.Generators.RoomType

setHeight(int) - Method in class MapGenerator.MainStructure.Map

setHeight(int) - Method in class MapGenerator.MainStructure.Room

setLevel(int) - Method in class MapGenerator.MainStructure.Room

setMap(Map) - Method in class MapGenerator.Generators.RoomsGenerator

setPORT(int) - Static method in class MapGenerator.Vrep.VrepSocket

Sets port number

setRoom_count(int) - Method in class MapGenerator.Generators.RoomsGenerator

setsCounter - Variable in class MapGenerator.KruskalsAlg.KruskalsGraph

setState(Field.State) - Method in class MapGenerator.MainStructure.Field

setTo(Node<T>) - Method in class MapGenerator.Graph.Edge

setWeight(int) - Method in class MapGenerator.Graph.Edge

setWidth(int) - Method in class MapGenerator.Generators.RoomsGenerator

setWidth(int) - Method in class MapGenerator.Generators.RoomType

setWidth(int) - Method in class MapGenerator.MainStructure.Map

setWidth(int) - Method in class MapGenerator.MainStructure.Room

setX(int) - Method in class MapGenerator.MainStructure.Room

setY(int) - Method in class MapGenerator.MainStructure.Room

showWallMap() - Method in class MapGenerator.MainStructure.MapGui

Show ASCII graphical representation of the map

swapRooms(int, int) - Method in class MapGenerator.MainStructure.Map

Swaps these two rooms in rooms hierarchy.

swapWith(Room) - Method in class MapGenerator.MainStructure.Room

A C D E F G H K L M N P R S T U V W

Skip navigation links

Overview

Package

Class

Tree

Deprecated

Index

Help

Prev Letter

Next Letter

Frames

No Frames

All Classes


text:
package blackhole_test

import (
	"bufio"
	"net/http"
	"testing"

	"v2ray.com/core/common"
	"v2ray.com/core/common/buf"
	. "v2ray.com/core/proxy/blackhole"
)

func TestHTTPResponse(t *testing.T) {
	buffer := buf.New()

	httpResponse := new(HTTPResponse)
	httpResponse.WriteTo(buf.NewWriter(buffer))

	reader := bufio.NewReader(buffer)
	response, err := http.ReadResponse(reader, nil)
	common.Must(err)
	if response.StatusCode != 403 {
		t.Error("expected status code 403, but got ", response.StatusCode)
	}
}


text:
Node app for over-the-air ios app updates

## local settings

Just put a file called `local_settings.js` in the root folder with overrides from `conf/base.js`

example:
```
'use strict';
var extend = require('extend');
var BASE_SETTINGS = require('./conf/base');
var path = require('path');

var SETTINGS = {};
SETTINGS.DEBUG = true;
SETTINGS.CACHE_TEMPLATES = false;
SETTINGS.SERVER_MODE = 'development';
SETTINGS.SERVE_STATIC = true;
SETTINGS.SERVER_DOMAIN = '127.0.0.1:5001';
SETTINGS.SERVER_URL = 'http://' + SETTINGS.SERVER_DOMAIN;

SETTINGS.PACKAGE_ROOT = __dirname;
SETTINGS.DATA_DIR = path.join(SETTINGS.PACKAGE_ROOT, 'data');

SETTINGS.VALID_BUNDLES = [
  'com.example.testApp'
];

module.exports = extend(BASE_SETTINGS, SETTINGS);
```

## client upload script

There is an example of a .ipa upload script in `client/` which can be used to automate the upload of a package, along with version information.

example use:

```
node upload.js --version="1.0" --build="1" --package="1.0.1" --bundle="com.example.testApp" --name="My App" --ipapath="path to ipa file"
```


text:
<?php

namespace Blog\Repository;

use Blog\Application;
use Doctrine\ORM\EntityRepository;

/**
 * EmployeeRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class EmployeeRepository extends EntityRepository
{
	/**
	 * [findAllEmployees of a company]
	 * @return [array] [Employee object]
	 */
	public function findCompanyEmployees()
	{
		$app = new Application();
		$results = $this->findAll();

		foreach($results as $result)
		{
			$company = $app['repository.company']->findCompanyById($result->getCompanyId());

			$employees[$result->getId()] = [
				'id' => $result->getId(),
				'firstName' => $result->getFirstName(),
				'lastName' => $result->getLastName(),
				'position' => $result->getposition(),
				'phone' => $result->getPhone(),
				'fax' => $result->getFax(),
				'email' => $result->getEmail(),
				'company' => $company
			];
		}
		return $employees;
	}

	/**
	 * Find employee by id
	 * @param  [integer] $id
	 * @return [array] employee object
	 */
	public function findEmployeeById($id)
	{
		$app = new Application();
		$result = $this->findOneBy(['id' => $id]);

		if(!$result) {
			throw new \Exception("Employee not found");
		}

		$company = $app['repository.company']->findCompanyById($result->getCompanyId());

		$employee[$result->getId()] = [
			'id' => $result->getId(),
			'firstName' => $result->getFirstName(),
			'lastName' => $result->getLastName(),
			'position' => $result->getposition(),
			'phone' => $result->getPhone(),
			'fax' => $result->getFax(),
			'email' => $result->getEmail(),
			'company' => $company
		];
		return $employee;
	}

	/**
	 * finds all employees of a company
	 * @param  [integer] $companyId
	 * @return [array] employee objects
	 * @todo  Could be refactored as similar function is used in product repo
 	 */
	public function findEmployeesByCompanyId($companyId)
	{
		$app = new Application();
		$results = $this->createQueryBuilder('e')
			->select('e')
			->where('e.company = :company_id')
			->setParameter('company_id', $companyId)
			->getQuery()
			->getResult();

		foreach($results as $result)
		{
			$employees[] = array(
				'id' => $result->getId(),
				'firstName' => $result->getFirstName(),
				'lastName' => $result->getLastName(),
				'position' => $result->getposition(),
				'phone' => $result->getPhone(),
				'fax' => $result->getFax(),
				'email' => $result->getEmail(),
			);
		}
		return $employees;
	}
}


text:
CREATE TABLE IF NOT EXISTS `[prefix]oauth2_clients` (
	`id` varchar(32) NOT NULL,
	`secret` varchar(32) NOT NULL,
	`name` varchar(255) NOT NULL,
	`domain` varchar(255) NOT NULL,
	`active` tinyint(1) unsigned NOT NULL DEFAULT '0',
	PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `[prefix]oauth2_clients_grant_access` (
	`id` varchar(32) NOT NULL,
	`user` int(10) unsigned NOT NULL COMMENT 'User id',
	PRIMARY KEY (`id`,`user`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `[prefix]oauth2_clients_sessions` (
	`id` varchar(32) NOT NULL COMMENT 'Client id',
	`user` int(10) unsigned NOT NULL COMMENT 'User id',
	`session` varchar(32) NOT NULL,
	`created` bigint(20) unsigned NOT NULL,
	`expire` bigint(20) unsigned NOT NULL,
	`access_token` varchar(32) NOT NULL,
	`refresh_token` varchar(32) NOT NULL,
	`code` varchar(32) NOT NULL,
	`type` set('code','token') NOT NULL DEFAULT 'code',
	`redirect_uri` varchar(32) NOT NULL,
	UNIQUE KEY `access_token` (`access_token`),
	UNIQUE KEY `refresh_token` (`refresh_token`),
	KEY `id` (`id`),
	KEY `user` (`user`),
	KEY `expire` (`expire`),
	KEY `session` (`session`),
	KEY `code` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


text:
#include <config.h>

#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
#include <stdio.h>
#include <sys/types.h>
#ifdef USE_PAM
#include "pam_defs.h"
#endif				/* USE_PAM */
#include <pwd.h>
#include "defines.h"
#include "prototypes.h"
#include "groupio.h"
#ifdef SHADOWGRP
#include "sgroupio.h"
#endif

/* Exit Status Values */
/*@-exitarg@*/
#define EXIT_SUCCESS		0	/* success */
#define EXIT_USAGE		1	/* invalid command syntax */
#define EXIT_GROUP_FILE		2	/* group file access problems */
#define EXIT_NOT_ROOT		3	/* not superuser  */
#define EXIT_NOT_EROOT		4	/* not effective superuser  */
#define EXIT_NOT_PRIMARY	5	/* not primary owner of group  */
#define EXIT_NOT_MEMBER		6	/* member of group does not exist */
#define EXIT_MEMBER_EXISTS	7	/* member of group already exists */
#define EXIT_INVALID_USER	8	/* specified user does not exist */
#define EXIT_INVALID_GROUP	9	/* specified group does not exist */

/*
 * Global variables
 */
const char *Prog;

static char *adduser = NULL;
static char *deluser = NULL;
static char *thisgroup = NULL;
static bool purge = false;
static bool list = false;
static int exclusive = 0;
static bool gr_locked = false;
#ifdef SHADOWGRP
/* Indicate if shadow groups are enabled on the system
 * (/etc/gshadow present) */
static bool is_shadowgrp;
static bool sgr_locked = false;
#endif

/* local function prototypes */
static char *whoami (void);
static void add_user (const char *user,
                      const struct group *grp);
static void remove_user (const char *user,
                         const struct group *grp);
static void purge_members (const struct group *grp);
static void display_members (const char *const *members);
static /*@noreturn@*/void usage (int status);
static void process_flags (int argc, char **argv);
static void check_perms (void);
static void fail_exit (int code);
#define isroot()		(getuid () == 0)

static char *whoami (void)
{
	/* local, no need for xgetgrgid */
	struct group *grp = getgrgid (getgid ());
	/* local, no need for xgetpwuid */
	struct passwd *usr = getpwuid (getuid ());

	if (   (NULL != usr)
	    && (NULL != grp)
	    && (0 == strcmp (usr->pw_name, grp->gr_name))) {
		return xstrdup (usr->pw_name);
	} else {
		return NULL;
	}
}

/*
 * add_user - Add an user to the specified group
 */
static void add_user (const char *user,
                      const struct group *grp)
{
	struct group *newgrp;

	/* Make sure the user is not already part of the group */
	if (is_on_list (grp->gr_mem, user)) {
		fprintf (stderr,
		         _("%s: user '%s' is already a member of '%s'\n"),
		         Prog, user, grp->gr_name);
		fail_exit (EXIT_MEMBER_EXISTS);
	}

	newgrp = __gr_dup(grp);
	if (NULL == newgrp) {
		fprintf (stderr,
		         _("%s: Out of memory. Cannot update %s.\n"),
		         Prog, gr_dbname ());
		fail_exit (13);
	}

	/* Add the user to the /etc/group group */
	newgrp->gr_mem = add_list (newgrp->gr_mem, user);

#ifdef SHADOWGRP
	if (is_shadowgrp) {
		const struct sgrp *sg = sgr_locate (newgrp->gr_name);
		struct sgrp *newsg;

		if (NULL == sg) {
			/* Create a shadow group based on this group */
			static struct sgrp sgrent;
			sgrent.sg_name = xstrdup (newgrp->gr_name);
			sgrent.sg_mem = dup_list (newgrp->gr_mem);
			sgrent.sg_adm = (char **) xmalloc (sizeof (char *));
#ifdef FIRST_MEMBER_IS_ADMIN
			if (sgrent.sg_mem[0]) {
				sgrent.sg_adm[0] = xstrdup (sgrent.sg_mem[0]);
				sgrent.sg_adm[1] = NULL;
			} else
#endif
			{
				sgrent.sg_adm[0] = NULL;
			}

			/* Move any password to gshadow */
			sgrent.sg_passwd = newgrp->gr_passwd;
			newgrp->gr_passwd = SHADOW_PASSWD_STRING;

			newsg = &sgrent;
		} else {
			newsg = __sgr_dup (sg);
			if (NULL == newsg) {
				fprintf (stderr,
				         _("%s: Out of memory. Cannot update %s.\n"),
				         Prog, sgr_dbname ());
				fail_exit (13);
			}
			/* Add the user to the members */
			newsg->sg_mem = add_list (newsg->sg_mem, user);
			/* Do not touch the administrators */
		}

		if (sgr_update (newsg) == 0) {
			fprintf (stderr,
			         _("%s: failed to prepare the new %s entry '%s'\n"),
			         Prog, sgr_dbname (), newsg->sg_name);
			fail_exit (13);
		}
	}
#endif

	if (gr_update (newgrp) == 0) {
		fprintf (stderr,
		         _("%s: failed to prepare the new %s entry '%s'\n"),
		         Prog, gr_dbname (), newgrp->gr_name);
		fail_exit (13);
	}
}

/*
 * remove_user - Remove an user from a given group
 */
static void remove_user (const char *user,
                         const struct group *grp)
{
	struct group *newgrp;

	/* Check if the user is a member of the specified group */
	if (!is_on_list (grp->gr_mem, user)) {
		fprintf (stderr,
		         _("%s: user '%s' is not a member of '%s'\n"),
		         Prog, user, grp->gr_name);
		fail_exit (EXIT_NOT_MEMBER);
	}

	newgrp = __gr_dup (grp);
	if (NULL == newgrp) {
		fprintf (stderr,
		         _("%s: Out of memory. Cannot update %s.\n"),
		         Prog, gr_dbname ());
		fail_exit (13);
	}

	/* Remove the user from the /etc/group group */
	newgrp->gr_mem = del_list (newgrp->gr_mem, user);

#ifdef SHADOWGRP
	if (is_shadowgrp) {
		const struct sgrp *sg = sgr_locate (newgrp->gr_name);
		struct sgrp *newsg;

		if (NULL == sg) {
			/* Create a shadow group based on this group */
			static struct sgrp sgrent;
			sgrent.sg_name = xstrdup (newgrp->gr_name);
			sgrent.sg_mem = dup_list (newgrp->gr_mem);
			sgrent.sg_adm = (char **) xmalloc (sizeof (char *));
#ifdef FIRST_MEMBER_IS_ADMIN
			if (sgrent.sg_mem[0]) {
				sgrent.sg_adm[0] = xstrdup (sgrent.sg_mem[0]);
				sgrent.sg_adm[1] = NULL;
			} else
#endif
			{
				sgrent.sg_adm[0] = NULL;
			}

			/* Move any password to gshadow */
			sgrent.sg_passwd = newgrp->gr_passwd;
			newgrp->gr_passwd = SHADOW_PASSWD_STRING;

			newsg = &sgrent;
		} else {
			newsg = __sgr_dup (sg);
			if (NULL == newsg) {
				fprintf (stderr,
				         _("%s: Out of memory. Cannot update %s.\n"),
				         Prog, sgr_dbname ());
				fail_exit (13);
			}
			/* Remove the user from the members */
			newsg->sg_mem = del_list (newsg->sg_mem, user);
			/* Remove the user from the administrators */
			newsg->sg_adm = del_list (newsg->sg_adm, user);
		}

		if (sgr_update (newsg) == 0) {
			fprintf (stderr,
			         _("%s: failed to prepare the new %s entry '%s'\n"),
			         Prog, sgr_dbname (), newsg->sg_name);
			fail_exit (13);
		}
	}
#endif

	if (gr_update (newgrp) == 0) {
		fprintf (stderr,
		         _("%s: failed to prepare the new %s entry '%s'\n"),
		         Prog, gr_dbname (), newgrp->gr_name);
		fail_exit (13);
	}
}

/*
 * purge_members - Rmeove every members of the specified group
 */
static void purge_members (const struct group *grp)
{
	struct group *newgrp = __gr_dup (grp);

	if (NULL == newgrp) {
		fprintf (stderr,
		         _("%s: Out of memory. Cannot update %s.\n"),
		         Prog, gr_dbname ());
		fail_exit (13);
	}

	/* Remove all the members of the /etc/group group */
	newgrp->gr_mem[0] = NULL;

#ifdef SHADOWGRP
	if (is_shadowgrp) {
		const struct sgrp *sg = sgr_locate (newgrp->gr_name);
		struct sgrp *newsg;

		if (NULL == sg) {
			/* Create a shadow group based on this group */
			static struct sgrp sgrent;
			sgrent.sg_name = xstrdup (newgrp->gr_name);
			sgrent.sg_mem = (char **) xmalloc (sizeof (char *));
			sgrent.sg_mem[0] = NULL;
			sgrent.sg_adm = (char **) xmalloc (sizeof (char *));
			sgrent.sg_adm[0] = NULL;

			/* Move any password to gshadow */
			sgrent.sg_passwd = newgrp->gr_passwd;
			newgrp->gr_passwd = xstrdup(SHADOW_PASSWD_STRING);

			newsg = &sgrent;
		} else {
			newsg = __sgr_dup (sg);
			if (NULL == newsg) {
				fprintf (stderr,
				         _("%s: Out of memory. Cannot update %s.\n"),
				         Prog, sgr_dbname ());
				fail_exit (13);
			}
			/* Remove all the members of the /etc/gshadow
			 * group */
			newsg->sg_mem[0] = NULL;
			/* Remove all the administrators of the
			 * /etc/gshadow group */
			newsg->sg_adm[0] = NULL;
		}

		if (sgr_update (newsg) == 0) {
			fprintf (stderr,
			         _("%s: failed to prepare the new %s entry '%s'\n"),
			         Prog, sgr_dbname (), newsg->sg_name);
			fail_exit (13);
		}
	}
#endif

	if (gr_update (newgrp) == 0) {
		fprintf (stderr,
		         _("%s: failed to prepare the new %s entry '%s'\n"),
		         Prog, gr_dbname (), newgrp->gr_name);
		fail_exit (13);
	}
}

static void display_members (const char *const *members)
{
	int i;

	for (i = 0; NULL != members[i]; i++) {
		printf ("%s ", members[i]);

		if (NULL == members[i + 1]) {
			printf ("\n");
		} else {
			printf (" ");
		}
	}
}

static /*@noreturn@*/void usage (int status)
{
	FILE *usageout = (EXIT_SUCCESS != status) ? stderr : stdout;
	(void) fprintf (usageout,
	                _("Usage: %s [options] [action]\n"
	                  "\n"
	                  "Options:\n"),
	                Prog);
	(void) fputs (_("  -g, --group groupname         change groupname instead of the user's group\n"
	                "                                (root only)\n"), usageout);
	(void) fputs (_("  -R, --root CHROOT_DIR         directory to chroot into\n"), usageout);
	(void) fputs (_("\n"), usageout);
	(void) fputs (_("Actions:\n"), usageout);
	(void) fputs (_("  -a, --add username            add username to the members of the group\n"), usageout);
	(void) fputs (_("  -d, --delete username         remove username from the members of the group\n"), usageout);
	(void) fputs (_("  -h, --help                    display this help message and exit\n"), usageout);
	(void) fputs (_("  -p, --purge                   purge all members from the group\n"), usageout);
	(void) fputs (_("  -l, --list                    list the members of the group\n"), usageout);
	exit (status);
}

/*
 * process_flags - perform command line argument setting
 */
static void process_flags (int argc, char **argv)
{
	int c;
	static struct option long_options[] = {
		{"add",    required_argument, NULL, 'a'},
		{"delete", required_argument, NULL, 'd'},
		{"group",  required_argument, NULL, 'g'},
		{"help",   no_argument,       NULL, 'h'},
		{"list",   no_argument,       NULL, 'l'},
		{"purge",  no_argument,       NULL, 'p'},
		{"root",   required_argument, NULL, 'R'},
		{NULL, 0, NULL, '\0'}
	};

	while ((c = getopt_long (argc, argv, "a:d:g:hlpR:",
	                         long_options, NULL)) != EOF) {
		switch (c) {
		case 'a':
			adduser = xstrdup (optarg);
			++exclusive;
			break;
		case 'd':
			deluser = xstrdup (optarg);
			++exclusive;
			break;
		case 'g':
			thisgroup = xstrdup (optarg);
			break;
		case 'h':
			usage (EXIT_SUCCESS);
			/*@notreached@*/break;
		case 'l':
			list = true;
			++exclusive;
			break;
		case 'p':
			purge = true;
			++exclusive;
			break;
		case 'R': /* no-op, handled in process_root_flag () */
			break;
		default:
			usage (EXIT_USAGE);
		}
	}

	if ((exclusive > 1) || (optind < argc)) {
		usage (EXIT_USAGE);
	}

	/* local, no need for xgetpwnam */
	if (   (NULL != adduser)
	    && (getpwnam (adduser) == NULL)) {
		fprintf (stderr, _("%s: user '%s' does not exist\n"),
		         Prog, adduser);
		fail_exit (EXIT_INVALID_USER);
	}

}

static void check_perms (void)
{
	if (!list) {
#ifdef USE_PAM
		pam_handle_t *pamh = NULL;
		int retval;
		struct passwd *pampw;

		pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
		if (NULL == pampw) {
			fprintf (stderr,
			         _("%s: Cannot determine your user name.\n"),
			         Prog);
			fail_exit (1);
		}

		retval = pam_start ("groupmems", pampw->pw_name, &conv, &pamh);

		if (PAM_SUCCESS == retval) {
			retval = pam_authenticate (pamh, 0);
		}

		if (PAM_SUCCESS == retval) {
			retval = pam_acct_mgmt (pamh, 0);
		}

		if (PAM_SUCCESS != retval) {
			fprintf (stderr, _("%s: PAM: %s\n"),
			         Prog, pam_strerror (pamh, retval));
			SYSLOG((LOG_ERR, "%s", pam_strerror (pamh, retval)));
			if (NULL != pamh) {
				(void) pam_end (pamh, retval);
			}
			fail_exit (1);
		}
		(void) pam_end (pamh, retval);
#endif
	}
}

static void fail_exit (int code)
{
	if (gr_locked) {
		if (gr_unlock () == 0) {
			fprintf (stderr,
			         _("%s: failed to unlock %s\n"),
			         Prog, gr_dbname ());
			SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
			/* continue */
		}
	}

#ifdef SHADOWGRP
	if (sgr_locked) {
		if (sgr_unlock () == 0) {
			fprintf (stderr,
			         _("%s: failed to unlock %s\n"),
			         Prog, sgr_dbname ());
			SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
			/* continue */
		}
	}
#endif

	exit (code);
}

static void open_files (void)
{
	if (!list) {
		if (gr_lock () == 0) {
			fprintf (stderr,
			         _("%s: cannot lock %s; try again later.\n"),
			         Prog, gr_dbname ());
			fail_exit (EXIT_GROUP_FILE);
		}
		gr_locked = true;

#ifdef SHADOWGRP
		if (is_shadowgrp) {
			if (sgr_lock () == 0) {
				fprintf (stderr,
				         _("%s: cannot lock %s; try again later.\n"),
				         Prog, sgr_dbname ());
				fail_exit (EXIT_GROUP_FILE);
			}
			sgr_locked = true;
		}
#endif
	}

	if (gr_open (list ? O_RDONLY : O_RDWR) == 0) {
		fprintf (stderr, _("%s: cannot open %s\n"), Prog, gr_dbname ());
		fail_exit (EXIT_GROUP_FILE);
	}

#ifdef SHADOWGRP
	if (is_shadowgrp) {
		if (sgr_open (list ? O_RDONLY : O_RDWR) == 0) {
			fprintf (stderr, _("%s: cannot open %s\n"), Prog, sgr_dbname ());
			fail_exit (EXIT_GROUP_FILE);
		}
	}
#endif
}

static void close_files (void)
{
	if ((gr_close () == 0) && !list) {
		fprintf (stderr, _("%s: failure while writing changes to %s\n"), Prog, gr_dbname ());
		SYSLOG ((LOG_ERR, "failure while writing changes to %s", gr_dbname ()));
		fail_exit (EXIT_GROUP_FILE);
	}
	if (gr_locked) {
		if (gr_unlock () == 0) {
			fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
			SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
			/* continue */
		}
		gr_locked = false;
	}

#ifdef SHADOWGRP
	if (is_shadowgrp) {
		if ((sgr_close () == 0) && !list) {
			fprintf (stderr, _("%s: failure while writing changes to %s\n"), Prog, sgr_dbname ());
			SYSLOG ((LOG_ERR, "failure while writing changes to %s", sgr_dbname ()));
			fail_exit (EXIT_GROUP_FILE);
		}
		if (sgr_locked) {
			if (sgr_unlock () == 0) {
				fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
				SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
				/* continue */
			}
			sgr_locked = false;
		}
	}
#endif
}

int main (int argc, char **argv)
{
	char *name;
	const struct group *grp;

	/*
	 * Get my name so that I can use it to report errors.
	 */
	Prog = Basename (argv[0]);

	(void) setlocale (LC_ALL, "");
	(void) bindtextdomain (PACKAGE, LOCALEDIR);
	(void) textdomain (PACKAGE);

	process_root_flag ("-R", argc, argv);

	OPENLOG ("groupmems");

#ifdef SHADOWGRP
	is_shadowgrp = sgr_file_present ();
#endif

	process_flags (argc, argv);

	if (NULL == thisgroup) {
		name = whoami ();
		if (!list && (NULL == name)) {
			fprintf (stderr, _("%s: your groupname does not match your username\n"), Prog);
			fail_exit (EXIT_NOT_PRIMARY);
		}
	} else {
		name = thisgroup;
		if (!list && !isroot ()) {
			fprintf (stderr, _("%s: only root can use the -g/--group option\n"), Prog);
			fail_exit (EXIT_NOT_ROOT);
		}
	}

	check_perms ();

	open_files ();

	grp = gr_locate (name);
	if (NULL == grp) {
		fprintf (stderr, _("%s: group '%s' does not exist in %s\n"),
		         Prog, name, gr_dbname ());
		fail_exit (EXIT_INVALID_GROUP);
	}

	if (list) {
		display_members ((const char *const *)grp->gr_mem);
	} else if (NULL != adduser) {
		add_user (adduser, grp);
	} else if (NULL != deluser) {
		remove_user (deluser, grp);
	} else if (purge) {
		purge_members (grp);
	}

	close_files ();

	exit (EXIT_SUCCESS);
}


text:
@class GKTurnBasedMatchmakerViewController;
@class GKTurnBasedMatch;
@class NSError;

@protocol GKTurnBasedMatchmakerViewControllerDelegate <NSObject>
@optional
- (void)turnBasedMatchmakerViewController:(GKTurnBasedMatchmakerViewController*)viewController didFindMatch:(GKTurnBasedMatch*)match;
- (void)turnBasedMatchmakerViewController:(GKTurnBasedMatchmakerViewController*)viewController playerQuitForMatch:(GKTurnBasedMatch*)match;

@required
- (void)turnBasedMatchmakerViewController:(GKTurnBasedMatchmakerViewController*)viewController didFailWithError:(NSError*)error;
- (void)turnBasedMatchmakerViewControllerWasCancelled:(GKTurnBasedMatchmakerViewController*)viewController;
@end


text:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>client-parent</artifactId>
        <groupId>com.hyrax.client</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hyrax.client.email</groupId>
    <artifactId>email-client</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>client-api</module>
        <module>client-api-impl</module>
        <module>client-spring-boot-autoconfigure</module>
        <module>client-spring-boot-starter</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.hyrax.client.email</groupId>
                <artifactId>email-client-api</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.hyrax.client.email</groupId>
                <artifactId>email-client-api-impl</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.hyrax.client.email</groupId>
                <artifactId>email-client-spring-boot-autoconfigure</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>


text:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <com.brightspotinnovations.me2.view.MoodColorChanger
            android:id="@+id/orange"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:padding="4dip"/>

        <com.brightspotinnovations.me2.view.MoodColorChanger
            android:id="@+id/yellow"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:padding="4dip"/>

        <com.brightspotinnovations.me2.view.MoodColorChanger
            android:id="@+id/green"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:padding="4dip"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <com.brightspotinnovations.me2.view.MoodColorChanger
            android:id="@+id/pink"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:padding="4dip"/>

        <com.brightspotinnovations.me2.view.MoodColorChanger
            android:id="@+id/blue"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:padding="4dip"/>

        <com.brightspotinnovations.me2.view.MoodColorChanger
            android:id="@+id/gray"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:padding="4dip"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <com.brightspotinnovations.me2.view.MoodColorChanger
            android:id="@+id/purple"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:padding="4dip"/>

        <com.brightspotinnovations.me2.view.MoodColorChanger
            android:id="@+id/black"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:padding="4dip"/>

        <com.brightspotinnovations.me2.view.MoodColorChanger
            android:id="@+id/red"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:padding="4dip"/>

    </LinearLayout>

</LinearLayout>


text:
#ifndef OSL_SCATNAMES_H
# define OSL_SCATNAMES_H

# include <stdio.h>
# include <osl/interface.h>
# include <osl/strings.h>

# if defined(__cplusplus)
extern "C"
  {
# endif

# define OSL_URI_SCATNAMES        "scatnames"

/**
 * The osl_scatnames_t structure stores a scatnames extention to the core
 * OpenScop representation. It is devoted to store the scattering dimension
 * names in a textual form. The names are stored using an osl_strings_t
 * structure and their order in the array corresponds to their dimension
 * order.
 */
struct osl_scatnames {
  osl_strings_p names;  /**< List of textual scattering dimension names. */
};
typedef struct osl_scatnames   osl_scatnames_t;
typedef struct osl_scatnames * osl_scatnames_p;

/*+***************************************************************************
 *                          Structure display function                       *
 *****************************************************************************/
void            osl_scatnames_idump(FILE *, osl_scatnames_p, int);
void            osl_scatnames_dump(FILE *, osl_scatnames_p);
char *          osl_scatnames_sprint(osl_scatnames_p);

/*****************************************************************************
 *                               Reading function                            *
 *****************************************************************************/
osl_scatnames_p osl_scatnames_sread(char **);

/*+***************************************************************************
 *                    Memory allocation/deallocation function                *
 *****************************************************************************/
osl_scatnames_p osl_scatnames_malloc(void);
void            osl_scatnames_free(osl_scatnames_p);

/*+***************************************************************************
 *                            Processing functions                           *
 *****************************************************************************/
osl_scatnames_p osl_scatnames_clone(osl_scatnames_p);
int             osl_scatnames_equal(osl_scatnames_p, osl_scatnames_p);
osl_interface_p osl_scatnames_interface(void);

# if defined(__cplusplus)
  }
# endif

#endif /* define OSL_SCATNAMES_H */


text:
/* Hans Pabst (Intel Corp.)
******************************************************************************/
#ifndef LIBXSTREAM_HPP
#define LIBXSTREAM_HPP

#include <libxstream.h>

#if defined(LIBXSTREAM_EXPORTED) || defined(__LIBXSTREAM)

/** Data type representing a signal. */
typedef uintptr_t libxstream_signal;

typedef void libxstream_lock;
LIBXSTREAM_TARGET(mic) libxstream_lock* libxstream_lock_create();
LIBXSTREAM_TARGET(mic) libxstream_lock* libxstream_lock_get(const volatile void* address);
LIBXSTREAM_TARGET(mic) void libxstream_lock_destroy(libxstream_lock* lock);
LIBXSTREAM_TARGET(mic) void libxstream_lock_acquire(libxstream_lock* lock);
LIBXSTREAM_TARGET(mic) void libxstream_lock_release(libxstream_lock* lock);
LIBXSTREAM_TARGET(mic) bool libxstream_lock_try(libxstream_lock* lock);

LIBXSTREAM_TARGET(mic) size_t nthreads_active();
LIBXSTREAM_TARGET(mic) int this_thread_id();
LIBXSTREAM_TARGET(mic) void this_thread_yield();
LIBXSTREAM_TARGET(mic) void this_thread_sleep(size_t ms = LIBXSTREAM_SLEEP_MS);
LIBXSTREAM_TARGET(mic) void this_thread_wait(size_t& cycle);

/**
 * The following flags are complimentary to libxstream_call_flags,
 * but considered internal and not to be exposed in the public API.
 */
enum {
  LIBXSTREAM_CALL_DEVICE    = ( 2 * (LIBXSTREAM_CALL_INVALID - 1)),
  LIBXSTREAM_CALL_LOOP      = ( 4 * (LIBXSTREAM_CALL_INVALID - 1)),
  LIBXSTREAM_CALL_EVENT     = ( 8 * (LIBXSTREAM_CALL_INVALID - 1)),
  LIBXSTREAM_CALL_EXTERNAL  = (16 * (LIBXSTREAM_CALL_INVALID - 1))
};

#endif // defined(LIBXSTREAM_EXPORTED) || defined(__LIBXSTREAM)
#endif // LIBXSTREAM_HPP


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"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.5"/>
<title>ADC: README.md Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>

<link href="navtree.css" rel="stylesheet" type="text/css"/>

<link href="search/search.css" rel="stylesheet" type="text/css"/>

<link href="doxygen.css" rel="stylesheet" type="text/css" />

ADC
    1.0

Main Page

Related Pages

Classes

Files

          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)"
               onblur="searchBox.OnSearchFieldFocus(false)"
               onkeyup="searchBox.OnSearchFieldChange(event)"/>

File List

File Members

 All Classes Files Functions Variables Macros Pages

README.md

Go to the documentation of this file.

    1 ADC

    2 ===

    3

    4 Teensy 3.0 ADC implementation library created by Pedro Villanueva.

    5

    6 It implements all functions of the Teensy 3.0 internal ADC.

    7

    8 Some of the code was extracted from the file analog.c included in the distribution of Teensduino by PJRC.COM.

    9

   10 See example folder.

   11

   12 TODO:

   13   * Stuff related to the conversion speed and power. Useful if you want more speed or to save power.

ADC

Definition: ADC.h:70

README.md

Generated on Sun Mar 16 2014 11:05:55 for ADC by

     1.8.5


text:
""" comment
export DJANGO_SETTINGS_MODULE="opentrain.settings"
"""
import os
os.environ['DJANGO_SETTINGS_MODULE']='opentrain.settings'
#/home/oferb/docs/train_project/OpenTrains/webserver
import timetable.services
import analysis.models
import numpy as np
import itertools
import datetime
import algorithm.shapes as shapes
import algorithm.stops as stops
import ot_utils

FAKE_BSSID_PREFIX = 'FAKE'

def generate_mock_reports(device_id='fake_device_1', trip_id='010714_00115', day=None, from_stop_id=None, to_stop_id=None, nostop_percent=0.2, station_radius_in_meters=300):
    trip = timetable.services.get_trip(trip_id)
    # TODO(oferb):
    stop_times = trip.get_stop_times()
    if not day:
        day = ot_utils.get_localtime_now()
        print 'WARNING: Note that there may be time zone issues'
    else:
        day = ot_utils.get_localtime(day)

    # filter stop times by from_stop_id and to_stop_id:
    trip_stop_ids = [x.stop.gtfs_stop_id for x in stop_times]
    from_stop_ind = 0
    if from_stop_id:
        from_stop_ind = trip_stop_ids.index(from_stop_id)
    to_stop_ind = None
    if to_stop_id:
        to_stop_ind = trip_stop_ids.index(to_stop_id)+1
    stop_times = stop_times[from_stop_ind:to_stop_ind]
    trip_stop_ids = set([x.stop.gtfs_stop_id for x in stop_times])

    # get coords:
    coords = trip.get_points()
    #coords = np.array(coords)
    accuracies = np.ones((len(coords),1))*ot_utils.meter_distance_to_coord_distance(station_radius_in_meters)

    # map shape-points to stops:
    stop_ids = stops.all_stops.query_stops(coords, accuracies)

    # filter out stops not in trip:
    stop_ids_uniques = set(stop_ids)
    if len(trip_stop_ids - stop_ids_uniques) > 0:
        unfound_stops = trip_stop_ids - stop_ids_uniques
        print('Warning: these stops were not found in gtfs shape: %s' % (unfound_stops))
    stops_not_in_trip = stop_ids_uniques - trip_stop_ids - set([stops.NOSTOP_ID])
    stop_ids = [x if x not in stops_not_in_trip else stops.NOSTOP_ID for x in stop_ids]

    # strip nostops from start and end
    while len(stop_ids) > 0 and stop_ids[0] == stops.NOSTOP_ID:
        del stop_ids[0]
    while len(stop_ids) > 0 and stop_ids[-1] == stops.NOSTOP_ID:
        del stop_ids[-1]

    # remove nostop reports according to nostop_percent:
    nostop_inds = [i for i in xrange(len(stop_ids)) if stop_ids[i] == stops.NOSTOP_ID]
    keep_inds = [i for i in xrange(len(stop_ids)) if stop_ids[i] != stops.NOSTOP_ID]
    nostop_inds = nostop_inds[::int(1/nostop_percent)]
    keep_inds.extend(nostop_inds)
    stop_ids = [stop_ids[i] for i in xrange(len(stop_ids)) if i in keep_inds]
    coords = [coords[i] for i in xrange(len(coords)) if i in keep_inds]

    stop_id_to_stop_time_dict = {}
    for stop_time in stop_times:
        stop_id_to_stop_time_dict[stop_time.stop.gtfs_stop_id] = stop_time

    from itertools import groupby
    stop_id_groups = []
    stop_id_group_lens = []
    stop_id_unique_keys = []
    for k, g in groupby(stop_ids):
        g = list(g)
        stop_id_groups.append(g)      # Store group iterator as a list
        stop_id_group_lens.append(len(g))
        stop_id_unique_keys.append(k)

    # create reports with corresponding location and wifi:
    reports = []
    last_stop = None
    prev_stop_id = None
    stop_index = -1
    counter = -1
    group_index = -1
    for coord, stop_id, i in zip(coords, stop_ids, xrange(len(stop_ids))):
        if stop_id != prev_stop_id:
            counter = -1
            group_index += 1
            if stop_id != stops.NOSTOP_ID:
                stop_index += 1
                interval_start = stop_times[stop_index].exp_arrival
                interval_end = stop_times[stop_index].exp_departure
                #print '%s %s' % (stop_id, stop_times[stop_index].stop.stop_id)
            else:
                interval_start = stop_times[stop_index].exp_departure
                interval_end = stop_times[stop_index+1].exp_arrival
        #print i, stop_id
        counter += 1
        interval_ratio = float(counter)/stop_id_group_lens[group_index]
        timestamp = interval_start + datetime.timedelta(seconds=interval_ratio*(interval_end-interval_start).total_seconds())
        #timestamp = day.replace(hour=timestamp/3600, minute=timestamp % 3600 / 60, second=timestamp % 60 / 60)

        report = analysis.models.Report()
        reports.append(report)
        loc = analysis.models.LocationInfo()
        #loc.report = report
        loc.accuracy = 0.1
        loc.lat = coord[0]
        loc.lon = coord[1]
        loc.provider = 'mock'
        loc.timestamp = timestamp
        report.my_loc_mock = loc

        wifi_report_train = analysis.models.SingleWifiReport()
        wifi_report_train.report = report
        wifi_report_train.SSID = TRAIN_SSID
        wifi_report_train.frequency = -13
        wifi_report_train.signal = 13
        wifi_report_train.key = '%s_%s' % (FAKE_BSSID_PREFIX, device_id)
        report.wifi_set_mock = [wifi_report_train]

        if stop_id != stops.NOSTOP_ID:
            wifi_report_station = analysis.models.SingleWifiReport()
            wifi_report_station.report = report
            wifi_report_station.SSID = STATION_SSID
            wifi_report_station.frequency = 12
            wifi_report_station.signal = -12
            wifi_report_station.key = 'FAKE_%s' % (stop_id)
            report.wifi_set_mock.append(wifi_report_station)

        report.device_id = device_id
        report.timestamp = timestamp
        report.created = timestamp

        report.device_id = device_id

        prev_stop_id = stop_id

    return reports

#general
STATION_SSID = 'S-ISRAEL-RAILWAYS'
TRAIN_SSID = 'ISRAEL-RAILWAYS'

def raw_report_from_report(report):
    rr = dict()
    items = []
    fake_timestamp = int(ot_utils.dt_time_to_unix_time(report.timestamp)*1000)
    item = dict()
    item['app_version_name'] = u'0.7.6'
    item['app_version_code'] = 18
    item['time'] = fake_timestamp
    item['device_id'] = report.device_id
    wifis = []
    for wifi in report.get_wifi_set_all():
        w = dict()
        w['signal'] = wifi.signal
        w['frequency'] = wifi.frequency
        w['SSID'] = wifi.SSID
        w['key'] = wifi.key
        wifis.append(w)
    item['wifi'] = wifis
    loc = report.get_my_loc()
    if loc:
        l = dict()
        l['bearing'] = 0
        l['altitude'] = 0
        l['provider'] = loc.provider
        l['long'] = loc.lon
        l['lat'] = loc.lat
        l['time'] = fake_timestamp
        l['accuracy'] = loc.accuracy
        item['location_api'] = l
    items.append(item)
    rr['items'] = items
    return rr

if __name__ == '__main__':
    reports = generate_mock_reports(nostop_percent=0.05)
    print len(reports)


text:
class DogPostNetworkService {
  constructor($http, $state, $q, API_URL) {
    'ngInject';

    const self = this;

    self.DOGS_URL = `${API_URL}/dogs`;
    self.REGIONS_URL = `${API_URL}/regions`;
    self.GENDERS_URL = `${API_URL}/genders`;
    self.STATUSES_URL = `${API_URL}/dogs/statuses`;
    self.SIZES_URL = `${API_URL}/sizes`;

    self._$http = $http;
    self._$state = $state;
    self._$q = $q;
  }

  postDog(dogModel) {
    const self = this;

    return self._$http.put(self.DOGS_URL, dogModel, {headers: {'Accept': 'application/json', 'Content-Type': 'application/json'}})
      .then(data => {
        self._$state.go('dogs');
      })
      .catch(error => {
        console.log(error);
      });
  }

  getSpecificModelData(modelPath) {
    const self = this;

    return self._$http.get(modelPath);
  }
}

export default DogPostNetworkService;


text:
"use strict";

define(['domain/pages', 'views/entity/page', 'views/entity/list/list'], function (pages, Page, List) {

    var namespace = 'brands';

    var NewList = List.extend({
        initialize: function () {
            this.$el.html('Переопределили метод');
        }
    });

    var View = Page.extend({
        List: NewList,
        batya: function() {
            this.$el.html('asdf');
        }
    });

    return pages.add({
        namespace: namespace,
        parent: 'directory',
        icon: 'glyphicon-tags',
        caption: 'Бренды',
        View: View
    });
});


text:
#import "NSObject.h"

@class IMAttributedStringParserContext;

@interface IMAttributedStringParser : NSObject
{
    IMAttributedStringParserContext *_context;
}

+ (id)sharedInstance;
- (void)parseContext:(id)arg1;
- (void)_preprocessWithContext:(id)arg1 string:(id *)arg2;

@end


text:
The Batch Pattern
=================

The batch pattern allows for a tradeoff to be made between the number of queries
required versus the amount of memory required when processing large amounts of
data that is coming from multiple tables of a relational database.

## An Example Problem

We are tracking a database of music.  We might have tables
for:
 - Artists
 - Albums
 - Songs

If we were to want to display a list of artists and the albums they released,
how might we do this?

### N+1 Technique

We could request and iterate through all artists.  For each artist iterated,
we could then request and iterate through all albums associated with the
individual artist.

In terms of actually coding that, a simple way to do this processing might be:

```php
$artist_sql = <<<ARTIST_SQL
SELECT *
FROM artists
ORDER BY last_name, first_name
ARTIST_SQL;

$album_sql = <<<ALBUM_SQL
SELECT *
FROM albums
WHERE artist_id = ?artist_id?
ORDER BY title
ALBUM_SQL;

$artist_results = $db->query($artist_sql);
while ($artist = $artist_results->fetch()) {
    echo "{$artist['last_name']}, {$artist['first_name']}\n";

    $album_results = $db->query($album_sql, ['artist_id' => $artist['id']]);
    while ($album = $album_results->fetch()) {
        echo "> {$album['title']}\n";
    }
}
```

The problem with this solution is that it does not scale.  Known as the
"N+1 Problem", for every artist added to the database, another query is going
to need to be executed to retrieve that artist's albums.  Even with prepared
statements, each query executed will result in more latency during the network
roundtrip plus the overhead of executing the prepared statement one more time.

The "N+1 problem" is amplified even further when there are more tables that
need to be queried per iteration.  Think about if you wanted to get a list of
songs for each of the albums iterated.  For each artist, you need to execute
1 query to retrieve the list of albums plus `M` queries to retrieve the songs
(1 query for each of the artist's `M` albums).  Overall, you end up running
`N*M` queries, which with a large number of artist and album records will become
a problem quickly.

### Bulk Loading Technique

An alternative that allows the "N+1 problem" to be avoided is to load the needed
data in bulk:

```php
$artist_sql = <<<ARTIST_SQL
SELECT *
FROM artists
ORDER BY last_name, first_name
ARTIST_SQL;

$album_sql = <<<ALBUM_SQL
SELECT *
FROM albums
ORDER BY title
ALBUM_SQL;

$album_results = $db->query($album_sql);
$albums_by_artist_id = [];
while ($album = $album_results->fetch()) {
    $albums_by_artist_id[$album['artist_id']][$album['id']] = $album;
}

$artists = $db->query($artist_sql);
while ($artist = $artists->fetch()) {
    echo "{$artist['last_name']}, {$artist['first_name']}\n";

    if (!isset($albums_by_artist_id[$artist['id']])) {
        continue;
    }

    foreach ($albums_by_artist_id[$artist['id']] as $album) {
        echo "> {$album['title']}\n";
    }
}
```

This solution, like the N+1 solution, works well for small data sets.  However,
as the data set grows, the amount of memory the bulk load solution requires
grows in a non-scaleable way.  The amount of memory consumed for such a solution
can easily get into the hundreds of megabytes and even into the gigabytes,
depending on the data set size.

## A Solution: The Batch Pattern

The batch pattern allows for a tradeoff to be made between the number of queries
required versus the amount of memory required when processing large amounts of
data that is coming from multiple tables of a relational database.

The batch pattern works by breaking the large data sets into smaller data sets.
Each smaller data set is then processed using the bulk loading technique.

### Generating Batches

In Postgres a simple and efficient way to generate batches is to create a
temporary table containing the primary IDs of the primary data element and an
auto-generated batch number:

```php
$batch_seq_sql = <<<SEQUENCE_SQL
CREATE TEMPORARY SEQUENCE artist_batches_seq
CACHE 10
SEQUENCE_SQL;

$batch_table_sql = <<<TABLE_SQL
CREATE TEMPORARY TABLE artist_batches
(
    batch_num INTEGER NOT NULL
        DEFAULT (nextval('artist_batches_seq'::regclass) / ?batch_size?::integer) + 1,
    artist_id INTEGER NOT NULL
)
TABLE_SQL;

$batch_populator_sql = <<<POPULATOR_SQL
INSERT INTO artist_batches
(
    artist_id
)
SELECT id
FROM artists
ORDER BY last_name, first_name
POPULATOR_SQL;

$batch_index_sql = <<<INDEX_SQL
CREATE UNIQUE INDEX batch_num_id_idx
    ON artist_batches
    USING btree (batch_num, artist_id);
CREATE UNIQUE INDEX id_idx
    ON artist_batches
    USING btree (artist_id);
INDEX_SQL;

$db->query($batch_seq_sql);
$db->query($batch_table_sql, ['batch_size' => 250]);
$db->query($batch_populator_sql);
$db->query($batch_index_sql);
```

### Processing Batches

Once the batches are generated, we can start iterating through the batches
fairly easily by grabbing the minimum and maximum batch numbers:

```php
$batch_num_sql = <<<BATCH_NUM_SQL
SELECT
    MIN(batch_num) AS min_batch_num,
    MAX(batch_num) AS max_batch_num
FROM artist_batches
BATCH_NUM_SQL;

$batch_nums = $db->getRow($batch_num_sql);
$min_batch = $batch_nums['min_batch_num'];
$max_batch = $batch_nums['max_batch_num'];

if ($min_batch) {
    for ($batch_num = $min_batch; $batch_num <= $max_batch; $batch_num++) {
        $this->processBatch($batch_num);
    }
}
```

### Process a Single Batch

Now that we are iterating through the batches, it is time we do something
with a batch.  We can adapt some of the logic we used in the bulk loading
technique to work with the batch pattern:

```php
// private function processBatch($batch_num)

$artist_sql = <<<ARTIST_SQL
SELECT *
FROM artists
WHERE EXISTS (
        SELECT 1
        FROM artist_batches batches
        WHERE batches.batch_num = ?batch_num?
            AND batches.artist_id = artists.id
    )
ORDER BY last_name, first_name
ARTIST_SQL;

$album_sql = <<<ALBUM_SQL
SELECT *
FROM albums
WHERE EXISTS (
        SELECT 1
        FROM artist_batches batches
        WHERE batches.batch_num = ?batch_num?
            AND batches.artist_id = albums.artist_id
    )
ORDER BY title
ALBUM_SQL;

$album_results = $db->query($album_sql, ['batch_num' => $batch_num]);
$albums_by_artist_id = [];
while ($album = $album_results->fetch()) {
    $albums_by_artist_id[$album['artist_id']][$album['id']] = $album;
}

$artists = $db->query($artist_sql, ['batch_num' => $batch_num]);
while ($artist = $artists->fetch()) {
    echo "{$artist['last_name']}, {$artist['first_name']}\n";

    if (!isset($albums_by_artist_id[$artist['id']])) {
        continue;
    }

    foreach ($albums_by_artist_id[$artist['id']] as $album) {
        echo "> {$album['title']}\n";
    }
}
```

## Choose Your Tradeoff

Once the batch processing is in place, the tradeoff between the number of queries
versus memory consumption can be fine tuned.

Fine tuning is as simple as benchmarking timing and memory consumption statistics
when changing the size of the batches.  In most cases, the limitation is
available memory.  In this case, we can decrease the size of the batches until
each batch can safely run without hitting the memory limit.

Keep in mind that setting a batch size to infinity is very similar to using
the "Bulk Loading Technique", in which everything is loaded into memory.  Setting
the batch size to one is very similar to using the "N+1 Technique", which
requires individual queries for every primary data element.


text:
rmw_ret_t
copy_services_to_names_and_types(
  const std::map<std::string, std::set<std::string>> & services,
  rcutils_allocator_t * allocator,
  rmw_names_and_types_t * service_names_and_types);

rmw_ret_t
copy_topics_names_and_types(
  const std::map<std::string, std::set<std::string>> & topics,
  rcutils_allocator_t * allocator,
  bool no_demangle,
  rmw_names_and_types_t * topic_names_and_types);

#endif  // RMW_CONNEXT_SHARED_CPP__NAMES_AND_TYPES_HELPERS_HPP_