<commit_before># -*- coding: utf-8 -*-
import pymongo
from tools import decode_to_string
import time

class Tester(object):
  """
  Tester
  Args:
    cred (dict(str: str)): credentials
    tests (Test[]): tests to run
  """

  def __init__(self, cred, tests):
    self.cred = cred
    self.tests = tests
    self.conn = self.get_connection()
    self.info = self.get_info()

  def run(self, each, end):
    """
    Args:
      each (): function to call before each test runs (used to update and redraw the screen)
      end (function): function to call when all the tests have finished running
    """
    result = []
    for test in self.tests:
      each(test)
      res = test.run(self)
      result.append(res)
      time.sleep(0.5)
      if test.breaks == bool(res['result']):
        break

    self.conn.close()
    end(result)

  def get_connection(self):
    """
    Returns:
      pymongo.MongoClient: a client that uses as arguments the fqdn and the port of the credentials

    """
    fqdn, port = self.cred['nodelist'][0]
    return pymongo.MongoClient(fqdn, port, serverSelectionTimeoutMS=1)

  def get_info(self):
    """
    Returns:
      dict() or None: Get information about the MongoDB server we’re connected to.
    Note:
      https://docs.mongodb.com/v3.2/reference/command/buildInfo/#dbcmd.buildInfo
    """
    try:
      info = self.conn.server_info()
      return info
    except pymongo.errors.ConnectionFailure:
      return None

  def get_db(self):
    """
    Authenticates to database
    Returns:
      pymongo.database.Database or False: authenticates and returns the database(singleton) or false if authentication fails
    """
    if hasattr(self, 'db'):
      return self.db
    try:
      db = self.conn[self.cred['database']]
      db.authenticate(self.cred['username'], self.cred['password'])
      self.db = db
      return self.db
    except (pymongo.errors.OperationFailure, ValueError, TypeError):
      return False

class Test(object):

  def __init__(self, fn, severity, title, caption, message, breaks=None):
    """
    Args:
      fn ():
      severity (str):
      title (str):
      caption (str):
      message (str[] or function): messages to display when the test is completed
      breaks (bool): True if the test suite should stop False otherwise

    Notes:
      message should contain an array with the messages for the different results that the test can return
      if message is a function it should return a str[]
    """
    self.fn, self.severity, self.title, self.caption, self.message, self.breaks = fn, severity, title, caption, message, breaks

  def run(self, tester):
    """
    Args:
      cred (dict(str:str)): parsed MongoDB URI
    Returns:
      tuple(int, str): test result value and message
    """
    self.tester = tester
    result = self.fn(self)
    message = self.message
    value = result[0] if type(result) is list else result
    # if the message is a function then the test result must be of the type [int or bool, str]
    # where result[0] must be a boolean or an int  with the value 0 for false, 1 true or 2 for custom
    # result[1] must be a string with the data to display in the message
    if callable(message):
      message = message(result[1])
    message = message[value]

    return {'severity': self.severity, 'title': self.title, 'caption': self.caption, 'message': message, 'result': value}

import socket

def try_address(test):
  fqdn, _port = test.tester.cred['nodelist'][0]
  try:
    socket.gethostbyname_ex(fqdn)
  except socket.gaierror:
    return False
  else:
    return True

def try_socket(test, forced_port=None):
  fqdn, port = test.tester.cred['nodelist'][0]
  try:
    socket.create_connection((fqdn, forced_port or port), 5)
  except:
    return True
  else:
    return False

def try_authorization(test):
  try:
    test.tester.conn.database_names()
  except pymongo.errors.OperationFailure:
    return True
  else:
    return False

def try_credentials(test):
  """
  verify if the credentials are valid
  """
  return bool(test.tester.get_db())

def try_javascript(test):
  """
  Check if javascript is enabled in MongoDB
  """
  try:
    db = test.tester.get_db()
    db.test.find_one({"$where": "function() { return true;}"})
  except pymongo.errors.OperationFailure:
    return True
  else:
    return False

def try_roles(test):
  """
  Verify that the user roles are not administrative
  Returns:
    [bool or 2 , str ]: True Valid role, False invalid role, 2 custome role ,
  """

  def valid_role(role):
    """
    Args:
      role (str): name of a role
    Returns:
      Bool: True if the role is not administrative
    """
    return not role in ['userAdminAnyDatabase', 'dbAdminAnyDatabase' 'dbAdmin', 'dbOwner', 'userAdmin', 'clusterAdmin']

  def result_default_value():
    """

    Returns:
      dict(set()): returns an empty dictionary that contains 3 empty sets where valid, invalid and custom roles are stored
    """
    return {'invalid': set([]), 'valid': set([]), 'custom': set([])}

  def combine_result(a, b):
    """
    Args:
      a (dict(set())): result_default_value
      b (dict(set())): result_default_value
    Returns:
      dict(set()): the union of 2 default values
    """
    return {'invalid': a['invalid'].union(b['invalid']), 'valid': a['valid'].union(b['valid']), 'custom': a['custom'].union(b['custom'])}

  def reduce_roles(roles):
    """
    Validate and combine a list of roles
    Args:
     roles (list(dict())): roles
    Returns:
      result_default_value: with the roles sorted by valid, invalid, custom
    """
    return reduce(lambda x, y: combine_result(x, y), map(lambda r: validate_role(r), roles))

  def basic_validation(role):
    """
    Basic validation in case validate role fails due to lack of permissions
    Returns:
      result_default_value
    """
    validated = result_default_value()
    for role in roles['roles']:
      if valid_role(role['role']):
        validated['valid'].add(role['role'])
      else:
        validated['invalid'].add(role['role'])
    return validated

  def validate_role(role):
    """
    Recursively process the different roles that a role implements or inherits
    Args:
      role (list or dict): value returned by db.command 'usersInfo' or 'rolesInfo'
    """
    if type(role) is list and bool(role):
      return reduce_roles(role)
    elif type(role) is dict:
      if 'role' in role and 'isBuiltin' in role:
        result = result_default_value()
        is_valid_role = valid_role(role['role'])
        if is_valid_role and role['isBuiltin']:
          result['valid'].add(role['role'])
        elif is_valid_role:
          result['custom'].add(role['role'])
        else:
          result['invalid'].add(role['role'])
        inherited = validate_role(
            role['inheritedRoles']) if 'inheritedRoles' in role else result_default_value()
        other_roles = validate_role(
            role['roles']) if 'roles' in role else result_default_value()
        return combine_result(result, combine_result(inherited, other_roles))
      if 'role' in role:
        return validate_role(db.command('rolesInfo', role)['roles'])
      if 'roles' in role and bool(role['roles']):
        return validate_role(role['roles'])
      else:
        raise Exception('Non exhaustive type case')
    elif type(role) is list:
      # empty list
      return result_default_value()
    else:
      raise Exception('Non exhaustive type case')

  # get the database
  db = test.tester.get_db()
  roles = db.command('usersInfo', test.tester.cred['username'])['users'][0]
  validated = {}
  message = ""
  try:
    validated = validate_role(roles)
  except pymongo.errors.OperationFailure:
    # if the users doesn't have permission to run the command 'rolesInfo'
    validated = basic_validation(roles)
    if bool(validated['valid']):
      message = 'You user permissions ' + decode_to_string(validated['valid']) + ' didn\'t allow us to do an exhaustive check'
      return [2 , message ]
  if bool(validated['invalid']):
    message = decode_to_string(validated['invalid'])
  elif decode_to_string(validated['custom']):
    message = 'Your user’s roles set ' + decode_to_string(validated['valid']) + ' seem to be ok, but we couldn\'t do an exhaustive check.'
  else:
    message = decode_to_string(validated['valid'])
  return [False if bool(validated['invalid']) else [True, 2][bool(validated['custom'])] , message]

def try_dedicated_user(test):
  """
  Verify that the role only applies to one database
  """
  db = test.tester.get_db()
  roles = db.command('usersInfo', test.tester.cred['username'])['users'][0]
  user_role_dbs = set()
  for role in roles['roles']:
    user_role_dbs.add(role['db'])

  return [bool(len(user_role_dbs)), decode_to_string(user_role_dbs)]

basic_tests = [
    Test(
        try_address,
        0,
        'Domain exists',
        'The FQDN must exist in order to perform the test.',
        ['The provided domain name does not exist.',
         'The provided domain name does exist.', ],
        breaks=False
    ),
    Test(
        lambda test:  test.tester.cred['nodelist'][0][1] != 27017,
        2,
        'MongoDB listens on a port different to default one',
        'Using the default MongoDB port makes it too easy for potential attackers to locate and target your server.',
        ['Your server is listening on default port 27017. Please read this guide on how to change the listening port.',
         'Your server is listening in a non-obvious port. Well done.', ],
        breaks=False
    ),

    Test(
        try_socket,
        1,
        'Server only allows connecting from intended hosts / networks',
        'Best practice is to only listen to incoming connections whose originating IP belongs to the applications or systems that are intended to use the database. This protects your server from denial-of-service attacks and some other vulnerabilities that may be present on other services running on the same device. ',
        ['Your server allows connections from unauthorized hosts. Read this guide on how to block unauthorized hosts.',
         'Your server does not allow connection from unauthorized hosts. Well done.', ],
    ),
    Test(
        lambda test: try_socket(test, 28017),
        0,
        'MongoDB HTTP status interface is not accessible on port 28017',
        'HTTP status interface should be disabled in production environments to prevent potential data exposure and vulnerability to attackers.',
        ['HTTP interface is enabled. Please read this guide on how to disable MongoDB HTTP status interface.',
         'HTTP interface is disabled. Well done.', ],
        # breaks=True # uncomment after debug
    ),
    Test(
      lambda test: bool(hasattr(test.tester.info, 'version')),
      0, #TODO update number
      'MongoDB is not exposing its version number',
      'Publicly exposing the version number makes it too easy for potential attackers to immediately exploit known vulnerabilities.',
      ['MongoDB version number is exposed. This could be solved using a reverse proxy.',
      'MongoDB is hiding its version number. Well done.'],
    ),
    Test(
        lambda test: [test.tester.info['version'] > "2.4", str(test.tester.info['version'])],
        0,  # TODO update number
        'MongoDB version is newer than 2.4',
        'MongoDB versions prior to 2.4 allow usage of the “db” object inside “$where” clauses in your queries. This is a huge security flaw that allows attackers to inject and run arbitrary code in your database.',
        lambda message: ['You are running MongoDB version ' + message + '. Well done.',
                      'You are running MongoDB version ' + message + '.', ],
    ),
    Test(
        lambda test: test.tester.info['openssl']['running'] != 'disabled',
        0,  # TODO update number
        'Encryption is enabled',
        'Enable TLS/SSL to encrypt communications between your Mongo client and Mongo server to avoid eavesdropping, tampering and “man in the middle” attacks.',
        ['Encryption is disabled.',
         'TLS/SSL is enabled. Well done.', ],

    ),
    Test(
        try_authorization,
        0,  # TODO update number
        'Authentication is enabled',
        'Enable access control and specify the authentication mechanism, Authentication requires that all clients and servers provide valid credentials before they can connect to the system. In clustered deployments, enable authentication for each MongoDB server.',
        ['Authentication is disabled.', 'Authentication is enabled. Well done.', ],
    ),
]

