<commit_before># -*- coding: utf-8 -*-
u"""Test background processes

:copyright: Copyright (c) 2019 RadiaSoft LLC.  All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
from __future__ import absolute_import, division, print_function
from pykern.pkcollections import PKDict
import pytest

def test_controls(fc):
    fc.sr_animation_run(
        fc.sr_sim_data('FODO with instruments'),
        'instrumentAnimation',
        PKDict(),
        expect_completed=False,
    )

def test_elegant(fc):
    import sirepo.template.lattice
    data = fc.sr_sim_data('Compact Storage Ring')
    sirepo.template.lattice.LatticeUtil.find_first_command(data, 'bunched_beam').n_particles_per_bunch = 1
    fc.sr_animation_run(
        data,
        'animation',
        PKDict({
            'elementAnimation22-13': PKDict(
                expect_x_range='0.0, 46',
                expect_y_range='-1.*e-15, 34.6',
            ),
        }),
        timeout=30,
    )

def test_jspec(fc):
    data = fc.sr_sim_data('DC Cooling Example')
    data.models.simulationSettings.update(PKDict(
        time=1,
        step_number=1,
        time_step=1,
    ))
    fc.sr_animation_run(
        data,
        'animation',
        PKDict(
            #TODO(robnagler) these are sometimes off, just rerun
            beamEvolutionAnimation=PKDict(
                expect_y_range=r'2.15e-06',
            ),
            coolingRatesAnimation=PKDict(
                expect_x_range=r'0, 1\.0',
            ),
        ),
        timeout=20,
    )

def test_madx(fc):
    import sirepo.template.lattice
    data = fc.sr_sim_data('FODO PTC')
    sirepo.template.lattice.LatticeUtil.find_first_command(data, 'beam').npart = 1
    fc.sr_animation_run(
        data,
        'animation',
        PKDict(),
    )

def test_ml(fc):
    fc.sr_animation_run(
        fc.sr_sim_data('iris Dataset'),
        'animation',
        PKDict(),
        timeout=45,
    )

def test_opal(fc):
    fc.sr_animation_run(
        fc.sr_sim_data('CSR Bend Drift'),
        'animation',
        PKDict(),
    )

def test_radia(fc):
    fc.sr_animation_run(
        fc.sr_sim_data('Dipole'),
        'solverAnimation',
        PKDict(),
    )

def test_srw(fc):
    data = fc.sr_sim_data("Young's Double Slit Experiment")
    data.models.multiElectronAnimation.numberOfMacroElectrons = 4
    data.models.simulation.sampleFactor = 0.0001
    fc.sr_animation_run(
        data,
        'multiElectronAnimation',
        PKDict(
            multiElectronAnimation=PKDict(
                # Prevents "Memory Error" because SRW uses computeJobStart as frameCount
                frame_index=0,
                expect_title='E=4240 eV',
            ),
        ),
        timeout=20,
    )

def test_synergia(fc):
    fc.sr_animation_run(
        fc.sr_sim_data('Simple FODO'),
        'animation',
        PKDict(
            beamEvolutionAnimation=PKDict(
                frame_index=7,
                expect_y_range='^.0.00262.*, 0.00572',
            ),
            bunchAnimation=PKDict(
                frame_count_key='bunchAnimation.frameCount',
                expect_title=lambda i: r'turn {}\b'.format(i),
                expect_y_range=lambda i: [
                    '-0.01.*, 0.01.*, 200',
                    '-0.01.*, 0.01.*, 200',
                ][i]
            ),
            turnComparisonAnimation=PKDict(
                 frame_index=0,
                 expect_y_range='^.0.0026.*, 0.0057',
            ),
        ),
    )

def test_warppba(fc):
    data = fc.sr_sim_data('Laser Pulse')
    data.models.simulationGrid.update(PKDict(
        rScale=1,
        rLength=5.081245038595,
        rMax=5.081245038595,
        rMin=0,
        rCount=8,
        rCellsPerSpotSize=8,
        rParticlesPerCell=1,
        rCellResolution=20,
        zScale=1,
        zLength=10.162490077316,
        zMax=1.6,
        zMin=-10.162490077316,
        zCellsPerWavelength=8,
        zCount=118,
        zParticlesPerCell=1,
        zCellResolution=20,
    ))
    data.models.electronPlasma.length = 0.05
    fc.sr_animation_run(
        data,
        'animation',
        PKDict(
            particleAnimation=PKDict(
                expect_title=lambda i: r'iteration {}\)'.format((i + 1) * 50),
                expect_y_range='-5.7.*e-06, 5.7.*e-06, 219',
            ),
            fieldAnimation=PKDict(
                expect_title=lambda i: r'iteration {}\)'.format((i + 1) * 50),
                expect_y_range='-5.*e-06, 5.*e-06, 18',
            ),
        ),
        timeout=20,
    )

def test_warpvnd(fc):
    fc.sr_animation_run(
        fc.sr_sim_data('Two Poles'),
        'fieldCalculationAnimation',
        PKDict(
            fieldCalcAnimation=PKDict(
                frame_index=0,
                expect_y_range='-1e-07, 1e-07, 23',
            ),
        ),
        timeout=20,
    )
    data = fc.sr_sim_data('Two Poles')
    data.models.simulationGrid.update(PKDict(
        num_steps=100,
        channel_width=0.09,
    ))
    fc.sr_animation_run(
        data,
        'animation',
        PKDict(
            currentAnimation=PKDict(
                frame_index=0,
                expect_y_range='0.0, 9.9.*e-06',
            ),
            fieldAnimation=PKDict(
                frame_index=0,
                expect_y_range='-4.5e-08, 4.5e-08, 23',
            ),
        ),
        timeout=20,
    )

def test_zgoubi(fc):
    fc.sr_animation_run(
        fc.sr_sim_data('EMMA'),
        'animation',
        PKDict(
            bunchAnimation=PKDict(
                expect_title=lambda i: 'Pass {}'.format(i) if i else 'Initial Distribution',
                expect_y_range=lambda i: [
                    '-0.0462.*, -0.0281.*, 200',
                    '-0.0471.*, -0.0283.*, 200',
                    '-0.0472.*, -0.0274.*, 200',
                    '-0.0460.*, -0.0280.*, 200',
                    '-0.0460.*, -0.0294.*, 200',
                    '-0.0473.*, -0.0275.*, 200',
                    '-0.0480.*, -0.0281.*, 200',
                    '-0.0479.*, -0.0299.*, 200',
                    '-0.0481.*, -0.0294.*, 200',
                    '-0.0488.*, -0.0292.*, 200',
                    '-0.0484.*, -0.0303.*, 200',
                ][i],
            ),
        ),
    )
<commit_msg>fix #4555: animation_test intermittently failing (#4572)<commit_after># -*- coding: utf-8 -*-
u"""Test background processes

:copyright: Copyright (c) 2019 RadiaSoft LLC.  All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
from __future__ import absolute_import, division, print_function
from pykern.pkcollections import PKDict
import pytest

def test_controls(fc):
    fc.sr_animation_run(
        fc.sr_sim_data('FODO with instruments'),
        'instrumentAnimation',
        PKDict(),
        expect_completed=False,
    )

def test_elegant(fc):
    import sirepo.template.lattice
    data = fc.sr_sim_data('Compact Storage Ring')
    sirepo.template.lattice.LatticeUtil.find_first_command(data, 'bunched_beam').n_particles_per_bunch = 1
    fc.sr_animation_run(
        data,
        'animation',
        PKDict({
            'elementAnimation22-13': PKDict(
                expect_x_range='0.0, 46',
                expect_y_range='-1.*e-15, 34.6',
            ),
        }),
        timeout=30,
    )

def test_jspec(fc):
    data = fc.sr_sim_data('DC Cooling Example')
    data.models.simulationSettings.update(PKDict(
        time=1,
        step_number=1,
        time_step=1,
    ))
    fc.sr_animation_run(
        data,
        'animation',
        PKDict(
            #TODO(robnagler) these are sometimes off, just rerun
            beamEvolutionAnimation=PKDict(
                expect_y_range=r'2.15e-06',
            ),
            coolingRatesAnimation=PKDict(
                expect_x_range=r'0, 1\.0',
            ),
        ),
        timeout=20,
    )

def test_madx(fc):
    import sirepo.template.lattice
    data = fc.sr_sim_data('FODO PTC')
    sirepo.template.lattice.LatticeUtil.find_first_command(data, 'beam').npart = 1
    fc.sr_animation_run(
        data,
        'animation',
        PKDict(),
    )

def test_ml(fc):
    fc.sr_animation_run(
        fc.sr_sim_data('iris Dataset'),
        'animation',
        PKDict(),
        timeout=45,
    )

def test_opal(fc):
    fc.sr_animation_run(
        fc.sr_sim_data('CSR Bend Drift'),
        'animation',
        PKDict(),
    )

def test_radia(fc):
    fc.sr_animation_run(
        fc.sr_sim_data('Dipole'),
        'solverAnimation',
        PKDict(),
    )

def test_srw(fc):
    data = fc.sr_sim_data("Young's Double Slit Experiment")
    data.models.multiElectronAnimation.numberOfMacroElectrons = 4
    data.models.simulation.sampleFactor = 0.0001
    fc.sr_animation_run(
        data,
        'multiElectronAnimation',
        PKDict(
            multiElectronAnimation=PKDict(
                # Prevents "Memory Error" because SRW uses computeJobStart as frameCount
                frame_index=0,
                expect_title='E=4240 eV',
            ),
        ),
        timeout=20,
    )

def test_synergia(fc):
    fc.sr_animation_run(
        fc.sr_sim_data('Simple FODO'),
        'animation',
        PKDict(
            beamEvolutionAnimation=PKDict(
                frame_index=7,
                expect_y_range='^.0.00262.*, 0.00572',
            ),
            bunchAnimation=PKDict(
                frame_count_key='bunchAnimation.frameCount',
                expect_title=lambda i: r'turn {}\b'.format(i),
                expect_y_range=lambda i: [
                    '-0.01.*, 0.01.*, 200',
                    '-0.01.*, 0.01.*, 200',
                ][i]
            ),
            turnComparisonAnimation=PKDict(
                 frame_index=0,
                 expect_y_range='^.0.0026.*, 0.0057',
            ),
        ),
    )

