<commit_before># coding: utf-8
from __future__ import unicode_literals

from yargy.tokenizer import Token, Tokenizer
from yargy.labels import LABELS_LOOKUP_MAP

class Stack(list):

    '''
    Special list for grammar, which holds matches with rule index
    (by which token was captured)
    '''

    def have_matches_by_rule_index(self, rule_index):
        '''
        Checks that stack contains matches by rule index
        '''
        return any(
            (rule == rule_index for (rule, _) in reversed(self))
        )

    def flatten(self):
        '''
        Returns matched tokens without rule indexes
        '''
        return [value for (_, value) in self]

class Grammar(object):

    '''
    Grammar contains stack and list of rules.
    When GLR-parser iterates over tokens it calls
    `shift` & `reduce` methods on each grammar which checks
    current grammar stack & provided token on current rule
    when stack contents matches all rules - grammar returns stack,
    which contains actual text match
    '''

    def __init__(self, name, rules):
        self.name = name
        self.rules = rules + [
            {
                'terminal': True,
            },
        ]
        self.reset()

    def shift(self, token, recheck=False):
        '''
        Parser <- [grammar_1, grammar_2]
                   |
                   V
                shift(token_1)
        Grammar_1 -> []
        Grammar_2 -> [token_1]
                   |
                   V
                shift(token_2)
        Grammar_1 -> []
        Grammar_2 -> [token_1, token_2]
                   |
                   V
                reduce()
        Grammar_1 -> []
        Grammar_2 -> [] <-> returns stack [token_1, token_2]

        :return: list with matched tokens or None if (at current step) stack doesn't contains matches
        :rtype: list or None
        '''
        rule = self.rules[self.index]

        repeatable = rule.get('repeatable', False)
        optional = rule.get('optional', False)
        terminal = rule.get('terminal', False)
        skip = rule.get('skip', False)

        if not all(self.match(token, rule)) and not terminal:
            if optional or (repeatable and self.stack.have_matches_by_rule_index(self.index)):
                another_recheck = False
                self.index += 1
            else:
                another_recheck = True
                self.reset()
            if not recheck:
                self.shift(token, recheck=another_recheck) # recheck current token on next rule
        else:
            # token matches current rule
            if not terminal:
                # append token to stack if it's not a terminal rule and current rule
                # doesn't have 'skip' option
                if not skip:
                    self.stack.append((self.index, token))
                if not repeatable:
                    if len(self.rules) >= (self.index + 1):
                        self.index += 1

    def reduce(self, end_of_stream=False):
        '''
        Reduce method returns grammar stack if
        current grammar index equals to last (terminal) rule
        '''
        if not self.stack:
            return None

        current_rule = self.rules[self.index]
        terminal_rule = self.rules[-1]

        if current_rule == terminal_rule:
            match = self.stack.flatten()
            self.reset()
            return match

        if end_of_stream:
            is_repeatable_and_have_matches = (
                current_rule.get('repeatable', False)
                and
                self.stack.have_matches_by_rule_index(self.index)
            )
            is_optional = current_rule.get('optional', False)
            if is_repeatable_and_have_matches or is_optional:
                match = self.stack.flatten()
                self.reset()
                return match

    def reset(self):
        self.stack = Stack()
        self.index = 0

    def match(self, token, rule):
        labels = rule.get('labels', [])
        stack = self.stack.flatten()
        for (name, value) in labels:
            yield LABELS_LOOKUP_MAP[name](token, value, stack)

    def __repr__(self):
        return 'Grammar(name=\'{name}\', stack={stack})'.format(
            name=self.name,
            stack=self.stack,
        )

class Parser(object):

    '''
    Yet another GLR-parser.
    '''

    def __init__(self, grammars, tokenizer=None, pipelines=None, cache_size=0):
        self.grammars = grammars
        self.tokenizer = tokenizer or Tokenizer(cache_size=cache_size)
        self.pipelines = pipelines or []

    def extract(self, text):
        stream = self.tokenizer.transform(text)
        for pipeline in self.pipelines:
            stream = pipeline(stream)
        for token in stream:
            for grammar in self.grammars:
                grammar.shift(token)
                match = grammar.reduce()
                if match:
                    yield (grammar, match)
        for grammar in self.grammars:
            match = grammar.reduce(end_of_stream=True)
            if match:
                yield (grammar, match)

class Combinator(object):

    '''
    Combinator merges multiple grammars (in multiple enums) into one parser
    '''

    def __init__(self, classes, *args, **kwargs):
        self.classes = {}
        self.grammars = []
        for _class in classes:
            _class_name = _class.__name__
            for rule in _class.__members__.values():
                name = "{0}__{1}".format(_class_name, rule.name)
                self.classes[name] = rule
                self.grammars.append(Grammar(name, rule.value))
        self.parser = Parser(self.grammars, *args, **kwargs)

    def extract(self, text):
        for (rule, match) in self.parser.extract(text):
            yield self.classes[rule.name], match

    def resolve_matches(self, matches):
        matches = sorted(matches, key=lambda m: len(m[1]), reverse=True)
        index = {}
        for match in matches:
            tokens = match[-1]
            head, tail = tokens[0], tokens[-1]
            x, y = head.position[0], tail.position[1]
            length = 0 + (y - x)
            for (m_x, m_y, m_len) in index.keys():
                if (x >= m_x and y <= m_y):
                    if (m_len > length):
                        break
            else:
                index[(x, y, length)] = match
        return index.values()
<commit_msg>Additional checks for terminal rule in grammars reduce method<commit_after># coding: utf-8
from __future__ import unicode_literals

from yargy.tokenizer import Token, Tokenizer
from yargy.labels import LABELS_LOOKUP_MAP

class Stack(list):

    '''
    Special list for grammar, which holds matches with rule index
    (by which token was captured)
    '''

    def have_matches_by_rule_index(self, rule_index):
        '''
        Checks that stack contains matches by rule index
        '''
        return any(
            (rule == rule_index for (rule, _) in reversed(self))
        )

    def flatten(self):
        '''
        Returns matched tokens without rule indexes
        '''
        return [value for (_, value) in self]

class Grammar(object):

    '''
    Grammar contains stack and list of rules.
    When GLR-parser iterates over tokens it calls
    `shift` & `reduce` methods on each grammar which checks
    current grammar stack & provided token on current rule
    when stack contents matches all rules - grammar returns stack,
    which contains actual text match
    '''

    def __init__(self, name, rules):
        self.name = name
        self.rules = rules + [
            {
                'terminal': True,
            },
        ]
        self.reset()

    def shift(self, token, recheck=False):
        '''
        Parser <- [grammar_1, grammar_2]
                   |
                   V
                shift(token_1)
        Grammar_1 -> []
        Grammar_2 -> [token_1]
                   |
                   V
                shift(token_2)
        Grammar_1 -> []
        Grammar_2 -> [token_1, token_2]
                   |
                   V
                reduce()
        Grammar_1 -> []
        Grammar_2 -> [] <-> returns stack [token_1, token_2]

        :return: list with matched tokens or None if (at current step) stack doesn't contains matches
        :rtype: list or None
        '''
        rule = self.rules[self.index]

        repeatable = rule.get('repeatable', False)
        optional = rule.get('optional', False)
        terminal = rule.get('terminal', False)
        skip = rule.get('skip', False)

        if not all(self.match(token, rule)) and not terminal:
            if optional or (repeatable and self.stack.have_matches_by_rule_index(self.index)):
                self.index += 1
            else:
                self.reset()
            if not recheck:
                self.shift(token, recheck=True) # recheck current token on next rule
        else:
            # token matches current rule
            if not terminal:
                # append token to stack if it's not a terminal rule and current rule
                # doesn't have 'skip' option
                if not skip:
                    self.stack.append((self.index, token))
                if not repeatable:
                    if len(self.rules) >= (self.index + 1):
                        self.index += 1

    def reduce(self, end_of_stream=False):
        '''
        Reduce method returns grammar stack if
        current grammar index equals to last (terminal) rule
        '''
        if not self.stack:
            return None

        current_rule = self.rules[self.index]
        terminal_rule = self.rules[-1]

        if current_rule == terminal_rule:
            match = self.stack.flatten()
            self.reset()
            return match

        if end_of_stream:
            is_repeatable_and_have_matches = (
                current_rule.get('repeatable', False)
                and
                self.stack.have_matches_by_rule_index(self.index)
            )
            is_optional = current_rule.get('optional', False)
            next_rule_is_terminal = (self.rules[self.index + 1] == terminal_rule)
            if (is_repeatable_and_have_matches or is_optional) and next_rule_is_terminal:
                match = self.stack.flatten()
                self.reset()
                return match

    def reset(self):
        self.stack = Stack()
        self.index = 0

    def match(self, token, rule):
        labels = rule.get('labels', [])
        stack = self.stack.flatten()
        for (name, value) in labels:
            yield LABELS_LOOKUP_MAP[name](token, value, stack)

    def __repr__(self):
        return 'Grammar(name=\'{name}\', stack={stack})'.format(
            name=self.name,
            stack=self.stack,
        )

