<commit_before>import unittest
from srqi.core import my_utils, Parse_Syngo
from srqi.inquiries import operator_improvement
from srqi import test
import os
from datetime import date

class Test_Operator_Improvement(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        data_dir = os.path.join(os.path.dirname(test.__file__),'data')
        data_file = os.path.join(data_dir, 'test_operator_improvement.xls')
        syngo_procs = Parse_Syngo.parse_syngo_file(data_file)
        cls.syngo_procs = syngo_procs
        inq_cls = operator_improvement.Operator_Improvement
        inq_cls.PROCS_PER_WINDOW.set_value(3)
        inq_cls.MIN_REPS.set_value(4)
        inq_cls.CLAMP.set_value(False)
        inq_cls.NORMALIZE_PENALTY.set_value(False)
        cls.inq_cls = inq_cls
        cls.inq = operator_improvement.Operator_Improvement([],
                                                             extra_procs = syngo_procs)

    def test_num_rads(self):
        self.assertEquals(len(self.inq.lookup),2)

    def test_stewart(self):
        expected = [(date(2011,5,15),4.5),
                    (date(2011,5,22),4),
                    (date(2011,5,24),.5),
                    (date(2011,5,28),-2),
                    (date(2011,5,29),-1)]
        equal = self._lists_equal(self.inq.lookup['Stewart, J.'], expected)
        self.assertTrue(equal, "Incorect metrics for Stewart. Expected :\n " + str(expected) + "\n Got \n" + str(self.inq.lookup['Stewart, J.']))

    def test_rayner(self):
        expected = [(date(2011,6,5), 1.5),
                    (date(2011,6,8), 1.5)]
        equal = self._lists_equal(self.inq.lookup['Rayner, K.'], expected)
        self.assertTrue(equal, "Incorect metrics for Rayner. Expected :\n " + str(expected) + "\n Got \n" + str(self.inq.lookup['Rayner, K.']))

    def test_get_procedures_helper(self):
        cpt_to_procs = operator_improvement.get_procedures_helper([], self.syngo_procs,
                                                         self.inq_cls.MIN_REPS.value)
        expected_cpt_list = ['1','2']
        self.assertEqual(set(expected_cpt_list),set(cpt_to_procs.keys()),
                        """get_procedures_helper returned an incorrect list of included cpt codes.
                        Expected: """ + str(expected_cpt_list) +"\n Got: " + str(cpt_to_procs.keys()))
        self.assertEqual(13, sum(map(len,cpt_to_procs.values())),
                         "Total number of procedures returned by get_procedures helper is incorrect.")

    def test_sort_by_rads_helper(self):
        cpt_to_procs = operator_improvement.get_procedures_helper([],
                                                                  self.syngo_procs,
                                                                  self.inq_cls.MIN_REPS.value)
        syngo_procs = sum(cpt_to_procs.values(),[])
        rad1_to_procs = operator_improvement.sort_by_rads_helper(syngo_procs,
                                                                 self.inq_cls.PROCS_PER_WINDOW.value)
        self.assertEqual(len(sum(rad1_to_procs.values(),[])),12,
                         "Incorrect total number of procedures found by sort_by_rads_helper")

    def test_get_procedure_windows(self):
        STEP_SIZE = 2
        cpt_to_procs = operator_improvement.get_procedures_helper([],
                                                                  self.syngo_procs,
                                                                  self.inq_cls.MIN_REPS.value)
        syngo_procs = sum(cpt_to_procs.values(),[])
        rad1_to_procs = operator_improvement.sort_by_rads_helper(syngo_procs,
                                                                 self.inq_cls.PROCS_PER_WINDOW.value)
        for i,proc_list in enumerate(rad1_to_procs.values()):
            if not i==len(proc_list)-1:
                windows = operator_improvement.get_procedure_windows(proc_list,
                                                                     self.inq_cls.PROCS_PER_WINDOW.value,
                                                                     STEP_SIZE)
                for window in windows:
                    self.assertEqual(len(window), self.inq_cls.PROCS_PER_WINDOW.value,
                                     "get_procedure_windows returns windows of incorrect length")
        stew_windows = operator_improvement.get_procedure_windows(rad1_to_procs['Stewart, J.'],
                                                                     self.inq_cls.PROCS_PER_WINDOW.value,
                                                                     STEP_SIZE)
        self.assertEqual(sorted([4,5,1]), [p.fluoro for p in stew_windows[0]])
        self.assertEqual(sorted([1,1,4]), [p.fluoro for p in stew_windows[1]])
        self.assertEqual(sorted([4,1,2]), [p.fluoro for p in stew_windows[2]])
        self.assertEqual(len(stew_windows),3)

    def _lists_equal(self, out, expected):
        failed = False
        for out, expected in zip(out,expected):
            if not out[0] == expected[0]:
                failed = True
                break
            elif not abs(expected[1]-out[1]) <.000001:
                failed = True
                break
        return not failed

if __name__ == '__main__':
    unittest.main()

<commit_msg>fixed tests broken by dividing by window_size in operator_improvement<commit_after>import unittest
from srqi.core import my_utils, Parse_Syngo
from srqi.inquiries import operator_improvement
from srqi import test
import os
from datetime import date

wind_size =3.0
class Test_Operator_Improvement(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        data_dir = os.path.join(os.path.dirname(test.__file__),'data')
        data_file = os.path.join(data_dir, 'test_operator_improvement.xls')
        syngo_procs = Parse_Syngo.parse_syngo_file(data_file)
        cls.syngo_procs = syngo_procs
        inq_cls = operator_improvement.Operator_Improvement
        inq_cls.PROCS_PER_WINDOW.set_value(int(window_size))
        inq_cls.MIN_REPS.set_value(4)
        inq_cls.CLAMP.set_value(False)
        inq_cls.NORMALIZE_PENALTY.set_value(False)
        cls.inq_cls = inq_cls
        cls.inq = operator_improvement.Operator_Improvement([],
                                                             extra_procs = syngo_procs)

    def test_num_rads(self):
        self.assertEquals(len(self.inq.lookup),2)

    def test_stewart(self):
        expected = [(date(2011,5,15),4.5/wind_size),
                    (date(2011,5,22),4/wind_size),
                    (date(2011,5,24),.5/wind_size),
                    (date(2011,5,28),-2/wind_size),
                    (date(2011,5,29),-1/wind_size)]
        equal = self._lists_equal(self.inq.lookup['Stewart, J.'], expected)
        self.assertTrue(equal, "Incorect metrics for Stewart. Expected :\n " + str(expected) + "\n Got \n" + str(self.inq.lookup['Stewart, J.']))

    def test_rayner(self):
        expected = [(date(2011,6,5), 1.5/wind_size),
                    (date(2011,6,8), 1.5/wind_size)]
        equal = self._lists_equal(self.inq.lookup['Rayner, K.'], expected)
        self.assertTrue(equal, "Incorect metrics for Rayner. Expected :\n " + str(expected) + "\n Got \n" + str(self.inq.lookup['Rayner, K.']))

    def test_get_procedures_helper(self):
        cpt_to_procs = operator_improvement.get_procedures_helper([], self.syngo_procs,
                                                         self.inq_cls.MIN_REPS.value)
        expected_cpt_list = ['1','2']
        self.assertEqual(set(expected_cpt_list),set(cpt_to_procs.keys()),
                        """get_procedures_helper returned an incorrect list of included cpt codes.
                        Expected: """ + str(expected_cpt_list) +"\n Got: " + str(cpt_to_procs.keys()))
        self.assertEqual(13, sum(map(len,cpt_to_procs.values())),
                         "Total number of procedures returned by get_procedures helper is incorrect.")

    def test_sort_by_rads_helper(self):
        cpt_to_procs = operator_improvement.get_procedures_helper([],
                                                                  self.syngo_procs,
                                                                  self.inq_cls.MIN_REPS.value)
        syngo_procs = sum(cpt_to_procs.values(),[])
        rad1_to_procs = operator_improvement.sort_by_rads_helper(syngo_procs,
                                                                 self.inq_cls.PROCS_PER_WINDOW.value)
        self.assertEqual(len(sum(rad1_to_procs.values(),[])),12,
                         "Incorrect total number of procedures found by sort_by_rads_helper")

    def test_get_procedure_windows(self):
        STEP_SIZE = 2
        cpt_to_procs = operator_improvement.get_procedures_helper([],
                                                                  self.syngo_procs,
                                                                  self.inq_cls.MIN_REPS.value)
        syngo_procs = sum(cpt_to_procs.values(),[])
        rad1_to_procs = operator_improvement.sort_by_rads_helper(syngo_procs,
                                                                 self.inq_cls.PROCS_PER_WINDOW.value)
        for i,proc_list in enumerate(rad1_to_procs.values()):
            if not i==len(proc_list)-1:
                windows = operator_improvement.get_procedure_windows(proc_list,
                                                                     self.inq_cls.PROCS_PER_WINDOW.value,
                                                                     STEP_SIZE)
                for window in windows:
                    self.assertEqual(len(window), self.inq_cls.PROCS_PER_WINDOW.value,
                                     "get_procedure_windows returns windows of incorrect length")
        stew_windows = operator_improvement.get_procedure_windows(rad1_to_procs['Stewart, J.'],
                                                                     self.inq_cls.PROCS_PER_WINDOW.value,
                                                                     STEP_SIZE)
        self.assertEqual(sorted([4,5,1]), [p.fluoro for p in stew_windows[0]])
        self.assertEqual(sorted([1,1,4]), [p.fluoro for p in stew_windows[1]])
        self.assertEqual(sorted([4,1,2]), [p.fluoro for p in stew_windows[2]])
        self.assertEqual(len(stew_windows),3)

    def _lists_equal(self, out, expected):
        failed = False
        for out, expected in zip(out,expected):
            if not out[0] == expected[0]:
                failed = True
                break
            elif not abs(expected[1]-out[1]) <.000001:
                failed = True
                break
        return not failed

if __name__ == '__main__':
    unittest.main()

<|endoftext|>


<commit_before>import logging
import random
from itertools import chain
from . import heroes
from .cards import Card, cardsForHero, THE_COIN
from .entity import Entity
from .enums import CardType, GameTag, Zone
from .exceptions import *
from .player import Player
from .utils import _TAG

class Deck(object):
	MAX_CARDS = 30
	MAX_UNIQUE_CARDS = 2
	MAX_UNIQUE_LEGENDARIES = 1

	@classmethod
	def randomDraft(cls, hero):
		"""
		Return a deck of 30 random cards from the \a hero's collection
		"""
		deck = []
		logging.info("Drafting a random deck for %r" % (hero))
		collection = cardsForHero(hero)
		heroCard = Card(hero)
		while len(deck) < cls.MAX_CARDS:
			card = random.choice(collection)
			if deck.count(card) < cls.MAX_UNIQUE_CARDS:
				# todo legendary check too
				deck.append(card)
		return Deck([Card(card) for card in deck], hero=heroCard)

	def __init__(self, cards, hero, name=None):
		self.cards = cards
		self.hero = hero
		if name is None:
			name = "Custom %s" % (hero)
		self.name = name

	def __str__(self):
		return self.name

	def __repr__(self):
		return "<%s (%i cards)>" % (self.hero, len(self.cards))

	def __iter__(self):
		return self.cards.__iter__()

	def shuffle(self):
		logging.info("Shuffling %r..." % (self))
		random.shuffle(self.cards)

class Game(Entity):
	TIMEOUT_TURN = 75
	TIMEOUT_MULLIGAN = 85
	MAX_MINIONS_ON_FIELD = 8
	# Game draws after 50 full turns (100 game turns)
	MAX_TURNS = 100

	def __init__(self, players):
		super().__init__()
		self.players = players
		for player in players:
			player.game = self
		self.turn = 0
		self.currentPlayer = None
		self.auras = []

	def __repr__(self):
		return "<%s %s>" % (self.__class__.__name__, self)

	def __str__(self):
		if not hasattr(self, "players"):
			return "Uninitialized Game"
		return "%r vs %r" % (self.players[0], self.players[1])

	@property
	def board(self):
		return self.currentPlayer.field + self.currentPlayer.opponent.field

	@property
	def entities(self):
		return chain([self], self.auras, self.player1.entities, self.player2.entities)

	turn = _TAG(GameTag.TURN, 0)

	def tossCoin(self):
		outcome = random.randint(0, 1)
		# player who wins the outcome is the index
		winner = self.players[outcome]
		loser = winner.opponent
		logging.info("Tossing the coin... %s wins!" % (winner))
		return winner, loser

	def start(self):
		logging.info("Starting game: %r" % (self))
		for player in self.players:
			for card in player.deck:
				card.controller = player
				card.zone = Zone.DECK
			player.summon(player.deck.hero)
			player.deck.shuffle()
			player.draw(3)
		self.player1, self.player2 = self.tossCoin()
		self.player2.draw()
		self.beginMulligan()
		self.player1.setTag(GameTag.FIRST_PLAYER, True)

	def onMulliganInput(self, player, cards):
		assert player.canMulligan
		logging.info("Received mulligan input from %r: %r" % (player, cards))
		drawn = player.draw(len(cards), hold=True)
		for i, index in enumerate(cards):
			player.placeCardInDeck(player.cards[card])
			player.insertToHand(drawn[i], index)
		player.canMulligan = False

	def beginMulligan(self):
		logging.info("Entering mulligan phase")
		logging.info("%s gets The Coin (%s)" % (self.player2, THE_COIN))
		self.player2.addToHand(Card(THE_COIN))
		self.broadcast("TURN_BEGIN", self.player1)

	def endTurn(self):
		logging.info("%s ends turn" % (self.currentPlayer))
		self.broadcast("TURN_END", self.currentPlayer)
		self.broadcast("TURN_BEGIN", self.currentPlayer.opponent)

	##
	# Events

	events = ["UPDATE", "TURN_BEGIN", "TURN_END", "DAMAGE", "HEAL", "CARD_DESTROYED", "MINION_DESTROYED"]

	def UPDATE(self):
		for card in self.board:
			if card.health == 0:
				card.destroy()

	def TURN_BEGIN(self, player):
		self.turn += 1
		logging.info("%s begins turn %i" % (player, self.turn))
		if self.turn == self.MAX_TURNS:
			raise GameOver("It's a draw!")
		if self.currentPlayer:
			self.currentPlayer.currentPlayer = False
		self.currentPlayer = player
		self.currentPlayer.currentPlayer = True
		player.broadcast("OWN_TURN_BEGIN")

	def TURN_END(self, player):
		player.broadcast("OWN_TURN_END")

	def DAMAGE(self, source, target, amount):
		source.controller.broadcast("OWN_DAMAGE", source, target, amount)

	def HEAL(self, source, target, amount):
		source.controller.broadcast("OWN_HEAL", source, target, amount)

	def MINION_DESTROYED(self, minion):
		minion.controller.broadcast("OWN_MINION_DESTROYED", minion)

	def CARD_DESTROYED(self, card):
		card.controller.broadcast("OWN_CARD_DESTROYED", card)
		if card.type == CardType.MINION:
			self.broadcast("MINION_DESTROYED", card)
<commit_msg>Broadcast OWN_DAMAGE to the correct entities<commit_after>import logging
import random
from itertools import chain
from . import heroes
from .cards import Card, cardsForHero, THE_COIN
from .entity import Entity
from .enums import CardType, GameTag, Zone
from .exceptions import *
from .player import Player
from .utils import _TAG

class Deck(object):
	MAX_CARDS = 30
	MAX_UNIQUE_CARDS = 2
	MAX_UNIQUE_LEGENDARIES = 1

	@classmethod
	def randomDraft(cls, hero):
		"""
		Return a deck of 30 random cards from the \a hero's collection
		"""
		deck = []
		logging.info("Drafting a random deck for %r" % (hero))
		collection = cardsForHero(hero)
		heroCard = Card(hero)
		while len(deck) < cls.MAX_CARDS:
			card = random.choice(collection)
			if deck.count(card) < cls.MAX_UNIQUE_CARDS:
				# todo legendary check too
				deck.append(card)
		return Deck([Card(card) for card in deck], hero=heroCard)

	def __init__(self, cards, hero, name=None):
		self.cards = cards
		self.hero = hero
		if name is None:
			name = "Custom %s" % (hero)
		self.name = name

	def __str__(self):
		return self.name

	def __repr__(self):
		return "<%s (%i cards)>" % (self.hero, len(self.cards))

	def __iter__(self):
		return self.cards.__iter__()

	def shuffle(self):
		logging.info("Shuffling %r..." % (self))
		random.shuffle(self.cards)

class Game(Entity):
	TIMEOUT_TURN = 75
	TIMEOUT_MULLIGAN = 85
	MAX_MINIONS_ON_FIELD = 8
	# Game draws after 50 full turns (100 game turns)
	MAX_TURNS = 100

	def __init__(self, players):
		super().__init__()
		self.players = players
		for player in players:
			player.game = self
		self.turn = 0
		self.currentPlayer = None
		self.auras = []

	def __repr__(self):
		return "<%s %s>" % (self.__class__.__name__, self)

	def __str__(self):
		if not hasattr(self, "players"):
			return "Uninitialized Game"
		return "%r vs %r" % (self.players[0], self.players[1])

	@property
	def board(self):
		return self.currentPlayer.field + self.currentPlayer.opponent.field

	@property
	def entities(self):
		return chain([self], self.auras, self.player1.entities, self.player2.entities)

	turn = _TAG(GameTag.TURN, 0)

	def tossCoin(self):
		outcome = random.randint(0, 1)
		# player who wins the outcome is the index
		winner = self.players[outcome]
		loser = winner.opponent
		logging.info("Tossing the coin... %s wins!" % (winner))
		return winner, loser

	def start(self):
		logging.info("Starting game: %r" % (self))
		for player in self.players:
			for card in player.deck:
				card.controller = player
				card.zone = Zone.DECK
			player.summon(player.deck.hero)
			player.deck.shuffle()
			player.draw(3)
		self.player1, self.player2 = self.tossCoin()
		self.player2.draw()
		self.beginMulligan()
		self.player1.setTag(GameTag.FIRST_PLAYER, True)

	def onMulliganInput(self, player, cards):
		assert player.canMulligan
		logging.info("Received mulligan input from %r: %r" % (player, cards))
		drawn = player.draw(len(cards), hold=True)
		for i, index in enumerate(cards):
			player.placeCardInDeck(player.cards[card])
			player.insertToHand(drawn[i], index)
		player.canMulligan = False

	def beginMulligan(self):
		logging.info("Entering mulligan phase")
		logging.info("%s gets The Coin (%s)" % (self.player2, THE_COIN))
		self.player2.addToHand(Card(THE_COIN))
		self.broadcast("TURN_BEGIN", self.player1)

	def endTurn(self):
		logging.info("%s ends turn" % (self.currentPlayer))
		self.broadcast("TURN_END", self.currentPlayer)
		self.broadcast("TURN_BEGIN", self.currentPlayer.opponent)

	##
	# Events

	events = ["UPDATE", "TURN_BEGIN", "TURN_END", "DAMAGE", "HEAL", "CARD_DESTROYED", "MINION_DESTROYED"]

	def UPDATE(self):
		for card in self.board:
			if card.health == 0:
				card.destroy()

	def TURN_BEGIN(self, player):
		self.turn += 1
		logging.info("%s begins turn %i" % (player, self.turn))
		if self.turn == self.MAX_TURNS:
			raise GameOver("It's a draw!")
		if self.currentPlayer:
			self.currentPlayer.currentPlayer = False
		self.currentPlayer = player
		self.currentPlayer.currentPlayer = True
		player.broadcast("OWN_TURN_BEGIN")

	def TURN_END(self, player):
		player.broadcast("OWN_TURN_END")

	def DAMAGE(self, source, target, amount):
		target.controller.broadcast("OWN_DAMAGE", source, target, amount)

	def HEAL(self, source, target, amount):
		source.controller.broadcast("OWN_HEAL", source, target, amount)

	def MINION_DESTROYED(self, minion):
		minion.controller.broadcast("OWN_MINION_DESTROYED", minion)

	def CARD_DESTROYED(self, card):
		card.controller.broadcast("OWN_CARD_DESTROYED", card)
		if card.type == CardType.MINION:
			self.broadcast("MINION_DESTROYED", card)
<|endoftext|>


<commit_before>#!/usr/bin/env python3.6

# Author: Eric Turgeon
# License: BSD

from subprocess import call
from sys import argv
from os import path, remove, getcwd
import getopt

results_xml = getcwd() + '/results/'
localHome = path.expanduser('~')
keyPath = localHome + '/.ssh/test_id_rsa'

error_msg = """Usage for %s:
    --ip <###.###.###.###>     - IP of the FreeNAS
    --password <root password> - Password of the FreeNAS root user
    --interface <interface>    - The interface that FreeNAS is run one
    """ % argv[0]

# if have no argumment stop
if len(argv) == 1:
    print(error_msg)
    exit()

# look if all the argument are there.
try:
    myopts, args = getopt.getopt(argv[1:], 'ipI', ["ip=",
        "password=","interface="])
except getopt.GetoptError as e:
    print (str(e))
    print(error_msg)
    exit()

for output, arg in myopts:
    if output in ('-i', '--ip'):
        ip = arg
    elif output in ('-p', '--password'):
        passwd = arg
    elif output in ('-I', '--interface'):
        interface = arg

cfg_content = """#!/usr/bin/env python3.6

import os

user = "root"
password = "%s"
ip = "%s"
freenas_url = 'http://' + ip + '/api/v1.0'
interface = "%s"
ntpServer = "10.20.20.122"
localHome = "%s"
disk1 = "ada1"
disk2 = "ada2"
keyPath = "%s"
""" % (passwd, ip, interface, localHome, keyPath)

cfg_file = open("config.py", 'w')
cfg_file.writelines(cfg_content)
cfg_file.close()

from functions import setup_ssh_agent, create_key, add_ssh_key

# Setup ssh agent befor starting test.
setup_ssh_agent()
if path.exists(keyPath) is False:
    create_key(keyPath)
add_ssh_key(keyPath)

f = open(keyPath +'.pub', 'r')
Key = f.readlines()[0].rstrip()

cfg_file = open("config.py", 'a')
cfg_file.writelines('sshKey = "%s"\n' % Key)
cfg_file.close()

call(["py.test-3.6", "--junitxml", "%snetwork_result.xml" % results_xml, "network.py"])
call(["py.test-3.6", "--junitxml", "%sssh_result.xml" % results_xml, "ssh.py"])
call(["py.test-3.6", "--junitxml", "%sstorage_result.xml" % results_xml, "storage.py"])
call(["py.test-3.6", "--junitxml", "%sntp_result.xml" % results_xml, "ntp.py"])
call(["py.test-3.6", "--junitxml", "%sbootenv_result.xml" % results_xml, "bootenv.py"])
call(["py.test-3.6", "--junitxml", "%scronjob_result.xml" % results_xml, "cronjob.py"])
call(["py.test-3.6", "--junitxml", "%sdebug_result.xml" % results_xml, "debug.py"])
call(["py.test-3.6", "--junitxml", "%semails_result.xml" % results_xml, "emails.py"])
call(["py.test-3.6", "--junitxml", "%suser_result.xml" % results_xml, "user.py"])
call(["py.test-3.6", "--junitxml", "%sftp_result.xml" % results_xml, "ftp.py"])

<commit_msg>Change disk name from ada to vtbd<commit_after>#!/usr/bin/env python3.6

# Author: Eric Turgeon
# License: BSD

from subprocess import call
from sys import argv
from os import path, remove, getcwd
import getopt

results_xml = getcwd() + '/results/'
localHome = path.expanduser('~')
keyPath = localHome + '/.ssh/test_id_rsa'

error_msg = """Usage for %s:
    --ip <###.###.###.###>     - IP of the FreeNAS
    --password <root password> - Password of the FreeNAS root user
    --interface <interface>    - The interface that FreeNAS is run one
    """ % argv[0]

# if have no argumment stop
if len(argv) == 1:
    print(error_msg)
    exit()

# look if all the argument are there.
try:
    myopts, args = getopt.getopt(argv[1:], 'ipI', ["ip=",
        "password=","interface="])
except getopt.GetoptError as e:
    print (str(e))
    print(error_msg)
    exit()

for output, arg in myopts:
    if output in ('-i', '--ip'):
        ip = arg
    elif output in ('-p', '--password'):
        passwd = arg
    elif output in ('-I', '--interface'):
        interface = arg

cfg_content = """#!/usr/bin/env python3.6

import os

user = "root"
password = "%s"
ip = "%s"
freenas_url = 'http://' + ip + '/api/v1.0'
interface = "%s"
ntpServer = "10.20.20.122"
localHome = "%s"
disk1 = "vtbd1"
disk2 = "vtbd2"
keyPath = "%s"
""" % (passwd, ip, interface, localHome, keyPath)

cfg_file = open("config.py", 'w')
cfg_file.writelines(cfg_content)
cfg_file.close()

from functions import setup_ssh_agent, create_key, add_ssh_key

# Setup ssh agent befor starting test.
setup_ssh_agent()
if path.exists(keyPath) is False:
    create_key(keyPath)
add_ssh_key(keyPath)

f = open(keyPath +'.pub', 'r')
Key = f.readlines()[0].rstrip()

cfg_file = open("config.py", 'a')
cfg_file.writelines('sshKey = "%s"\n' % Key)
cfg_file.close()

call(["py.test-3.6", "--junitxml", "%snetwork_result.xml" % results_xml, "network.py"])
call(["py.test-3.6", "--junitxml", "%sssh_result.xml" % results_xml, "ssh.py"])
call(["py.test-3.6", "--junitxml", "%sstorage_result.xml" % results_xml, "storage.py"])
call(["py.test-3.6", "--junitxml", "%sntp_result.xml" % results_xml, "ntp.py"])
call(["py.test-3.6", "--junitxml", "%sbootenv_result.xml" % results_xml, "bootenv.py"])
call(["py.test-3.6", "--junitxml", "%scronjob_result.xml" % results_xml, "cronjob.py"])
call(["py.test-3.6", "--junitxml", "%sdebug_result.xml" % results_xml, "debug.py"])
call(["py.test-3.6", "--junitxml", "%semails_result.xml" % results_xml, "emails.py"])
call(["py.test-3.6", "--junitxml", "%suser_result.xml" % results_xml, "user.py"])
call(["py.test-3.6", "--junitxml", "%sftp_result.xml" % results_xml, "ftp.py"])

<|endoftext|>


<commit_before>import copy
import inspect
import os
import socket
import subprocess
import threading

import plumbum
import rpyc
from zeroconf import ServiceBrowser, Zeroconf

import config
import utils

class Box(object):
    def __init__(self, name, ip, port, appdir, user="pi", status="initializing"):
        self.name = name   # name of remote box
        self.ip = ip       # IP address
        self.port = port   # port on which fishremote.py is accepting connections
        self.user = user   # username for SSH login to remote box

        self.appdir = appdir  # path to track data directory on remote box
        # build useful paths
        self.trackdir = os.path.join(self.appdir, config.TRACKDIR)
        self.archivedir = os.path.join(self.appdir, config.ARCHIVEDIR)
        self.dbgframedir = os.path.join(self.appdir, config.DBGFRAMEDIR)

        self.status = status
        self.local = None  # True if box is actually the local machine

        self._tunnel = None  # SSH tunnel instance
        self._rpc = None     # RPC connection instance

    def connect(self):
        self.local = (self.ip == utils.get_routed_ip())
        if not self.local:
            # only connect if it's a separate machine
            self._tunnel = plumbum.SshMachine(self.ip, user=self.user)
            self._rpc = rpyc.ssh_connect(self._tunnel, self.port)
        else:
            self._rpc = rpyc.connect("localhost", self.port)
        self.status = "connected"

    def down(self):
        self.status = "down"
        if self._rpc:
            self._rpc.close()
        if self._tunnel:
            self._tunnel.close()

    def sync_data(self):
        ''' Copy/sync track data from this box to the local track directory.'''
        if self.status != "connected":
            return "Sync failed.\nNot connected."

        if self.local:
            # data is already local; no need to sync
            return "Data is local.\nSync not needed."

        try:
            # Copy remote files into an archive dir, then have rsync
            # delete the originals after the transfer
            cmd = ['ssh', '%s@%s' % (self.user, self.ip), 'cp -r %s %s' % (self.trackdir, self.archivedir)]
            subprocess.check_output(cmd)
            # Currently does *not* copy the debugframes (following 2 lines are
            # commented), so they will be removed from remote entirely.
            #cmd = ['ssh', '%s@%s' % (self.user, self.ip), 'cp -r %s %s' % (self.dbgramedir, self.archivedir)]
            #subprocess.check_output(cmd)

            # Both source and dest must end with / to copy contents of one folder
            # into another, isntead of copying the source folder into the
            # destination as a new folder there.
            # In os.path.join, the '' ensures a trailing /
            cmd = ['rsync', '-rvt', '--remove-source-files', '%s@%s:%s/' % (self.user, self.ip, self.trackdir), os.path.join(config.TRACKDIR, self.name, '')]
            subprocess.check_output(cmd)

            cmd = ['rsync', '-rvt', '--remove-source-files', '%s@%s:%s/' % (self.user, self.ip, self.dbgframedir), os.path.join(config.DBGFRAMEDIR, self.name, '')]  # '' to ensure trailing /
            subprocess.check_output(cmd)

            return "Sync successful."

        except subprocess.CalledProcessError as e:
            return "Sync failed.\n%s" % str(e)

    @property
    def rpc(self):
        if self._rpc:
            # check for closed connection
            if self._rpc.closed:
                self.down()
                return None
            return self._rpc.root
        else:
            return None

    def __getattr__(self, name):
        '''Return something from self._rpc.root if it wasn't found in this
        object directly.  Lets use use one object namespace to access both
        "local" methods like sync_data() and remote RPC methods.'''
        if hasattr(self._rpc.root, name):
            return getattr(self._rpc.root, name)
        else:
            # default behavior
            raise AttributeError

class BoxManager(object):
    def __init__(self):
        super(BoxManager, self).__init__()
        self._boxes = dict()
        self._boxlock = threading.Lock()

        # work around a bug in zeroconf on Cygwin
        try:
            zeroconf = Zeroconf()
        except socket.error:
            zeroconf = Zeroconf([utils.get_routed_ip()])
        self._browser = ServiceBrowser(zeroconf, "_fishbox._tcp.local.", self)  # starts its own daemon thread

    def add_service(self, zeroconf, type, name):
        info = zeroconf.get_service_info(type, name)
        print("Service %s added, service info: %s" % (name, info))
        boxname = name.split('.')[0]
        with self._boxlock:
            newbox = Box(name=boxname,
                         ip=socket.inet_ntoa(info.address),
                         port=info.port,
                         appdir=info.properties['appdir'],
                         user=info.properties['user'])
            newbox.connect()
            self._boxes[boxname] = newbox

    def remove_service(self, zeroconf, type, name):
        print("Service %s removed" % name)
        boxname = name.split('.')[0]
        with self._boxlock:
            #del self._boxes[boxname]
            self._boxes[boxname].down()

    def get_boxes(self):
        with self._boxlock:
            return copy.copy(self._boxes)

class BoxManagerPlugin(object):
    ''' A plugin to inject a box list from a global BoxManager into any Bottle
    routes that need it (indicated via a "boxes" keyword (customizable)).
    '''

    name = 'boxmanager'
    api = 2

    def __init__(self, kw="boxes"):
        self._boxmanager = BoxManager()
        self._keyword = kw

    def setup(self, app):
        pass

    def close(self):
        pass

    def apply(self, callback, route):
        # Test if the original callback accepts our keyword.
        # Ignore it if it does not need a box list.
        args = inspect.getargspec(route.callback).args
        if self._keyword not in args:
            return callback

        def wrapper(*args, **kwargs):
            # inject the box list as a keyword argument
            kwargs[self._keyword] = self._boxmanager.get_boxes()

            return callback(*args, **kwargs)

        # Replace the route callback with the wrapped one
        return wrapper
<commit_msg>Run part of sync over existing SSH tunnel, not new one<commit_after>import copy
import inspect
import os
import socket
import subprocess
import threading

import plumbum
import rpyc
from zeroconf import ServiceBrowser, Zeroconf

import config
import utils

class Box(object):
    def __init__(self, name, ip, port, appdir, user="pi", status="initializing"):
        self.name = name   # name of remote box
        self.ip = ip       # IP address
        self.port = port   # port on which fishremote.py is accepting connections
        self.user = user   # username for SSH login to remote box

        self.appdir = appdir  # path to track data directory on remote box
        # build useful paths
        self.trackdir = os.path.join(self.appdir, config.TRACKDIR)
        self.archivedir = os.path.join(self.appdir, config.ARCHIVEDIR)
        self.dbgframedir = os.path.join(self.appdir, config.DBGFRAMEDIR)

        self.status = status
        self.local = None  # True if box is actually the local machine

        self._tunnel = None  # SSH tunnel instance
        self._rpc = None     # RPC connection instance

    def connect(self):
        self.local = (self.ip == utils.get_routed_ip())
        if not self.local:
            # only connect if it's a separate machine
            self._tunnel = plumbum.SshMachine(self.ip, user=self.user)
            self._rpc = rpyc.ssh_connect(self._tunnel, self.port)
        else:
            self._rpc = rpyc.connect("localhost", self.port)
        self.status = "connected"

    def down(self):
        self.status = "down"
        if self._rpc:
            self._rpc.close()
        if self._tunnel:
            self._tunnel.close()

    def sync_data(self):
        ''' Copy/sync track data from this box to the local track directory.'''
        if self.status != "connected":
            return "Sync failed.\nNot connected."

        if self.local:
            # data is already local; no need to sync
            return "Data is local.\nSync not needed."

        try:
            # Copy remote files into an archive dir, then have rsync
            # delete the originals after the transfer
            self._tunnel["cp -r %s %s" % (self.trackdir, self.archivedir)]
            #cmd = ['ssh', '%s@%s' % (self.user, self.ip), 'cp -r %s %s' % (self.trackdir, self.archivedir)]
            #subprocess.check_output(cmd)
            ## Currently does *not* copy the debugframes (following 2 lines are
            ## commented), so they will be removed from remote entirely.
            ##cmd = ['ssh', '%s@%s' % (self.user, self.ip), 'cp -r %s %s' % (self.dbgramedir, self.archivedir)]
            ##subprocess.check_output(cmd)

            # Both source and dest must end with / to copy contents of one folder
            # into another, isntead of copying the source folder into the
            # destination as a new folder there.
            # In os.path.join, the '' ensures a trailing /
            cmd = ['rsync', '-rvt', '--remove-source-files', '%s@%s:%s/' % (self.user, self.ip, self.trackdir), os.path.join(config.TRACKDIR, self.name, '')]
            subprocess.check_output(cmd)

            cmd = ['rsync', '-rvt', '--remove-source-files', '%s@%s:%s/' % (self.user, self.ip, self.dbgframedir), os.path.join(config.DBGFRAMEDIR, self.name, '')]  # '' to ensure trailing /
            subprocess.check_output(cmd)

            return "Sync successful."

        except subprocess.CalledProcessError as e:
            return "Sync failed.\n%s" % str(e)

    @property
    def rpc(self):
        if self._rpc:
            # check for closed connection
            if self._rpc.closed:
                self.down()
                return None
            return self._rpc.root
        else:
            return None

    def __getattr__(self, name):
        '''Return something from self._rpc.root if it wasn't found in this
        object directly.  Lets use use one object namespace to access both
        "local" methods like sync_data() and remote RPC methods.'''
        if hasattr(self._rpc.root, name):
            return getattr(self._rpc.root, name)
        else:
            # default behavior
            raise AttributeError

class BoxManager(object):
    def __init__(self):
        super(BoxManager, self).__init__()
        self._boxes = dict()
        self._boxlock = threading.Lock()

        # work around a bug in zeroconf on Cygwin
        try:
            zeroconf = Zeroconf()
        except socket.error:
            zeroconf = Zeroconf([utils.get_routed_ip()])
        self._browser = ServiceBrowser(zeroconf, "_fishbox._tcp.local.", self)  # starts its own daemon thread

    def add_service(self, zeroconf, type, name):
        info = zeroconf.get_service_info(type, name)
        print("Service %s added, service info: %s" % (name, info))
        boxname = name.split('.')[0]
        with self._boxlock:
            newbox = Box(name=boxname,
                         ip=socket.inet_ntoa(info.address),
                         port=info.port,
                         appdir=info.properties['appdir'],
                         user=info.properties['user'])
            newbox.connect()
            self._boxes[boxname] = newbox

    def remove_service(self, zeroconf, type, name):
        print("Service %s removed" % name)
        boxname = name.split('.')[0]
        with self._boxlock:
            #del self._boxes[boxname]
            self._boxes[boxname].down()

    def get_boxes(self):
        with self._boxlock:
            return copy.copy(self._boxes)

class BoxManagerPlugin(object):
    ''' A plugin to inject a box list from a global BoxManager into any Bottle
    routes that need it (indicated via a "boxes" keyword (customizable)).
    '''

    name = 'boxmanager'
    api = 2

    def __init__(self, kw="boxes"):
        self._boxmanager = BoxManager()
        self._keyword = kw

    def setup(self, app):
        pass

    def close(self):
        pass

    def apply(self, callback, route):
        # Test if the original callback accepts our keyword.
        # Ignore it if it does not need a box list.
        args = inspect.getargspec(route.callback).args
        if self._keyword not in args:
            return callback

        def wrapper(*args, **kwargs):
            # inject the box list as a keyword argument
            kwargs[self._keyword] = self._boxmanager.get_boxes()

            return callback(*args, **kwargs)

        # Replace the route callback with the wrapped one
        return wrapper
<|endoftext|>


<commit_before>import time
import re
from netmiko.cisco_base_connection import CiscoBaseConnection
from netmiko.ssh_exception import NetmikoAuthenticationException
from netmiko import log

class HuaweiBase(CiscoBaseConnection):
    def session_preparation(self):
        """Prepare the session after the connection has been established."""
        self._test_channel_read()
        self.set_base_prompt()
        self.disable_paging(command="screen-length 0 temporary")
        # Clear the read buffer
        time.sleep(0.3 * self.global_delay_factor)
        self.clear_buffer()

    def config_mode(self, config_command="system-view"):
        """Enter configuration mode."""
        return super().config_mode(config_command=config_command)

    def exit_config_mode(self, exit_config="return", pattern=r">"):
        """Exit configuration mode."""
        return super().exit_config_mode(exit_config=exit_config, pattern=pattern)

    def check_config_mode(self, check_string="]"):
        """Checks whether in configuration mode. Returns a boolean."""
        return super().check_config_mode(check_string=check_string)

    def check_enable_mode(self, *args, **kwargs):
        """Huawei has no enable mode."""
        pass

    def enable(self, *args, **kwargs):
        """Huawei has no enable mode."""
        return ""

    def exit_enable_mode(self, *args, **kwargs):
        """Huawei has no enable mode."""
        return ""

    def set_base_prompt(
        self, pri_prompt_terminator=">", alt_prompt_terminator="]", delay_factor=1
    ):
        """
        Sets self.base_prompt

        Used as delimiter for stripping of trailing prompt in output.

        Should be set to something that is general and applies in multiple contexts. For Comware
        this will be the router prompt with < > or [ ] stripped off.

        This will be set on logging in, but not when entering system-view
        """
        log.debug("In set_base_prompt")
        delay_factor = self.select_delay_factor(delay_factor)
        self.clear_buffer()
        self.write_channel(self.RETURN)
        time.sleep(0.5 * delay_factor)

        prompt = self.read_channel()
        prompt = self.normalize_linefeeds(prompt)

        # If multiple lines in the output take the last line
        prompt = prompt.split(self.RESPONSE_RETURN)[-1]
        prompt = prompt.strip()

        # Check that ends with a valid terminator character
        if not prompt[-1] in (pri_prompt_terminator, alt_prompt_terminator):
            raise ValueError(f"Router prompt not found: {prompt}")

        # Strip off any leading HRP_. characters for USGv5 HA
        prompt = re.sub(r"^HRP_.", "", prompt, flags=re.M)

        # Strip off leading and trailing terminator
        prompt = prompt[1:-1]
        prompt = prompt.strip()
        self.base_prompt = prompt
        log.debug(f"prompt: {self.base_prompt}")

        return self.base_prompt

    def save_config(self, cmd="save", confirm=True, confirm_response="y"):
        """ Save Config for HuaweiSSH"""
        return super().save_config(
            cmd=cmd, confirm=confirm, confirm_response=confirm_response
        )

class HuaweiSSH(HuaweiBase):
    """Huawei SSH driver."""

    pass

class HuaweiTelnet(HuaweiBase):
    """Huawei Telnet driver."""

    def telnet_login(
        self,
        pri_prompt_terminator=r"]\s*$",
        alt_prompt_terminator=r">\s*$",
        username_pattern=r"(?:user:|username|login|user name)",
        pwd_pattern=r"assword",
        delay_factor=1,
        max_loops=20,
    ):
        """Telnet login for Huawei Devices"""

        delay_factor = self.select_delay_factor(delay_factor)
        password_change_prompt = r"(Change now|Please choose 'YES' or 'NO').+"
        combined_pattern = r"({}|{}|{})".format(
            pri_prompt_terminator, alt_prompt_terminator, password_change_prompt
        )

        output = ""
        return_msg = ""
        i = 1
        while i <= max_loops:
            try:
                output = self.read_channel()
                return_msg += output

                # Search for username pattern / send username
                if re.search(username_pattern, output, flags=re.I):
                    self.write_channel(self.username + self.TELNET_RETURN)
                    time.sleep(1 * delay_factor)
                    output = self.read_channel()
                    return_msg += output

                # Search for password pattern / send password
                if re.search(pwd_pattern, output, flags=re.I):
                    self.write_channel(self.password + self.TELNET_RETURN)
                    time.sleep(0.5 * delay_factor)
                    output = self.read_channel()
                    return_msg += output
                    if re.search(
                        pri_prompt_terminator, output, flags=re.M
                    ) or re.search(alt_prompt_terminator, output, flags=re.M):
                        return return_msg

                # Search for password change prompt, send "N"
                if re.search(password_change_prompt, output):
                    self.write_channel("N" + self.TELNET_RETURN)
                    output = self.read_until_pattern(pattern=combined_pattern)
                    return_msg += output

                # Check if proper data received
                if re.search(pri_prompt_terminator, output, flags=re.M) or re.search(
                    alt_prompt_terminator, output, flags=re.M
                ):
                    return return_msg

                self.write_channel(self.TELNET_RETURN)
                time.sleep(0.5 * delay_factor)
                i += 1

            except EOFError:
                self.remote_conn.close()
                msg = f"Login failed: {self.host}"
                raise NetmikoAuthenticationException(msg)

        # Last try to see if we already logged in
        self.write_channel(self.TELNET_RETURN)
        time.sleep(0.5 * delay_factor)
        output = self.read_channel()
        return_msg += output
        if re.search(pri_prompt_terminator, output, flags=re.M) or re.search(
            alt_prompt_terminator, output, flags=re.M
        ):
            return return_msg

        self.remote_conn.close()
        msg = f"Login failed: {self.host}"
        raise NetmikoAuthenticationException(msg)

class HuaweiVrpv8SSH(HuaweiSSH):
    def commit(self, comment="", delay_factor=1):
        """
        Commit the candidate configuration.

        Commit the entered configuration. Raise an error and return the failure
        if the commit fails.

        default:
           command_string = commit
        comment:
           command_string = commit comment <comment>

        """
        delay_factor = self.select_delay_factor(delay_factor)
        error_marker = "Failed to generate committed config"
        command_string = "commit"

        if comment:
            command_string += f' comment "{comment}"'

        output = self.config_mode()
        output += self.send_command_expect(
            command_string,
            strip_prompt=False,
            strip_command=False,
            delay_factor=delay_factor,
            expect_string=r"]",
        )
        output += self.exit_config_mode()

        if error_marker in output:
            raise ValueError(f"Commit failed with following errors:\n\n{output}")
        return output

    def save_config(self, *args, **kwargs):
        """Not Implemented"""
        raise NotImplementedError
<commit_msg>Add `special_login_handler()` to ignore password change request on Huawei (#1505)<commit_after>import time
import re
from netmiko.cisco_base_connection import CiscoBaseConnection
from netmiko.ssh_exception import NetmikoAuthenticationException
from netmiko import log

class HuaweiBase(CiscoBaseConnection):
    def session_preparation(self):
        """Prepare the session after the connection has been established."""
        self._test_channel_read()
        self.set_base_prompt()
        self.disable_paging(command="screen-length 0 temporary")
        # Clear the read buffer
        time.sleep(0.3 * self.global_delay_factor)
        self.clear_buffer()

    def config_mode(self, config_command="system-view"):
        """Enter configuration mode."""
        return super().config_mode(config_command=config_command)

    def exit_config_mode(self, exit_config="return", pattern=r">"):
        """Exit configuration mode."""
        return super().exit_config_mode(exit_config=exit_config, pattern=pattern)

    def check_config_mode(self, check_string="]"):
        """Checks whether in configuration mode. Returns a boolean."""
        return super().check_config_mode(check_string=check_string)

    def check_enable_mode(self, *args, **kwargs):
        """Huawei has no enable mode."""
        pass

    def enable(self, *args, **kwargs):
        """Huawei has no enable mode."""
        return ""

    def exit_enable_mode(self, *args, **kwargs):
        """Huawei has no enable mode."""
        return ""

    def set_base_prompt(
        self, pri_prompt_terminator=">", alt_prompt_terminator="]", delay_factor=1
    ):
        """
        Sets self.base_prompt

        Used as delimiter for stripping of trailing prompt in output.

        Should be set to something that is general and applies in multiple contexts. For Comware
        this will be the router prompt with < > or [ ] stripped off.

        This will be set on logging in, but not when entering system-view
        """
        log.debug("In set_base_prompt")
        delay_factor = self.select_delay_factor(delay_factor)
        self.clear_buffer()
        self.write_channel(self.RETURN)
        time.sleep(0.5 * delay_factor)

        prompt = self.read_channel()
        prompt = self.normalize_linefeeds(prompt)

        # If multiple lines in the output take the last line
        prompt = prompt.split(self.RESPONSE_RETURN)[-1]
        prompt = prompt.strip()

        # Check that ends with a valid terminator character
        if not prompt[-1] in (pri_prompt_terminator, alt_prompt_terminator):
            raise ValueError(f"Router prompt not found: {prompt}")

        # Strip off any leading HRP_. characters for USGv5 HA
        prompt = re.sub(r"^HRP_.", "", prompt, flags=re.M)

        # Strip off leading and trailing terminator
        prompt = prompt[1:-1]
        prompt = prompt.strip()
        self.base_prompt = prompt
        log.debug(f"prompt: {self.base_prompt}")

        return self.base_prompt

    def save_config(self, cmd="save", confirm=True, confirm_response="y"):
        """ Save Config for HuaweiSSH"""
        return super().save_config(
            cmd=cmd, confirm=confirm, confirm_response=confirm_response
        )

class HuaweiSSH(HuaweiBase):
    """Huawei SSH driver."""

    def special_login_handler(self):
        """Handle password change request by ignoring it"""

        password_change_prompt = r"(Change now|Please choose 'YES' or 'NO').+"
        output = self.read_until_prompt_or_pattern(password_change_prompt)
        if re.search(password_change_prompt, output):
            self.write_channel("N\n")
            self._read_channel_expect(pattern="N\n")
        return output

class HuaweiTelnet(HuaweiBase):
    """Huawei Telnet driver."""

    def telnet_login(
        self,
        pri_prompt_terminator=r"]\s*$",
        alt_prompt_terminator=r">\s*$",
        username_pattern=r"(?:user:|username|login|user name)",
        pwd_pattern=r"assword",
        delay_factor=1,
        max_loops=20,
    ):
        """Telnet login for Huawei Devices"""

        delay_factor = self.select_delay_factor(delay_factor)
        password_change_prompt = r"(Change now|Please choose 'YES' or 'NO').+"
        combined_pattern = r"({}|{}|{})".format(
            pri_prompt_terminator, alt_prompt_terminator, password_change_prompt
        )

        output = ""
        return_msg = ""
        i = 1
        while i <= max_loops:
            try:
                output = self.read_channel()
                return_msg += output

                # Search for username pattern / send username
                if re.search(username_pattern, output, flags=re.I):
                    self.write_channel(self.username + self.TELNET_RETURN)
                    time.sleep(1 * delay_factor)
                    output = self.read_channel()
                    return_msg += output

                # Search for password pattern / send password
                if re.search(pwd_pattern, output, flags=re.I):
                    self.write_channel(self.password + self.TELNET_RETURN)
                    time.sleep(0.5 * delay_factor)
                    output = self.read_channel()
                    return_msg += output
                    if re.search(
                        pri_prompt_terminator, output, flags=re.M
                    ) or re.search(alt_prompt_terminator, output, flags=re.M):
                        return return_msg

                # Search for password change prompt, send "N"
                if re.search(password_change_prompt, output):
                    self.write_channel("N" + self.TELNET_RETURN)
                    output = self.read_until_pattern(pattern=combined_pattern)
                    return_msg += output

                # Check if proper data received
                if re.search(pri_prompt_terminator, output, flags=re.M) or re.search(
                    alt_prompt_terminator, output, flags=re.M
                ):
                    return return_msg

                self.write_channel(self.TELNET_RETURN)
                time.sleep(0.5 * delay_factor)
                i += 1

            except EOFError:
                self.remote_conn.close()
                msg = f"Login failed: {self.host}"
                raise NetmikoAuthenticationException(msg)

        # Last try to see if we already logged in
        self.write_channel(self.TELNET_RETURN)
        time.sleep(0.5 * delay_factor)
        output = self.read_channel()
        return_msg += output
        if re.search(pri_prompt_terminator, output, flags=re.M) or re.search(
            alt_prompt_terminator, output, flags=re.M
        ):
            return return_msg

        self.remote_conn.close()
        msg = f"Login failed: {self.host}"
        raise NetmikoAuthenticationException(msg)

class HuaweiVrpv8SSH(HuaweiSSH):
    def commit(self, comment="", delay_factor=1):
        """
        Commit the candidate configuration.

        Commit the entered configuration. Raise an error and return the failure
        if the commit fails.

        default:
           command_string = commit
        comment:
           command_string = commit comment <comment>

        """
        delay_factor = self.select_delay_factor(delay_factor)
        error_marker = "Failed to generate committed config"
        command_string = "commit"

        if comment:
            command_string += f' comment "{comment}"'

        output = self.config_mode()
        output += self.send_command_expect(
            command_string,
            strip_prompt=False,
            strip_command=False,
            delay_factor=delay_factor,
            expect_string=r"]",
        )
        output += self.exit_config_mode()

        if error_marker in output:
            raise ValueError(f"Commit failed with following errors:\n\n{output}")
        return output

    def save_config(self, *args, **kwargs):
        """Not Implemented"""
        raise NotImplementedError
<|endoftext|>


<commit_before>from __future__ import absolute_import

__all__ = ['SentryNotifier']

from freight import http
from freight.models import App, TaskStatus

from .base import Notifier, NotifierEvent

class SentryNotifier(Notifier):
    def get_options(self):
        return {
            'webhook_url': {'required': True},
        }

    def send(self, task, config, event):
        if task.status != TaskStatus.finished and event != NotifierEvent.TASK_STARTED:
            return

        webhook_url = config['webhook_url']

        app = App.query.get(task.app_id)

        payload = {
            'number': task.number,
            'app_name': app.name,
            'task_name': task.name,
            'env': task.environment,
            'ref': task.ref,
            'sha': task.sha,
            'duration': task.duration,
            'event': 'started' if event == NotifierEvent.TASK_STARTED else 'finished',
            'dateStarted': task.date_started.isoformat() + 'Z' if task.date_started else None,
            'dateFinished': task.date_finished.isoformat() + 'Z' if task.date_finished else None,
            'link': http.absolute_uri('/tasks/{}/{}/{}/'.format(app.name, task.environment, task.number)),
        }

        http.post(webhook_url, json=payload)
<commit_msg>Improve Sentry hook<commit_after>from __future__ import absolute_import

__all__ = ['SentryNotifier']

from freight import http
from freight.models import App, TaskStatus

from .base import Notifier, NotifierEvent

class SentryNotifier(Notifier):
    def get_options(self):
        return {
            'webhook_url': {'required': True},
        }

    def should_send(self, task, config, event):
        if event == NotifierEvent.TASK_STARTED:
            return True
        elif event == NotifierEvent.TASK_FINISHED and task.status == TaskStatus.finished:
            return True
        return False

    def send(self, task, config, event):
        webhook_url = config['webhook_url']

        app = App.query.get(task.app_id)

        payload = {
            'number': task.number,
            'app_name': app.name,
            'task_name': task.name,
            'env': task.environment,
            'ref': task.ref,
            'sha': task.sha,
            'duration': task.duration,
            'event': 'started' if event == NotifierEvent.TASK_STARTED else 'finished',
            'dateStarted': task.date_started.isoformat() + 'Z' if task.date_started else None,
            'dateFinished': task.date_finished.isoformat() + 'Z' if task.date_finished else None,
            'link': http.absolute_uri('/tasks/{}/{}/{}/'.format(app.name, task.environment, task.number)),
        }

        http.post(webhook_url, json=payload)
<|endoftext|>


<commit_before>"""
Align

Based on the alignoffset.pro IDL code written by Chae 2004

Parameters
Image :
Template :

Keyword
Cor : If True, then return values are x, y offsets
      and 2-Dimensional correlation value array of the two input images
      Default is False.

"""
from __future__ import absolute_import, print_function, division

__author__="J. Kang : jhkang@astro.snu.ac.kr"
__date__="Sep 01 2016"

from scipy.fftpack import ifft2,fft2
import fisspy.io.data

def alignoffset(image,template,cor=False):
    nxi,nyi=image.shape
    nxt,nyt=template.shape

    if nxi==nxt and nyi==nyt:
        raise ValueError('Two images are incompatible')

    <commit_msg>Delete alignoffset.py<commit_after><|endoftext|>


<commit_before>import time
import re
from netmiko.cisco_base_connection import CiscoBaseConnection
from netmiko.ssh_exception import NetmikoAuthenticationException
from netmiko import log

class HuaweiBase(CiscoBaseConnection):
    def session_preparation(self):
        """Prepare the session after the connection has been established."""
        self.ansi_escape_codes = True
        self._test_channel_read()
        self.set_base_prompt()
        self.disable_paging(command="screen-length 0 temporary")
        # Clear the read buffer
        time.sleep(0.3 * self.global_delay_factor)
        self.clear_buffer()

    def strip_ansi_escape_codes(self, string_buffer):
        """
        Huawei does a strange thing where they add a space and then add ESC[1D
        to move the cursor to the left one.

        The extra space is problematic.
        """
        code_cursor_left = chr(27) + r"\[\d+D"
        output = string_buffer
        pattern = rf" {code_cursor_left}"
        output = re.sub(pattern, "", output)

        log.debug("Stripping ANSI escape codes")
        log.debug(f"new_output = {output}")
        log.debug(f"repr = {repr(output)}")
        return super().strip_ansi_escape_codes(output)

    def config_mode(self, config_command="system-view"):
        """Enter configuration mode."""
        return super().config_mode(config_command=config_command)

    def exit_config_mode(self, exit_config="return", pattern=r">"):
        """Exit configuration mode."""
        return super().exit_config_mode(exit_config=exit_config, pattern=pattern)

    def check_config_mode(self, check_string="]"):
        """Checks whether in configuration mode. Returns a boolean."""
        return super().check_config_mode(check_string=check_string)

    def check_enable_mode(self, *args, **kwargs):
        """Huawei has no enable mode."""
        pass

    def enable(self, *args, **kwargs):
        """Huawei has no enable mode."""
        return ""

    def exit_enable_mode(self, *args, **kwargs):
        """Huawei has no enable mode."""
        return ""

    def set_base_prompt(
        self, pri_prompt_terminator=">", alt_prompt_terminator="]", delay_factor=1
    ):
        """
        Sets self.base_prompt

        Used as delimiter for stripping of trailing prompt in output.

        Should be set to something that is general and applies in multiple contexts.
        For Huawei this will be the router prompt with < > or [ ] stripped off.

        This will be set on logging in, but not when entering system-view
        """
        # log.debug("In set_base_prompt")
        delay_factor = self.select_delay_factor(delay_factor)
        self.clear_buffer()
        self.write_channel(self.RETURN)
        time.sleep(0.5 * delay_factor)

        prompt = self.read_channel()
        prompt = self.normalize_linefeeds(prompt)

        # If multiple lines in the output take the last line
        prompt = prompt.split(self.RESPONSE_RETURN)[-1]
        prompt = prompt.strip()

        # Check that ends with a valid terminator character
        if not prompt[-1] in (pri_prompt_terminator, alt_prompt_terminator):
            raise ValueError(f"Router prompt not found: {prompt}")

        # Strip off any leading HRP_. characters for USGv5 HA
        prompt = re.sub(r"^HRP_.", "", prompt, flags=re.M)

        # Strip off leading and trailing terminator
        prompt = prompt[1:-1]
        prompt = prompt.strip()
        self.base_prompt = prompt
        log.debug(f"prompt: {self.base_prompt}")

        return self.base_prompt

    def save_config(self, cmd="save", confirm=True, confirm_response="y"):
        """ Save Config for HuaweiSSH"""
        return super().save_config(
            cmd=cmd, confirm=confirm, confirm_response=confirm_response
        )

class HuaweiSSH(HuaweiBase):
    """Huawei SSH driver."""

    def special_login_handler(self):
        """Handle password change request by ignoring it"""

        password_change_prompt = r"((Change now|Please choose).+Y/N)|([\]>]\s*$)"
        output = self.read_until_pattern(password_change_prompt)
        if re.search(password_change_prompt, output):
            self.write_channel("N\n")
            self.clear_buffer()
        return output

class HuaweiTelnet(HuaweiBase):
    """Huawei Telnet driver."""

    def telnet_login(
        self,
        pri_prompt_terminator=r"]\s*$",
        alt_prompt_terminator=r">\s*$",
        username_pattern=r"(?:user:|username|login|user name)",
        pwd_pattern=r"assword",
        delay_factor=1,
        max_loops=20,
    ):
        """Telnet login for Huawei Devices"""

        delay_factor = self.select_delay_factor(delay_factor)
        password_change_prompt = r"(Change now|Please choose 'YES' or 'NO').+"
        combined_pattern = r"({}|{}|{})".format(
            pri_prompt_terminator, alt_prompt_terminator, password_change_prompt
        )

        output = ""
        return_msg = ""
        i = 1
        while i <= max_loops:
            try:
                # Search for username pattern / send username
                output = self.read_until_pattern(
                    pattern=username_pattern, re_flags=re.I
                )
                return_msg += output
                self.write_channel(self.username + self.TELNET_RETURN)

                # Search for password pattern / send password
                output = self.read_until_pattern(pattern=pwd_pattern, re_flags=re.I)
                return_msg += output
                self.write_channel(self.password + self.TELNET_RETURN)

                # Waiting for combined output
                output = self.read_until_pattern(pattern=combined_pattern)
                return_msg += output

                # Search for password change prompt, send "N"
                if re.search(password_change_prompt, output):
                    self.write_channel("N" + self.TELNET_RETURN)
                    output = self.read_until_pattern(pattern=combined_pattern)
                    return_msg += output

                # Check if proper data received
                if re.search(pri_prompt_terminator, output, flags=re.M) or re.search(
                    alt_prompt_terminator, output, flags=re.M
                ):
                    return return_msg

                self.write_channel(self.TELNET_RETURN)
                time.sleep(0.5 * delay_factor)
                i += 1

            except EOFError:
                self.remote_conn.close()
                msg = f"Login failed: {self.host}"
                raise NetmikoAuthenticationException(msg)

        # Last try to see if we already logged in
        self.write_channel(self.TELNET_RETURN)
        time.sleep(0.5 * delay_factor)
        output = self.read_channel()
        return_msg += output
        if re.search(pri_prompt_terminator, output, flags=re.M) or re.search(
            alt_prompt_terminator, output, flags=re.M
        ):
            return return_msg

        self.remote_conn.close()
        msg = f"Login failed: {self.host}"
        raise NetmikoAuthenticationException(msg)

class HuaweiVrpv8SSH(HuaweiSSH):
    def commit(self, comment="", delay_factor=1):
        """
        Commit the candidate configuration.

        Commit the entered configuration. Raise an error and return the failure
        if the commit fails.

        default:
           command_string = commit
        comment:
           command_string = commit comment <comment>

        """
        delay_factor = self.select_delay_factor(delay_factor)
        error_marker = "Failed to generate committed config"
        command_string = "commit"

        if comment:
            command_string += f' comment "{comment}"'

        output = self.config_mode()
        output += self.send_command_expect(
            command_string,
            strip_prompt=False,
            strip_command=False,
            delay_factor=delay_factor,
            expect_string=r"]",
        )
        output += self.exit_config_mode()

        if error_marker in output:
            raise ValueError(f"Commit failed with following errors:\n\n{output}")
        return output

    def save_config(self, *args, **kwargs):
        """Not Implemented"""
        raise NotImplementedError
<commit_msg>Adding a comment<commit_after>import time
import re
from netmiko.cisco_base_connection import CiscoBaseConnection
from netmiko.ssh_exception import NetmikoAuthenticationException
from netmiko import log

class HuaweiBase(CiscoBaseConnection):
    def session_preparation(self):
        """Prepare the session after the connection has been established."""
        self.ansi_escape_codes = True
        self._test_channel_read()
        self.set_base_prompt()
        self.disable_paging(command="screen-length 0 temporary")
        # Clear the read buffer
        time.sleep(0.3 * self.global_delay_factor)
        self.clear_buffer()

    def strip_ansi_escape_codes(self, string_buffer):
        """
        Huawei does a strange thing where they add a space and then add ESC[1D
        to move the cursor to the left one.

        The extra space is problematic.
        """
        code_cursor_left = chr(27) + r"\[\d+D"
        output = string_buffer
        pattern = rf" {code_cursor_left}"
        output = re.sub(pattern, "", output)

        log.debug("Stripping ANSI escape codes")
        log.debug(f"new_output = {output}")
        log.debug(f"repr = {repr(output)}")
        return super().strip_ansi_escape_codes(output)

    def config_mode(self, config_command="system-view"):
        """Enter configuration mode."""
        return super().config_mode(config_command=config_command)

    def exit_config_mode(self, exit_config="return", pattern=r">"):
        """Exit configuration mode."""
        return super().exit_config_mode(exit_config=exit_config, pattern=pattern)

    def check_config_mode(self, check_string="]"):
        """Checks whether in configuration mode. Returns a boolean."""
        return super().check_config_mode(check_string=check_string)

    def check_enable_mode(self, *args, **kwargs):
        """Huawei has no enable mode."""
        pass

    def enable(self, *args, **kwargs):
        """Huawei has no enable mode."""
        return ""

    def exit_enable_mode(self, *args, **kwargs):
        """Huawei has no enable mode."""
        return ""

    def set_base_prompt(
        self, pri_prompt_terminator=">", alt_prompt_terminator="]", delay_factor=1
    ):
        """
        Sets self.base_prompt

        Used as delimiter for stripping of trailing prompt in output.

        Should be set to something that is general and applies in multiple contexts.
        For Huawei this will be the router prompt with < > or [ ] stripped off.

        This will be set on logging in, but not when entering system-view
        """
        # log.debug("In set_base_prompt")
        delay_factor = self.select_delay_factor(delay_factor)
        self.clear_buffer()
        self.write_channel(self.RETURN)
        time.sleep(0.5 * delay_factor)

        prompt = self.read_channel()
        prompt = self.normalize_linefeeds(prompt)

        # If multiple lines in the output take the last line
        prompt = prompt.split(self.RESPONSE_RETURN)[-1]
        prompt = prompt.strip()

        # Check that ends with a valid terminator character
        if not prompt[-1] in (pri_prompt_terminator, alt_prompt_terminator):
            raise ValueError(f"Router prompt not found: {prompt}")

        # Strip off any leading HRP_. characters for USGv5 HA
        prompt = re.sub(r"^HRP_.", "", prompt, flags=re.M)

        # Strip off leading and trailing terminator
        prompt = prompt[1:-1]
        prompt = prompt.strip()
        self.base_prompt = prompt
        log.debug(f"prompt: {self.base_prompt}")

        return self.base_prompt

    def save_config(self, cmd="save", confirm=True, confirm_response="y"):
        """ Save Config for HuaweiSSH"""
        return super().save_config(
            cmd=cmd, confirm=confirm, confirm_response=confirm_response
        )

class HuaweiSSH(HuaweiBase):
    """Huawei SSH driver."""

    def special_login_handler(self):
        """Handle password change request by ignoring it"""

        # Huawei can prompt for password change. Search for that or for normal prompt
        password_change_prompt = r"((Change now|Please choose))|([\]>]\s*$)"
        output = self.read_until_pattern(password_change_prompt)
        if re.search(password_change_prompt, output):
            self.write_channel("N\n")
            self.clear_buffer()
        return output

class HuaweiTelnet(HuaweiBase):
    """Huawei Telnet driver."""

    def telnet_login(
        self,
        pri_prompt_terminator=r"]\s*$",
        alt_prompt_terminator=r">\s*$",
        username_pattern=r"(?:user:|username|login|user name)",
        pwd_pattern=r"assword",
        delay_factor=1,
        max_loops=20,
    ):
        """Telnet login for Huawei Devices"""

        delay_factor = self.select_delay_factor(delay_factor)
        password_change_prompt = r"(Change now|Please choose 'YES' or 'NO').+"
        combined_pattern = r"({}|{}|{})".format(
            pri_prompt_terminator, alt_prompt_terminator, password_change_prompt
        )

        output = ""
        return_msg = ""
        i = 1
        while i <= max_loops:
            try:
                # Search for username pattern / send username
                output = self.read_until_pattern(
                    pattern=username_pattern, re_flags=re.I
                )
                return_msg += output
                self.write_channel(self.username + self.TELNET_RETURN)

                # Search for password pattern / send password
                output = self.read_until_pattern(pattern=pwd_pattern, re_flags=re.I)
                return_msg += output
                self.write_channel(self.password + self.TELNET_RETURN)

                # Waiting for combined output
                output = self.read_until_pattern(pattern=combined_pattern)
                return_msg += output

                # Search for password change prompt, send "N"
                if re.search(password_change_prompt, output):
                    self.write_channel("N" + self.TELNET_RETURN)
                    output = self.read_until_pattern(pattern=combined_pattern)
                    return_msg += output

                # Check if proper data received
                if re.search(pri_prompt_terminator, output, flags=re.M) or re.search(
                    alt_prompt_terminator, output, flags=re.M
                ):
                    return return_msg

                self.write_channel(self.TELNET_RETURN)
                time.sleep(0.5 * delay_factor)
                i += 1

            except EOFError:
                self.remote_conn.close()
                msg = f"Login failed: {self.host}"
                raise NetmikoAuthenticationException(msg)

        # Last try to see if we already logged in
        self.write_channel(self.TELNET_RETURN)
        time.sleep(0.5 * delay_factor)
        output = self.read_channel()
        return_msg += output
        if re.search(pri_prompt_terminator, output, flags=re.M) or re.search(
            alt_prompt_terminator, output, flags=re.M
        ):
            return return_msg

        self.remote_conn.close()
        msg = f"Login failed: {self.host}"
        raise NetmikoAuthenticationException(msg)

class HuaweiVrpv8SSH(HuaweiSSH):
    def commit(self, comment="", delay_factor=1):
        """
        Commit the candidate configuration.

        Commit the entered configuration. Raise an error and return the failure
        if the commit fails.

        default:
           command_string = commit
        comment:
           command_string = commit comment <comment>

        """
        delay_factor = self.select_delay_factor(delay_factor)
        error_marker = "Failed to generate committed config"
        command_string = "commit"

        if comment:
            command_string += f' comment "{comment}"'

        output = self.config_mode()
        output += self.send_command_expect(
            command_string,
            strip_prompt=False,
            strip_command=False,
            delay_factor=delay_factor,
            expect_string=r"]",
        )
        output += self.exit_config_mode()

        if error_marker in output:
            raise ValueError(f"Commit failed with following errors:\n\n{output}")
        return output

    def save_config(self, *args, **kwargs):
        """Not Implemented"""
        raise NotImplementedError
<|endoftext|>


<commit_before>import matplotlib
matplotlib.use('Agg')
from isochrones.dartmouth import Dartmouth_Isochrone
from isochrones.starmodel import StarModel
from isochrones.observation import ObservationTree
import pandas as pd
import matplotlib.pyplot as plt

f1 = open('evidence_bound.txt','w')
f2 = open('evidence_unassociated.txt','w')

for n in range(0,2,1):
    if n < 10:
        i = '0' + str(n)
    else:
        i = str(n)

    #same system case
    df = pd.read_csv('df_binary_test{}.csv'.format(i))

    dar = Dartmouth_Isochrone()
    t = ObservationTree.from_df(df, name='test{}'.format(i))
    t.define_models(dar)

    mod = StarModel(dar, obs=t)
    mod.fit_multinest(n_live_points=1000,
                        basename='test{}_bound'.format(i))
    evi = mod.evidence
    evi = str(evi)
    f1.write('case{}: '.format(i) + evi + '\n')

    #mod.corner(['mass_0_0','mass_0_1','distance_0','AV_0'])
    mod.corner_physical(props=['mass', 'distance', 'AV'])
    mod.corner_observed()

    #unassociated case
    dar = Dartmouth_Isochrone()
    t = ObservationTree.from_df(df, name='test{}'.format(i))
    t.define_models(dar, index = [0,1])

    mod = StarModel(dar, obs=t)
    mod.fit_multinest(n_live_points=1000,
                        basename='test{}_unassociated'.format(i))
    evi = mod.evidence
    evi = str(evi)
    f2.write('case{}: '.format(i) + evi + '\n')

    #mod.corner(['mass_0_0','mass_0_1','distance_0','distance_1','AV_0','AV_1'])
    mod.corner_physical(props=['mass', 'distance', 'AV'])
    mod.corner_observed()

f1.close()
f2.close()
<commit_msg>update<commit_after>import matplotlib
matplotlib.use('Agg')
from isochrones.dartmouth import Dartmouth_Isochrone
from isochrones.starmodel import StarModel
from isochrones.observation import ObservationTree
import pandas as pd
import matplotlib.pyplot as plt

f1 = open('evidence_bound.txt','w')
f2 = open('evidence_unassociated.txt','w')

for n in range(0,2,1):
    if n < 10:
        i = '0' + str(n)
    else:
        i = str(n)

    #same system case
    df = pd.read_csv('df_binary_test{}.csv'.format(i))

    dar = Dartmouth_Isochrone()
    t = ObservationTree.from_df(df, name='test{}'.format(i))
    t.define_models(dar)

    mod = StarModel(dar, obs=t)
    mod.fit_multinest(n_live_points=1000,
                        basename='test{}_bound'.format(i))
    evi = mod.evidence
    evi = str(evi)
    f1.write('case{}: '.format(i) + evi + '\n')

    #mod.corner(['mass_0_0','mass_0_1','distance_0','AV_0'])
    fig = mod.corner_physical(props=['mass', 'distance', 'AV'])
    fig.savefig('test{}_corner_physical.png'.format(i))
    fig = mod.corner_observed()
    fig.savefig('test{}_corner_observed.png'.format(i))

    #unassociated case
    dar = Dartmouth_Isochrone()
    t = ObservationTree.from_df(df, name='test{}'.format(i))
    t.define_models(dar, index = [0,1])

    mod = StarModel(dar, obs=t)
    mod.fit_multinest(n_live_points=1000,
                        basename='test{}_unassociated'.format(i))
    evi = mod.evidence
    evi = str(evi)
    f2.write('case{}: '.format(i) + evi + '\n')

    #mod.corner(['mass_0_0','mass_0_1','distance_0','distance_1','AV_0','AV_1'])
    mod.corner_physical(props=['mass', 'distance', 'AV'])
    mod.corner_observed()

f1.close()
f2.close()
<|endoftext|>


<commit_before>from os import getenv, system
from time import sleep
from copy import deepcopy

class InterruptDiff(object):

    def __init__(self):
        self.filename = '/proc/interrupts'
        self.previous = []
        self.current = []
        self.interval = int(getenv('INTERVAL', 1))
        self.diff = []
        self.iterations = int(getenv('ITERATIONS', 60))
        self.skipzero = bool(getenv('SKIPZERO', True))
        self.ignore_limit = int(getenv('IGNORE_LE', 80))

    def parse(self):
        with open(self.filename) as file_fd:
            return [line.strip().split() for line in file_fd.readlines()]

    def eval(self):
        self.diff = deepcopy(self.current)
        changes = []
        for ln, line in enumerate(self.diff):
            changed = False
            for cn, column in enumerate(line):
                if not column.isdigit():
                    continue
                new_value = int(column) - int(self.previous[ln][cn])
                if new_value > 0:
                    changed = True
                self.diff[ln][cn] = str(new_value)

            changes.append(changed)

    def __print__(self):
        def is_changed(line):
            return any(int(x) > self.ignore_limit for x in line if x.isdigit())
        system('clear')
        print
        cpucount = len(self.diff[0]) - 1
        total = ['Total:'] + [sum(int(x[column]) for x in self.diff if len(x)
                                  > cpucount + 1) for column in xrange(1, cpucount + 2)]
        print "\t".join(str(x) for x in total)

        for line in self.diff:
            if line[0] == 'CPU0':
                line.insert(0, ' ')
            elif self.skipzero and not is_changed(line):
                continue
            print "\t".join(line)

    def run(self):
        while self.iterations > -1:
            self.iterations -= 1
            sleep(self.interval)
            self.previous = self.current
            self.current = self.parse()
            if not all((self.previous, self.current)):
                continue
            self.eval()
            self.__print__()
<commit_msg>Refactoring: deleted useless code in diff eval in irqtop.<commit_after>from os import getenv, system
from time import sleep
from copy import deepcopy

class InterruptDiff(object):

    def __init__(self):
        self.filename = '/proc/interrupts'
        self.previous = []
        self.current = []
        self.interval = int(getenv('INTERVAL', 1))
        self.diff = []
        self.iterations = int(getenv('ITERATIONS', 60))
        self.skipzero = bool(getenv('SKIPZERO', True))
        self.ignore_limit = int(getenv('IGNORE_LE', 80))

    def parse(self):
        with open(self.filename) as file_fd:
            return [line.strip().split() for line in file_fd.readlines()]

    def eval(self):
        self.diff = deepcopy(self.current)
        for ln, line in enumerate(self.diff):
            for cn, column in enumerate(line):
                if not column.isdigit():
                    continue
                new_value = int(column) - int(self.previous[ln][cn])
                self.diff[ln][cn] = str(new_value)

    def __print__(self):
        def is_changed(line):
            return any(int(x) > self.ignore_limit for x in line if x.isdigit())
        system('clear')
        print
        cpucount = len(self.diff[0]) - 1
        total = ['Total:'] + [sum(int(x[column]) for x in self.diff if len(x)
                                  > cpucount + 1) for column in xrange(1, cpucount + 2)]
        print "\t".join(str(x) for x in total)

        for line in self.diff:
            if line[0] == 'CPU0':
                line.insert(0, ' ')
            elif self.skipzero and not is_changed(line):
                continue
            print "\t".join(line)

    def run(self):
        while self.iterations > -1:
            self.iterations -= 1
            sleep(self.interval)
            self.previous = self.current
            self.current = self.parse()
            if not all((self.previous, self.current)):
                continue
            self.eval()
            self.__print__()
<|endoftext|>


<commit_before>"""
fs.expose.ftp
==============

Expose an FS object over FTP (via pyftpdlib).

This module provides the necessary interfaces to expose an FS object over
FTP, plugging into the infrastructure provided by the 'pyftpdlib' module.

To use this in combination with fsserve, do the following:

$ fsserve -t 'ftp' $HOME

The above will serve your home directory in read-only mode via anonymous FTP on the
loopback address.
"""

import os
import stat
import time
from functools import wraps

from pyftpdlib import ftpserver

from fs.path import *
from fs.osfs import OSFS
from fs.errors import convert_fs_errors

# Get these once so we can reuse them:
UID = os.getuid()
GID = os.getgid()

def decode_args(f):
    """
    Decodes string arguments using the decoding defined on the method's class.
    This decorator is for use on methods (functions which take a class or instance
    as the first parameter).
    """
    @wraps(f)
    def wrapper(self, *args):
        encoded = []
        for arg in args:
            if isinstance(arg, str):
                arg = arg.decode(self.encoding)
            encoded.append(arg)
        return f(self, *encoded)
    return wrapper

class FakeStat(object):
    """
    Pyftpdlib uses stat inside the library. This class emulates the standard
    os.stat_result class to make pyftpdlib happy. Think of it as a stat-like
    object ;-).
    """
    def __init__(self, **kwargs):
        for attr in dir(stat):
            if not attr.startswith('ST_'):
                continue
            attr = attr.lower()
            value = kwargs.get(attr, 0)
            setattr(self, attr, value)

class FTPFS(ftpserver.AbstractedFS):
    """
    The basic FTP Filesystem. This is a bridge between a pyfs filesystem and pyftpdlib's
    AbstractedFS. This class will cause the FTP server to service the given fs instance.
    """
    encoding = 'utf8'
    "Sets the encoding to use for paths."

    def __init__(self, fs, root, cmd_channel, encoding=None):
        self.fs = fs
        if encoding is not None:
            self.encoding = encoding
        super(FTPFS, self).__init__(root, cmd_channel)

    def validpath(self, path):
        try:
            normpath(path)
            return True
        except:
            return False

    @convert_fs_errors
    @decode_args
    def open(self, path, mode):
        return self.fs.open(path, mode)

    def chdir(self, path):
        self._cwd = self.ftp2fs(path)

    @convert_fs_errors
    @decode_args
    def mkdir(self, path):
        self.fs.makedir(path)

    @convert_fs_errors
    @decode_args
    def listdir(self, path):
        return map(lambda x: x.encode(self.encoding), self.fs.listdir(path))

    @convert_fs_errors
    @decode_args
    def rmdir(self, path):
        self.fs.removedir(path)

    @convert_fs_errors
    @decode_args
    def remove(self, path):
        self.fs.remove(path)

    @convert_fs_errors
    @decode_args
    def rename(self, src, dst):
        self.fs.rename(src, dst)

    def chmod(self, path, mode):
        return

    @convert_fs_errors
    @decode_args
    def stat(self, path):
        info = self.fs.getinfo(path)
        kwargs = {
            'st_size': info.get('size'),
        }
        # Give the fs a chance to provide the uid/gid. Otherwise echo the current
        # uid/gid.
        kwargs['st_uid'] = info.get('st_uid', UID)
        kwargs['st_gid'] = info.get('st_gid', GID)
        if 'st_atime' in info:
            kwargs['st_atime'] = info.get('st_atime')
        elif 'accessed_time' in info:
            kwargs['st_atime'] = time.mktime(info.get("accessed_time").timetuple())
        if 'st_mtime' in info:
            kwargs['st_mtime'] = info.get('st_mtime')
        elif 'modified_time' in info:
            kwargs['st_mtime'] = time.mktime(info.get("modified_time").timetuple())
        # Pyftpdlib uses st_ctime on Windows platform, try to provide it.
        if 'st_ctime' in info:
            kwargs['st_ctime'] = info.get('st_ctime')
        elif 'created_time' in info:
            kwargs['st_ctime'] = time.mktime(info.get("created_time").timetuple())
        elif 'st_mtime' in kwargs:
            # As a last resort, just copy the modified time.
            kwargs['st_ctime'] = kwargs['st_mtime']
        if self.fs.isdir(path):
            kwargs['st_mode'] = 0777 | stat.S_IFDIR
        else:
            kwargs['st_mode'] = 0777 | stat.S_IFREG
        return FakeStat(**kwargs)

    # No link support...
    lstat = stat

    @convert_fs_errors
    @decode_args
    def isfile(self, path):
        return self.fs.isfile(path)

    @convert_fs_errors
    @decode_args
    def isdir(self, path):
        return self.fs.isdir(path)

    @convert_fs_errors
    @decode_args
    def getsize(self, path):
        return self.fs.getsize(path)

    @convert_fs_errors
    @decode_args
    def getmtime(self, path):
        return self.fs.getinfo(path).time

    def realpath(self, path):
        return path

    def lexists(self, path):
        return True

class FTPFSFactory(object):
    """
    A factory class which can hold a reference to a file system object and
    encoding, then later pass it along to an FTPFS instance. An instance of
    this object allows multiple FTPFS instances to be created by pyftpdlib
    while sharing the same fs.
    """
    def __init__(self, fs, encoding=None):
        """
        Initializes the factory with an fs instance.
        """
        self.fs = fs
        self.encoding = encoding

    def __call__(self, root, cmd_channel):
        """
        This is the entry point of pyftpdlib. We will pass along the two parameters
        as well as the previously provided fs instance and encoding.
        """
        return FTPFS(self.fs, root, cmd_channel, encoding=self.encoding)

class HomeFTPFS(FTPFS):
    """
    A file system which serves a user's home directory.
    """
    def __init__(self, root, cmd_channel):
        """
        Use the provided user's home directory to create an FTPFS that serves an OSFS
        rooted at the home directory.
        """
        super(DemoFS, self).__init__(OSFS(root_path=root), '/', cmd_channel)

def serve_fs(fs, addr, port):
    """
    Creates a basic anonymous FTP server serving the given FS on the given address/port
    combo.
    """
    from pyftpdlib.contrib.authorizers import UnixAuthorizer
    ftp_handler = ftpserver.FTPHandler
    ftp_handler.authorizer = ftpserver.DummyAuthorizer()
    ftp_handler.authorizer.add_anonymous('/')
    ftp_handler.abstracted_fs = FTPFSFactory(fs)
    s = ftpserver.FTPServer((addr, port), ftp_handler)
    s.serve_forever()
<commit_msg>Ensure files are not treated as directories (even when they are :-)<commit_after>"""
fs.expose.ftp
==============

Expose an FS object over FTP (via pyftpdlib).

This module provides the necessary interfaces to expose an FS object over
FTP, plugging into the infrastructure provided by the 'pyftpdlib' module.

To use this in combination with fsserve, do the following:

$ fsserve -t 'ftp' $HOME

The above will serve your home directory in read-only mode via anonymous FTP on the
loopback address.
"""

import os
import stat
import time
from functools import wraps

from pyftpdlib import ftpserver

from fs.path import *
from fs.osfs import OSFS
from fs.errors import convert_fs_errors

# Get these once so we can reuse them:
UID = os.getuid()
GID = os.getgid()

def decode_args(f):
    """
    Decodes string arguments using the decoding defined on the method's class.
    This decorator is for use on methods (functions which take a class or instance
    as the first parameter).
    """
    @wraps(f)
    def wrapper(self, *args):
        encoded = []
        for arg in args:
            if isinstance(arg, str):
                arg = arg.decode(self.encoding)
            encoded.append(arg)
        return f(self, *encoded)
    return wrapper

class FakeStat(object):
    """
    Pyftpdlib uses stat inside the library. This class emulates the standard
    os.stat_result class to make pyftpdlib happy. Think of it as a stat-like
    object ;-).
    """
    def __init__(self, **kwargs):
        for attr in dir(stat):
            if not attr.startswith('ST_'):
                continue
            attr = attr.lower()
            value = kwargs.get(attr, 0)
            setattr(self, attr, value)

class FTPFS(ftpserver.AbstractedFS):
    """
    The basic FTP Filesystem. This is a bridge between a pyfs filesystem and pyftpdlib's
    AbstractedFS. This class will cause the FTP server to service the given fs instance.
    """
    encoding = 'utf8'
    "Sets the encoding to use for paths."

    def __init__(self, fs, root, cmd_channel, encoding=None):
        self.fs = fs
        if encoding is not None:
            self.encoding = encoding
        super(FTPFS, self).__init__(root, cmd_channel)

    def validpath(self, path):
        try:
            normpath(path)
            return True
        except:
            return False

    @convert_fs_errors
    @decode_args
    def open(self, path, mode):
        return self.fs.open(path, mode)

    def chdir(self, path):
        self._cwd = self.ftp2fs(path)

    @convert_fs_errors
    @decode_args
    def mkdir(self, path):
        self.fs.makedir(path)

    @convert_fs_errors
    @decode_args
    def listdir(self, path):
        return map(lambda x: x.encode(self.encoding), self.fs.listdir(path))

    @convert_fs_errors
    @decode_args
    def rmdir(self, path):
        self.fs.removedir(path)

    @convert_fs_errors
    @decode_args
    def remove(self, path):
        self.fs.remove(path)

    @convert_fs_errors
    @decode_args
    def rename(self, src, dst):
        self.fs.rename(src, dst)

    def chmod(self, path, mode):
        return

    @convert_fs_errors
    @decode_args
    def stat(self, path):
        info = self.fs.getinfo(path)
        kwargs = {
            'st_size': info.get('size'),
        }
        # Give the fs a chance to provide the uid/gid. Otherwise echo the current
        # uid/gid.
        kwargs['st_uid'] = info.get('st_uid', UID)
        kwargs['st_gid'] = info.get('st_gid', GID)
        if 'st_atime' in info:
            kwargs['st_atime'] = info.get('st_atime')
        elif 'accessed_time' in info:
            kwargs['st_atime'] = time.mktime(info.get("accessed_time").timetuple())
        if 'st_mtime' in info:
            kwargs['st_mtime'] = info.get('st_mtime')
        elif 'modified_time' in info:
            kwargs['st_mtime'] = time.mktime(info.get("modified_time").timetuple())
        # Pyftpdlib uses st_ctime on Windows platform, try to provide it.
        if 'st_ctime' in info:
            kwargs['st_ctime'] = info.get('st_ctime')
        elif 'created_time' in info:
            kwargs['st_ctime'] = time.mktime(info.get("created_time").timetuple())
        elif 'st_mtime' in kwargs:
            # As a last resort, just copy the modified time.
            kwargs['st_ctime'] = kwargs['st_mtime']
        mode = 0777
        # Merge in the type (dir or file). File is tested first, some file systems
        # such as ArchiveMountFS treat archive files as directories too. By checking
        # file first, any such files will be only files (not directories).
        if self.fs.isfile(path):
            mode |= stat.S_IFREG
        elif self.fs.isdir(path):
            mode |= stat.S_IFDIR
        kwargs['st_mode'] = mode
        return FakeStat(**kwargs)

    # No link support...
    lstat = stat

    @convert_fs_errors
    @decode_args
    def isfile(self, path):
        return self.fs.isfile(path)

    @convert_fs_errors
    @decode_args
    def isdir(self, path):
        return self.fs.isdir(path)

    @convert_fs_errors
    @decode_args
    def getsize(self, path):
        return self.fs.getsize(path)

    @convert_fs_errors
    @decode_args
    def getmtime(self, path):
        return self.fs.getinfo(path).time

    def realpath(self, path):
        return path

    def lexists(self, path):
        return True

class FTPFSFactory(object):
    """
    A factory class which can hold a reference to a file system object and
    encoding, then later pass it along to an FTPFS instance. An instance of
    this object allows multiple FTPFS instances to be created by pyftpdlib
    while sharing the same fs.
    """
    def __init__(self, fs, encoding=None):
        """
        Initializes the factory with an fs instance.
        """
        self.fs = fs
        self.encoding = encoding

    def __call__(self, root, cmd_channel):
        """
        This is the entry point of pyftpdlib. We will pass along the two parameters
        as well as the previously provided fs instance and encoding.
        """
        return FTPFS(self.fs, root, cmd_channel, encoding=self.encoding)

class HomeFTPFS(FTPFS):
    """
    A file system which serves a user's home directory.
    """
    def __init__(self, root, cmd_channel):
        """
        Use the provided user's home directory to create an FTPFS that serves an OSFS
        rooted at the home directory.
        """
        super(DemoFS, self).__init__(OSFS(root_path=root), '/', cmd_channel)

def serve_fs(fs, addr, port):
    """
    Creates a basic anonymous FTP server serving the given FS on the given address/port
    combo.
    """
    from pyftpdlib.contrib.authorizers import UnixAuthorizer
    ftp_handler = ftpserver.FTPHandler
    ftp_handler.authorizer = ftpserver.DummyAuthorizer()
    ftp_handler.authorizer.add_anonymous('/')
    ftp_handler.abstracted_fs = FTPFSFactory(fs)
    s = ftpserver.FTPServer((addr, port), ftp_handler)
    s.serve_forever()
<|endoftext|>


<commit_before>#!/usr/bin/env python

"""Flask-GCM wrapper for python-gcm."""

from gcmclient import (
    GCM as _GCM,
    GCMAuthenticationError,
    JSONMessage,
    PlainTextMessage,
    Result,
)
from gcmclient.gcm import GCM_URL

# Version info
__project__ = 'Flask-GCM'
__MAJOR__ = 0
__MINOR__ = 1
__PATCH__ = 0
__version__ = '{0}.{1}.{2}'.format(__MAJOR__, __MINOR__, __PATCH__)
VERSION = __project__ + '-' + __version__

# Define what gets imported when using `from flask_gcm import *`
__all__ = ('GCMAuthenticationError',
           'JSONMessage',
           'PlainTextMessage',
           'GCM',
           'Result')

class GCM(_GCM):

    """Convenience wrapper class for GCM library."""

    def __init__(self, app=None, **kwargs):
        """Initialize the GCM object with flask app settings."""
        self.app = app
        if self.app:
            self.init_app(app, **kwargs)

    def init_app(self, app, **kwargs):
        """Initialize the GCM object with flask app settings."""
        # Configure default GCM settings
        app.config.setdefault('GCM_URL', GCM_URL)
        app.config.setdefault('GCM_KEY', None)

        # Hard limit in GCM API:
        # http://developer.android.com/training/cloudsync/gcm.html
        app.config.setdefault('GCM_MAX_DEVICES_PER_REQUEST', 1000)

        # Configure GCM from app settings
        self.url = app.config.get('GCM_URL') or GCM_URL
        self.api_key = app.config.get('GCM_KEY')

        # Register GCM on app extensions
        if not hasattr(app, 'extensions'):  # pragma: no cover
            # For older versions of Flask before 0.7
            app.extensions = {}
        app.extensions['gcm'] = self

        # init python-gcm object
        super(self.__class__, self).__init__(self.api_key, url=self.url,
                                             **kwargs)
<commit_msg>update version<commit_after>#!/usr/bin/env python

"""Flask-GCM wrapper for python-gcm."""

from gcmclient import (
    GCM as _GCM,
    GCMAuthenticationError,
    JSONMessage,
    PlainTextMessage,
    Result,
)
from gcmclient.gcm import GCM_URL

# Version info
__project__ = 'Flask-GCM'
__MAJOR__ = 0
__MINOR__ = 2
__PATCH__ = 0
__version__ = '{0}.{1}.{2}'.format(__MAJOR__, __MINOR__, __PATCH__)
VERSION = __project__ + '-' + __version__

# Define what gets imported when using `from flask_gcm import *`
__all__ = ('GCMAuthenticationError',
           'JSONMessage',
           'PlainTextMessage',
           'GCM',
           'Result')

class GCM(_GCM):

    """Convenience wrapper class for GCM library."""

    def __init__(self, app=None, **kwargs):
        """Initialize the GCM object with flask app settings."""
        self.app = app
        if self.app:
            self.init_app(app, **kwargs)

    def init_app(self, app, **kwargs):
        """Initialize the GCM object with flask app settings."""
        # Configure default GCM settings
        app.config.setdefault('GCM_URL', GCM_URL)
        app.config.setdefault('GCM_KEY', None)

        # Hard limit in GCM API:
        # http://developer.android.com/training/cloudsync/gcm.html
        app.config.setdefault('GCM_MAX_DEVICES_PER_REQUEST', 1000)

        # Configure GCM from app settings
        self.url = app.config.get('GCM_URL') or GCM_URL
        self.api_key = app.config.get('GCM_KEY')

        # Register GCM on app extensions
        if not hasattr(app, 'extensions'):  # pragma: no cover
            # For older versions of Flask before 0.7
            app.extensions = {}
        app.extensions['gcm'] = self

        # init python-gcm object
        super(self.__class__, self).__init__(self.api_key, url=self.url,
                                             **kwargs)
<|endoftext|>


<commit_before>class UniformExchange(object):
    def __init__(self, A):
        if not isinstance(A, (float, int)) or A <= 0:
            raise ValueError('Exchange constant must be positive float/int.')
        else:
            self.A = A

    def get_mif(self):
        # Create mif string.
        mif = '# UniformExchange\n'
        mif += 'set A {}\n'.format(self.A)
        mif += 'Specify Oxs_UniformExchange {\n'
        mif += '\tA $A\n'
        mif += '}\n\n'

        return mif
<commit_msg>Remove set statements.<commit_after>class UniformExchange(object):
    def __init__(self, A):
        if not isinstance(A, (float, int)) or A <= 0:
            raise ValueError('Exchange constant must be positive float/int.')
        else:
            self.A = A

    def get_mif(self):
        # Create mif string.
        mif = '# UniformExchange\n'
        mif += 'Specify Oxs_UniformExchange {\n'
        mif += '\tA {}\n'.format(self.A)
        mif += '}\n\n'

        return mif
<|endoftext|>