advanced_tests = [
  # these tests require credentials
  Test(
      lambda test: bool(test.tester.get_db()),
      0,  # TODO update number
      'Valid credentials',
      'To continue testing the provided credentials must be valid.',
      ['Invalid credentials',
       'Credentials are valid', ],
      breaks=False
  ),

  Test(
      try_javascript,
      0,  # TODO update number
      'Javascript is not allowed in queries',
      'Running Javascript inside queries makes MongoDB powerful but also vulnerable to injection and denial-of-service attacks. Javascript should be always disabled unless it is strictly needed by your application.',
      ['Usage of Javascript is allowed inside queries.',
       'Usage of Javascript is not allowed inside queries. Well done.', ],
  ),

  Test(
      try_roles,
      0,  # TODO update number
      'Role granted to the provided user only permits CRUD operations',
      'Your user should only be allowed to perform CRUD (create, replace, update and delete) operations. Granting database administration roles such as “dbAdmin”, “dbOwner” or “userAdmin” is a huge risk for data integrity.',
      lambda message: ['Your user’s roles set has a too high permissions profile ' + message + '. Please read this guide to learn how to create a readWrite user in MongoDB.',
                    'Your user’s roles set ' +  message +  ' ???. Well done.',
                    message],
  ),

  Test(
      try_dedicated_user,
      0,  # TODO update number
      'Run MongoDB with a dedicated user',
      'Run MongoDB processes with a dedicated operating system user account. Ensure that the account has permissions to access data but no unnecessary permissions.',
      lambda message: ['Your user account has permissions for ' + message + ' databases.',
                       'No unnecessary permissions are given, your user account only has permissions over ' + message + ' database. Well done.',],
  ),
]
<commit_msg>Fix bug in port validation test<commit_after># -*- coding: utf-8 -*-
import pymongo
from tools import decode_to_string
import time

class Tester(object):
  """
  Tester
  Args:
    cred (dict(str: str)): credentials
    tests (Test[]): tests to run
  """

  def __init__(self, cred, tests):
    self.cred = cred
    self.tests = tests
    self.conn = self.get_connection()
    self.info = self.get_info()

  def run(self, each, end):
    """
    Args:
      each (): function to call before each test runs (used to update and redraw the screen)
      end (function): function to call when all the tests have finished running
    """
    result = []
    for test in self.tests:
      each(test)
      res = test.run(self)
      result.append(res)
      time.sleep(0.5)
      if test.breaks == bool(res['result']):
        break

    self.conn.close()
    end(result)

  def get_connection(self):
    """
    Returns:
      pymongo.MongoClient: a client that uses as arguments the fqdn and the port of the credentials

    """
    fqdn, port = self.cred['nodelist'][0]
    return pymongo.MongoClient(fqdn, port, serverSelectionTimeoutMS=1)

  def get_info(self):
    """
    Returns:
      dict() or None: Get information about the MongoDB server we’re connected to.
    Note:
      https://docs.mongodb.com/v3.2/reference/command/buildInfo/#dbcmd.buildInfo
    """
    try:
      info = self.conn.server_info()
      return info
    except pymongo.errors.ConnectionFailure:
      return None

  def get_db(self):
    """
    Authenticates to database
    Returns:
      pymongo.database.Database or False: authenticates and returns the database(singleton) or false if authentication fails
    """
    if hasattr(self, 'db'):
      return self.db
    try:
      db = self.conn[self.cred['database']]
      db.authenticate(self.cred['username'], self.cred['password'])
      self.db = db
      return self.db
    except (pymongo.errors.OperationFailure, ValueError, TypeError):
      return False

class Test(object):

  def __init__(self, fn, severity, title, caption, message, breaks=None):
    """
    Args:
      fn ():
      severity (str):
      title (str):
      caption (str):
      message (str[] or function): messages to display when the test is completed
      breaks (bool): True if the test suite should stop False otherwise

    Notes:
      message should contain an array with the messages for the different results that the test can return
      if message is a function it should return a str[]
    """
    self.fn, self.severity, self.title, self.caption, self.message, self.breaks = fn, severity, title, caption, message, breaks

  def run(self, tester):
    """
    Args:
      cred (dict(str:str)): parsed MongoDB URI
    Returns:
      tuple(int, str): test result value and message
    """
    self.tester = tester
    result = self.fn(self)
    message = self.message
    value = result[0] if type(result) is list else result
    # if the message is a function then the test result must be of the type [int or bool, str]
    # where result[0] must be a boolean or an int  with the value 0 for false, 1 true or 2 for custom
    # result[1] must be a string with the data to display in the message
    if callable(message):
      message = message(result[1])
    message = message[value]

    return {'severity': self.severity, 'title': self.title, 'caption': self.caption, 'message': message, 'result': value}

import socket

def try_address(test):
  fqdn, _port = test.tester.cred['nodelist'][0]
  try:
    socket.gethostbyname_ex(fqdn)
  except socket.gaierror:
    return False
  else:
    return True

def try_socket(test, forced_port=None):
  fqdn, port = test.tester.cred['nodelist'][0]
  try:
    socket.create_connection((fqdn, forced_port or port), 5)
  except:
    return True
  else:
    return False

def try_authorization(test):
  try:
    test.tester.conn.database_names()
  except pymongo.errors.OperationFailure:
    return True
  else:
    return False

def try_credentials(test):
  """
  verify if the credentials are valid
  """
  return bool(test.tester.get_db())

def try_javascript(test):
  """
  Check if javascript is enabled in MongoDB
  """
  try:
    db = test.tester.get_db()
    db.test.find_one({"$where": "function() { return true;}"})
  except pymongo.errors.OperationFailure:
    return True
  else:
    return False

def try_roles(test):
  """
  Verify that the user roles are not administrative
  Returns:
    [bool or 2 , str ]: True Valid role, False invalid role, 2 custome role ,
  """

  def valid_role(role):
    """
    Args:
      role (str): name of a role
    Returns:
      Bool: True if the role is not administrative
    """
    return not role in ['userAdminAnyDatabase', 'dbAdminAnyDatabase' 'dbAdmin', 'dbOwner', 'userAdmin', 'clusterAdmin']

  def result_default_value():
    """

    Returns:
      dict(set()): returns an empty dictionary that contains 3 empty sets where valid, invalid and custom roles are stored
    """
    return {'invalid': set([]), 'valid': set([]), 'custom': set([])}

  def combine_result(a, b):
    """
    Args:
      a (dict(set())): result_default_value
      b (dict(set())): result_default_value
    Returns:
      dict(set()): the union of 2 default values
    """
    return {'invalid': a['invalid'].union(b['invalid']), 'valid': a['valid'].union(b['valid']), 'custom': a['custom'].union(b['custom'])}

  def reduce_roles(roles):
    """
    Validate and combine a list of roles
    Args:
     roles (list(dict())): roles
    Returns:
      result_default_value: with the roles sorted by valid, invalid, custom
    """
    return reduce(lambda x, y: combine_result(x, y), map(lambda r: validate_role(r), roles))

  def basic_validation(role):
    """
    Basic validation in case validate role fails due to lack of permissions
    Returns:
      result_default_value
    """
    validated = result_default_value()
    for role in roles['roles']:
      if valid_role(role['role']):
        validated['valid'].add(role['role'])
      else:
        validated['invalid'].add(role['role'])
    return validated

  def validate_role(role):
    """
    Recursively process the different roles that a role implements or inherits
    Args:
      role (list or dict): value returned by db.command 'usersInfo' or 'rolesInfo'
    """
    if type(role) is list and bool(role):
      return reduce_roles(role)
    elif type(role) is dict:
      if 'role' in role and 'isBuiltin' in role:
        result = result_default_value()
        is_valid_role = valid_role(role['role'])
        if is_valid_role and role['isBuiltin']:
          result['valid'].add(role['role'])
        elif is_valid_role:
          result['custom'].add(role['role'])
        else:
          result['invalid'].add(role['role'])
        inherited = validate_role(
            role['inheritedRoles']) if 'inheritedRoles' in role else result_default_value()
        other_roles = validate_role(
            role['roles']) if 'roles' in role else result_default_value()
        return combine_result(result, combine_result(inherited, other_roles))
      if 'role' in role:
        return validate_role(db.command('rolesInfo', role)['roles'])
      if 'roles' in role and bool(role['roles']):
        return validate_role(role['roles'])
      else:
        raise Exception('Non exhaustive type case')
    elif type(role) is list:
      # empty list
      return result_default_value()
    else:
      raise Exception('Non exhaustive type case')

  # get the database
  db = test.tester.get_db()
  roles = db.command('usersInfo', test.tester.cred['username'])['users'][0]
  validated = {}
  message = ""
  try:
    validated = validate_role(roles)
  except pymongo.errors.OperationFailure:
    # if the users doesn't have permission to run the command 'rolesInfo'
    validated = basic_validation(roles)
    if bool(validated['valid']):
      message = 'You user permissions ' + decode_to_string(validated['valid']) + ' didn\'t allow us to do an exhaustive check'
      return [2 , message ]
  if bool(validated['invalid']):
    message = decode_to_string(validated['invalid'])
  elif decode_to_string(validated['custom']):
    message = 'Your user’s roles set ' + decode_to_string(validated['valid']) + ' seem to be ok, but we couldn\'t do an exhaustive check.'
  else:
    message = decode_to_string(validated['valid'])
  return [False if bool(validated['invalid']) else [True, 2][bool(validated['custom'])] , message]

def try_dedicated_user(test):
  """
  Verify that the role only applies to one database
  """
  db = test.tester.get_db()
  roles = db.command('usersInfo', test.tester.cred['username'])['users'][0]
  user_role_dbs = set()
  for role in roles['roles']:
    user_role_dbs.add(role['db'])

  return [bool(len(user_role_dbs)), decode_to_string(user_role_dbs)]