def test_warppba(fc):
    data = fc.sr_sim_data('Laser Pulse')
    data.models.simulationGrid.update(PKDict(
        rScale=1,
        rLength=5.081245038595,
        rMax=5.081245038595,
        rMin=0,
        rCount=8,
        rCellsPerSpotSize=8,
        rParticlesPerCell=1,
        rCellResolution=20,
        zScale=1,
        zLength=10.162490077316,
        zMax=1.6,
        zMin=-10.162490077316,
        zCellsPerWavelength=8,
        zCount=118,
        zParticlesPerCell=1,
        zCellResolution=20,
    ))
    data.models.electronPlasma.length = 0.05
    fc.sr_animation_run(
        data,
        'animation',
        PKDict(
            particleAnimation=PKDict(
                expect_title=lambda i: r'iteration {}\)'.format((i + 1) * 50),
                expect_y_range='-5.7.*e-06, 5.7.*e-06, 219',
            ),
            fieldAnimation=PKDict(
                expect_title=lambda i: r'iteration {}\)'.format((i + 1) * 50),
                expect_y_range='-5.*e-06, 5.*e-06, 18',
            ),
        ),
        timeout=20,
    )

def test_warpvnd(fc):
    fc.sr_animation_run(
        fc.sr_sim_data('Two Poles'),
        'fieldCalculationAnimation',
        PKDict(
            fieldCalcAnimation=PKDict(
                frame_index=0,
                expect_y_range='-1e-07, 1e-07, 23',
            ),
        ),
        timeout=20,
    )
    data = fc.sr_sim_data('Two Poles')
    data.models.simulationGrid.update(PKDict(
        num_steps=100,
        channel_width=0.09,
    ))
    fc.sr_animation_run(
        data,
        'animation',
        PKDict(
            currentAnimation=PKDict(
                frame_index=0,
                expect_y_range='0.0, 9.9.*e-06',
            ),
            fieldAnimation=PKDict(
                frame_index=0,
                expect_y_range='-4.5e-08, 4.5e-08, 23',
            ),
        ),
        timeout=30,
    )

def test_zgoubi(fc):
    fc.sr_animation_run(
        fc.sr_sim_data('EMMA'),
        'animation',
        PKDict(
            bunchAnimation=PKDict(
                expect_title=lambda i: 'Pass {}'.format(i) if i else 'Initial Distribution',
                expect_y_range=lambda i: [
                    '-0.0462.*, -0.0281.*, 200',
                    '-0.0471.*, -0.0283.*, 200',
                    '-0.0472.*, -0.0274.*, 200',
                    '-0.0460.*, -0.0280.*, 200',
                    '-0.0460.*, -0.0294.*, 200',
                    '-0.0473.*, -0.0275.*, 200',
                    '-0.0480.*, -0.0281.*, 200',
                    '-0.0479.*, -0.0299.*, 200',
                    '-0.0481.*, -0.0294.*, 200',
                    '-0.0488.*, -0.0292.*, 200',
                    '-0.0484.*, -0.0303.*, 200',
                ][i],
            ),
        ),
    )
<|endoftext|>


<commit_before># Distributed under the terms of GPL version 2 or any later
# Copyright (C) Alexey Nezhdanov 2004
# Configuration reader for xmppd.py

# $Id: config.py,v 1.3 2004/10/03 17:47:50 snakeru Exp $

from xmpp import *
import ConfigParser

from string import *
from xml.sax import ContentHandler
from xml.sax import parseString
from xml.sax import make_parser
from xml.sax.handler import *

servernames = []
certificatefile = ''
spoolpath = ''
sslkeyfile = ''

class xmppdXML:
	'''XML streams parser for xmppd'''
        def __init__(self):
                self.ch = xmppdXMLHandler()

        def parseString(self, xmlstream, tag_attr, chars):
                self.ch.tag_attr = tag_attr
                parseString(xmlstream, self.ch)
                return self.ch.tag_name, self.ch.message

class xmppdXMLHandler(ContentHandler):
        def __init__(self):
                self.chars = ""
                self.message = ""
		self.section = ""
                self.current_tag = ""

        def startElement(self, name, attrs):
		global certificatefile
		global spoolpath
                self.chars = ""
                self.tag_name = name
                if name == "server":
                        self.section = "server"
                        #print "Current Tag: " + name
                elif name == "servernames":
                        self.section = "servernames"
                        #print "Current Tag: " + name
                elif name == "name":
                        self.current_tag = "name"
                        #print "Current Tag: " + name
                elif name == "certificate":
                        self.section = "certificate"
			try:
				certificatefile = attrs['file']
			except:
				certificatefile = ''
                        #print "Current Tag: " + name
                elif name == "spool":
                        self.section = "spool"
			try:
				spoolpath = attrs['path']
			except:
				spoolpath = '.'
                        #print "Current Tag: " + name
                else:
                        self.chars = ""
                        self.tag_name = name

        def characters(self, ch):
                self.chars = str(self.chars + ch)

        def endElement(self, name):
                #print 'Final de: ' + name + ' con current_tag: ' + self.current_tag
                #print 'Caracteres : ' + self.chars
                if name == "name":
			servernames.append(self.chars)
                self.message = self.chars

class Config(PlugIn):
    NS=''
    def __init__(self):
        PlugIn.__init__(self)
        self.DBG_LINE='config'

    def plugin(self,server):
        server.servernames=[]

	# Invoke the parsing
	parser = make_parser()
        parser.setFeature(feature_namespaces, 0)
        dh = xmppdXMLHandler()
        parser.setContentHandler(dh)
        parser.parse(server.cfgfile)

        for name in servernames:
            server.servernames.append(name.strip())
	if certificatefile == '':
		server.sslcertfile = None
	else:
		server.sslcertfile = certificatefile
	if sslkeyfile == '':
		server.sslkeyfile = server.sslcertfile
	else:
		server.sslkeyfile = sslkeyfile

	# This was good ... for a time
	#server.spoolpath = spoolpath
	server.spoolpath = os.environ['HOME'] + '.xmppd'

<commit_msg>packaging<commit_after># Distributed under the terms of GPL version 2 or any later
# Copyright (C) Alexey Nezhdanov 2004
# Configuration reader for xmppd.py

# $Id: config.py,v 1.3 2004/10/03 17:47:50 snakeru Exp $

from xmpp import *
import ConfigParser

from string import *
from xml.sax import ContentHandler
from xml.sax import parseString
from xml.sax import make_parser
from xml.sax.handler import *
import os

servernames = []
certificatefile = ''
spoolpath = ''
sslkeyfile = ''

class xmppdXML:
	'''XML streams parser for xmppd'''
        def __init__(self):
                self.ch = xmppdXMLHandler()

        def parseString(self, xmlstream, tag_attr, chars):
                self.ch.tag_attr = tag_attr
                parseString(xmlstream, self.ch)
                return self.ch.tag_name, self.ch.message

class xmppdXMLHandler(ContentHandler):
        def __init__(self):
                self.chars = ""
                self.message = ""
		self.section = ""
                self.current_tag = ""

        def startElement(self, name, attrs):
		global certificatefile
		global spoolpath
                self.chars = ""
                self.tag_name = name
                if name == "server":
                        self.section = "server"
                        #print "Current Tag: " + name
                elif name == "servernames":
                        self.section = "servernames"
                        #print "Current Tag: " + name
                elif name == "name":
                        self.current_tag = "name"
                        #print "Current Tag: " + name
                elif name == "certificate":
                        self.section = "certificate"
			try:
				certificatefile = attrs['file']
			except:
				certificatefile = ''
                        #print "Current Tag: " + name
                elif name == "spool":
                        self.section = "spool"
			try:
				spoolpath = attrs['path']
			except:
				spoolpath = '.'
                        #print "Current Tag: " + name
                else:
                        self.chars = ""
                        self.tag_name = name

        def characters(self, ch):
                self.chars = str(self.chars + ch)

        def endElement(self, name):
                #print 'Final de: ' + name + ' con current_tag: ' + self.current_tag
                #print 'Caracteres : ' + self.chars
                if name == "name":
			servernames.append(self.chars)
                self.message = self.chars

class Config(PlugIn):
    NS=''
    def __init__(self):
        PlugIn.__init__(self)
        self.DBG_LINE='config'

    def plugin(self,server):
        server.servernames=[]

	# Invoke the parsing
	parser = make_parser()
        parser.setFeature(feature_namespaces, 0)
        dh = xmppdXMLHandler()
        parser.setContentHandler(dh)
        parser.parse(server.cfgfile)

        for name in servernames:
            server.servernames.append(name.strip())
	if certificatefile == '':
		server.sslcertfile = None
	else:
		server.sslcertfile = certificatefile
	if sslkeyfile == '':
		server.sslkeyfile = server.sslcertfile
	else:
		server.sslkeyfile = sslkeyfile

	# This was good ... for a time
	#server.spoolpath = spoolpath
	server.spoolpath = os.environ['HOME'] + '.xmppd'

<|endoftext|>


<commit_before>from debuild import app
from monomoy.core import db
from monomoy.utils import JSONEncoder
from debuild.utils import db_find
from chatham.builders import Builder

from flask import request
import json

API_BASE = '/api'
DEBUG = True

# Core verbs:
#
#  - token
#  - ping
#  - result

def serialize(obj, allok):
    obj['status'] = 'ok' if allok else 'nokay'

    if DEBUG:
        return json.dumps(obj, sort_keys=True, indent=4, cls=JSONEncoder)
    return json.dumps(obj, cls=JSONEncoder)

def api_abort(code, text):
    return serialize({
        'code': code,
        'text': text
    }, False)

