text:
#include "LAYOUT.h"
#include <PhysBAM_Geometry/Read_Write/Grids_Uniform_Level_Sets/READ_WRITE_FAST_LEVELSET.h>
#include <PhysBAM_Geometry/Read_Write/Grids_Uniform_Level_Sets/READ_WRITE_LEVELSET_1D.h>
#include <PhysBAM_Geometry/Read_Write/Grids_Uniform_Level_Sets/READ_WRITE_LEVELSET_2D.h>
#include <PhysBAM_Geometry/Read_Write/Implicit_Objects_Uniform/READ_WRITE_LEVELSET_IMPLICIT_OBJECT.h>

using namespace PhysBAM;
//#####################################################################
// LAYOUT Constructor
//#####################################################################
template<class TV>
LAYOUT<TV>::LAYOUT(const STREAM_TYPE stream_type_input)
    :stream_type(stream_type_input),initial_time(0),first_frame(0),last_frame(100),frame_rate(24),
    restart(0),write_debug_data(false),output_directory("output"),cfl(.9),mac_grid(TV_INT(),RANGE<TV>::Unit_Box(),true),
    projection(mac_grid,false,false,thread_queue),advection_scalar(thread_queue),
levelset(mac_grid,*new ARRAY<T,TV_INT>())
{}
//#####################################################################
// CFL or courant number, determines size of time step due to grid spacing
//#####################################################################
template<class TV> typename TV::SCALAR LAYOUT<TV>::
CFL(ARRAY<T,FACE_INDEX<TV::dimension> >& face_velocities)
{
    T dt=FLT_MAX;
    DOMAIN_ITERATOR_THREADED_ALPHA<LAYOUT<TV>,TV>(mac_grid.Domain_Indices(),thread_queue).template Run<ARRAY<T,FACE_INDEX<TV::dimension> >&,T&>(*this,&LAYOUT::CFL_Threaded,face_velocities,dt);
    return dt;
}
//#####################################################################
// LAYOUT destructor, empty because stub function, not needed
//#####################################################################
template<class TV>
LAYOUT<TV>::~LAYOUT()
{}
//#####################################################################
// get time at input frame based on frame rate
//#####################################################################
T Time_At_Frame(const int frame) const
{return initial_time+(frame-first_frame)/frame_rate;}

template<class TV>
void LAYOUT<TV>::Initialize()
{}

template<class TV>
typename TV::SCALAR LAYOUT<TV>::Maximum_Dt() const
{
    return 1e-3;
}
//#####################################################################
// Set_Boundary_Conditions
//#####################################################################
template<class TV> void LAYOUT<TV>::
Set_Boundary_Conditions(const T time)
{
    projection.elliptic_solver->psi_D.Fill(false);
    projection.elliptic_solver->psi_N.Fill(false);

    for(int axis=1;axis<=TV::dimension;axis++) for(int axis_side=1;axis_side<=2;axis_side++)
    {
        int side=2*(axis-1)+axis_side;//making a value for side...
//if axis_side==1 then interior_cell_offset=TV_INT() else -TV_INT
        TV_INT interior_cell_offset, exterior_cell_offset, boundary_face_offset;
        if(axis_side==1)
        {
                interior_cell_offset = TV_INT();
                exterior_cell_offset = -TV_INT::Axis_Vector(axis);
                boundary_face_offset = TV_INT::Axis_Vector(axis);
        }
        else
        {
                interior_cell_offset = -TV_INT::Axis_Vector(axis);
                exterior_cell_offset = TV_INT();
                boundary_face_offset = -TV_INT::Axis_Vector(axis);
        }

        if(domain_boundary(axis)(axis_side))
        {//this is a boundary
            for(typename GRID<TV>::FACE_ITERATOR iterator(mac_grid,1,GRID<TV>::BOUNDARY_REGION,side);iterator.Valid();iterator.Next())
            {
                TV_INT face=iterator.Face_Index()+boundary_face_offset;
                if(levelset.phi(face+interior_cell_offset)<=0)//checks if level set phi is less than or equal to zero(not water) at boundary face
                {

                    if(face_velocities.Component(axis).Valid_Index(face))//is this a valid position for face velocities? if so
                        {
                        projection.elliptic_solver->psi_N.Component(axis)(face)=true;//pressure solver is looking for a valid normal component
                        face_velocities.Component(axis)(face)=0;//no slip velocity condition
                        }
                }
                else
                {
                TV_INT cell=face+exterior_cell_offset;
                projection.elliptic_solver->psi_D(cell)=true;
                projection.p(cell)=0;
                }
            }
        }
        else
        {
                for(typename GRID<TV>::FACE_ITERATOR iterator(mac_grid,1,GRID<TV>::BOUNDARY_REGION,side);iterator.Valid();iterator.Next())
                        {
                        TV_INT cell=iterator.Face_Index()+interior_cell_offset;
                        projection.elliptic_solver->psi_D(cell)=true;
                        projection.p(cell)=0;
                        }
        }
    }
    for(typename GRID<TV>::FACE_ITERATOR iterator(mac_grid);iterator.Valid();iterator.Next())
    {
     /*  if(time<=3 && source.Lazy_Inside(iterator.Location()))
        {
            projection.elliptic_solver->psi_N(iterator.Full_Index())=true;
            if(iterator.Axis()==1) face_velocities(iterator.Full_Index())=1;
            else face_velocities(iterator.Full_Index())=0;
        }*/
    }
    for(typename GRID<TV>::CELL_ITERATOR iterator(projection.p_grid);iterator.Valid();iterator.Next())
    {
        if(levelset.phi(iterator.Cell_Index())>0)
        {
        projection.elliptic_solver->psi_D(iterator.Cell_Index())=true;
        projection.p(iterator.Cell_Index())=0;
        }
    }
    if(projection.elliptic_solver->mpi_grid){
        projection.elliptic_solver->mpi_grid->Exchange_Boundary_Cell_Data(projection.elliptic_solver->psi_D,1,false);
        projection.elliptic_solver->mpi_grid->Exchange_Boundary_Cell_Data(projection.p,1,false);}
}

//#####################################################################
//
//#####################################################################

template<class TV>
void LAYOUT<TV>::Write_Output(const int frame) const
{
    std::string f=STRING_UTILITIES::string_sprintf("%d",frame);
    FILE_UTILITIES::Write_To_File(stream_type,output_directory+"/"+f+"/mac_velocities",face_velocities);
    std::string output_directory="output";
    FILE_UTILITIES::Create_Directory("output/"+STRING_UTILITIES::Value_To_String(frame));
    FILE_UTILITIES::Write_To_File(stream_type,output_directory+"/"+f+"/levelset",levelset);
    //FILE_UTILITIES::Write_To_File(stream_type,output_directory+"/"+f+"/grid",mac_grid);
    //FILE_UTILITIES::Write_To_File(stream_type,output_directory+"/common/grid",mac_grid);
    FILE_UTILITIES::Write_To_File(stream_type,output_directory+"/"+f+"/levelset",levelset);
    if(write_debug_data){
        FILE_UTILITIES::Write_To_File(stream_type,output_directory+"/"+f+"/pressure",projection.p);
        FILE_UTILITIES::Write_To_File(stream_type,output_directory+"/"+f+"/psi_N",projection.elliptic_solver->psi_N);
        FILE_UTILITIES::Write_To_File(stream_type,output_directory+"/"+f+"/psi_D",projection.elliptic_solver->psi_D);}

}
template<class TV> void LAYOUT<TV>::
Read_Output_Files(const int frame)
{
    std::string f=STRING_UTILITIES::string_sprintf("%d",frame);
    FILE_UTILITIES::Read_From_File(stream_type,output_directory+"/"+f+"/levelset",levelset);
    std::string filename;
    filename=output_directory+"/"+f+"/mac_velocities";
    if(FILE_UTILITIES::File_Exists(filename)){LOG::cout<<"Reading mac_velocities "<<filename<<std::endl;FILE_UTILITIES::Read_From_File(stream_type,filename,face_velocities);}
    filename=output_directory+"/"+f+"/pressure";
    if(FILE_UTILITIES::File_Exists(filename)){LOG::cout<<"Reading pressure "<<filename<<std::endl;FILE_UTILITIES::Read_From_File(stream_type,filename,projection.p);}
}

//
template class LAYOUT<VECTOR<float,2> >;
template class LAYOUT<VECTOR<float,3> >;


text:
'use strict';

// Dependencies
declare var require;
var fs:any = require('fs');
var glob:any = require('glob');
var path:any = require('path');
var handlebars:any = require('handlebars');

// Helpers
module validate {

    /* Check a file exists */
    export function exists(grunt:any, target:any):boolean {
        if (!grunt.file.exists(target)) {
            grunt.log.warn('Invalid path: ' + target);
            return false;
        }
        return true;
    }

    /* Check if a target is a directory for not */
    export function is_dir(grunt, target:any):boolean {
        if (!grunt.file.isDir(target)) {
            grunt.log.warn('Invalid path: ' + target + ' is not a directory');
            return false;
        }
        return true;
    }
}

function task(grunt) {

    grunt.registerMultiTask('x_combine', 'A template based grunt file combiner.', function () {

        // Merge task-specific and/or target-specific options with these defaults.
        var options = this.options({
            prefix: '',
            postfix: ''
        });

        // Iterate over all specified file groups.
        this.files.forEach(function (f) {

            // Validate config
            if (!validate.exists(grunt, f.dest)) { return false; }
            if (!validate.exists(grunt, f.template)) { return false; }

            // Compile template
            var source = grunt.file.read(f.template);
            var template = handlebars.compile(source);

            f.src.forEach(function (target) {

                // Validate file target
                if (!validate.exists(grunt, target)) { return false; }
                if (!validate.is_dir(grunt, target)) { return false; }

                // Find all subtarget in the directory
                var matches = glob.sync(target+"/*", {sync: true});
                var keys = matches.map((x) => { return path.basename(x).replace(/\./g, '_'); });

                // Generate an output name
                var fragments = target.split('/');
                var output_target = fragments[fragments.length - 1];
                var output_path = f.dest + '/' + options.prefix + output_target + options.postfix;
                var output_name = options.prefix + output_target;
                var output_id = output_name.replace('-', '_');

                // Generate template dictionary
                var data = {
                    prefix: options.prefix,
                    postfix: options.postfix,
                    id: output_id,
                    name: output_name,
                    path: output_path
                };
                for (var i = 0; i < matches.length; ++i) {
                    var tmp = grunt.file.read(matches[i]).trim();
                    var tmptmp = handlebars.compile(tmp);
                    data[keys[i]] = tmptmp(data);
                }

                // Generate an output file from the template
                var output_raw = template(data);
                grunt.file.write(output_path, output_raw);

                return true;
            });
            // grunt.file.write(f.dest, src);
            // Print a success message.
            //grunt.log.writeln('File "' + f.dest + '" created.');
            return true;
        });
    });
}
export = task;

redpajama_set_name: RedPajamaGithub


text:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PoshSec.Framework.PShell
{
    class psfilenameeditor : System.Drawing.Design.UITypeEditor
    {
        public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return System.Drawing.Design.UITypeEditorEditStyle.Modal;
        }

        [RefreshProperties(System.ComponentModel.RefreshProperties.All)]
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (context == null || context.Instance == null)
            {
                return base.EditValue(context, provider, value);
            }
            else
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Title = "Select " + context.PropertyDescriptor.DisplayName;
                dlg.FileName = (string)value;
                dlg.Filter = "All Files (*.*)|*.*";
                dlg.CheckFileExists = false;
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    value = dlg.FileName;
                }
                dlg.Dispose();
                dlg = null;
                return value;
            }
        }
    }
}


text:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using VoidEngine.Helpers;

namespace VoidEngine.VGame
{
	/// <summary>
	/// The sprite class for VoidEngine.
	/// </summary>
	public class Sprite
	{
		/// <summary>
		/// The sprites animation properties.
		/// </summary>
		public struct AnimationSet
		{
			/// <summary>
			/// The name of the animation.
			/// </summary>
			public string name;
			/// <summary>
			/// The texture of the sprite sheet.
			/// </summary>
			public Texture2D texture;
			/// <summary>
			/// The size of the frame on the sprite sheet.
			/// </summary>
			public Point frameSize;
			/// <summary>
			/// The amount of the frames in the animation
			/// </summary>
			public Point sheetSize;
			/// <summary>
			/// The starting cordinates of the animation of the spritesheet.
			/// </summary>
			public Point startPosition;
			/// <summary>
			/// The tick of the animation.
			/// </summary>
			public int framesPerMillisecond;
			/// <summary>
			///
			/// </summary>
			public bool isLooping;

			/// <summary>
			/// For creating a new animation set.
			/// </summary>
			/// <param name="name">The name of the animation.</param>
			/// <param name="texture">The texture of the sprite sheet.</param>
			/// <param name="frameSize">The size of the frame on the sprite sheet.</param>
			/// <param name="sheetSize">The amount of frames in the animation.</param>
			/// <param name="startPosition">The starting cordinates of the animation on the spritesheet.</param>
			/// <param name="framesPerMillisecond">The tick of the animation.</param>
			public AnimationSet(string name, Texture2D texture, Point frameSize, Point sheetSize, Point startPosition, int framesPerMillisecond, bool isLooping)
			{
				this.name = name;
				this.texture = texture;
				this.frameSize = frameSize;
				this.sheetSize = sheetSize;
				this.startPosition = startPosition;
				this.framesPerMillisecond = framesPerMillisecond;
				this.isLooping = isLooping;
			}
		}

		/// <summary>
		/// The axis of the sprite to flip at.
		/// </summary>
		protected float speed;
		public enum Axis
		{
			X,
			Y,
			NONE
		}

		#region Animations
		/// <summary>
		/// Gets the current animation that the sprite is set to.
		/// </summary>
		public AnimationSet CurrentAnimation;
		/// <summary>
		/// Gets or sets the animation sets.
		/// Can only be set by child or self.
		/// </summary>
		public List<AnimationSet> AnimationSets
		{
			get;
			protected set;
		}
		/// <summary>
		/// The current frame that the sprite is at in its animation.
		/// </summary>
		public Point CurrentFrame;
		/// <summary>
		/// Gets or sets the animations frame tick time.
		/// </summary>
		protected int LastFrameTime
		{
			get;
			set;
		}
		/// <summary>
		/// Gets or sets the SpriteEffects value of the sprite.
		/// </summary>
		protected SpriteEffects FlipEffect
		{
			get;
			set;
		}
		/// <summary>
		/// Gets if the sprite is flipped.
		/// </summary>
		public bool isFlipped
		{
			get
			{
				if (FlipEffect != SpriteEffects.None)
				{
					return true;
				}

				return false;
			}
		}
		/// <summary>
		/// The offset position of the sprite.
		/// </summary>
		public Vector2 Offset = Vector2.Zero;
		/// <summary>
		/// Gets ors sets the scale factor of the sprite.
		/// </summary>
		public float Scale
		{
			get;
			set;
		}
		#endregion
		#region Movement
		/// <summary>
		/// The position that the player is at.
		/// Can only be used by child or self.
		/// </summary>
		protected Vector2 position;
		/// <summary>
		/// Gets or sets the position that the sprite is at.
		/// </summary>
		public Vector2 Position
		{
			get
			{
				return position;
			}
			set
			{
				position = value;
			}
		}
		/// <summary>
		/// Gets or sets the rotation of the sprite.
		/// Can only be set by child or self.
		/// Use values between 0 and 1.
		/// Ex. 'Rotation = $Degree$ * (float)Math.PI / 180;'
		/// </summary>
		public float Rotation
		{
			get;
			protected set;
		}
		/// <summary>
		/// The point to move the sprite from.
		/// Other wise known as the origin.
		/// </summary>
		public Vector2 RotationCenter = Vector2.Zero;
		#endregion

		/// <summary>
		/// Gets the bounding Collisions.
		/// </summary>
		public Rectangle BoundingCollisions
		{
			get
			{
				int left = (int)Math.Round(Position.X) + inbounds.X;
				int top = (int)Math.Round(Position.Y) + inbounds.Y;

				return new Rectangle(left, top, inbounds.Width, inbounds.Height);
			}
		}
		/// <summary>
		/// Gets or sets the inner bounds of the player.
		/// </summary>
		public Rectangle Inbounds
		{
			get
			{
				return inbounds;
			}
			set
			{
				inbounds = value;
			}
		}
		/// <summary>
		/// The inner bounds of the player.
		/// </summary>
		protected Rectangle inbounds;
		/// <summary>
		/// Gets or sets the sprites color.
		/// Can be only set by child or self.
		/// </summary>
		public Color Color
		{
			get;
			protected set;
		}

		/// <summary>
		/// Creates a sprite.
		/// </summary>
		/// <param name="position">The stating position of the sprite.</param>
		/// <param name="color">The color to mask the sprite with.</param>
		/// <param name="animationSetList">The animation set of the sprite.</param>
		public Sprite(Vector2 position, Color color, List<AnimationSet> animationSetList)
		{
			AnimationSets = new List<AnimationSet>();
			AnimationSets = animationSetList;
			this.position = position;
			LastFrameTime = 0;
			Color = color;
			Scale = 1f;
		}

		/// <summary>
		/// Creates a sprite.
		/// </summary>
		/// <param name="position">The stating position of the sprite.</param>
		/// <param name="color">The color to mask the sprite with.</param>
		/// <param name="texture">The texture of the sprite.</param>
		public Sprite(Vector2 position, Color color, Texture2D texture)
		{
			AnimationSets = new List<AnimationSet>();
			this.position = position;
			LastFrameTime = 0;
			Color = color;
			Scale = 1f;
		}

		/// <summary>
		/// Update's the sprites frames.
		/// To make custom update functions, add the sprite class as
		/// a child of a class. Be sure to use 'using VoidEngine;'.
		/// </summary>
		/// <param name="gameTime">The game time that the game runs off of.</param>
		public virtual void Update(GameTime gameTime)
		{
			HandleAnimations(gameTime);
		}

		/// <summary>
		/// Used to draw the sprite.
		/// Put inbetween the spriteBatch.Begin and spriteBatch.End
		/// </summary>
		/// <param name="gameTime">The game time, that the game runs off of.</param>
		/// <param name="spriteBatch">The sprite batch to draw from.</param>
		public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
		{
			spriteBatch.Draw(CurrentAnimation.texture, position - Offset, new Rectangle(CurrentAnimation.startPosition.X + (CurrentFrame.X * CurrentAnimation.frameSize.X), CurrentAnimation.startPosition.Y + (CurrentFrame.Y * CurrentAnimation.frameSize.Y), CurrentAnimation.frameSize.X, CurrentAnimation.frameSize.Y), Color, Rotation, RotationCenter, Scale, FlipEffect, 0);
		}

		/// <summary>
		/// Sets the current animation based off of it's name.
		/// </summary>
		/// <param name="setName">The name of the animation to set the sprite to.</param>
		public void SetAnimation(string setName)
		{
			if (CurrentAnimation.name != setName)
			{
				foreach (AnimationSet a in AnimationSets)
				{
					if (a.name == setName)
					{
						CurrentAnimation = a;
						CurrentFrame = Point.Zero;
					}
				}
			}
		}

		/// <summary>
		/// Flips the sprite texture based off a bool and axis.
		/// To flip back turn isFlip to false or use the second version.
		/// </summary>
		/// <param name="axis">The axis to flip on</param>
		protected void FlipSprite(Axis axis)
		{
			if (axis == Axis.Y)
			{
				FlipEffect = SpriteEffects.FlipHorizontally;
			}
			if (axis == Axis.X)
			{
				FlipEffect = SpriteEffects.FlipVertically;
			}
			if (axis == Axis.NONE)
			{
				FlipEffect = SpriteEffects.None;
			}
		}

		/// <summary>
		/// Handles the animations for the sprite class.
		/// </summary>
		/// <param name="gameTime">The GameTime that the game is running off of.</param>
		protected virtual void HandleAnimations(GameTime gameTime)
		{
			LastFrameTime += gameTime.ElapsedGameTime.Milliseconds;

			if (LastFrameTime >= CurrentAnimation.framesPerMillisecond)
			{
				CurrentFrame.X++;

				if (CurrentFrame.X >= CurrentAnimation.sheetSize.X)
				{
					CurrentFrame.Y++;
					if (CurrentAnimation.isLooping)
					{
						CurrentFrame.X = 0;
					}
					else
					{
						CurrentFrame.X = CurrentAnimation.sheetSize.X;
					}

					if (CurrentFrame.Y >= CurrentAnimation.sheetSize.Y)
					{
						CurrentFrame.Y = 0;
					}
				}

				LastFrameTime = 0;
			}
		}

		/// <summary>
		/// Another way to add an animation indiviually.
		/// </summary>
		/// <param name="name">The name of the animation set.</param>
		/// <param name="texture">The sprite sheet of the animation.</param>
		/// <param name="frameSize">The frame size of the animation.</param>
		/// <param name="sheetSize">The sheet size of the animation.</param>
		/// <param name="startPosition">The start position of the animation.</param>
		/// <param name="framesPerMillisecond">The frames per milliscond between each frame.</param>
		/// <param name="isLooping">Sets if the animation loops.</param>
		protected virtual void AddAnimation(string name, Texture2D texture, Point frameSize, Point sheetSize, Point startPosition, int framesPerMillisecond, bool isLooping)
		{
			AnimationSets.Add(new AnimationSet(name, texture, frameSize, sheetSize, startPosition, framesPerMillisecond, isLooping));
		}

		/// <summary>
		/// Another way to add the animations to the sprites.
		/// </summary>
		/// <param name="texture">The texture of the sprite.</param>
		protected virtual void AddAnimations(Texture2D texture)
		{
		}

		public static void DrawBoundingCollisions(Texture2D line, Rectangle boundingCollisions, Color color, SpriteBatch spriteBatch)
		{
			spriteBatch.Draw(line, new Rectangle(boundingCollisions.X, boundingCollisions.Y, boundingCollisions.Width, 1), color);
			spriteBatch.Draw(line, new Rectangle(boundingCollisions.Right - 1, boundingCollisions.Y, 1, boundingCollisions.Height), color);
			spriteBatch.Draw(line, new Rectangle(boundingCollisions.X, boundingCollisions.Bottom - 1, boundingCollisions.Width, 1), color);
			spriteBatch.Draw(line, new Rectangle(boundingCollisions.X, boundingCollisions.Y, 1, boundingCollisions.Height), color);
		}

