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

import pprint
import requests
import time

from neutron_lbaas.tests.tempest.v2.scenario import base
from tempest import config

config = config.CONF

class F5StatsBaseTestCase(base.BaseTestCase):

    def setUp(self):
        super(F5StatsBaseTestCase, self).setUp()
        self.tenant_id = self.subnet['tenant_id']
        self.members = {}
        self._create_servers()
        self._start_servers()
        self._create_load_balancer()
        self._create_member(
            self.server_fixed_ips[self.servers['primary']],
            'primary',
            load_balancer_id=self.load_balancer.get('id'),
            pool_id=self.pool.get('id'),
            subnet_id=self.subnet.get('id'))
        self._create_member(
            self.server_fixed_ips[self.servers['secondary']],
            'secondary',
            load_balancer_id=self.load_balancer.get('id'),
            pool_id=self.pool.get('id'),
            subnet_id=self.subnet.get('id'))
        self._wait_for_load_balancer_status(self.load_balancer.get('id'))

    def _create_load_balancer(self, ip_version=4, persistence_type=None):
        self.create_lb_kwargs = {'tenant_id': self.tenant_id,
                                 'vip_subnet_id': self.subnet['id']}
        self.load_balancer = self.load_balancers_client.create_load_balancer(
            **self.create_lb_kwargs)
        load_balancer_id = self.load_balancer['id']
        self.addCleanup(self._cleanup_load_balancer, load_balancer_id)
        self._wait_for_load_balancer_status(load_balancer_id)

        listener = self._create_listener(load_balancer_id=load_balancer_id)
        self._wait_for_load_balancer_status(load_balancer_id)
        self.listener_id = listener.get('id')

        self.pool = self._create_pool(listener_id=listener.get('id'),
                                      persistence_type=persistence_type)
        self._wait_for_load_balancer_status(load_balancer_id)

        # self._create_members(load_balancer_id=load_balancer_id,
        #                     pool_id=self.pool['id'],
        #                     subnet_id=self.subnet['id'])

        self.vip_ip = self.load_balancer.get('vip_address')

        # if the ipv4 is used for lb, then fetch the right values from
        # tempest.conf file
        if ip_version == 4:
            if (config.network.public_network_id and not
                    config.network.project_networks_reachable):
                self._assign_floating_ip_to_lb_vip(self.load_balancer)
                self.vip_ip = self.floating_ips[
                    self.load_balancer.id][0]['floating_ip_address']

        # Currently the ovs-agent is not enforcing security groups on the
        # vip port - see https://bugs.launchpad.net/neutron/+bug/1163569
        # However the linuxbridge-agent does, and it is necessary to add a
        # security group with a rule that allows tcp port 80 to the vip port.
        self.ports_client.update_port(
            self.load_balancer.get('vip_port_id'),
            security_groups=[self.security_group.id])

    def _create_member(self, server_ip, server_position, load_balancer_id=None,
                       pool_id=None, subnet_id=None):
        """Create two members.

        In case there is only one server, create both members with the same ip
        but with different ports to listen on.
        """

        member = self.members_client.create_member(
            pool_id=pool_id,
            address=server_ip,
            protocol_port=self.port1,
            subnet_id=subnet_id)
        self._wait_for_load_balancer_status(load_balancer_id)
        self.members[server_position] = member
        self.assertTrue(self.members[server_position])

    def _run_traffic(
            self, headers=None, cookies=None, uri_path=None,
            expected_status=200, count=10):

        for x in range(count):
            try:
                if not uri_path:
                    uri_path = 'http://{}'.format(self.vip_ip)
                print('making request to {}'.format(uri_path))
                res = requests.get(uri_path, headers=headers, cookies=cookies)
                assert res.status_code == expected_status
            except Exception:
                time.sleep(1)
                continue

    def _get_stats(self):
        return self.load_balancers_client.get_load_balancer_stats(
            self.load_balancer.get('id'))

class TestLoadBalancerStats(F5StatsBaseTestCase):

    def setUp(self):
        super(TestLoadBalancerStats, self).setUp()

    def test_stats(self):
        request_count = 10  # number of requests to make

        # get initial stats -- should be 0
        before_stats = self._get_stats()
        assert before_stats['active_connections'] == 0
        assert before_stats['bytes_in'] == 0
        assert before_stats['bytes_out'] == 0
        assert before_stats['total_connections'] == 0

        # send traffic through load balancer
        self._run_traffic(count=request_count)

        # Get stats -- may take some time for BIG-IP to update. Try
        # until stats total_connections is equal to number of requests.
        # Bail after 60 seconds.
        for x in range(60):
            after_stats = self._get_stats()
            if after_stats['total_connections'] == request_count:
                break
            time.sleep(1)

        # validate final stats
        assert after_stats['active_connections'] == 0
        assert after_stats['bytes_in'] > 0
        assert after_stats['bytes_out'] > 0
        assert after_stats['total_connections'] == request_count

        print('===Success===')
        print(pprint.pprint(after_stats))
<commit_msg>test_stats test failing in stable/newton due to changes in neutron_lbaas test setup<commit_after># Copyright 2016 F5 Networks Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

import pprint
import requests
import time

from neutron_lbaas.tests.tempest.v2.scenario import base
from tempest import config

config = config.CONF

class F5StatsBaseTestCase(base.BaseTestCase):

    def setUp(self):
        super(F5StatsBaseTestCase, self).setUp()
        self.tenant_id = self.subnet['tenant_id']
        self.members = {}
        self._create_servers()
        self._start_servers()
        self._create_load_balancer()
        self._create_member(
            self.server_fixed_ips[self.servers['primary']],
            'primary',
            load_balancer_id=self.load_balancer.get('id'),
            pool_id=self.pool.get('id'),
            subnet_id=self.subnet.get('id'))
        self._create_member(
            self.server_fixed_ips[self.servers['secondary']],
            'secondary',
            load_balancer_id=self.load_balancer.get('id'),
            pool_id=self.pool.get('id'),
            subnet_id=self.subnet.get('id'))
        self._wait_for_load_balancer_status(self.load_balancer.get('id'))

    def _create_load_balancer(self, ip_version=4, persistence_type=None):
        self.create_lb_kwargs = {'tenant_id': self.tenant_id,
                                 'vip_subnet_id': self.subnet['id']}
        self.load_balancer = self.load_balancers_client.create_load_balancer(
            **self.create_lb_kwargs)
        load_balancer_id = self.load_balancer['id']
        self.addCleanup(self._cleanup_load_balancer, load_balancer_id)
        self._wait_for_load_balancer_status(load_balancer_id)

        listener = self._create_listener(load_balancer_id=load_balancer_id)
        self._wait_for_load_balancer_status(load_balancer_id)
        self.listener_id = listener.get('id')

        self.pool = self._create_pool(listener_id=listener.get('id'),
                                      persistence_type=persistence_type)
        self._wait_for_load_balancer_status(load_balancer_id)

        # self._create_members(load_balancer_id=load_balancer_id,
        #                     pool_id=self.pool['id'],
        #                     subnet_id=self.subnet['id'])

        self.vip_ip = self.load_balancer.get('vip_address')

        # if the ipv4 is used for lb, then fetch the right values from
        # tempest.conf file
        if ip_version == 4:
            if (config.network.public_network_id and not
                    config.network.project_networks_reachable):
                self._assign_floating_ip_to_lb_vip(self.load_balancer)
                self.vip_ip = self.floating_ips[
                    load_balancer_id][0]['floating_ip_address']

        # Currently the ovs-agent is not enforcing security groups on the
        # vip port - see https://bugs.launchpad.net/neutron/+bug/1163569
        # However the linuxbridge-agent does, and it is necessary to add a
        # security group with a rule that allows tcp port 80 to the vip port.
        self.ports_client.update_port(
            self.load_balancer.get('vip_port_id'),
            security_groups=[self.security_group.get('id')])

    def _create_member(self, server_ip, server_position, load_balancer_id=None,
                       pool_id=None, subnet_id=None):
        """Create two members.

        In case there is only one server, create both members with the same ip
        but with different ports to listen on.
        """

        member = self.members_client.create_member(
            pool_id=pool_id,
            address=server_ip,
            protocol_port=self.port1,
            subnet_id=subnet_id)
        self._wait_for_load_balancer_status(load_balancer_id)
        self.members[server_position] = member
        self.assertTrue(self.members[server_position])

    def _run_traffic(
            self, headers=None, cookies=None, uri_path=None,
            expected_status=200, count=10):

        for x in range(count):
            try:
                if not uri_path:
                    uri_path = 'http://{}'.format(self.vip_ip)
                print('making request to {}'.format(uri_path))
                res = requests.get(uri_path, headers=headers, cookies=cookies)
                assert res.status_code == expected_status
            except Exception:
                time.sleep(1)
                continue

    def _get_stats(self):
        return self.load_balancers_client.get_load_balancer_stats(
            self.load_balancer.get('id'))

class TestLoadBalancerStats(F5StatsBaseTestCase):

    def setUp(self):
        super(TestLoadBalancerStats, self).setUp()

    def test_stats(self):
        request_count = 10  # number of requests to make

        # get initial stats -- should be 0
        before_stats = self._get_stats()
        assert before_stats['active_connections'] == 0
        assert before_stats['bytes_in'] == 0
        assert before_stats['bytes_out'] == 0
        assert before_stats['total_connections'] == 0

        # send traffic through load balancer
        self._run_traffic(count=request_count)

        # Get stats -- may take some time for BIG-IP to update. Try
        # until stats total_connections is equal to number of requests.
        # Bail after 60 seconds.
        for x in range(60):
            after_stats = self._get_stats()
            if after_stats['total_connections'] == request_count:
                break
            time.sleep(1)

        # validate final stats
        assert after_stats['active_connections'] == 0
        assert after_stats['bytes_in'] > 0
        assert after_stats['bytes_out'] > 0
        assert after_stats['total_connections'] == request_count

        print('===Success===')
        print(pprint.pprint(after_stats))
<|endoftext|>


<commit_before>#!/usr/bin/python
import os
import os.path
import sys
sys.path.append('..')

import numpy
import scipy
from matplotlib import pyplot

from Tkinter import Tk
from tkFileDialog import askopenfilename

import fc.io
import fc.excel_io
import fc.gate
import fc.plot
import fc.transform
import fc.mef
import fc.stats