class Parser(object):

    '''
    Yet another GLR-parser.
    '''

    def __init__(self, grammars, tokenizer=None, pipelines=None, cache_size=0):
        self.grammars = grammars
        self.tokenizer = tokenizer or Tokenizer(cache_size=cache_size)
        self.pipelines = pipelines or []

    def extract(self, text):
        stream = self.tokenizer.transform(text)
        for pipeline in self.pipelines:
            stream = pipeline(stream)
        for token in stream:
            for grammar in self.grammars:
                grammar.shift(token)
                match = grammar.reduce()
                if match:
                    yield (grammar, match)
        for grammar in self.grammars:
            match = grammar.reduce(end_of_stream=True)
            if match:
                yield (grammar, match)

class Combinator(object):

    '''
    Combinator merges multiple grammars (in multiple enums) into one parser
    '''

    def __init__(self, classes, *args, **kwargs):
        self.classes = {}
        self.grammars = []
        for _class in classes:
            _class_name = _class.__name__
            for rule in _class.__members__.values():
                name = "{0}__{1}".format(_class_name, rule.name)
                self.classes[name] = rule
                self.grammars.append(Grammar(name, rule.value))
        self.parser = Parser(self.grammars, *args, **kwargs)

    def extract(self, text):
        for (rule, match) in self.parser.extract(text):
            yield self.classes[rule.name], match

    def resolve_matches(self, matches):
        matches = sorted(matches, key=lambda m: len(m[1]), reverse=True)
        index = {}
        for match in matches:
            tokens = match[-1]
            head, tail = tokens[0], tokens[-1]
            x, y = head.position[0], tail.position[1]
            length = 0 + (y - x)
            for (m_x, m_y, m_len) in index.keys():
                if (x >= m_x and y <= m_y):
                    if (m_len > length):
                        break
            else:
                index[(x, y, length)] = match
        return index.values()
<|endoftext|>


<commit_before>

# pylint: disable-msg=C0301,W0613,W0612

'''

yolklib.py
==========

Desc: Library for getting information about Python packages installed by
      setuptools, package metadata, package dependencies, and querying
      The CheeseShop (PYPI) for Python package release information.

Author: Rob Cakebread <gentoodev a t gmail.com>

License  : PSF (Python Software Foundation License)

'''

__docformat__ = 'restructuredtext'

import pkg_resources

class Distributions:

    """Helper class for pkg_resources"""

    def __init__(self):
        """init"""

        self.environment = pkg_resources.Environment()
        self.working_set = pkg_resources.WorkingSet()

    def query_activated(self, dist):
        """Return True if distribution is active"""
        if dist in self.working_set:
            return True

    def get_distributions(self, show, pkg_name="", version=""):
        """List installed packages"""
        for name, dist in self.get_alpha(show, pkg_name, version):
            ver = dist.version
            for p in self.environment[dist.project_name]:
                if ver == p.version:
                    if show == "nonactive" and dist not in self.working_set:
                        yield (dist, self.query_activated(dist))
                    elif show == "active" and dist in self.working_set:
                        yield (dist, self.query_activated(dist))
                    elif show == "all":
                        yield (dist, self.query_activated(dist))

    def get_alpha(self, show, pkg_name="", version=""):
        """Return list of alphabetized packages"""
        alpha_list = []
        for dist in self.get_packages(show):
            if pkg_name and dist.project_name != pkg_name:
                #Only checking for a single package name
                pass
            elif version and dist.version != version:
                #Only checking for a single version of a package
                pass
            else:
                alpha_list.append((dist.project_name + dist.version, dist))
        alpha_list.sort()
        return alpha_list

    def get_packages(self, show):
        """Return list of Distributions"""

        if show == 'nonactive' or show == "all":
            all_packages = []
            for package in self.environment:
                #There may be multiple versions of same packages
                for i in range(len(self.environment[package])):
                    if self.environment[package][i]:
                        all_packages.append(self.environment[package][i])
            return all_packages
        else:
            # Only activated packages
            return self.working_set

    def get_highest_installed(self, project_name):
        return self.environment[project_name][0].version

    def case_sensitive_name(self, package_name):
        """Return case-sensitive package name given any-case package name"""
        if len(self.environment[package_name]):
            return self.environment[package_name][0].project_name
        else:
            raise "No installed package found: %s" % package_name

def get_highest_version(versions):
    """Given list of versions returns highest available version for a package"""
    #Used to sort versions returned from PyPI
    sorted_versions = []
    for ver in versions:
        sorted_versions.append((pkg_resources.parse_version(ver), ver))

    sorted_versions.reverse()
    return sorted_versions[0][1]

<commit_msg>Use pkg_resource.require instead of Enviroent directly<commit_after>

# pylint: disable-msg=C0301,W0613,W0612

'''

yolklib.py
==========

Desc: Library for getting information about Python packages installed by
      setuptools, package metadata, package dependencies, and querying
      The CheeseShop (PYPI) for Python package release information.

Author: Rob Cakebread <gentoodev a t gmail.com>

License  : PSF (Python Software Foundation License)

'''

__docformat__ = 'restructuredtext'

import pkg_resources

class Distributions:

    """Helper class for pkg_resources"""

    def __init__(self):
        """init"""

        self.environment = pkg_resources.Environment()
        self.working_set = pkg_resources.WorkingSet()

    def query_activated(self, dist):
        """Return True if distribution is active"""
        if dist in self.working_set:
            return True

    def get_distributions(self, show, pkg_name="", version=""):
        """List installed packages"""
        for name, dist in self.get_alpha(show, pkg_name, version):
            ver = dist.version
            for p in self.environment[dist.project_name]:
                if ver == p.version:
                    if show == "nonactive" and dist not in self.working_set:
                        yield (dist, self.query_activated(dist))
                    elif show == "active" and dist in self.working_set:
                        yield (dist, self.query_activated(dist))
                    elif show == "all":
                        yield (dist, self.query_activated(dist))

    def get_alpha(self, show, pkg_name="", version=""):
        """Return list of alphabetized packages"""
        alpha_list = []
        for dist in self.get_packages(show):
            if pkg_name and dist.project_name != pkg_name:
                #Only checking for a single package name
                pass
            elif version and dist.version != version:
                #Only checking for a single version of a package
                pass
            else:
                alpha_list.append((dist.project_name + dist.version, dist))
        alpha_list.sort()
        return alpha_list

    def get_packages(self, show):
        """Return list of Distributions"""

        if show == 'nonactive' or show == "all":
            all_packages = []
            for package in self.environment:
                #There may be multiple versions of same packages
                for i in range(len(self.environment[package])):
                    if self.environment[package][i]:
                        all_packages.append(self.environment[package][i])
            return all_packages
        else:
            # Only activated packages
            return self.working_set

    def get_highest_installed(self, project_name):
        """Return highest version of installed package"""
        #XXX These yield same results. Test to see which is more
        #efficient.
        #return self.environment[project_name][0].version
        return pkg_resources.require(project_name)[0].version

    def case_sensitive_name(self, package_name):
        """Return case-sensitive package name given any-case package name"""
        if len(self.environment[package_name]):
            return self.environment[package_name][0].project_name
        else:
            raise "No installed package found: %s" % package_name

def get_highest_version(versions):
    """Given list of versions returns highest available version for a package"""
    #Used to sort versions returned from PyPI
    sorted_versions = []
    for ver in versions:
        sorted_versions.append((pkg_resources.parse_version(ver), ver))

    sorted_versions.reverse()
    return sorted_versions[0][1]

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import division

from ethereum.utils import encode_hex

from raiden.utils import keccak
from raiden.exceptions import HashLengthNot32

def hash_pair(first, second):
    if second is None:
        return first
    if first is None:
        return second
    if first > second:
        return keccak(second + first)
    return keccak(first + second)

