text:
{% extends '../layouts/default.html' %}

{% block graphViewPrePageWrapper %}

{% if graph.isNew %}
  {% set action = '/graphs' %}
{% else %}
  {% set action = '/graphs/' + graph._id %}
{% endif %}

<form method="post" id="kathaa-graph-form" action="{{ action }}" enctype="multipart/form-data" role="form" class="form-horizontal">
  <input type="hidden" name="_csrf" value="{{ csrf_token }}">
  <input name="body" id="kathaa-graph-body" class="form-control hidden" value="{{ graph.body }}" required=""></input>

  {% if not graph.isNew %}
    <input type="hidden" name="_method" value="PUT">
  {% endif %}
{% endblock %}

{% block graphViewPostPageWrapper %}
  </form>
{% endblock %}

{% block head %}
{% endblock %}

{% block main %}

{{ title }}

{% endblock %}

{% block content %}

  <audio id="chime_bell_ding.wav" class="hidden"><source src="/sounds/chime_bell_ding.wav"></audio>
  <audio id="slow_success.wav" class="hidden"><source src="/sounds/slow_success.wav"></audio>

Loading....

        <the-graph-editor id="editor"
        width="100%" height="400"
        grid="72"
        snap="36"
        theme="dark">
        </the-graph-editor>

        <the-graph-nav id="nav" width="216" height="162"></the-graph-nav>

Module Properties
 (  )

Info

Module Properties

Code

Input

Output

                            <textarea id="kathaa-edit-module-process-definition-tab-code" style="min-height:300px"></textarea>

                                  Input Ports
                                  <select id="kathaa-edit-module-input-tab-ports" class="form-control">
                                  </select>

                            <textarea id="kathaa-edit-module-input-tab-code" style="min-height:300px"></textarea>

                                  Output Ports
                                  <select id="kathaa-edit-module-output-tab-ports" class="form-control">
                                  </select>

                            <textarea id="kathaa-edit-module-output-tab-code" style="min-height:300px"></textarea>

                <button type="button" class="close" onclick="$('#kathaa-add-module').hide();">×Close</button>

  Add Module

                            Name
                            Version
                            Description

                <button type="button" class="close" onclick="$('#kathaa-group-modules').hide();">×Close</button>

Group Modules

                <form class="form-horizontal">

Group Name

<input id="kathaa-group-modules-name" type="text" placeholder="" class="form-control">

Group Description

<textarea id="kathaa-group-modules-description" class="form-control" rows="5"></textarea>

                </form>

                <button type="button" class="btn btn-white" onclick="$('#kathaa-group-modules').hide();">Close</button>
                <button id="kathaa-group-modules-save" type="button" class="btn btn-primary">Save</button>

                <button type="button" class="close" onclick="$('#kathaa-debug-view').hide();">×Close</button>

Debug View

                <textarea id="kathaa-debug-view-editor" style="max-height:300px"></textarea>

                <button type="button" class="btn btn-white" onclick="$('#kathaa-debug-view').hide();">Close</button>

                <button type="button" class="close" onclick="$('#kathaa-edit-edge').hide();">×Close</button>

Edit Edge

                Please select the intended edge color

..

..

..

..

..

..

..

..

..

..

..

                <button type="button" class="btn btn-white" onclick="$('#kathaa-edit-edge').hide();">Close</button>

{% endblock %}


text:
import distutils.ccompiler as cc
import os, os.path
import platform
import ctypes
import distutils.cmd
from distutils.core import setup

class build_chipmunk(distutils.cmd.Command):
    description = """build chipmunk to a shared library"""

    user_options = [('compiler=', 'c', 'specify the compiler type. It must understand GCC arguments')
                    ,('release', 'r', 'build chipmunk without debug asserts')
                    ]

    boolean_options = ['release']

    help_options = [
        ('help-compiler', None, "list available compilers", cc.show_compilers)
        ]

    compiler = None

    def initialize_options (self):
        self.compiler = None
        self.release = False

    def finalize_options (self):
        pass

    def compile_chipmunk(self):
        if self.release:
            print("compiling chipmunk in Release mode (No debug output or asserts)" )
        else:
            print("compiling chipmunk in Debug mode (Defualt, prints debug output and asserts)")

        compiler = cc.new_compiler(compiler=self.compiler)

        source_folders = ['chipmunk_src', os.path.join('chipmunk_src','constraints')]
        sources = []
        for folder in source_folders:
            for fn in os.listdir(folder):
                fn_path = os.path.join(folder, fn)
                if fn_path[-1] == 'c':
                    sources.append(fn_path)
                elif fn_path[-1] == 'o':
                    os.remove(fn_path)

        include_folders = [os.path.join('chipmunk_src','include','chipmunk')]

        compiler_preargs = ['-std=gnu99', '-ffast-math', '-DCHIPMUNK_FFI', '-Wno-unknown-pragmas', '-fPIC', '-DCP_USE_CGPOINTS=0'] # '-DCP_ALLOW_PRIVATE_ACCESS']

        if self.release:
            compiler_preargs.append('-DNDEBUG')

        # check if we are on a 64bit python
        arch = ctypes.sizeof(ctypes.c_voidp) * 8

        if arch == 64 and platform.system() == 'Linux':
            compiler_preargs += ['-m64', '-O3']
        elif arch == 32 and platform.system() == 'Linux':
            compiler_preargs += ['-m32', '-O3']
        elif platform.system() == 'Darwin':
            #No -O3 on OSX. There's a bug in the clang compiler when using O3.
            compiler_preargs += ['-arch', 'i386', '-arch', 'x86_64']

        if platform.system() in ('Windows', 'Microsoft'):
            # Compile with stddecl instead of cdecl (-mrtd).
            # Using cdecl cause a missing bytes issue in some cases
            # Because -mrtd and -fomit-frame-pointer (which is included in -O)
            # gives problem with function pointer to the sdtlib free function
            # we also have to use -fno-omit-frame-pointer
            compiler_preargs += ['-mrtd', '-O3', '-shared', '-fno-omit-frame-pointer']

        if arch == 64 and platform.system() in ('Windows', 'Microsoft'):
            compiler_preargs += ['-m64']
        if arch == 32 and platform.system() in ('Windows', 'Microsoft'):
            compiler_preargs += ['-m32']

        for x in compiler.executables:
            args = getattr(compiler, x)
            try:
                args.remove('-mno-cygwin') #Not available on newer versions of gcc
                args.remove('-mdll')
            except:
                pass

        objs = compiler.compile(sources, include_dirs=include_folders, extra_preargs=compiler_preargs)

        libname = 'chipmunk'
        if arch == 64 and platform.system() != 'Darwin':
            libname += '64'
        if platform.system() == 'Darwin':
            libname = compiler.library_filename(libname, lib_type='dylib')
            compiler.set_executable('linker_so', ['cc', '-dynamiclib', '-arch', 'i386', '-arch', 'x86_64'])
        else:
            libname = compiler.library_filename(libname, lib_type='shared')
        linker_preargs = []
        if platform.system() == 'Linux' and platform.machine() == 'x86_64':
            linker_preargs += ['-fPIC']
        if platform.system() in ('Windows', 'Microsoft'):
            # link with stddecl instead of cdecl
            linker_preargs += ['-mrtd']
            # remove link against msvcr*. this is a bit ugly maybe.. :)
            compiler.dll_libraries = [lib for lib in compiler.dll_libraries if not lib.startswith("msvcr")]
        compiler.link(cc.CCompiler.SHARED_LIBRARY, objs, libname, output_dir = 'pymunk', extra_preargs=linker_preargs)

    def run(self):
        self.compile_chipmunk()

# todo: add/remove/think about this list
classifiers = ['Development Status :: 5 - Production/Stable'
    , 'License :: OSI Approved :: MIT License'
    , 'Operating System :: OS Independent'
    , 'Programming Language :: Python'
    , 'Topic :: Games/Entertainment'
    , 'Topic :: Software Development :: Libraries'
    , 'Topic :: Software Development :: Libraries :: pygame'
]

from distutils.command import bdist
bdist.bdist.format_commands += ['msi']
bdist.bdist.format_command['msi'] = ('bdist_msi', "Microsoft Installer")

setup(
    name='pymunk'
    , url='http://www.pymunk.org'
    , author='Victor Blomqvist'
    , author_email='vb@viblo.se'
    , version='4.0.0' # remember to change me for new versions!
    , description='pymunk is a easy-to-use pythonic 2d physics library built on top of Chipmunk'
    , long_description=open('README.txt').read()
    , packages=['pymunk','pymunkoptions']
    , package_data = {'pymunk': ['chipmunk.dll'
                                , 'chipmunk64.dll'
                                , 'libchipmunk.so'
                                , 'libchipmunk64.so'
                                , 'libchipmunk.dylib']}
    , license='MIT License'
    , classifiers=classifiers
    , cmdclass={'build_chipmunk':build_chipmunk}
    )

redpajama_set_name: RedPajamaGithub


text:
require 'spec_helper'
require 'nokogiri'

describe CookbooksHelper do
  describe '#contingent_link' do
    it 'links to a cookbook using the cookbook name and version' do
      apt = create(:cookbook, name: 'apt')
      nginx = create(:cookbook, name: 'nginx')
      version = create(:cookbook_version, cookbook: nginx, version: '10.10.10')
      dependency = create(:cookbook_dependency, cookbook: apt, name: 'apt', cookbook_version: version)
      output = helper.contingent_link(dependency)
      expect(output).to match(/nginx 10\.10\.10/)
      expect(output).to match(%r{/cookbooks/nginx})
    end
  end

  describe '#latest_cookbook_version_url' do
    it 'should return an api url to the latest cookbook version' do
      apt = create(:cookbook, name: 'apt')
      version = create(:cookbook_version, cookbook: apt, version: '10.10.10')
      output = helper.latest_cookbook_version_url(apt.reload)
      expect(output).to eql('http://test.host/api/v1/cookbooks/apt/versions/10.10.10')
    end
  end

  describe '#follow_button_for' do
    it "returns a follow button if current user doesn't follow the given cookbook" do
      cookbook = double(
        :cookbook,
        followed_by?: false,
        cookbook_followers_count: 100,
        name: 'redis'
      )
      allow(helper).to receive(:current_user) { true }

      expect(helper.follow_button_for(cookbook)).to match(/follow/)
    end

    it 'returns a follow button with query params if any are given' do
      cookbook = double(
        :cookbook,
        followed_by?: false,
        cookbook_followers_count: 100,
        name: 'redis'
      )
      allow(helper).to receive(:current_user) { true }

      expect(helper.follow_button_for(cookbook, list: true)).to match(/\?list=true/)
    end

    it 'returns an unfollow button if the current user follows the given cookbook' do
      cookbook = double(
        :cookbook,
        followed_by?: true,
        cookbook_followers_count: 100,
        name: 'redis'
      )
      allow(helper).to receive(:current_user) { true }

      expect(helper.follow_button_for(cookbook)).to match(/unfollow/)
    end

    it 'returns a call to action follow button if there is no current user' do
      cookbook = double(:cookbook, cookbook_followers_count: 100, name: 'redis')
      allow(helper).to receive(:current_user) { false }

      expect(helper.follow_button_for(cookbook)).to match(/sign-in-to-follow/)
    end

    it 'uses the result of a passed block for the button text' do
      cookbook = double(:cookbook, cookbook_followers_count: 100, name: 'redis')
      allow(helper).to receive(:current_user) { false }

      block = proc { 'awesome text' }

      result = helper.follow_button_for(cookbook, &block)

      expect(result).to match(/awesome text/)
    end
  end

  describe '#link_to_sorted_cookbooks' do
    it 'returns an active link if the :order param is the given ordering' do
      allow(helper).to receive(:params) do
        { order: 'excellent', controller: 'cookbooks', action: 'index' }
      end

      link = Nokogiri::HTML(
        helper.link_to_sorted_cookbooks('Excellent', 'excellent')
      ).css('a').first

      expect(link['class']).to match(/active/)
    end

    it 'returns a non-active link if the :order param is not the given ordering' do
      allow(helper).to receive(:params) do
        { order: 'excellent', controller: 'cookbooks', action: 'index' }
      end

      link = Nokogiri::HTML(
        helper.link_to_sorted_cookbooks('Not Excellent', 'not_excellent')
      ).css('a').first

      expect(link['class'].to_s.split(' ')).to_not include('active')
    end

    it 'generates a link to the current page with the given ordering' do
      allow(helper).to receive(:params) do
        { order: 'excellent', controller: 'cookbooks', action: 'index' }
      end

      link = Nokogiri::HTML(
        helper.link_to_sorted_cookbooks('Not Excellent', 'not_excellent')
      ).css('a').first

      expect(URI(link['href']).path).
        to eql(url_for(controller: 'cookbooks', action: 'index'))
      expect(URI(link['href']).query).to include('order=not_excellent')
    end
  end

  describe '#foodcritic_info' do
    context 'when a cookbook version has passed foodcritic' do
      let(:passing_foodcritic) { "\nRun with Foodcritic Version 7.0.1 with tags ~FC031 ~FC045" }

      it 'does not include a new line at the beginning' do
        expect(helper.foodcritic_info(passing_foodcritic, false)).to_not match(/^\n/)
      end
      it 'does not include a