def main():
    # Launch dialog to select input file
    Tk().withdraw() # don't show main window
    input_form = askopenfilename(filetypes = [('Excel files', '*.xlsx')])
    if not input_form:
        print "Canelled."
        return

    # Get base directory
    basedir, __ = os.path.split(input_form)

    # Generate plotting directories
    beads_plot_dir = "{}/{}".format(basedir, 'plot_beads')
    gated_plot_dir = "{}/{}".format(basedir, 'plot_gated')
    if not os.path.exists(beads_plot_dir):
        os.makedirs(beads_plot_dir)
    if not os.path.exists(gated_plot_dir):
        os.makedirs(gated_plot_dir)

    # Generate path of output file
    output_form = "{}/{}".format(basedir, 'output.xls')

    # Get beads files data from input form
    beads_info = fc.excel_io.import_rows(input_form, "beads")
    to_mef_all = {}

    print "\nProcessing beads..."
    for bi in beads_info:
        # Open file
        di = fc.io.TaborLabFCSData("{}/{}".format(basedir, bi['File Path']))
        print "{} ({} events).".format(str(di), di.shape[0])

        # Trim
        di = fc.gate.start_end(di, num_start=250, num_end=100)
        di = fc.gate.high_low(di, ['FSC-H', 'SSC-H'])
        # Density gate
        print "Running density gate (fraction = {:.2f})..."\
            .format(bi['Gate Fraction'])
        gated_di, gate_contour = fc.gate.density2d(data = di,
                                gate_fraction = float(bi['Gate Fraction']))
        # Plot
        pyplot.figure(figsize = (6,4))
        fc.plot.density_and_hist(di, gated_di,
            density_channels = ['FSC-H', 'SSC-H'],
            hist_channels = ['FL1-H'],
            gate_contour = gate_contour,
            density_params = {'mode': 'scatter'},
            hist_params = {'div': 4, 'edgecolor': 'g'},
            savefig = '{}/density_hist_{}.png'.format(beads_plot_dir, di))

        # Process MEF values
        mef = []
        mef_channels = []
        if 'MEFL' in bi:
            mefl = bi['MEFL'].split(',')
            mefl = [int(e) if e.strip().isdigit() else numpy.nan for e in mefl]
            mef.append(mefl)
            mef_channels.append('FL1-H')
        mef = numpy.array(mef)

        # Obtain standard curve transformation
        print "\nCalculating standard curve..."
        to_mef = fc.mef.get_transform_fxn(gated_di, mef,
                        cluster_method = bi['Clustering Method'],
                        cluster_channels = ['FL1-H', 'FL2-H', 'FL3-H'],
                        mef_channels = mef_channels, verbose = True,
                        plot = True, plot_dir = beads_plot_dir)

        # Save MEF transformation function
        to_mef_all[bi['File Path']] = to_mef

    # Process data files
    print "\nLoading data..."
    # Get beads files data from input form
    cells_info = fc.excel_io.import_rows(input_form, "cells")

    # Load data files
    data = []
    for ci in cells_info:
        di = fc.io.TaborLabFCSData("{}/{}".format(basedir, ci['File Path']))
        data.append(di)

        gain = di[:,'FL1-H'].channel_info[0]['pmt_voltage']
        print "{} ({} events, FL1-H gain = {}).".format(str(di),
            di.shape[0], gain)

    # Basic gating/trimming
    ch_all = ['FSC-H', 'SSC-H', 'FL1-H', 'FL2-H', 'FL3-H']
    data_trimmed = [fc.gate.start_end(di, num_start=250, num_end=100)\
                                                         for di in data]
    data_trimmed = [fc.gate.high_low(di, ch_all) for di in data_trimmed]
    # Exponential transformation
    data_transf = [fc.transform.exponentiate(di, ['FSC-H', 'SSC-H']) \
                                                        for di in data_trimmed]

    # Transform to MEF
    print "\nPerforming MEF transformation..."
    data_mef = []
    for ci, di in zip(cells_info, data_transf):
        print "{}...".format(str(di))
        to_mef = to_mef_all[ci['Beads File Path']]
        data_mef.append(to_mef(di, 'FL1-H'))

    # Density gate
    print "\nRunning density gate on data files..."
    data_gated = []
    data_gated_contour = []
    for ci, di in zip(cells_info, data_mef):
        print "{} (gate fraction = {:.2f})...".format(str(di),
                ci['Gate Fraction'])
        di_gated, gate_contour = fc.gate.density2d(data = di, bins_log = True,
                                        gate_fraction = ci['Gate Fraction'])
        data_gated.append(di_gated)
        data_gated_contour.append(gate_contour)

    # Plot
    print "\nPlotting density diagrams and histograms of data"
    for di, dig, dgc in zip(data_mef, data_gated, data_gated_contour):
        print "{}...".format(str(di))
        # Plot
        fc.plot.density_and_hist(di, gated_data = dig, figsize = (7,7),
            density_channels = ['FSC-H', 'SSC-H'],
            hist_channels = ['FL1-H'], gate_contour = dgc,
            density_params = {'mode': 'scatter', 'log': True},
            hist_params = {'div': 4, 'log': True, 'edgecolor': 'g',
                'xlabel': 'Molecules of Equivalent Fluorescein (MEFL)'},
            savefig = '{}/{}.png'.format(gated_plot_dir, str(di)))
        pyplot.close()

    # Export to output excel file
    print "\nWriting output file..."
    # Calculate statistics
    for ci, di, diug in zip(cells_info, data_gated, data):
        ci['Ungated Counts'] = diug.shape[0]
        ci['Gated Counts'] = di.shape[0]
        for channel in ['FL1-H']:
            ci[channel + ' Mean'] = fc.stats.mean(di, channel)
            ci[channel + ' Geom. Mean'] = fc.stats.gmean(di, channel)
            ci[channel + ' Median'] = fc.stats.median(di, channel)
            ci[channel + ' Mode'] = fc.stats.mode(di, channel)
            ci[channel + ' Std'] = fc.stats.std(di, channel)
            ci[channel + ' CV'] = fc.stats.CV(di, channel)
            ci[channel + ' IQR'] = fc.stats.iqr(di, channel)
            ci[channel + ' RCV'] = fc.stats.RCV(di, channel)
    # Convert data
    headers = cells_info[0].keys()
    content = []
    for ci in cells_info:
        row = []
        for h in headers:
            row.append(ci[h])
        content.append(row)
    # Save output excel file
    ws = [headers] + content
    fc.excel_io.export_workbook(output_form, {'cells': ws})

    print "\nDone."
    raw_input("Press Enter to exit...")

if __name__ == "__main__":
    main()<commit_msg>Fixed #12<commit_after>#!/usr/bin/python
import os
import os.path
import sys
sys.path.append('..')

import numpy
import scipy
from matplotlib import pyplot

from Tkinter import Tk
from tkFileDialog import askopenfilename

import fc.io
import fc.excel_io
import fc.gate
import fc.plot
import fc.transform
import fc.mef
import fc.stats

def main():
    # Launch dialog to select input file
    Tk().withdraw() # don't show main window
    input_form = askopenfilename(filetypes = [('Excel files', '*.xlsx')])
    if not input_form:
        print "Canelled."
        return

    # Get base directory
    basedir, input_file = os.path.split(input_form)
    input_filename, __ = os.path.splitext(input_file)

    # Generate plotting directories
    beads_plot_dir = "{}/{}".format(basedir, 'plot_beads')
    gated_plot_dir = "{}/{}".format(basedir, 'plot_gated')
    if not os.path.exists(beads_plot_dir):
        os.makedirs(beads_plot_dir)
    if not os.path.exists(gated_plot_dir):
        os.makedirs(gated_plot_dir)

    # Generate path of output file
    output_form = "{}/{}".format(basedir, input_filename + '_output.xls')

    # Get beads files data from input form
    beads_info = fc.excel_io.import_rows(input_form, "beads")
    to_mef_all = {}

    print "\nProcessing beads..."
    for bi in beads_info:
        # Open file
        di = fc.io.TaborLabFCSData("{}/{}".format(basedir, bi['File Path']))
        print "{} ({} events).".format(str(di), di.shape[0])

        # Trim
        di = fc.gate.start_end(di, num_start=250, num_end=100)
        di = fc.gate.high_low(di, ['FSC-H', 'SSC-H'])
        # Density gate
        print "Running density gate (fraction = {:.2f})..."\
            .format(bi['Gate Fraction'])
        gated_di, gate_contour = fc.gate.density2d(data = di,
                                gate_fraction = float(bi['Gate Fraction']))
        # Plot
        pyplot.figure(figsize = (6,4))
        fc.plot.density_and_hist(di, gated_di,
            density_channels = ['FSC-H', 'SSC-H'],
            hist_channels = ['FL1-H'],
            gate_contour = gate_contour,
            density_params = {'mode': 'scatter'},
            hist_params = {'div': 4, 'edgecolor': 'g'},
            savefig = '{}/density_hist_{}.png'.format(beads_plot_dir, di))

        # Process MEF values
        mef = []
        mef_channels = []
        if 'MEFL' in bi:
            mefl = bi['MEFL'].split(',')
            mefl = [int(e) if e.strip().isdigit() else numpy.nan for e in mefl]
            mef.append(mefl)
            mef_channels.append('FL1-H')
        mef = numpy.array(mef)

        # Obtain standard curve transformation
        print "\nCalculating standard curve..."
        to_mef = fc.mef.get_transform_fxn(gated_di, mef,
                        cluster_method = bi['Clustering Method'],
                        cluster_channels = ['FL1-H', 'FL2-H', 'FL3-H'],
                        mef_channels = mef_channels, verbose = True,
                        plot = True, plot_dir = beads_plot_dir)

        # Save MEF transformation function
        to_mef_all[bi['File Path']] = to_mef

    # Process data files
    print "\nLoading data..."
    # Get beads files data from input form
    cells_info = fc.excel_io.import_rows(input_form, "cells")

    # Load data files
    data = []
    for ci in cells_info:
        di = fc.io.TaborLabFCSData("{}/{}".format(basedir, ci['File Path']))
        data.append(di)

        gain = di[:,'FL1-H'].channel_info[0]['pmt_voltage']
        print "{} ({} events, FL1-H gain = {}).".format(str(di),
            di.shape[0], gain)

    # Basic gating/trimming
    ch_all = ['FSC-H', 'SSC-H', 'FL1-H', 'FL2-H', 'FL3-H']
    data_trimmed = [fc.gate.start_end(di, num_start=250, num_end=100)\
                                                         for di in data]
    data_trimmed = [fc.gate.high_low(di, ch_all) for di in data_trimmed]
    # Exponential transformation
    data_transf = [fc.transform.exponentiate(di, ['FSC-H', 'SSC-H']) \
                                                        for di in data_trimmed]

    # Transform to MEF
    print "\nPerforming MEF transformation..."
    data_mef = []
    for ci, di in zip(cells_info, data_transf):
        print "{}...".format(str(di))
        to_mef = to_mef_all[ci['Beads File Path']]
        data_mef.append(to_mef(di, 'FL1-H'))

    # Density gate
    print "\nRunning density gate on data files..."
    data_gated = []
    data_gated_contour = []
    for ci, di in zip(cells_info, data_mef):
        print "{} (gate fraction = {:.2f})...".format(str(di),
                ci['Gate Fraction'])
        di_gated, gate_contour = fc.gate.density2d(data = di, bins_log = True,
                                        gate_fraction = ci['Gate Fraction'])
        data_gated.append(di_gated)
        data_gated_contour.append(gate_contour)

    # Plot
    print "\nPlotting density diagrams and histograms of data"
    for di, dig, dgc in zip(data_mef, data_gated, data_gated_contour):
        print "{}...".format(str(di))
        # Plot
        fc.plot.density_and_hist(di, gated_data = dig, figsize = (7,7),
            density_channels = ['FSC-H', 'SSC-H'],
            hist_channels = ['FL1-H'], gate_contour = dgc,
            density_params = {'mode': 'scatter', 'log': True},
            hist_params = {'div': 4, 'log': True, 'edgecolor': 'g',
                'xlabel': 'Molecules of Equivalent Fluorescein (MEFL)'},
            savefig = '{}/{}.png'.format(gated_plot_dir, str(di)))
        pyplot.close()

    # Export to output excel file
    print "\nWriting output file..."
    # Calculate statistics
    for ci, di, diug in zip(cells_info, data_gated, data):
        ci['Ungated Counts'] = diug.shape[0]
        ci['Gated Counts'] = di.shape[0]
        for channel in ['FL1-H']:
            ci[channel + ' Mean'] = fc.stats.mean(di, channel)
            ci[channel + ' Geom. Mean'] = fc.stats.gmean(di, channel)
            ci[channel + ' Median'] = fc.stats.median(di, channel)
            ci[channel + ' Mode'] = fc.stats.mode(di, channel)
            ci[channel + ' Std'] = fc.stats.std(di, channel)
            ci[channel + ' CV'] = fc.stats.CV(di, channel)
            ci[channel + ' IQR'] = fc.stats.iqr(di, channel)
            ci[channel + ' RCV'] = fc.stats.RCV(di, channel)
    # Convert data
    headers = cells_info[0].keys()
    content = []
    for ci in cells_info:
        row = []
        for h in headers:
            row.append(ci[h])
        content.append(row)
    # Save output excel file
    ws = [headers] + content
    fc.excel_io.export_workbook(output_form, {'cells': ws})

    print "\nDone."
    raw_input("Press Enter to exit...")