		public bool CheckInRadius(Vector2 position, float radius)
		{
			if (CollisionHelper.Magnitude(position - this.Position) < radius)
			{
				return true;
			}

			return false;
		}
	}
}


text:
<?
namespace Saja\AssetsLoader\Latte\Macros;

use Latte\Macros\MacroSet;
use Latte\MacroNode;
use Latte\PhpWriter;
use Latte\Compiler;

use Saja\AssetsLoader\AssetsLoader;

class AssetMacros extends MacroSet
{
    public static function install(Compiler $compiler)
    {
        $me = new static($compiler);
        $me->addMacro('asset', array($me, 'macroAsset'));
    }

    public function macroAsset(MacroNode $node, PhpWriter $writer){
        switch($node->args){
            case 'css':
                return AssetsLoader::getInstance()->renderCss();
            case 'js':
                return AssetsLoader::getInstance()->renderJs();
        }
        return null;
    }
}


text:
import sys
import time
import h5py
import scipy as sp
from scipy import linalg
from ldpred import util
from ldpred import ld
from ldpred import reporting
from ldpred import coord_genotypes

def ldpred_inf(beta_hats, h2=0.1, n=1000, inf_shrink_matrices=None,
               reference_ld_mats=None, genotypes=None, ld_window_size=100, verbose=False):
    """
    Apply the infinitesimal shrink w LD (which requires LD information).

    If reference_ld_mats are supplied, it uses those, otherwise it uses the LD in the genotype data.

    If genotypes are supplied, then it assumes that beta_hats and the genotypes are synchronized.

    """
    n = float(n)
    if verbose:
        print('Doing LD correction')
    t0 = time.time()
    m = len(beta_hats)
    updated_betas = sp.empty(m)

    for i, wi in enumerate(range(0, m, ld_window_size)):
        start_i = wi
        stop_i = min(m, wi + ld_window_size)
        curr_window_size = stop_i - start_i
        if inf_shrink_matrices!=None:
            A_inv = inf_shrink_matrices[i]
        else:
            if reference_ld_mats != None:
                D = reference_ld_mats[i]
            else:
                if genotypes != None:
                    X = genotypes[start_i: stop_i]
                    num_indivs = X.shape[1]
                    D = sp.dot(X, X.T) / num_indivs
                else:
                    raise NotImplementedError
            A = ((m / h2) * sp.eye(curr_window_size) + (n / (1.0)) * D)
            A_inv = linalg.pinv(A)
        updated_betas[start_i: stop_i] = sp.dot(A_inv * n , beta_hats[start_i: stop_i])  # Adjust the beta_hats

        if verbose:
            sys.stdout.write('\r%0.2f%%' % (100.0 * (min(1, float(wi + ld_window_size) / m))))
            sys.stdout.flush()

    t1 = time.time()
    t = (t1 - t0)
    if verbose:
        print('\nIt took %d minutes and %0.2f seconds to perform the Infinitesimal LD shrink' % (t / 60, t % 60))
    return updated_betas

def ldpred_inf_genomewide(data_file=None, ld_radius = None, ld_dict=None, out_file_prefix=None,
                          n=None, h2=None, use_gw_h2=False, verbose=False, summary_dict=None):
    """
    Calculate LDpred for a genome
    """

    df = h5py.File(data_file,'r')
    has_phenotypes = 'y' in df
    if has_phenotypes:
        y = df['y'][...]  # Phenotype
        num_individs = len(y)
        risk_scores_pval_derived = sp.zeros(num_individs)

    ld_scores_dict = ld_dict['ld_scores_dict']
    chrom_ref_ld_mats = ld_dict['chrom_ref_ld_mats']

    print('Applying LDpred-inf with LD radius: %d' % ld_radius)
    results_dict = {}
    cord_data_g = df['cord_data']

    mean_n = coord_genotypes.get_mean_sample_size(n, cord_data_g)

    #Calculating genome-wide heritability using LD score regression, and partition heritability by chromsomes
    herit_dict = ld.get_chromosome_herits(cord_data_g, ld_scores_dict, mean_n, h2=h2, use_gw_h2=use_gw_h2,
                                          debug=verbose,summary_dict=summary_dict)

    if out_file_prefix:
        #Preparing output files
        raw_effect_sizes = []
        ldpred_effect_sizes = []
        sids = []
        chromosomes = []
        positions = []
        nts = []

    for chrom_str in util.chromosomes_list:
        if chrom_str in cord_data_g:
            g = cord_data_g[chrom_str]
            if has_phenotypes:
                if 'raw_snps_val' in g:
                    raw_snps = g['raw_snps_val'][...]
                else:
                    raw_snps = g['raw_snps_ref'][...]

            pval_derived_betas = g['betas'][...]
            if out_file_prefix:
                chromosomes.extend([chrom_str]*len(pval_derived_betas))
                positions.extend(g['positions'][...])
                sids_arr = (g['sids'][...]).astype(util.sids_u_dtype)
                sids.extend(sids_arr)
                raw_effect_sizes.extend(g['log_odds'][...])
                nts_arr = (g['nts'][...]).astype(util.nts_u_dtype)
                nts.extend(nts_arr)

            h2_chrom = herit_dict[chrom_str]['h2']
            updated_betas = ldpred_inf(pval_derived_betas, genotypes=None, reference_ld_mats=chrom_ref_ld_mats[chrom_str],
                                                h2=h2_chrom, n=mean_n, ld_window_size=2*ld_radius, verbose=False)

            if verbose:
                print('Calculating scores for Chromosome %s'%((chrom_str.split('_'))[1]))
            snp_stds = g['snp_stds_ref'][...]
            updated_betas = updated_betas / (snp_stds.flatten())
            ldpred_effect_sizes.extend(updated_betas)
            if has_phenotypes:
                prs = sp.dot(updated_betas, raw_snps)
                risk_scores_pval_derived += prs
                corr = sp.corrcoef(y, prs)[0, 1]
                r2 = corr ** 2
                if verbose:
                    print('The R2 prediction accuracy of PRS using %s was: %0.4f' %(chrom_str, r2))

    if has_phenotypes:
        num_indivs = len(y)
        results_dict['y']=y
        results_dict['risk_scores_pd']=risk_scores_pval_derived
        print('Prediction accuracy was assessed using %d individuals.'%(num_indivs))

        corr = sp.corrcoef(y, risk_scores_pval_derived)[0, 1]
        r2 = corr ** 2
        results_dict['r2_pd']=r2
        print('The  R2 prediction accuracy (observed scale) for the whole genome was: %0.4f (%0.6f)' % (r2, ((1-r2)**2)/num_indivs))

        if corr<0:
            risk_scores_pval_derived = -1* risk_scores_pval_derived
        auc = util.calc_auc(y,risk_scores_pval_derived)
        print('AUC for the whole genome was: %0.4f'%auc)

        #Now calibration
        denominator = sp.dot(risk_scores_pval_derived.T, risk_scores_pval_derived)
        y_norm = (y-sp.mean(y))/sp.std(y)
        numerator = sp.dot(risk_scores_pval_derived.T, y_norm)
        regression_slope = (numerator / denominator)
        print('The slope for predictions with P-value derived  effects is: %0.4f'%regression_slope)
        results_dict['slope_pd']=regression_slope

    weights_out_file = '%s_LDpred-inf.txt' % (out_file_prefix)
    with open(weights_out_file,'w') as f:
        f.write('chrom    pos    sid    nt1    nt2    raw_beta    ldpred_inf_beta\n')
        for chrom, pos, sid, nt, raw_beta, ldpred_beta in zip(chromosomes, positions, sids, nts, raw_effect_sizes, ldpred_effect_sizes):
            nt1,nt2 = nt[0],nt[1]
            f.write('%s    %d    %s    %s    %s    %0.4e    %0.4e\n'%(chrom, pos, sid, nt1, nt2, raw_beta, ldpred_beta))

def main(p_dict):

    summary_dict = {}
    summary_dict[0]={'name':'Coordinated data filename','value':p_dict['cf']}
    summary_dict[0.1]={'name':'SNP weights output file (prefix)', 'value':p_dict['out']}
    summary_dict[0.2]={'name':'LD data filename (prefix)', 'value':p_dict['ldf']}
    summary_dict[1]={'name':'LD radius used','value':str(p_dict['ldr'])}
    t0 = time.time()
    summary_dict[1.09]={'name':'dash', 'value':'LD information'}
    ld_dict = ld.get_ld_dict_using_p_dict(p_dict, summary_dict={})
    t1 = time.time()
    t = (t1 - t0)
    summary_dict[1.2]={'name':'Running time for calculating LD information:','value':'%d min and %0.2f secs'% (t / 60, t % 60)}
    t0 = time.time()
    summary_dict[1.9]={'name':'dash', 'value':'LDpred infinitesimal model'}

    ldpred_inf_genomewide(data_file=p_dict['cf'], out_file_prefix=p_dict['out'], ld_radius=p_dict['ldr'],
                          ld_dict = ld_dict, n=p_dict['N'], h2=p_dict['h2'], use_gw_h2=p_dict['use_gw_h2'],
                          verbose=p_dict['debug'], summary_dict=summary_dict)
    t1 = time.time()
    t = (t1 - t0)
    summary_dict[2.2]={'name':'Running time for LDpred-inf:','value':'%d min and %0.2f secs'% (t / 60, t % 60)}
    reporting.print_summary(summary_dict, 'Summary of LDpred-inf')


text:
"""Google Cloud Game Servers sample for updating the rollout of a game
server deployment to remove the override config.

Example usage:
    python update_rollout_remove_override.py --project-id <project-id> --deployment-id <deployment-id>
"""

import argparse

from google.cloud import gaming
from google.cloud.gaming_v1.types import game_server_deployments
from google.protobuf import field_mask_pb2 as field_mask  # type: ignore

# [START cloud_game_servers_deployment_rollout_remove_override]
def update_rollout_remove_override(project_id, deployment_id):
    """Update the rollout of a game server deployment to remove the override config."""

    client = gaming.GameServerDeploymentsServiceClient()

    # Location is hard coded as global, as game server deployments can
    # only be created in global.  This is done for all operations on
    # game Server deployments, as well as for its child resource types.
    request = game_server_deployments.UpdateGameServerDeploymentRolloutRequest()
    request.rollout.name = (
        f"projects/{project_id}/locations/global/gameServerDeployments/{deployment_id}"
    )
    request.rollout.game_server_config_overrides = []
    request.update_mask = field_mask.FieldMask(paths=["game_server_config_overrides"])

    operation = client.update_game_server_deployment_rollout(request)
    print(f"Update deployment rollout operation: {operation.operation.name}")
    operation.result(timeout=120)

# [END cloud_game_servers_deployment_rollout_remove_override]

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--project-id", help="Your cloud project ID.", required=True)
    parser.add_argument(
        "--deployment-id", help="Your game server deployment ID.", required=True
    )

    args = parser.parse_args()

    update_rollout_remove_override(args.project_id, args.deployment_id)