def iterate_pairwise(elements):
    """ iterate pairwise over the given list, i.e. the functions yields
    a list of consecutive 2-tuples. The second element in the last
    tuple yielded is None iff the len of the given list is odd """
    for i in range(len(elements) // 2):
        yield elements[i * 2], elements[i * 2 + 1]
    if len(elements) % 2:
        yield elements[-1], None

def merkletreelayers(elements):
    """ computes the layers of the merkletree. First layer is the list
    of elements and the last layer is a list with a single entry, the
    merkleroot """

    yield elements
    if len(elements) == 0:
        yield [""]
    while len(elements) > 1:
        elements = [hash_pair(a, b) for a, b in iterate_pairwise(elements)]
        yield elements

def merkleproof_from_layers(layers, idx):
    proof = []
    for layer in layers:
        pair_idx = idx - 1 if idx % 2 else idx + 1
        if pair_idx < len(layer):
            proof.append(layer[pair_idx])
        idx = idx // 2
    return proof

class Merkletree(object):
    def __init__(self, elements):
        self._layers = list(merkletreelayers(build_list(elements)))

    @property
    def merkleroot(self):
        return self._layers[-1][0]

    def make_proof(self, element):
        """ The proof contains all elements between `element` and `root`.
            If on all of [element] + proof is recursively hash_pair applied one
            gets the root.
        """
        return merkleproof_from_layers(self._layers, self._layers[0].index(element))

def merkleroot(elements):
    """
    Args:
        elements (List[str]): List of hashes that make the merkletree.

    Returns:
        str: The root element of the merkle tree.
    Raises:
        HashLengthNot32: The length of one of the elements is not 32
    """
    return Merkletree(elements).merkleroot

def build_list(elements):
    result = list()

    for item in set(elements):
        if item:
            if len(item) != 32:
                raise HashLengthNot32()

            result.append(item)

    result.sort()
    return result

def check_proof(proof, root, hash_):
    for x in proof:
        hash_ = hash_pair(hash_, x)

    return hash_ == root

def get_proof(lst, proof_for, root=None):
    tree = Merkletree(lst)

    root_hash = tree.merkleroot
    if root and root != root_hash:
        raise ValueError('root hashes did not match {} {}'.format(
            encode_hex(root_hash),
            encode_hex(root)
        ))

    return tree.make_proof(proof_for)
<commit_msg>moved build_list logic to MerkleTree constructor<commit_after># -*- coding: utf-8 -*-
from __future__ import division

from ethereum.utils import encode_hex

from raiden.utils import keccak
from raiden.exceptions import HashLengthNot32

def hash_pair(first, second):
    if second is None:
        return first
    if first is None:
        return second
    if first > second:
        return keccak(second + first)
    return keccak(first + second)

def iterate_pairwise(elements):
    """ iterate pairwise over the given list, i.e. the functions yields
    a list of consecutive 2-tuples. The second element in the last
    tuple yielded is None iff the len of the given list is odd """
    for i in range(len(elements) // 2):
        yield elements[i * 2], elements[i * 2 + 1]
    if len(elements) % 2:
        yield elements[-1], None

def merkletreelayers(elements):
    """ computes the layers of the merkletree. First layer is the list
    of elements and the last layer is a list with a single entry, the
    merkleroot """

    yield elements
    if len(elements) == 0:
        yield [""]
    while len(elements) > 1:
        elements = [hash_pair(a, b) for a, b in iterate_pairwise(elements)]
        yield elements

def merkleproof_from_layers(layers, idx):
    proof = []
    for layer in layers:
        pair_idx = idx - 1 if idx % 2 else idx + 1
        if pair_idx < len(layer):
            proof.append(layer[pair_idx])
        idx = idx // 2
    return proof

def merkleroot(elements):
    """
    Args:
        elements (List[str]): List of hashes that make the merkletree.

    Returns:
        str: The root element of the merkle tree.
    Raises:
        HashLengthNot32: The length of one of the elements is not 32
    """
    return Merkletree(elements).merkleroot

def check_proof(proof, root, hash_):
    for x in proof:
        hash_ = hash_pair(hash_, x)

    return hash_ == root

def get_proof(lst, proof_for, root=None):
    tree = Merkletree(lst)

    root_hash = tree.merkleroot
    if root and root != root_hash:
        raise ValueError('root hashes did not match {} {}'.format(
            encode_hex(root_hash),
            encode_hex(root)
        ))

    return tree.make_proof(proof_for)

class Merkletree(object):
    def __init__(self, elements):
        if not all(isinstance(item, (str, bytes)) for item in elements):
            raise ValueError('all elements must be str')

        if any(len(item) != 32 for item in elements):
            raise HashLengthNot32()

        if len(elements) != len(set(elements)):
            raise ValueError('Duplicated element')

        leafs = sorted(item for item in elements)
        self._layers = list(merkletreelayers(leafs))

    @property
    def merkleroot(self):
        return self._layers[-1][0]

    def make_proof(self, element):
        """ The proof contains all elements between `element` and `root`.
            If on all of [element] + proof is recursively hash_pair applied one
            gets the root.
        """
        return merkleproof_from_layers(self._layers, self._layers[0].index(element))
<|endoftext|>


<commit_before>#!/usr/bin/env python
import osr
import gdal
import numpy as np
import sys

idx_min_x = idx_max_x = idx_min_y = idx_max_y = 0
spa_x = spa_y = 0
lat_0 = lat_1 = lon_0 = 0
lat_c = lon_c = 0
raster_data = None

def get_parameters(line):
    global idx_min_x, idx_max_x, idx_min_y, idx_max_y, spa_x, spa_y, lat_0, lat_1, lon_0, lat_c, lon_c
    splitted = line.split(' ')

    i = 0
    while splitted[i] != 'Indexs=':
        i += 1

    idx_min_x = int(splitted[i + 1])
    idx_max_x = int(splitted[i + 2])
    idx_min_y = int(splitted[i + 3])
    idx_max_y = int(splitted[i + 4])

    i = 0
    while splitted[i] != 'Proj=':
        i += 1

    if splitted[i + 1] != 'lambert':
        print "Error - no lambert projection found..."
        return

    spa_x = float(splitted[i + 2])
    spa_y = float(splitted[i + 3])
    lat_0 = float(splitted[i + 4])
    lat_1 = float(splitted[i + 5])
    lon_0 = float(splitted[i + 6])
    lat_c = float(splitted[i + 7])
    lon_c = float(splitted[i + 8])

def read_data(line, idx):
    splitted = line.split(' ')

    if len(splitted) != idx_max_x - idx_min_x + 1:
        print "Error - grid resolution wrong?!?"
        return

    for i in range(len(splitted)):
        raster_data[(idx_max_y - idx_min_y) - idx - 1, i] = float(splitted[i])
        #raster_data[idx, i] = float(splitted[i])

i = 0
for line in open(sys.argv[1]):
    i += 1
    if line == '---':
        continue

    if line.startswith('Model='):
        get_parameters(line)
        raster_data = np.zeros((idx_max_x - idx_min_x + 1, idx_max_y - idx_min_y + 1), dtype=np.float32)

    if i >= 5:
        read_data(line, i - 5)

lcc = osr.SpatialReference()
lcc.ImportFromProj4("+proj=lcc +lat_1=" + str(lat_1) + " +lat_0=" + str(lat_0) + " +lon_0=" + str(lon_0) + " +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs")

epsg4326 = osr.SpatialReference()
epsg4326.ImportFromEPSG(4326)

epsg4326_to_lcc = osr.CoordinateTransformation(epsg4326, lcc)

width = (idx_max_x - idx_min_x) + 1
height = (idx_max_y - idx_min_y) + 1

center_lcc = epsg4326_to_lcc.TransformPoint(lon_c, lat_c)
geotransform = [center_lcc[0] - width * spa_x / 2, spa_x, 0, center_lcc[1] + height * spa_y / 2, 0, -spa_y]

driver = gdal.GetDriverByName('GTiff')
dst_ds = driver.Create(sys.argv[1] + ".tiff", width, height, 1, gdal.GDT_Float32)
dst_ds.SetProjection(lcc.ExportToWkt())
dst_ds.SetGeoTransform(geotransform)
dst_ds.GetRasterBand(1).WriteArray(raster_data)
dst_ds = None

<commit_msg>Fix 2d raster size<commit_after>#!/usr/bin/env python
import osr
import gdal
import numpy as np
import sys

idx_min_x = idx_max_x = idx_min_y = idx_max_y = 0
spa_x = spa_y = 0
lat_0 = lat_1 = lon_0 = 0
lat_c = lon_c = 0
raster_data = None

def get_parameters(line):
    global idx_min_x, idx_max_x, idx_min_y, idx_max_y, spa_x, spa_y, lat_0, lat_1, lon_0, lat_c, lon_c
    splitted = line.split(' ')

    i = 0
    while splitted[i] != 'Indexs=':
        i += 1

    idx_min_x = int(splitted[i + 1])
    idx_max_x = int(splitted[i + 2])
    idx_min_y = int(splitted[i + 3])
    idx_max_y = int(splitted[i + 4])

    i = 0
    while splitted[i] != 'Proj=':
        i += 1

    if splitted[i + 1] != 'lambert':
        print "Error - no lambert projection found..."
        return

    spa_x = float(splitted[i + 2])
    spa_y = float(splitted[i + 3])
    lat_0 = float(splitted[i + 4])
    lat_1 = float(splitted[i + 5])
    lon_0 = float(splitted[i + 6])
    lat_c = float(splitted[i + 7])
    lon_c = float(splitted[i + 8])

def read_data(line, idx):
    splitted = line.split(' ')

    if len(splitted) != idx_max_x - idx_min_x + 1:
        print "Error - grid resolution wrong?!?"
        return

    for i in range(len(splitted)):
        raster_data[(idx_max_y - idx_min_y) - idx - 1, i] = float(splitted[i])
        #raster_data[idx, i] = float(splitted[i])

i = 0
for line in open(sys.argv[1]):
    i += 1
    if line == '---':
        continue

    if line.startswith('Model='):
        get_parameters(line)
        raster_data = np.zeros((idx_max_y - idx_min_y + 1, idx_max_x - idx_min_x + 1), dtype=np.float32)

    if i >= 5:
        read_data(line, i - 5)

lcc = osr.SpatialReference()
lcc.ImportFromProj4("+proj=lcc +lat_1=" + str(lat_1) + " +lat_0=" + str(lat_0) + " +lon_0=" + str(lon_0) + " +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs")

epsg4326 = osr.SpatialReference()
epsg4326.ImportFromEPSG(4326)

epsg4326_to_lcc = osr.CoordinateTransformation(epsg4326, lcc)

width = (idx_max_x - idx_min_x) + 1
height = (idx_max_y - idx_min_y) + 1

center_lcc = epsg4326_to_lcc.TransformPoint(lon_c, lat_c)
geotransform = [center_lcc[0] - width * spa_x / 2, spa_x, 0, center_lcc[1] + height * spa_y / 2, 0, -spa_y]

driver = gdal.GetDriverByName('GTiff')
dst_ds = driver.Create(sys.argv[1] + ".tiff", width, height, 1, gdal.GDT_Float32)
dst_ds.SetProjection(lcc.ExportToWkt())
dst_ds.SetGeoTransform(geotransform)
dst_ds.GetRasterBand(1).WriteArray(raster_data)
dst_ds = None

<|endoftext|>


<commit_before>import click
import os
import sys

from subprocess import list2cmdline
from honcho.manager import Manager

from .base import cli

@cli.command()
@click.option('--host', default='0.0.0.0')
@click.option('--port', default=8080, type=int)
@click.option('--processes', default=1, type=int)
@click.option('--threads', default=4, type=int)
def web(host, port, processes, threads):
    os.environ['PYTHONUNBUFFERED'] = 'true'

    daemons = [
        (
            'web', [
                'uwsgi',
                '--master',
                '--enable-threads',
                '--lazy-apps',
                '--single-interpreter',
                '--http={}:{}'.format(host, port),
                '--processes={}'.format(processes),
                '--threads={}'.format(threads),
                '--log-x-forwarded-for',
                '--buffer-size=32768',
                '--post-buffering=65536',
                '--need-app',
                '--disable-logging',
                '--thunder-lock',
                '--vacuum',
                '--home={}'.format(sys.prefix),
                '--auto-procname',
                '--procname-prefix-spaced=[zeus]',
                '--module=zeus.app:app',
                '--die-on-term',
            ]
        ),
    ]

    cwd = os.path.realpath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))

    manager = Manager()
    for name, cmd in daemons:
        manager.add_process(
            name,
            list2cmdline(cmd),
            quiet=False,
            cwd=cwd,
            # env=os.environ,
        )

    manager.loop()
    sys.exit(manager.returncode)