if __name__ == "__main__":
    main()<|endoftext|>


<commit_before>#!/usr/bin/python
import random, sys, os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, 'common')))
import workload_common
from vcoptparse import *

op = workload_common.option_parser_for_memcache()
op["num_keys"] = IntFlag("--num-keys", 5000)
op["sequential"] = BoolFlag("--sequential")
op["phase"] = ChoiceFlag("--phase", ["w", "r", "wr"])
opts = op.parse(sys.argv)

with workload_common.make_memcache_connection(opts) as mc:

    if "w" in opts["phase"]:
        print "Inserting"
        keys = [str(x) for x in xrange(opts["num_keys"])]
        if not opts["sequential"]:
            random.shuffle(keys)
        i = 0
        for key in keys:
            # if (i % 500 == 0 or (i < 500 and (i & (i - 1)) == 0)):
            #     print i
            ok = mc.set(key, key)
            if ok == 0:
                raise ValueError("Could not set %r" % key)
            i += 1
        if "r" not in opts["phase"]:
            print "Dumping chosen keys to disk"
            with open("keys", "w") as keys_file:
                keys_file.write(" ".join(keys))

    if "r" in opts["phase"]:
        if "w" not in opts["phase"]:
            print "Loading chosen keys from disk"
            with open("keys", "r") as keys_file:
                keys = keys_file.read().split(" ")
        # Verify everything
        print "Verifying"
        i = 0
        for key in keys:
            if i % 16 == 0:
                values = mc.get_multi(keys[i:i+16])
            value = values[key]
            if value != key:
                raise ValueError("Key %r is set to %r, expected %r" % (key, value, key))
            i += 1

    print "Success"
<commit_msg>Add default value for --phase parameter to many_keys.py<commit_after>#!/usr/bin/python
import random, sys, os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, 'common')))
import workload_common
from vcoptparse import *

op = workload_common.option_parser_for_memcache()
op["num_keys"] = IntFlag("--num-keys", 5000)
op["sequential"] = BoolFlag("--sequential")
op["phase"] = ChoiceFlag("--phase", ["w", "r", "wr"], "wr")
opts = op.parse(sys.argv)

with workload_common.make_memcache_connection(opts) as mc:

    if "w" in opts["phase"]:
        print "Inserting"
        keys = [str(x) for x in xrange(opts["num_keys"])]
        if not opts["sequential"]:
            random.shuffle(keys)
        i = 0
        for key in keys:
            # if (i % 500 == 0 or (i < 500 and (i & (i - 1)) == 0)):
            #     print i
            ok = mc.set(key, key)
            if ok == 0:
                raise ValueError("Could not set %r" % key)
            i += 1
        if "r" not in opts["phase"]:
            print "Dumping chosen keys to disk"
            with open("keys", "w") as keys_file:
                keys_file.write(" ".join(keys))

    if "r" in opts["phase"]:
        if "w" not in opts["phase"]:
            print "Loading chosen keys from disk"
            with open("keys", "r") as keys_file:
                keys = keys_file.read().split(" ")
        # Verify everything
        print "Verifying"
        i = 0
        for key in keys:
            if i % 16 == 0:
                values = mc.get_multi(keys[i:i+16])
            value = values[key]
            if value != key:
                raise ValueError("Key %r is set to %r, expected %r" % (key, value, key))
            i += 1

    print "Success"
<|endoftext|>


<commit_before>import xarray as xr
from tikon.central.módulo import Módulo
from tikon.central.simul import SimulMódulo
from tikon.móds.clima.res import ResultadoClima
from tikon.utils import EJE_PARC, EJE_TIEMPO, EJE_COORD
from تقدیر.مقام import مقام, ذرائع_بنانا

class SimulClima(SimulMódulo):
    def __init__(símismo, mód, simul_exper, ecs, vars_interés):
        centroides = simul_exper.exper.controles['centroides']
        elev = simul_exper.exper.controles['elevaciones']
        parcelas = simul_exper.exper.controles['parcelas']
        eje_t = simul_exper.t.eje
        t_inic, t_final = eje_t[0], eje_t[-1]

        variables = mód.variables
        d_datos = {
            prc: مقام(
                centroides.loc[{EJE_PARC: prc, EJE_COORD: 'lat'}].item(),
                centroides.loc[{EJE_PARC: prc, EJE_COORD: 'lon'}].item(),
                elev.loc[{EJE_PARC: prc}].item()
            ).کوائف_پانا(
                سے=t_inic, تک=t_final, ذرائع=mód.fuentes, خاکے=mód.escenario
            ).روزانہ().loc[eje_t[0]:eje_t[-1]]
            for prc in parcelas
        }
        símismo.datos = xr.Dataset({
            res: xr.DataArray(
                [d_datos[prc][res if res in d_datos[prc] else _vr_tikon_a_taqdir(res)] for prc in parcelas],
                coords={EJE_PARC: parcelas, EJE_TIEMPO: eje_t},
                dims=[EJE_PARC, EJE_TIEMPO]
            ) for res in variables
        })
        if 'temp_prom' in símismo.datos.data_vars:
            símismo.datos['temp_prom'] = símismo.datos['temp_prom'].fillna(
                (símismo.datos['temp_máx'] + símismo.datos['temp_mín']) / 2
            )
        for vr in símismo.datos.data_vars:
            if símismo.datos[vr].isnull().any():
                raise ValueError('Faltan datos en {vr}.'.format(vr=vr))

        super().__init__(Clima, simul_exper, ecs=ecs, vars_interés=vars_interés)

    @property
    def resultados(símismo):
        l_res = []
        for var in símismo.datos:  # type: str
            cls_res = type(var, (ResultadoClima,), {'nombre': var, 'unids': lambda: None})
            l_res.append(cls_res)
        return l_res

    def requísitos(símismo, controles=False):
        if controles:
            return {'centroides', 'elevaciones'}

    def incrementar(símismo, paso, f):
        super().incrementar(paso, f)
        diarios = símismo.datos.loc[{EJE_TIEMPO: f}].drop_vars(EJE_TIEMPO)
        for res in símismo:
            símismo[res].datos = diarios[str(res)]

class Clima(Módulo):
    nombre = 'clima'
    cls_simul = SimulClima

    def __init__(símismo, fuentes=None, variables=None, escenario=8.5):
        símismo.fuentes = ذرائع_بنانا(fuentes)
        símismo.escenario = escenario
        símismo.variables = variables or list(set(vr for fnt in fuentes for vr in fnt.متغیرات))

        super().__init__()

def _vr_tikon_a_taqdir(vr):
    if vr in _conv_vars_taqdir:
        return _conv_vars_taqdir[vr]
    return vr

_conv_vars_taqdir = {
    'precip': 'بارش',
    'rad_solar': 'شمسی_تابکاری',
    'temp_máx': 'درجہ_حرارت_زیادہ',
    'temp_mín': 'درجہ_حرارت_کم',
    'temp_prom': 'درجہ_حرارت_اوسط',
    'fecha': 'تاریخ'
}
<commit_msg>Mejor así<commit_after>import xarray as xr
from tikon.central.módulo import Módulo
from tikon.central.simul import SimulMódulo
from tikon.móds.clima.res import ResultadoClima
from tikon.utils import EJE_PARC, EJE_TIEMPO, EJE_COORD
from تقدیر.مقام import مقام, ذرائع_بنانا

class SimulClima(SimulMódulo):
    def __init__(símismo, mód, simul_exper, ecs, vars_interés):
        centroides = simul_exper.exper.controles['centroides']
        elev = simul_exper.exper.controles['elevaciones']
        parcelas = simul_exper.exper.controles['parcelas']
        eje_t = simul_exper.t.eje
        t_inic, t_final = eje_t[0], eje_t[-1]

        variables = mód.variables
        d_datos = {
            prc: مقام(
                centroides.loc[{EJE_PARC: prc, EJE_COORD: 'lat'}].item(),
                centroides.loc[{EJE_PARC: prc, EJE_COORD: 'lon'}].item(),
                elev.loc[{EJE_PARC: prc}].item()
            ).کوائف_پانا(
                سے=t_inic, تک=t_final, ذرائع=mód.fuentes, خاکے=mód.escenario
            ).روزانہ().loc[eje_t[0]:eje_t[-1]]
            for prc in parcelas
        }
        símismo.datos = xr.Dataset({
            res: xr.DataArray(
                [d_datos[prc][res if res in d_datos[prc] else _vr_tikon_a_taqdir(res)] for prc in parcelas],
                coords={EJE_PARC: parcelas, EJE_TIEMPO: eje_t},
                dims=[EJE_PARC, EJE_TIEMPO]
            ) for res in variables
        })
        if all(x in símismo.datos.data_vars for x in ('temp_prom', 'temp_máx', 'temp_mín')):
            símismo.datos['temp_prom'] = símismo.datos['temp_prom'].fillna(
                (símismo.datos['temp_máx'] + símismo.datos['temp_mín']) / 2
            )
        for vr in símismo.datos.data_vars:
            if símismo.datos[vr].isnull().any():
                raise ValueError('Faltan datos en {vr}.'.format(vr=vr))

        super().__init__(Clima, simul_exper, ecs=ecs, vars_interés=vars_interés)

    @property
    def resultados(símismo):
        l_res = []
        for var in símismo.datos:  # type: str
            cls_res = type(var, (ResultadoClima,), {'nombre': var, 'unids': lambda: None})
            l_res.append(cls_res)
        return l_res

    def requísitos(símismo, controles=False):
        if controles:
            return {'centroides', 'elevaciones'}

    def incrementar(símismo, paso, f):
        super().incrementar(paso, f)
        diarios = símismo.datos.loc[{EJE_TIEMPO: f}].drop_vars(EJE_TIEMPO)
        for res in símismo:
            símismo[res].datos = diarios[str(res)]