text:
exports.authenticate = function authenticate(userDoc,req,res,next) {
  //NOTE: this could maybe be smarter (eg. tying user doc in DB to session
  //instead of copying it on login)
  req.session.currentUser = {
    username: userDoc.username,
    email: userDoc.email,
    _id: userDoc._id
  };
  if (next) next();
};

exports.unauthenticate = function unauthenticate(req,res,next) {
  delete req.session.currentUser;
  if (next) next();
};


text:
<?php

namespace NS\SentinelBundle\Repository;

use Doctrine\ORM\QueryBuilder;
use NS\SentinelBundle\Repository\Common as CommonRepository;

class CountryRepository extends CommonRepository
{
    /**
     * @param null $codes
     * @param bool $includeInactive
     * @return array
     */
    public function getChain($codes = null, $includeInactive = false): array
    {
        $queryBuilder = $this->getChainQueryBuilder($includeInactive);

        if (is_array($codes)) {
            $queryBuilder->andWhere($queryBuilder->expr()->in('c.code', '?1'))->setParameter(1, $codes);
        } elseif ($codes !== null) {
            $queryBuilder->andWhere('c.code = :code')->setParameter('code', $codes);
        }

        return $queryBuilder->getQuery()->getResult();
    }

    /**
     *
     * @param bool $includeInactive
     * @return QueryBuilder
     */
    public function getChainQueryBuilder($includeInactive = false): QueryBuilder
    {
        $queryBuilder = $this->_em
            ->createQueryBuilder()
            ->from($this->getEntityName(), 'c', 'c.code')
            ->select('c,r')
            ->innerJoin('c.region', 'r');

        if (!$includeInactive) {
            $queryBuilder->where('c.active = :isActive')->setParameter('isActive', true);
        }

        return $this->secure($queryBuilder);
    }

    /**
     * @param $alias
     * @param $caseClass
     * @return QueryBuilder
     */
    public function getWithCasesForDate($alias, $caseClass): QueryBuilder
    {
        return $this->secure($this->_em->createQueryBuilder()
            ->select("cf, $alias, s, c, r, COUNT(IDENTITY($alias)) as totalCases")
            ->from($caseClass, 'cf')
            ->innerJoin('cf.referenceLab', $alias)
            ->leftJoin('cf.site', 's')
            ->innerJoin('cf.country', 'c')
            ->innerJoin('c.region', 'r')
            ->groupBy('cf.country')
            ->addOrderBy('r.name', 'ASC')
            ->addOrderBy('c.name', 'ASC'));
    }
}


text:
addCommandHandler ( "mdel", function ( player, _, modDir, modName )
    if not hasRights ( player ) then
        return false
    end

    if type ( modDir ) ~= "string" or type ( modName ) ~= "string" then
        outputDebugString ( "Please enter valid arguments! use 'mdel [moddir] [modname]'!" )
        return false
    end

    local txd = modDir.."/"..modName..".txd"
    local dff = modDir.."/"..modName..".dff"
    local hnd = modDir.."/"..modName..".hnd"

    if fileExists ( txd ) then
        fileDelete ( txd )
        xmlDestroyNode ( mods[txd] )
        outputDebugString ( "Deleted TXD in "..modDir.." for item "..modName )
    end

    if fileExists ( dff ) then
        fileDelete ( dff )
        xmlDestroyNode ( mods[dff] )
        outputDebugString ( "Deleted DFF in "..modDir.." for item "..modName )
    end

    if fileExists ( hnd ) then
        fileDelete ( hnd )
        outputDebugString ( "Deleted handling in "..modDir.." for item "..modName )
    end

    outputDebugString ( "Deleted mod "..modName..". Restarting resource.." )
    restartResource ( )

    return true
end )

addCommandHandler ( "mclear", function ( player, _, modDir )
    if not hasRights ( player ) then
        return false
    end

    if type ( modDir ) ~= "string" then
        outputDebugString ( "Please enter valid arguments! use 'mclear [moddir]'!" )
        return false
    end

    local tab
    if modDir == "vehicles" then
        tab = validVehicleModels
    elseif modDir == "weapons" then
        tab = validWeaponModels
    elseif modDir == "skins" then
        tab = validPedModels
    else
        outputDebugString ( "Please use a valid mod directory! Valids are: 'vehicles', 'weapons', 'skins'." )
        return false
    end

    for modelName,modelID in pairs ( tab ) do
        local txd = modDir.."/"..modelName..".txd"
        local dff = modDir.."/"..modelName..".dff"
        local hnd = modDir.."/"..modelName..".hnd"

        if fileExists ( txd ) then
            fileDelete ( txd )
            xmlDestroyNode ( mods[txd] )
            outputDebugString ( "Deleted TXD in "..modDir.." for item "..modelName )
        end

        if fileExists ( dff ) then
            fileDelete ( dff )
            xmlDestroyNode ( mods[dff] )
            outputDebugString ( "Deleted DFF in "..modDir.." for item "..modelName )
        end

        if fileExists ( hnd ) then
            fileDelete ( hnd )
            outputDebugString ( "Deleted handling in "..modDir.." for item "..modelName )
        end
    end

    outputDebugString ( "Cleared mod directory "..modDir..". Restarting resource.." )
    restartResource ( )

    return true
end )

function hasRights ( player )
    if getElementType ( player ) == "console" then
        return true
    end

    local pAccount = getPlayerAccount ( player )
    if isGuestAccount ( pAccount ) then
        return false
    end

    if isObjectInACLGroup ( "user."..pAccount, aclGetGroup ( "admin" ) ) then
        return true
    end

    return false
end