def api_validate(keys):
    req = request.values
    for key in ['node', 'signature']:
        if key not in req:
            return False

    for key in keys:
        if key not in req:
            return False

    node = req['node']
    builder = Builder(node)
    return builder.validate_request(req['signature'])

@app.route('%s/token' % (API_BASE), methods=['GET', 'POST'])
def token():
    """ Unauth'd ping """
    req = request.values
    if 'node' not in req:
        return api_abort('nsp-node', 'No such param: node')

    node = req['node']
    bob = Builder(node)
    key = bob.new_token()

    return serialize({
        "token": key
    }, True)

@app.route("%s/ping" % (API_BASE), methods=['GET', 'POST'])
def ping():
    req = request.values
    if not api_validate([]):
        return api_abort('bad-sig', 'bad signature')
    builder = Builder(req['node'])
    builder.ping()
    return serialize({
        'ping': 'pung'
    }, True)

@app.route("%s/result" % (API_BASE), methods=['GET', 'POST'])
def result():
    req = request.values
    if not api_validate(['data']):
        return api_abort('bad-sig', 'bad signature')  # factor this out
    builder = Builder(req['node'])
    builder.ping()
    data = json.loads(req['data'])
    job = data['job']
    jobj = db_find('jobs', job)
    if jobj['builder'] is None:
        return api_abort('bad-builder', 'bad builder node')

    builder = Builder(jobj['builder'])
    if jobj['builder'] != builder._obj['_id']:  # XXX: Fixme
        return api_abort('bad-builder', 'foo bad builder node')

    check_id = db.checks.insert(data)

    return serialize({
        'check': check_id
    }, True)
<commit_msg>fixing the job<commit_after>from debuild import app
from monomoy.core import db
from monomoy.utils import JSONEncoder
from debuild.utils import db_find
from chatham.builders import Builder

from flask import request
import json

API_BASE = '/api'
DEBUG = True

# Core verbs:
#
#  - token
#  - ping
#  - result

def serialize(obj, allok):
    obj['status'] = 'ok' if allok else 'nokay'

    if DEBUG:
        return json.dumps(obj, sort_keys=True, indent=4, cls=JSONEncoder)
    return json.dumps(obj, cls=JSONEncoder)

def api_abort(code, text):
    return serialize({
        'code': code,
        'text': text
    }, False)

def api_validate(keys):
    req = request.values
    for key in ['node', 'signature']:
        if key not in req:
            return False

    for key in keys:
        if key not in req:
            return False

    node = req['node']
    builder = Builder(node)
    return builder.validate_request(req['signature'])

@app.route('%s/token' % (API_BASE), methods=['GET', 'POST'])
def token():
    """ Unauth'd ping """
    req = request.values
    if 'node' not in req:
        return api_abort('nsp-node', 'No such param: node')

    node = req['node']
    bob = Builder(node)
    key = bob.new_token()

    return serialize({
        "token": key
    }, True)

@app.route("%s/ping" % (API_BASE), methods=['GET', 'POST'])
def ping():
    req = request.values
    if not api_validate([]):
        return api_abort('bad-sig', 'bad signature')
    builder = Builder(req['node'])
    builder.ping()
    return serialize({
        'ping': 'pung'
    }, True)

@app.route("%s/result" % (API_BASE), methods=['GET', 'POST'])
def result():
    req = request.values
    if not api_validate(['data']):
        return api_abort('bad-sig', 'bad signature')  # factor this out
    builder = Builder(req['node'])
    builder.ping()
    data = json.loads(req['data'])
    job = data['job']
    jobj = db_find('jobs', job)
    data['job'] = jobj['_id']

    if jobj['builder'] is None:
        return api_abort('bad-builder', 'bad builder node')

    builder = Builder(jobj['builder'])
    if jobj['builder'] != builder._obj['_id']:  # XXX: Fixme
        return api_abort('bad-builder', 'foo bad builder node')

    check_id = db.checks.insert(data)

    return serialize({
        'check': check_id
    }, True)
<|endoftext|>


<commit_before>from gitcd.Exceptions import GitcdException
from gitcd.Config.File import File as ConfigFile
from gitcd.Interface.AbstractInterface import AbstractInterface
from gitcd.Interface.Cli import Cli
from gitcd.Cli.Command import Command as CliCommand

from pprint import pprint

class Gitcd(object):

  interface = False
  configFile = ConfigFile()
  cliCommand = CliCommand()

  featureMethods = {
    'start': 'featureStart',
    'test': 'featureTest',
    'review': 'featureReview',
    'finish': 'featureFinish',
    'deploy': 'featureDeploy'
  }

  def setInterface(self, interface: AbstractInterface):
    self.interface = interface

  def setConfigFilename(self, configFilename: str):
    self.configFile.setConfigFilename(configFilename)

  def loadConfig(self):
    # todo: maybe a warning if we are working with the default values
    self.configFile.load()

  def init(self):
    self.configFile.setMaster(
      self.interface.askFor("Branch name for production releases?",
      False,
      self.configFile.getMaster())
    )

    self.configFile.setFeature(
      self.interface.askFor("Branch name for feature development?",
      False,
      self.configFile.getFeature())
    )

    self.configFile.setTest(
      self.interface.askFor("Branch name for test releases?",
      False,
      self.configFile.getTest())
    )

    self.configFile.setTag(
      self.interface.askFor("Version tag prefix?",
      False,
      self.configFile.getTag())
    )

    self.configFile.write()
    pprint(self.configFile.config)

  def feature(self, command, branch):
    # remote upate
    self.update()

    # dispatch from mapping
    featureMethod = getattr(self, self.featureMethods[command])
    featureMethod(branch)

  def update(self):
    self.cliCommand.execute("git remote update")

  # maybe even take this in a own feature class
  def featureStart(self, branch: str):
    print("gitcd feature start")

  def featureTest(self, branch: str):
    print("gitcd feature test")

  def featureReview(self, branch: str):
    print("gitcd feature review")

  def featureFinish(self, branch: str):
    print("gitcd feature finish")

  def featureDeploy(self, branch: str):
    # todo: no branchname needed cause we are just tagging the master branch
    # and it should probably be called directly from feature finish
    print("gitcd feature deploy")

<commit_msg>Removed misplaced newline<commit_after>from gitcd.Exceptions import GitcdException
from gitcd.Config.File import File as ConfigFile
from gitcd.Interface.AbstractInterface import AbstractInterface
from gitcd.Interface.Cli import Cli
from gitcd.Cli.Command import Command as CliCommand

from pprint import pprint

class Gitcd(object):

  interface = False
  configFile = ConfigFile()
  cliCommand = CliCommand()

  featureMethods = {
    'start': 'featureStart',
    'test': 'featureTest',
    'review': 'featureReview',
    'finish': 'featureFinish',
    'deploy': 'featureDeploy'
  }

  def setInterface(self, interface: AbstractInterface):
    self.interface = interface

  def setConfigFilename(self, configFilename: str):
    self.configFile.setConfigFilename(configFilename)

  def loadConfig(self):
    # todo: maybe a warning if we are working with the default values
    self.configFile.load()

  def init(self):
    self.configFile.setMaster(
      self.interface.askFor("Branch name for production releases?",
      False,
      self.configFile.getMaster())
    )

    self.configFile.setFeature(
      self.interface.askFor("Branch name for feature development?",
      False,
      self.configFile.getFeature())
    )

    self.configFile.setTest(
      self.interface.askFor("Branch name for test releases?",
      False,
      self.configFile.getTest())
    )

    self.configFile.setTag(
      self.interface.askFor("Version tag prefix?",
      False,
      self.configFile.getTag())
    )

    self.configFile.write()
    pprint(self.configFile.config)

  def feature(self, command, branch):
    # remote upate
    self.update()

    # dispatch from mapping
    featureMethod = getattr(self, self.featureMethods[command])
    featureMethod(branch)

  def update(self):
    self.cliCommand.execute("git remote update")

  # maybe even take this in a own feature class
  def featureStart(self, branch: str):
    print("gitcd feature start")

  def featureTest(self, branch: str):
    print("gitcd feature test")

  def featureReview(self, branch: str):
    print("gitcd feature review")

  def featureFinish(self, branch: str):
    print("gitcd feature finish")

  def featureDeploy(self, branch: str):
    # todo: no branchname needed cause we are just tagging the master branch
    # and it should probably be called directly from feature finish
    print("gitcd feature deploy")

<|endoftext|>


<commit_before>#!/usr/bin/env python
# coding=utf-8
"""
Collection of tests.

Tests methods need to start with "test_", otherwise you're free to do
whatever you want here.
"""
from __future__ import unicode_literals
from __future__ import absolute_import

__author__ = "Manuel Ebert"
__copyright__ = "Copyright 2015, summer.ai"
__date__ = "2015-12-17"
__email__ = "manuel@summer.ai"

from serapis.qualify_word import clean_and_qualify
import codecs

def test_qualify():
    with codecs.open("serapis/tests/data/words_qualified.txt", 'r', 'utf-8') as wordlist:
        for word in wordlist.readlines():
            assert clean_and_qualify(word), "Word '{}' falsely marked as invalid".format(word.strip())

def test_disqualify():
    with codecs.open("serapis/tests/data/words_disqualified.txt", 'r', 'utf-8') as wordlist:
        for word in wordlist.readlines():
            assert not clean_and_qualify(word), "Word '{}' falsely marked as valid".format(word.strip())
<commit_msg>Fix tests<commit_after>#!/usr/bin/env python
# coding=utf-8
"""
Collection of tests.

Tests methods need to start with "test_", otherwise you're free to do
whatever you want here.
"""
from __future__ import unicode_literals
from __future__ import absolute_import

__author__ = "Manuel Ebert"
__copyright__ = "Copyright 2015, summer.ai"
__date__ = "2015-12-17"
__email__ = "manuel@summer.ai"

from serapis.preprocess import clean_and_qualify_term
import codecs