class Clima(Módulo):
    nombre = 'clima'
    cls_simul = SimulClima

    def __init__(símismo, fuentes=None, variables=None, escenario=8.5):
        símismo.fuentes = ذرائع_بنانا(fuentes)
        símismo.escenario = escenario
        símismo.variables = variables or list(set(vr for fnt in fuentes for vr in fnt.متغیرات))

        super().__init__()

def _vr_tikon_a_taqdir(vr):
    if vr in _conv_vars_taqdir:
        return _conv_vars_taqdir[vr]
    return vr

_conv_vars_taqdir = {
    'precip': 'بارش',
    'rad_solar': 'شمسی_تابکاری',
    'temp_máx': 'درجہ_حرارت_زیادہ',
    'temp_mín': 'درجہ_حرارت_کم',
    'temp_prom': 'درجہ_حرارت_اوسط',
    'fecha': 'تاریخ'
}
<|endoftext|>


<commit_before>import glob
import re
import getopt
import sys
import os

verbose = False
directory = ''
site = ''

def print_help():
    print 'findinscope.py -d <directory> (optional)'
try:
    opts, args = getopt.getopt(sys.argv[1:],"hd:s:v",["help", "directory=", 'site=','verbose'])
except getopt.GetoptError:
    print_help()
    sys.exit(2)

for opt, arg in opts:
    if opt == '-h':
        print_help()
        sys.exit()
    elif opt in ('-d', '--directory'):
        directory = arg
    elif opt in ('-v', '--verbose'):
        verbose = True
    elif opt in ('-s', '--site'):
        site = arg

if directory is not '':
    try:
        os.chdir(directory)
    except Exception as e:
        print_help()
        print e
        sys.exit()

hostnames = []
for file in glob.glob('*.txt'):
    host = ''
    with open(file) as switch_output:
        for line in switch_output:
            h = re.search(r'^(\S+)[#>]',line)
            s = re.search(r'(WS-C(?!(?:AC-4000W-US|6K-VTT-E|6509-E-FAN))\S+).*SN: (\S+)',line)
            if h is not None:
                host = h.group(1)
            if s is not None:
                hostnames.append(host+','+s.group(1)+','+s.group(2)+'\n')

host_set = set(hostnames)
if args == []:
    if site is '':
        for hostname in host_set:
            print(hostname).rstrip()
    else:
        for hostname in host_set:
            sitecode = hostname[0:3]
            if sitecode.upper() == site.upper():
                print hostname.rstrip()
else:
    if site is '':
        with open(args[0],'wb') as x:
            x.write('Hostname,PID,Serial Number\n')
            for hostname in host_set:
                x.write(hostname)

    else:
        for hostname in host_set:
            sitecode = hostname[0:3]
            with open(args[0],'wb') as x:
                x.write('Hostname,PID,Serial Number\n')
                for hostname in host_set:
                    if sitecode.upper() == site.upper():
                        x.write(hostname)

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


<commit_before># -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from contextlib import contextmanager
import functools
import logging

from odoo.tests.common import TransactionCase

sql_logger = logging.getLogger('odoo.sql_db')

def queryCount(count):
    """ Decorate a method to check the number of queries it makes. """
    def decorate(func):
        @functools.wraps(func)
        def wrapper(self):
            # warm up the caches
            self._round = False
            func(self)
            self.env.cache.invalidate()
            # test for real, and check query count
            self._round = True
            self.resetQueryCount()
            func(self)
            self.assertLessEqual(self.cr.sql_log_count - self._count, count)

        return wrapper

    return decorate

class TestPerformance(TransactionCase):

    def resetQueryCount(self):
        """ Reset the query counter. """
        self._count = self.cr.sql_log_count

    def str(self, value):
        """ Return a value different from run to run. """
        return value + 'z' if self._round else value

    def int(self, value):
        """ Return a value different from run to run. """
        return value + 1 if self._round else value

    @contextmanager
    def logQueries(self):
        """ Log the queries that are made in this scope. """
        sql_log, level = self.cr.sql_log, sql_logger.getEffectiveLevel()
        try:
            sql_logger.setLevel(logging.DEBUG)
            self.cr.sql_log = True
            yield
        finally:
            self.cr.sql_log = sql_log
            sql_logger.setLevel(level)

    @queryCount(3)
    def test_read_base(self):
        """ Read records. """
        records = self.env['test_performance.base'].search([])
        self.assertEqual(len(records), 5)
        self.resetQueryCount()

        # without cache
        for record in records:
            record.partner_id.country_id.name

        # with cache
        for record in records:
            record.partner_id.country_id.name

        # value_pc must have been prefetched, too
        for record in records:
            record.value_pc

    @queryCount(3)
    def test_read_mail(self):
        """ Read records inheriting from 'mail.thread'. """
        records = self.env['test_performance.mail'].search([])
        self.assertEqual(len(records), 5)
        self.resetQueryCount()

        # without cache
        for record in records:
            record.partner_id.country_id.name

        # with cache
        for record in records:
            record.partner_id.country_id.name

        # value_pc must have been prefetched, too
        for record in records:
            record.value_pc

    @queryCount(1)
    def test_write_base(self):
        """ Write records (no recomputation). """
        records = self.env['test_performance.base'].search([])
        self.assertEqual(len(records), 5)
        self.resetQueryCount()

        records.write({'name': self.str('X')})

    @queryCount(3)
    def test_write_base_with_recomputation(self):
        """ Write records (with recomputation). """
        records = self.env['test_performance.base'].search([])
        self.assertEqual(len(records), 5)
        self.resetQueryCount()

        records.write({'value': self.int(20)})

    @queryCount(7)
    def test_write_mail(self):
        """ Write records inheriting from 'mail.thread' (no recomputation). """
        records = self.env['test_performance.mail'].search([])
        self.assertEqual(len(records), 5)
        self.resetQueryCount()

        records.write({'name': self.str('X')})

    @queryCount(9)
    def test_write_mail_with_recomputation(self):
        """ Write records inheriting from 'mail.thread' (with recomputation). """
        records = self.env['test_performance.mail'].search([])
        self.assertEqual(len(records), 5)
        self.resetQueryCount()

        records.write({'value': self.int(20)})

    @queryCount(45)
    def test_write_mail_with_tracking(self):
        """ Write records inheriting from 'mail.thread' (with field tracking). """
        record = self.env['test_performance.mail'].search([], limit=1)
        self.assertEqual(len(record), 1)
        self.resetQueryCount()

        record.track = self.str('X')

    @queryCount(6)
    def test_create_base(self):
        """ Create records. """
        model = self.env['test_performance.base']
        model.create({'name': self.str('X')})

    @queryCount(38)
    def test_create_base_with_lines(self):
        """ Create records with lines. """
        model = self.env['test_performance.base']
        model.create({
            'name': self.str('Y'),
            'line_ids': [(0, 0, {'value': val}) for val in range(10)],
        })

    @queryCount(3)
    def test_create_mail(self):
        """ Create records inheriting from 'mail.thread' (without field tracking). """
        model = self.env['test_performance.mail']
        model.with_context(tracking_disable=True).create({'name': self.str('X')})

    @queryCount(87)
    def test_create_mail_with_tracking(self):
        """ Create records inheriting from 'mail.thread' (with field tracking). """
        model = self.env['test_performance.mail']
        model.create({'name': self.str('Y')})
<commit_msg>[IMP] test_performance: run tests as demo user as well<commit_after># -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from contextlib import contextmanager
import functools
import logging

from odoo.tests.common import TransactionCase

sql_logger = logging.getLogger('odoo.sql_db')

def queryCount(**counters):
    """ Decorate a method to check the number of queries it makes. """
    def decorate(func):
        @functools.wraps(func)
        def wrapper(self):
            for user in self.env.user + self.env.ref('base.user_demo'):
                # switch user
                self.uid = user.id
                self.env = self.env(user=self.uid)
                # warm up the caches
                self._round = False
                func(self)
                self.env.cache.invalidate()
                # test for real, and check query count
                self._round = True
                self.resetQueryCount()
                func(self)
                self.assertLessEqual(self.cr.sql_log_count - self._count,
                                     counters[user.login],
                                     "as user %s" % user.login)

        return wrapper

    return decorate