<commit_msg>build: Enable chunked input management for uwsgi (#67)<commit_after>import click
import os
import sys

from subprocess import list2cmdline
from honcho.manager import Manager

from .base import cli

@cli.command()
@click.option('--host', default='0.0.0.0')
@click.option('--port', default=8080, type=int)
@click.option('--processes', default=1, type=int)
@click.option('--threads', default=4, type=int)
def web(host, port, processes, threads):
    os.environ['PYTHONUNBUFFERED'] = 'true'

    daemons = [
        (
            'web', [
                'uwsgi',
                '--master',
                '--enable-threads',
                '--lazy-apps',
                '--single-interpreter',
                '--http={}:{}'.format(host, port),
                '--processes={}'.format(processes),
                '--threads={}'.format(threads),
                '--log-x-forwarded-for',
                '--buffer-size=32768',
                '--post-buffering=65536',
                '--need-app',
                '--disable-logging',
                '--thunder-lock',
                '--vacuum',
                '--home={}'.format(sys.prefix),
                '--auto-procname',
                '--procname-prefix-spaced=[zeus]',
                '--module=zeus.app:app',
                '--die-on-term',
                '--wsgi-manage-chunked-input',
            ]
        ),
    ]

    cwd = os.path.realpath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))

    manager = Manager()
    for name, cmd in daemons:
        manager.add_process(
            name,
            list2cmdline(cmd),
            quiet=False,
            cwd=cwd,
            # env=os.environ,
        )

    manager.loop()
    sys.exit(manager.returncode)
<|endoftext|>


<commit_before>__version__ = "0.9.3"
<commit_msg>0.9.4<commit_after>__version__ = "0.9.4"
<|endoftext|>


<commit_before>__version__ = "0.10.26"
<commit_msg>0.10.27<commit_after>__version__ = "0.10.27"
<|endoftext|>


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

import os
import errno

from pybuilder.core import init, task, use_plugin, description, after, before
from pybuilder.utils import assert_can_execute, execute_command

use_plugin("core")

@init
def init_ronn_manpage_plugin(project):
    project.set_property_if_unset("dir_manpages", "docs/man")
    project.set_property_if_unset("manpage_source", "README.md")
    project.set_property_if_unset("manpage_section", 1)

@after("prepare")
def assert_ronn_is_executable(logger):
    """
        Asserts that the ronn script is executable.
    """
    logger.debug("Checking if ronn is executable.")

    assert_can_execute(command_and_arguments=["ronn", "--version"],
                       prerequisite="ronn",
                       caller="plugin ronn_manpage_plugin")

@after("prepare")
def assert_gzip_is_executable(logger):
    """
        Asserts that the gzip program is executable.
    """
    logger.debug("Checking if gzip is executable.")

    assert_can_execute(command_and_arguments=["gzip", "--version"],
                       prerequisite="gzip",
                       caller="plugin ronn_manpage_plugin")

@task
@before(("package", "publish"))
@description("Generates manpages using ronn.")
def generate_manpages(project, logger):
    """
        Uses the ronn script to convert a markdown source to a gzipped manpage.
    """
    logger.info('Generating manpages')
    try:
        os.makedirs(project.get_property('dir_manpages'))
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise
    ronn_report_file = project.expand_path("$dir_reports/{0}".format('generate_manpage'))
    generate_manpages_command = build_generate_manpages_command(project)
    execute_command(generate_manpages_command, ronn_report_file, shell=True)

def build_generate_manpages_command(project):
    ronn_pipe_command = 'ronn -r --pipe %s' % project.get_property('manpage_source')
    compressed_manpage_file = '%s.%d.gz' % (project.name, project.get_property('manpage_section'))
    compress_command = 'gzip -9 > %s' % os.path.join(project.get_property('dir_manpages'), compressed_manpage_file)
    generate_manpages_command = '%s | %s' % (ronn_pipe_command, compress_command)
    return generate_manpages_command
<commit_msg>ensuring that generate_manpages runs after prepare, relaxing other constraints<commit_after>#   This file is part of PyBuilder
#
#   Copyright 2011-2013 PyBuilder Team
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

import os
import errno

from pybuilder.core import init, task, use_plugin, description, depends, after
from pybuilder.utils import assert_can_execute, execute_command

use_plugin("core")

@init
def init_ronn_manpage_plugin(project):
    project.set_property_if_unset("dir_manpages", "docs/man")
    project.set_property_if_unset("manpage_source", "README.md")
    project.set_property_if_unset("manpage_section", 1)

@after("prepare")
def assert_ronn_is_executable(logger):
    """
        Asserts that the ronn script is executable.
    """
    logger.debug("Checking if ronn is executable.")

    assert_can_execute(command_and_arguments=["ronn", "--version"],
                       prerequisite="ronn",
                       caller="plugin ronn_manpage_plugin")

@after("prepare")
def assert_gzip_is_executable(logger):
    """
        Asserts that the gzip program is executable.
    """
    logger.debug("Checking if gzip is executable.")

    assert_can_execute(command_and_arguments=["gzip", "--version"],
                       prerequisite="gzip",
                       caller="plugin ronn_manpage_plugin")

@task
@depends("prepare")
@description("Generates manpages using ronn.")
def generate_manpages(project, logger):
    """
        Uses the ronn script to convert a markdown source to a gzipped manpage.
    """
    logger.info('Generating manpages')
    try:
        os.makedirs(project.get_property('dir_manpages'))
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise
    ronn_report_file = project.expand_path("$dir_reports/{0}".format('generate_manpage'))
    generate_manpages_command = build_generate_manpages_command(project)
    execute_command(generate_manpages_command, ronn_report_file, shell=True)

def build_generate_manpages_command(project):
    ronn_pipe_command = 'ronn -r --pipe %s' % project.get_property('manpage_source')
    compressed_manpage_file = '%s.%d.gz' % (project.name, project.get_property('manpage_section'))
    compress_command = 'gzip -9 > %s' % os.path.join(project.get_property('dir_manpages'), compressed_manpage_file)
    generate_manpages_command = '%s | %s' % (ronn_pipe_command, compress_command)
    return generate_manpages_command
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
#
#    Authors: Laetitia Gangloff
#    Copyright (c) 2014 Acsone SA/NV (http://www.acsone.eu)
#    All Rights Reserved
#
#    WARNING: This program as such is intended to be used by professional
#    programmers who take the whole responsibility of assessing all potential
#    consequences resulting from its eventual inadequacies and bugs.
#    End users who are looking for a ready-to-use solution with commercial
#    guarantees and support are strongly advised to contact a Free Software
#    Service Company.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#

from openerp.osv import fields, orm