basic_tests = [
    Test(
        try_address,
        0,
        'Domain exists',
        'The FQDN must exist in order to perform the test.',
        ['The provided domain name does not exist.',
         'The provided domain name does exist.', ],
        breaks=False
    ),
    Test(
        lambda test:  not(test.tester.cred['nodelist'][0][1] == 27017 and bool(test.tester.info)),
        2,
        'MongoDB listens on a port different to default one',
        'Using the default MongoDB port makes it too easy for potential attackers to locate and target your server.',
        ['Your server is listening on default port 27017. Please read this guide on how to change the listening port.',
         'Your server is listening in a non-obvious port. Well done.', ],
    ),

    Test(
        try_socket,
        1,
        'Server only allows connecting from intended hosts / networks',
        'Best practice is to only listen to incoming connections whose originating IP belongs to the applications or systems that are intended to use the database. This protects your server from denial-of-service attacks and some other vulnerabilities that may be present on other services running on the same device. ',
        ['Your server allows connections from unauthorized hosts. Read this guide on how to block unauthorized hosts.',
         'Your server does not allow connection from unauthorized hosts. Well done.', ],
        breaks=True
    ),
    Test(
        lambda test: try_socket(test, 28017),
        0,
        'MongoDB HTTP status interface is not accessible on port 28017',
        'HTTP status interface should be disabled in production environments to prevent potential data exposure and vulnerability to attackers.',
        ['HTTP interface is enabled. Please read this guide on how to disable MongoDB HTTP status interface.',
         'HTTP interface is disabled. Well done.', ],
    ),
    Test(
      lambda test: bool(hasattr(test.tester.info, 'version')),
      0, #TODO update number
      'MongoDB is not exposing its version number',
      'Publicly exposing the version number makes it too easy for potential attackers to immediately exploit known vulnerabilities.',
      ['MongoDB version number is exposed. This could be solved using a reverse proxy.',
      'MongoDB is hiding its version number. Well done.'],
    ),
    Test(
        lambda test: [test.tester.info['version'] > "2.4", str(test.tester.info['version'])],
        0,  # TODO update number
        'MongoDB version is newer than 2.4',
        'MongoDB versions prior to 2.4 allow usage of the “db” object inside “$where” clauses in your queries. This is a huge security flaw that allows attackers to inject and run arbitrary code in your database.',
        lambda message: ['You are running MongoDB version ' + message + '. Well done.',
                      'You are running MongoDB version ' + message + '.', ],
    ),
    Test(
        lambda test: test.tester.info['openssl']['running'] != 'disabled',
        0,  # TODO update number
        'Encryption is enabled',
        'Enable TLS/SSL to encrypt communications between your Mongo client and Mongo server to avoid eavesdropping, tampering and “man in the middle” attacks.',
        ['Encryption is disabled.',
         'TLS/SSL is enabled. Well done.', ],

    ),
    Test(
        try_authorization,
        0,  # TODO update number
        'Authentication is enabled',
        'Enable access control and specify the authentication mechanism, Authentication requires that all clients and servers provide valid credentials before they can connect to the system. In clustered deployments, enable authentication for each MongoDB server.',
        ['Authentication is disabled.', 'Authentication is enabled. Well done.', ],
    ),
]

advanced_tests = [
  # these tests require credentials
  Test(
      lambda test: bool(test.tester.get_db()),
      0,  # TODO update number
      'Valid credentials',
      'To continue testing the provided credentials must be valid.',
      ['Invalid credentials',
       'Credentials are valid', ],
      breaks=False
  ),

  Test(
      try_javascript,
      0,  # TODO update number
      'Javascript is not allowed in queries',
      'Running Javascript inside queries makes MongoDB powerful but also vulnerable to injection and denial-of-service attacks. Javascript should be always disabled unless it is strictly needed by your application.',
      ['Usage of Javascript is allowed inside queries.',
       'Usage of Javascript is not allowed inside queries. Well done.', ],
  ),

  Test(
      try_roles,
      0,  # TODO update number
      'Role granted to the provided user only permits CRUD operations',
      'Your user should only be allowed to perform CRUD (create, replace, update and delete) operations. Granting database administration roles such as “dbAdmin”, “dbOwner” or “userAdmin” is a huge risk for data integrity.',
      lambda message: ['Your user’s roles set has a too high permissions profile ' + message + '. Please read this guide to learn how to create a readWrite user in MongoDB.',
                    'Your user’s roles set ' +  message +  ' ???. Well done.',
                    message],
  ),

  Test(
      try_dedicated_user,
      0,  # TODO update number
      'Run MongoDB with a dedicated user',
      'Run MongoDB processes with a dedicated operating system user account. Ensure that the account has permissions to access data but no unnecessary permissions.',
      lambda message: ['Your user account has permissions for ' + message + ' databases.',
                       'No unnecessary permissions are given, your user account only has permissions over ' + message + ' database. Well done.',],
  ),
]
<|endoftext|>


<commit_before>from werkzeug import serving
from werkzeug import wrappers

from . import responses
from . import routes

class Steinie(routes.Router):
    def __call__(self, environ, start_response):
        return self.wsgi_app(environ, start_response)

    def wsgi_app(self, environ, start_response):
        request = wrappers.Request(environ)
        fn = self.find_for(request)
        return wrappers.Response(fn())(environ, start_response)

    def run(self):
        host = "localhost"
        port = 5151
        serving.run_simple(host, port, self, use_debugger=True)

def create_app(callback):
    def app(environ, start_response):
        request = wrappers.Request(environ)
        try:
            callback(request)

            # `callback` should have bailed by now with a response, so
            # we haven't found anything
            response = responses.Http404()
            return response.do(environ, start_response)

        except responses.HttpResponse as response:
            return response.do(environ, start_response)
    return app
<commit_msg>Remove this old code<commit_after>from werkzeug import serving
from werkzeug import wrappers

from . import routes

class Steinie(routes.Router):
    def __call__(self, environ, start_response):
        return self.wsgi_app(environ, start_response)

    def wsgi_app(self, environ, start_response):
        request = wrappers.Request(environ)
        fn = self.find_for(request)
        return wrappers.Response(fn())(environ, start_response)

    def run(self):
        host = "localhost"
        port = 5151
        serving.run_simple(host, port, self, use_debugger=True)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#
import os
import globalvar
import systemstorages
import config
import fields
import emc_cmds

def main_menu():
    global disk_volume, lun_size

    os.system('clear')
    print('[ Storage Adm ]\n[ Version {0} - © 2015 ]\n\n'.format(
        globalvar.version))

    stgadm = raw_input('STG Adm options\n\n'
                       '1. Add new volumes to existent host.\n'
                       '\nPlease choose an option: ')

    # Add new volumes to existent host menu

    if stgadm == '1':

        print('\n\nRequest informations\n')
        change = fields.Fields('change', 'Ticket/Change/Work Order: ')
        change.chkfieldstr()
        change = change.strvarout()

        hostname_client = fields.Fields('hostname', 'Hostname Client: ')
        hostname_client.chkfieldstr()
        hostname_client = hostname_client.strvarout()

        storage_name = raw_input('Storage Name: ')

        wwn_client = fields.Fields('wwn_client', 'WWN Server Client: ')
        wwn_client.chkfieldstr()
        wwn_client = wwn_client.strvarout()

        disk_volume = int()
        lun_size = int()

        while True:
            try:
                disk_volume = int(raw_input("Total Disk Required (GB): "))
                break
            except (TypeError, ValueError):
                print(
                    '\tERROR: Total Disk need to be an int value in GB. '
                    '\nDo not use GB. Example: 1000 for 1000GB (1TB)')

        while True:
            try:
                lun_size = int(raw_input("Default LUN size (GB): "))
                break
            except (TypeError, ValueError):
                print(
                    '\tERROR: LUN Size need to be an int value.'
                    '\nDo not use GB. Example 100 for 100GB size of LUNs')

        if ':' in wwn_client:
            wwn_client = wwn_client.replace(':', '')

        # select storage
        get_stg = systemstorages.SystemStorages()
        get_stg.selectstorage()

        stg_type = get_stg.gettype()

        if stg_type == 'EMC_VMAX':
            stg_sid = get_stg.getsid()
            stg_name = get_stg.getstorage()
            menu_emc_vmax(stg_name, stg_type, stg_sid, wwn_client,
                          hostname_client, storage_name)

        elif stg_type == 'EMC_VNX':
            pass

        else:
            print('ERRO: Storage type {0} invalid. Check config file'.format(
                stg_type))