text:
[![Build status](https://ci.appveyor.com/api/projects/status/f4h9ga7grda1mt4v?svg=true)](https://ci.appveyor.com/project/finalstream/finalstreamcommons-core) [![NuGet](https://img.shields.io/nuget/v/FinalstreamCommons.Core.svg?style=plastic)](https://www.nuget.org/packages/FinalstreamCommons.Core/) [![GitHub license](https://img.shields.io/github/license/finalstream/FinalstreamCommons.Core.svg)]()

Windowsアプリケーション開発をサポートするコアライブラリです。
ほかのライブラリに依存しません。

##主な機能

###Builders
* CommandLineBuilder(コマンドライン文字列を生成)

##Comparers
* NaturalComparer(エクスプローラと同じソートを行う)

##Extentions
* CollectionExtentions(2つのコレクションの差分更新)

###Utils
* ColorUtils(反対色などを取得)
* DateUtils(シーズン取得)
* DialogUtils(ファイル選択、フォルダ選択ダイアログ)
* ScreenUtils(デバイス名からディスプレイデバイス情報取得)

##Systems
* DynamicLinkLibraryInfo(DLLの情報を取得)

###Windows
* GlobalMouseHook(グローバルエリアのマウスの動きをフック。ダブルクリック判定&デスクトップ判定可能)
* ClipboardMonitor(クリップボード監視)


text:
using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Windows.Forms;
using System.Collections.Generic;

using SharpVectors.Xml;
using SharpVectors.Dom.Svg;
using SharpVectors.Dom.Events;
using SharpVectors.Dom.Stylesheets;

namespace SharpVectors.Renderers.Forms
{
    public class SvgPictureBoxWindow : SvgWindow
    {
        #region Private fields

        private SvgPictureBox _svgPictureBox;

        #endregion

        #region Contructors and Destructor

        public SvgPictureBoxWindow(long innerWidth, long innerHeight, ISvgRenderer renderer)
            : base(innerWidth, innerHeight, renderer)
        {
        }

        public SvgPictureBoxWindow(SvgWindow parentWindow, long innerWidth, long innerHeight)
            : base(parentWindow, innerWidth, innerHeight)
        {
        }

        public SvgPictureBoxWindow(SvgPictureBox control, ISvgRenderer renderer)
            : base(control.Width, control.Height, renderer)
        {
            if (control == null)
            {
                throw new NullReferenceException("control cannot be null");
            }

            _svgPictureBox = control;
        }

        #endregion

        #region ISvgWindow Members

        public override long InnerWidth
        {
            get
            {
                if (_svgPictureBox != null)
                {
                    return _svgPictureBox.Width;
                }

                return base.InnerWidth;
            }
            set
            {
                base.InnerWidth = value;
            }
        }

        public override long InnerHeight
        {
            get
            {
                if (_svgPictureBox != null)
                {
                    return _svgPictureBox.Height;
                }

                return base.InnerHeight;
            }
            set
            {
                base.InnerHeight = value;
            }
        }

        public override string Source
        {
            get
            {
                SvgDocument document = (SvgDocument)this.Document;
                return (document != null) ? document.Url : String.Empty;
            }
            set
            {
                Uri uri = new Uri(new Uri(Application.ExecutablePath), value);

                SvgDocument document = new SvgDocument(this);
                document.Load(uri.AbsoluteUri);

                this.Document = document;
            }
        }

        public override DirectoryInfo WorkingDir
        {
            get
            {
                return SvgApplicationContext.ExecutableDirectory;
            }
        }

        public override void Alert(string message)
        {
            if (String.IsNullOrEmpty(message))
            {
                return;
            }

            MessageBox.Show(message);
        }

        public override SvgWindow CreateOwnedWindow(long innerWidth, long innerHeight)
        {
            return new SvgPictureBoxWindow(this, innerWidth, innerHeight);
        }

        #endregion
    }
}


text:
MIME-Version: 1.0
Server: CERN/3.0
Date: Monday, 16-Dec-96 23:57:02 GMT
Content-Type: text/html
Content-Length: 1904
Last-Modified: Tuesday, 28-Nov-95 18:39:48 GMT

<title>Disclaimers and Other Information</title>

Disclaimers and Other Information

This is some of the information you should know about the Cornell
University Computer Science Department's local World Wide Web server.

Accuracy of Information

The information in the local CS Web is provided and maintained by many
different people, so use any information you find here with that in
mind.  While we will try to keep the information here up-to-date and
accurate, we can provide no guarantees.  If you encounter inaccuracies
or out-of-date material, please send mail to the author of the
document in question or to  www@cs.cornell.edu.

File Permissions on the local Web

Some of the information on the CS Web is department specific.  Any
links to files residing off directories explicitly marked for
external access will not be traceable by those outside the CS department
network.  If you see links to files which you feel should be accessable
by the world, please send mail to the author of the document containing
the link or to  www@cs.cornell.edu.

Presentation of Information

The html documents on the CS Web were created to be used with the
 Netscape Web
browser.  If you have another type of browser, some documents
may not appear as you would expect.  In addition, some of the files
available are in other formats that may or may not be easily converted
and readable by your browser.

Disclaimer

Unless otherwise noted, the information contained in the Cornell Computer
Science Department Web is not necessarily endorsed by the
Department or by Cornell University.  Views expressed by individuals on
this local Web are the sole responsibility of the individual.

Any questions or comments about the disclaimers or information here
should be sent to www@cs.cornell.edu.


text:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">GradleApp</string>
</resources>


text:
"test_add_bcast",
"test_add_uint8",
"test_argmax_default_axis_example",
"test_argmax_default_axis_example_select_last_index",
"test_argmax_default_axis_random",
"test_argmax_default_axis_random_select_last_index",
"test_argmax_keepdims_example",
"test_argmax_keepdims_example_select_last_index",
"test_argmax_keepdims_random",
"test_argmax_keepdims_random_select_last_index",
"test_argmax_negative_axis_keepdims_example",
"test_argmax_negative_axis_keepdims_example_select_last_index",
"test_argmax_negative_axis_keepdims_random",
"test_argmax_negative_axis_keepdims_random_select_last_index",
"test_argmax_no_keepdims_example",
"test_argmax_no_keepdims_example_select_last_index",
"test_argmax_no_keepdims_random",
"test_argmax_no_keepdims_random_select_last_index",
"test_argmin_default_axis_example",
"test_argmin_default_axis_example_select_last_index",
"test_argmin_default_axis_random",
"test_argmin_default_axis_random_select_last_index",
"test_argmin_keepdims_example",
"test_argmin_keepdims_example_select_last_index",
"test_argmin_keepdims_random",
"test_argmin_keepdims_random_select_last_index",
"test_argmin_negative_axis_keepdims_example",
"test_argmin_negative_axis_keepdims_example_select_last_index",
"test_argmin_negative_axis_keepdims_random",
"test_argmin_negative_axis_keepdims_random_select_last_index",
"test_argmin_no_keepdims_example",
"test_argmin_no_keepdims_example_select_last_index",
"test_argmin_no_keepdims_random",
"test_argmin_no_keepdims_random_select_last_index",
"test_averagepool_2d_pads_count_include_pad",
"test_averagepool_2d_precomputed_pads_count_include_pad",
"test_averagepool_2d_same_lower",
"test_basic_conv_with_padding",
"test_basic_conv_without_padding",
"test_cast_FLOAT_to_STRING",
"test_cast_STRING_to_FLOAT",
"test_castlike_FLOAT_to_STRING_expanded",
"test_castlike_STRING_to_FLOAT_expanded",
"test_concat_1d_axis_negative_1",
"test_conv_with_autopad_same",
"test_conv_with_strides_and_asymmetric_padding",
"test_conv_with_strides_no_padding",
"test_conv_with_strides_padding",
"test_div_bcast",
"test_div_uint8",
"test_dropout_default_ratio",
"test_flatten_axis0",
"test_flatten_axis2",
"test_flatten_axis3",
"test_flatten_negative_axis1",
"test_flatten_negative_axis2",
"test_flatten_negative_axis4",
"test_logsoftmax_default_axis",
"test_logsoftmax_large_number",
"test_logsoftmax_large_number_expanded",
"test_maxpool_2d_dilations",
"test_maxpool_2d_same_lower",
"test_maxpool_2d_uint8",
"test_maxpool_with_argmax_2d_precomputed_pads",
"test_maxpool_with_argmax_2d_precomputed_strides",
"test_maxunpool_export_with_output_shape",
"test_mul_bcast",
"test_mul_uint8",
"test_reduce_prod_default_axes_keepdims_example", // FP16 only
"test_reduce_prod_default_axes_keepdims_random", // FP16 only
"test_reduce_prod_do_not_keepdims_random", // FP16 only
"test_reduce_prod_keepdims_random", // FP16 only
"test_reduce_prod_negative_axes_keepdims_random", // FP16 only
"test_reduce_sum_square_default_axes_keepdims_random", // FP16 only
"test_reduce_sum_square_do_not_keepdims_random", // FP16 only
"test_reduce_sum_square_keepdims_random", // FP16 only
"test_reduce_sum_square_negative_axes_keepdims_random", // FP16 only
"test_softmax_default_axis",
"test_softmax_large_number",  // FP16 only
"test_softmax_large_number_expanded",  // FP16 only
"test_sub_bcast",
"test_sub_uint8",
"test_tan",  // FP16 only
"test_upsample_nearest",
"test_scatter_elements_with_axis",
"test_scatter_elements_with_duplicate_indices",
"test_scatter_elements_with_negative_indices",
"test_scatter_elements_with_reduction_max",
"test_scatter_elements_with_reduction_min",
"test_scatter_elements_without_axis",
"test_scatter_with_axis",
"test_scatter_without_axis",
"test_scatternd",
"test_scatternd_add",
"test_scatternd_max",
"test_scatternd_min",
"test_scatternd_multiply",


text:
<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Backpack\LangFileManager Language Lines
    |--------------------------------------------------------------------------
    |
    | The following language lines are used by the CRUD interface for lang files.
    | You are free to change them to anything
    | you want to customize your views to better match your application.
    |
    */

    'active'                 => 'Attivo',
    'cant_edit_online'       => 'Questa lingua non può essere modificata online.',
    'code_iso639-1'          => 'Codice (ISO 639-1)',
    'default'                => 'Predefinito',
    'empty_file'             => 'Nessuna traduzione disponibile.',
    'flag_image'             => 'Immagine bandiera',
    'key'                    => 'Chiave',
    'language'               => 'lingua',
    'language_name'          => 'Nome lingua',
    'language_text'          => 'Testo :language_name',
    'language_translation'   => 'Traduzione :language_name',
    'languages'              => 'lingue',
    'please_fill_all_fields' => 'Compila tutto i campi',
    'rules_text'             => "<strong>Avviso: </strong> Non tradurre le parole che iniziano con due punti (es: ':number_of_items'). Queste verranno sostituite automaticamente con un valore adeguato. Se tradotte, smetteranno di funzionare.",
    'saved'                  => 'Salvato',
    'site_texts'             => 'Testi del sito',
    'switch_to'              => 'Passa a',
    'texts'                  => 'testi',
    'translate'              => 'Traduci',
    'translations'           => 'Traduzioni',
    'native_name'            => 'Nome nativo',

];


text:
'use strict';

const path = require('path');
const fs = require('fs');
const encryptor = require('./encryptor');
const crypto = require('crypto');

const algorithm = 'aes256';
const encryptorOptions = {
  algorithm: algorithm,
  saveDecryptFile: false
};

const getUserData = login => {
  let filePath = path.join('./data', login);

  try {
    let data = JSON.parse(fs.readFileSync(filePath, { encoding: 'utf8' }));

    return {
      code: 200,
      data: data
    };
  } catch(e) {
    global.logger.log('\tgetUserData', filePath, e);
    return {
      code: 501
    };
  }
}

const saveUserData = (login, data) => {
  let filePath = path.join('./data', login);

  try {
    fs.writeFileSync(filePath, JSON.stringify(data, null, 2), {
      encoding: 'utf8'
    });
  } catch(e) {
    global.logger.log('\tsaveUserData', e);
    return {
      code: 403
    };
  }
}

const closeConnection = response => {
  // envia erro 403 para a conexão do usuário
  response.statusCode = 403;
  response.end('403 not allowed');
}

// valida se um usuario existe, tentando validar se o arquivo
// do hash de senha dele existe
const theUserExists = login => {
  return new Promise((resolve, reject) => {
    let dataPath = path.join('./data', `${login}.dat`);
    fs.access(dataPath, fs.constants && fs.constants.R_OK || fs.R_OK, err => {
      err ? reject(err) : resolve(dataPath);
    });
  });
}

// decriptografa o arquivo de hash, para poder
// validar a senha
const validateAndDecryptFile = (dataPath, password) => {
  return new Promise((resolve, reject) => {
    encryptor
      .decrypt(dataPath, password, encryptorOptions)
      .then(data => resolve(data))
      .catch(err => reject(err));
  });
}

class User {
  constructor(login, passwd) {
    let user = this;
    user.login = login;
    user.dataPath = '';
    user.token = crypto.randomBytes(32).toString('hex');
    user.data = {};

    return new Promise((resolve, reject) => {
      // valida se o usuário existe
      theUserExists(login)
        // sucesso, usuário existe
        // tenta descriptografar arquivo
        .then(dataPath => {
          user.dataPath = dataPath;
          return validateAndDecryptFile(dataPath, passwd);
        })

        // dispara ok para view
        .then(data => {
          try {
            user.data = JSON.parse(data);
          } catch(e) {
            user.data = {};
          }

          return resolve(user);
        })

        // erro, usuário não existe
        .catch(e => {
          global.logger.error('login error: ', e);
          reject(e);
        });
    });
  }

  add(service) {
    this.data.services = this.data.services || [];
    this.data.services.push(service);

    return this;
  }

  update(service) {
    let services = this.data.services || [];
    for(let i = 0; i < services.length; i++) {
      if(service.name === services[i].name) {
        services[i] = service;
        break;
      }
    }

    return this;
  }

  delete(service) {
    this.data.services = this.data.services.filter(item => {
      return service.name !== item.name;
    });

    return this;
  }

  commit(passwd) {
    return new Promise((resolve, reject) => {
      encryptor
        .encrypt(JSON.stringify(this.data), this.dataPath, passwd)
        .then(resolve)
        .catch(reject);
    });
  }
}

module.exports = (login, passwd) => new User(login, passwd);


text:
HElib
=====

HElib is an open-source ([Apache License v2.0][5]) software library that
implements [homomorphic encryption][6] (HE). Currently available schemes
are the implementations of the [Brakerski-Gentry-Vaikuntanathan][1] (BGV)
scheme with [bootstrapping][8] and the Approximate Number scheme of
[Cheon-Kim-Kim-Song][9] (CKKS), along with many optimizations to make
homomorphic evaluation run faster, focusing mostly on effective use of
the [Smart-Vercauteren][2] ciphertext packing techniques and
the [Gentry-Halevi-Smart][3] optimizations. See [this report][7] for a
description of a few of the algorithms using in this library.

Since mid-2018 HElib has been under extensive refactoring for *Reliability*,
*Robustness & Serviceability*, *Performance*, and most importantly *Usability*
for researchers and developers working on HE and its uses.

HElib supports an *"assembly language for HE"*, providing low-level routines
(set, add, multiply, shift, etc.), sophisticated automatic noise management,
improved BGV bootstrapping, multi-threading, and also support for Ptxt (plaintext)
objects which mimics the functionality of Ctxt (ciphertext) objects.
See [changes.md](changes.md) for more details.

Full installation instructions and a list of the required dependencies can be found
in [INSTALL.md](INSTALL.md).

If you are interested in contributing to HElib, please read our
[Contributing Guidelines](CONTRIBUTING.md).

HElib is written in C++14 and uses the [NTL mathematical library][4].
HElib is distributed under the terms of the [Apache License v2.0][5].

  [1]: http://eprint.iacr.org/2011/277       "BGV12"
  [2]: http://eprint.iacr.org/2011/133       "SV11"
  [3]: http://eprint.iacr.org/2012/099       "GHS12"
  [4]: http://www.shoup.net/ntl/             "NTL"
  [5]: http://www.apache.org/licenses/LICENSE-2.0  "Apache-v2.0"
  [6]: http://en.wikipedia.org/wiki/Homomorphic_encryption "Homomorphic encryption"
  [7]: http://eprint.iacr.org/2014/106       "algorithms"
  [8]: http://eprint.iacr.org/2014/873       "bootstrapping"
  [9]: http://eprint.iacr.org/2016/421       "CKKS16"
  [10]: https://github.com/homenc/HElib      "GitHubPages"


text:
package hu.fordprog.regx.interpreter.regex;

public class RegexCharacter implements Atom {
  private final String character;

  public RegexCharacter(String character) {
    this.character = character;
  }

  public String getCharacter() {
    return character;
  }

  @Override
  public Automaton makeAutomaton(){
    Automaton automaton = new Automaton();

    automaton.setStartState(1);
    automaton.addNewStateTransition(1, character, 2);
    automaton.addNewAcceptState(2);

    return automaton;
  }

  @Override
  public Regex normalize() {
    return this;
  }

  @Override
  public Regex simplify() {
    return this;
  }

  @Override
  public String asText() {
    return character;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    RegexCharacter character1 = (RegexCharacter) o;

    return getCharacter().equals(character1.getCharacter());

  }

  @Override
  public int hashCode() {
    return getCharacter().hashCode();
  }
}


text:
#include "common.h"
#include <io.h>
#include <direct.h>
#include "portable.h"

/* Return how long the named file is in bytes.
 * Return -1 if no such file. */
off_t fileSize(char *fileName)
{
int fd;
long size;
fd = _open(fileName, _O_RDONLY, 0);
if (fd < 0)
    return -1;
size = _lseek(fd, 0L, SEEK_END);
_close(fd);
return size;
}

long clock1000()
/* A millisecond clock. */
{
return clock() /* 1000/CLOCKS_PER_SEC */;   /* CLOCKS_PER_SEC == 1000 for windows */
}

long clock1()
/* Second clock. */
{
return clock()/CLOCKS_PER_SEC;
}

void uglyfBreak()
/* Go into debugger. */
{
__asm { int 3 } /* uglyf */
}

char *getCurrentDir()
/* Return current directory. */
{
static char dir[_MAX_PATH];

if( _getcwd( dir, _MAX_PATH ) == NULL )
    errnoAbort("can't get current directory");
return dir;
}

void setCurrentDir(char *newDir)
/* Set current directory.  Abort if it fails. */
{
if (_chdir(newDir) != 0)
    errnoAbort("can't to set current directory: %s", newDir);
}

struct slName *listDir(char *dir, char *pattern)
/* Return an alphabetized list of all files that match
 * the wildcard pattern in directory. */
{
long hFile;
struct _finddata_t fileInfo;
struct slName *list = NULL, *name;
boolean otherDir = FALSE;
char *currentDir;

if (dir == NULL || sameString(".", dir) || sameString("", dir))
    dir = "";
else
    {
    currentDir = getCurrentDir();
    setCurrentDir(dir);
    otherDir = TRUE;
    }

if (pattern == NULL)
    pattern = *;
if( (hFile = _findfirst( pattern, &fileInfo)) == -1L )
    return NULL;

do
    {
    if (!sameString(".", fileInfo.name) && !sameString("..", fileInfo.name))
        {
        name = newSlName(fileInfo.name);
        slAddHead(&list, name);
        }
    }
while( _findnext( hFile, &fileInfo) == 0 );
_findclose( hFile );
if (otherDir)
    setCurrentDir(currentDir);
slNameSort(&list);
return list;
}


text:
import pymongo
import gridfs
import settings

def copy_data(source_db_name, dest_db_name, query):
    source = pymongo.Connection(**settings.DB_SETTINGS)[source_db_name]
    dest = pymongo.Connection(**settings.DB_SETTINGS)[dest_db_name]

    source_gridfs = gridfs.GridFS(source, collection='files')
    dest_gridfs = gridfs.GridFS(dest, collection='files')

    for doc in source.docs.find(query):
        print 'Copying doc %s...' % doc['_id']

        # flip some flags
        #doc['stats'] = {}
        doc['in_aggregates'] = False
        doc['in_cluster_db'] = False
        doc['in_search_index'] = False

        dest.docs.save(doc)

        file_ids = []
        for view in doc.get('views', []):
            if view.get('content', None):
                file_ids.append(view['content'])

        for attachment in doc.get('attachments', []):
            for view in attachment.get('views', []):
                if view.get('content', None):
                    file_ids.append(view['content'])

        for fid in file_ids:
            print "Copying file %s" % fid

            # delete out of the dest in case it's already there
            dest_gridfs.delete(fid)

            # then read out from the old one
            fdata = source_gridfs.get(fid).read()

            # ... and write to the new one
            dest_gridfs.put(fdata, _id=fid)

        print "Done."

    dkt_query = dict(query)
    if "docket_id" in dkt_query:
        dkt_query['_id'] = dkt_query['docket_id']
        del dkt_query['docket_id']

    for dkt in source.dockets.find(dkt_query):
        print 'Copying docket %s...' % dkt['_id']

        # flip some flags
        #dkt['stats'] = {}
        dkt['in_search_index'] = False

        if 'source' not in dkt:
            dkt['source'] = 'regulations.gov'

        dest.dockets.save(dkt)

        print "Done."


text:
angular.module("vgTwitterApp").controller("TwitterCtrl",
    ["tweets", function TwitterCtrl(tweets) {
        this.config = {
            sources: [{src: "https://www.youtube.com/watch?v=I7ZUkd44-Co"}],
            tweets: tweets.data,
            plugins: {
                analytics: {
                    category: "Videogular",
                    label: "Twitter Widget",
                    events: {
                        ready: true,
                        play: true,
                        pause: true,
                        stop: true,
                        complete: true,
                        progress: 10
                    }
                }
            }
        };
    }]
);


text:
Admin Router is an open-source NGINX configuration created by
Mesosphere that provides central authentication and proxy to DC/OS services
within the cluster.

## Routes

Admin Router runs on both master and agent nodes, each with different configurations. From these NGINX config files, [Ngindox](https://github.com/karlkfi/ngindox) is used to generates swagger-like docs:

**Master Routes:**

- NGINX: [nginx.master.conf](nginx.master.conf)
- YAML: [docs/api/nginx.master.yaml](docs/api/nginx.master.yaml)
- HMTL: [docs/api/nginx.master.html](docs/api/nginx.master.html)
- Rendered: <https://rawgit.com/dcos/dcos/master/packages/adminrouter/extra/src/docs/api/nginx.master.html>

**Agent Routes:**

- NGINX: [nginx.agent.conf](nginx.agent.conf)
- YAML: [docs/api/nginx.agent.yaml](docs/api/nginx.agent.yaml)
- HMTL: [docs/api/nginx.agent.html](docs/api/nginx.agent.html)
- Rendered: <https://rawgit.com/dcos/dcos/master/packages/adminrouter/extra/src/docs/api/nginx.agent.html>

Use `make api-docs` to regenerate the YAML and HTML files.

Use `make check-api-docs` to validate that the YAML and HTML files are up to date.

## Ports summary

## Repository structure

There are two Admin Router "flavours" residing in DC/OS repos:
  * Opensource version or `Open` in short
  * Enterprise version or `EE` in short

The `Open` version is the base on top of which `EE` version is built. `EE` is in
fact an overlay on top of `Open`, it re-uses some of its components.

### Complexity vs. code re-use
It is crucial to understand that the more generalised the AR repositories
are, the more complicated they will become. Increased complexity will result in
people making mistakes and/or situations where complex rules are violated in
favour of development speed and thus copypasting code. It's all about striking
the right balance so sometimes code *is* duplicated between repositories in order
to make it easier for contributors to work with repositories.

### Nginx includes
All AR code, both Lua and non-Lua, can be divided into following groups:

 * common code for masters and agents, both EE and Open
 * agent-specific code, both EE and Open
 * master-specific code, both EE and Open
 * Open-specific code, both agent and master
 * EE-specific code, both agent and master
 * EE agent specific code
 * EE master specific code
 * Open agent specific code
 * Open master specific code

on top of that, Nginx-specific configuration is divided into three sections:

 * main
 * http
 * server

This gives us in total 27 possible "buckets" for Nginx directives. The
differentiation between sections could be avoided if we decide to use some more
advanced templating but this would further complicate configuration and
make it more difficult for people to test, and develop AR on live clusters.

Directly from it, stems the idea how the NGINX includes can be structured:

* open:

```
includes
├── http
│   ├── agent.conf
│   ├── common.conf
│   ├── master.conf
│   └── open
│       ├── common.conf
│       └── master.conf
├── main
│   ├── common.conf
│   └── open
│       └── common.conf
├── server
│   ├── common.conf
│   ├── master.conf
│   └── open
│       ├── agent.conf
│       └── master.conf
├── snakeoil.crt
└── snakeoil.key
```

* ee:

```
includes
├── http
│   └── ee
│       ├── common.conf
│       └── master.conf
├── main
│   └── ee
│       └── common.conf
└── server
    └── ee
        ├── agent.conf
        ├── common.conf
        └── master.conf
```

All Nginx related configuration directives reside in the `includes` directory which
contains directories reflecting all the sections present in NGINX configuration
(http|main|server). Because they are always present, no
matter the flavour/server type, they are chosen as top-level directories
in the `includes` dir. The `common.conf`, `agent.conf` and `master.conf`
files are flavour-agnostic thus they reside in each section's main dir
and are present/included only in the Open AR repository. EE repository re-uses them
after being applied on top of the Open repository. Contents of each of the
files are as follows:
 * `common.conf` contains all the things that are common across all flavours
   and server types for given section
 * `agent.conf` contains all the things that are common for all agents across
   all flavours
 * `master.conf` contains all the things that are common for all masters across
   all flavours

Each section can have either `ee` or `open` directories, but never both. `ee`
directory is only present in EE repo, Open repository contains only `open` directories.
Contents of these directories may be as follows:
 * `common.conf` contains all the things that are common for given flavour, no
   matter the server type
 * `agent.conf` contains all the things that are common for agents in given
   flavour
 * `master.conf` contains all the things that are common for masters in given
   flavour

The order of includes is for the time being hard-coded in nginx.\*.conf files.
Within particular section though it does not matter that much as:
 * [nginx by default orders the imports while globbing](https://serverfault.com/questions/361134/nginx-includes-config-files-not-in-order)
 * location blocks *MUST NOT* rely on the order in which they appear
   in the config file and define matching rules precise enough to avoid ambiguity.

All the includes are bound together by `nginx.(master|agent).conf` files.
`nginx.master.conf` for `Open` master looks like at the time of writing:
```
include includes/main/common.conf;
include includes/main/open/common.conf;

http {
    include includes/http/common.conf;
    include includes/http/master.conf;
    include includes/http/open/common.conf;
    include includes/http/open/master.conf;

    server {
        server_name master.mesos leader.mesos;

        include includes/server/common.conf;
        include includes/server/master.conf;
        include includes/server/open/master.conf;

        include /opt/mesosphere/etc/adminrouter-listen-open.conf;
        include /opt/mesosphere/etc/adminrouter-upstreams-open.conf;
        include /opt/mesosphere/etc/adminrouter-tls.conf;
    }
}
```
and for the agent:
```
include includes/main/common.conf;
include includes/main/open/common.conf;

http {
    include includes/http/common.conf;
    include includes/http/agent.conf;
    include includes/http/open/common.conf;

    server {
        server_name agent.mesos;

        include includes/server/common.conf;
        include includes/server/open/agent.conf;

        include /opt/mesosphere/etc/adminrouter-listen-open.conf;
        include /opt/mesosphere/etc/adminrouter-tls.conf;
    }
}
```

### Enforcing code reuse
The EE repository contains only EE directories, all common/agent-common/master-common
code resides in Open repository. This way EE repository becomes an overlay on
top of Open. Only `nginx.(master|agent).conf` are overwritten while applying EE
repository on top of Open during DC/OS image build. EE DC/OS image build scripts
remove all open directories from the Open repository before applying EE repository on
top of it.

This is not a bulletproof solution for preventing code
duplication (developers can simply start putting copies of code to both
`open/` and `ee/` directories) but it makes it easier to reuse the
code and encourages good behaviours.

### Lua code deduplication
It is not strictly required to provide the same level of flexibility for Lua code
as it is the case for Nginx includes and thus it's possible to simplify the code
a bit. It is sufficient to just differentiate Lua code basing on the repository flavour.
Both agents and masters can share the same Lua code.

There are two possible reasons that may be preventing Lua code from being
shared:
* the same code is executed but with different call arguments. An example to
  this may be `auth.validate_jwt_or_exit()` function. In the Open it takes no arguments,
  in EE it takes more than one.
* the code differs between EE and open but shares some common libraries/functions.
  Great example for this is `auth.check_acl_or_exit()` which internally, among
  many other things, calls argument-less `auth.check_jwt_or_exit()`.

In order to address these issues, a couple of patterns were selected:
 * modules which have flavour-specific function arguments export argument-less
   functions for the NGINX configuration. They translate the original call into
   a call with correct arguments. This approach requires splitting the module into
   `ee.lua|open.lua|common.lua` parts which is described in next bullet point.
   Depending on the flavour, either `ee.lua` or `open.lua` is imported and correct
   argument-less function is used. This approach also enables to share some of
   the NGINX `location` blocks between flavours - `location` code is the same,
   even though the Lua code used by the block differs.
 * some libraries/functions are structured in a way that extracts parts common for
   both flavours and `ee/open` parts that are included only in EE and Open repos
   respectively. An example of this approach is auth library which is splitted
   into three parts:
    * `lib/auth/common.lua` - present only in Open repository, with functions
      shared by both EE and Open code.
    * `lib/auth/ee.lua` - present only in EE repository, with functions specific
      to EE that use boilerplate from lib/auth/common.lua.
    * `lib/auth/open.lua` - as above but for Open repository.
   `init_by_lua` OpenResty call in `includes/http/(open|ee)/common.conf` imports
   `auth.open` or `auth.ee` modules as auth respectively.  The module is registered in
   the global namespace so all other Lua code uses it. This approach also allows
   for some degree of code separation enforcement as Open lua libs are removed
   during EE repository apply.

A special case of the "same code path, different call arguments"
problem is when a module needs to be initialized differently depending
on the flavour. A great example of it is `cache.lua` module which in Open
does not require any extra authentication data. To solve it, modules are
required to provide `.init()` function which is returned by require
`<module-name>` statement. The init function accepts arguments which
reconfigure module instance according to the flavour requirements.

Modules that do not require any customization in regards of flavour are left
as-is/usually do not follow any of these patterns. It is possible to standardise
it, but it does not seem to be justified. For example:
 * `util.lua` directly exposes its functions as they are stateless and shared
   between both flavours
 * `lib/auth/open.lua` uses the `.init()` pattern in order to achive different
   behaviour depending on the flavour.

## Service Endpoints

Admin Router allows Marathon tasks to define custom service UI and HTTP endpoints, which are made available via `<dcos-cluster>/service/<service-name>`. This can be achieved by setting the following Marathon task labels:

```
"labels": {
    "DCOS_SERVICE_NAME": "service-name",
    "DCOS_SERVICE_PORT_INDEX": "0",
    "DCOS_SERVICE_SCHEME": "http"
  }
```

In this case `http://<dcos-cluster>/service/service-name` would be forwarded to the host running the task using the first port allocated to the task.

In order for the forwarding to work reliably across task failures, we recommend co-locating the endpoints with the task. This way, if the task is restarted on a potentially other host and with different ports, Admin Router will pick up the new labels and update the routing. NOTE: Due to caching there might be an up to 30-second delay until the new routing is working.

We would recommend having only a single task setting these labels for a given `service-name`.
In the case of multiple task instances with the same `service-name` label, Admin Router will pick one of the tasks instances deterministically, but this might make debugging issues more difficult.

The endpoint should only use relative links for links and referenced assets such as .js and .css files. This is due to the fact, that the linked resources will be reachable only in their relative location `<dcos-cluster>/services/<service-name><link>`.

Tasks running in nested [Marathon app groups](https://mesosphere.github.io/marathon/docs/application-groups.html) will be available only using their service name (i.e, `<dcos-cluster>/service/<service-name>`) and not considering the marathon app group name (i.e., `<dcos-cluster>/service/app-group/<service-name>`).

## Authorization and authentication

DC/OS uses JWT at the core of its authentication and authorization features and
Admin Router performs a vital role in facilitating it. Lots of services which
constitute DC/OS do not have their own authorizers and rely on Admin Router to
provide them with authn/authn+authz enforcement.

Authn and authz code differs considerably between Open Source and Enterprise
versions of Admin Router. This documentation focuses on describing common and
Open Source related code. For EE features documentation please consult
README.md in EE Admin Router repository.

### Authentication

For Admin Router to establish the identity of the subject issuing a
request, it looks for JWT token in the request. It may be present in following
places:
* request headers: client needs to set `Authorization` header with
  `token=<token payload>` value.
* client sets `dcos-acs-auth-cookie` cookie with the token payload as a value

In the case when both request header and the cookie is set, request header token takes
priority. Admin Router does not issue JWT tokens itself as this is the task of
the IAM service. Please consult DC/OS documentation for more details.

Currently, DC/OS uses/sets following mandatory claims:
* `uid` which is ID of the user/subject making the request as defined in IAM
* `exp` claim as defined in section 4.1.4 of RFC7519

If the JWT signature is valid and token has not expired, then uid claim is used
while communicating with IAM to make authz decisions.

The way tokens are validated is shared between EE and Open, with the only
difference being the signing algorithm used by the tokens. In case of Open it's
`HS256` and in case of EE: `RS256`

In case when authentication was not successful, Admin Router responds with 401
error page with `WWW-Authenticate` header set to authentication process type
that is required to finish the authn.

### Authorization

Open Source Admin Router performs authorization basing on the sole fact that
user identity was transferred to Open Source IAM. This is done in by checking if
the user with given `uid` extracted from JWT claim is present and active in the
IAM using the `do_authn_and_authz_or_exit` LUA auth module method.

### Parameter-less interface

Authz differs significantly between EE and Open Source Admin Router,
but the location blocks in `nginx.*.conf` files not necessarily. So to
be able to share as much code as possible between EE and Open, there has to be
a common interface for EE and Open LUA auth code that location blocks can use.

This is the reason why LUA code responsible for auth exposes thin
parameter-less wrappers around all the functions, for example:

```
res.access_lashupkey_endpoint = function()
    return res.do_authn_and_authz_or_exit()
end
```

This way by importing EE LUA auth module instead of Open, location block can
use EE features immediately without any extra reconfiguration and the
additional parameters that are required in EE are coded directly in the LUA
module.

More information about EE-Open code sharing and code layout that stems from it
can be found in `Code sharing` section of this readme.

## Caching

In order to serve some of the requests, Admin Router relies on the information
 that can be obtained from Marathon and  Mesos. This includes:
* Mesos agents - e.g. `/agent/.*` location
* Marathon leader - e.g. `/system/v1/leader/marathon/.*` location
* Tasks/Frameworks running in the cluster - e.g. `/service/.*` location

Due to scalability reasons, it's impossible to obtain this data on each and
every request to given endpoint as it will overload Mesos/Marathon. So
the idea was born to pre-fetch this data and store it in shared memory where
each NGINX worker process can access it.

### Architecture

Due to the nature of NGINX, there are some limitations when it comes to Lua
code that OpenResty can run. For example:
* threading is unavailable, it's recommended to use recursive timers (http://stackoverflow.com/a/19060625/145400) for asynchronous tasks
* it's impossible to hold back NGINX request processing machinery from within
  certain initialization hooks as workers work independently.
* Using ngx.timer API in `init_by_lua` is not possible because init_by_lua runs
  in the NGINX master process instead of the worker processes which does the
  real request processing, etc. (https://github.com/openresty/lua-nginx-module/issues/330#issuecomment-33622121)

So a decision was made to periodically poll Mesos and Marathon for relevant data
using recursive timers. There are two variables that control this behaviour:
* `CACHE_FIRST_POLL_DELAY` - first poll for Mesos and Marathon occurs after this
  amount of time passed since worker initialization
* `CACHE_POLL_PERIOD` - after the first poll is done, every other is scheduled
  every this number of seconds.
Obviously `CACHE_FIRST_POLL_DELAY` should be much smaller than `CACHE_POLL_PERIOD`.

Cache refresh can also be triggered by a request coming in during
<0, `CACHE_FIRST_POLL_DELAY`> period. In this case, cache refresh will not occur
during the `CACHE_FIRST_POLL_DELAY` timer execution as the contents of the
shared memory will still be considered fresh.

### Cache timers

The `freshness` of the cache is governed by few variables:
* `CACHE_EXPIRATION` - if the age of the cached data is smaller than
  `CACHE_EXPIRATION` seconds, then the cache refresh will not occur if it's
  ngx.timer context and request processing code will use the data stored in shared memory.
* `CACHE_MAX_AGE_SOFT_LIMIT` - between `CACHE_EXPIRATION` seconds and
  `CACHE_MAX_AGE_SOFT_LIMIT` seconds, cache is still considered "usable" in
  request context, but the ngx.timer context will try to update it with fresh
  data fetched from Mesos and Marathon
* `CACHE_MAX_AGE_HARD_LIMIT` - between `CACHE_MAX_AGE_SOFT_LIMIT` and
  `CACHE_MAX_AGE_HARD_LIMIT` cache is still usable in request context, but
  with each access to it, a warning message is written to the NGINX log.
  Timer context will try to update the cache.
* beyond `CACHE_MAX_AGE_HARD_LIMIT` age, cache is considered unusable and
  every request made to the location that uses it will fail with 503 status.

The relation between these is:
`CACHE_EXPIRATION` < `CACHE_MAX_AGE_SOFT_LIMIT` << `CACHE_MAX_AGE_HARD_LIMIT`

The reason why we put `<<` in front of `CACHE_MAX_AGE_HARD_LIMIT` is to make
the cache a bit of a "best-effort" one - In the case when Mesos and/or Marathon
dies, the cache should still be able to serve data for a reasonable amount of time
and thus give the operator some time to solve the underlying issue. For example
Mesos tasks do not move that often and the data stored in NGINX should still be
usable, at least partially.

### Locking and error handling

Each worker tries to perform cache updates independently. On top of that during
the early stage of Admin Router operation, a request can trigger the update as
well. In order to coordinate it, locking was introduced.

There are two different locking behaviours, depending on the context from which
the update was triggered:
* for timer-based refreshes, the lock is non-blocking. If there is an update already
  in progress, execution is aborted, and next timer-based refresh is scheduled.
* in case of request-triggered update, lock is blocking and the lock timeout
  is equal to `CACHE_REFRESH_LOCK_TIMEOUT` seconds. This way, during
  `<0, CACHE_FIRST_POLL_DELAY>` period, requests are queued while waiting for the
  first, refresh to succeed.

Request to Mesos/Marathon can take at most `CACHE_BACKEND_REQUEST_TIMEOUT` seconds.
After that, the request is considered failed, and it is retried during the next
update.

Worth noting is that NGINX reload resets all the timers. Cache is left intact
though.

## Testing

Admin Router repository includes a test harness that is meant to make
testing easier and in some cases - possible. It's written in Python and
uses pytest fixtures and custom modules to mock out all relevant DC/OS
features and control NGINX startup and termination.

All the tests are executed in a Docker container which is controlled by the
Makefile. Inside the container pytest command is started which in turn pulls
in all the relevant fixtures, such as Syslog mock, mocker (DC/OS endpoints
mock), DNS mock, etc... Finally, an NGINX is spawned using the configuration
bind-mounted from the developer's repository. Tests may launch NGINX multiple
times, in different configurations, depending on what is needed. After the
tests runner finishes, all the processes and the environment is cleaned up
by pytest.

Below, there is a general overview of all the components of the test harness.
More detailed documentation can be found in docstrings and comments in the code
itself. If in doubt, it also may be very helpful to look for examples among
existing tests.

### Quickstart
To execute all the tests, just issue:

	make test

In order have fine-grained control over the pytest command, execute:

	make shell

This command will launch an interactive environment inside the container that
has all the dependencies installed. The developer may now run the `pytest`
command as need, debug the environment and temporarily add/change
the dependencies.

### Makefile
Makefile provides an easy way to start the testing environment without the need
to worry about the correct docker commands. Its core concept is `adminrouter-devkit`
container which is contains all the dependencies that are needed to run
Admin Router, and inside which all tests-related commands are run.

It exposes a couple of targets:
* `make clean` - remove all containers created by the test harness. It does not
  remove the images themselves though as the layer cache may be useful later
  on and the user may remove them themselves.
* `make devkit` - creates the `adminrouter-devkit` container. By default other
   targets execute this step automatically if devkit container image does not
   exist yet.
* `make update-devkit` - updates `adminrouter-devkit`. Should be run every time
   the Dockerfile or its dependencies change.
* `make tests` - launch all the tests. Worth noting is the fact that McCabe
   complexity of the code is also verified, and an error is raised if it's
   equal to or above 10.
* `make shell` - launch an interactive shell within the devkit container. Should
  be used when fine grained control of the tests is necessary or during debugging.
* `make flake8` - launch flake8 which will check all the tests and test-harness
  files by default.

### Docker container
As mentioned earlier, all the commands are executed inside the `adminrouter-devkit`
container. It follows the same build process for NGINX that happens during
DC/OS build with the exception of setting the  `--with-debug` flag. It also
contains some basic debugging tools, pytest related dependencies and
files that help pytest mimic the DC/OS environment. Their location is then
exported via environment variables to the pytest code, so changing their location
can be done by edition only the Dockerfile.

Due to some issues with overlayfs on older kernels, the container links
/tmp directory to /dev/shm.

### Repository flavours
Admin Router repository can come in two variants/flavours - Enterprise and Opensource.
Tests determine it basing on the directory structure - if tests contain
`test-harness/tests/open/` directory then the repo is treated as Opensource one,
and in case when `test-harness/tests/ee/` directory is present - as the
enterprise one. Pytest fixture `repo_is_ee` takes care of it and is pulled in
as dependencies by all the fixtures that need this information.

### Service startup ordering and cleanup
Mocking out DC/OS is a complex task, and a huge part in it have pytest
fixtures. They take care of proper ordering of mocks and subprocess start/stop and
cleanup and the cleanup ordering itself.

Tracking the chain of fixtures and how they use each other may provide better
understanding of how the test harness works.

### JWT
DC/OS IAM relies on JSON Web Tokens for transmitting security information between DC/OS
components. Open repository flavour uses HS256 tokens, while EE relies on RS256.
Test harness needs to have a way to create them for use in the tests. This is
done by `test-harness/modules/mocker/jwt.py` module, which together with
`repo_is_ee` fixture provides abstracts away the type of the
token used by Admin Router itself.

### Mocker
Mocker takes care of simulating DC/OS HTTP endpoints that Admin Router uses. It's
basically just a thin management layer on top of multiple python-based
HTTP servers, each one of them mocking out particular component/upstream of DC/OS.

It exposes the `.send_command()` method that allows to reconfigure endpoints
the way the tests need it. It calls a getattr on the endpoint
instance to execute the given function and pass it attributes as specified
by the `.send_command()` call arguments. Please see the function signature in
the file `test-harness/modules/mocker/common.py` for details.

#### Endpoints
Endpoints are threading HTTP servers based on http.server python module. Because
of the fact that there are many different kinds of endpoints that need to be
employed by mocker, and that there is a lot of shared behaviour between them,
they follow inheritance tree as below:

* `ReflectingUnixSocketEndpoint`, `ReflectingTCPIPEndpoint`: both of them send back
  the request data in the response body to the client for inspection. The only
  difference between them is that the former is listening on Unix Socket
  and the latter is listening on TCP/IP socket. They are useful for very simple
  tests that only check if the request is hitting the right upstream and the
  headers are correct.
* `IAMEndpoint`, `MarathonEndpoint`, `MesosEndpoint`: specialized endpoints that
  are mimicking IAM, Marathon and Mesos respectively. Apart from the basic
  functionality (reset, bork, etc...), they are also capable of recording requests
  sent to them, and then returning them through mockers `send_command` back to the
  tests code.
* all remaining endpoints depicted in the hierarchy are not directly usable but
  can be inherited from and extended if necessary for the purposes of testing.

Each endpoint has an id, that it basically it's http address (i.e.
`http://127.0.0.1:8080` or `http:///run/dcos/dcos-metrics-agent.sock`). It's
available via the `.id()` method. They are started and stopped via `.start()`
and `.stop()` methods during the start and stop of mocker instance
respectively. Each endpoint can be set to respond to each and every request
with an error (`500 Internal server error`).

#### DNS mock

The AR requires a working DNS server that responds correctly to the following
queries:

* `leader.mesos` - `A`: current Mesos leader instance
* `master.mesos` - `A`: any Mesos master instance
* `agent.mesos` - `A`: any Mesos agent
* `slave.mesos` - `A`: any Mesos agent

Mocking library comes with a simple DNS in-memory programmable server that
can be used to mock various DNS query responses and also to simulate leader
instance changes.

#### Subprocess management
Pytest fixture starts an `Admin Router` subprocess.

The subprocess doesn't always log to stderr/stdout, so a very simple syslog mock
is also provided. All the stdouts and stderrs are piped into the central log
processing class LogCatcher.

##### LogCatcher
As mentioned in the previous paragraph, all the stdout/stderr file descriptors
and syslog messages are piped into this class. It uses `poll()` call to monitor
all the sources for new information and push it into:
* standard python logging module
* logs located in `test-harness/logs/`
* internal buffer which can be used by tests for logging-based testing

The internal buffer is available through
`stdout_line_buffer`/`stderr_line_buffer` methods of AR object and Syslog
object(available through a fixture). The buffer itself is implemented as a
plain python list where each log line represents a single entry. This list is
shared across all the objects that are groking the buffer, and there is no
extra protection from manipulating it from within tests so extra care needs
to be taken.

In order to simplify handling of the log lines buffers, `LineBufferFilter` class
has been created. It exposes two interfaces:
* context manager that allows for groking the buffer entries that were created
  while executing the context:

  ```
    filter_regexp = 'validate_jwt_or_exit\(\): User not found: `{}`'.format(uid)
    lbf = LineBufferFilter(filter_regexp,
                           line_buffer=master_ar_process.stderr_line_buffer)
    with lbf:
        resp = requests.get(url,
                            allow_redirects=False,
                            headers=header)
    assert lbf.extra_matches == {}

  ```
* `scan_log_buffer()` method that scans all the entries, since the subprocess
  start.
  ```
    filter_regexp = 'Secret key not set or empty string.'

    lbf = LineBufferFilter(filter_regexp,
                           line_buffer=ar_process_without_secret_key.stderr_line_buffer)

    lbf.scan_log_buffer()

    assert lbf.extra_matches == {}

  ```
Separation of the log entries stemming from different instances of a given
Subprocess class in the logfile is done by placing the following line in the log
file:
```
✄✄✄✄✄✄✄✄✄✄ Logging of this instance ends here ✄✄✄✄✄✄✄✄✄✄

```

##### Syslog mock
Syslog mock is a very simple Python hack - a DGRAM Unix Socket is created and
added to LogWatcher. LogWatcher itself takes care of draining data from it,
with the line length limit hard-coded to 4096 bytes.

##### NGINX
The NGINX subprocess is different from others in regard to its lifetime. Pytest
fixture that pulls it into the test is module-scoped by default. If there is a
need to have custom lifetime or just single-test scoped lifetime, then it's
necessary to use `nginx_class` fixture instead of simple `master_ar_process` or
`agent_ar_process` ones.

NGINX instances have the `.make_url_from_path` method which is a convenient way
to generate AR URLs for tests. It uses exhibitor endpoint as it's present in
all DC/OS configurations and uses auth features as well.

#### pytest tests
Directly from the fact that Admin Router subprocess fixture is module scoped,
stems some of the current structure of the tests directory. There are three
different files where tests reside:
* `test_agent.py`: all the tests related to agent Admin Router, which do not
  require custom AR fixtures
* `test_master.py`: all the tests related to master Admin Router, which do not
  require custom AR fixtures
* `test_boot_envvars.py`: tests that verify adminrouter startup variables

`test_agent.py` and `test_master.py` may be splitted apart into smaller units,
but with each unit an extra start and stop of AR master or agent is required.
This slows down the tests. Running session-scoped AR is impossible. For now,
each endpoint has tests grouped on class level.

Test files are grouped into repository flavour directories, which
group the tests that are specific for given version:
* `test-harness/modules/ee/test_*.py` for Enterprise
* `test-harness/modules/open/test_*.py` for Open
* `test-harness/tests/test_*.py` common for both variants

#### Tooling
Code uses some extra tooling in order to enforce coding standards.

Currently it's only `isort` together with flake8-isort plugin. In order to
properly distinguish between 2nd party and 3rd party modules, the `.isort.cfg`
file lists all local modules in `known_first_party` config parameter.

#### Debugging threads-related issues
The fixtures, mocks, and all other features make the code heavily threaded. In
order to simplify debugging a special signal handler is installed. It launches a
pdb sessions when USR1 signal is received.

Code also relies on thread safety/atomicity of some of Python's
expressions/statements:

    https://docs.python.org/3.6/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe

This is why the context of each endpont is not protected by locks, in
case when it's only about fetching a single value from context dict or
storing/appending one there.