class TestPerformance(TransactionCase):

    def resetQueryCount(self):
        """ Reset the query counter. """
        self._count = self.cr.sql_log_count

    def str(self, value):
        """ Return a value different from run to run. """
        return value + 'z' if self._round else value

    def int(self, value):
        """ Return a value different from run to run. """
        return value + 1 if self._round else value

    @contextmanager
    def logQueries(self):
        """ Log the queries that are made in this scope. """
        sql_log, level = self.cr.sql_log, sql_logger.getEffectiveLevel()
        try:
            sql_logger.setLevel(logging.DEBUG)
            self.cr.sql_log = True
            yield
        finally:
            self.cr.sql_log = sql_log
            sql_logger.setLevel(level)

    @queryCount(admin=3, demo=3)
    def test_read_base(self):
        """ Read records. """
        records = self.env['test_performance.base'].search([])
        self.assertEqual(len(records), 5)
        self.resetQueryCount()

        # without cache
        for record in records:
            record.partner_id.country_id.name

        # with cache
        for record in records:
            record.partner_id.country_id.name

        # value_pc must have been prefetched, too
        for record in records:
            record.value_pc

    @queryCount(admin=3, demo=3)
    def test_read_mail(self):
        """ Read records inheriting from 'mail.thread'. """
        records = self.env['test_performance.mail'].search([])
        self.assertEqual(len(records), 5)
        self.resetQueryCount()

        # without cache
        for record in records:
            record.partner_id.country_id.name

        # with cache
        for record in records:
            record.partner_id.country_id.name

        # value_pc must have been prefetched, too
        for record in records:
            record.value_pc

    @queryCount(admin=1, demo=1)
    def test_write_base(self):
        """ Write records (no recomputation). """
        records = self.env['test_performance.base'].search([])
        self.assertEqual(len(records), 5)
        self.resetQueryCount()

        records.write({'name': self.str('X')})

    @queryCount(admin=3, demo=3)
    def test_write_base_with_recomputation(self):
        """ Write records (with recomputation). """
        records = self.env['test_performance.base'].search([])
        self.assertEqual(len(records), 5)
        self.resetQueryCount()

        records.write({'value': self.int(20)})

    @queryCount(admin=7, demo=7)
    def test_write_mail(self):
        """ Write records inheriting from 'mail.thread' (no recomputation). """
        records = self.env['test_performance.mail'].search([])
        self.assertEqual(len(records), 5)
        self.resetQueryCount()

        records.write({'name': self.str('X')})

    @queryCount(admin=9, demo=9)
    def test_write_mail_with_recomputation(self):
        """ Write records inheriting from 'mail.thread' (with recomputation). """
        records = self.env['test_performance.mail'].search([])
        self.assertEqual(len(records), 5)
        self.resetQueryCount()

        records.write({'value': self.int(20)})

    @queryCount(admin=45, demo=62)
    def test_write_mail_with_tracking(self):
        """ Write records inheriting from 'mail.thread' (with field tracking). """
        record = self.env['test_performance.mail'].search([], limit=1)
        self.assertEqual(len(record), 1)
        self.resetQueryCount()

        record.track = self.str('X')

    @queryCount(admin=6, demo=6)
    def test_create_base(self):
        """ Create records. """
        model = self.env['test_performance.base']
        model.create({'name': self.str('X')})

    @queryCount(admin=38, demo=38)
    def test_create_base_with_lines(self):
        """ Create records with lines. """
        model = self.env['test_performance.base']
        model.create({
            'name': self.str('Y'),
            'line_ids': [(0, 0, {'value': val}) for val in range(10)],
        })

    @queryCount(admin=3, demo=3)
    def test_create_mail(self):
        """ Create records inheriting from 'mail.thread' (without field tracking). """
        model = self.env['test_performance.mail']
        model.with_context(tracking_disable=True).create({'name': self.str('X')})

    @queryCount(admin=87, demo=114)
    def test_create_mail_with_tracking(self):
        """ Create records inheriting from 'mail.thread' (with field tracking). """
        model = self.env['test_performance.mail']
        model.create({'name': self.str('Y')})
<|endoftext|>


<commit_before>#Snowbrawl
from helpers import *

arenas = {}

"""
ArenaName
PlayersPerGame
Objective
PlayersInQue
"""

@hook.event("Entity.ProjectileHitEvent", "high")
def onHit(event):
    if event.getEntity().getType() != EntityType.SNOWBALL:
        return
    if event.getWorld().getName() != "minigames":
        return
    event.getWorld().createExplosion(event.getLocation(),1)

@hook.event("PlayerInteractEvent")
def onClick(event):
    if (event.getAction() != Action.RIGHT_CLICK_BLOCK):
        return
    block = event.getClickedBlock()
    if not block.getMaterial() in [Material.SIGN_POST, Material.WALL_SIGN]:
        return
    sign_state = block.getState()
    lines      = bukkit.block.Sign.getLines(sign_state)

class Queue(Object):

    queue = []

    def __init__(self):

    def add(name):
        queue.append(name)
    def rem(names):
        for name in names:
            queue.remove(name)
    def shrink():
        queue.remove(0)

class Arena(Object):

    queue = Queue()
    size = 0
    coordinate = Coordinate(0, 0)

    def __init__(self, coordinate, size):
        self.coordinate = coordinate
        self.size = size

    def getLocation():
        return coordinate
    def getSize():
        return size
    def getQueue():
        return queue

class Coordinate(Object):

    x = 0
    z = 0

    def __init__(self, x, z):
        self.x = x
        self.z = z

    def getX():
        return x
    def getZ():
        return z

    def inRange(location, range):
        xd = location.getBlockX() - getX()
        zd = location.getBlockZ() - getZ()
        return xd >= 0 and xd <= range_ and zd >= 0 and zd <= range_
"""
class Match(Object):

    names = []

    def __init__(self, ):
"""
def getArena(location):
    for name in arenas:
        arena = arenas.get(name)
        if arena.getLocation().inRange(location, arena.getSize() - 1)
            return arena
    return None<commit_msg>added debug code<commit_after>#Snowbrawl
from helpers import *

arenas = {}

"""
ArenaName
PlayersPerGame
Objective
PlayersInQue
"""

@hook.event("Entity.ProjectileHitEvent", "high")
def onHit(event):
    print "thrown"
    if event.getEntity().getType() != EntityType.SNOWBALL:
        print "Not a snowball?"
        return
    if event.getWorld().getName() != "minigames":
        print "Not in minigames"
        return
    print "all good."
    event.getWorld().createExplosion(event.getLocation(),1)

@hook.event("PlayerInteractEvent")
def onClick(event):
    if (event.getAction() != Action.RIGHT_CLICK_BLOCK):
        return
    block = event.getClickedBlock()
    if not block.getMaterial() in [Material.SIGN_POST, Material.WALL_SIGN]:
        return
    sign_state = block.getState()
    lines      = bukkit.block.Sign.getLines(sign_state)

class Queue(Object):

    queue = []

    def __init__(self):

    def add(name):
        queue.append(name)
    def rem(names):
        for name in names:
            queue.remove(name)
    def shrink():
        queue.remove(0)

class Arena(Object):

    queue = Queue()
    size = 0
    coordinate = Coordinate(0, 0)

    def __init__(self, coordinate, size):
        self.coordinate = coordinate
        self.size = size

    def getLocation():
        return coordinate
    def getSize():
        return size
    def getQueue():
        return queue

class Coordinate(Object):

    x = 0
    z = 0

    def __init__(self, x, z):
        self.x = x
        self.z = z

    def getX():
        return x
    def getZ():
        return z

    def inRange(location, range):
        xd = location.getBlockX() - getX()
        zd = location.getBlockZ() - getZ()
        return xd >= 0 and xd <= range_ and zd >= 0 and zd <= range_
"""
class Match(Object):

    names = []

    def __init__(self, ):
"""
def getArena(location):
    for name in arenas:
        arena = arenas.get(name)
        if arena.getLocation().inRange(location, arena.getSize() - 1)
            return arena
    return None<|endoftext|>


<commit_before># Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.naming import make_autoname
from frappe import _, msgprint, throw
import frappe.defaults
from frappe.utils import flt, cint, cstr
from frappe.desk.reportview import build_match_conditions
from erpnext.utilities.transaction_base import TransactionBase
from erpnext.accounts.party import validate_party_accounts, get_dashboard_info # keep this
from frappe.contacts.address_and_contact import load_address_and_contact, delete_contact_and_address

class Customer(TransactionBase):
	def get_feed(self):
		return self.customer_name

	def onload(self):
		"""Load address and contacts in `__onload`"""
		load_address_and_contact(self, "customer")
		self.load_dashboard_info()

	def load_dashboard_info(self):
		info = get_dashboard_info(self.doctype, self.name)
		self.set_onload('dashboard_info', info)

	def autoname(self):
		cust_master_name = frappe.defaults.get_global_default('cust_master_name')
		if cust_master_name == 'Customer Name':
			self.name = self.get_customer_name()
		else:
			if not self.naming_series:
				frappe.throw(_("Series is mandatory"), frappe.MandatoryError)

			self.name = make_autoname(self.naming_series+'.#####')

	def get_customer_name(self):
		if frappe.db.get_value("Customer", self.customer_name):
			count = frappe.db.sql("""select ifnull(MAX(CAST(SUBSTRING_INDEX(name, ' ', -1) AS UNSIGNED)), 0) from tabCustomer
				 where name like %s""", "%{0} - %".format(self.customer_name), as_list=1)[0][0]
			count = cint(count) + 1
			return "{0} - {1}".format(self.customer_name, cstr(count))

		return self.customer_name

	def after_insert(self):
		'''If customer created from Lead, update customer id in quotations, opportunities'''
		self.update_lead_status()

	def validate(self):
		self.flags.is_new_doc = self.is_new()
		self.flags.old_lead = self.lead_name
		validate_party_accounts(self)
		self.validate_credit_limit_on_change()

	def on_update(self):
		self.validate_name_with_customer_group()

		if self.flags.old_lead != self.lead_name:
			self.update_lead_status()

		if self.flags.is_new_doc:
			self.create_lead_address_contact()

	def update_lead_status(self):
		'''If Customer created from Lead, update lead status to "Converted"
		update Customer link in Quotation, Opportunity'''
		if self.lead_name:
		 	frappe.db.set_value('Lead', self.lead_name, 'status', 'Converted', update_modified=False)

			for doctype in ('Opportunity', 'Quotation'):
				for d in frappe.get_all(doctype, {'lead': self.lead_name}):
					frappe.db.set_value(doctype, d.name, 'customer', self.name, update_modified=False)

	def create_lead_address_contact(self):
		if self.lead_name:
			# assign lead address to customer (if already not set)
			address_names = frappe.get_all('Dynamic Link', filters={
								"parenttype":"Address",
								"link_doctype":"Lead",
								"link_name":self.lead_name
							}, fields=["parent as name"])

			for address_name in address_names:
				address = frappe.get_doc('Address', address_name.get('name'))
				if not address.has_link('Customer', self.name):
					address.append('links', dict(link_doctype='Customer', link_name=self.name))
					address.save()

			lead = frappe.db.get_value("Lead", self.lead_name, ["lead_name", "email_id", "phone", "mobile_no", "gender", "salutation"], as_dict=True)

			lead.lead_name = lead.lead_name.split(" ")
			lead.first_name = lead.lead_name[0]
			lead.last_name = " ".join(lead.lead_name[1:])

			# create contact from lead
			contact = frappe.new_doc('Contact')
			contact.first_name = lead.first_name
			contact.last_name = lead.last_name
			contact.gender = lead.gender
			contact.salutation = lead.salutation
			contact.email_id = lead.email_id
			contact.phone = lead.phone
			contact.mobile_no = lead.mobile_no
			contact.is_primary_contact = 1
			contact.append('links', dict(link_doctype='Customer', link_name=self.name))
			contact.flags.ignore_permissions = self.flags.ignore_permissions
			contact.autoname()
			if not frappe.db.exists("Contact", contact.name):
				contact.insert()

	def validate_name_with_customer_group(self):
		if frappe.db.exists("Customer Group", self.name):
			frappe.throw(_("A Customer Group exists with same name please change the Customer name or rename the Customer Group"), frappe.NameError)

	def validate_credit_limit_on_change(self):
		if self.get("__islocal") or self.credit_limit == frappe.db.get_value("Customer", self.name, "credit_limit"):
			return

		for company in frappe.get_all("Company"):
			outstanding_amt = get_customer_outstanding(self.name, company.name)
			if flt(self.credit_limit) < outstanding_amt:
				frappe.throw(_("""New credit limit is less than current outstanding amount for the customer. Credit limit has to be atleast {0}""").format(outstanding_amt))

	def on_trash(self):
		delete_contact_and_address('Customer', self.name)
		if self.lead_name:
			frappe.db.sql("update `tabLead` set status='Interested' where name=%s", self.lead_name)

	def after_rename(self, olddn, newdn, merge=False):
		if frappe.defaults.get_global_default('cust_master_name') == 'Customer Name':
			frappe.db.set(self, "customer_name", newdn)