def menu_emc_vmax(stg_name, stg_type, stg_sid, wwn_client, change=None,
                  hostname_client=None, storage_name=None):
    # get storage informations

    global pool_option

    chk_server = emc_cmds.VMAX(config.symcli_path, stg_sid,
                               wwn_client)

    print('\nCollecting some storage informations. Please wait...')

    ign = chk_server.get_ign(wwn_client)

    if ign[0] != 0:
        print('ERROR: {0}'.format(ign[1].replace('\n', '')))
        exit(1)
    else:
        ign = ign[1]

    mvn = chk_server.get_mvn(ign)

    if mvn[0] != 0:
        print('ERROR: {0}'.format(mvn[1]))
        exit(1)
    else:
        mvn = mvn[1]

    sgn = chk_server.get_sgn(mvn)

    if sgn[0] != 0:
        print('ERROR: {0}'.format(sgn[1]))
        exit(1)
    else:
        sgn = sgn[1]

    print('\nGetting information about pools from storage. Please wait...')

    # get storage pools
    lspool = chk_server.lspools()

    # check if command worked well.
    if lspool[0] != 0:
        print('Error: {0}'.format(lspool[1]))
        exit(1)

    lspool = lspool[1]
    print lspool
    lspool = lspool.split('\n')

    print('Select the Storage Pool from {0} storage (SID:{1})\n'
          .format(stg_name, stg_sid))

    # creating array to select storage pool
    pool_list = []

    for l_lspool in lspool[8:]:
        if l_lspool == '':
            break
        else:
            pool_list.append(l_lspool.split()[0])

    count = 0
    for l_pool in pool_list:
        print ('{0}: {1}'.format(count, l_pool))
        count += 1

    while True:
        try:
            pool_option = int(raw_input("Select the pool: "))
            break
        except IndexError:
            print(
                '\tERROR: Select an existing option between'
                '0 and {0}.'.format(count))

    print('\nConfig validation\n')
    print('\nClient Information')
    print(50 * '-')
    print('Register      : {0}'.format(change))
    print('Client Server : {0}'.format(hostname_client))
    print('Storage Name  : {0}'.format(storage_name))
    print('WWN Client    : {0}'.format(wwn_client))
    print('\nStorage Information')
    print(50 * '-')
    print('Storage Name          : {0}'.format(stg_name))
    print('Storage Type          : {0}'.format(stg_type))
    print('Storage SID           : {0}'.format(stg_sid))
    print('Initiator Group Name  : {0}'.format(ign))
    print('Making View Names     : {0}'.format(mvn))
    print('Storage Group Name    : {0}'.format(sgn))
    print('Storage Pool          : {0}'.format(pool_list[pool_option]))
    print('\nInformations about the request:')
    print(50 * '-')
    print('Disk Volume   : {0}'.format(disk_volume))
    print('LUN Size      : {0}'.format(lun_size))
    print('')

    def choose_device_type():
        print('Select the type of device you want create:\n')
        print('0. Meta device')
        print('1. Regular device')
        device_type = raw_input('\nChoose an option: ')

        if device_type == '0':

            while True:
                try:
                    size_check = 1

                    while size_check != 0:
                        member_size = int(raw_input(
                            'What is the size of member? (GB): '))
                        size_check = lun_size % member_size
                        if size_check != 0:
                            print('ERRO: The member size ({0} GB) needs to '
                                  'be an integer divisor by the '
                                  'LUN Size ({1}GB).'
                                  .format(member_size, lun_size))

                    break
                except (TypeError, ValueError):
                    print(
                        '\tERROR: Total Disk need to be an int value in GB.'
                        '\nDo not use GB. Example: 1000 for 1000GB (1TB)')

        elif device_type == '1':
            pass

        else:
            print('ERRO: Wrong option.')
            choose_device_type()

    choose_device_type()
<commit_msg>Menu working fine. Without finish<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#
import os
import globalvar
import systemstorages
import config
import fields
import emc_cmds

def main_menu():
    global disk_volume, lun_size

    os.system('clear')
    print('[ Storage Adm ]\n[ Version {0} - © 2015 ]\n\n'.format(
        globalvar.version))

    stgadm = raw_input('STG Adm options\n\n'
                       '1. Add new volumes to existent host.\n'
                       '\nPlease choose an option: ')

    # Add new volumes to existent host menu

    if stgadm == '1':

        print('\n\nRequest informations\n')
        change = fields.Fields('change', 'Ticket/Change/Work Order: ')
        change.chkfieldstr()
        change = change.strvarout()

        hostname_client = fields.Fields('hostname', 'Hostname Client: ')
        hostname_client.chkfieldstr()
        hostname_client = hostname_client.strvarout()

        storage_name = raw_input('Storage Name: ')

        wwn_client = fields.Fields('wwn_client', 'WWN Server Client: ')
        wwn_client.chkfieldstr()
        wwn_client = wwn_client.strvarout()

        disk_volume = int()
        lun_size = int()

        while True:
            try:
                disk_volume = int(raw_input("Total Disk Required (GB): "))
                break
            except (TypeError, ValueError):
                print(
                    '\tERROR: Total Disk need to be an int value in GB. '
                    '\nDo not use GB. Example: 1000 for 1000GB (1TB)')

        while True:
            try:
                lun_size = int(raw_input("Default LUN size (GB): "))
                break
            except (TypeError, ValueError):
                print(
                    '\tERROR: LUN Size need to be an int value.'
                    '\nDo not use GB. Example 100 for 100GB size of LUNs')

        if ':' in wwn_client:
            wwn_client = wwn_client.replace(':', '')

        # select storage
        get_stg = systemstorages.SystemStorages()
        get_stg.selectstorage()

        stg_type = get_stg.gettype()

        if stg_type == 'EMC_VMAX':
            stg_sid = get_stg.getsid()
            stg_name = get_stg.getstorage()
            menu_emc_vmax(change, hostname_client, storage_name, stg_name,
                          stg_type, stg_sid, wwn_client)

        elif stg_type == 'EMC_VNX':
            pass

        else:
            print('ERRO: Storage type {0} invalid. Check config file'.format(
                stg_type))

def menu_emc_vmax(change=None, hostname_client=None, storage_name=None,
                  stg_name=None, stg_type=None, stg_sid=None, wwn_client=None):

    # get storage informations
    global pool_option

    chk_server = emc_cmds.VMAX(config.symcli_path, stg_sid,
                               wwn_client)

    print('\nCollecting some storage informations. Please wait...')

    ign = chk_server.get_ign(wwn_client)

    if ign[0] != 0:
        print('ERROR: {0}'.format(ign[1].replace('\n', '')))
        exit(1)
    else:
        ign = ign[1]

    mvn = chk_server.get_mvn(ign)

    if mvn[0] != 0:
        print('ERROR: {0}'.format(mvn[1]))
        exit(1)
    else:
        mvn = mvn[1]

    sgn = chk_server.get_sgn(mvn)

    if sgn[0] != 0:
        print('ERROR: {0}'.format(sgn[1]))
        exit(1)
    else:
        sgn = sgn[1]

    print('\nGetting information about pools from storage. Please wait...')

    # get storage pools
    lspool = chk_server.lspools()

    # check if command worked well.
    if lspool[0] != 0:
        print('Error: {0}'.format(lspool[1]))
        exit(1)

    lspool = lspool[1]
    print lspool
    lspool = lspool.split('\n')

    print('Select the Storage Pool from {0} storage (SID:{1})\n'
          .format(stg_name, stg_sid))

    # creating array to select storage pool
    pool_list = []

    for l_lspool in lspool[8:]:
        if l_lspool == '':
            break
        else:
            pool_list.append(l_lspool.split()[0])

    count = 0
    for l_pool in pool_list:
        print ('{0}: {1}'.format(count, l_pool))
        count += 1

    while True:
        try:
            pool_option = int(raw_input("Select the pool: "))
            break
        except IndexError:
            print(
                '\tERROR: Select an existing option between'
                '0 and {0}.'.format(count))

    def choose_device_type():
        print('Select the type of device you want create:\n')
        print('0. Meta device')
        print('1. Regular device')
        device_type = raw_input('\nChoose an option: ')

        if device_type == '0':

            device_type = 'meta'

            while True:
                try:
                    size_check = 1

                    while size_check != 0:
                        member_size = int(raw_input(
                            'What is the size of member? (GB): '))
                        size_check = lun_size % member_size
                        if size_check != 0:
                            print('ERRO: The member size ({0} GB) needs to '
                                  'be an integer divisor by the '
                                  'LUN Size ({1}GB).'
                                  .format(member_size, lun_size))

                        else:
                            return member_size, device_type

                    break
                except (TypeError, ValueError):
                    print(
                        '\tERROR: Total Disk need to be an int value in GB.'
                        '\nDo not use GB. Example: 1000 for 1000GB (1TB)')

        elif device_type == '1':

            device_type = 'regular'
            member_size = None
            return member_size, device_type

        else:
            print('ERRO: Wrong option.')
            choose_device_type()

    device_config = choose_device_type()
    lun_type = device_config[0]
    member_meta_size = device_config[1]

    print('\nConfig validation\n')
    print('\nClient Information')
    print(50 * '-')
    print('Register      : {0}'.format(change))
    print('Client Server : {0}'.format(hostname_client))
    print('Storage Name  : {0}'.format(storage_name))
    print('WWN Client    : {0}'.format(wwn_client))
    print('\nStorage Information')
    print(50 * '-')
    print('Storage Name          : {0}'.format(stg_name))
    print('Storage Type          : {0}'.format(stg_type))
    print('Storage SID           : {0}'.format(stg_sid))
    print('Initiator Group Name  : {0}'.format(ign))
    print('Making View Names     : {0}'.format(mvn))
    print('Storage Group Name    : {0}'.format(sgn))
    print('Storage Pool          : {0}'.format(pool_list[pool_option]))
    print('\nInformations about the request:')
    print(50 * '-')
    print('Disk Volume   : {0}GB'.format(disk_volume))
    print('LUN Size      : {0}GB'.format(lun_size))
    print('Device type   : {0}GB'.format(lun_type))
    if lun_type == 'meta':
        print('Member Size   : {0}GB (meta)'.format(member_meta_size))

    save_config = fields.YesNo('Do you would like save this allocation?: ',
                               'n')
    save_config = save_config.check()

    if save_config == 'y':
        pass
<|endoftext|>


<commit_before>#!/usr/bin/env python
from yahoo_finance import Share
import operator

def getStockPrices(stocks):
  #stocks must be an array of stock symbol strings

  # Refresh stock prices
  [Share(symbol).refresh() for symbol in stocks]

  # Return list of new prices
  return dict(zip(stocks,[Share(symbol).get_price() for symbol in stocks]))

def getFastestChangingPrices(oldstockprices,newstockprices):
  #stock prices must be dicts in the form (symbol,price)

  pdiff = dict()
  ptop = dict()
  for sym in newstockprices:
    try:
	  pdiff[sym] = str((float(newstockprices[sym])-float(oldstockprices[sym]))/float(oldstockprices[sym])*100)
	except:
	  pdiff[sym] = 0;

  pdiff = sorted(pdiff.items(), key=operator.itemgetter(1), reverse=True)
  return pdiff[:4]<commit_msg>Fixed tab/space issue in stockPrices.py<commit_after>#!/usr/bin/env python
from yahoo_finance import Share
import operator

def getStockPrices(stocks):
	#stocks must be an array of stock symbol strings

	# Refresh stock prices
	[Share(symbol).refresh() for symbol in stocks]

	# Return list of new prices
	return dict(zip(stocks,[Share(symbol).get_price() for symbol in stocks]))

def getFastestChangingPrices(oldstockprices,newstockprices):
	#stock prices must be dicts in the form (symbol,price)
	pdiff = dict()
	ptop = dict()
	for sym in newstockprices:
	try:
		pdiff[sym] = str((float(newstockprices[sym])-float(oldstockprices[sym]))/float(oldstockprices[sym])*100)
	except:
		pdiff[sym] = 0;

	pdiff = sorted(pdiff.items(), key=operator.itemgetter(1), reverse=True)
	return pdiff[:4]<|endoftext|>


<commit_before># This file is NOT licensed under the GPLv3, which is the license for the rest
# of YouCompleteMe.
#
# Here's the license text for this file:
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>