class res_partner(orm.Model):
    _inherit = 'res.partner'

    _columns = {
        'property_account_supplier_accrual': fields.property(
            'account.account',
            type='many2one',
            relation='account.account',
            string="Accrual Account Supplier",
            view_load=True,
            domain="[('type', '=', 'payable')]",
            help="This account will be used instead of the default one as \
                the supplier accrual account for the current partner",
            required=True),
        'property_account_customer_accrual': fields.property(
            'account.account',
            type='many2one',
            relation='account.account',
            string="Accrual Account Customer",
            view_load=True,
            domain="[('type', '=', 'receivable')]",
            help="This account will be used instead of the default one as \
                the customer accrual account for the current partner",
            required=True),
        'property_journal_accrual': fields.property(
            'account.journal',
            type='many2one',
            relation='account.journal',
            string="Accrual Journal",
            view_load=True,
            domain="[('type', '=', 'general')]",
            help="This journal will be used as the default one for accrual \
                operation for the current partner",
            required=True),
    }

    def _commercial_fields(self, cr, uid, context=None):
        return super(res_partner, self)._commercial_fields(
            cr, uid, context=context) + ['property_account_supplier_accrual',
                                         'property_account_customer_accrual',
                                         'property_journal_accrual']
<commit_msg>[FIX][account_invoice_accrual] Crash without model=model_name<commit_after># -*- coding: utf-8 -*-
#
#
#    Authors: Laetitia Gangloff
#    Copyright (c) 2014 Acsone SA/NV (http://www.acsone.eu)
#    All Rights Reserved
#
#    WARNING: This program as such is intended to be used by professional
#    programmers who take the whole responsibility of assessing all potential
#    consequences resulting from its eventual inadequacies and bugs.
#    End users who are looking for a ready-to-use solution with commercial
#    guarantees and support are strongly advised to contact a Free Software
#    Service Company.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#

from openerp.osv import fields, orm

class res_partner(orm.Model):
    _inherit = 'res.partner'

    _columns = {
        'property_account_supplier_accrual': fields.property(
            model='account.account',
            type='many2one',
            relation='account.account',
            string="Accrual Account Supplier",
            view_load=True,
            domain="[('type', '=', 'payable')]",
            help="This account will be used instead of the default one as \
                the supplier accrual account for the current partner",
            required=True),
        'property_account_customer_accrual': fields.property(
            model='account.account',
            type='many2one',
            relation='account.account',
            string="Accrual Account Customer",
            view_load=True,
            domain="[('type', '=', 'receivable')]",
            help="This account will be used instead of the default one as \
                the customer accrual account for the current partner",
            required=True),
        'property_journal_accrual': fields.property(
            model='account.journal',
            type='many2one',
            relation='account.journal',
            string="Accrual Journal",
            view_load=True,
            domain="[('type', '=', 'general')]",
            help="This journal will be used as the default one for accrual \
                operation for the current partner",
            required=True),
    }

    def _commercial_fields(self, cr, uid, context=None):
        return super(res_partner, self)._commercial_fields(
            cr, uid, context=context) + ['property_account_supplier_accrual',
                                         'property_account_customer_accrual',
                                         'property_journal_accrual']
<|endoftext|>


<commit_before>from __future__ import print_function, division, absolute_import #, unicode_literals # not casa compatible
from builtins import bytes, dict, object, range, map, input#, str # not casa compatible
from future.utils import itervalues, viewitems, iteritems, listvalues, listitems
from io import open

import click
import shutil
import subprocess
import os
import glob
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logging.captureWarnings(True)
logger = logging.getLogger(__name__)
logger.setLevel(20)

vys_cfile = '/home/cbe-master/realfast/soft/vysmaw_apps/vys.conf'
default_preffile = '/lustre/evla/test/realfast/realfast.yml'

@click.group('realfast')
def cli():
    pass

@cli.command()
@click.option('--mode', default='line')
@click.option('--line', default=None)
@click.option('--filepath', default='/home/cbe-master/realfast/soft/logs/')
@click.option('--globstr', default='rf*log')
@click.option('--age', default=3600, type=int)
def grep(mode, line, filepath, globstr, age):
    """ Find line in files in path
    Also has modes 'badclose' (and more?), where line is set
    for user.
    """

    import os.path
    import glob
    import time
    now = time.time()

    if mode == 'line':
        assert line is not None
    elif mode == 'badclose':
        line = '6: 0}'

    files0 = list(glob.glob(os.path.join(filepath, globstr)))
    files1 = list(filter(lambda x: now-os.path.getmtime(x) < age, files0))
    print('Running grep on {1} files ({0} match globstr)\n'.format(len(files1), len(files0)))
    files = ' '.join(files1)
    callstr = 'grep "{0}" {1}'.format(line, files)
    if len(files1):
#        print(callstr)
        subprocess.call(callstr, shell=True)
    else:
        print('No files found')

@cli.command()
@click.option('--channel', default='#alerts')
@click.option('--message')
def slack(channel, message):
    """ Update slack with something.
    """

    from realfast import util
    util.update_slack(channel, message)

@cli.command()
@click.option('--preffile', default='realfast.yml')
@click.option('--inprefs', default={})
def config_catcher(preffile, inprefs):
    """ Runs async process to catch multicast messages to form scan config objects
    Can be saved to pklfile and optionally attached to preferences from preffile.
    """

    from realfast import controllers

    logger.info("Running config catcher with preffile={0}"
                .format(preffile))
    config = controllers.config_controller(preffile=preffile, inprefs=inprefs)
    config.run()

@cli.command()
@click.option('--mode', default='deployment')
@click.option('--preffile', default=default_preffile)
@click.option('--threshold', type=float, default=None)
@click.option('--rsync_with_fetch', is_flag=True)
@click.option('--rsync_with_reader', is_flag=True)
@click.option('--excludeants', default=None)
def run(mode, preffile, threshold, rsync_with_fetch, rsync_with_reader, excludeants):
    """ Run realfast controller to catch scan configs and start rfpipe.
    mode can be "deployment" or "development", which defines scheduler IP.
    preffile can be realfast.yml or another yml config file.
    """

    from realfast import controllers

    if mode == 'deployment':
        host = '10.80.200.201:8786'
    elif mode == 'development':
        host = '10.80.200.201:8796'
    else:
        logger.warn("mode not recognized (deployment or development allowed)")
        return 1

    # overload some preferences
    inprefs = {}
    if threshold is not None:
        inprefs['sigma_image1'] = threshold
    if excludeants is not None:
        inprefs['excludeants'] = excludeants

    try:
        rfc = controllers.realfast_controller(host=host, preffile=preffile, inprefs=inprefs,
                                              rsync_with_reader=rsync_with_reader, rsync_with_fetch=rsync_with_fetch)
        rfc.initialize()
        rfc.run()
    except KeyboardInterrupt:
        logger.warn("Cleaning up before stopping processing.")
        rfc.cleanup_loop()
    except OSError:
        logger.warn("Could not start controller. Are scheduler and workers running?")

@cli.command()
@click.option('--sdmname', default=None)
@click.option('--candid', default=None)
@click.option('--indexprefix', default='new')
@click.option('--copybdf', is_flag=True)
def buildsdm(sdmname, candid, indexprefix, copybdf):
    """ Assemble sdm/bdf from cbe lustre.
    Can find it from sdmname or can look up by candid.
    """

    if sdmname is None:
        from realfast import elastic
        if candid is None:
            logger.exception("Need to provide canid or sdmname")
        doc = elastic.get_doc(indexprefix + 'cands', candid)
        assert 'sdmname' in doc['_source'], 'No sdmname associated with that candid'
        sdmname = doc['_source']['sdmname'].split('/')[-1]
        logger.info("Got sdmname {0} from {1}cands index".format(sdmname, indexprefix))

    sdmloc = '/home/mctest/evla/mcaf/workspace/'
    sdmname_full = os.path.join(sdmloc, sdmname)
    if os.path.exists(sdmname_full):
        shutil.copytree(sdmname_full, os.path.join('.', sdmname), ignore_dangling_symlinks=True, symlinks=True)
    else:
        logger.info("Trying realfast temp archive...")
        sdmloc = '/lustre/evla/test/realfast/archive/sdm_archive'
        sdmname_full = os.path.join(sdmloc, sdmname)
        if os.path.exists(sdmname_full):
            shutil.copytree(sdmname_full, os.path.join('.', sdmname), ignore_dangling_symlinks=True, symlinks=True)
        else:
            logger.warn("No SDM found")
            return

    bdfdestination = os.path.join('.', sdmname, 'ASDMBinary')
    if not os.path.exists(bdfdestination):
        os.mkdir(bdfdestination)

    bdft = sdmname.split('_')[-1]
    # remove suffix for sdms created multiple times
    if bdft[-2] is '.':
        bdft = bdft[:-2]
    bdfdir = '/lustre/evla/wcbe/data/realfast/'
    bdf0 = glob.glob('{0}/*{1}'.format(bdfdir, bdft))
    if len(bdf0) == 1:
        bdf0 = bdf0[0].split('/')[-1]
        newbdfpath = os.path.join(bdfdestination, bdf0)
        if os.path.exists(newbdfpath) and copybdf and os.path.islink(newbdfpath):
            os.unlink(newbdfpath)

        if not os.path.exists(newbdfpath):
            if copybdf:
                shutil.copy(os.path.join(bdfdir, bdf0), newbdfpath)
            else:
                os.symlink(os.path.join(bdfdir, bdf0), newbdfpath)
    elif len(bdf0) == 0:
        logger.warn("No bdf found for {0}".format(sdmname))
    else:
        logger.warn("Could not find unique bdf for {0} in {1}. No bdf copied.".format(sdmname, bdf0))