def test_qualify():
    with codecs.open("serapis/tests/data/words_qualified.txt", 'r', 'utf-8') as wordlist:
        for word in wordlist.readlines():
            assert clean_and_qualify_term(word), "Word '{}' falsely marked as invalid".format(word.strip())

def test_disqualify():
    with codecs.open("serapis/tests/data/words_disqualified.txt", 'r', 'utf-8') as wordlist:
        for word in wordlist.readlines():
            assert not clean_and_qualify_term(word), "Word '{}' falsely marked as valid".format(word.strip())
<|endoftext|>


<commit_before>import os.path as op
import nose.tools as nt
from nipype import Workflow, Node, MapNode, IdentityInterface, DataGrabber
from nipype.interfaces.base import (BaseInterfaceInputSpec, TraitedSpec,
                                    BaseInterface)

from .. import graphutils as gu

def make_simple_workflow():

    wf = Workflow(name="test")

    node1 = Node(IdentityInterface(fields=["foo"]), name="node1")
    node2 = MapNode(IdentityInterface(fields=["foo"]),
                    name="node2", iterfield=["foo"])
    node3 = Node(IdentityInterface(fields=["foo"]), name="node3")

    wf.connect([
        (node1, node2, [("foo", "foo")]),
        (node2, node3, [("foo", "foo")]),
        ])

    return wf, node1, node2, node3

def test_input_wrapper():

    wf, node1, node2, node3 = make_simple_workflow()

    s_list = ['s1', 's2']
    s_node = gu.make_subject_source(s_list)

    g_node = Node(DataGrabber(in_fields=["foo"],
                              out_fields=["bar"]),
                  name="g_node")

    iw = gu.InputWrapper(wf, s_node, g_node, node1)

    yield nt.assert_equal, iw.wf, wf
    yield nt.assert_equal, iw.subj_node, s_node
    yield nt.assert_equal, iw.grab_node, g_node
    yield nt.assert_equal, iw.in_node, node1

    iw.connect_inputs()

    g = wf._graph
    yield nt.assert_true, s_node in g.nodes()
    yield nt.assert_true, g_node in g.nodes()
    yield nt.assert_true, (s_node, g_node) in g.edges()

def test_find_mapnodes():

    wf = make_simple_workflow()[0]
    mapnodes = gu.find_mapnodes(wf)
    yield nt.assert_equal, mapnodes, ["node2"]

def test_find_nested_workflows():

    wf, node1, node2, node3 = make_simple_workflow()
    inner_wf = make_simple_workflow()[0]

    wf.connect(node3, "foo", inner_wf, "node1.foo"),

    workflows = gu.find_nested_workflows(wf)

    yield nt.assert_equal, workflows, [inner_wf]

def test_make_subject_source():

    subj_list = ['s1', 's2', 's3']
    node = gu.make_subject_source(subj_list)
    iterable_name, iterable_val = node.iterables
    yield nt.assert_equal, iterable_name, "subject_id"
    yield nt.assert_equal, iterable_val, subj_list

def test_list_out_file():

    class Foo(BaseInterface):

        output_spec = gu.SingleOutFile

        _list_outputs = gu.list_out_file("bar.nii")

    outputs = Foo()._list_outputs()
    nt.assert_equal(outputs["out_file"], op.abspath("bar.nii"))
<commit_msg>Remove tests for deprecated graphutils code<commit_after><|endoftext|>


<commit_before>from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import StaleElementReferenceException

def _wait_for_condition(
    condition, component, message='', driver=None, timeout=10
):
    """Wait until the expected condition is true and return the result"""
    if not driver:
        driver = component._element
    return WebDriverWait(
        driver, timeout).until(condition, message)

class text_to_be_present_in_component(object):
    """ An expectation for checking if the given text is present in the
    provided component.
    """
    def __init__(self, component, text_):
        self.component = component
        self.text = text_

    def __call__(self, driver):
        return self.text in self.component._element.text

class component_to_be_clickable(object):
    """ An expectation that checks if the given component is clickable """
    def __init__(self, component):
        self.component = component

    def __call__(self, driver):
        try:
            return (
                self.component._element.is_enabled()
                and self.component._element.is_displayed()
            )
        except StaleElementReferenceException:
            return False
<commit_msg>Update some docstrings<commit_after>"""
    Conditions using Keteparaha components
"""
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import StaleElementReferenceException

def _wait_for_condition(
    condition, component, message='', driver=None, timeout=10
):
    """Wait until the expected condition is true and return the result"""
    if not driver:
        driver = component._element
    return WebDriverWait(
        driver, timeout).until(condition, message)

class text_to_be_present_in_component(object):
    """An expectation for checking if the given text is present in the
    provided component.
    """
    def __init__(self, component, text_):
        self.component = component
        self.text = text_

    def __call__(self, driver):
        return self.text in self.component._element.text

class component_to_be_clickable(object):
    """An expectation that checks if the given component is clickable """
    def __init__(self, component):
        self.component = component

    def __call__(self, driver):
        try:
            return (
                self.component._element.is_enabled()
                and self.component._element.is_displayed()
            )
        except StaleElementReferenceException:
            return False
<|endoftext|>


<commit_before># Copyright 2012-2016 The Meson development 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 sys, stat, traceback, pickle, argparse
import time, datetime
import os.path
from . import environment, interpreter, mesonlib
from . import build
import platform
from . import mlog, coredata
from .mesonlib import MesonException
from .wrap import WrapMode

parser = argparse.ArgumentParser()

default_warning = '1'

def add_builtin_argument(name, **kwargs):
    k = kwargs.get('dest', name.replace('-', '_'))
    c = coredata.get_builtin_option_choices(k)
    b = True if kwargs.get('action', None) in ['store_true', 'store_false'] else False
    h = coredata.get_builtin_option_description(k)
    if not b:
        h = h.rstrip('.') + ' (default: %s).' % coredata.get_builtin_option_default(k)
    if c and not b:
        kwargs['choices'] = c
    parser.add_argument('--' + name, default=coredata.get_builtin_option_default(k), help=h, **kwargs)

add_builtin_argument('prefix')
add_builtin_argument('libdir')
add_builtin_argument('libexecdir')
add_builtin_argument('bindir')
add_builtin_argument('sbindir')
add_builtin_argument('includedir')
add_builtin_argument('datadir')
add_builtin_argument('mandir')
add_builtin_argument('infodir')
add_builtin_argument('localedir')
add_builtin_argument('sysconfdir')
add_builtin_argument('localstatedir')
add_builtin_argument('sharedstatedir')
add_builtin_argument('backend')
add_builtin_argument('buildtype')
add_builtin_argument('strip', action='store_true')
add_builtin_argument('unity')
add_builtin_argument('werror', action='store_true')
add_builtin_argument('layout')
add_builtin_argument('default-library')
add_builtin_argument('warnlevel', dest='warning_level')
add_builtin_argument('stdsplit', action='store_false')
add_builtin_argument('errorlogs', action='store_false')

parser.add_argument('--cross-file', default=None,
                    help='File describing cross compilation environment.')
parser.add_argument('-D', action='append', dest='projectoptions', default=[], metavar="option",
                    help='Set the value of an option, can be used several times to set multiple options.')
parser.add_argument('-v', '--version', action='version',
                    version=coredata.version)
# See the mesonlib.WrapMode enum for documentation
parser.add_argument('--wrap-mode', default=WrapMode.default,
                    type=lambda t: getattr(WrapMode, t), choices=WrapMode,
                    help='Special wrap mode to use')
parser.add_argument('directories', nargs='*')