import os
import ycm_core

# These are the compilation flags that will be used in case there's no
# compilation database set (by default, one is not set).
# CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.
flags = [
'-Wall',
'-Wextra',
'-Wno-unused-parameter',
'-fno-strict-aliasing',
#'-std=c++0x',
'-x',
'c++',
'-isystem',
'/usr/include',
'-isystem',
'/usr/local/include',
'-I',
'/opt/ros/indigo/include',
'-I',
'../../devel/include',
'-I',
'include',
'-I',
'/usr/include/pcl-1.7',
'-I',
'/usr/include/eigen3',
'-I',
'/usr/local/caffe/include',
'-I',
'/usr/local/cuda/include',
'-I',
'rapid_db/include',
'-I',
'rapid_display/include',
'-I',
'rapid_fetch/include',
'-I',
'rapid_manipulation/include',
'-I',
'rapid_pbd/include',
'-I',
'rapid_pbd_msgs/include',
'-I',
'rapid_perception/include',
'-I',
'rapid_pr2/include',
'-I',
'rapid_ros/include',
'-I',
'rapid_sound/include',
'-I',
'rapid_utils/include',
'-I',
'rapid_viz/include',
'-I',
'../stf/include',
'-I',
'applications/object_search/include',
'-I',
'applications/object_search_msgs/include'
]

# Set this to the absolute path to the folder (NOT the file!) containing the
# compile_commands.json file to use that instead of 'flags'. See here for
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
#
# You can get CMake to generate this file for you by adding:
#   set( CMAKE_EXPORT_COMPILE_COMMANDS 1 )
# to your CMakeLists.txt file.
#
# Most projects will NOT need to set this to anything; you can just change the
# 'flags' list of compilation flags. Notice that YCM itself uses that approach.
compilation_database_folder = ''

if os.path.exists( compilation_database_folder ):
  database = ycm_core.CompilationDatabase( compilation_database_folder )
else:
  database = None

SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]

def DirectoryOfThisScript():
  return os.path.dirname( os.path.abspath( __file__ ) )

def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
  if not working_directory:
    return list( flags )
  new_flags = []
  make_next_absolute = False
  path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
  for flag in flags:
    new_flag = flag

    if make_next_absolute:
      make_next_absolute = False
      if not flag.startswith( '/' ):
        new_flag = os.path.join( working_directory, flag )

    for path_flag in path_flags:
      if flag == path_flag:
        make_next_absolute = True
        break

      if flag.startswith( path_flag ):
        path = flag[ len( path_flag ): ]
        new_flag = path_flag + os.path.join( working_directory, path )
        break

    if new_flag:
      new_flags.append( new_flag )
  return new_flags

def IsHeaderFile( filename ):
  extension = os.path.splitext( filename )[ 1 ]
  return extension in [ '.h', '.hxx', '.hpp', '.hh' ]

def GetCompilationInfoForFile( filename ):
  # The compilation_commands.json file generated by CMake does not have entries
  # for header files. So we do our best by asking the db for flags for a
  # corresponding source file, if any. If one exists, the flags for that file
  # should be good enough.
  if IsHeaderFile( filename ):
    basename = os.path.splitext( filename )[ 0 ]
    for extension in SOURCE_EXTENSIONS:
      replacement_file = basename + extension
      if os.path.exists( replacement_file ):
        compilation_info = database.GetCompilationInfoForFile(
          replacement_file )
        if compilation_info.compiler_flags_:
          return compilation_info
    return None
  return database.GetCompilationInfoForFile( filename )

def FlagsForFile( filename, **kwargs ):
  if database:
    # Bear in mind that compilation_info.compiler_flags_ does NOT return a
    # python list, but a "list-like" StringVec object
    compilation_info = GetCompilationInfoForFile( filename )
    if not compilation_info:
      return None

    final_flags = MakeRelativePathsInFlagsAbsolute(
      compilation_info.compiler_flags_,
      compilation_info.compiler_working_dir_ )

    # NOTE: This is just for YouCompleteMe; it's highly likely that your project
    # does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
    # ycm_extra_conf IF YOU'RE NOT 100% SURE YOU NEED IT.
    try:
      final_flags.remove( '-stdlib=libc++' )
    except ValueError:
      pass
  else:
    relative_to = DirectoryOfThisScript()
    final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )

  return {
    'flags': final_flags,
    'do_cache': True
  }
<commit_msg>Added extra directories.<commit_after># This file is NOT licensed under the GPLv3, which is the license for the rest
# of YouCompleteMe.
#
# Here's the license text for this file:
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>

import os
import ycm_core

# These are the compilation flags that will be used in case there's no
# compilation database set (by default, one is not set).
# CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.
flags = [
'-Wall',
'-Wextra',
'-Wno-unused-parameter',
'-fno-strict-aliasing',
#'-std=c++0x',
'-x',
'c++',
'-isystem',
'/usr/include',
'-isystem',
'/usr/local/include',
'-I',
'/opt/ros/indigo/include',
'-I',
'../../devel/include',
'-I',
'include',
'-I',
'/usr/include/pcl-1.7',
'-I',
'/usr/include/eigen3',
'-I',
'/usr/local/caffe/include',
'-I',
'/usr/local/cuda/include',
'-I',
'rapid_db/include',
'-I',
'rapid_display/include',
'-I',
'rapid_fetch/include',
'-I',
'rapid_manipulation/include',
'-I',
'rapid_pbd/include',
'-I',
'rapid_pbd_msgs/include',
'-I',
'rapid_perception/include',
'-I',
'rapid_pr2/include',
'-I',
'rapid_ros/include',
'-I',
'rapid_sound/include',
'-I',
'rapid_utils/include',
'-I',
'rapid_viz/include',
'-I',
'applications/object_search/include',
'-I',
'applications/object_search_msgs/include',
'-I',
'../transform_graph/include',
'-I',
'../robot_markers/include',
]

# Set this to the absolute path to the folder (NOT the file!) containing the
# compile_commands.json file to use that instead of 'flags'. See here for
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
#
# You can get CMake to generate this file for you by adding:
#   set( CMAKE_EXPORT_COMPILE_COMMANDS 1 )
# to your CMakeLists.txt file.
#
# Most projects will NOT need to set this to anything; you can just change the
# 'flags' list of compilation flags. Notice that YCM itself uses that approach.
compilation_database_folder = ''

if os.path.exists( compilation_database_folder ):
  database = ycm_core.CompilationDatabase( compilation_database_folder )
else:
  database = None

SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]

def DirectoryOfThisScript():
  return os.path.dirname( os.path.abspath( __file__ ) )

def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
  if not working_directory:
    return list( flags )
  new_flags = []
  make_next_absolute = False
  path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
  for flag in flags:
    new_flag = flag

    if make_next_absolute:
      make_next_absolute = False
      if not flag.startswith( '/' ):
        new_flag = os.path.join( working_directory, flag )

    for path_flag in path_flags:
      if flag == path_flag:
        make_next_absolute = True
        break

      if flag.startswith( path_flag ):
        path = flag[ len( path_flag ): ]
        new_flag = path_flag + os.path.join( working_directory, path )
        break

    if new_flag:
      new_flags.append( new_flag )
  return new_flags

def IsHeaderFile( filename ):
  extension = os.path.splitext( filename )[ 1 ]
  return extension in [ '.h', '.hxx', '.hpp', '.hh' ]

def GetCompilationInfoForFile( filename ):
  # The compilation_commands.json file generated by CMake does not have entries
  # for header files. So we do our best by asking the db for flags for a
  # corresponding source file, if any. If one exists, the flags for that file
  # should be good enough.
  if IsHeaderFile( filename ):
    basename = os.path.splitext( filename )[ 0 ]
    for extension in SOURCE_EXTENSIONS:
      replacement_file = basename + extension
      if os.path.exists( replacement_file ):
        compilation_info = database.GetCompilationInfoForFile(
          replacement_file )
        if compilation_info.compiler_flags_:
          return compilation_info
    return None
  return database.GetCompilationInfoForFile( filename )