' do
        expect(helper.foodcritic_info(passing_foodcritic, false)).to_not include('
')
      end
    end

    context 'when a cookbook version has not passed foodcritic' do
      let(:failing_foodcritic) do
        "FC064: Ensure issues_url is set in metadata:\nRun with Foodcritic Version 7.0.1 with tags ~FC031 ~FC045"
      end

      it 'includes at least one
' do
        expect(helper.foodcritic_info(failing_foodcritic, true)) .to include('
')
      end
    end

    context 'when feedback is nil' do
      it 'does not include a new line at the beginning' do
        expect(helper.foodcritic_info(nil, true)) .to eq('No foodcritic feedback available')
      end
    end
  end
end


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

<title>org.robolectric.shadows.gms</title>
<link rel="stylesheet" type="text/css" href="../../../../stylesheet.css" title="Style">

org.robolectric.shadows.gms

Classes

ShadowGoogleAuthUtil

ShadowGoogleAuthUtil.GoogleAuthUtilImpl

ShadowGooglePlayServicesUtil

ShadowGooglePlayServicesUtil.GooglePlayServicesUtilImpl

Shadows


text:
---
title: Develop
---

DemocracyOS development guide
==========================

We'll be using version `^1.0.0` of DemocracyOS.

There are several ways for developing DemocracyOS. The most straightforward way is by leveraging [`Docker`](https://www.docker.com), since this will save you the need for configuring your environment for running and building natively. For more on running our dockerised development environment jump to the [Running the app](http://docs.democracyos.org/develop/#running-the-app) section.

Alternatively, configure your local environment as follows:

# Stack

#### Node.js and npm
You need [Node.js v4.0.0](https://nodejs.org/en/blog/release/v4.0.0/) or greater to run DemocracyOS, but we always try to keep up with their latest release, so feel free to try newer `node` versions.

There are many options out there to get it installed:

* [Official site](https://nodejs.org/en/download/): both Node.js and npm come bundled with this installation.

* [nvm](https://github.com/creationix/nvm): Node Version Manager - Simple bash script to manage multiple active node.js versions

* [tj/n](https://github.com/tj/n): another -yet simpler- node version manager

#### MongoDB

We support the [latest supported version](https://www.mongodb.com/support-policy) of [`MongoDB`](https://www.mongodb.org/).

So [download](https://www.mongodb.com/download-center) the version for your system and follow the installation instructions to get it running.

Alternatively, you may want to run `MongoDB` in a `Docker` container. If so, just do:

`docker run -p 27017:27017 --name mongodb mongo`

After your container is up and running, [update your configuration](http://docs.democracyos.org/develop/#configuration) so your can access the container properly.

```
Note: this manual usage of a MongoDB container if using docker-compose.
```

#### Browserify

We use [Browserify](http://browserify.org) to bundle all our client side code, but we use it as a dependency so you don't need to install anything.

#### Operating System

DemocracyOS runs properly on `OS/X` and plenty of `Linux` distros, especially `Debian`-based ones.
We currently don't support `Windows` for running natively, so if you're running `Windows` please rely on `Docker` as described in the [Running the app section](http://docs.democracyos.org/develop/#running-the-app), or use another form of virtualisation (e.g.: `Vagrant`, `VirtualBox`, etc.).

# Configuration

All the configuration variables are on the `/config/defaults.json` file. If you want to override any of them, you should create a new file called `development.json` on the same directory and re-write the name and the value you want.

Also, to pass configuration variables to the client we use the `client` key, which accepts an array of other configuration variable names.

So if you want to add a new variable called `example` and pass it to the client, the steps are:
1. Add the key-value pair to the `defaults.json` file, the key should be `example` and the value whatever you want. It must have a default value, even if it is an empty string.
2. If you want to pass that variable to the client, then add `'example'` to the client array in that same file.
3. If you want to test with a different value for the `example` variable, create a `development.json` file in the same directory and override the default value.

Configuration variable names must be `camelCased` as in production we match Environment Variables with Configuration Variables in this way:

In defaults.json:
```
{
  ...
  myVariable: 'default',
  myOtherVariable: {
    someProperty: 10
  }
  ...

}
```
In production, those environment variables must be:
```
MY_VARIABLE="not default"
MY_OTHER_VARIABLE_SOME_PROPERTY=20
```
As you can see, we match camelCase and nested objects with underscores, and we infer the type using the `defaults.json` file (`myVariable` is a `string` and `someProperty` is a number in this case).

# Running the app

DemocracyOS can be run in many ways, but we prefer either of these two approaches:

#### Native

Just use `make` on the project root folder; that will run the app.

For more intensive development, you might want to use `gulp bws` for development, that will wait for changes on the project files to rebuild every time. For that, we must have `gulp` installed:

```
npm install -g gulp
```

#### Docker containers

You can run your development environment inside a [`Docker`](https://www.docker.com) container pretty easily, and it will save you from installing anything on your development machine besides `Docker` itself.

First, you must have the latest version of [`Docker Engine`](https://www.docker.com/products/docker-engine) and [Docker Compose](https://www.docker.com/products/docker-compose) available in your machine.
If you're either a `Windows` or `OS/X` user, using the [`Docker Toolbox`](https://www.docker.com/products/overview#/docker_toolbox) is recommended as you may have to rely on [`Docker Machine`](https://www.docker.com/products/docker-machine) unless you get the [native `docker` apps currently on open beta](https://beta.docker.com/).

Regardless of how you got `Docker`, running the app is completely straightforward:

`$ make docker`

And that's it. If you don't have `make` available, this also works:

`$ docker-compose up app`

Every change to the files you make on your local files will get mirrored inside your development container. If you're wondering how it is that this works, see this ✨[thorough explanation](http://giphy.com/gifs/VHngktboAlxHW/fullscreen)✨.

# Folders structure

We group the code that belongs to specific parts of the app in three folders (site, admin and settings) that will generate specific front end bundle for each part, the respective folders are `/lib/site`, `/lib/admin` and `/lib/settings`
The rest of the code that are generic or are not specific of a bundle are located directly in `/lib`

# Models

All our data is stored in MongoDB and to connect with it we use [mongoose](http://mongoosejs.com/), an elegant mongodb object modeling for node.js.

Models are store on `lib/models` directory. If you want to add a new model, then do it in a new file under that directory following a similar structure as the models we have.

After that `require` it in `lib/models/index.js`.

# Database-API

For creating, reading, updating and destroying models we have a data layer which interacts with the models. It's called `db-api` and it's located under `lib/db-api`.

After creating a new model, you should include a new file under the `lib/db-api` directory with its name and `export`ing methods for:

* create
* search
* all
* findOne
* destroy
* update
* custom operation

You can take a look at the current `db-api`s we have under that directory to see some examples.

# Web API

We expose those calls from the database api via a restful api. Each `model` have a different file that follows the convention `lib/api/${modelName}/index.js`, for example: `lib/api/topic/index.js`.

These modules are added to the app on `lib/api/boot/index.js` and you should add them like:

```
app.use('/api', require('lib/api/${modelName}'));
```

So it can be accessed on the client on the URL: `/api/modelName`.

# Routing

### Server Side

If you want to add a new route to DemocracyOS, first thing to do is to create a new module under the corresponding bundle folder (for example: `lib/site`) for it and expose an `app` (an `express` instance) that requires `lib/site/layout`. For example:

In `lib/site/mypage/index.js`
```
/**
 * Module dependencies.
 */

var express = require('express');
var app = module.exports = express();

app.get('/mypage', require('lib/site/layout'));
```

And at the bottom of `lib/site/boot/index.js`
```
app.use(require('lib/site/mypage'));
```

### Client Side

This applies to `/lib/site` only because we are currently migrating the other bundles to React, if you are working on `lib/admin` or `lib/settings` check for the [DEPRECATED](#DEPRECATED) section below.

For client side routing, we use [react-router 2.6.1](https://reacttraining.com/react-router/), a client-side router that integrates with react's composability.

You should create a new route on `lib/site/boot/router.js` and include the following lines:

```
<Route path='my-route' component={MyComponent} />
```

If you are creating a page for displaying content (and not topics) you should create a react component like so:

```
import React, { Component } from 'react'

export default class YourPage extends Component {
  render () {
    return <div>Your page here</div>
  }
}
```

# Stores

Stores are the responsible for communicating with the server side API to get the needed data. A store must have a `name` function defined returning the name of the model that's related to, because it is used to build up the API URL.

For making requests to the server we recoment [fetch](https://github.com/github/fetch).

If you create a new store you should extend `lib/stores/store/store.js`.

```
import Store from lib/stores/store/store';

class MyModelStore extends Store {
  name() {
    return 'myModel'; // calls will be made to '/api/myModel'
  }
}
```

That store comes already bundled with methods like:

* findOne
* findAll
* destroy

Store uses [Promises](http://babeljs.io/docs/learn-es2015/) to make async calls, caching the promises on a private variable called `_fetches`.

To see a full example of a customized store, you can see `lib/stores/topic-store/topic-store.js`.

# DEPRECATED

### Client Side routing

For client side routing, we use [page.js](https://github.com/visionmedia/page.js), a micro client-side router inspired by the Express router.

You should create a new file on the corresponding bundle folder (eg.: `lib/admin/mypage/mypage.js`) and include the following lines:

```
import page from 'page';

page('/mypage', (ctx, next) => {
  // your page logic goes here
});
```

If you are creating a page for displaying content (and not topics) you should first empty and then render everything inside the `#content` element. You can do that simply with [component-dom](https://github.com/component/dom):

```
import o from 'component-dom';
import page from 'page';
import MyPageView from './view';

page('/mypage', (ctx, next) => {
  let myPage = new MyPageView();

  let el = o('#content');
  myPage.appendTo(el[0]);
});
```

Which leads us to... views.

### Views

To create new views (the HTML that you will show to the end user) start by creating a new file inside `lib/admin/mypage` called `view.js`.

We have our own `View` library under `lib/view/view.js` which provides you with methods for:

* `constructor`: pass a template and locals for that template
* `switchOn`: function called when the template is rendered in the DOM
* `switchOff`: function called when the template is removed from the DOM
* `appendTo`: appends the created element inside the passed element
* `bind`: used to bind event handlers
* others that you can see on `lib/view/view.js`

You shouldn't be touching this file but instead you should extend your own views from it:

```
import View from '../view/view.js';
import template from './template.jade';

export default class MyView extends View {

  constructor (topic) {
    super(template, { topic });
  }
}
```

### Templates

On the previous example, we used a template. This file is written in [jade](http://jade-lang.com/) format and it should have one and only one container element. If you write something like:

```
.topic-container
  .topic-body
    // ...
.comments
  .comment-form
    // ...
```

You have two root containers (`topic-container` and `.comments`) and you will run into problems with that. Templates can be passed in data on the `locals` objects: on the view when you call `super(template, locals)`.

### Middlewares

You will probably need the data on your pages, for that you can create a custom [middleware](https://github.com/visionmedia/page.js#routing) function.

To keep things organized we have created some modules like `lib/middlewares/topic-middlewares/topic-middlewares.js`.

Under the routes that need data, you should include these middlewares and probably you will be using a store for making those calls.

When the data is ready, then you should call `next` on that middleware, for example:

```
export function findMyModel(ctx, next) {
  myModelStore
    .findOne(ctx.params.id)
    .then(model => {
      ctx.model = model;
      next();
    })
    .catch(err => {
      if (404 !== err.status) throw err;
      log(`Unable to load model for ${ctx.params.id}`);
    });
}
```


text:
/**********************************************************************
* $Id$		usb.h		2011-06-02
*//**
* @file		usb.h
* @brief	USB Definitions
* @version	1.0
* @date		02. June. 2011
* @author	NXP MCU SW Application Team
*
* Copyright(C) 2011, NXP Semiconductor
* All rights reserved.
*
***********************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
**********************************************************************/

#ifndef __USB_H__
#define __USB_H__

#include "usbcfg.h"

#ifdef USE_USB0
#define LPC_USB LPC_USB0	// Use USB0
#else
#define LPC_USB LPC_USB1	// Use USB1
#endif
#if defined   (  __GNUC__  )
#define __packed __attribute__((__packed__))
#endif

#if defined     (  __CC_ARM  )
typedef __packed union {
#elif defined   (  __GNUC__  )
typedef union __packed {
#elif defined   (  __IAR_SYSTEMS_ICC__  )
#pragma pack(1)
typedef union {
#endif
  uint16_t W;
#if defined     (  __CC_ARM  )
  __packed struct {
#elif defined   (  __GNUC__  )
  struct __packed {
#elif defined   (  __IAR_SYSTEMS_ICC__  )
#pragma pack(1)
  struct {
#endif
    uint8_t L;
    uint8_t H;
  } WB;
#ifdef __IAR_SYSTEMS_ICC__
#pragma pack()
#endif
} WORD_BYTE;
#ifdef __IAR_SYSTEMS_ICC__
#pragma pack()
#endif

/* bmRequestType.Dir */
#define REQUEST_HOST_TO_DEVICE     0
#define REQUEST_DEVICE_TO_HOST     1

/* bmRequestType.Type */
#define REQUEST_STANDARD           0
#define REQUEST_CLASS              1
#define REQUEST_VENDOR             2
#define REQUEST_RESERVED           3

/* bmRequestType.Recipient */
#define REQUEST_TO_DEVICE          0
#define REQUEST_TO_INTERFACE       1
#define REQUEST_TO_ENDPOINT        2
#define REQUEST_TO_OTHER           3

/* bmRequestType Definition */
#if defined     (  __CC_ARM  )
typedef __packed union _REQUEST_TYPE {
#elif defined   (  __GNUC__  )
typedef union __packed _REQUEST_TYPE {
#elif defined   (  __IAR_SYSTEMS_ICC__  )
#pragma pack(1)
typedef union _REQUEST_TYPE {
#endif
#if defined     (  __CC_ARM  )
	__packed struct _BM {
#elif defined   (  __GNUC__  )
	struct __packed _BM {
#elif defined   (  __IAR_SYSTEMS_ICC__  )
#pragma pack(1)
	struct _BM {
#endif
    uint8_t Recipient : 5;
    uint8_t Type      : 2;
    uint8_t Dir       : 1;
  } BM;
#ifdef __IAR_SYSTEMS_ICC__
#pragma pack()
#endif
  uint8_t B;
} REQUEST_TYPE;
#ifdef __IAR_SYSTEMS_ICC__
#pragma pack()
#endif

/* USB Standard Request Codes */
#define USB_REQUEST_GET_STATUS                 0
#define USB_REQUEST_CLEAR_FEATURE              1
#define USB_REQUEST_SET_FEATURE                3
#define USB_REQUEST_SET_ADDRESS                5
#define USB_REQUEST_GET_DESCRIPTOR             6
#define USB_REQUEST_SET_DESCRIPTOR             7
#define USB_REQUEST_GET_CONFIGURATION          8
#define USB_REQUEST_SET_CONFIGURATION          9
#define USB_REQUEST_GET_INTERFACE              10
#define USB_REQUEST_SET_INTERFACE              11
#define USB_REQUEST_SYNC_FRAME                 12

/* USB GET_STATUS Bit Values */
#define USB_GETSTATUS_SELF_POWERED             0x01
#define USB_GETSTATUS_REMOTE_WAKEUP            0x02
#define USB_GETSTATUS_ENDPOINT_STALL           0x01

/* USB Standard Feature selectors */
#define USB_FEATURE_ENDPOINT_STALL             0
#define USB_FEATURE_REMOTE_WAKEUP              1
#define USB_FEATURE_TEST_MODE                  2

/* USB Default Control Pipe Setup Packet */
#if defined     (  __CC_ARM  )
typedef __packed struct _USB_SETUP_PACKET {
#elif defined   (  __GNUC__  )
typedef struct __packed _USB_SETUP_PACKET {
#elif defined   (  __IAR_SYSTEMS_ICC__  )
#pragma pack(1)
typedef struct _USB_SETUP_PACKET {
#endif
  REQUEST_TYPE bmRequestType;
  uint8_t         bRequest;
  WORD_BYTE    wValue;
  WORD_BYTE    wIndex;
  uint16_t         wLength;
} USB_SETUP_PACKET;
#ifdef __IAR_SYSTEMS_ICC__
#pragma pack()
#endif

/* USB Descriptor Types */
#define USB_DEVICE_DESCRIPTOR_TYPE                  1
#define USB_CONFIGURATION_DESCRIPTOR_TYPE           2
#define USB_STRING_DESCRIPTOR_TYPE                  3
#define USB_INTERFACE_DESCRIPTOR_TYPE               4
#define USB_ENDPOINT_DESCRIPTOR_TYPE                5
#define USB_DEVICE_QUALIFIER_DESCRIPTOR_TYPE        6
#define USB_OTHER_SPEED_CONFIG_DESCRIPTOR_TYPE      7
#define USB_INTERFACE_POWER_DESCRIPTOR_TYPE         8
#define USB_OTG_DESCRIPTOR_TYPE                     9
#define USB_DEBUG_DESCRIPTOR_TYPE                  10
#define USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE  11

/* USB Device Classes */
#define USB_DEVICE_CLASS_RESERVED              0x00
#define USB_DEVICE_CLASS_AUDIO                 0x01
#define USB_DEVICE_CLASS_COMMUNICATIONS        0x02
#define USB_DEVICE_CLASS_HUMAN_INTERFACE       0x03
#define USB_DEVICE_CLASS_MONITOR               0x04
#define USB_DEVICE_CLASS_PHYSICAL_INTERFACE    0x05
#define USB_DEVICE_CLASS_POWER                 0x06
#define USB_DEVICE_CLASS_PRINTER               0x07
#define USB_DEVICE_CLASS_STORAGE               0x08
#define USB_DEVICE_CLASS_HUB                   0x09
#define USB_DEVICE_CLASS_MISCELLANEOUS         0xEF
#define USB_DEVICE_CLASS_VENDOR_SPECIFIC       0xFF

/* bmAttributes in Configuration Descriptor */
#define USB_CONFIG_POWERED_MASK                0x40
#define USB_CONFIG_BUS_POWERED                 0x80
#define USB_CONFIG_SELF_POWERED                0xC0
#define USB_CONFIG_REMOTE_WAKEUP               0x20

/* bMaxPower in Configuration Descriptor */
#define USB_CONFIG_POWER_MA(mA)                ((mA)/2)

/* bEndpointAddress in Endpoint Descriptor */
#define USB_ENDPOINT_DIRECTION_MASK            0x80
#define USB_ENDPOINT_OUT(addr)                 ((addr) | 0x00)
#define USB_ENDPOINT_IN(addr)                  ((addr) | 0x80)

/* bmAttributes in Endpoint Descriptor */
#define USB_ENDPOINT_TYPE_MASK                 0x03
#define USB_ENDPOINT_TYPE_CONTROL              0x00
#define USB_ENDPOINT_TYPE_ISOCHRONOUS          0x01
#define USB_ENDPOINT_TYPE_BULK                 0x02
#define USB_ENDPOINT_TYPE_INTERRUPT            0x03
#define USB_ENDPOINT_SYNC_MASK                 0x0C
#define USB_ENDPOINT_SYNC_NO_SYNCHRONIZATION   0x00
#define USB_ENDPOINT_SYNC_ASYNCHRONOUS         0x04
#define USB_ENDPOINT_SYNC_ADAPTIVE             0x08
#define USB_ENDPOINT_SYNC_SYNCHRONOUS          0x0C
#define USB_ENDPOINT_USAGE_MASK                0x30
#define USB_ENDPOINT_USAGE_DATA                0x00
#define USB_ENDPOINT_USAGE_FEEDBACK            0x10
#define USB_ENDPOINT_USAGE_IMPLICIT_FEEDBACK   0x20
#define USB_ENDPOINT_USAGE_RESERVED            0x30

/* USB Standard Device Descriptor */
#if defined     (  __CC_ARM  )
typedef __packed struct _USB_DEVICE_DESCRIPTOR {
#elif defined   (  __GNUC__  )
typedef struct __packed _USB_DEVICE_DESCRIPTOR {
#elif defined   (  __IAR_SYSTEMS_ICC__  )
#pragma pack(1)
typedef struct _USB_DEVICE_DESCRIPTOR {
#endif
  uint8_t  bLength;
  uint8_t  bDescriptorType;
  uint16_t  bcdUSB;
  uint8_t  bDeviceClass;
  uint8_t  bDeviceSubClass;
  uint8_t  bDeviceProtocol;
  uint8_t  bMaxPacketSize0;
  uint16_t  idVendor;
  uint16_t  idProduct;
  uint16_t  bcdDevice;
  uint8_t  iManufacturer;
  uint8_t  iProduct;
  uint8_t  iSerialNumber;
  uint8_t  bNumConfigurations;
} USB_DEVICE_DESCRIPTOR;
#ifdef __IAR_SYSTEMS_ICC__
#pragma pack()
#endif

/* USB 2.0 Device Qualifier Descriptor */
#if defined     (  __CC_ARM  )
typedef __packed struct _USB_DEVICE_QUALIFIER_DESCRIPTOR {
#elif defined   (  __GNUC__  )
typedef struct __packed _USB_DEVICE_QUALIFIER_DESCRIPTOR {
#elif defined   (  __IAR_SYSTEMS_ICC__  )
#pragma pack(1)
typedef struct _USB_DEVICE_QUALIFIER_DESCRIPTOR {
#endif
  uint8_t  bLength;
  uint8_t  bDescriptorType;
  uint16_t  bcdUSB;
  uint8_t  bDeviceClass;
  uint8_t  bDeviceSubClass;
  uint8_t  bDeviceProtocol;
  uint8_t  bMaxPacketSize0;
  uint8_t  bNumConfigurations;
  uint8_t  bReserved;
} USB_DEVICE_QUALIFIER_DESCRIPTOR;
#ifdef __IAR_SYSTEMS_ICC__
#pragma pack()
#endif

/* USB Standard Configuration Descriptor */
#if defined     (  __CC_ARM  )
typedef __packed struct _USB_CONFIGURATION_DESCRIPTOR {
#elif defined   (  __GNUC__  )
typedef struct __packed _USB_CONFIGURATION_DESCRIPTOR {
#elif defined   (  __IAR_SYSTEMS_ICC__  )
#pragma pack(1)
typedef struct _USB_CONFIGURATION_DESCRIPTOR {
#endif
  uint8_t  bLength;
  uint8_t  bDescriptorType;
  uint16_t  wTotalLength;
  uint8_t  bNumInterfaces;
  uint8_t  bConfigurationValue;
  uint8_t  iConfiguration;
  uint8_t  bmAttributes;
  uint8_t  bMaxPower;
} USB_CONFIGURATION_DESCRIPTOR;
#ifdef __IAR_SYSTEMS_ICC__
#pragma pack()
#endif

/* USB Standard Interface Descriptor */
#if defined     (  __CC_ARM  )
typedef __packed struct _USB_INTERFACE_DESCRIPTOR {
#elif defined   (  __GNUC__  )
typedef struct __packed _USB_INTERFACE_DESCRIPTOR {
#elif defined   (  __IAR_SYSTEMS_ICC__  )
#pragma pack(1)
typedef struct _USB_INTERFACE_DESCRIPTOR {
#endif
  uint8_t  bLength;
  uint8_t  bDescriptorType;
  uint8_t  bInterfaceNumber;
  uint8_t  bAlternateSetting;
  uint8_t  bNumEndpoints;
  uint8_t  bInterfaceClass;
  uint8_t  bInterfaceSubClass;
  uint8_t  bInterfaceProtocol;
  uint8_t  iInterface;
} USB_INTERFACE_DESCRIPTOR;
#ifdef __IAR_SYSTEMS_ICC__
#pragma pack()
#endif

/* USB Standard Endpoint Descriptor */
#if defined     (  __CC_ARM  )
typedef __packed struct _USB_ENDPOINT_DESCRIPTOR {
#elif defined   (  __GNUC__  )
typedef struct __packed _USB_ENDPOINT_DESCRIPTOR {
#elif defined   (  __IAR_SYSTEMS_ICC__  )
#pragma pack(1)
typedef struct _USB_ENDPOINT_DESCRIPTOR {
#endif
  uint8_t  bLength;
  uint8_t  bDescriptorType;
  uint8_t  bEndpointAddress;
  uint8_t  bmAttributes;
  uint16_t  wMaxPacketSize;
  uint8_t  bInterval;
} USB_ENDPOINT_DESCRIPTOR;
#ifdef __IAR_SYSTEMS_ICC__
#pragma pack()
#endif

/* USB String Descriptor */
#if defined     (  __CC_ARM  )
typedef __packed struct _USB_STRING_DESCRIPTOR {
#elif defined   (  __GNUC__  )
typedef struct __packed _USB_STRING_DESCRIPTOR {
#elif defined   (  __IAR_SYSTEMS_ICC__  )
#pragma pack(1)
typedef struct _USB_STRING_DESCRIPTOR {
#endif
  uint8_t  bLength;
  uint8_t  bDescriptorType;
  uint16_t  bString/*[]*/;
} USB_STRING_DESCRIPTOR;
#ifdef __IAR_SYSTEMS_ICC__
#pragma pack()
#endif

/* USB Common Descriptor */
#if defined     (  __CC_ARM  )
typedef __packed struct _USB_COMMON_DESCRIPTOR {
#elif defined   (  __GNUC__  )
typedef struct __packed _USB_COMMON_DESCRIPTOR {
#elif defined   (  __IAR_SYSTEMS_ICC__  )
#pragma pack(1)
typedef struct _USB_COMMON_DESCRIPTOR {
#endif
  uint8_t  bLength;
  uint8_t  bDescriptorType;
} USB_COMMON_DESCRIPTOR;

/* USB Other Speed Configuration */
#if defined     (  __CC_ARM  )
typedef __packed struct _USB_OTHER_SPEED_CONFIGURATION {
#elif defined   (  __GNUC__  )
typedef struct __packed _USB_OTHER_SPEED_CONFIGURATION {
#elif defined   (  __IAR_SYSTEMS_ICC__  )
#pragma pack(1)
typedef struct _USB_OTHER_SPEED_CONFIGURATION {
#endif

  uint8_t  bLength;
  uint8_t  bDescriptorType;
  uint16_t wTotalLength;
  uint8_t  bNumInterfaces;
  uint8_t  bConfigurationValue;
  uint8_t  IConfiguration;
  uint8_t  bmAttributes;
  uint8_t  bMaxPower;
} USB_OTHER_SPEED_CONFIGURATION;

/* USB Endpoint Callback Events */
#define USB_EVT_SETUP       1   /* Setup Packet */
#define USB_EVT_OUT         2   /* OUT Packet */
#define USB_EVT_IN          3   /*  IN Packet */
#define USB_EVT_OUT_NAK     4   /* OUT Packet - Not Acknowledged */
#define USB_EVT_IN_NAK      5   /*  IN Packet - Not Acknowledged */
#define USB_EVT_OUT_STALL   6   /* OUT Packet - Stalled */
#define USB_EVT_IN_STALL    7   /*  IN Packet - Stalled */
#define USB_EVT_OUT_DMA_EOT 8   /* DMA OUT EP - End of Transfer */
#define USB_EVT_IN_DMA_EOT  9   /* DMA  IN EP - End of Transfer */
#define USB_EVT_OUT_DMA_NDR 10  /* DMA OUT EP - New Descriptor Request */
#define USB_EVT_IN_DMA_NDR  11  /* DMA  IN EP - New Descriptor Request */
#define USB_EVT_OUT_DMA_ERR 12  /* DMA OUT EP - Error */
#define USB_EVT_IN_DMA_ERR  13  /* DMA  IN EP - Error */

/* call back structure */
typedef struct _USB_INIT_
{
  uint32_t ep0_maxp;
  /* USB Device Events Callback Functions */
  void (* USB_Power_Event)(uint32_t  power);
  void (* USB_Reset_Event)(void);
  void (* USB_Suspend_Event)(void);
  void (* USB_Resume_Event)(void);
  void (* USB_WakeUp_Event)(void);
  void (* USB_SOF_Event)(void);
  void (* USB_Error_Event)(uint32_t error);
  /* USB Core Events Callback Functions */
  void (* USB_Configure_Event)(void);
  void (* USB_Interface_Event)(void);
  void (* USB_Feature_Event)(void);
  /* USB Endpoint Events Callback Pointers */
  void (* USB_P_EP[4])(uint32_t event);
} LPC_USBDRV_INIT_T;

#ifdef __IAR_SYSTEMS_ICC__
#pragma pack()
#endif

#endif  /* __USB_H__ */


text:
import os
import sys
import platform
import flare
import argparse

flare.ReIm_npts=32768
parnames=["m1","m2","tRef","phiRef","distance","lambda","beta","inclination","polarization"]
Npar=len(parnames)

def read_params(filename):
    keys=[]
    vals=[]
    print "Reading params from file '"+filename+"'"
    with open(filename,'r') as f:
        line="#"
        while(not "Injection parameters" in line): line=f.readline() #Skip any front matter
        line=f.readline() #skip one line
        line=f.readline()
        while(not "---------" in line):
            cols = line.split()
            keys.append(cols[0][:-1])
            vals.append(float(cols[1]))
            line=f.readline()
    parbyname=dict(zip(keys, vals))
    #print "parbyname:\n",parbyname
    pars=[parbyname[name] for name in parnames]
    return pars

script_discover="""#!/usr/bin/csh
###This file autogenerated by flare_submit.py
#SBATCH --job-name=SCRIPT_NAME
#SBATCH --nodes=SCRIPT_NODES --ntasks=SCRIPT_TASKS --constraint=hasw
#SBATCH --time=SCRIPT_HOURS:SCRIPT_MINS:00
#SBATCH --account=s0982
SCRIPT_DEBUG

module purge
module load comp/intel-15.0.3.187 lib/mkl-15.0.3.187 mpi/impi-5.0.3.048
module load other/SSSO_Ana-PyD/SApd_2.4.0_py2.7
setenv ROM_DATA_PATH SCRIPT_ROM_DATA_PATH
set outdir=./SCRIPT_TAG-ntSCRIPT_TASKS
mkdir -p $outdir
cd $outdir
setenv OMP_NUM_THREADS SCRIPT_THREADS;
SCRIPT_RUNNING_CMD
#Note: The following construction kills the run after SCRIPT_HOURS:SCRIPT_MINS:00, to mimic queue submission
sh -c '{  \\
  SCRIPT_FISHER_COMMAND ; \\
  time mpirun -np SCRIPT_TASKS SCRIPT_COMMAND 1>SCRIPT_NAME.out 2>&1; \\
  SCRIPT_TEST_CMD; \\
  kill 0 -s INT; \\
} | { \\
  sleep SCRIPT_KILL_AFTER_SECONDS; \\
  SCRIPT_RESTART_CMD; \\
  pkill -g 0 SCRIPT_EXEC_NAME; \\
  echo SCRIPT_EXEC_NAME killed after timeout\\
  kill 0 -s INT; \\
}; '
echo "Exiting"
#exit 0
"""

script_macos="""#!/usr/bin/csh
###This file autogenerated by flare_submit.py
setenv ROM_DATA_PATH SCRIPT_ROM_DATA_PATH
set outdir=./SCRIPT_TAG-ntSCRIPT_TASKS
mkdir -p $outdir
cd $outdir
setenv OMP_NUM_THREADS SCRIPT_THREADS;
SCRIPT_RUNNING_CMD
#Note: The following construction kills the run after SCRIPT_HOURS:SCRIPT_MINS:00, to mimic queue submission
sh -c '{  \\
  SCRIPT_FISHER_COMMAND ; \\
  time SCRIPT_COMMAND 1>SCRIPT_NAME.out 2>&1; \\
  sleep 1; \\
  if [ ! -f killed.flag ]; then SCRIPT_TEST_CMD; fi; \\
  exit 0; \\
} | { \\
  sleep SCRIPT_KILL_AFTER_SECONDS; \\
  SCRIPT_RESTART_CMD; \\
  echo > killed.flag \\
  pkill -g 0 SCRIPT_EXEC_NAME; echo SCRIPT_EXEC_NAME killed after timeout\\
  exit 0; \\
}; '
rm -f killed.flag
echo "Exiting"
#exit 0
"""

def generate(system,argv,rcname=""):
    if(system=="discover"):
        script=script_discover
        procs_per_node=24
    elif(system=="macos"):
        script=script_macos
        procs_per_node=8
    else:
        sys.exit("System '"+str(system)+"' not recognized.")

    #set up command-line arguments
    parser = argparse.ArgumentParser(description="generate a discover queue script for a flare run");
    paramsgroup = parser.add_mutually_exclusive_group(required=True)
    parser.add_argument('name',help="The basename for the run")
    parser.add_argument('snr',help="SNR for the run",type=float)
    paramsgroup.add_argument('-p',help="List "+str(Npar)+" injection parameters for the run",nargs=Npar,type=float)
    paramsgroup.add_argument('-f',help="Provide filename with injection parameters")
    parser.add_argument('-T',help="Specify BAMBI tol",type=float,default=1.0)
    parser.add_argument('-d',help="Run in debug mode",action="store_true")
    parser.add_argument('--mcmc',help="Run with ptmcmc instead of bambi",action="store_true")
    parser.add_argument('-n',help="Number of bambi-nodes or ptmcmc-threads to run on (default 1 node or "+str(procs_per_node)+" threads)",type=int,default=-1)
    parser.add_argument('-t',help="Time in hours to run (default 12, but always 1 or 0 with debug)",type=int,default=-1)
    parser.add_argument('-m',help="Time: addtional minutes to run (default 00)",type=int,default=0)
    parser.add_argument('--lm22',help="Use only the 22 mode.",action="store_true")
    parser.add_argument('--live',help="Set number of nested sampling live points for bambi default=4000).",default=4000)
    parser.add_argument('--nonMultimodal',help="Do not use BAMBI multimodal option",action="store_true")
    parser.add_argument('-r',help="Restart directory (default ""=no restart)",default="")
    parser.add_argument('--noFish',help="Skip initial fisher call",action="store_true")

    args=parser.parse_args(argv)

    #We assume that the script is located in the flare/python directory
    #below [:-7] is to strip "/python
    #flare_path= os.path.dirname(os.path.realpath(__file__))[:-7]
    flare_path=flare.flare_dir

    name=args.name
    hours=args.t
    minutes=args.m
    nodes=args.n

    if(hours<0):
        if(minutes>0):hours=0
        else:hours=12

    #Setup flare flags
    if(args.p is None):
        params=read_params(args.f)
    else:
        params=args.p
    flags=flare.set_flare_flags(args.snr,params)
    if(args.lm22):flags+=" --nbmodeinj 1 --nbmodetemp 1 " #for no higher modes in injection and template

    #generate fisher command
    fcmd   = flare.getFisherCommand(name+"-Fisher")+" "+flags

    #tidy-up commands
    if(rcname==""):
        running_cmd=""
        restart_cmd="echo Need to restart"
        done_cmd="echo Done"
    else:
        run_control="python "+flare_path+"/python/run_control.py "+rcname+" --set "+name+" "
        running_cmd=run_control+"running"
        restart_cmd=run_control+"restart_at_${res}"
        done_cmd=run_control+"done"

    #process time info
    if args.d:
        debug="#SBATCH --qos=debug\n"
        if(60*hours+minutes>60):
            hours=1
            minutes=0
    else:
        debug=""

    total_seconds=3600*hours+60*minutes-200
    checkp_seconds=(int)((total_seconds-200)*0.99)
    checkp_hours=checkp_seconds/3600.0
    print "checkp_secs=",checkp_seconds
    print "checkp_hours=",checkp_hours

    #generate the sampler command
    if(not args.mcmc):
        sampler="bambi"
        if(nodes<1):nodes=1
        tasks=nodes*(procs_per_node-3)
        fthreads=procs_per_node-3
        threads=1
        exec_name   = "/LISAinference/LISAinference"
        flags += flare.set_bambi_flags(name,nlive=args.live,tol=args.T,multimodal=(not args.nonMultimodal))
        if(len(args.r)>0):flags += " --resume"
        test_cmd=done_cmd
        new_res="1";
        if(len(args.r)>0):new_res=str(int(args.r)+1)
        restart_cmd="res="+new_res+"; "+restart_cmd
    else:
        sampler="ptmcmc"
        if(nodes<1):nodes=procs_per_node
        threads=nodes
        fthreads=threads
        nodes=1
        tasks=1
        exec_name   = "/LISAinference/LISAinference_ptmcmc"
        flags += flare.set_mcmc_flags(name,60) + " --noFisher "
        flags += "--checkp_at_time="+str(checkp_hours)+" "
        if(len(args.r)>0):
            if("X" in args.r):
                flags += "--restart_dir=`ls -1rtd *-cp|tail -n 1`"
            else:
                flags += "--restart_dir="+args.r+" "
        #flags += "--covariance_file="+name+"-Fisher_fishcov.dat"
        #test_cmd="if ( ls -ort | tail -n 3 | grep 'step_.*-cp' - ) ; then "+restart_cmd+"; else "+done_cmd+"; fi "
        #restart_cmd="res=`ls -rt test0-ptmcmc-nt1/ | tail -n 3 | grep 'step_.*-cp'`"
        test_cmd="echo test "
        restart_cmd="echo restart"
    #finish commands
    cmd = flare_path+"/"+exec_name+" "+flags
    fcmd = "export OMP_NUM_THREADS="+str(fthreads)+" ; "+fcmd+" 1>"+name+"-Fisher.out 2>&1"
    exec_name=os.path.basename(exec_name)

    #do we really want to run Fisher?
    if(len(args.r)>0 or args.noFish):
        fcmd="echo"

    log_name=name
    if(len(args.r)>0):log_name=name+"."+args.r

    #Replace tags:
    #SCRIPT_NAME
    script = script.replace("SCRIPT_NAME", log_name)
    #SCRIPT_SAMPLER
    tag=name+"-"+sampler
    if(not args.T==1.0):tag+="-"+str(args.T)
    if(args.nonMultimodal):tag+="-nonMM"
    script = script.replace("SCRIPT_TAG", tag)
    #SCRIPT_NODES
    script = script.replace("SCRIPT_NODES", str(nodes))
    #SCRIPT_HOURS
    script = script.replace("SCRIPT_HOURS", str(hours))
    #SCRIPT_MINS
    script = script.replace("SCRIPT_MINS", str(minutes))
    #SCRIPT_TASKS
    script = script.replace("SCRIPT_TASKS", str(tasks))
    #SCRIPT_THREADS
    script = script.replace("SCRIPT_THREADS", str(threads))
    #SCRIPT_COMMAND
    script = script.replace("SCRIPT_COMMAND", cmd)
    #SCRIPT_FISHER_COMMAND
    script = script.replace("SCRIPT_FISHER_COMMAND", fcmd )
    #SCRIPT_DEBUG
    script = script.replace("SCRIPT_DEBUG", debug)
    #SCRIPT_EXEC_NAME
    script = script.replace("SCRIPT_EXEC_NAME", exec_name)
    #SCRIPT_KILL_AFTER_SECONDS
    script = script.replace("SCRIPT_KILL_AFTER_SECONDS", str(total_seconds))
    #SCRIPT_ROM_DATA_PATH
    script = script.replace("SCRIPT_ROM_DATA_PATH", flare_path+"/"+flare.ROM_DATA_PATH)
    #run_control:
    script = script.replace("SCRIPT_RUNNING_CMD", running_cmd)
    script = script.replace("SCRIPT_RESTART_CMD", restart_cmd)
    script = script.replace("SCRIPT_TEST_CMD", test_cmd)

    #print script

    script_name=tag+".sub"
    if(len(args.r)>0):script_name=script_name+"."+args.r

    print "writing  script: ",script_name
    with open(script_name,'w') as file:
        file.write(script)
        file.close()
    return script_name

if __name__ == "__main__":
    print "Running flare_submit.generate as main program."
    #detect system
    system="discover"
    if(platform.system()=="Darwin"):
        system="macos"
    generate(system,sys.argv[1:])


text:
        {{ 'FILTER_ADD_CRITERION' | translate }}

        Key
        Value

          {{ 'FILTER_REMOVE_CRITERION' | translate }}

            <select class="form-control"
                    ng-model="queryParam.key"
                    name="queryParamKey"
                    ng-change="valueChanged(queryParam, this.criteriaFieldForm.queryParamValue)"
                    cam-unique-value="{{ getQueryParamKeys() }}"
                    required
                    ng-disabled="filter.id && !accesses.update">
              <optgroup ng-repeat="criteriaGroup in criteria"
                        label="{{ criteriaGroup.group | translate }}">

                <option ng-repeat="criterion in criteriaGroup.options"
                        ng-selected="criterion.name === getCriterionName(queryParam.key)"
                        ng-value="criterion.name">

                  {{ criterion.label | translate }}
                  {{ (criterion.expressionSupport ? '*' : '') }}

                </option>
              </optgroup>

            </select>

                  {{ 'REQUIRED_FIELD' | translate }}

                  {{ 'REQUIRE_UNIQUE_KEY' | translate }}

              <input class="form-control"
                     name="queryParamValue"
                     type="text"
                     ng-model="queryParam.value"
                     ng-change="valueChanged(queryParam, this.criteriaFieldForm.queryParamValue)"
                     required
                     ng-readonly="filter.id && !accesses.update"
                     ng-disabled="filter.id && !accesses.update" />

                {{ getCriteriaHelp(queryParam.key) | translate }}

                  {{ 'REQUIRED_FIELD' | translate }}

                  {{ 'REQUIRED_INTEGER_FIELD' | translate }}

                  {{ 'INVALID_DATE' | translate }}


text:
$(document).ready(function(){
	$('#signup').submit(function(e){
		removeFeedback();
		var errors=validateForm();
		if(errors==""){
			return true;
		}
		else{
			provideFeedback(errors);
			e.preventDefault();
			return false;
		}
	});
		var illegalfields=new Array();
		var errorfields=new Array();
		var error=new Array();

		function validateForm(){

			var namelegal=/(?:^[A-Z][a-z]+$)/g;
			var phonelegal=/^[0-9]{4}-[0-9]{7}$/g;
			var emaillegal=/^[a-z_][a-z0-9]+(?:[-._][a-z0-9]+)*@[a-z]+(?:[-._][a-z0-9]+)*\.[a-z]+$/g;
			if($('#fname').val()==""){
				errorfields.push('fname');
			}
			//if ($('#email').val()=="") {
			//	errorfields.push('email');
			//}
			if ($('#password1').val()=="") {
				errorfields.push('password1');
			}
			if ($('#mobile').val()=="") {
				errorfields.push('mobile');
			}
			if ($('#password2').val()=="") {
				errorfields.push('password2');
			}

			var name_ok=namelegal.exec($('#fname').val());
			if(!name_ok){
				illegalfields.push('fname');
			}
			var phone_ok=phonelegal.exec($('#mobile').val());
			if(!phone_ok){
				illegalfields.push('mobile');
			}
			//var email_ok=emaillegal.exec($('#email').val());
			//if(!email_ok){
				//illegalfields.push('email');
			//}
			if($('#password2').val()!=$('#password1').val()){
				illegalfields.push('password2');
			}
			//for (var i = 0; i <nameillegal.length; i++) {
			//	if ($("#name").val()==nameillegal[i]) {
			//		illegalfields.push('name');
				//}
		    //}
		    if(illegalfields!=""){
		    	error.push("illegalfields");

			}

			if(errorfields!=""){
				error.push("errorfields");
			}
			return error;
		}

	function provideFeedback(incomingerrors){
		for (var i = 0; i < incomingerrors.length; i++) {
			if(incomingerrors[i]=="illegalfields"){
				for (var j = 0; j < illegalfields.length; j++) {
					if(illegalfields[j]=="fname"){
						$("#fname").addClass('errorclass');
						$("#fnameerror").html("Not a Valid Name.Please enter in correct Name Format");
						$("#fnameerror").css("visibility","visible");
					}
					if(illegalfields[j]=="phone"){
						$("#mobile").addClass('errorclass');
						$("#mobileerror").html("Not a Valid Phone Number.Please enter in correct Format");
						$("#mobileerror").css("visibility","visible");

					}
					if(illegalfields[j]=="email"){
						$("#email").addClass('errorclass');
						$("#emailerror").html("Not a Valid Email.Please enter correct email");
						$("#emailerror").css("visibility","visible");

					}
					if(illegalfields[j]=="password2"){
						$("#password2").addClass('errorclass');
						$("#password2error").html("Passwords do not match");
						$("#password2error").css("visibility","visible");
					}
				}
			}
			if(incomingerrors[i]=="errorfields"){
				for(var j=0; j<errorfields.length;j++){
					if(errorfields[j]=="name"){
						$("#fname").addClass('errorclass');
						$("#fnameerror").html("Name is required");
						$("#fnameerror").css("visibility","visible");
					}
					if (errorfields[j]=="phone") {
						$("#mobile").addClass('errorclass');
						$("#mobileerror").html("Phone Number is required");
				        $("#mobileerror").css("visibility","visible");
					}
					if (errorfields[j]=="email") {
						$("#email").addClass('errorclass');
						$("#emailerror").html("Email is required");
				        $("#emailerror").css("visibility","visible");
					}
					if (errorfields[j]=="password1") {
						$("#password1").addClass('errorclass');
						$("#password1error").html("Password is required");
				        $("#password1error").css("visibility","visible");
					}
					if (errorfields[j]=="password2") {
						$("#password2").addClass('errorclass');
						$("#password2error").html("Password verification is required");
				        $("#password2error").css("visibility","visible");
					}
				}
			}
		}

		//for(var i=0;i<incomingerrors.length;i++){
			//$('#'+incomingerrors[i]).addClass("errorclass");
			//$('#'+incomingerrors[i]+'error').removeClass("errorfeedback");
		//}
		//$("#errordiv").html("errors encountered");
	}

	function removeFeedback(){
		illegalfields=[];
		errorfields=[];
		error=[];
		$("input").each(function(){
			$(this).removeClass("errorclass");
		});
		$(".errorspan").each(function(){
			$(this).css("visibility","hidden");
	});
   }
});


text:
<!DOCTYPE html>

        <svg width="500" height="500">
            <path class="squiggles" style="stroke:black;fill:none;" stroke="black" d="M0,250Q250,125,250,250"></path>
        </svg>


text:
<stix:STIX_Package
	xmlns:cyboxCommon="http://cybox.mitre.org/common-2"
	xmlns:cybox="http://cybox.mitre.org/cybox-2"
	xmlns:cyboxVocabs="http://cybox.mitre.org/default_vocabularies-2"
	xmlns:example="http://example.com"
	xmlns:incident="http://stix.mitre.org/Incident-1"
	xmlns:ta="http://stix.mitre.org/ThreatActor-1"
	xmlns:stixCommon="http://stix.mitre.org/common-1"
	xmlns:stixVocabs="http://stix.mitre.org/default_vocabularies-1"
	xmlns:stix-ciqidentity="http://stix.mitre.org/extensions/Identity#CIQIdentity3.0-1"
	xmlns:stix="http://stix.mitre.org/stix-1"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:xal="urn:oasis:names:tc:ciq:xal:3"
	xmlns:xnl="urn:oasis:names:tc:ciq:xnl:3"
	xmlns:xpil="urn:oasis:names:tc:ciq:xpil:3"
	xsi:schemaLocation="
	http://cybox.mitre.org/common-2 http://cybox.mitre.org/XMLSchema/common/2.1/cybox_common.xsd
	http://cybox.mitre.org/cybox-2 http://cybox.mitre.org/XMLSchema/core/2.1/cybox_core.xsd
	http://cybox.mitre.org/default_vocabularies-2 http://cybox.mitre.org/XMLSchema/default_vocabularies/2.1/cybox_default_vocabularies.xsd
	http://stix.mitre.org/Incident-1 http://stix.mitre.org/XMLSchema/incident/1.1.1/incident.xsd
	http://stix.mitre.org/ThreatActor-1 http://stix.mitre.org/XMLSchema/threat_actor/1.1.1/threat_actor.xsd
	http://stix.mitre.org/common-1 http://stix.mitre.org/XMLSchema/common/1.1.1/stix_common.xsd
	http://stix.mitre.org/default_vocabularies-1 http://stix.mitre.org/XMLSchema/default_vocabularies/1.1.1/stix_default_vocabularies.xsd
	http://stix.mitre.org/extensions/Identity#CIQIdentity3.0-1 http://stix.mitre.org/XMLSchema/extensions/identity/ciq_3.0/1.1.1/ciq_3.0_identity.xsd
	http://stix.mitre.org/stix-1 http://stix.mitre.org/XMLSchema/core/1.1.1/stix_core.xsd
	urn:oasis:names:tc:ciq:xal:3 http://stix.mitre.org/XMLSchema/external/oasis_ciq_3.0/xAL.xsd
	urn:oasis:names:tc:ciq:xnl:3 http://stix.mitre.org/XMLSchema/external/oasis_ciq_3.0/xNL.xsd
	urn:oasis:names:tc:ciq:xpil:3 http://stix.mitre.org/XMLSchema/external/oasis_ciq_3.0/xPIL.xsd" id="example:Package-38026bfe-209e-4492-990f-f8b0c5fdf4e9" version="1.1.1" timestamp="2014-11-24T16:59:31+00:00">
    <stix:Incidents>
        <stix:Incident id="example:incident-82436f1a-37d4-4394-89d4-da543b0c3cf1" timestamp="2014-11-24T16:59:31+00:00" xsi:type='incident:IncidentType'>
            <incident:Title>Two letters were placed in the same envelope and sent to Patient A. Patient A to mail letter back to PO. Copy of letter mailed to Patient B. Letter contained Patient B's name, address, and medical information.</incident:Title>
            <incident:External_ID source="VERIS">DCB13C6C-4E88-45D1-9F0D-CFAC67C8BD32</incident:External_ID>
            <incident:Time>
                <incident:Initial_Compromise precision="day">2010-05-12T00:00:00</incident:Initial_Compromise>
            </incident:Time>
            <incident:Reporter>
                <stixCommon:Identity xsi:type='stix-ciqidentity:CIQIdentity3.0InstanceType'>
                    <ExtSch:Specification xmlns:ExtSch="http://stix.mitre.org/extensions/Identity#CIQIdentity3.0-1">
  <xpil:PartyName xmlns:xpil="urn:oasis:names:tc:ciq:xpil:3">
    <xnl:NameLine xmlns:xnl="urn:oasis:names:tc:ciq:xnl:3">swidup</xnl:NameLine>
  </xpil:PartyName>
</ExtSch:Specification>
                </stixCommon:Identity>
            </incident:Reporter>
            <incident:Victim xsi:type='stix-ciqidentity:CIQIdentity3.0InstanceType'>
                <ExtSch:Specification xmlns:ExtSch="http://stix.mitre.org/extensions/Identity#CIQIdentity3.0-1">
  <xpil:PartyName xmlns:xpil="urn:oasis:names:tc:ciq:xpil:3">
    <xnl:NameLine xmlns:xnl="urn:oasis:names:tc:ciq:xnl:3">United States Department of Veterans Affairs</xnl:NameLine>
  </xpil:PartyName>
  <xpil:Addresses xmlns:xpil="urn:oasis:names:tc:ciq:xpil:3">
    <xpil:Address>
      <xal:Country xmlns:xal="urn:oasis:names:tc:ciq:xal:3">
        <xal:NameElement>US</xal:NameElement>
      </xal:Country>
      <xal:AdministrativeArea xmlns:xal="urn:oasis:names:tc:ciq:xal:3">
        <xal:NameElement>NE</xal:NameElement>
      </xal:AdministrativeArea>
    </xpil:Address>
  </xpil:Addresses>
</ExtSch:Specification>
            </incident:Victim>
            <incident:Affected_Assets>
                <incident:Affected_Asset>
                    <incident:Type>Documents</incident:Type>
                    <incident:Nature_Of_Security_Effect>
                        <incident:Property_Affected>
                            <incident:Property xsi:type="stixVocabs:LossPropertyVocab-1.0">Confidentiality</incident:Property>
                            <incident:Description_Of_Effect>Unknown</incident:Description_Of_Effect>
                        </incident:Property_Affected>
                    </incident:Nature_Of_Security_Effect>
                </incident:Affected_Asset>
            </incident:Affected_Assets>
            <incident:Attributed_Threat_Actors>
                <incident:Threat_Actor>
                    <stixCommon:Threat_Actor idref="example:threatactor-9eaf63ee-0261-4f62-95ac-20dbaaa04750" xsi:type='ta:ThreatActorType'/>
                </incident:Threat_Actor>
            </incident:Attributed_Threat_Actors>
            <incident:Security_Compromise xsi:type="stixVocabs:SecurityCompromiseVocab-1.0">Yes</incident:Security_Compromise>
            <incident:Discovery_Method xsi:type="stixVocabs:DiscoveryMethodVocab-1.0">Unknown</incident:Discovery_Method>
            <incident:Information_Source>
                <stixCommon:Identity>
                    <stixCommon:Name>vcdb</stixCommon:Name>
                </stixCommon:Identity>
                <stixCommon:Tools>
                    <cyboxCommon:Tool>
                        <cyboxCommon:Name>veris2stix</cyboxCommon:Name>
                        <cyboxCommon:Vendor>MITRE</cyboxCommon:Vendor>
                        <cyboxCommon:Version>0.1</cyboxCommon:Version>
                    </cyboxCommon:Tool>
                    <cyboxCommon:Tool>
                        <cyboxCommon:Name>VERIS schema</cyboxCommon:Name>
                        <cyboxCommon:Vendor>Verizon</cyboxCommon:Vendor>
                        <cyboxCommon:Version>1.3</cyboxCommon:Version>
                    </cyboxCommon:Tool>
                </stixCommon:Tools>
                <stixCommon:References>
                    <stixCommon:Reference>http://vcdb.org/pdf/va-security.pdf</stixCommon:Reference>
                </stixCommon:References>
            </incident:Information_Source>
        </stix:Incident>
    </stix:Incidents>
    <stix:Threat_Actors>
        <stix:Threat_Actor id="example:threatactor-9eaf63ee-0261-4f62-95ac-20dbaaa04750" timestamp="2014-12-22T16:35:19.739000+00:00" xsi:type='ta:ThreatActorType'>
            <ta:Type timestamp="2014-12-22T16:35:19.739000+00:00">
                <stixCommon:Value xsi:type="stixVocabs:ThreatActorTypeVocab-1.0">Insider Threat</stixCommon:Value>
                <stixCommon:Description>Unknown</stixCommon:Description>
            </ta:Type>
            <ta:Motivation timestamp="2014-12-22T16:35:19.739000+00:00">
                <stixCommon:Value>NA</stixCommon:Value>
            </ta:Motivation>
        </stix:Threat_Actor>
    </stix:Threat_Actors>
</stix:STIX_Package>


text:
package os.hw.nine.algorithm;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.stream.Collectors;

import os.hw.nine.SchedulerType;
import os.hw.nine.io.AlgorithmResult;

public class LOOKAlgorithm implements SchedulerAlgorithm {
	private final SchedulerType type = SchedulerType.LOOK;

	@Override
	public AlgorithmResult runWith(LinkedList<Integer> fifoOrder,
			int startPosition, int previousPosition, int numCylinders) {
		// TODO Auto-generated method stub
		boolean down = Integer.compare(previousPosition, startPosition) > 0;
		int currentPosition = startPosition;
		int runningDistance = 0;
		LinkedList<Integer> result = new LinkedList<>();

		while (!fifoOrder.isEmpty()) {
			LinkedList<Integer> temp;
			int value = -1;
			final int finalCurrentPos = currentPosition;
			if (down) {
				temp = new LinkedList<Integer>(fifoOrder.stream()
						.filter(i -> i < finalCurrentPos)
						.collect(Collectors.toList()));
				value = temp.stream().max(Comparator.naturalOrder()).get();
			} else {
				temp = new LinkedList<Integer>(fifoOrder.stream()
						.filter(i -> i > finalCurrentPos)
						.collect(Collectors.toList()));
				value = temp.stream().min(Comparator.naturalOrder()).get();
			}

			final Integer request = fifoOrder.remove(fifoOrder.indexOf(value));
			runningDistance += Math.abs(currentPosition - request);
			currentPosition = request;
			result.add(request);

			final int currentPosition2 = currentPosition;
			final boolean headingDownAndNothingLower = down
														&& !(fifoOrder.stream()
																.anyMatch(i -> i < currentPosition2));
			final boolean headingUpAndNothingHigher = !down
														&& !(fifoOrder.stream()
																.anyMatch(i -> i > currentPosition2));

			if (headingDownAndNothingLower || headingUpAndNothingHigher) {
				down = !down;
			}

		}
		return new AlgorithmResult(result, runningDistance, this.type);
	}

}


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

package com.google.security.zynamics.binnavi.debug.models.processmanager;

import com.google.common.base.Preconditions;
import com.google.security.zynamics.zylib.disassembly.IAddress;

/**
 * Describes a section of accessible memory in the target process. Each byte of the section is
 * assumed to be readable.
 */
public final class MemorySection {
  /**
   * Start offset of the memory section.
   */
  private final IAddress memorySectionStartAddress;

  /**
   * Ends offset of the memory section.
   */
  private final IAddress memorySectionEndAddress;

  /**
   * Creates a new memory section description object.
   *
   * @param start The start offset of the memory section.
   * @param end The last offset of the memory section.
   *
   * @throws IllegalArgumentException Thrown if either parameter is negative or end <= start.
   */
  public MemorySection(final IAddress start, final IAddress end) {
    memorySectionStartAddress =
        Preconditions.checkNotNull(start, "IE00753: Section start can not be null");
    memorySectionEndAddress =
        Preconditions.checkNotNull(end, "IE00754: Section end can not be null");
    Preconditions.checkArgument(end.toBigInteger().compareTo(start.toBigInteger()) != -1,
        "IE00755: Section end can not come before section start");
  }

  /**
   * Returns the last offset that belongs to the memory section.
   *
   * @return The last offset that belongs to the memory section.
   */
  public IAddress getEnd() {
    return memorySectionEndAddress;
  }

  /**
   * Returns the size of the memory section in bytes.
   *
   * @return The size of the memory section.
   */
  public int getSize() {
    return (int) (memorySectionEndAddress.toLong() - memorySectionStartAddress.toLong()) + 1;
  }

  /**
   * Returns the first offset that belongs to the memory section.
   *
   * @return The first offset that belongs to the memory section.
   */
  public IAddress getStart() {
    return memorySectionStartAddress;
  }

  /**
   * Turns the memory section description into a readable string.
   *
   * @return The memory section description as a readable string.
   */
  @Override
  public String toString() {
    // TODO: Not fit for 64bit
    return String.format("%s - %s", memorySectionStartAddress, memorySectionEndAddress);
  }
}


text:
<?xml version="1.0" encoding="UTF-8"?>
<DEFECTS>
  <DEFECT>
    <SFA>
      <FILEPATH>d:\src\sarif-sdk\src\sarif.functionaltests\convertertestdata\prefast\src\standardux\</FILEPATH>
      <FILENAME>standardc6316.cxx</FILENAME>
      <LINE>19</LINE>
      <COLUMN>12</COLUMN>
    </SFA>
    <DEFECTCODE>6316</DEFECTCODE>
    <DESCRIPTION>Incorrect operator:  tested expression is constant and non-zero.  Use bitwise-and to determine whether bits are set.</DESCRIPTION>
    <FUNCTION>fd</FUNCTION>
    <DECORATED>?fd@@YAXH@Z</DECORATED>
    <FUNCLINE>17</FUNCLINE>
    <PATH />
  </DEFECT>
</DEFECTS>


text:
package org.zstack.storage.primary.local;

import org.springframework.beans.factory.annotation.Autowired;
import org.zstack.core.cloudbus.CloudBus;
import org.zstack.core.db.DatabaseFacade;
import org.zstack.core.db.Q;
import org.zstack.core.db.SQLBatch;
import org.zstack.core.db.SimpleQuery;
import org.zstack.core.db.SimpleQuery.Op;
import org.zstack.core.errorcode.ErrorFacade;
import org.zstack.header.apimediator.ApiMessageInterceptionException;
import org.zstack.header.apimediator.ApiMessageInterceptor;
import org.zstack.header.apimediator.StopRoutingException;
import org.zstack.header.errorcode.SysErrors;
import org.zstack.header.host.HostInventory;
import org.zstack.header.host.HostVO;
import org.zstack.header.host.HostVO_;
import org.zstack.header.message.APIMessage;
import org.zstack.header.storage.primary.PrimaryStorageState;
import org.zstack.header.storage.primary.PrimaryStorageVO;
import org.zstack.header.storage.primary.PrimaryStorageVO_;
import org.zstack.header.vm.VmInstanceState;
import org.zstack.header.vm.VmInstanceVO;
import org.zstack.header.vm.VmInstanceVO_;
import org.zstack.header.volume.VolumeStatus;
import org.zstack.header.volume.VolumeType;
import org.zstack.header.volume.VolumeVO;
import org.zstack.header.volume.VolumeVO_;

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

import static org.zstack.core.Platform.*;

/**
 * Created by frank on 7/1/2015.
 */
public class LocalStorageApiInterceptor implements ApiMessageInterceptor {
    @Autowired
    private ErrorFacade errf;
    @Autowired
    private DatabaseFacade dbf;
    @Autowired
    private CloudBus bus;

    @Override
    public APIMessage intercept(APIMessage msg) throws ApiMessageInterceptionException {
        if (msg instanceof APIAddLocalPrimaryStorageMsg) {
            validate((APIAddLocalPrimaryStorageMsg) msg);
        } else if (msg instanceof APILocalStorageMigrateVolumeMsg) {
            validate((APILocalStorageMigrateVolumeMsg) msg);
        } else if (msg instanceof APILocalStorageGetVolumeMigratableHostsMsg) {
            validate((APILocalStorageGetVolumeMigratableHostsMsg) msg);
        }

        return msg;
    }

    private void validate(APILocalStorageGetVolumeMigratableHostsMsg msg) {
        APILocalStorageGetVolumeMigratableReply reply = new APILocalStorageGetVolumeMigratableReply();

        SimpleQuery<LocalStorageResourceRefVO> q = dbf.createQuery(LocalStorageResourceRefVO.class);
        q.add(LocalStorageResourceRefVO_.resourceType, Op.EQ, VolumeVO.class.getSimpleName());
        q.add(LocalStorageResourceRefVO_.resourceUuid, Op.EQ, msg.getVolumeUuid());
        LocalStorageResourceRefVO ref = q.find();
        if (ref == null) {
            reply.setInventories(new ArrayList<HostInventory>());
            bus.reply(msg, reply);
            throw new StopRoutingException();
        }

        msg.setPrimaryStorageUuid(ref.getPrimaryStorageUuid());
    }

    private void validate(APILocalStorageMigrateVolumeMsg msg) {

        new SQLBatch() {
            @Override
            protected void scripts() {

                //1.confirm that  volume is on local storage and  not on the dest Host.
                LocalStorageResourceRefVO ref = Q.New(LocalStorageResourceRefVO.class)
                        .eq(LocalStorageResourceRefVO_.resourceType,VolumeVO.class.getSimpleName())
                        .eq(LocalStorageResourceRefVO_.resourceUuid,msg.getVolumeUuid()).find();
                if (ref == null) {
                    throw new ApiMessageInterceptionException(argerr("the volume[uuid:%s] is not on any local primary storage", msg.getVolumeUuid()));
                }
                msg.setPrimaryStorageUuid(ref.getPrimaryStorageUuid());

                if (ref.getHostUuid().equals(msg.getDestHostUuid())) {
                    throw new ApiMessageInterceptionException(argerr("the volume[uuid:%s] is already on the host[uuid:%s]", msg.getVolumeUuid(), msg.getDestHostUuid()));
                }

                //2.confirm primary storage is available.
                PrimaryStorageVO vo = Q.New(PrimaryStorageVO.class).eq(PrimaryStorageVO_.uuid,ref.getPrimaryStorageUuid()).find();
                if (vo == null) {
                    throw new ApiMessageInterceptionException(argerr("the primary storage[uuid:%s] is not found", msg.getPrimaryStorageUuid()));
                }

                if (vo.getState() == PrimaryStorageState.Disabled || vo.getState() == PrimaryStorageState.Maintenance) {
                    throw new ApiMessageInterceptionException(argerr("the primary storage[uuid:%s] is disabled or maintenance cold migrate is not allowed", ref.getPrimaryStorageUuid()));
                }

                //3.confirm the dest host belong to the local storage where the volume locates
                List<LocalStorageHostRefVO> refs = Q.New(LocalStorageHostRefVO.class)
                        .eq(LocalStorageHostRefVO_.hostUuid,msg.getDestHostUuid())
                        .eq(LocalStorageHostRefVO_.primaryStorageUuid,ref.getPrimaryStorageUuid()).list();
                if (refs.size() == 0) {
                    throw new ApiMessageInterceptionException(argerr("the dest host[uuid:%s] doesn't belong to the local primary storage[uuid:%s] where the" +
                            " volume[uuid:%s] locates", msg.getDestHostUuid(), ref.getPrimaryStorageUuid(), msg.getVolumeUuid()));
                }

                //4.confirm primary storage is available.
                VolumeVO vol = Q.New(VolumeVO.class).eq(VolumeVO_.uuid,msg.getVolumeUuid()).find();
                if (VolumeStatus.Ready != vol.getStatus()) {
                    throw new ApiMessageInterceptionException(argerr("the volume[uuid:%s] is not in status of Ready, cannot migrate it", msg.getVolumeUuid()));
                }

                //5.confirm that the data volume has detach the vm and the root volume will migrate to appropriate cluster.
                if (vol.getType() == VolumeType.Data && vol.getVmInstanceUuid() != null) {
                    throw new ApiMessageInterceptionException(argerr("the data volume[uuid:%s, name: %s] is still attached on the VM[uuid:%s]. Please detach" +
                            " it before migration", vol.getUuid(), vol.getName(), vol.getVmInstanceUuid()));
                } else if (vol.getType() == VolumeType.Root) {

                    VmInstanceState vmstate = Q.New(VmInstanceVO.class)
                            .select(VmInstanceVO_.state)
                            .eq(VmInstanceVO_.uuid,vol.getVmInstanceUuid()).findValue();
                    if (VmInstanceState.Stopped != vmstate) {
                        throw new ApiMessageInterceptionException(operr("the volume[uuid:%s] is the root volume of the vm[uuid:%s]. Currently the vm is in" +
                                " state of %s, please stop it before migration", vol.getUuid(), vol.getVmInstanceUuid(), vmstate));
                    }

                    long count = Q.New(VolumeVO.class)
                            .eq(VolumeVO_.type,VolumeType.Data)
                            .eq(VolumeVO_.vmInstanceUuid,vol.getVmInstanceUuid()).count();
                    if (count != 0) {
                        throw new ApiMessageInterceptionException(operr("the volume[uuid:%s] is the root volume of the vm[uuid:%s]. Currently the vm still" +
                                " has %s data volumes attached, please detach them before migration", vol.getUuid(), vol.getVmInstanceUuid(), count));
                    }

                    String clusterUuid = Q.New(HostVO.class).select(HostVO_.clusterUuid)
                            .eq(HostVO_.uuid,msg.getDestHostUuid()).findValue();
                    String originClusterUuid = Q.New(VmInstanceVO.class)
                            .select(VmInstanceVO_.clusterUuid)
                            .eq(VmInstanceVO_.uuid, vol.getVmInstanceUuid()).findValue();
                    if(originClusterUuid == null){
                        throw new ApiMessageInterceptionException(
                                err(SysErrors.INTERNAL,"The clusterUuid of vm[uuid:%s] cannot be null when migrate the root volume[uuid:%s, name: %s]",vol.getVmInstanceUuid(),vol.getUuid(),vol.getName()));
                    }

                    if(!originClusterUuid.equals(clusterUuid)){
                        List<String> originL2NetworkList  = sql("select l2NetworkUuid from L3NetworkVO" +
                                " where uuid in(select l3NetworkUuid from VmNicVO where vmInstanceUuid = :vmUuid)")
                                .param("vmUuid",vol.getVmInstanceUuid()).list();
                        List<String> l2NetworkList = sql("select l2NetworkUuid from L2NetworkClusterRefVO" +
                                " where clusterUuid = :clusterUuid")
                                .param("clusterUuid",clusterUuid).list();
                        for(String l2:originL2NetworkList){
                            if(!l2NetworkList.contains(l2)){
                                throw new ApiMessageInterceptionException(
                                        operr("The two clusters[uuid:%s,uuid:%s] cannot access each other in l2 network  when migrate the vm[uuid;%s] to another cluster", originClusterUuid, clusterUuid, vol.getVmInstanceUuid()));
                            }
                        }
                    }
                }

            }
        }.execute();

    }

    private void validate(APIAddLocalPrimaryStorageMsg msg) {
        String url = msg.getUrl();
        if (!url.startsWith("/")) {
            throw new ApiMessageInterceptionException(argerr("the url[%s] is not an absolute path starting with '/'", msg.getUrl()));
        }
        if (url.startsWith("/dev") || url.startsWith("/proc") || url.startsWith("/sys")) {
            throw new ApiMessageInterceptionException(argerr(" the url contains an invalid folder[/dev or /proc or /sys]"));
        }
    }
}


text:
CREATE VIEW View_Tests AS
SELECT
Tests.TestID,
Programs.ProgramID,
Languages.Language,
RIGHT(Programs.Program,30) AS Program,
Phases.Phase,
CASE
    WHEN Programs.DeathTime IS NULL THEN NULL
    WHEN Tests.ExpectedType  = Programs.ResultType  AND Tests.ExpectedValue  = Programs.ResultValue  THEN TRUE
    WHEN Tests.ExpectedTypes = Programs.ResultTypes AND Tests.ExpectedValues = Programs.ResultValues THEN TRUE
    WHEN Tests.ExpectedLog IS NOT NULL AND EXISTS (
        SELECT 1
        FROM Log
        INNER JOIN Phases    ON Phases.PhaseID       = Log.PhaseID
        INNER JOIN Nodes     ON Nodes.NodeID         = Log.NodeID
        INNER JOIN NodeTypes ON NodeTypes.NodeTypeID = Nodes.NodeTypeID
        WHERE Log.ProgramID = Tests.ProgramID
        AND format('%s %s %s',
            Phases.Phase,
            Log.Severity::text,
            NodeTypes.NodeType
        ) = Tests.ExpectedLog
    )
    THEN TRUE
    WHEN Tests.ExpectedSTDOUT IS NOT NULL AND array_to_string(Tests.ExpectedSTDOUT,E'\n') = (
        SELECT string_agg(Log.Message, E'\n' ORDER BY Log.LogID)
        FROM Log
        WHERE Log.ProgramID = Tests.ProgramID
        AND   Log.Severity  = 'STDOUT'
        AND   Log.Message IS NOT NULL
    )
    THEN TRUE
    WHEN Tests.ExpectedError IS NOT NULL AND array_to_string(Sort_Array(Tests.ExpectedError),E'\n') = (
        SELECT string_agg(Log.Message, E'\n' ORDER BY Log.Message)
        FROM Log
        WHERE Log.ProgramID = Tests.ProgramID
        AND   Log.Severity  = 'ERROR'
        AND   Log.Message IS NOT NULL
    )
    THEN TRUE
    WHEN Tests.ExpectedSTDOUT IS NULL
    AND  Tests.ExpectedError  IS NULL
    AND NOT EXISTS (
        SELECT 1
        FROM Log
        WHERE Log.ProgramID = Tests.ProgramID
        AND   Log.Severity  IN ('STDOUT', 'ERROR')
        AND   Log.Message IS NOT NULL
    )
    THEN TRUE
    ELSE FALSE
END AS OK,
(
    SELECT Strip_ANSI(Log.Message)
    FROM Log
    WHERE Log.ProgramID = Programs.ProgramID
    ORDER BY Log.LogID DESC
    LIMIT 1
) AS LastLog,
Tests.ExpectedSTDOUT,
(
    SELECT string_agg(Log.Message, E'\n' ORDER BY Log.LogID)
    FROM Log
    WHERE Log.ProgramID = Tests.ProgramID
    AND   Log.Severity  = 'STDOUT'
    AND   Log.Message IS NOT NULL
) AS STDOUT,
(
    SELECT Log.ErrorType
    FROM Log
    WHERE Log.ProgramID = Programs.ProgramID
    AND Log.ErrorType IS NOT NULL
    ORDER BY Log.LogID DESC
    LIMIT 1
) AS LastErrorType,
(
    SELECT Log.ErrorInfo
    FROM Log
    WHERE Log.ProgramID = Programs.ProgramID
    AND Log.ErrorInfo IS NOT NULL
    ORDER BY Log.LogID DESC
    LIMIT 1
) AS LastErrorInfo,
Tests.ExpectedError,
(
    SELECT string_agg(Log.Message, E'\n' ORDER BY Log.LogID)
    FROM Log
    WHERE Log.ProgramID = Tests.ProgramID
    AND   Log.Severity  = 'ERROR'
    AND   Log.Message IS NOT NULL
) AS Error
FROM Tests
INNER JOIN Programs        ON Programs.ProgramID   = Tests.ProgramID
INNER JOIN Languages       ON Languages.LanguageID = Programs.LanguageID
INNER JOIN Phases          ON Phases.PhaseID       = Programs.PhaseID
LEFT  JOIN Nodes           ON Nodes.NodeID         = Programs.NodeID
ORDER BY Programs.ProgramID
;


text:
__author__ = 'William Lees'
__docformat__ = "restructuredtext en"

import os
import sys
import argparse
import csv
import re
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import matplotlib.mlab as mlab
import itertools

def main(argv):
    parser = argparse.ArgumentParser(description='Read an IgBlastPlus file and plot the contained CDR3 lengths.')
    parser.add_argument('infiles', help='input files, separated by commas (IgBLASTPlus format, nt or aa)')
    parser.add_argument('-b', '--barcolour', help='colour or list of colours for bars')
    parser.add_argument('-c', '--cols', help='Number of columns for plot')
    parser.add_argument('-d', '--dupheader', help='Prefix for duplicate count, eg "DUPCOUNT=" for Presto')
    parser.add_argument('-g', '--gradientfill', help='fill bars with a gradiented colour', action='store_true')
    parser.add_argument('-gh', '--grid_horizontal', help='horizontal grid lines', action='store_true')
    parser.add_argument('-gv', '--grid_vertical', help='vertical grid lines every n bars')
    parser.add_argument('-ga', '--gauss', help='plot best-fit Gaussian distribution', action='store_true')
    parser.add_argument('-s', '--save', help='Save output to file (as opposed to interactive display)')
    parser.add_argument('-sz', '--size', help='Figure size (x,y)')
    parser.add_argument('-t', '--titles', help='titles for each plot, separated by commas')
    parser.add_argument('-u', '--unique', help='only count unique sequences', action='store_true')
    parser.add_argument('-w', '--width', help='relative bar width (number between 0 and 1)')
    parser.add_argument('-xmax', '--xmax', help='Max x-value to use on all charts')
    parser.add_argument('-xmin', '--xmin', help='Min x-value to use on all charts')
    parser.add_argument('-y', '--ymax', help='Max y-value to use on all charts')
    args = parser.parse_args()
    infiles = args.infiles.split(',')
    titles = args.titles.split(',') if args.titles else [""]
    ncols = int(args.cols) if args.cols else 1
    xmin = int(args.xmin) if args.xmin else 1
    xmax = int(args.xmax) if args.xmax else None
    ymax = float(args.ymax) if args.ymax else None
    outfile = args.save if args.save else None
    dupheader = args.dupheader
    bar_width = float(args.width) if args.width else 1.0
    mapcolour = args.barcolour if args.barcolour else 'blue'
    mapcolour = mapcolour.split(',')
    grid_vertical = int(args.grid_vertical) if args.grid_vertical else False
    gauss = args.gauss

    nrows = len(infiles) / ncols
    if len(infiles) % ncols != 0:
        nrows += 1
    (sizex, sizey) = args.size.split(',') if args.size else (8*ncols,4*nrows)

    lengths = []
    for infile in infiles:
        lengths.append(determine_stats(dupheader, infile, args.unique))

    if not outfile or len(outfile) < 5 or outfile[-4:] != '.csv':
        plt.figure(figsize=(float(sizex),float(sizey)))
        plot_number = 1
        plotted = False
        for (length, title, colour) in zip(lengths, itertools.cycle(titles), itertools.cycle(mapcolour)):
            if length is not None:
                plot_file(length, xmin, xmax, ymax, title, nrows, ncols, plot_number, colour, bar_width, args.gradientfill, args.grid_horizontal, grid_vertical, gauss)
                plot_number += 1
                plotted = True
        if not plotted:
            quit()

        plt.tight_layout()
        if outfile:
            plt.savefig(outfile)
        else:
            plt.show()
    else:
        minlength = 9999
        maxlength = 0
        for length in lengths:
            if length is not None:
                maxlength = max(maxlength, max([i for i in range(len(length)) if length[i] > 0]))
                minlength = min(minlength, min([i for i in range(len(length)) if length[i] > 0]))

        with open(outfile, 'wb') as fo:
            writer = csv.writer(fo)
            writer.writerow(['Length'] + range(minlength, maxlength))
            ititle = iter(titles) if titles else iter(infiles)
            for length in lengths:
                if length is not None:
                    writer.writerow([next(ititle)] + list(length[minlength:maxlength]))

def plot_file(lengths, xmin, xmax, ymax, title, nrows, ncols, plot_number, mapcolour, bar_width, gradientfill, grid_horizontal, grid_vertical, gauss):
    if not xmax:
        for xmax in range(len(lengths)-1, 0, -1):
            if lengths[xmax] > 0:
                break

    ax = plt.subplot(nrows, ncols, plot_number)
    ax.tick_params(direction='out', top=False, right=False)

    if title != "":
        plt.xlabel(title)

    bar_pos = np.arange(xmax + 1)

    # if xmax is set, all the x axes will be the same, so only put labels on the bottom row
    # if we have a vertical grid, label just one point in each column

    if xmax is None or plot_number > (nrows-1)*ncols:
        if grid_vertical:
            labels = []
            if grid_vertical % 2 == 0:
                midpoint = grid_vertical/2
            else:
                midpoint = int(grid_vertical/2)
            for tick in bar_pos:
                if tick % grid_vertical == midpoint:
                    labels.append(str(tick))
                else:
                    labels.append(' ')
        else:
            labels = ['%d' % x for x in bar_pos]
    else:
        labels = []

    plt.xticks(bar_pos+0.5, labels)

    if ymax:
        plt.ylim(0, ymax)
    if xmax or xmin != 0:
        plt.xlim(xmin, xmax)

    if bar_width < 1.:
        bar_pos = bar_pos + (1 - bar_width) / 2.

    if grid_horizontal:
        plt.grid(which='major', axis='y', c='black', linestyle='-', alpha=0.6, zorder=1)

    if gradientfill:
        gbar(bar_pos, lengths, mapcolour, width=bar_width)
    else:
        plt.bar(bar_pos, lengths[:xmax+1], width=bar_width, color=mapcolour, zorder=10)

    if grid_vertical:
        pos = 0
        while pos < xmax:
            ymin, ymax = plt.ylim()
            plt.plot([bar_pos[pos] - (1 - bar_width)/2, bar_pos[pos] - (1 - bar_width)/2], [ymin, ymax], c='black', linestyle='-', alpha=0.6, zorder=1)
            pos += grid_vertical

    # Remove every other y label because we get far too many by default

    locs, labels = plt.yticks()
    newlocs = []
    newlabels = []

    for i in range(0, len(labels)):
        if i % 2 != 0:
            newlocs.append(locs[i])
            newlabels.append(str(int(locs[i])))

    plt.yticks(newlocs, newlabels)

    if gauss:
        values = []
        for i in range(len(lengths)-1):
            if lengths[i] > 0:
                foo = [i]*lengths[i]
                values += ([i] * lengths[i])
        values = np.array(values)
        mean = np.mean(values)
        variance = np.var(values)
        sigma = np.sqrt(variance)
        x = np.linspace(min(values), max(values), 100)
        plt.plot(x, mlab.normpdf(x, mean, sigma)*len(values), zorder=20)

    ax.set_aspect('auto')
    plt.tight_layout()

def gbar(x, y, mapcolour, width=1, bottom=0):
    X = [[.6, .6], [.7, .7]]
    c = mcolors.ColorConverter().to_rgb
    cm = make_colormap([c('white'), c(mapcolour)])
    for left, top in zip(x, y):
        if top != bottom:
            right = left + width
            plt.imshow(X, interpolation='bicubic', cmap=cm, extent=(left, right, bottom, top), alpha=1, zorder=10)
            plt.plot([left, left], [bottom, top], color='black', linestyle='-', zorder=20)
            plt.plot([right, right], [bottom, top], color='black', linestyle='-', zorder=20)
            plt.plot([right, left], [top, top], color='black', linestyle='-', zorder=20)

# From http://stackoverflow.com/questions/16834861/create-own-colormap-using-matplotlib-and-plot-color-scale
def make_colormap(seq):
    """Return a LinearSegmentedColormap
    seq: a sequence of floats and RGB-tuples. The floats should be increasing
    and in the interval (0,1).
    """
    seq = [(None,) * 3, 0.0] + list(seq) + [1.0, (None,) * 3]
    cdict = {'red': [], 'green': [], 'blue': []}
    for i, item in enumerate(seq):
        if isinstance(item, float):
            r1, g1, b1 = seq[i - 1]
            r2, g2, b2 = seq[i + 1]
            cdict['red'].append([item, r1, r2])
            cdict['green'].append([item, g1, g2])
            cdict['blue'].append([item, b1, b2])
    return mcolors.LinearSegmentedColormap('CustomMap', cdict)

def determine_stats(dupheader, infile, unique):
    seen = {}
    with open(infile, 'r') as fi:
        ln = fi.readline()
        sep = ("\t" if "\t" in ln else ",")
        fi.seek(0)
        reader = csv.DictReader(fi, delimiter=sep)
        lengths = np.zeros(600)
        first_row = True
        seen_data = False
        for row in reader:
            if first_row:
                for f in ('Functionality', 'CDR3-IMGT', 'Sequence ID'):
                    if f not in row:
                        print 'Error: required field %s not in %s.' % (f, infile)
                        quit()
                first_row = False
            if 'unproductive' not in row['Functionality'] and row['CDR3-IMGT'] and (
                not unique or row['CDR3-IMGT'] not in seen):
                lengths[len(row['CDR3-IMGT'])] += (get_size(row['Sequence ID'], dupheader) if dupheader else 1)
                seen[row['CDR3-IMGT']] = 1
                seen_data = True

    if not seen_data:
        print 'Warning: no functional CDR3 records found in file %s.' % infile
        return None

    return lengths

# Find duplicate size, or return 1

def get_size(s, dupheader):
    count = None
    if dupheader in s:
        spl = s.split(dupheader)
        for i in range(1, len(spl[1])):
            if spl[1][0:i].isdigit():
                count = int(spl[1][0:i])
            else:
                break

    return count if count else 1

if __name__=="__main__":
    main(sys.argv)


text:
using System.Diagnostics;

namespace Bickern
{
    public class ProxyService : IProxyService
    {
        public void AddV4Proxy(string ip, int port)
        {
            RunCommand($"netsh interface portproxy add v4tov4 listenport=80 listenaddress={ip} connectport={port} connectaddress=127.0.0.1");
        }

        public void RemoveV4Proxy(string ip)
        {
            RunCommand($"netsh interface portproxy delete v4tov4 listenport=80 listenaddress={ip}");
        }

        private static void RunCommand(string command)
        {
            var proc = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "cmd.exe",
                    Arguments = $"/C {command}",
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    CreateNoWindow = true
                }
            };
            proc.Start();
        }
    }
}