class MesonApp:

    def __init__(self, dir1, dir2, script_launcher, handshake, options, original_cmd_line_args):
        (self.source_dir, self.build_dir) = self.validate_dirs(dir1, dir2, handshake)
        self.meson_script_launcher = script_launcher
        self.options = options
        self.original_cmd_line_args = original_cmd_line_args

    def has_build_file(self, dirname):
        fname = os.path.join(dirname, environment.build_filename)
        return os.path.exists(fname)

    def validate_core_dirs(self, dir1, dir2):
        ndir1 = os.path.abspath(os.path.realpath(dir1))
        ndir2 = os.path.abspath(os.path.realpath(dir2))
        if not os.path.exists(ndir1):
            os.makedirs(ndir1)
        if not os.path.exists(ndir2):
            os.makedirs(ndir2)
        if not stat.S_ISDIR(os.stat(ndir1).st_mode):
            raise RuntimeError('%s is not a directory' % dir1)
        if not stat.S_ISDIR(os.stat(ndir2).st_mode):
            raise RuntimeError('%s is not a directory' % dir2)
        if os.path.samefile(dir1, dir2):
            raise RuntimeError('Source and build directories must not be the same. Create a pristine build directory.')
        if self.has_build_file(ndir1):
            if self.has_build_file(ndir2):
                raise RuntimeError('Both directories contain a build file %s.' % environment.build_filename)
            return ndir1, ndir2
        if self.has_build_file(ndir2):
            return ndir2, ndir1
        raise RuntimeError('Neither directory contains a build file %s.' % environment.build_filename)

    def validate_dirs(self, dir1, dir2, handshake):
        (src_dir, build_dir) = self.validate_core_dirs(dir1, dir2)
        priv_dir = os.path.join(build_dir, 'meson-private/coredata.dat')
        if os.path.exists(priv_dir):
            if not handshake:
                msg = '''Trying to run Meson on a build directory that has already been configured.
If you want to build it, just run your build command (e.g. ninja) inside the
build directory. Meson will autodetect any changes in your setup and regenerate
itself as required.

If you want to change option values, use the mesonconf tool instead.'''
                raise RuntimeError(msg)
        else:
            if handshake:
                raise RuntimeError('Something went terribly wrong. Please file a bug.')
        return src_dir, build_dir

    def check_pkgconfig_envvar(self, env):
        curvar = os.environ.get('PKG_CONFIG_PATH', '')
        if curvar != env.coredata.pkgconf_envvar:
            mlog.warning('PKG_CONFIG_PATH has changed between invocations from "%s" to "%s".' %
                         (env.coredata.pkgconf_envvar, curvar))
            env.coredata.pkgconf_envvar = curvar

    def generate(self):
        env = environment.Environment(self.source_dir, self.build_dir, self.meson_script_launcher, self.options, self.original_cmd_line_args)
        mlog.initialize(env.get_log_dir())
        mlog.debug('Build started at', datetime.datetime.now().isoformat())
        mlog.debug('Python binary:', sys.executable)
        mlog.debug('Python system:', platform.system())
        mlog.log(mlog.bold('The Meson build system'))
        self.check_pkgconfig_envvar(env)
        mlog.log('Version:', coredata.version)
        mlog.log('Source dir:', mlog.bold(self.source_dir))
        mlog.log('Build dir:', mlog.bold(self.build_dir))
        if env.is_cross_build():
            mlog.log('Build type:', mlog.bold('cross build'))
        else:
            mlog.log('Build type:', mlog.bold('native build'))
        b = build.Build(env)
        if self.options.backend == 'ninja':
            from .backend import ninjabackend
            g = ninjabackend.NinjaBackend(b)
        elif self.options.backend == 'vs':
            from .backend import vs2010backend
            g = vs2010backend.autodetect_vs_version(b)
            mlog.log('Auto detected Visual Studio backend:', mlog.bold(g.name))
        elif self.options.backend == 'vs2010':
            from .backend import vs2010backend
            g = vs2010backend.Vs2010Backend(b)
        elif self.options.backend == 'vs2015':
            from .backend import vs2015backend
            g = vs2015backend.Vs2015Backend(b)
        elif self.options.backend == 'vs2017':
            from .backend import vs2017backend
            g = vs2017backend.Vs2017Backend(b)
        elif self.options.backend == 'xcode':
            from .backend import xcodebackend
            g = xcodebackend.XCodeBackend(b)
        else:
            raise RuntimeError('Unknown backend "%s".' % self.options.backend)

        intr = interpreter.Interpreter(b, g)
        if env.is_cross_build():
            mlog.log('Host machine cpu family:', mlog.bold(intr.builtin['host_machine'].cpu_family_method([], {})))
            mlog.log('Host machine cpu:', mlog.bold(intr.builtin['host_machine'].cpu_method([], {})))
            mlog.log('Target machine cpu family:', mlog.bold(intr.builtin['target_machine'].cpu_family_method([], {})))
            mlog.log('Target machine cpu:', mlog.bold(intr.builtin['target_machine'].cpu_method([], {})))
        mlog.log('Build machine cpu family:', mlog.bold(intr.builtin['build_machine'].cpu_family_method([], {})))
        mlog.log('Build machine cpu:', mlog.bold(intr.builtin['build_machine'].cpu_method([], {})))
        intr.run()
        coredata_mtime = time.time()
        g.generate(intr)
        g.run_postconf_scripts()
        dumpfile = os.path.join(env.get_scratch_dir(), 'build.dat')
        with open(dumpfile, 'wb') as f:
            pickle.dump(b, f)
        # Write this last since we use the existence of this file to check if
        # we generated the build file successfully, so we don't want an error
        # that pops up during generation, post-conf scripts, etc to cause us to
        # incorrectly signal a successful meson run which will cause an error
        # about an already-configured build directory when the user tries again.
        #
        # However, we set the mtime to an earlier value to ensure that doing an
        # mtime comparison between the coredata dump and other build files
        # shows the build files to be newer, not older.
        env.dump_coredata(coredata_mtime)

def run_script_command(args):
    cmdname = args[0]
    cmdargs = args[1:]
    if cmdname == 'exe':
        import mesonbuild.scripts.meson_exe as abc
        cmdfunc = abc.run
    elif cmdname == 'cleantrees':
        import mesonbuild.scripts.cleantrees as abc
        cmdfunc = abc.run
    elif cmdname == 'install':
        import mesonbuild.scripts.meson_install as abc
        cmdfunc = abc.run
    elif cmdname == 'commandrunner':
        import mesonbuild.scripts.commandrunner as abc
        cmdfunc = abc.run
    elif cmdname == 'delsuffix':
        import mesonbuild.scripts.delwithsuffix as abc
        cmdfunc = abc.run
    elif cmdname == 'depfixer':
        import mesonbuild.scripts.depfixer as abc
        cmdfunc = abc.run
    elif cmdname == 'dirchanger':
        import mesonbuild.scripts.dirchanger as abc
        cmdfunc = abc.run
    elif cmdname == 'gtkdoc':
        import mesonbuild.scripts.gtkdochelper as abc
        cmdfunc = abc.run
    elif cmdname == 'msgfmthelper':
        import mesonbuild.scripts.msgfmthelper as abc
        cmdfunc = abc.run
    elif cmdname == 'regencheck':
        import mesonbuild.scripts.regen_checker as abc
        cmdfunc = abc.run
    elif cmdname == 'symbolextractor':
        import mesonbuild.scripts.symbolextractor as abc
        cmdfunc = abc.run
    elif cmdname == 'scanbuild':
        import mesonbuild.scripts.scanbuild as abc
        cmdfunc = abc.run
    elif cmdname == 'vcstagger':
        import mesonbuild.scripts.vcstagger as abc
        cmdfunc = abc.run
    elif cmdname == 'gettext':
        import mesonbuild.scripts.gettext as abc
        cmdfunc = abc.run
    elif cmdname == 'yelphelper':
        import mesonbuild.scripts.yelphelper as abc
        cmdfunc = abc.run
    elif cmdname == 'uninstall':
        import mesonbuild.scripts.uninstall as abc
        cmdfunc = abc.run
    elif cmdname == 'dist':
        import mesonbuild.scripts.dist as abc
        cmdfunc = abc.run
    elif cmdname == 'coverage':
        import mesonbuild.scripts.coverage as abc
        cmdfunc = abc.run
    else:
        raise MesonException('Unknown internal command {}.'.format(cmdname))
    return cmdfunc(cmdargs)

def run(mainfile, args):
    if sys.version_info < (3, 3):
        print('Meson works correctly only with python 3.3+.')
        print('You have python %s.' % sys.version)
        print('Please update your environment')
        return 1
    if len(args) >= 2 and args[0] == '--internal':
        if args[1] != 'regenerate':
            script = args[1]
            try:
                sys.exit(run_script_command(args[1:]))
            except MesonException as e:
                mlog.log(mlog.red('\nError in {} helper script:'.format(script)))
                mlog.log(e)
                sys.exit(1)
        args = args[2:]
        handshake = True
    else:
        handshake = False
    args = mesonlib.expand_arguments(args)
    options = parser.parse_args(args)
    args = options.directories
    if not args or len(args) > 2:
        # if there's a meson.build in the dir above, and not in the current
        # directory, assume we're in the build directory
        if not args and not os.path.exists('meson.build') and os.path.exists('../meson.build'):
            dir1 = '..'
            dir2 = '.'
        else:
            print('{} <source directory> <build directory>'.format(sys.argv[0]))
            print('If you omit either directory, the current directory is substituted.')
            print('Run {} --help for more information.'.format(sys.argv[0]))
            return 1
    else:
        dir1 = args[0]
        if len(args) > 1:
            dir2 = args[1]
        else:
            dir2 = '.'
    try:
        app = MesonApp(dir1, dir2, mainfile, handshake, options, sys.argv)
    except Exception as e:
        # Log directory does not exist, so just print
        # to stdout.
        print('Error during basic setup:\n')
        print(e)
        return 1
    try:
        app.generate()
    except Exception as e:
        if isinstance(e, MesonException):
            if hasattr(e, 'file') and hasattr(e, 'lineno') and hasattr(e, 'colno'):
                mlog.log(mlog.red('\nMeson encountered an error in file %s, line %d, column %d:' % (e.file, e.lineno, e.colno)))
            else:
                mlog.log(mlog.red('\nMeson encountered an error:'))
            mlog.log(e)
        else:
            traceback.print_exc()
        return 1
    return 0
<commit_msg>Add an env var to force meson to print a backtrace<commit_after># Copyright 2012-2016 The Meson development 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 sys, stat, traceback, pickle, argparse
import time, datetime
import os.path
from . import environment, interpreter, mesonlib
from . import build
import platform
from . import mlog, coredata
from .mesonlib import MesonException
from .wrap import WrapMode

parser = argparse.ArgumentParser()

default_warning = '1'

def add_builtin_argument(name, **kwargs):
    k = kwargs.get('dest', name.replace('-', '_'))
    c = coredata.get_builtin_option_choices(k)
    b = True if kwargs.get('action', None) in ['store_true', 'store_false'] else False
    h = coredata.get_builtin_option_description(k)
    if not b:
        h = h.rstrip('.') + ' (default: %s).' % coredata.get_builtin_option_default(k)
    if c and not b:
        kwargs['choices'] = c
    parser.add_argument('--' + name, default=coredata.get_builtin_option_default(k), help=h, **kwargs)

add_builtin_argument('prefix')
add_builtin_argument('libdir')
add_builtin_argument('libexecdir')
add_builtin_argument('bindir')
add_builtin_argument('sbindir')
add_builtin_argument('includedir')
add_builtin_argument('datadir')
add_builtin_argument('mandir')
add_builtin_argument('infodir')
add_builtin_argument('localedir')
add_builtin_argument('sysconfdir')
add_builtin_argument('localstatedir')
add_builtin_argument('sharedstatedir')
add_builtin_argument('backend')
add_builtin_argument('buildtype')
add_builtin_argument('strip', action='store_true')
add_builtin_argument('unity')
add_builtin_argument('werror', action='store_true')
add_builtin_argument('layout')
add_builtin_argument('default-library')
add_builtin_argument('warnlevel', dest='warning_level')
add_builtin_argument('stdsplit', action='store_false')
add_builtin_argument('errorlogs', action='store_false')