@cli.command()
@click.option('--globstr', default='/home/mctest/evla/mcaf/workspace/realfast*')
def backup(globstr):
    """ Get all SDMs in sdm building directory and run buildsdm on them to save locally.
    """

    import glob

    sdmnames = glob.glob(globstr)
    for sdmname in sdmnames:
        sdmname = os.path.basename(sdmname)
        if not os.path.exists(sdmname):
            args = ["realfast", "buildsdm", "--sdmname", sdmname]
            logger.info("building sdm {0} locally".format(sdmname))
            subprocess.call(args)
        else:
            logger.info("sdm {0} already exists locally".format(sdmname))

@cli.command()
@click.argument('sdmname')
@click.option('--notebook', default='Search_and_refine.ipynb')
@click.option('--on_rfnode', type=bool, default=True)
@click.option('--preffile', default=None)
def refinement_notebook(sdmname, notebook, on_rfnode, preffile):
    """ Compile notebook
    """

    notebookpath = '/home/cbe-master/realfast/soft/realfast/realfast/notebooks'

    # report-mode just shows output of each cell
    args = ["papermill", "--report-mode", "-p", "sdmname", sdmname, os.path.join(notebookpath, notebook), sdmname+".ipynb"]
    subprocess.call(args)
    args = ["jupyter", "nbconvert", sdmname+".ipynb", "--to", "html", "--output", sdmname+".html"]
    subprocess.call(args)
    destination = 'claw@nmpost-master:/lustre/aoc/projects/fasttransients/realfast/plots/refinement'
    args = ["rsync", "-av", "--remove-source-files", "--include", "{0}.html".format(sdmname), "--exclude", "*", '.', destination]
    logger.info("Refinement notebook available at http://realfast.nrao.edu/plots/refinement/{0}.html".format(sdmname))
    status = subprocess.call(args)
    if not status:
        os.remove(sdmname + '.ipynb')

@cli.command()
@click.argument('candid')
@click.option('--indexprefix', default='new')
@click.option('--ddm', default=50)
@click.option('--dm_steps', default=50)
@click.option('--npix_max', default=None)
@click.option('--mode', default='deployment')
def refine_candid(candid, indexprefix, ddm, dm_steps, npix_max, mode):
    """ Compile notebook
    """
    from realfast import util

    util.refine_candid(candid, indexprefix, ddm, dm_steps, npix_max, mode)

@cli.command()
@click.argument('query')
@click.option('--indexprefix', default='new')
@click.option('--confirm', default=True)
@click.option('--mode', default='deployment')
def refine_all(query, indexprefix, confirm, mode):
    """ Refines all candidates matching query
    """

    from realfast import elastic, util

    Ids = elastic.get_ids(indexprefix+'cands', query)
    if confirm:
        yn = input("Refine {0} candidates matching query {1}?".format(len(Ids), query))
        if yn.lower() in ['y', 'yes']:
            for Id in Ids:
                util.refine_candid(Id, mode=mode)

@click.group('realfast_portal')
def cli2():
    pass

@cli2.command()
@click.argument('index')
def get_ids(index):
    """ Get ids in given index
    """

    from realfast import elastic

    logger.info("Getting Ids in index {0}".format(index))
    print(elastic.get_ids(index))

@cli2.command()
@click.argument('index')
@click.argument('_id')
def get_doc(index, _id):
    """ Get doc with _id in given index
    """

    from realfast import elastic

    logger.info("Getting Id {0} in index {1}".format(_id, index))
    print(elastic.get_doc(index, _id))

@cli2.command()
@click.option('--prefix1', default='new')
@click.option('--prefix2', default='final')
@click.option('--datasetid', default=None)
@click.option('--scanid', default=None)
@click.option('--force', is_flag=True)
def move_dataset(prefix1, prefix2, datasetid, scanid, force):
    """ Move datasetId or scanId from prefix1 to prefix2
    """

    from realfast import elastic

    elastic.move_dataset(prefix1, prefix2, datasetId=datasetid, scanId=scanid, force=force)

@cli2.command()
@click.option('--prefix1', default='new')
@click.option('--prefix2', default='final')
def move_consensus(prefix1, prefix2):
    """ Use consensus to move candidates from 1 to 2
    """

    from realfast import elastic

@cli2.command()
@click.option('--prefix', default='new')
@click.option('--datasetid', default=None)
@click.option('--scanid', default=None)
@click.option('--force', is_flag=True)
def remove_dataset(prefix, datasetid, scanid, force):
    """ Remove all data associated with given datasetid
    """

    from realfast import elastic

    elastic.remove_dataset(prefix, datasetId=datasetid, scanId=scanid, force=force)

@cli2.command()
@click.argument('prefix')
def audit_indexprefix(prefix):
    """ Audit all indices with given prefix
    """

    from realfast import elastic

    elastic.audit_indexprefix(prefix)

@cli2.command()
@click.argument('prefix')
def reset_indices(prefix):
    """ Reset all indices with given prefix
    """

    from realfast import elastic

    elastic.reset_indices(prefix)
<commit_msg>archive-local set up with cron on test day mornings<commit_after>from __future__ import print_function, division, absolute_import #, unicode_literals # not casa compatible
from builtins import bytes, dict, object, range, map, input#, str # not casa compatible
from future.utils import itervalues, viewitems, iteritems, listvalues, listitems
from io import open

import click
import shutil
import subprocess
import os
import glob
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logging.captureWarnings(True)
logger = logging.getLogger(__name__)
logger.setLevel(20)

vys_cfile = '/home/cbe-master/realfast/soft/vysmaw_apps/vys.conf'
default_preffile = '/lustre/evla/test/realfast/realfast.yml'

@click.group('realfast')
def cli():
    pass

@cli.command()
@click.option('--mode', default='line')
@click.option('--line', default=None)
@click.option('--filepath', default='/home/cbe-master/realfast/soft/logs/')
@click.option('--globstr', default='rf*log')
@click.option('--age', default=3600, type=int)
def grep(mode, line, filepath, globstr, age):
    """ Find line in files in path
    Also has modes 'badclose' (and more?), where line is set
    for user.
    """

    import os.path
    import glob
    import time
    now = time.time()

    if mode == 'line':
        assert line is not None
    elif mode == 'badclose':
        line = '6: 0}'

    files0 = list(glob.glob(os.path.join(filepath, globstr)))
    files1 = list(filter(lambda x: now-os.path.getmtime(x) < age, files0))
    print('Running grep on {1} files ({0} match globstr)\n'.format(len(files1), len(files0)))
    files = ' '.join(files1)
    callstr = 'grep "{0}" {1}'.format(line, files)
    if len(files1):
#        print(callstr)
        subprocess.call(callstr, shell=True)
    else:
        print('No files found')

@cli.command()
@click.option('--channel', default='#alerts')
@click.option('--message')
def slack(channel, message):
    """ Update slack with something.
    """

    from realfast import util
    util.update_slack(channel, message)

@cli.command()
@click.option('--preffile', default='realfast.yml')
@click.option('--inprefs', default={})
def config_catcher(preffile, inprefs):
    """ Runs async process to catch multicast messages to form scan config objects
    Can be saved to pklfile and optionally attached to preferences from preffile.
    """

    from realfast import controllers

    logger.info("Running config catcher with preffile={0}"
                .format(preffile))
    config = controllers.config_controller(preffile=preffile, inprefs=inprefs)
    config.run()

@cli.command()
@click.option('--mode', default='deployment')
@click.option('--preffile', default=default_preffile)
@click.option('--threshold', type=float, default=None)
@click.option('--rsync_with_fetch', is_flag=True)
@click.option('--rsync_with_reader', is_flag=True)
@click.option('--excludeants', default=None)
def run(mode, preffile, threshold, rsync_with_fetch, rsync_with_reader, excludeants):
    """ Run realfast controller to catch scan configs and start rfpipe.
    mode can be "deployment" or "development", which defines scheduler IP.
    preffile can be realfast.yml or another yml config file.
    """

    from realfast import controllers

    if mode == 'deployment':
        host = '10.80.200.201:8786'
    elif mode == 'development':
        host = '10.80.200.201:8796'
    else:
        logger.warn("mode not recognized (deployment or development allowed)")
        return 1

    # overload some preferences
    inprefs = {}
    if threshold is not None:
        inprefs['sigma_image1'] = threshold
    if excludeants is not None:
        inprefs['excludeants'] = excludeants

    try:
        rfc = controllers.realfast_controller(host=host, preffile=preffile, inprefs=inprefs,
                                              rsync_with_reader=rsync_with_reader, rsync_with_fetch=rsync_with_fetch)
        rfc.initialize()
        rfc.run()
    except KeyboardInterrupt:
        logger.warn("Cleaning up before stopping processing.")
        rfc.cleanup_loop()
    except OSError:
        logger.warn("Could not start controller. Are scheduler and workers running?")