def get_customer_list(doctype, txt, searchfield, start, page_len, filters):
	if frappe.db.get_default("cust_master_name") == "Customer Name":
		fields = ["name", "customer_group", "territory"]
	else:
		fields = ["name", "customer_name", "customer_group", "territory"]

	match_conditions = build_match_conditions("Customer")
	match_conditions = "and {}".format(match_conditions) if match_conditions else ""

	return frappe.db.sql("""select %s from `tabCustomer` where docstatus < 2
		and (%s like %s or customer_name like %s)
		{match_conditions}
		order by
		case when name like %s then 0 else 1 end,
		case when customer_name like %s then 0 else 1 end,
		name, customer_name limit %s, %s""".format(match_conditions=match_conditions) %
		(", ".join(fields), searchfield, "%s", "%s", "%s", "%s", "%s", "%s"),
		("%%%s%%" % txt, "%%%s%%" % txt, "%%%s%%" % txt, "%%%s%%" % txt, start, page_len))

def check_credit_limit(customer, company):
	customer_outstanding = get_customer_outstanding(customer, company)

	credit_limit = get_credit_limit(customer, company)
	if credit_limit > 0 and flt(customer_outstanding) > credit_limit:
		msgprint(_("Credit limit has been crossed for customer {0} {1}/{2}")
			.format(customer, customer_outstanding, credit_limit))

		# If not authorized person raise exception
		credit_controller = frappe.db.get_value('Accounts Settings', None, 'credit_controller')
		if not credit_controller or credit_controller not in frappe.get_roles():
			throw(_("Please contact to the user who have Sales Master Manager {0} role")
				.format(" / " + credit_controller if credit_controller else ""))

def get_customer_outstanding(customer, company):
	# Outstanding based on GL Entries
	outstanding_based_on_gle = frappe.db.sql("""select sum(debit) - sum(credit)
		from `tabGL Entry` where party_type = 'Customer' and party = %s and company=%s""", (customer, company))

	outstanding_based_on_gle = flt(outstanding_based_on_gle[0][0]) if outstanding_based_on_gle else 0

	# Outstanding based on Sales Order
	outstanding_based_on_so = frappe.db.sql("""
		select sum(base_grand_total*(100 - per_billed)/100)
		from `tabSales Order`
		where customer=%s and docstatus = 1 and company=%s
		and per_billed < 100 and status != 'Closed'""", (customer, company))

	outstanding_based_on_so = flt(outstanding_based_on_so[0][0]) if outstanding_based_on_so else 0.0

	# Outstanding based on Delivery Note
	unmarked_delivery_note_items = frappe.db.sql("""select
			dn_item.name, dn_item.amount, dn.base_net_total, dn.base_grand_total
		from `tabDelivery Note` dn, `tabDelivery Note Item` dn_item
		where
			dn.name = dn_item.parent
			and dn.customer=%s and dn.company=%s
			and dn.docstatus = 1 and dn.status not in ('Closed', 'Stopped')
			and ifnull(dn_item.against_sales_order, '') = ''
			and ifnull(dn_item.against_sales_invoice, '') = ''""", (customer, company), as_dict=True)

	outstanding_based_on_dn = 0.0

	for dn_item in unmarked_delivery_note_items:
		si_amount = frappe.db.sql("""select sum(amount)
			from `tabSales Invoice Item`
			where dn_detail = %s and docstatus = 1""", dn_item.name)[0][0]

		if flt(dn_item.amount) > flt(si_amount) and dn_item.base_net_total:
			outstanding_based_on_dn += ((flt(dn_item.amount) - flt(si_amount)) \
				/ dn_item.base_net_total) * dn_item.base_grand_total

	return outstanding_based_on_gle + outstanding_based_on_so + outstanding_based_on_dn

def get_credit_limit(customer, company):
	credit_limit = None

	if customer:
		credit_limit, customer_group = frappe.db.get_value("Customer", customer, ["credit_limit", "customer_group"])

		if not credit_limit:
			credit_limit = frappe.db.get_value("Customer Group", customer_group, "credit_limit")

	if not credit_limit:
		credit_limit = frappe.db.get_value("Company", company, "credit_limit")

	return flt(credit_limit)
<commit_msg>[Fix] Data not showing in the heatmap of the customer (#9819)<commit_after># Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.naming import make_autoname
from frappe import _, msgprint, throw
import frappe.defaults
from frappe.utils import flt, cint, cstr
from frappe.desk.reportview import build_match_conditions
from erpnext.utilities.transaction_base import TransactionBase
from erpnext.accounts.party import validate_party_accounts, get_dashboard_info, get_timeline_data # keep this
from frappe.contacts.address_and_contact import load_address_and_contact, delete_contact_and_address

class Customer(TransactionBase):
	def get_feed(self):
		return self.customer_name

	def onload(self):
		"""Load address and contacts in `__onload`"""
		load_address_and_contact(self, "customer")
		self.load_dashboard_info()

	def load_dashboard_info(self):
		info = get_dashboard_info(self.doctype, self.name)
		self.set_onload('dashboard_info', info)

	def autoname(self):
		cust_master_name = frappe.defaults.get_global_default('cust_master_name')
		if cust_master_name == 'Customer Name':
			self.name = self.get_customer_name()
		else:
			if not self.naming_series:
				frappe.throw(_("Series is mandatory"), frappe.MandatoryError)

			self.name = make_autoname(self.naming_series+'.#####')

	def get_customer_name(self):
		if frappe.db.get_value("Customer", self.customer_name):
			count = frappe.db.sql("""select ifnull(MAX(CAST(SUBSTRING_INDEX(name, ' ', -1) AS UNSIGNED)), 0) from tabCustomer
				 where name like %s""", "%{0} - %".format(self.customer_name), as_list=1)[0][0]
			count = cint(count) + 1
			return "{0} - {1}".format(self.customer_name, cstr(count))

		return self.customer_name

	def after_insert(self):
		'''If customer created from Lead, update customer id in quotations, opportunities'''
		self.update_lead_status()

	def validate(self):
		self.flags.is_new_doc = self.is_new()
		self.flags.old_lead = self.lead_name
		validate_party_accounts(self)
		self.validate_credit_limit_on_change()

	def on_update(self):
		self.validate_name_with_customer_group()

		if self.flags.old_lead != self.lead_name:
			self.update_lead_status()

		if self.flags.is_new_doc:
			self.create_lead_address_contact()

	def update_lead_status(self):
		'''If Customer created from Lead, update lead status to "Converted"
		update Customer link in Quotation, Opportunity'''
		if self.lead_name:
		 	frappe.db.set_value('Lead', self.lead_name, 'status', 'Converted', update_modified=False)

			for doctype in ('Opportunity', 'Quotation'):
				for d in frappe.get_all(doctype, {'lead': self.lead_name}):
					frappe.db.set_value(doctype, d.name, 'customer', self.name, update_modified=False)

	def create_lead_address_contact(self):
		if self.lead_name:
			# assign lead address to customer (if already not set)
			address_names = frappe.get_all('Dynamic Link', filters={
								"parenttype":"Address",
								"link_doctype":"Lead",
								"link_name":self.lead_name
							}, fields=["parent as name"])

			for address_name in address_names:
				address = frappe.get_doc('Address', address_name.get('name'))
				if not address.has_link('Customer', self.name):
					address.append('links', dict(link_doctype='Customer', link_name=self.name))
					address.save()

			lead = frappe.db.get_value("Lead", self.lead_name, ["lead_name", "email_id", "phone", "mobile_no", "gender", "salutation"], as_dict=True)

			lead.lead_name = lead.lead_name.split(" ")
			lead.first_name = lead.lead_name[0]
			lead.last_name = " ".join(lead.lead_name[1:])

			# create contact from lead
			contact = frappe.new_doc('Contact')
			contact.first_name = lead.first_name
			contact.last_name = lead.last_name
			contact.gender = lead.gender
			contact.salutation = lead.salutation
			contact.email_id = lead.email_id
			contact.phone = lead.phone
			contact.mobile_no = lead.mobile_no
			contact.is_primary_contact = 1
			contact.append('links', dict(link_doctype='Customer', link_name=self.name))
			contact.flags.ignore_permissions = self.flags.ignore_permissions
			contact.autoname()
			if not frappe.db.exists("Contact", contact.name):
				contact.insert()

	def validate_name_with_customer_group(self):
		if frappe.db.exists("Customer Group", self.name):
			frappe.throw(_("A Customer Group exists with same name please change the Customer name or rename the Customer Group"), frappe.NameError)

	def validate_credit_limit_on_change(self):
		if self.get("__islocal") or self.credit_limit == frappe.db.get_value("Customer", self.name, "credit_limit"):
			return

		for company in frappe.get_all("Company"):
			outstanding_amt = get_customer_outstanding(self.name, company.name)
			if flt(self.credit_limit) < outstanding_amt:
				frappe.throw(_("""New credit limit is less than current outstanding amount for the customer. Credit limit has to be atleast {0}""").format(outstanding_amt))

	def on_trash(self):
		delete_contact_and_address('Customer', self.name)
		if self.lead_name:
			frappe.db.sql("update `tabLead` set status='Interested' where name=%s", self.lead_name)

	def after_rename(self, olddn, newdn, merge=False):
		if frappe.defaults.get_global_default('cust_master_name') == 'Customer Name':
			frappe.db.set(self, "customer_name", newdn)

def get_customer_list(doctype, txt, searchfield, start, page_len, filters):
	if frappe.db.get_default("cust_master_name") == "Customer Name":
		fields = ["name", "customer_group", "territory"]
	else:
		fields = ["name", "customer_name", "customer_group", "territory"]

	match_conditions = build_match_conditions("Customer")
	match_conditions = "and {}".format(match_conditions) if match_conditions else ""

	return frappe.db.sql("""select %s from `tabCustomer` where docstatus < 2
		and (%s like %s or customer_name like %s)
		{match_conditions}
		order by
		case when name like %s then 0 else 1 end,
		case when customer_name like %s then 0 else 1 end,
		name, customer_name limit %s, %s""".format(match_conditions=match_conditions) %
		(", ".join(fields), searchfield, "%s", "%s", "%s", "%s", "%s", "%s"),
		("%%%s%%" % txt, "%%%s%%" % txt, "%%%s%%" % txt, "%%%s%%" % txt, start, page_len))

def check_credit_limit(customer, company):
	customer_outstanding = get_customer_outstanding(customer, company)

	credit_limit = get_credit_limit(customer, company)
	if credit_limit > 0 and flt(customer_outstanding) > credit_limit:
		msgprint(_("Credit limit has been crossed for customer {0} {1}/{2}")
			.format(customer, customer_outstanding, credit_limit))

		# If not authorized person raise exception
		credit_controller = frappe.db.get_value('Accounts Settings', None, 'credit_controller')
		if not credit_controller or credit_controller not in frappe.get_roles():
			throw(_("Please contact to the user who have Sales Master Manager {0} role")
				.format(" / " + credit_controller if credit_controller else ""))

def get_customer_outstanding(customer, company):
	# Outstanding based on GL Entries
	outstanding_based_on_gle = frappe.db.sql("""select sum(debit) - sum(credit)
		from `tabGL Entry` where party_type = 'Customer' and party = %s and company=%s""", (customer, company))

	outstanding_based_on_gle = flt(outstanding_based_on_gle[0][0]) if outstanding_based_on_gle else 0

	# Outstanding based on Sales Order
	outstanding_based_on_so = frappe.db.sql("""
		select sum(base_grand_total*(100 - per_billed)/100)
		from `tabSales Order`
		where customer=%s and docstatus = 1 and company=%s
		and per_billed < 100 and status != 'Closed'""", (customer, company))

	outstanding_based_on_so = flt(outstanding_based_on_so[0][0]) if outstanding_based_on_so else 0.0

	# Outstanding based on Delivery Note
	unmarked_delivery_note_items = frappe.db.sql("""select
			dn_item.name, dn_item.amount, dn.base_net_total, dn.base_grand_total
		from `tabDelivery Note` dn, `tabDelivery Note Item` dn_item
		where
			dn.name = dn_item.parent
			and dn.customer=%s and dn.company=%s
			and dn.docstatus = 1 and dn.status not in ('Closed', 'Stopped')
			and ifnull(dn_item.against_sales_order, '') = ''
			and ifnull(dn_item.against_sales_invoice, '') = ''""", (customer, company), as_dict=True)

	outstanding_based_on_dn = 0.0

	for dn_item in unmarked_delivery_note_items:
		si_amount = frappe.db.sql("""select sum(amount)
			from `tabSales Invoice Item`
			where dn_detail = %s and docstatus = 1""", dn_item.name)[0][0]

		if flt(dn_item.amount) > flt(si_amount) and dn_item.base_net_total:
			outstanding_based_on_dn += ((flt(dn_item.amount) - flt(si_amount)) \
				/ dn_item.base_net_total) * dn_item.base_grand_total

	return outstanding_based_on_gle + outstanding_based_on_so + outstanding_based_on_dn

def get_credit_limit(customer, company):
	credit_limit = None

	if customer:
		credit_limit, customer_group = frappe.db.get_value("Customer", customer, ["credit_limit", "customer_group"])

		if not credit_limit:
			credit_limit = frappe.db.get_value("Customer Group", customer_group, "credit_limit")

	if not credit_limit:
		credit_limit = frappe.db.get_value("Company", company, "credit_limit")

	return flt(credit_limit)
<|endoftext|>


<commit_before>import os

from django.test import RequestFactory

from onadata.apps.api.viewsets.xform_viewset import XFormViewSet
from onadata.apps.main.tests.test_base import TestBase

class TestExportViewSet(TestBase):

    def setUp(self):
        super(self.__class__, self).setUp()
        self._create_user_and_login()
        self._publish_transportation_form()
        self.factory = RequestFactory()
        self.extra = {
            'HTTP_AUTHORIZATION': 'Token %s' % self.user.auth_token}

    def _filename_from_disposition(self, content_disposition):
        filename_pos = content_disposition.index('filename=')
        self.assertTrue(filename_pos != -1)
        return content_disposition[filename_pos + len('filename='):]

    def test_form_data_export(self):
        self._make_submissions()
        view = XFormViewSet.as_view({
            'get': 'retrieve'
        })
        formid = self.xform.pk
        # csv
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid, format='csv')
        self.assertEqual(response.status_code, 200)
        headers = dict(response.items())
        content_disposition = headers['Content-Disposition']
        filename = self._filename_from_disposition(content_disposition)
        basename, ext = os.path.splitext(filename)
        self.assertEqual(headers['Content-Type'], 'application/csv')
        self.assertEqual(ext, '.csv')

        # xls
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid, format='xls')
        self.assertEqual(response.status_code, 200)
        headers = dict(response.items())
        content_disposition = headers['Content-Disposition']
        filename = self._filename_from_disposition(content_disposition)
        basename, ext = os.path.splitext(filename)
        self.assertEqual(headers['Content-Type'],
                         'application/vnd.openxmlformats')
        self.assertEqual(ext, '.xlsx')