parser.add_argument('--cross-file', default=None,
                    help='File describing cross compilation environment.')
parser.add_argument('-D', action='append', dest='projectoptions', default=[], metavar="option",
                    help='Set the value of an option, can be used several times to set multiple options.')
parser.add_argument('-v', '--version', action='version',
                    version=coredata.version)
# See the mesonlib.WrapMode enum for documentation
parser.add_argument('--wrap-mode', default=WrapMode.default,
                    type=lambda t: getattr(WrapMode, t), choices=WrapMode,
                    help='Special wrap mode to use')
parser.add_argument('directories', nargs='*')

class MesonApp:

    def __init__(self, dir1, dir2, script_launcher, handshake, options, original_cmd_line_args):
        (self.source_dir, self.build_dir) = self.validate_dirs(dir1, dir2, handshake)
        self.meson_script_launcher = script_launcher
        self.options = options
        self.original_cmd_line_args = original_cmd_line_args

    def has_build_file(self, dirname):
        fname = os.path.join(dirname, environment.build_filename)
        return os.path.exists(fname)

    def validate_core_dirs(self, dir1, dir2):
        ndir1 = os.path.abspath(os.path.realpath(dir1))
        ndir2 = os.path.abspath(os.path.realpath(dir2))
        if not os.path.exists(ndir1):
            os.makedirs(ndir1)
        if not os.path.exists(ndir2):
            os.makedirs(ndir2)
        if not stat.S_ISDIR(os.stat(ndir1).st_mode):
            raise RuntimeError('%s is not a directory' % dir1)
        if not stat.S_ISDIR(os.stat(ndir2).st_mode):
            raise RuntimeError('%s is not a directory' % dir2)
        if os.path.samefile(dir1, dir2):
            raise RuntimeError('Source and build directories must not be the same. Create a pristine build directory.')
        if self.has_build_file(ndir1):
            if self.has_build_file(ndir2):
                raise RuntimeError('Both directories contain a build file %s.' % environment.build_filename)
            return ndir1, ndir2
        if self.has_build_file(ndir2):
            return ndir2, ndir1
        raise RuntimeError('Neither directory contains a build file %s.' % environment.build_filename)

    def validate_dirs(self, dir1, dir2, handshake):
        (src_dir, build_dir) = self.validate_core_dirs(dir1, dir2)
        priv_dir = os.path.join(build_dir, 'meson-private/coredata.dat')
        if os.path.exists(priv_dir):
            if not handshake:
                msg = '''Trying to run Meson on a build directory that has already been configured.
If you want to build it, just run your build command (e.g. ninja) inside the
build directory. Meson will autodetect any changes in your setup and regenerate
itself as required.

If you want to change option values, use the mesonconf tool instead.'''
                raise RuntimeError(msg)
        else:
            if handshake:
                raise RuntimeError('Something went terribly wrong. Please file a bug.')
        return src_dir, build_dir

    def check_pkgconfig_envvar(self, env):
        curvar = os.environ.get('PKG_CONFIG_PATH', '')
        if curvar != env.coredata.pkgconf_envvar:
            mlog.warning('PKG_CONFIG_PATH has changed between invocations from "%s" to "%s".' %
                         (env.coredata.pkgconf_envvar, curvar))
            env.coredata.pkgconf_envvar = curvar

    def generate(self):
        env = environment.Environment(self.source_dir, self.build_dir, self.meson_script_launcher, self.options, self.original_cmd_line_args)
        mlog.initialize(env.get_log_dir())
        mlog.debug('Build started at', datetime.datetime.now().isoformat())
        mlog.debug('Python binary:', sys.executable)
        mlog.debug('Python system:', platform.system())
        mlog.log(mlog.bold('The Meson build system'))
        self.check_pkgconfig_envvar(env)
        mlog.log('Version:', coredata.version)
        mlog.log('Source dir:', mlog.bold(self.source_dir))
        mlog.log('Build dir:', mlog.bold(self.build_dir))
        if env.is_cross_build():
            mlog.log('Build type:', mlog.bold('cross build'))
        else:
            mlog.log('Build type:', mlog.bold('native build'))
        b = build.Build(env)
        if self.options.backend == 'ninja':
            from .backend import ninjabackend
            g = ninjabackend.NinjaBackend(b)
        elif self.options.backend == 'vs':
            from .backend import vs2010backend
            g = vs2010backend.autodetect_vs_version(b)
            mlog.log('Auto detected Visual Studio backend:', mlog.bold(g.name))
        elif self.options.backend == 'vs2010':
            from .backend import vs2010backend
            g = vs2010backend.Vs2010Backend(b)
        elif self.options.backend == 'vs2015':
            from .backend import vs2015backend
            g = vs2015backend.Vs2015Backend(b)
        elif self.options.backend == 'vs2017':
            from .backend import vs2017backend
            g = vs2017backend.Vs2017Backend(b)
        elif self.options.backend == 'xcode':
            from .backend import xcodebackend
            g = xcodebackend.XCodeBackend(b)
        else:
            raise RuntimeError('Unknown backend "%s".' % self.options.backend)

        intr = interpreter.Interpreter(b, g)
        if env.is_cross_build():
            mlog.log('Host machine cpu family:', mlog.bold(intr.builtin['host_machine'].cpu_family_method([], {})))
            mlog.log('Host machine cpu:', mlog.bold(intr.builtin['host_machine'].cpu_method([], {})))
            mlog.log('Target machine cpu family:', mlog.bold(intr.builtin['target_machine'].cpu_family_method([], {})))
            mlog.log('Target machine cpu:', mlog.bold(intr.builtin['target_machine'].cpu_method([], {})))
        mlog.log('Build machine cpu family:', mlog.bold(intr.builtin['build_machine'].cpu_family_method([], {})))
        mlog.log('Build machine cpu:', mlog.bold(intr.builtin['build_machine'].cpu_method([], {})))
        intr.run()
        coredata_mtime = time.time()
        g.generate(intr)
        g.run_postconf_scripts()
        dumpfile = os.path.join(env.get_scratch_dir(), 'build.dat')
        with open(dumpfile, 'wb') as f:
            pickle.dump(b, f)
        # Write this last since we use the existence of this file to check if
        # we generated the build file successfully, so we don't want an error
        # that pops up during generation, post-conf scripts, etc to cause us to
        # incorrectly signal a successful meson run which will cause an error
        # about an already-configured build directory when the user tries again.
        #
        # However, we set the mtime to an earlier value to ensure that doing an
        # mtime comparison between the coredata dump and other build files
        # shows the build files to be newer, not older.
        env.dump_coredata(coredata_mtime)

def run_script_command(args):
    cmdname = args[0]
    cmdargs = args[1:]
    if cmdname == 'exe':
        import mesonbuild.scripts.meson_exe as abc
        cmdfunc = abc.run
    elif cmdname == 'cleantrees':
        import mesonbuild.scripts.cleantrees as abc
        cmdfunc = abc.run
    elif cmdname == 'install':
        import mesonbuild.scripts.meson_install as abc
        cmdfunc = abc.run
    elif cmdname == 'commandrunner':
        import mesonbuild.scripts.commandrunner as abc
        cmdfunc = abc.run
    elif cmdname == 'delsuffix':
        import mesonbuild.scripts.delwithsuffix as abc
        cmdfunc = abc.run
    elif cmdname == 'depfixer':
        import mesonbuild.scripts.depfixer as abc
        cmdfunc = abc.run
    elif cmdname == 'dirchanger':
        import mesonbuild.scripts.dirchanger as abc
        cmdfunc = abc.run
    elif cmdname == 'gtkdoc':
        import mesonbuild.scripts.gtkdochelper as abc
        cmdfunc = abc.run
    elif cmdname == 'msgfmthelper':
        import mesonbuild.scripts.msgfmthelper as abc
        cmdfunc = abc.run
    elif cmdname == 'regencheck':
        import mesonbuild.scripts.regen_checker as abc
        cmdfunc = abc.run
    elif cmdname == 'symbolextractor':
        import mesonbuild.scripts.symbolextractor as abc
        cmdfunc = abc.run
    elif cmdname == 'scanbuild':
        import mesonbuild.scripts.scanbuild as abc
        cmdfunc = abc.run
    elif cmdname == 'vcstagger':
        import mesonbuild.scripts.vcstagger as abc
        cmdfunc = abc.run
    elif cmdname == 'gettext':
        import mesonbuild.scripts.gettext as abc
        cmdfunc = abc.run
    elif cmdname == 'yelphelper':
        import mesonbuild.scripts.yelphelper as abc
        cmdfunc = abc.run
    elif cmdname == 'uninstall':
        import mesonbuild.scripts.uninstall as abc
        cmdfunc = abc.run
    elif cmdname == 'dist':
        import mesonbuild.scripts.dist as abc
        cmdfunc = abc.run
    elif cmdname == 'coverage':
        import mesonbuild.scripts.coverage as abc
        cmdfunc = abc.run
    else:
        raise MesonException('Unknown internal command {}.'.format(cmdname))
    return cmdfunc(cmdargs)