@cli.command()
@click.option('--sdmname', default=None)
@click.option('--candid', default=None)
@click.option('--indexprefix', default='new')
@click.option('--copybdf', is_flag=True)
def buildsdm(sdmname, candid, indexprefix, copybdf):
    """ Assemble sdm/bdf from cbe lustre.
    Can find it from sdmname or can look up by candid.
    """

    if sdmname is None:
        from realfast import elastic
        if candid is None:
            logger.exception("Need to provide canid or sdmname")
        doc = elastic.get_doc(indexprefix + 'cands', candid)
        assert 'sdmname' in doc['_source'], 'No sdmname associated with that candid'
        sdmname = doc['_source']['sdmname'].split('/')[-1]
        logger.info("Got sdmname {0} from {1}cands index".format(sdmname, indexprefix))

    sdmloc = '/home/mctest/evla/mcaf/workspace/'
    sdmname_full = os.path.join(sdmloc, sdmname)
    if os.path.exists(sdmname_full):
        shutil.copytree(sdmname_full, os.path.join('.', sdmname), ignore_dangling_symlinks=True, symlinks=True)
    else:
        logger.info("Trying realfast temp archive...")
        sdmloc = '/lustre/evla/test/realfast/archive/sdm_archive'
        sdmname_full = os.path.join(sdmloc, sdmname)
        if os.path.exists(sdmname_full):
            shutil.copytree(sdmname_full, os.path.join('.', sdmname), ignore_dangling_symlinks=True, symlinks=True)
        else:
            logger.warn("No SDM found")
            return

    bdfdestination = os.path.join('.', sdmname, 'ASDMBinary')
    if not os.path.exists(bdfdestination):
        os.mkdir(bdfdestination)

    bdft = sdmname.split('_')[-1]
    # remove suffix for sdms created multiple times
    if bdft[-2] is '.':
        bdft = bdft[:-2]
    bdfdir = '/lustre/evla/wcbe/data/realfast/'
    bdf0 = glob.glob('{0}/*{1}'.format(bdfdir, bdft))
    if len(bdf0) == 1:
        bdf0 = bdf0[0].split('/')[-1]
        newbdfpath = os.path.join(bdfdestination, bdf0)
        if os.path.exists(newbdfpath) and copybdf and os.path.islink(newbdfpath):
            os.unlink(newbdfpath)

        if not os.path.exists(newbdfpath):
            if copybdf:
                shutil.copy(os.path.join(bdfdir, bdf0), newbdfpath)
            else:
                os.symlink(os.path.join(bdfdir, bdf0), newbdfpath)
    elif len(bdf0) == 0:
        logger.warn("No bdf found for {0}".format(sdmname))
    else:
        logger.warn("Could not find unique bdf for {0} in {1}. No bdf copied.".format(sdmname, bdf0))

@cli.command()
@click.option('--globstr', default='/home/mctest/evla/mcaf/workspace/realfast*')
def backup(globstr):
    """ Get all SDMs in sdm building directory and run buildsdm on them to save locally.
    """

    import glob

    sdmnames = glob.glob(globstr)
    for sdmname in sdmnames:
        sdmname = os.path.basename(sdmname)
        if not os.path.exists(sdmname):
            args = ["realfast", "buildsdm", "--sdmname", sdmname]
            logger.info("building sdm {0} locally".format(sdmname))
            subprocess.call(args)
        else:
            logger.info("sdm {0} already exists locally".format(sdmname))

@cli.command()
@click.argument('sdmname')
@click.option('--notebook', default='Search_and_refine.ipynb')
@click.option('--on_rfnode', type=bool, default=True)
@click.option('--preffile', default=None)
def refinement_notebook(sdmname, notebook, on_rfnode, preffile):
    """ Compile notebook
    """

    notebookpath = '/home/cbe-master/realfast/soft/realfast/realfast/notebooks'

    # report-mode just shows output of each cell
    args = ["papermill", "--report-mode", "-p", "sdmname", sdmname, os.path.join(notebookpath, notebook), sdmname+".ipynb"]
    subprocess.call(args)
    args = ["jupyter", "nbconvert", sdmname+".ipynb", "--to", "html", "--output", sdmname+".html"]
    subprocess.call(args)
    destination = 'claw@nmpost-master:/lustre/aoc/projects/fasttransients/realfast/plots/refinement'
    args = ["rsync", "-av", "--remove-source-files", "--include", "{0}.html".format(sdmname), "--exclude", "*", '.', destination]
    logger.info("Refinement notebook available at http://realfast.nrao.edu/plots/refinement/{0}.html".format(sdmname))
    status = subprocess.call(args)
    if not status:
        os.remove(sdmname + '.ipynb')

@cli.command()
@click.argument('candid')
@click.option('--indexprefix', default='new')
@click.option('--ddm', default=50)
@click.option('--dm_steps', default=50)
@click.option('--npix_max', default=None)
@click.option('--mode', default='deployment')
def refine_candid(candid, indexprefix, ddm, dm_steps, npix_max, mode):
    """ Compile notebook
    """
    from realfast import util

    util.refine_candid(candid, indexprefix, ddm, dm_steps, npix_max, mode)

@cli.command()
@click.argument('query')
@click.option('--indexprefix', default='new')
@click.option('--confirm', default=True)
@click.option('--mode', default='deployment')
def refine_all(query, indexprefix, confirm, mode):
    """ Refines all candidates matching query
    """

    from realfast import elastic, util

    Ids = elastic.get_ids(indexprefix+'cands', query)
    if confirm:
        yn = input("Refine {0} candidates matching query {1}?".format(len(Ids), query))
        if yn.lower() in ['y', 'yes']:
            for Id in Ids:
                util.refine_candid(Id, mode=mode)

@cli.command()
@click.option('--confirm', default=True, type=bool)
def archive_local(confirm):
    """ Move data from lustre workdir into local archive
    """

    import datetime
    import os
    import glob
    import shutil

    if os.getcwd() == '/lustre/evla/test/realfast':
        now = datetime.datetime.now()
        dirname = 'archive/{0}{1}{2:02}'.format(now.year-2000, now.strftime("%b").lower(), now.day)
        if not os.path.exists(dirname):
            os.mkdir(dirname)
            logger.info("Creating directory {0} in local archive".format(dirname))
        filelist = glob.glob("cands_*[html|pkl|png]")
        if confirm:
            yn = input("Move {0} files to {1}?".format(len(filelist), dirname))
            if yn.lower() in ['y', 'yes']:
                for fp in filelist:
                    shutil.move(fp, dirname)

@click.group('realfast_portal')
def cli2():
    pass

@cli2.command()
@click.argument('index')
def get_ids(index):
    """ Get ids in given index
    """

    from realfast import elastic

    logger.info("Getting Ids in index {0}".format(index))
    print(elastic.get_ids(index))

@cli2.command()
@click.argument('index')
@click.argument('_id')
def get_doc(index, _id):
    """ Get doc with _id in given index
    """

    from realfast import elastic

    logger.info("Getting Id {0} in index {1}".format(_id, index))
    print(elastic.get_doc(index, _id))

@cli2.command()
@click.option('--prefix1', default='new')
@click.option('--prefix2', default='final')
@click.option('--datasetid', default=None)
@click.option('--scanid', default=None)
@click.option('--force', is_flag=True)
def move_dataset(prefix1, prefix2, datasetid, scanid, force):
    """ Move datasetId or scanId from prefix1 to prefix2
    """

    from realfast import elastic

    elastic.move_dataset(prefix1, prefix2, datasetId=datasetid, scanId=scanid, force=force)

@cli2.command()
@click.option('--prefix1', default='new')
@click.option('--prefix2', default='final')
def move_consensus(prefix1, prefix2):
    """ Use consensus to move candidates from 1 to 2
    """

    from realfast import elastic

@cli2.command()
@click.option('--prefix', default='new')
@click.option('--datasetid', default=None)
@click.option('--scanid', default=None)
@click.option('--force', is_flag=True)
def remove_dataset(prefix, datasetid, scanid, force):
    """ Remove all data associated with given datasetid
    """

    from realfast import elastic

    elastic.remove_dataset(prefix, datasetId=datasetid, scanId=scanid, force=force)

@cli2.command()
@click.argument('prefix')
def audit_indexprefix(prefix):
    """ Audit all indices with given prefix
    """

    from realfast import elastic

    elastic.audit_indexprefix(prefix)

@cli2.command()
@click.argument('prefix')
def reset_indices(prefix):
    """ Reset all indices with given prefix
    """

    from realfast import elastic

    elastic.reset_indices(prefix)
<|endoftext|>


<commit_before>#!/usr/bin/python
#
# Copyright 2016 AT&T Intellectual Property, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import functest.core.feature_base as base

class Copper(base.FeatureBase):
    def __init__(self):
        super(Copper, self).__init__(project='copper',
                                     case='copper-notification',
                                     repo='dir_repo_copper')
        self.cmd = "%s/tests/run.sh %s/tests" % (self.repo, self.repo)
<commit_msg>Bugfix: fix the wrong path in copper<commit_after>#!/usr/bin/python
#
# Copyright 2016 AT&T Intellectual Property, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import functest.core.feature_base as base