def FlagsForFile( filename, **kwargs ):
  if database:
    # Bear in mind that compilation_info.compiler_flags_ does NOT return a
    # python list, but a "list-like" StringVec object
    compilation_info = GetCompilationInfoForFile( filename )
    if not compilation_info:
      return None

    final_flags = MakeRelativePathsInFlagsAbsolute(
      compilation_info.compiler_flags_,
      compilation_info.compiler_working_dir_ )

    # NOTE: This is just for YouCompleteMe; it's highly likely that your project
    # does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
    # ycm_extra_conf IF YOU'RE NOT 100% SURE YOU NEED IT.
    try:
      final_flags.remove( '-stdlib=libc++' )
    except ValueError:
      pass
  else:
    relative_to = DirectoryOfThisScript()
    final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )

  return {
    'flags': final_flags,
    'do_cache': True
  }
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""This module contains all the built in cli commands of corral"""

# =============================================================================
# IMPORTS
# =============================================================================

import collections
import code
import os
import logging
import sys

import six

from .. import db, conf, run
from ..libs import sqlalchemy_sql_shell as sql_shell

from .base import BaseCommand

# =============================================================================
# BUILT-INS COMMANDS
# =============================================================================

class CreateDB(BaseCommand):
    """Create all the database structure"""

    options = {
        "title": "createdb"}

    def setup(self):
        self.parser.add_argument(
            "--noinput", dest="noinput", action="store_true", default=False,
            help="Create the database without asking")

    def handle(self, noinput, **kwargs):
        if noinput:
            answer = "yes"
        else:
            answer = self.ask(
                "Do you want to create the database [Yes/no]? ").lower()
        while answer.lower() not in ("yes", "no"):
            answer = self.ask("Please answer 'yes' or 'no': ").lower()
        if answer == "yes":
            db.create_all()

class Shell(BaseCommand):
    """Run the Python shell inside Corral enviroment"""

    options = {
        "title": "shell"}

    def _get_locals(self):
        slocals = {}
        slocals.update({
            cls.__name__: cls for cls in db.Model.__subclasses__()})
        if hasattr(conf.settings, "SHELL_LOCALS"):
            slocals.update(conf.settings.SHELL_LOCALS)
        return slocals

    def _create_banner(self, slocals):
        by_module = collections.defaultdict(list)
        for k, v in slocals.items():
            module_name = getattr(v, "__module__", None) or ""
            by_module[module_name].append(k)
        lines = []
        for module_name, imported in sorted(six.iteritems(by_module)):
            prefix = ", ".join(imported)
            suffix = "({})".format(module_name) if module_name else ""
            line = "LOAD: {} {}".format(prefix, suffix)
            lines.append(line)
        lines.append("-" * 80)
        return "\n".join(lines)

    def run_plain(self, slocals, banner):
        console = code.InteractiveConsole(slocals)
        console.interact(banner)

    def run_ipython(self, slocals, banner):
        from IPython import start_ipython
        start_ipython(
            argv=['--TerminalInteractiveShell.banner2={}'.format(banner)],
            user_ns=slocals)

    def run_bpython(self, slocals, banner):
        from bpython import embed
        embed(locals_=slocals, banner=banner)

    def setup(self):
        self.shells = collections.OrderedDict()
        try:
            import IPython  # noqa
            self.shells["ipython"] = self.run_ipython
        except ImportError:
            pass
        try:
            import bpython  # noqa
            self.shells["bpython"] = self.run_bpython
        except ImportError:
            pass
        self.shells["plain"] = self.run_plain

        self.parser.add_argument(
            "--shell", "-s", dest="shell", action="store",
            choices=self.shells.keys(), default=list(self.shells.keys())[0],
            help="Specify the shell to be used")

    def handle(self, shell):
        slocals = self._get_locals()
        with db.session_scope() as session:
            slocals["session"] = session
            banner = self._create_banner(slocals)
            shell = self.shells[shell]
            shell(slocals, banner)

class Notebook(BaseCommand):
    """Run the Jupyter notebook inside Corral enviroment"""

    options = {
        "title": "notebook"}

    def handle(self):
        from IPython import start_ipython
        start_ipython(argv=['notebook'])

class DBShell(BaseCommand):
    """Run an SQL shell throught sqlalchemy"""

    options = {"title": "dbshell"}

    def handle(self):
        elogger = logging.getLogger('sqlalchemy.engine')
        original_level = elogger.level
        try:
            elogger.setLevel(logging.WARNING)
            print("Connected to: {}".format(db.engine))
            sql_shell.run(db.engine)
        finally:
            elogger.setLevel(original_level)

class Exec(BaseCommand):
    """Execute file inside corral environment"""

    options = {"title": "exec"}

    def setup(self):
        self.parser.add_argument("path", action="store", help="Path to script")

    def handle(self, path):
        ns = {}
        fname = os.path.basename(path)
        with open(path) as fp:
            code = compile(fp.read(), fname, 'exec')
            exec(code, ns, ns)

class Load(BaseCommand):
    """Excecute the loader class"""

    options = {"title": "load"}

    def handle(self):
        cls = run.load_loader()
        run.execute_loader(cls, sync=True)

class Run(BaseCommand):
    """Excecute the steps in order or one step in particular"""

    OPTIONS = {"title": "run"}

    def _step_classes(self, class_name):
        if class_name in self.buff:
            self.parser.error("Duplicated step name '{}'".format(class_name))
        self.buff.add(class_name)
        try:
            return self.mapped_steps[class_name]
        except KeyError:
            self.parser.error("Invalid step name '{}'".format(class_name))

    def setup(self):
        self.all_steps = run.load_steps()
        self.mapped_steps = {cls.__name__: cls for cls in self.all_steps}
        self.buff = set()

        self.parser.add_argument(
            "--steps", dest="step_classes", action="store", nargs="+",
            help="Step class name", default=self.all_steps,
            type=self._step_classes)
        self.parser.add_argument(
            "--sync", dest="sync", action="store_true",
            help="Execute every step synchronous", default=False)

    def handle(self, step_classes, sync):
        procs = []
        for step_cls in step_classes:
            proc = run.execute_step(step_cls, sync=sync)
            procs.append(proc)
        if not sync:
            for proc in procs:
                proc.join()
            exitcodes = [proc.exitcode for proc in procs]

            status = sum(exitcodes)
            sys.exit(status)
<commit_msg>only project models are loaded in shell ns<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""This module contains all the built in cli commands of corral"""

# =============================================================================
# IMPORTS
# =============================================================================

import inspect
import collections
import code
import os
import logging
import sys

import six

from .. import db, conf, run
from ..libs import sqlalchemy_sql_shell as sql_shell

from .base import BaseCommand

# =============================================================================
# BUILT-INS COMMANDS
# =============================================================================

class CreateDB(BaseCommand):
    """Create all the database structure"""

    options = {
        "title": "createdb"}

    def setup(self):
        self.parser.add_argument(
            "--noinput", dest="noinput", action="store_true", default=False,
            help="Create the database without asking")

    def handle(self, noinput, **kwargs):
        if noinput:
            answer = "yes"
        else:
            answer = self.ask(
                "Do you want to create the database [Yes/no]? ").lower()
        while answer.lower() not in ("yes", "no"):
            answer = self.ask("Please answer 'yes' or 'no': ").lower()
        if answer == "yes":
            db.create_all()

class Shell(BaseCommand):
    """Run the Python shell inside Corral enviroment"""

    options = {
        "title": "shell"}

    def _get_locals(self):
        slocals = {}
        slocals.update({
            k: v for k, v in vars(db.load_models_module()).items()
            if inspect.isclass(v) and issubclass(v, db.Model)})
        if hasattr(conf.settings, "SHELL_LOCALS"):
            slocals.update(conf.settings.SHELL_LOCALS)
        return slocals

    def _create_banner(self, slocals):
        by_module = collections.defaultdict(list)
        for k, v in slocals.items():
            module_name = getattr(v, "__module__", None) or ""
            by_module[module_name].append(k)
        lines = []
        for module_name, imported in sorted(six.iteritems(by_module)):
            prefix = ", ".join(imported)
            suffix = "({})".format(module_name) if module_name else ""
            line = "LOAD: {} {}".format(prefix, suffix)
            lines.append(line)
        lines.append("-" * 80)
        return "\n".join(lines)

    def run_plain(self, slocals, banner):
        console = code.InteractiveConsole(slocals)
        console.interact(banner)

    def run_ipython(self, slocals, banner):
        from IPython import start_ipython
        start_ipython(
            argv=['--TerminalInteractiveShell.banner2={}'.format(banner)],
            user_ns=slocals)

    def run_bpython(self, slocals, banner):
        from bpython import embed
        embed(locals_=slocals, banner=banner)

    def setup(self):
        self.shells = collections.OrderedDict()
        try:
            import IPython  # noqa
            self.shells["ipython"] = self.run_ipython
        except ImportError:
            pass
        try:
            import bpython  # noqa
            self.shells["bpython"] = self.run_bpython
        except ImportError:
            pass
        self.shells["plain"] = self.run_plain

        self.parser.add_argument(
            "--shell", "-s", dest="shell", action="store",
            choices=self.shells.keys(), default=list(self.shells.keys())[0],
            help="Specify the shell to be used")

    def handle(self, shell):
        slocals = self._get_locals()
        with db.session_scope() as session:
            slocals["session"] = session
            banner = self._create_banner(slocals)
            shell = self.shells[shell]
            shell(slocals, banner)

class Notebook(BaseCommand):
    """Run the Jupyter notebook inside Corral enviroment"""

    options = {
        "title": "notebook"}

    def handle(self):
        from IPython import start_ipython
        start_ipython(argv=['notebook'])

class DBShell(BaseCommand):
    """Run an SQL shell throught sqlalchemy"""

    options = {"title": "dbshell"}

    def handle(self):
        elogger = logging.getLogger('sqlalchemy.engine')
        original_level = elogger.level
        try:
            elogger.setLevel(logging.WARNING)
            print("Connected to: {}".format(db.engine))
            sql_shell.run(db.engine)
        finally:
            elogger.setLevel(original_level)

class Exec(BaseCommand):
    """Execute file inside corral environment"""

    options = {"title": "exec"}

    def setup(self):
        self.parser.add_argument("path", action="store", help="Path to script")

    def handle(self, path):
        ns = {}
        fname = os.path.basename(path)
        with open(path) as fp:
            code = compile(fp.read(), fname, 'exec')
            exec(code, ns, ns)

class Load(BaseCommand):
    """Excecute the loader class"""

    options = {"title": "load"}

    def handle(self):
        cls = run.load_loader()
        run.execute_loader(cls, sync=True)

class Run(BaseCommand):
    """Excecute the steps in order or one step in particular"""

    OPTIONS = {"title": "run"}

    def _step_classes(self, class_name):
        if class_name in self.buff:
            self.parser.error("Duplicated step name '{}'".format(class_name))
        self.buff.add(class_name)
        try:
            return self.mapped_steps[class_name]
        except KeyError:
            self.parser.error("Invalid step name '{}'".format(class_name))

    def setup(self):
        self.all_steps = run.load_steps()
        self.mapped_steps = {cls.__name__: cls for cls in self.all_steps}
        self.buff = set()

        self.parser.add_argument(
            "--steps", dest="step_classes", action="store", nargs="+",
            help="Step class name", default=self.all_steps,
            type=self._step_classes)
        self.parser.add_argument(
            "--sync", dest="sync", action="store_true",
            help="Execute every step synchronous", default=False)

    def handle(self, step_classes, sync):
        procs = []
        for step_cls in step_classes:
            proc = run.execute_step(step_cls, sync=sync)
            procs.append(proc)
        if not sync:
            for proc in procs:
                proc.join()
            exitcodes = [proc.exitcode for proc in procs]

            status = sum(exitcodes)
            sys.exit(status)
<|endoftext|>


<commit_before>import os
import ycm_core

flags = [
    '-Wall',
    '-Wextra',
    '-Werror',
    '-Wno-long-long',
    '-Wno-variadic-macros',
    '-fexceptions',
    '-DNDEBUG',
    '-std=c++11',
    '-x',
    'c++',
    '-I',
    '.',
    '-I',
    'include/neoc',
    'opt/cpr/include'
]

SOURCE_EXTENSIONS = ['.cpp', '.cc', '.c']

def DirectoryOfThisScript():
  return os.path.dirname( os.path.abspath( __file__ ) )

def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
  if not working_directory:
    return list( flags )
  new_flags = []
  make_next_absolute = False
  path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
  for flag in flags:
    new_flag = flag

    if make_next_absolute:
      make_next_absolute = False
      if not flag.startswith( '/' ):
        new_flag = os.path.join( working_directory, flag )

    for path_flag in path_flags:
      if flag == path_flag:
        make_next_absolute = True
        break

      if flag.startswith( path_flag ):
        path = flag[ len( path_flag ): ]
        new_flag = path_flag + os.path.join( working_directory, path )
        break

    if new_flag:
      new_flags.append( new_flag )
  return new_flags