def run(mainfile, args):
    if sys.version_info < (3, 3):
        print('Meson works correctly only with python 3.3+.')
        print('You have python %s.' % sys.version)
        print('Please update your environment')
        return 1
    if len(args) >= 2 and args[0] == '--internal':
        if args[1] != 'regenerate':
            script = args[1]
            try:
                sys.exit(run_script_command(args[1:]))
            except MesonException as e:
                mlog.log(mlog.red('\nError in {} helper script:'.format(script)))
                mlog.log(e)
                sys.exit(1)
        args = args[2:]
        handshake = True
    else:
        handshake = False
    args = mesonlib.expand_arguments(args)
    options = parser.parse_args(args)
    args = options.directories
    if not args or len(args) > 2:
        # if there's a meson.build in the dir above, and not in the current
        # directory, assume we're in the build directory
        if not args and not os.path.exists('meson.build') and os.path.exists('../meson.build'):
            dir1 = '..'
            dir2 = '.'
        else:
            print('{} <source directory> <build directory>'.format(sys.argv[0]))
            print('If you omit either directory, the current directory is substituted.')
            print('Run {} --help for more information.'.format(sys.argv[0]))
            return 1
    else:
        dir1 = args[0]
        if len(args) > 1:
            dir2 = args[1]
        else:
            dir2 = '.'
    try:
        app = MesonApp(dir1, dir2, mainfile, handshake, options, sys.argv)
    except Exception as e:
        # Log directory does not exist, so just print
        # to stdout.
        print('Error during basic setup:\n')
        print(e)
        return 1
    try:
        app.generate()
    except Exception as e:
        if isinstance(e, MesonException):
            if hasattr(e, 'file') and hasattr(e, 'lineno') and hasattr(e, 'colno'):
                mlog.log(mlog.red('\nMeson encountered an error in file %s, line %d, column %d:' % (e.file, e.lineno, e.colno)))
            else:
                mlog.log(mlog.red('\nMeson encountered an error:'))
            mlog.log(e)
            if os.environ.get('MESON_FORCE_BACKTRACE'):
                raise
        else:
            if os.environ.get('MESON_FORCE_BACKTRACE'):
                raise
            traceback.print_exc()
        return 1
    return 0
<|endoftext|>


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

import itertools

import numpy
import array
import fastcluster
import hcluster

TYPES = { type(1) : 'l',
          type('a') : 'c',
          type(u'a') : 'u'}

def connected_components(edgelist) :
    root = {}
    component = {}
    indices = {}

    for i, edge in enumerate(edgelist['pairs']) :
        (a, b) = edge
        edge = a, b
        root_a = root.get(a)
        root_b = root.get(b)

        if root_a is None and root_b is None :
            component[a] = set([a, b])
            indices[a] = [i]
            root[a] = root[b] = a
        elif root_a is None or root_b is None :
            if root_a is None :
                a, b = b, a
                root_a, root_b = root_b, root_a
            component[root_a].add(b)
            indices[root_a].append(i)
            root[b] = root_a
        elif root_a != root_b :
            component_a = component[root_a]
            component_b = component[root_b]
            if len(component_a) < len(component_b) :
                root_a, root_b = root_b, root_a
                component_a, component_b = component_b, component_a

            component_a |= component_b
            indices[root_a].extend(indices[root_b])

            for node in component_b :
                root[node] = root_a

            del component[root_b]
            del indices[root_b]
        else :
            indices[root_a].append(i)

    for sub_graph in indices.values() :
        yield edgelist[sub_graph]

def condensedDistance(dupes):
    '''
    Convert the pairwise list of distances in dupes to "condensed
    distance matrix" required by the hierarchical clustering
    algorithms. Also return a dictionary that maps the distance matrix
    to the record_ids.

    The formula for an index of the condensed matrix is

    index = {N choose 2}-{N-row choose 2} + (col-row-1)
          = N*(N-1)/2 - (N-row)*(N-row-1)/2 + col - row - 1
            ^^^^^^^^^   ^^^^^^^^^^^^^^^^^^^
          matrix_length       row_step

    where (row,col) is index of an uncondensed square N X N distance matrix.

    See http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.squareform.html
    '''

    candidate_set = numpy.unique(dupes['pairs'])
    candidate_set = numpy.sort(candidate_set)

    i_to_id = dict(enumerate(candidate_set))

    ids = candidate_set.searchsorted(dupes['pairs'])
    row = ids[:, 0]
    col = ids[:, 1]

    N = len(numpy.union1d(row, col))
    matrix_length = N * (N - 1) / 2

    row_step = (N - row) * (N - row - 1) / 2
    index = matrix_length - row_step + col - row - 1

    condensed_distances = numpy.ones(matrix_length, 'f4')
    condensed_distances[index] = 1 - dupes['score']

    return (i_to_id, condensed_distances)

def cluster(dupes, threshold=.5):
    '''
    Takes in a list of duplicate pairs and clusters them in to a
    list records that all refer to the same entity based on a given
    threshold

    Keyword arguments:
    threshold -- number betweent 0 and 1 (default is .5). lowering the
                 number will increase precision, raising it will increase
                 recall
    '''
    threshold = 1 - threshold

    dupe_sub_graphs = connected_components(dupes)

    clustering = {}
    cluster_id = 0
    for sub_graph in dupe_sub_graphs:
        if len(sub_graph) > 1:

            (i_to_id, condensed_distances) = condensedDistance(sub_graph)
            N = max(i_to_id) + 1

            linkage = fastcluster.linkage(condensed_distances,
                                          method='centroid',
                                          preserve_input=False)

            partition = hcluster.fcluster(linkage,
                                          threshold,
                                          criterion='distance')

            clusters = {}

            for (i, sub_cluster_id) in enumerate(partition):
                clusters.setdefault(cluster_id + sub_cluster_id, []).append(i)

            cophenetic_distances = hcluster.cophenet(linkage)

            for cluster_id, items in clusters.iteritems() :
                if len(items) > 1 :
                    score = clusterConfidence(items, cophenetic_distances, N)
                    clustering[cluster_id] = (tuple(i_to_id[item]
                                                    for item in items),
                                              1 - score)

            cluster_id += max(partition) + 1
        else:
            ids, score = sub_graph[0]
            clustering[cluster_id] = tuple(ids), score
            cluster_id += 1

    return clustering.values()

def clusterConfidence(items, cophenetic_distances, N) :
    max_score = 0
    i, other_items = items[0], items[1:]
    condensor = (N * (N-1))/2 - ((N-i)*(N-i-1))/2 - i - 1
    for j in other_items :
        ij =  condensor + j
        score = cophenetic_distances[ij]
        if score > max_score :
            max_score = score

    return max_score

def greedyMatching(dupes, threshold=0.5):
    covered_vertex_A = set([])
    covered_vertex_B = set([])
    clusters = []

    sorted_dupes = sorted(dupes, key=lambda score: score[1], reverse=True)
    dupes_list = [dupe for dupe in sorted_dupes if dupe[1] >= threshold]

    for dupe in dupes_list:
        vertices = dupe[0]
        if vertices[0] not in covered_vertex_A and vertices[1] not in covered_vertex_B:
            clusters.append(dupe)
            covered_vertex_A.update([vertices[0]])
            covered_vertex_B.update([vertices[1]])

    return clusters

def gazetteMatching(dupes, threshold=0.5, n=1):
    clusters = []

    sorted_dupes = sorted(dupes, key=lambda pair: (pair[0][0], -pair[1]))
    dupes_list = [dupe for dupe in sorted_dupes if dupe[1] >= threshold]

    if dupes_list :
        group = dupes_list[0][0][0]
        matches = []
        i = 0

        for pair in dupes_list:
            a, b = pair[0]
            if a == group :
                if i < n :
                    matches.append(pair)
                    i += 1
            else :
                clusters.append(tuple(matches))
                matches = [pair]
                i = 1
                group = a

        clusters.append(tuple(matches))

    return clusters
<commit_msg>clean up some dead code<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

import itertools

import numpy
import fastcluster
import hcluster

def connected_components(edgelist) :
    root = {}
    component = {}
    indices = {}

    for i, edge in enumerate(edgelist['pairs']) :
        (a, b) = edge
        edge = a, b
        root_a = root.get(a)
        root_b = root.get(b)

        if root_a is None and root_b is None :
            component[a] = set([a, b])
            indices[a] = [i]
            root[a] = root[b] = a
        elif root_a is None or root_b is None :
            if root_a is None :
                a, b = b, a
                root_a, root_b = root_b, root_a
            component[root_a].add(b)
            indices[root_a].append(i)
            root[b] = root_a
        elif root_a != root_b :
            component_a = component[root_a]
            component_b = component[root_b]
            if len(component_a) < len(component_b) :
                root_a, root_b = root_b, root_a
                component_a, component_b = component_b, component_a

            component_a |= component_b
            indices[root_a].extend(indices[root_b])

            for node in component_b :
                root[node] = root_a

            del component[root_b]
            del indices[root_b]
        else :
            indices[root_a].append(i)

    for sub_graph in indices.values() :
        yield edgelist[sub_graph]

def condensedDistance(dupes):
    '''
    Convert the pairwise list of distances in dupes to "condensed
    distance matrix" required by the hierarchical clustering
    algorithms. Also return a dictionary that maps the distance matrix
    to the record_ids.

    The formula for an index of the condensed matrix is

    index = {N choose 2}-{N-row choose 2} + (col-row-1)
          = N*(N-1)/2 - (N-row)*(N-row-1)/2 + col - row - 1
            ^^^^^^^^^   ^^^^^^^^^^^^^^^^^^^
          matrix_length       row_step

    where (row,col) is index of an uncondensed square N X N distance matrix.

    See http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.squareform.html
    '''

    candidate_set = numpy.unique(dupes['pairs'])
    candidate_set = numpy.sort(candidate_set)

    i_to_id = dict(enumerate(candidate_set))

    ids = candidate_set.searchsorted(dupes['pairs'])
    row = ids[:, 0]
    col = ids[:, 1]

    N = len(numpy.union1d(row, col))
    matrix_length = N * (N - 1) / 2

    row_step = (N - row) * (N - row - 1) / 2
    index = matrix_length - row_step + col - row - 1

    condensed_distances = numpy.ones(matrix_length, 'f4')
    condensed_distances[index] = 1 - dupes['score']

    return (i_to_id, condensed_distances)