class Copper(base.FeatureBase):
    def __init__(self):
        super(Copper, self).__init__(project='copper',
                                     case='copper-notification',
                                     repo='dir_repo_copper')
        self.cmd = 'cd %s/tests && ./run.sh' % self.repo
<|endoftext|>


<commit_before># -*- Mode: Python -*-

import lisp_reader
import transform
import graph
import nodes
import solver
import analyze
import cps
import backend
import os

class context:
    # maintain some context between passes
    def __init__ (self):
        self.datatypes = {}
        self.dep_graph = None
        self.scc_graph = None
        self.scc_map = None
        self.var_dict = None
        self.record_types = None
        self.datatypes = {}
        self.cincludes = set()
        self.records2 = {}
        self.labels2 = {}
        self.variant_labels = {
            # Hack.  since pxll.h and header.c:dump_object() already
            #   know about the 'builtin' cons and nil constructors, we
            #   hard-code them in here.  You'll note that TC_PAIR is
            #   two entries before TC_USEROBJ...
            'cons': -2,
            'nil': -3,
            }
        # don't throw away precious tag space.
        self.nvariant_offset = len(self.variant_labels)
        # constructed literals
        self.constructed = []
        self.symbols = {}

def compile_file (f, name, annotate=True, noinline=False, verbose=False, trace=False, step_solver=False, typetype=False):
    base, ext = os.path.splitext (name)
    print 'read...'
    r = lisp_reader.reader (f)
    exp = r.read_all()

    if verbose:
        print '--- read ---'
        pp (exp)

    # XXX might go easier on memory if we discarded the results of
    #  each pass after using it... 8^)

    c = context()

    print 'transform...'
    t = transform.transformer (c)
    exp2 = t.go (exp)
    if verbose:
        print '--- transform ---'
        pp (exp2)

    w = nodes.walker (c)
    exp3 = w.go (exp2)

    print 'rename...'
    # alpha conversion
    c.var_dict = nodes.rename_variables (exp3, c)
    # find strongly connected components
    print 'call graph...'
    c.dep_graph = graph.build_dependency_graph (exp3)
    c.scc_graph, c.scc_map = graph.strongly (c.dep_graph)

    print 'typing...'
    # run the constraint generator and solver to find types
    t = solver.typer (c, verbose, step=step_solver)

    if typetype:
        t.go (exp3)

    if verbose:
        print '--- typing 1 ---'
        exp3.pprint()

    a = analyze.analyzer (c, noinline, verbose)
    exp4 = a.analyze (exp3)

    if verbose:
        print '--- analyzer ---'
        exp4.pprint()

    t2 = solver.typer (c, verbose, step=step_solver)
    t.go (exp4)
    #import cProfile
    #cProfile.runctx ("t.go (exp4)", globals(), locals())
    if verbose:
        print '--- typing 2 ---'
        exp4.pprint()

    ic = cps.cps (c, verbose=verbose)
    exp5 = ic.go (exp4)

    if verbose:
        print '--- cps ---'
        cps.pretty_print (exp5)

    fo = open ('%s.c' % base, 'wb')
    num_regs = cps.the_register_allocator.max_reg
    b = backend.c_backend (fo, name, num_regs, c, annotate=annotate, trace=trace)
    b.emit (exp5)
    b.done()
    fo.close()

def cc (name, force_32=False, optimize=False, cc='gcc'):
    import os
    base, ext = os.path.splitext (name)
    uname = os.uname()
    machine = uname[-1]
    if machine == 'amd64':
        if force_32:
            arch = '-m32'
        else:
            arch = '-m64'
    else:
        arch = ''
    if optimize:
        #optimize = '-O3'
        optimize = '-O2'
    else:
        optimize = ''
    if uname[0] == 'Darwin':
        # doesn't work as of os x 10.6 [and won't work until clang supports lexical funs]
        #cc = '/Developer/usr/bin/clang'
        # *does* work as of os x 10.6
        cc = '/Developer/usr/bin/llvm-gcc'
        arch += ' -fnested-functions'
        if not force_32:
            arch += ' -m64'
    cmd = '%s -I. -g %s %s %s.c -o %s' % (cc, arch, optimize, base, base)
    print cmd
    os.system (cmd)

from pprint import pprint as pp

# raise this from the default 1000
import sys
sys.setrecursionlimit (10000)

if __name__ == '__main__':
    import os
    import sys
    import cStringIO

    def argtest (s):
        if s in sys.argv:
            sys.argv.remove (s)
            return True
        else:
            return False

    optimize = argtest ('-O')
    annotate = argtest ('-a')
    verbose  = argtest ('-v')
    trace    = argtest ('-t')
    noinline = argtest ('-ni')
    force_32 = argtest ('-f32')
    step_solver = argtest ('-ss')
    # run the type solver *before* inlining as well as after.
    typetype = argtest ('-tt')

    if '-f' in sys.argv:
        sys.argv.remove ('-f')
        name = sys.argv[1]
        compile_file (open (name, 'rb'), name, annotate, noinline, verbose, trace, step_solver, typetype)
        cc (name, optimize=optimize)
<commit_msg>moved context object into its own module start the shift toward passing optional args via context "-nrc" -DNO_RANGE_CHECK option switch back to /usr/local/bin/gcc (probably until dragonegg works)<commit_after># -*- Mode: Python -*-

import lisp_reader
import transform
import graph
import nodes
import solver
import analyze
import cps
import backend
import os
import context

def compile_file (f, name, c):
    base, ext = os.path.splitext (name)
    print 'read...'
    r = lisp_reader.reader (f)
    exp = r.read_all()

    if c.verbose:
        print '--- read ---'
        pp (exp)

    # XXX might go easier on memory if we discarded the results of
    #  each pass after using it... 8^)

    print 'transform...'
    t = transform.transformer (c)
    exp2 = t.go (exp)
    if c.verbose:
        print '--- transform ---'
        pp (exp2)

    w = nodes.walker (c)
    exp3 = w.go (exp2)

    print 'rename...'
    # alpha conversion
    c.var_dict = nodes.rename_variables (exp3, c)
    # find strongly connected components
    print 'call graph...'
    c.dep_graph = graph.build_dependency_graph (exp3)
    c.scc_graph, c.scc_map = graph.strongly (c.dep_graph)

    print 'typing...'
    # run the constraint generator and solver to find types
    t = solver.typer (c, c.verbose, step=c.step_solver)

    if c.typetype:
        t.go (exp3)

    if c.verbose:
        print '--- typing 1 ---'
        exp3.pprint()

    a = analyze.analyzer (c)
    exp4 = a.analyze (exp3)

    if c.verbose:
        print '--- analyzer ---'
        exp4.pprint()

    t2 = solver.typer (c, c.verbose, step=c.step_solver)
    t.go (exp4)
    #import cProfile
    #cProfile.runctx ("t.go (exp4)", globals(), locals())
    if c.verbose:
        print '--- typing 2 ---'
        exp4.pprint()

    ic = cps.cps (c, verbose=c.verbose)
    exp5 = ic.go (exp4)

    if c.verbose:
        print '--- cps ---'
        cps.pretty_print (exp5)

    fo = open ('%s.c' % base, 'wb')
    num_regs = cps.the_register_allocator.max_reg
    b = backend.c_backend (fo, name, num_regs, c)
    b.emit (exp5)
    b.done()
    fo.close()
    cc (name, c)

def cc (name, context):
    import os
    base, ext = os.path.splitext (name)
    uname = os.uname()
    machine = uname[-1]
    if machine == 'amd64':
        if context.force_32:
            arch = '-m32'
        else:
            arch = '-m64'
    else:
        arch = ''
    if context.optimize:
        #optimize = '-O3'
        optimize = '-O2'
    else:
        optimize = ''
    if uname[0] == 'Darwin':
        # doesn't work as of os x 10.6 [and won't work until clang supports lexical funs]
        #cc = '/Developer/usr/bin/clang'
        # *does* work as of os x 10.6
        #cc = '/Developer/usr/bin/llvm-gcc'
        cc = '/usr/local/bin/gcc'
        #arch += ' -fnested-functions'
        if not context.force_32:
            arch += ' -m64'
    if context.no_range:
        arch += ' -DNO_RANGE_CHECK '
    cmd = '%s -I. -g %s %s %s.c -o %s' % (cc, arch, optimize, base, base)
    print cmd
    os.system (cmd)

from pprint import pprint as pp

# raise this from the default 1000
import sys
sys.setrecursionlimit (10000)

if __name__ == '__main__':
    import os
    import sys
    import cStringIO

    def argtest (s):
        if s in sys.argv:
            sys.argv.remove (s)
            return True
        else:
            return False

    c = context.context()

    c.optimize = argtest ('-O')
    c.annotate = argtest ('-a')
    c.verbose  = argtest ('-v')
    c.trace    = argtest ('-t')
    c.noinline = argtest ('-ni')
    c.force_32 = argtest ('-f32')
    c.step_solver = argtest ('-ss')
    # run the type solver *before* inlining as well as after.
    c.typetype = argtest ('-tt')
    c.no_range = argtest ('-nrc')

    if '-f' in sys.argv:
        sys.argv.remove ('-f')
        name = sys.argv[1]
        compile_file (open (name, 'rb'), name, c)
<|endoftext|>