def IsHeaderFile( filename ):
  extension = os.path.splitext( filename )[ 1 ]
  return extension in ['.h', '.hpp']

def GetCompilationInfoForFile( filename ):
  # The compilation_commands.json file generated by CMake does not have entries
  # for header files. So we do our best by asking the db for flags for a
  # corresponding source file, if any. If one exists, the flags for that file
  # should be good enough.
  if IsHeaderFile( filename ):
    basename = os.path.splitext( filename )[ 0 ]
    for extension in SOURCE_EXTENSIONS:
      replacement_file = basename + extension
      if os.path.exists( replacement_file ):
        compilation_info = database.GetCompilationInfoForFile(
          replacement_file )
        if compilation_info.compiler_flags_:
          return compilation_info
    return None
  return database.GetCompilationInfoForFile( filename )

def FlagsForFile( filename, **kwargs ):
    relative_to = DirectoryOfThisScript()
    final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )

    return {
        'flags': final_flags,
        'do_cache': True
    }

<commit_msg>Forgot to update YCM's config file<commit_after>import os
import ycm_core

flags = [
    '-Wall',
    '-Wextra',
    '-Werror',
    '-Wno-long-long',
    '-Wno-variadic-macros',
    '-fexceptions',
    '-DNDEBUG',
    '-std=c++11',
    '-x',
    'c++',
    '-I',
    '.',
    '-I',
    'include/neapolis',
    'opt/cpr/include'
]

SOURCE_EXTENSIONS = ['.cpp', '.cc', '.c']

def DirectoryOfThisScript():
  return os.path.dirname( os.path.abspath( __file__ ) )

def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
  if not working_directory:
    return list( flags )
  new_flags = []
  make_next_absolute = False
  path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
  for flag in flags:
    new_flag = flag

    if make_next_absolute:
      make_next_absolute = False
      if not flag.startswith( '/' ):
        new_flag = os.path.join( working_directory, flag )

    for path_flag in path_flags:
      if flag == path_flag:
        make_next_absolute = True
        break

      if flag.startswith( path_flag ):
        path = flag[ len( path_flag ): ]
        new_flag = path_flag + os.path.join( working_directory, path )
        break

    if new_flag:
      new_flags.append( new_flag )
  return new_flags

def IsHeaderFile( filename ):
  extension = os.path.splitext( filename )[ 1 ]
  return extension in ['.h', '.hpp']

def GetCompilationInfoForFile( filename ):
  # The compilation_commands.json file generated by CMake does not have entries
  # for header files. So we do our best by asking the db for flags for a
  # corresponding source file, if any. If one exists, the flags for that file
  # should be good enough.
  if IsHeaderFile( filename ):
    basename = os.path.splitext( filename )[ 0 ]
    for extension in SOURCE_EXTENSIONS:
      replacement_file = basename + extension
      if os.path.exists( replacement_file ):
        compilation_info = database.GetCompilationInfoForFile(
          replacement_file )
        if compilation_info.compiler_flags_:
          return compilation_info
    return None
  return database.GetCompilationInfoForFile( filename )

def FlagsForFile( filename, **kwargs ):
    relative_to = DirectoryOfThisScript()
    final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )

    return {
        'flags': final_flags,
        'do_cache': True
    }

<|endoftext|>


<commit_before>from setuptools import setup

VERSION = '6.9.6-SNAPSHOT'

setup(
    name='Marvin',
    version=VERSION,
    description='Marvin - Python client for Cosmic',
    author='Mission Critical Cloud',
    author_email='int-cloud@schubergphilis.com',
    maintainer='Mission Critical Cloud',
    maintainer_email='int-cloud@schubergphilis.com',
    long_description='Marvin is the Cosmic python client written around the unittest framework',
    url='https://github.com/MissionCriticalCloud/marvin',
    packages=[
        'marvin',
        'marvin.cloudstackAPI',
        'marvin.lib',
        'marvin.config',
        'marvin.utils',
    ],
    package_dir={'marvin': 'marvin'},
    package_data={
        'marvin': [
            'utils/marvin_logging.yaml'
        ]
    },
    license='LICENSE.txt',
    install_requires=[
        'mysql-connector-python >= 1.1.6',
        'requests >= 2.2.1',
        'paramiko >= 1.13.0',
        'nose >= 1.3.3',
        'ddt >= 0.4.0',
        'pyvmomi >= 5.5.0',
        'netaddr >= 0.7.14',
        'pyyaml  >= 3.11'
    ],
    py_modules=[
        'marvin.marvinPlugin'
    ],
    zip_safe=False,
    entry_points={
        'nose.plugins': [
            'marvinPlugin = marvin.marvinPlugin:MarvinPlugin'
        ]
    }
)
<commit_msg>Updated Marvin version to 6.9.7-SNAPSHOT<commit_after>from setuptools import setup

VERSION = '6.9.7-SNAPSHOT'

setup(
    name='Marvin',
    version=VERSION,
    description='Marvin - Python client for Cosmic',
    author='Mission Critical Cloud',
    author_email='int-cloud@schubergphilis.com',
    maintainer='Mission Critical Cloud',
    maintainer_email='int-cloud@schubergphilis.com',
    long_description='Marvin is the Cosmic python client written around the unittest framework',
    url='https://github.com/MissionCriticalCloud/marvin',
    packages=[
        'marvin',
        'marvin.cloudstackAPI',
        'marvin.lib',
        'marvin.config',
        'marvin.utils',
    ],
    package_dir={'marvin': 'marvin'},
    package_data={
        'marvin': [
            'utils/marvin_logging.yaml'
        ]
    },
    license='LICENSE.txt',
    install_requires=[
        'mysql-connector-python >= 1.1.6',
        'requests >= 2.2.1',
        'paramiko >= 1.13.0',
        'nose >= 1.3.3',
        'ddt >= 0.4.0',
        'pyvmomi >= 5.5.0',
        'netaddr >= 0.7.14',
        'pyyaml  >= 3.11'
    ],
    py_modules=[
        'marvin.marvinPlugin'
    ],
    zip_safe=False,
    entry_points={
        'nose.plugins': [
            'marvinPlugin = marvin.marvinPlugin:MarvinPlugin'
        ]
    }
)
<|endoftext|>


<commit_before>def FlagsForFile(filename, **kwargs):
    return {
        'flags': [
            '-I', '/usr/local/include',
            '-I', '/usr/local/include/gtest',
            '-isystem',
            '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1',
            '-isystem',
            '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.0/include',
            '-isystem',
            '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include',
            '-isystem',
            '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include'
        ],
        'do_cache':
        False
    }
<commit_msg>Added compiler flags for YCM<commit_after>def FlagsForFile(filename, **kwargs):
    return {
        'flags': [
            '-Wall', '-Wextra', '-Werror', '-std=c++14'
            '-I', '/usr/local/include', '-I', '/usr/local/include/gtest',
            '-isystem',
            '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1',
            '-isystem',
            '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.0/include',
            '-isystem',
            '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include',
            '-isystem',
            '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include'
        ],
        'do_cache':
        False
    }
<|endoftext|>


<commit_before>from pylab import *

def rho(u,v):
    return u - min(u,v)

def path_coupled(N,J,level,w,T):
    Np = 20.
    Nt = 100000000.
    t_grid = zeros(Nt)

    N0 = N     # fine grid
    N1 = N/(pow(J,level)) # coarse grid

    X0 = zeros((Nt,N0))
    X1 = zeros((Nt,N1))

    Y0 = zeros((Nt,N0))
    Y1 = zeros((Nt,N1))

    x0 = zeros(N0)
    x1 = zeros(N1)
    y0 = zeros(N0)
    y1 = zeros(N1)

    # store absolute times for each reaction channel
    # reaction kinetics
    # there is ony one reaction: X+Y->0

    T_rx_0c1 = zeros(N0)
    T_rx_0m1 = zeros(N0)
    T_rx_1m0 = zeros(N1)

    # no diffusion for Y moleculers
    T_dx_0c1_l = zeros() # left diffusion
    T_dx_0c1_r = zeros() # right diffusions
    T_dx_0m1_l = zeros() #
    T_dx_1m0_l = zeros() #

    # compute reaction rates
    for i in range(N0):
        T_rx_0c1[i] = min(x0[i]*)
        T_rx_0c1[i] = min(x0[i]*)
        T_rx_0c1[i] = min(x0[i]*)
    for i in range(N1):

    # find min time and it's index
    m1 = min(T_rx_0c1)

    #update state of system
    x0[:]
    x0[:]
    x0[:]
    x0[:]

    while (t_grid[i] < T) and (i<Nt):
        i = i+1
        x0[:] = X0[i-1]
        x1[:] = X1[i-1]

        y0[:] = Y0[i-1]
        y1[:] = Y1[i-1]

        for i in range()
        r = rand()
        # find next reaction index
        # using r,x0,x1,y0,y1

        # implement reaction
<commit_msg>Worked on NRM<commit_after>from pylab import *

def rho(u,v):
    return u - min(u,v)

def exponential0(l):
    exp_max = 100000000000000.
    if (l <= 0):
        return exp_max
    else:
        return exponential(l)