def cluster(dupes, threshold=.5):
    '''
    Takes in a list of duplicate pairs and clusters them in to a
    list records that all refer to the same entity based on a given
    threshold

    Keyword arguments:
    threshold -- number betweent 0 and 1 (default is .5). lowering the
                 number will increase precision, raising it will increase
                 recall
    '''
    threshold = 1 - threshold

    dupe_sub_graphs = connected_components(dupes)

    clustering = {}
    cluster_id = 0
    for sub_graph in dupe_sub_graphs:
        if len(sub_graph) > 1:

            (i_to_id, condensed_distances) = condensedDistance(sub_graph)
            N = max(i_to_id) + 1

            linkage = fastcluster.linkage(condensed_distances,
                                          method='centroid',
                                          preserve_input=False)

            partition = hcluster.fcluster(linkage,
                                          threshold,
                                          criterion='distance')

            clusters = {}

            for (i, sub_cluster_id) in enumerate(partition):
                clusters.setdefault(cluster_id + sub_cluster_id, []).append(i)

            cophenetic_distances = hcluster.cophenet(linkage)

            for cluster_id, items in clusters.iteritems() :
                if len(items) > 1 :
                    score = clusterConfidence(items, cophenetic_distances, N)
                    clustering[cluster_id] = (tuple(i_to_id[item]
                                                    for item in items),
                                              1 - score)

            cluster_id += max(partition) + 1
        else:
            ids, score = sub_graph[0]
            clustering[cluster_id] = tuple(ids), score
            cluster_id += 1

    return clustering.values()

def clusterConfidence(items, cophenetic_distances, N) :
    max_score = 0
    i, other_items = items[0], items[1:]
    condensor = (N * (N-1))/2 - ((N-i)*(N-i-1))/2 - i - 1
    for j in other_items :
        ij =  condensor + j
        score = cophenetic_distances[ij]
        if score > max_score :
            max_score = score

    return max_score

def greedyMatching(dupes, threshold=0.5):
    covered_vertex_A = set([])
    covered_vertex_B = set([])
    clusters = []

    sorted_dupes = sorted(dupes, key=lambda score: score[1], reverse=True)
    dupes_list = [dupe for dupe in sorted_dupes if dupe[1] >= threshold]

    for dupe in dupes_list:
        vertices = dupe[0]
        if vertices[0] not in covered_vertex_A and vertices[1] not in covered_vertex_B:
            clusters.append(dupe)
            covered_vertex_A.update([vertices[0]])
            covered_vertex_B.update([vertices[1]])

    return clusters

def gazetteMatching(dupes, threshold=0.5, n=1):
    clusters = []

    sorted_dupes = sorted(dupes, key=lambda pair: (pair[0][0], -pair[1]))
    dupes_list = [dupe for dupe in sorted_dupes if dupe[1] >= threshold]

    if dupes_list :
        group = dupes_list[0][0][0]
        matches = []
        i = 0

        for pair in dupes_list:
            a, b = pair[0]
            if a == group :
                if i < n :
                    matches.append(pair)
                    i += 1
            else :
                clusters.append(tuple(matches))
                matches = [pair]
                i = 1
                group = a

        clusters.append(tuple(matches))

    return clusters
<|endoftext|>


<commit_before>import os
import subprocess
import difflib
import fnmatch
import re
import gitnb.config as con

def rglob(match='*',root='.',exclude_dirs=[]):
    """ Recursive Glob
        Args:
            match: <str> string to match
            root: <str> dir to start recursive search
            exclude: <list[str]> list of directories to skip
    """
    matches = []
    for froot, _, filenames in os.walk(root):
        if not any(xdir in froot for xdir in exclude_dirs):
            for filename in fnmatch.filter(filenames, match):
                matches.append(os.path.join(froot, filename))
    return matches

def copy_append(input_path,output_path,open_type=None):
    """ COPY OR APPEND input_path to output_path
    """
    if not open_type:
        if os.path.isfile(output_path): open_type='a'
        else: open_type='w'
    with open(output_path,open_type) as output_file:
        with open(input_path,'r') as input_file:
            output_file.write(input_file.read())

def git_add(path):
    """ GIT ADD FILE
    """
    safe_path="'{}'".format(path)
    cmd=' '.join(['git add',safe_path])
    subprocess.check_output(cmd, shell=True)

def truthy(value):
    """ Stringy Truthyness
    """
    value=str(value).lower().strip(' ')
    if value in ['none','false','0','nope','','[]']:
        return False
    else:
        return True

def read_lines(path):
    """ Read file
        return lines list
    """
    with open(path,'r') as file:
        lines=file.readlines()
    return lines

import subprocess

def nb_matching_lines(grep,path):
    """ count lines without match in file
        returns int
    """
    safe_path="'{}'".format(path)
    cmd="cat {} | grep {} | wc -l".format(safe_path,grep)
    out=subprocess.check_output(cmd, shell=True)
    return int(out.strip(' ').strip('\n'))

def remove_lines(ngrep,path,bak_ext='bak'):
    """ Removes Line with matching ngrep
        - creates a backup path.bak_ext
    """
    # backup file
    safe_path="'{}'".format(path)
    safe_path_bak="'{}.{}'".format(path,bak_ext)
    cmd1=' '.join(['cp',safe_path,safe_path_bak])
    subprocess.check_output(cmd1, shell=True)
    # backup file
    cmd2="cat {} | grep -v {} > {}".format(safe_path_bak,ngrep,safe_path)
    subprocess.check_output(cmd2, shell=True)

def mkdirs(path):
    """ Make parent dirs if they dont exist
    """
    if not os.path.exists(os.path.dirname(path)):
        nb_dir=os.path.dirname(path)
        if truthy(nb_dir):
            try:
                os.makedirs(os.path.dirname(path))
            except OSError as exc:
                if exc.errno != errno.EEXIST:
                    raise

<commit_msg>count lines - no subprocess: was getting error b/c of byte str<commit_after>import os
import subprocess
import difflib
import fnmatch
import re
import gitnb.config as con

def rglob(match='*',root='.',exclude_dirs=[]):
    """ Recursive Glob
        Args:
            match: <str> string to match
            root: <str> dir to start recursive search
            exclude: <list[str]> list of directories to skip
    """
    matches = []
    for froot, _, filenames in os.walk(root):
        if not any(xdir in froot for xdir in exclude_dirs):
            for filename in fnmatch.filter(filenames, match):
                matches.append(os.path.join(froot, filename))
    return matches

def copy_append(input_path,output_path,open_type=None):
    """ COPY OR APPEND input_path to output_path
    """
    if not open_type:
        if os.path.isfile(output_path): open_type='a'
        else: open_type='w'
    with open(output_path,open_type) as output_file:
        with open(input_path,'r') as input_file:
            output_file.write(input_file.read())

def git_add(path):
    """ GIT ADD FILE
    """
    safe_path="'{}'".format(path)
    cmd=' '.join(['git add',safe_path])
    subprocess.check_output(cmd, shell=True)

def truthy(value):
    """ Stringy Truthyness
    """
    value=str(value).lower().strip(' ')
    if value in ['none','false','0','nope','','[]']:
        return False
    else:
        return True

def read_lines(path):
    """ Read file
        return lines list
    """
    with open(path,'r') as file:
        lines=file.readlines()
    return lines

import subprocess

def nb_matching_lines(grep,path):
    """ count lines without match in file
        returns int
    """
    lines=readlines(path)
    lines=[line for line in lines if grep in line]
    return len(lines)
    # safe_path="'{}'".format(path)
    # cmd="cat {} | grep {} | wc -l".format(safe_path,grep)
    # out=subprocess.check_output(cmd, shell=True)
    # return int(out.strip(' ').strip('\n'))

def remove_lines(ngrep,path,bak_ext='bak'):
    """ Removes Line with matching ngrep
        - creates a backup path.bak_ext
    """
    # backup file
    safe_path="'{}'".format(path)
    safe_path_bak="'{}.{}'".format(path,bak_ext)
    cmd1=' '.join(['cp',safe_path,safe_path_bak])
    subprocess.check_output(cmd1, shell=True)
    # backup file
    cmd2="cat {} | grep -v {} > {}".format(safe_path_bak,ngrep,safe_path)
    subprocess.check_output(cmd2, shell=True)

def mkdirs(path):
    """ Make parent dirs if they dont exist
    """
    if not os.path.exists(os.path.dirname(path)):
        nb_dir=os.path.dirname(path)
        if truthy(nb_dir):
            try:
                os.makedirs(os.path.dirname(path))
            except OSError as exc:
                if exc.errno != errno.EEXIST:
                    raise

<|endoftext|>


<commit_before>#!/usr/bin/env python
import os
import subprocess as subp
from avocado import Test
from utils import utils

'''
This test will ping a specific device. The device needs to be specified in the
YAML file. If the device responds, the result will be found in the debug log.
If the test fails, a fail exception will be raised.
'''
class BluetoothPing(Test):
    def test(self):
      testdata = utils.load_yaml(self, "data/bluetooth_data.yaml")
      self.targetDeviceMac = testdata['testdata']['addr']

      p = subp.Popen(['sudo', 'l2ping', self.targetDeviceMac ,'-c', '5'], stdout=subp.PIPE, stderr=subp.PIPE)
      result = p.communicate()[0]
      returnCode = p.returncode

      if returnCode != 0 :
          self.fail("Could not ping " + self.targetDeviceMac)

     self.log.debug("Ping test working {0}".format(result))
<commit_msg>Update bluetooth_ping.py<commit_after>#!/usr/bin/env python
import os
import subprocess as subp
from avocado import Test
from utils import utils

'''
This test will ping a specific device. The device needs to be specified in the
YAML file. If the device responds, the result will be found in the debug log.
If the test fails, a fail exception will be raised.
'''
class BluetoothPing(Test):
    def test(self):
      testdata = utils.load_yaml(self, "data/bluetooth_data.yaml")
      self.targetDeviceMac = testdata['testdata']['addr']

      p = pingtest()

      if p != 0 :
          self.fail("Could not ping " + self.targetDeviceMac)

     self.log.debug("Ping test working {0}".format(result))

    def pingtest(self)
        p= subp.Popen(['sudo', 'l2ping', self.targetDeviceMac ,'-c', '5'], stdout=subp.PIPE, stderr=subp.PIPE)
        result = p.communicate()[0]
        returnCode = p.returncode
        return returnCode
<|endoftext|>