<commit_msg>ME: added a test for data endpoint xls export<commit_after>import os

from django.test import RequestFactory

from onadata.apps.api.viewsets.xform_viewset import XFormViewSet
from onadata.apps.api.viewsets.data_viewset import DataViewSet
from onadata.apps.main.tests.test_base import TestBase

class TestExportViewSet(TestBase):

    def setUp(self):
        super(self.__class__, self).setUp()
        self._create_user_and_login()
        self._publish_transportation_form()
        self.factory = RequestFactory()
        self.extra = {
            'HTTP_AUTHORIZATION': 'Token %s' % self.user.auth_token}

    def _filename_from_disposition(self, content_disposition):
        filename_pos = content_disposition.index('filename=')
        self.assertTrue(filename_pos != -1)
        return content_disposition[filename_pos + len('filename='):]

    def test_form_data_export(self):
        self._make_submissions()
        view = XFormViewSet.as_view({
            'get': 'retrieve'
        })
        formid = self.xform.pk
        # csv
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid, format='csv')
        self.assertEqual(response.status_code, 200)
        headers = dict(response.items())
        content_disposition = headers['Content-Disposition']
        filename = self._filename_from_disposition(content_disposition)
        basename, ext = os.path.splitext(filename)
        self.assertEqual(headers['Content-Type'], 'application/csv')
        self.assertEqual(ext, '.csv')

        # xls
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid, format='xls')
        self.assertEqual(response.status_code, 200)
        headers = dict(response.items())
        content_disposition = headers['Content-Disposition']
        filename = self._filename_from_disposition(content_disposition)
        basename, ext = os.path.splitext(filename)
        self.assertEqual(headers['Content-Type'],
                         'application/vnd.openxmlformats')
        self.assertEqual(ext, '.xlsx')

    def test_data_export(self):
        self._make_submissions()
        view = DataViewSet.as_view({
            'get': 'list'
        })
        formid = self.xform.pk
        # csv
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid, format='csv')
        self.assertEqual(response.status_code, 200)
        headers = dict(response.items())
        content_disposition = headers['Content-Disposition']
        filename = self._filename_from_disposition(content_disposition)
        basename, ext = os.path.splitext(filename)
        self.assertEqual(headers['Content-Type'], 'application/csv')
        self.assertEqual(ext, '.csv')

        # xls
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid, format='xls')
        self.assertEqual(response.status_code, 200)
        headers = dict(response.items())
        content_disposition = headers['Content-Disposition']
        filename = self._filename_from_disposition(content_disposition)
        basename, ext = os.path.splitext(filename)
        self.assertEqual(headers['Content-Type'],
                         'application/vnd.openxmlformats')
        self.assertEqual(ext, '.xlsx')
<|endoftext|>


<commit_before>from django.core.cache import cache
from django.http import HttpResponseBadRequest, HttpResponseForbidden
import hashlib

def throttle_view(func, methods=None, duration=15):
    """If I was a better programmer, I would get this to work with passed arguments,
    however - it's just not in the cards tonight. hence ...
    TODO: make this thing actually take arguments
    """
    def inner(request, *args, **kwargs):
        throttled_methods = methods if methods else ['POST', 'GET']
        if request.method in throttled_methods:
            remote_addr = request.META.get('HTTP_X_FORWARDED_FOR') or \
                          request.META.get('REMOTE_ADDR')
            key = (hashlib.md5('%s.%s' % (remote_addr, request.path_info))
                   .hexdigest())
            if cache.get(key):
                return HttpResponseForbidden("Sorry, you can only publish a page every %s seconds, try again in a bit" % duration)
            else:
                cache.set(key, True, duration)
                return func(request, *args, **kwargs)
    return inner

<commit_msg>removing some odd redundancy<commit_after>from django.core.cache import cache
from django.http import HttpResponseBadRequest, HttpResponseForbidden
import hashlib