def path_coupled(N,J,level,w,T):
    Np = 20.
    Nt = 100000000.
    t_grid = zeros(Nt)

    N0 = int(N/(pow(J,level)))     # fine grid
    N1 = int(N/(pow(J,level+1))) # coarse grid

    X0 = zeros((Nt,N0))
    X1 = zeros((Nt,N1))

    Y0 = zeros((Nt,N0))
    Y1 = zeros((Nt,N1))

    x0 = ones(N0)/(pow(J,level))
    x1 = ones(N1)/(pow(J,level+1))
    y0 = ones(N0)/(pow(J,level))
    y1 = ones(N1)/(pow(J,level+1))

    # only one reaction: X+Y->0
    t_r_0c1 = zeros(N0)
    t_r_0m1 = zeros(N0)
    t_r_1m0 = zeros(N1)

    a_r_0c1 = zeros(N0)
    a_r_0m1 = zeros(N0)
    a_r_1m0 = zeros(N1)

    #note: no diffusion for Y moleculers
    a_dx_0c1_l = zeros(N1-1) # left diffusion
    a_dx_0m1_l = zeros(N1-1)
    a_dx_1m0_l = zeros(N1-1)

    t_dx_0c1_l = zeros(N1-1) # left diffusion
    t_dx_0m1_l = zeros(N1-1)
    t_dx_1m0_l = zeros(N1-1)

    a_dx_0c1_r = zeros(N1-1) # right diffusions
    a_dx_0m1_r = zeros(N1-1)
    a_dx_1m0_r = zeros(N1-1)

    t_dx_0c1_r = zeros(N1-1) # right diffusions
    t_dx_0m1_r = zeros(N1-1)
    t_dx_1m0_r = zeros(N1-1)

    # these are the uncoupled diffusion channels on the fine grid
    a_dx_0_l = zeros(N1*(J-1))
    a_dx_0_r = zeros(N1*(J-1))

    t_dx_0_l = zeros(N1*(J-1))
    t_dx_0_r = zeros(N1*(J-1))

    # compute initial reaction rates and times
    # reaction kinetics

    for i in range(N1):
        a_sum = 0.
        for m in range(J):
            a_r_0c1[i*J+m] = min(x0[i*J+m]*y0[i*J+m],x1[i]*y1[i]*pow(J,-1.))
            a_r_0m1[i*J+m] = rho(x0[i*J+m]*y0[i*J+m],x1[i]*y1[i]*pow(J,-1.))
            t_r_0c1[i*J+m] = exponential0(a_r_0c1[i*J+m])
            t_r_0m1[i*J+m] = exponential0(a_r_0m1[i*J+m])
            a_sum = a_sum +x0[i*J+m]*y0[i*J+m]
        a_r_0m1[i] = rho(a_sum,x1[i]*y1[i])
        t_r_0m1[i] = exponential0(a_r_0m1[i])

    # diffusion
    for i in range(N1-1):
        a_dx_0c1_r[i] = min(w*x0[i*J],w*x1[i])
        a_dx_0m1_r[i] = rho(w*x0[i*J],w*x1[i])
        a_dx_1m0_r[i] = rho(w*x1[i],w*x0[i*J])
        a_dx_0c1_l[i] = min(w*x0[(i+1)*J],w*x1[i+1])
        a_dx_0m1_l[i] = rho(w*x0[(i+1)*J],w*x1[i+1])
        a_dx_1m0_l[i] = rho(w*x1[i+1],w*x0[(i+1)*J])

        t_dx_0c1_r[i] = exponential0(a_dx_0c1_r[i])
        t_dx_0m1_r[i] = exponential0(a_dx_0m1_r[i])
        t_dx_1m0_r[i] = exponential0(a_dx_1m0_r[i])
        t_dx_0c1_l[i] = exponential0(a_dx_0c1_l[i])
        t_dx_0m1_l[i] = exponential0(a_dx_0m1_l[i])
        t_dx_1m0_l[i] = exponential0(a_dx_1m0_l[i])

        for m in range(J-1):
            a_dx_0_r[i*(J-1)+m] = w*x0[i*J+m]
            a_dx_0_l[i*(J-1)+m] = w*x0[i*J+m+1]

            t_dx_0_r[i*(J-1)+m] = exponential0(a_dx_0_r[i*(J-1)+m])
            t_dx_0_l[i*(J-1)+m] = exponential0(a_dx_0_l[i*(J-1)+m])

    # find min time and it's index

    # generate 11 reactions
    T = array([min(t_r_0c1),min(t_r_0m1),min(t_r_1m0),
     min(t_dx_0c1_l),min(t_dx_0m1_l),min(t_dx_1m0_l),min(t_dx_0_l),
     min(t_dx_0c1_r),min(t_dx_0m1_r),min(t_dx_1m0_r),min(t_dx_0_r)])

    R = array([argmin(t_r_0c1),argmin(t_r_0m1),argmin(t_r_1m0),\
     argmin(t_dx_0c1_l),argmin(t_dx_0m1_l),argmin(t_dx_1m0_l),argmin(t_dx_0_l),\
     argmin(t_dx_0c1_r),argmin(t_dx_0m1_r),argmin(t_dx_1m0_r),argmin(t_dx_0_r)])

    t_next = min(T)
    r_next = argmin(T)
    if r_next == 0:
        # reaction: common
        k = R[argmin(T)]
        v0 = -identity(N0)[k]
        v1 = -identity(N1)[k/J]
        u0 = -identity(N0)[k]
        u1 = -identity(N1)[k/J]
    elif r_next == 1:
        # reaction: only fine grid
        k = R[argmin(T)]
        v0 = -identity(N0)[k]
        v1 = 0
        u0 = -identity(N0)[k]
        u1 = 0
    elif r_next == 2:
        # reaction: only coarse grid
        k = R[argmin(T)]
        v0 = 0.
        v1 = -identity(N1)[k]
        u0 = 0.
        u1 = -identity(N1)[k]
    elif r_next == 3:
        # left diffusion: common
        k = R[argmin(T)]
        v0 = identity(N0)[k]-identity(N0)[k+1]
        v1 = identity(N1)[k/J]-identity(N0)[k/J+1]
        u0 = 0
        u1 = 0
    elif r_next == 4:
        # left diffusion: only fine grid
        k = R[argmin(T)]
        v0 = identity(N0)[k]-identity(N0)[k+1]
        v1 = 0
        u0 = 0
        u1 = 0
    elif r_next == 5:
        # left diffusion: only coarse grid
        k = R[argmin(T)]
        v0 = 0
        v1 = identity(N1)[k]-identity(N1)[k+1]
        u0 = 0
        u1 = 0
    elif r_next == 6:
        # left diffusion: only fine grid
        k = R[argmin(T)]
        v0 = identity(N0)[k]-identity(N0)[k+1]
        v1 = 0
        u0 = 0
        u1 = 0
    elif r_next == 7:
        # right diffusion: common
        k = R[argmin(T)]
        v0 = -identity(N0)[k]+identity(N0)[k+1]
        v1 = -identity(N1)[k/J]+identity(N0)[k/J+1]
        u0 = 0
        u1 = 0
    elif r_next == 8:
        # right diffusion: only fine grid
        k = R[argmin(T)]
        v0 = -identity(N0)[k]+identity(N0)[k+1]
        v1 = 0
        u0 = 0
        u1 = 0
    elif r_next == 9:
        # right diffusion: only coarse grid
        k = R[argmin(T)]
        v0 = 0
        v1 = -identity(N1)[k]+identity(N1)[k+1]
        u0 = 0
        u1 = 0
    elif r_next == 10:
        # right diffusion: only fine grid
        k = R[argmin(T)]
        v0 = -identity(N0)[k]+identity(N0)[k+1]
        v1 = 0
        u0 = 0
        u1 = 0

    # recaluate rates

    # recaluate rates

    while (t_grid[i] < T) and (i<Nt):
        i = i+1
        x0[:] = X0[i-1]
        x1[:] = X1[i-1]

        y0[:] = Y0[i-1]
        y1[:] = Y1[i-1]
<|endoftext|>


<commit_before>def FlagsForFile(filename, **kwargs):
  return {
    'flags': [
      '-std=c99', '-D_POSIX_C_SOURCE=200112L', '-Wall', '-Wextra', '-Isrc/'
      ],
    'do_cache': True
  }
<commit_msg>Let YouCompleteMe use large file offsets<commit_after>def FlagsForFile(filename, **kwargs):
  return {
    'flags': [
      '-std=c99', '-D_POSIX_C_SOURCE=200112L', '-D_FILE_OFFSET_BITS=64',
      '-Wall', '-Wextra', '-Isrc/'
      ],
    'do_cache': True
  }
<|endoftext|>


<commit_before># Generated by YCM Generator at 2016-04-03 21:46:07.011244

# This file is NOT licensed under the GPLv3, which is the license for the rest
# of YouCompleteMe.
#
# Here's the license text for this file:
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>

import os
import ycm_core

flags = [
    '-x',
    'c',
    '-Wall',
]

# Set this to the absolute path to the folder (NOT the file!) containing the
# compile_commands.json file to use that instead of 'flags'. See here for
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
#
# You can get CMake to generate this file for you by adding:
#   set( CMAKE_EXPORT_COMPILE_COMMANDS 1 )
# to your CMakeLists.txt file.
#
# Most projects will NOT need to set this to anything; you can just change the
# 'flags' list of compilation flags. Notice that YCM itself uses that approach.
compilation_database_folder = ''

if os.path.exists( compilation_database_folder ):
  database = ycm_core.CompilationDatabase( compilation_database_folder )
else:
  database = None

SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]

def DirectoryOfThisScript():
  return os.path.dirname( os.path.abspath( __file__ ) )

def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
  if not working_directory:
    return list( flags )
  new_flags = []
  make_next_absolute = False
  path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
  for flag in flags:
    new_flag = flag

    if make_next_absolute:
      make_next_absolute = False
      if not flag.startswith( '/' ):
        new_flag = os.path.join( working_directory, flag )

    for path_flag in path_flags:
      if flag == path_flag:
        make_next_absolute = True
        break

      if flag.startswith( path_flag ):
        path = flag[ len( path_flag ): ]
        new_flag = path_flag + os.path.join( working_directory, path )
        break

    if new_flag:
      new_flags.append( new_flag )
  return new_flags

def IsHeaderFile( filename ):
  extension = os.path.splitext( filename )[ 1 ]
  return extension in [ '.h', '.hxx', '.hpp', '.hh' ]

def GetCompilationInfoForFile( filename ):
  # The compilation_commands.json file generated by CMake does not have entries
  # for header files. So we do our best by asking the db for flags for a
  # corresponding source file, if any. If one exists, the flags for that file
  # should be good enough.
  if IsHeaderFile( filename ):
    basename = os.path.splitext( filename )[ 0 ]
    for extension in SOURCE_EXTENSIONS:
      replacement_file = basename + extension
      if os.path.exists( replacement_file ):
        compilation_info = database.GetCompilationInfoForFile(
          replacement_file )
        if compilation_info.compiler_flags_:
          return compilation_info
    return None
  return database.GetCompilationInfoForFile( filename )

def FlagsForFile( filename, **kwargs ):
  if database:
    # Bear in mind that compilation_info.compiler_flags_ does NOT return a
    # python list, but a "list-like" StringVec object
    compilation_info = GetCompilationInfoForFile( filename )
    if not compilation_info:
      return None

    final_flags = MakeRelativePathsInFlagsAbsolute(
      compilation_info.compiler_flags_,
      compilation_info.compiler_working_dir_ )

  else:
    relative_to = DirectoryOfThisScript()
    final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )

  return {
    'flags': final_flags,
    'do_cache': True
  }

<commit_msg>gpg test<commit_after><|endoftext|>