def throttle_view(func, methods=['POST', 'GET'], duration=15):
    """If I was a better programmer, I would get this to work with passed arguments,
    however - it's just not in the cards tonight. hence ...
    TODO: make this thing actually take arguments
    """
    def inner(request, *args, **kwargs):
        if request.method in methods:
            remote_addr = request.META.get('HTTP_X_FORWARDED_FOR') or \
                          request.META.get('REMOTE_ADDR')
            key = (hashlib.md5('%s.%s' % (remote_addr, request.path_info))
                   .hexdigest())
            if cache.get(key):
                return HttpResponseForbidden("Sorry, you can only publish a page every %s seconds, try again in a bit" % duration)
            else:
                cache.set(key, True, duration)
                return func(request, *args, **kwargs)
    return inner

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""The code."""

class User(object):

    """The user model."""

    def __init__(self, id, version=None):
        """Create a new user with the given id."""
        self.id = id
        self.adjacent = set()
        self._version = version
        self.infection = None

    def connect(self, other):
        """Add a new relationship."""
        self.adjacent.add(other)
        other.adjacent.add(self)

    @property
    def version(self):
        """Try to get the version from the infection.

        If we aren't part of an infection, just return _version.
        """
        if self.infection is not None and self.infection.version is not None:
                return self.infection.version
        return self._version

class Infection(set):

    """A group of connected users, all running the same version."""

    def __init__(self, version=None):
        """If specified, version will be used by all contained users."""
        set.__init__(self)
        self.version = version

def infect(user, infection=None):
    """Add all users connected to user to a common infection group.

    Return the infection.

    This is just a DFS, should run in O(n) time.
    """
    if user.infection is not None:
        return user.infection

    # Default to a new infection
    if infection is None:
        infection = Infection()

    infection.add(user)
    user.infection = infection
    for adjacent in user.adjacent:
        infect(adjacent, infection)

    return infection

def total_infection(user, new_version):
    """Infect all the users connected to user.

    This runs in O(n).
    """
    infection = infect(user)
    infection.version = new_version

def main():
    """The main function."""

if __name__ == '__main__':
    main()
<commit_msg>Return the infection group from total_infection<commit_after># -*- coding: utf-8 -*-
"""The code."""

class User(object):

    """The user model."""

    def __init__(self, id, version=None):
        """Create a new user with the given id."""
        self.id = id
        self.adjacent = set()
        self._version = version
        self.infection = None

    def connect(self, other):
        """Add a new relationship."""
        self.adjacent.add(other)
        other.adjacent.add(self)

    @property
    def version(self):
        """Try to get the version from the infection.

        If we aren't part of an infection, just return _version.
        """
        if self.infection is not None and self.infection.version is not None:
                return self.infection.version
        return self._version

class Infection(set):

    """A group of connected users, all running the same version."""

    def __init__(self, version=None):
        """If specified, version will be used by all contained users."""
        set.__init__(self)
        self.version = version

def infect(user, infection=None):
    """Add all users connected to user to a common infection group.

    Return the infection.

    This is just a DFS, should run in O(n) time.
    """
    if user.infection is not None:
        return user.infection

    # Default to a new infection
    if infection is None:
        infection = Infection()

    infection.add(user)
    user.infection = infection
    for adjacent in user.adjacent:
        infect(adjacent, infection)

    return infection

def total_infection(user, new_version):
    """Infect all the users connected to user.

    This runs in O(n).
    """
    infection = infect(user)
    infection.version = new_version
    return infection

def main():
    """The main function."""

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>#!/usr/bin/python3

import os
import sys
import pwd
import struct
import socket
import hashlib
import logging

logger = logging.getLogger("sock2proc")

class ProcInfo(object):

    sock = -1 # used to find proc info vi proc/etc/net

    pid = -1
    euid = -1
    user = ""
    cmd = ""
    arg = ""
    env = ""
    clnt = ""
    binh = ""
    bindir = ""

    # Either sock (local tcp) or pid/euid (unix stream) must be present
    def __init__(self, sock = -1, pid = -1, euid = -1):
        self.sock = sock
        self.pid = pid
        self.euid = euid

    def proc_info(self):

        # Either pid or sock must be available

        if (self.pid == -1):
            if (self.sock ==""):
                return False
            else:
                inode, self.euid = ProcInfo.find_inode(self.sock)
                if (inode == -1):
                    return False

                self.pid = ProcInfo.find_pid(inode)
                if (self.pid == -1):
                    return False

        self.user = pwd.getpwuid(int(self.euid))[0];
        logger.info("user = %s", self.user)

        fname = open("/proc/" + self.pid + "/cmdline", "r")
        self.cmd = fname.readline()
        self.cmd = self.cmd.strip()
        fname.close()

        if (len(self.cmd) != 0):
             argv = self.cmd.split("\0")
             logger.info("proc %s argv: %s", self.pid, argv)
             self.cmd = argv[0]
             for i in range(1, len(argv)):
                 self.arg += argv[i] + " "
             self.arg = self.arg.strip(' ')

        #Special case where args part of command
        #TODO bug if space in filename (escaping)
        idx = self.cmd.find(" ")
        if (idx > 0):
            self.arg += self.cmd[(idx + 1):].strip()
            self.cmd = self.cmd[:idx].strip()

        logger.info("clnt exe: %s", self.cmd)
        logger.info("clnt arg: %s", self.arg)

        self.bindir, self.cmd = os.path.split(self.cmd)

        logger.info("clnt bin dir: %s", self.bindir)

        self.clnt = self.cmd + "@" + socket.getfqdn() + "#" + self.user
        logger.info("clnt id: %s", self.clnt)

        self.binh = ProcInfo.hash_file("/proc/" + self.pid + "/exe")

        logger.info("clnt exe hash: %s",self.binh)

        fname = open("/proc/" + self.pid + "/environ", "r")
        self.env = fname.readline()
        fname.close()
        self.env = self.env.replace("\0", "\n").strip()
        logger.info("clnt env: %s", self.env)

        return True

    # Find the inode of a local sock address
    @staticmethod
    def find_inode(sock):

        inode = -1
        euid = -1;

        fname = open("/proc/net/tcp", "r")
        fname.readline() # discard the 1st line
        while (True):
            line = fname.readline();
            if not line:
                break
            items = line.split()
            if (items[1].find(sock.upper()) != -1) :
                euid = items[7]
                inode = items[9]
                break

        fname.close()

        logger.info("euid = %s", euid)

        if (euid == -1):
            return -1, -1

        return int(inode), euid

    # Find the pid given its inode
    @staticmethod
    def find_pid(inode):

        files = os.listdir("/proc/")

        files.remove(str(os.getpid()))

        pids = []

        for f in files:
            try:
                integer = int(f)
                pids.append(str(integer))
            except ValueError:
                # don't care if not a pid
                pass

        for pid in pids:
            fds = os.listdir("/proc/%s/fd/" % pid)
            for fd in fds:
                if ('socket:[%d]' % inode) == os.readlink("/proc/%s/fd/%s" % (pid, fd)):
                    return pid
        return -1

    @staticmethod
    def hash_file(file):

        hash = ""

        with open(file, 'rb') as fh:
            m = hashlib.sha1()
            while True:
                data = fh.read(8192)
                if not data:
                    break
                m.update(data)
            hash = m.hexdigest()

        return hash

    @staticmethod
    def ip_to_nl(ip):
        t = socket.inet_aton(ip)
        return struct.unpack("I", t)[0]

    @staticmethod
    def sock2proc(client_address):

        logger.info ("client sock: %s", client_address);

        host, port = client_address

        clnt = hex(ProcInfo.ip_to_nl(host)).lstrip('0x') + ":" + hex(int(port)).lstrip('0x')

        proc = ProcInfo(sock = clnt)

        proc.proc_info()

        return proc

    @staticmethod
    def pipe2proc(pid, euid):

        logger.info ("client pid: %s, euid: %s", pid, euid);

        proc = ProcInfo(pid = pid, euid = euid)

        proc.proc_info()

        return proc

# Entrance for stand-alone execution
def main():

    logging.basicConfig(format='%(asctime)s %(levelname)s %(name)s %(message)s', level=logging.INFO)

    proc = ProcInfo.pipe2proc(sys.argv[1], sys.argv[2])

if __name__ == "__main__":

    main()

<commit_msg>envs formated to dict<commit_after>#!/usr/bin/python3

import os
import sys
import pwd
import struct
import socket
import hashlib
import logging

logger = logging.getLogger("sock2proc")

class ProcInfo(object):

    sock = -1 # used to find proc info vi proc/etc/net

    pid = -1
    euid = -1
    user = ""
    cmd = ""
    arg = ""
    env = {}
    clnt = ""
    binh = ""
    bindir = ""

    # Either sock (local tcp) or pid/euid (unix stream) must be present
    def __init__(self, sock = -1, pid = -1, euid = -1):
        self.sock = sock
        self.pid = pid
        self.euid = euid

    def proc_info(self):

        # Either pid or sock must be available

        if (self.pid == -1):
            if (self.sock ==""):
                return False
            else:
                inode, self.euid = ProcInfo.find_inode(self.sock)
                if (inode == -1):
                    return False

                self.pid = ProcInfo.find_pid(inode)
                if (self.pid == -1):
                    return False

        self.user = pwd.getpwuid(int(self.euid))[0];
        logger.info("user = %s", self.user)

        fname = open("/proc/" + self.pid + "/cmdline", "r")
        self.cmd = fname.readline()
        self.cmd = self.cmd.strip()
        fname.close()

        if (len(self.cmd) != 0):
             argv = self.cmd.split("\0")
             logger.info("proc %s argv: %s", self.pid, argv)
             self.cmd = argv[0]
             for i in range(1, len(argv)):
                 self.arg += argv[i] + " "
             self.arg = self.arg.strip(' ')

        #Special case where args part of command
        #TODO bug if space in filename (escaping)
        idx = self.cmd.find(" ")
        if (idx > 0):
            self.arg += self.cmd[(idx + 1):].strip()
            self.cmd = self.cmd[:idx].strip()

        logger.info("clnt exe: %s", self.cmd)
        logger.info("clnt arg: %s", self.arg)

        self.bindir, self.cmd = os.path.split(self.cmd)

        logger.info("clnt bin dir: %s", self.bindir)

        self.clnt = self.cmd + "@" + socket.getfqdn() + "#" + self.user
        logger.info("clnt id: %s", self.clnt)

        self.binh = ProcInfo.hash_file("/proc/" + self.pid + "/exe")

        logger.info("clnt exe hash: %s",self.binh)

        fname = open("/proc/" + self.pid + "/environ", "r")
        envlist = fname.readline()
        fname.close()

        envlist = envlist.split("\0")

        for i in range(0, len(envlist)) :
             nv = envlist[i].split("=", 2)
             if (len(nv[0]) == 0):
                 break
             self.env[nv[0].strip()] = nv[1].strip()

        logger.info("clnt env: %s", self.env)

        return True

    # Find the inode of a local sock address
    @staticmethod
    def find_inode(sock):

        inode = -1
        euid = -1;

        fname = open("/proc/net/tcp", "r")
        fname.readline() # discard the 1st line
        while (True):
            line = fname.readline();
            if not line:
                break
            items = line.split()
            if (items[1].find(sock.upper()) != -1) :
                euid = items[7]
                inode = items[9]
                break

        fname.close()

        logger.info("euid = %s", euid)

        if (euid == -1):
            return -1, -1

        return int(inode), euid

    # Find the pid given its inode
    @staticmethod
    def find_pid(inode):

        files = os.listdir("/proc/")

        files.remove(str(os.getpid()))

        pids = []

        for f in files:
            try:
                integer = int(f)
                pids.append(str(integer))
            except ValueError:
                # don't care if not a pid
                pass

        for pid in pids:
            fds = os.listdir("/proc/%s/fd/" % pid)
            for fd in fds:
                if ('socket:[%d]' % inode) == os.readlink("/proc/%s/fd/%s" % (pid, fd)):
                    return pid
        return -1

    @staticmethod
    def hash_file(file):

        hash = ""

        with open(file, 'rb') as fh:
            m = hashlib.sha1()
            while True:
                data = fh.read(8192)
                if not data:
                    break
                m.update(data)
            hash = m.hexdigest()

        return hash

    @staticmethod
    def ip_to_nl(ip):
        t = socket.inet_aton(ip)
        return struct.unpack("I", t)[0]

    @staticmethod
    def sock2proc(client_address):

        logger.info ("client sock: %s", client_address);

        host, port = client_address

        clnt = hex(ProcInfo.ip_to_nl(host)).lstrip('0x') + ":" + hex(int(port)).lstrip('0x')

        proc = ProcInfo(sock = clnt)

        proc.proc_info()

        return proc

    @staticmethod
    def pipe2proc(pid, euid):

        logger.info ("client pid: %s, euid: %s", pid, euid);

        proc = ProcInfo(pid = pid, euid = euid)

        proc.proc_info()

        return proc

# Entrance for stand-alone execution
def main():

    logging.basicConfig(format='%(asctime)s %(levelname)s %(name)s %(message)s', level=logging.INFO)

    proc = ProcInfo.pipe2proc(sys.argv[1], sys.argv[2])

if __name__ == "__main__":

    main()

<|endoftext|>