<commit_before># pylint: disable=invalid-name
# pylint: disable=missing-docstring
# pylint: disable=too-few-public-methods
# pylint: disable=too-many-instance-attributes
# pylint: disable=too-many-locals

import json
import os
import re
import subprocess
import sys
import warnings
import yaml

TESTCASES = list()
INVENTORY = 'test/fixtures/hosts'

HERE = os.path.abspath(os.path.dirname(__file__))
ROLE = re.match(
    r'^.*\/ansible-eos-([^/\s]+)\/test/arista-ansible-role-test$', HERE).group(1)
RUN_CONFIG_BACKUP = '_eos_role_test_{}_running'.format(ROLE)
START_CONFIG_BACKUP = '_eos_role_test_{}_startup'.format(ROLE)

EOS_ROLE_PLAYBOOK = 'test/arista-ansible-role-test/eos_role.yml'
EOS_MODULE_PLAYBOOK = 'test/arista-ansible-role-test/eos_module.yml'

LOG_FILE = '{}/roletest.log'.format(HERE)
try:
    os.remove(LOG_FILE)
except OSError:
    pass
LOG = open(LOG_FILE, 'w')

SEPARATOR = '    ' + '*' * 50

class TestCase(object):
    def __init__(self, **kwargs):
        self.name = kwargs['name']
        self.module = kwargs['module']

        self.host = None

        self.inventory = kwargs.get('inventory')
        self.exitcode = kwargs.get('exitcode', 0)
        self.idempotent = kwargs.get('idempotent', True)
        self.changed = kwargs.get('changed', True)
        self.present = kwargs.get('present')
        self.absent = kwargs.get('absent')

        self.arguments = kwargs.get('arguments', list())
        self.variables = dict()

        # optional properties
        self.setup = kwargs.get('setup', list())
        self.teardown = kwargs.get('teardown', list())

    def __str__(self):
        return self.name

class TestModule(object):
    def __init__(self, testcase):
        self.testcase = testcase
        self.description = 'Test [%s]: %s' % (testcase.module, testcase.name)

    def __call__(self):
        self.output('Run first pass')
        response = self.run_module()
        for device in response:
            hostname = device.keys()[0]
            reported = int(device[hostname]['changed'])
            expected = int(self.testcase.changed)
            msg = ("First pass role execution reported {} task change(s), "
                   "expected {}".format(reported, expected))
            self.output(msg)
            assert reported == expected, msg

        if self.testcase.idempotent:
            self.output('Run second pass')
            response = self.run_module()
            for device in response:
                hostname = device.keys()[0]
                reported = int(device[hostname]['changed'])
                msg = (
                    "Second pass role execution reported {} task change(s), "
                    "expected 0".format(reported))
                self.output(msg)
                assert not reported, msg

        if self.testcase.present:
            desc = 'Validate present configuration'
            self.output(desc)
            response = self.run_validation(self.testcase.present, desc=desc)
            for device in response:
                hostname = device.keys()[0]
                # Result should contain an empty list of updates
                delim = " ---\n"
                updates = device[hostname]['updates']
                msg = ("{} - Expected configuration\n{}{}\n{}not found "
                       "on device '{}'".format(desc, delim,
                                               '\n'.join(updates), delim,
                                               hostname))
                assert device[hostname]['updates'] == [], msg
                # Result should show no changes
                msg = ("{} - Device '{}' reported no updates, but "
                       "returned 'changed'".format(desc, hostname))
                assert device[hostname]['changed'] == False, msg

        if self.testcase.absent:
            desc = 'Validate absent configuration'
            self.output(desc)
            response = self.run_validation(self.testcase.absent, desc=desc)
            for device in response:
                hostname = device.keys()[0]
                # Result should show change has taken place
                msg = (
                    "{} - Entire absent configuration found on device '{}'".
                    format(desc, hostname)
                )
                assert device[hostname]['changed'] == True, msg
                # Compare changes with expected values, sorted at global level
                updates = '\n'.join(device[hostname]['updates'])
                updates = re.split(r'\n(?=\S)', updates)
                updates = '\n'.join(sorted(updates))
                absent = re.split(r'\n(?=\S)', self.testcase.absent.rstrip())
                absent = '\n'.join(sorted(absent))
                msg = ("{} - Some part of absent configuration found "
                       "on device '{}'".format(desc, hostname))
                assert updates == absent, msg

    def setUp(self):
        print("\n{}\n".format(SEPARATOR) +
              "  See run log for complete output:\n  {}".format(LOG_FILE) +
              "\n{}\n".format(SEPARATOR))

        LOG.write("\n\n\n{}\n".format(SEPARATOR) +
                  "  Begin log for {}".format(self.description) +
                  "\n{}\n\n".format(SEPARATOR))

        if self.testcase.setup:
            self.output('Running test case setup commands')
            setup_cmds = self.testcase.setup
            if not isinstance(setup_cmds, list):
                setup_cmds = setup_cmds.splitlines()
            self.output("{}".format(setup_cmds))

            args = {
                'module': 'eos_command',
                'description': 'Run test case setup commands',
                'cmds': ['configure terminal'] + setup_cmds,
            }

            arguments = [json.dumps(args)]

            ret_code, out, err = ansible_playbook(EOS_MODULE_PLAYBOOK,
                                                  arguments=arguments)

            if ret_code != 0:
                LOG.write("Playbook stdout:\n\n{}".format(out))
                LOG.write("Playbook stderr:\n\n{}".format(err))
                raise

    def tearDown(self):
        if self.testcase.teardown:
            self.output('Running test case teardown commands')
            teardown_cmds = self.testcase.teardown
            if not isinstance(teardown_cmds, list):
                teardown_cmds = teardown_cmds.splitlines()
            self.output("{}\n".format(teardown_cmds))

            args = {
                'module': 'eos_command',
                'description': 'Run test case teardown_cmds commands',
                'cmds': ['configure terminal'] + teardown_cmds,
            }

            arguments = [json.dumps(args)]

            ret_code, out, err = ansible_playbook(EOS_MODULE_PLAYBOOK,
                                                  arguments=arguments)

            if ret_code != 0:
                self.output("Playbook stdout:\n\n{}".format(out))
                self.output("Playbook stderr:\n\n{}".format(err))
                warnings.warn("\nError in test case teardown\n\n{}".format(
                    out))

    @classmethod
    def output(cls, text):
        print '>>', str(text)
        LOG.write('++ {}'.format(text) + '\n')

    def run_module(self):
        (retcode, out, _) = self.execute_module()
        out_stripped = re.sub(r'\"config\": \"! Command:.*\\nend\"',
                              '\"config\": \"--- stripped for space ---\"',
                              out)
        LOG.write("PLaybook stdout:\n\n{}".format(out_stripped))
        msg = "Return code: {}, Expected code: {}".format(retcode, self.testcase.exitcode)
        self.output(msg)
        assert retcode == self.testcase.exitcode, msg
        return self.parse_response(out)

    def execute_module(self):
        arguments = [json.dumps(self.testcase.arguments)]
        arguments.append(json.dumps(
            {'rolename': "ansible-eos-{}".format(ROLE)}))
        return ansible_playbook(EOS_ROLE_PLAYBOOK, arguments=arguments)

    def parse_response(self, output, validate=False):
        # Get all the lines after the 'PLAY RECAP ****...' header
        lines = re.sub(r'^.*PLAY RECAP \*+', '', output, 0, re.S).split('\n')
        # Remove any empty lines from the list
        lines = [x for x in lines if x]

        recap = []
        for line in lines:
            match = re.search(r'^(\S+)\s+\:\s+' \
                              r'ok=(\d+)\s+' \
                              r'changed=(\d+)\s+' \
                              r'unreachable=(\d+)\s+' \
                              r'failed=(\d+)', line)
            if not match:
                self.output("Playbook stdout:\n\n{}".format(output))
                raise ValueError("Unable to parse Ansible output for "
                                 "recap information")
            (name, okcount, changed, unreach, failed) = match.groups()
            recap.append({name: {'ok': okcount,
                                 'changed': changed,
                                 'unreachable': unreach,
                                 'failed': failed}})

        if not validate:
            return recap

        updates = []
        for device in recap:
            hostname = device.keys()[0]
            match = re.search(r'(?<!skipping: )\[%s\] => (\{.*\})' % hostname,
                              output, re.M)
            if not match:
                self.output("Playbook stdout:\n\n{}".format(output))
                raise ValueError("Unable to parse Ansible output for "
                                 "result validation")
            result = json.loads(match.group(1))
            updates.append({hostname: result})

        return updates

    def run_validation(self, src, desc='Validate configuration'):
        args = {'module': 'eos_template', 'description': desc, 'src': src, }
        arguments = [json.dumps(args)]
        (ret_code, out, _) = ansible_playbook(EOS_MODULE_PLAYBOOK,
                                              arguments=arguments,
                                              options=['--check'])
        LOG.write(out)
        assert ret_code == 0, "Validation playbook failed execution"
        return self.parse_response(out, validate=True)

def filter_modules(modules, filenames):
    if modules:
        modules = ['{0}.yml'.format(s) for s in modules.split(',')]
        return list(set(modules).intersection(filenames))
    return filenames

def setup():
    print >> sys.stderr, "Test Suite Setup:"

    run_backup = "  Backing up running-config on nodes ..."
    print >> sys.stderr, run_backup
    LOG.write('++ ' + run_backup.strip())
    args = {
        'module': 'eos_command',
        'description': 'Back up running-config on node',
        'cmds': [
            'configure terminal',
            'copy running-config {}'.format(RUN_CONFIG_BACKUP)
        ],
    }
    arguments = [json.dumps(args)]

    ret_code, out, err = ansible_playbook(EOS_MODULE_PLAYBOOK,
                                          arguments=arguments)

    if ret_code != 0:
        LOG.write(str(">> ansible-playbook {} stdout:\n".format(EOS_MODULE_PLAYBOOK), out))
        LOG.write(str(">> ansible-playbook {} stddrr:\n".format(EOS_MODULE_PLAYBOOK), err))
        teardown()
        raise RuntimeError("Error in Test Suite Setup")

    run_backup = "  Backing up startup-config on nodes ..."
    print >> sys.stderr, run_backup
    LOG.write('++ ' + run_backup.strip())
    args = {
        'module': 'eos_command',
        'description': 'Back up startup-config on node',
        'cmds': [
            'configure terminal',
            'copy startup-config {}'.format(START_CONFIG_BACKUP)
        ],
    }
    arguments = [json.dumps(args)]

    ret_code, out, err = ansible_playbook(EOS_MODULE_PLAYBOOK,
                                          arguments=arguments)

    if ret_code != 0:
        LOG.write(str(">> ansible-playbook {} stdout:\n".format(EOS_MODULE_PLAYBOOK), out))
        LOG.write(str(">> ansible-playbook {} stddrr:\n".format(EOS_MODULE_PLAYBOOK), err))
        teardown()
        raise RuntimeError("Error in Test Suite Setup")

    print >> sys.stderr, "  Gathering test cases ..."

    modules = os.environ.get('ANSIBLE_ROLE_TEST_CASES')

    testcases_home = os.path.join(HERE, 'testcases')
    if not os.path.exists(testcases_home):
        print >> sys.stderr, "\n  ***** Testcase directory not found!! *****"
        teardown()
        raise RuntimeError("Testcase path '{}' does not exist".format(testcases_home))

    filenames = os.listdir(testcases_home)

    for module in filter_modules(modules, filenames):
        path = os.path.join(testcases_home, module)
        definition = yaml.load(open(path))

        defaults = definition.get('defaults', {})
        testcases = definition.get('testcases', [])
        if not testcases:
            print >> sys.stderr, ("\n  ***** No testcases defined in "
                                  "module {} *****\n".format(module))
        else:
            for testcase in definition.get('testcases', []):
                kwargs = defaults.copy()
                kwargs.update(testcase)
                TESTCASES.append(TestCase(**kwargs))

    print >> sys.stderr, "  Setup complete\n"

def teardown():
    print >> sys.stderr, "\nTest Suite Teardown:"

    no_teardown = os.environ.get('NO_ANSIBLE_ROLE_TEST_TEARDOWN')

    if no_teardown:
        print >> sys.stderr, ("{}\n"
                              "  Skipping test suite teardown due to "
                              "NO_ANSIBLE_ROLE_TEST_TEARDOWN\n"
                              "  To restore each device to pre-test state "
                              "execute the following commands\n"
                              "  - configure terminal\n"
                              "  - configure replace {}\n"
                              "  - delete {}\n"
                              "  - copy {} startup-config\n"
                              "  - delete {}\n"
                              "{}".format(SEPARATOR, RUN_CONFIG_BACKUP,
                                          RUN_CONFIG_BACKUP,
                                          START_CONFIG_BACKUP,
                                          START_CONFIG_BACKUP, SEPARATOR))
    else:
        # Restore the running-config on the nodes
        # ---------------------------------------
        restore_backup = "  Restoring running-config on nodes ..."
        print >> sys.stderr, restore_backup
        LOG.write('++ ' + restore_backup.strip())
        args = {
            'module': 'eos_command',
            'description': 'Restore running-config from backup',
            'cmds': [
                'configure terminal',
                'configure replace {}'.format(RUN_CONFIG_BACKUP),
                'delete {}'.format(RUN_CONFIG_BACKUP),
            ],
        }
        arguments = [json.dumps(args)]

        # ret_code, out, err = ansible_playbook(CMD_PLAY, arguments=arguments)
        ret_code, out, err = ansible_playbook(EOS_MODULE_PLAYBOOK,
                                              arguments=arguments)

        if ret_code != 0:
            msg = "Error restoring running-config on nodes\n" \
                  "Running ansible-playbook {} -e {}\n" \
                  ">> stdout: {}\n" \
                  ">> stderr: {}\n".format(EOS_MODULE_PLAYBOOK, arguments, out, err)
            warnings.warn(msg)

        # Restore the startup-config on the nodes
        # ---------------------------------------
        restore_backup = "  Restoring startup-config on nodes ..."
        print >> sys.stderr, restore_backup
        LOG.write('++ ' + restore_backup.strip())
        args = {
            'module': 'eos_command',
            'description': 'Restore startup-config from backup',
            'cmds': [
                'configure terminal',
                'copy {} startup-config'.format(START_CONFIG_BACKUP),
                'delete {}'.format(START_CONFIG_BACKUP),
            ],
        }
        arguments = [json.dumps(args)]

        # ret_code, out, err = ansible_playbook(CMD_PLAY, arguments=arguments)
        ret_code, out, err = ansible_playbook(EOS_MODULE_PLAYBOOK,
                                              arguments=arguments)

        if ret_code != 0:
            msg = "Error restoring startup-config on nodes\n" \
                  "Running ansible-playbook {} -e {}\n" \
                  ">> stdout: {}\n" \
                  ">> stderr: {}\n".format(EOS_MODULE_PLAYBOOK, arguments, out, err)
            warnings.warn(msg)

    print >> sys.stderr, "  Teardown complete"

def test_module():
    for testcase in TESTCASES:
        yield TestModule(testcase)

def ansible_playbook(playbook, arguments=None, options=None):
    if arguments is None:
        arguments = []
    if options is None:
        options = []

    command = ['ansible-playbook']

    command.append(playbook)
    command.extend(['-i', INVENTORY])
    for arg in arguments:
        command.extend(['-e', arg])
    for opt in options:
        command.append(opt)
    command.append('-vvv')

    # Format the command string for output on error - for easier
    # copy/paste for manual run
    cmdstr = ''
    for segment in command:
        if segment[0] == '{':
            cmdstr = cmdstr + "\'{}\' ".format(segment)
        else:
            cmdstr = cmdstr + "{} ".format(segment)
    LOG.write("-- Ansible playbook command:\n-- {}\n".format(cmdstr))

    stdout = subprocess.PIPE
    stderr = subprocess.PIPE
    proc = subprocess.Popen(command, stdout=stdout, stderr=stderr)
    out, err = proc.communicate()

    return (proc.returncode, out, err)
<commit_msg>Squashed 'test/arista-ansible-role-test/' changes from 2c77a57..80011bd<commit_after># pylint: disable=invalid-name
# pylint: disable=missing-docstring
# pylint: disable=too-few-public-methods
# pylint: disable=too-many-instance-attributes
# pylint: disable=too-many-locals

import json
import os
import re
import subprocess
import sys
import warnings
import yaml

TESTCASES = list()
INVENTORY = 'test/fixtures/hosts'

HERE = os.path.abspath(os.path.dirname(__file__))
ROLE = re.match(
    r'^.*\/ansible-eos-([^/\s]+)\/test/arista-ansible-role-test$', HERE).group(1)
RUN_CONFIG_BACKUP = '_eos_role_test_{}_running'.format(ROLE)
START_CONFIG_BACKUP = '_eos_role_test_{}_startup'.format(ROLE)

EOS_ROLE_PLAYBOOK = 'test/arista-ansible-role-test/eos_role.yml'
EOS_MODULE_PLAYBOOK = 'test/arista-ansible-role-test/eos_module.yml'

LOG_FILE = '{}/roletest.log'.format(HERE)
try:
    os.remove(LOG_FILE)
except OSError:
    pass
LOG = open(LOG_FILE, 'w')

SEPARATOR = '    ' + '*' * 50

class TestCase(object):
    def __init__(self, **kwargs):
        self.name = kwargs['name']
        self.module = kwargs['module']

        self.host = None

        self.inventory = kwargs.get('inventory')
        self.exitcode = kwargs.get('exitcode', 0)
        self.idempotent = kwargs.get('idempotent', True)
        self.changed = kwargs.get('changed', True)
        self.present = kwargs.get('present')
        self.absent = kwargs.get('absent')

        self.arguments = kwargs.get('arguments', list())
        self.variables = dict()

        # optional properties
        self.setup = kwargs.get('setup', list())
        self.teardown = kwargs.get('teardown', list())

    def __str__(self):
        return self.name

class TestModule(object):
    def __init__(self, testcase):
        self.testcase = testcase
        self.description = 'Test [%s]: %s' % (testcase.module, testcase.name)

    def __call__(self):
        self.output('Run first pass')
        response = self.run_module()
        for device in response:
            hostname = device.keys()[0]
            reported = int(device[hostname]['changed'])
            expected = int(self.testcase.changed)
            msg = ("First pass role execution reported {} task change(s), "
                   "expected {}".format(reported, expected))
            self.output(msg)
            assert reported == expected, msg

        if self.testcase.idempotent:
            self.output('Run second pass')
            response = self.run_module()
            for device in response:
                hostname = device.keys()[0]
                reported = int(device[hostname]['changed'])
                msg = (
                    "Second pass role execution reported {} task change(s), "
                    "expected 0".format(reported))
                self.output(msg)
                assert not reported, msg

        if self.testcase.present:
            desc = 'Validate present configuration'
            self.output(desc)
            response = self.run_validation(self.testcase.present, desc=desc)
            for device in response:
                hostname = device.keys()[0]
                # Result should contain an empty list of updates
                delim = " ---\n"
                updates = device[hostname]['updates']
                msg = ("{} - Expected configuration\n{}{}\n{}not found "
                       "on device '{}'".format(desc, delim,
                                               '\n'.join(updates), delim,
                                               hostname))
                assert device[hostname]['updates'] == [], msg
                # Result should show no changes
                msg = ("{} - Device '{}' reported no updates, but "
                       "returned 'changed'".format(desc, hostname))
                assert device[hostname]['changed'] == False, msg

        if self.testcase.absent:
            desc = 'Validate absent configuration'
            self.output(desc)
            response = self.run_validation(self.testcase.absent, desc=desc)
            for device in response:
                hostname = device.keys()[0]
                # Result should show change has taken place
                msg = (
                    "{} - Entire absent configuration found on device '{}'".
                    format(desc, hostname)
                )
                assert device[hostname]['changed'] == True, msg
                # Compare changes with expected values, sorted at global level
                updates = '\n'.join(device[hostname]['updates'])
                updates = re.split(r'\n(?=\S)', updates)
                updates = '\n'.join(sorted(updates))
                absent = re.split(r'\n(?=\S)', self.testcase.absent.rstrip())
                absent = '\n'.join(sorted(absent))
                msg = ("{} - Some part of absent configuration found "
                       "on device '{}'".format(desc, hostname))
                assert updates == absent, msg

    def setUp(self):
        print("\n{}\n".format(SEPARATOR) +
              "  See run log for complete output:\n  {}".format(LOG_FILE) +
              "\n{}\n".format(SEPARATOR))

        LOG.write("\n\n\n{}\n".format(SEPARATOR) +
                  "  Begin log for {}".format(self.description) +
                  "\n{}\n\n".format(SEPARATOR))

        if self.testcase.setup:
            self.output('Running test case setup commands')
            setup_cmds = self.testcase.setup
            if not isinstance(setup_cmds, list):
                setup_cmds = setup_cmds.splitlines()
            self.output("{}".format(setup_cmds))

            args = {
                'module': 'eos_command',
                'description': 'Run test case setup commands',
                'cmds': ['configure terminal'] + setup_cmds,
            }

            arguments = [json.dumps(args)]

            ret_code, out, err = ansible_playbook(EOS_MODULE_PLAYBOOK,
                                                  arguments=arguments)

            if ret_code != 0:
                LOG.write("Playbook stdout:\n\n{}".format(out))
                LOG.write("Playbook stderr:\n\n{}".format(err))
                raise

    def tearDown(self):
        if self.testcase.teardown:
            self.output('Running test case teardown commands')
            teardown_cmds = self.testcase.teardown
            if not isinstance(teardown_cmds, list):
                teardown_cmds = teardown_cmds.splitlines()
            self.output("{}\n".format(teardown_cmds))

            args = {
                'module': 'eos_command',
                'description': 'Run test case teardown_cmds commands',
                'cmds': ['configure terminal'] + teardown_cmds,
            }

            arguments = [json.dumps(args)]

            ret_code, out, err = ansible_playbook(EOS_MODULE_PLAYBOOK,
                                                  arguments=arguments)

            if ret_code != 0:
                self.output("Playbook stdout:\n\n{}".format(out))
                self.output("Playbook stderr:\n\n{}".format(err))
                warnings.warn("\nError in test case teardown\n\n{}".format(
                    out))

    @classmethod
    def output(cls, text):
        print '>>', str(text)
        LOG.write('++ {}'.format(text) + '\n')

    def run_module(self):
        (retcode, out, _) = self.execute_module()
        out_stripped = re.sub(r'\"config\": \"! Command:.*\\nend\"',
                              '\"config\": \"--- stripped for space ---\"',
                              out)
        LOG.write("PLaybook stdout:\n\n{}".format(out_stripped))
        msg = "Return code: {}, Expected code: {}".format(retcode, self.testcase.exitcode)
        self.output(msg)
        assert retcode == self.testcase.exitcode, msg
        return self.parse_response(out)

    def execute_module(self):
        arguments = [json.dumps(self.testcase.arguments)]
        arguments.append(json.dumps(
            {'rolename': "ansible-eos-{}".format(ROLE)}))
        return ansible_playbook(EOS_ROLE_PLAYBOOK, arguments=arguments)

    def parse_response(self, output, validate=False):
        # Get all the lines after the 'PLAY RECAP ****...' header
        lines = re.sub(r'^.*PLAY RECAP \*+', '', output, 0, re.S).split('\n')
        # Remove any empty lines from the list
        lines = [x for x in lines if x]

        recap = []
        for line in lines:
            match = re.search(r'^(\S+)\s+\:\s+' \
                              r'ok=(\d+)\s+' \
                              r'changed=(\d+)\s+' \
                              r'unreachable=(\d+)\s+' \
                              r'failed=(\d+)', line)
            if not match:
                self.output("Playbook stdout:\n\n{}".format(output))
                raise ValueError("Unable to parse Ansible output for "
                                 "recap information")
            (name, okcount, changed, unreach, failed) = match.groups()
            recap.append({name: {'ok': okcount,
                                 'changed': changed,
                                 'unreachable': unreach,
                                 'failed': failed}})

        if not validate:
            return recap

        updates = []
        for device in recap:
            hostname = device.keys()[0]
            match = re.search(r'(?<!skipping: )\[%s\] => (\{.*\})' % hostname,
                              output, re.M)
            if not match:
                self.output("Playbook stdout:\n\n{}".format(output))
                raise ValueError("Unable to parse Ansible output for "
                                 "result validation")
            result = json.loads(match.group(1))
            updates.append({hostname: result})

        return updates

    def run_validation(self, src, desc='Validate configuration'):
        args = {'module': 'eos_template', 'description': desc, 'src': src, }
        arguments = [json.dumps(args)]
        (ret_code, out, _) = ansible_playbook(EOS_MODULE_PLAYBOOK,
                                              arguments=arguments,
                                              options=['--check'])
        LOG.write(out)
        assert ret_code == 0, "Validation playbook failed execution"
        return self.parse_response(out, validate=True)

def filter_modules(modules, filenames):
    if modules:
        modules = ['{0}.yml'.format(s) for s in modules.split(',')]
        return list(set(modules).intersection(filenames))
    return filenames

def setup():
    print >> sys.stderr, "Test Suite Setup:"

    run_backup = "  Backing up running-config on nodes ..."
    print >> sys.stderr, run_backup
    LOG.write('++ ' + run_backup.strip())
    args = {
        'module': 'eos_command',
        'description': 'Back up running-config on node',
        'cmds': [
            'configure terminal',
            'copy running-config {}'.format(RUN_CONFIG_BACKUP)
        ],
    }
    arguments = [json.dumps(args)]

    ret_code, out, err = ansible_playbook(EOS_MODULE_PLAYBOOK,
                                          arguments=arguments)

    if ret_code != 0:
        LOG.write(">> ansible-playbook {} stdout:\n{}".format(EOS_MODULE_PLAYBOOK, out))
        LOG.write(">> ansible-playbook {} stddrr:\n{}".format(EOS_MODULE_PLAYBOOK, err))
        teardown()
        raise RuntimeError("Error in Test Suite Setup")

    run_backup = "  Backing up startup-config on nodes ..."
    print >> sys.stderr, run_backup
    LOG.write('++ ' + run_backup.strip())
    args = {
        'module': 'eos_command',
        'description': 'Back up startup-config on node',
        'cmds': [
            'configure terminal',
            'copy startup-config {}'.format(START_CONFIG_BACKUP)
        ],
    }
    arguments = [json.dumps(args)]

    ret_code, out, err = ansible_playbook(EOS_MODULE_PLAYBOOK,
                                          arguments=arguments)

    if ret_code != 0:
        LOG.write(">> ansible-playbook {} stdout:\n{}".format(EOS_MODULE_PLAYBOOK, out))
        LOG.write(">> ansible-playbook {} stddrr:\n{}".format(EOS_MODULE_PLAYBOOK, err))
        teardown()
        raise RuntimeError("Error in Test Suite Setup")

    print >> sys.stderr, "  Gathering test cases ..."

    modules = os.environ.get('ANSIBLE_ROLE_TEST_CASES')

    testcases_home = os.path.join(HERE, 'testcases')
    if not os.path.exists(testcases_home):
        print >> sys.stderr, "\n  ***** Testcase directory not found!! *****"
        teardown()
        raise RuntimeError("Testcase path '{}' does not exist".format(testcases_home))

    filenames = os.listdir(testcases_home)

    for module in filter_modules(modules, filenames):
        path = os.path.join(testcases_home, module)
        definition = yaml.load(open(path))

        defaults = definition.get('defaults', {})
        testcases = definition.get('testcases', [])
        if not testcases:
            print >> sys.stderr, ("\n  ***** No testcases defined in "
                                  "module {} *****\n".format(module))
        else:
            for testcase in definition.get('testcases', []):
                kwargs = defaults.copy()
                kwargs.update(testcase)
                TESTCASES.append(TestCase(**kwargs))

    print >> sys.stderr, "  Setup complete\n"

def teardown():
    print >> sys.stderr, "\nTest Suite Teardown:"

    no_teardown = os.environ.get('NO_ANSIBLE_ROLE_TEST_TEARDOWN')

    if no_teardown:
        print >> sys.stderr, ("{}\n"
                              "  Skipping test suite teardown due to "
                              "NO_ANSIBLE_ROLE_TEST_TEARDOWN\n"
                              "  To restore each device to pre-test state "
                              "execute the following commands\n"
                              "  - configure terminal\n"
                              "  - configure replace {}\n"
                              "  - delete {}\n"
                              "  - copy {} startup-config\n"
                              "  - delete {}\n"
                              "{}".format(SEPARATOR, RUN_CONFIG_BACKUP,
                                          RUN_CONFIG_BACKUP,
                                          START_CONFIG_BACKUP,
                                          START_CONFIG_BACKUP, SEPARATOR))
    else:
        # Restore the running-config on the nodes
        # ---------------------------------------
        restore_backup = "  Restoring running-config on nodes ..."
        print >> sys.stderr, restore_backup
        LOG.write('++ ' + restore_backup.strip())
        args = {
            'module': 'eos_command',
            'description': 'Restore running-config from backup',
            'cmds': [
                'configure terminal',
                'configure replace {}'.format(RUN_CONFIG_BACKUP),
                'delete {}'.format(RUN_CONFIG_BACKUP),
            ],
        }
        arguments = [json.dumps(args)]

        # ret_code, out, err = ansible_playbook(CMD_PLAY, arguments=arguments)
        ret_code, out, err = ansible_playbook(EOS_MODULE_PLAYBOOK,
                                              arguments=arguments)

        if ret_code != 0:
            msg = "Error restoring running-config on nodes\n" \
                  "Running ansible-playbook {} -e {}\n" \
                  ">> stdout: {}\n" \
                  ">> stderr: {}\n".format(EOS_MODULE_PLAYBOOK, arguments, out, err)
            warnings.warn(msg)

        # Restore the startup-config on the nodes
        # ---------------------------------------
        restore_backup = "  Restoring startup-config on nodes ..."
        print >> sys.stderr, restore_backup
        LOG.write('++ ' + restore_backup.strip())
        args = {
            'module': 'eos_command',
            'description': 'Restore startup-config from backup',
            'cmds': [
                'configure terminal',
                'copy {} startup-config'.format(START_CONFIG_BACKUP),
                'delete {}'.format(START_CONFIG_BACKUP),
            ],
        }
        arguments = [json.dumps(args)]

        # ret_code, out, err = ansible_playbook(CMD_PLAY, arguments=arguments)
        ret_code, out, err = ansible_playbook(EOS_MODULE_PLAYBOOK,
                                              arguments=arguments)

        if ret_code != 0:
            msg = "Error restoring startup-config on nodes\n" \
                  "Running ansible-playbook {} -e {}\n" \
                  ">> stdout: {}\n" \
                  ">> stderr: {}\n".format(EOS_MODULE_PLAYBOOK, arguments, out, err)
            warnings.warn(msg)

    print >> sys.stderr, "  Teardown complete"

def test_module():
    for testcase in TESTCASES:
        yield TestModule(testcase)

def ansible_playbook(playbook, arguments=None, options=None):
    if arguments is None:
        arguments = []
    if options is None:
        options = []

    command = ['ansible-playbook']

    command.append(playbook)
    command.extend(['-i', INVENTORY])
    for arg in arguments:
        command.extend(['-e', arg])
    for opt in options:
        command.append(opt)
    command.append('-vvv')

    # Format the command string for output on error - for easier
    # copy/paste for manual run
    cmdstr = ''
    for segment in command:
        if segment[0] == '{':
            cmdstr = cmdstr + "\'{}\' ".format(segment)
        else:
            cmdstr = cmdstr + "{} ".format(segment)
    LOG.write("-- Ansible playbook command:\n-- {}\n".format(cmdstr))

    stdout = subprocess.PIPE
    stderr = subprocess.PIPE
    proc = subprocess.Popen(command, stdout=stdout, stderr=stderr)
    out, err = proc.communicate()

    return (proc.returncode, out, err)
<|endoftext|>


<commit_before>from dailyDBV2 import dailyDBV2
import time
import datetime
import cx_Oracle
import os
from dateutil import parser
import simplejson as json

QUERY_HOUR = 1
QUERY_LIMIT = 650

def multi_result():
    rec1 = []
    rec2 = []
    for i in range(1,10):
        rec1.append((i,i))
    for j in range(11,20):
        rec2.append((j,j))
    return (rec1,rec2)

def get_document_wget():
    url = get_URL()
    cmd = 'wget -q -r -l 2 -w 10 -O /tmp/PBMon_log.txt %s'%url
    os.system(cmd)
    time.sleep(5)
    contents = open('/tmp/PBMon_log.txt').read()
    return contents

def get_URL():
    return 'http://panda.cern.ch/server/pandamon/query?mode=mon&name=panda.mon.prod&type=analy_brokerage&hours=%d&limit=%d'%(QUERY_HOUR,QUERY_LIMIT)

def get_sitecloud_name(siteID):
    cloud = siteID
    site_name = siteID
    for site in dic:
        if site['panda_siteID']==siteID:
            cloud = site['cloud']
            site_name = site['agis_ssb_site_name']
            break
    return (site_name,cloud)

#contents = get_document_wget()
#print "Contents:",contents

fjson = open('panda_queues.json','r')
data = fjson.read()
dic = json.loads(data)
fjson.close()

fjson = open('unprocess.json','r')
data = fjson.read()
records = json.loads(data)
fjson.close()
print "Records"
for r in records:
    print r,":",records[r]

# print dic[10]['cloud'],dic[10]['panda_siteID']

con = cx_Oracle.connect('PandaBrokerageMonitor_ookey/PandaBrokerageMonitor2@devdb11')
print con.version
print "====="
cursor = con.cursor()
cursor1 = con.cursor()
cursor1.execute("select * from dailyLogV2")
"""
for result in cursor1:
    # print result
    dailyLogId = result[0]
    site_name, cloud = get_sitecloud_name(result[3])
    if cloud is not None:
        sql = "UPDATE dailyLog SET site='%s',cloud='%s' WHERE dailyLogId=%d"%(site_name,cloud,dailyLogId)
        cursor.execute(sql)
        con.commit()
"""
DATEFORMAT = "%Y-%m-%d %H:%M:%S"
t1 = time.time()
t2 = t1 - 7*24*60*60
tf = time.strftime(DATEFORMAT,time.localtime(t2))
print tf
print "====="
print "Year:",datetime.date.today().year
dt = parser.parse("2011-10-02 14:00")
dt1 = parser.parse("2011-10-02")
dt2 = parser.parse(tf)
print "DT:",dt
print "DT1:",dt1
print "DT2:",dt2
print "t1:",tf
if dt > dt2:
    print "BIG"

"""
cursor.execute("select * from dailyLog where logDate>'2011-10-06' order by dailyLogId DESC")
rows = cursor.fetchall()
print cursor.rowcount
for result in rows:
    print result
cursor.execute("select MAX(dailyLogId) as MaxId from dailyLog")
maxId = cursor.fetchone()[0]
print "MAXID=",maxId
cursor.close()
con.close()
"""
db = dailyDBV2()
maxId = db.get_max_id()
if maxId is None:
    maxId = 0

""" Test Adding
logs = []
for i in range(1,20):
    maxId = maxId+1
    logDate = str('2011-10-%02d'%i)
    logs.append((maxId,logDate,'A','TW','TW','Ookey',1))
db.add_logs(logs)
"""
"""
#logIds = [[1],[3],[5],[7],[9]]
logIds = []
for i in range(1,20,2):
    logDate = str('2011-10-%02d'%i)
    logId = db.is_exist_item(logDate,'A','TW','Ookey')
    if logId is not None:
        logIds.append([logId])
db.increase_logs_count(logIds)

print "MAXID from class=",db.get_max_id()

rec1,rec2 = multi_result()
print(rec1)[3][1]
print(rec2)[5][1]
"""
# os.system("wget http://adc-ssb.cern.ch/SITE_EXCLUSION/panda_queues.json")
cursor.close()
cursor1.close()
con.close()

<commit_msg><commit_after>from dailyDBV2 import dailyDBV2
import time
import datetime
import cx_Oracle
import os
from dateutil import parser
import simplejson as json

QUERY_HOUR = 1
QUERY_LIMIT = 650

def multi_result():
    rec1 = []
    rec2 = []
    for i in range(1,10):
        rec1.append((i,i))
    for j in range(11,20):
        rec2.append((j,j))
    return (rec1,rec2)

def get_document_wget():
    url = get_URL()
    cmd = 'wget -q -r -l 2 -w 10 -O /tmp/PBMon_log.txt %s'%url
    os.system(cmd)
    time.sleep(5)
    contents = open('/tmp/PBMon_log.txt').read()
    return contents

def get_URL():
    return 'http://panda.cern.ch/server/pandamon/query?mode=mon&name=panda.mon.prod&type=analy_brokerage&hours=%d&limit=%d'%(QUERY_HOUR,QUERY_LIMIT)

def get_sitecloud_name(siteID):
    cloud = siteID
    site_name = siteID
    for site in dic:
        if site['panda_siteID']==siteID:
            cloud = site['cloud']
            site_name = site['agis_ssb_site_name']
            break
    return (site_name,cloud)

#contents = get_document_wget()
#print "Contents:",contents

fjson = open('panda_queues.json','r')
data = fjson.read()
dic = json.loads(data)
fjson.close()

fjson = open('allunprocess.json','r')
data = fjson.read()
records = json.loads(data)
fjson.close()
print "Records"
for r in records:
    print r,":",records[r]

# print dic[10]['cloud'],dic[10]['panda_siteID']

con = cx_Oracle.connect('PandaBrokerageMonitor_ookey/PandaBrokerageMonitor2@devdb11')
print con.version
print "====="
cursor = con.cursor()
cursor1 = con.cursor()
cursor1.execute("select * from dailyLogV2")
"""
for result in cursor1:
    # print result
    dailyLogId = result[0]
    site_name, cloud = get_sitecloud_name(result[3])
    if cloud is not None:
        sql = "UPDATE dailyLog SET site='%s',cloud='%s' WHERE dailyLogId=%d"%(site_name,cloud,dailyLogId)
        cursor.execute(sql)
        con.commit()
"""
DATEFORMAT = "%Y-%m-%d %H:%M:%S"
t1 = time.time()
t2 = t1 - 7*24*60*60
tf = time.strftime(DATEFORMAT,time.localtime(t2))
print tf
print "====="
print "Year:",datetime.date.today().year
dt = parser.parse("2011-10-02 14:00")
dt1 = parser.parse("2011-10-02")
dt2 = parser.parse(tf)
print "DT:",dt
print "DT1:",dt1
print "DT2:",dt2
print "t1:",tf
if dt > dt2:
    print "BIG"

"""
cursor.execute("select * from dailyLog where logDate>'2011-10-06' order by dailyLogId DESC")
rows = cursor.fetchall()
print cursor.rowcount
for result in rows:
    print result
cursor.execute("select MAX(dailyLogId) as MaxId from dailyLog")
maxId = cursor.fetchone()[0]
print "MAXID=",maxId
cursor.close()
con.close()
"""
db = dailyDBV2()
maxId = db.get_max_id()
if maxId is None:
    maxId = 0

""" Test Adding
logs = []
for i in range(1,20):
    maxId = maxId+1
    logDate = str('2011-10-%02d'%i)
    logs.append((maxId,logDate,'A','TW','TW','Ookey',1))
db.add_logs(logs)
"""
"""
#logIds = [[1],[3],[5],[7],[9]]
logIds = []
for i in range(1,20,2):
    logDate = str('2011-10-%02d'%i)
    logId = db.is_exist_item(logDate,'A','TW','Ookey')
    if logId is not None:
        logIds.append([logId])
db.increase_logs_count(logIds)

print "MAXID from class=",db.get_max_id()

rec1,rec2 = multi_result()
print(rec1)[3][1]
print(rec2)[5][1]
"""
# os.system("wget http://adc-ssb.cern.ch/SITE_EXCLUSION/panda_queues.json")
cursor.close()
cursor1.close()
con.close()

<|endoftext|>


<commit_before>import json, os
from marketorestpython.client import MarketoClient

try:
    # Travis testing
    MUNCHKIN_ID = os.environ['MUNCHKIN_ID']
    CLIENT_ID = os.environ['CLIENT_ID']
    CLIENT_SECRET = os.environ['CLIENT_SECRET']
except KeyError:
    # local testing
    with open('conf.json', 'r', encoding='utf-8') as f:
        creds = json.loads(f.read())
    MUNCHKIN_ID = creds['munchkin_id']
    CLIENT_ID = creds['client_id']
    CLIENT_SECRET = creds['client_secret']

mc = MarketoClient(munchkin_id=MUNCHKIN_ID, client_id=CLIENT_ID, client_secret=CLIENT_SECRET)

segmentation_id = 1001

lead_id_1 = None
lead_id_2 = None
file_id = None
list_folder_id = None
list_id = None
files_folder_id = None
bulk_lead_export_id = None

def test_create_update_leads():
    leads = [{"email": "joe@example.com", "firstName": "Joey"}, {"email": "jill@example.com", "firstName": "Jillian"}]
    response = mc.execute(method='create_update_leads', leads=leads)
    global lead_id_1
    lead_id_1 = response[0]['id']
    global lead_id_2
    lead_id_2 = response[1]['id']
    assert response[0]['status'] in ['created','updated'] and response[1]['status'] in ['created','updated']

def test_get_folder_by_name():
    global list_folder_id
    list_folder = mc.execute(method='get_folder_by_name', name='Group Lists')
    print(list_folder)
    list_folder_id = list_folder[0]['id']
    print(list_folder_id)

def test_create_list():
    global list_folder_id
    global list_id
    list = mc.execute(method='create_list', name='unit test list', folderId=list_folder_id, folderType='Folder')
    print(list)
    list_id = list[0]['id']
    assert list

def test_update_list():
    global list_id
    list = mc.execute(method='update_list', id=list_id, name='unit test list (renamed)', description='added description')
    print(list)
    assert list

def test_add_leads_to_list():
    global lead_id_1
    global lead_id_2
    global list_id
    add_to_list = mc.execute(method='add_leads_to_list', listId=list_id, id=[lead_id_1, lead_id_2])
    assert add_to_list[0]['status'] == 'added' and add_to_list[1]['status'] == 'added'

def test_create_lead_export_job():
    global list_id
    global bulk_lead_export_id
    job = mc.execute(method='create_leads_export_job', fields=['firstName', 'lastName', 'email'],
                     filters={'staticListId': list_id})
    bulk_lead_export_id = job[0]['exportId']
    assert job[0]['status'] == 'Created'

def test_enqueue_lead_export_job():
    global bulk_lead_export_id
    enqueued_job_details = mc.execute(method='enqueue_leads_export_job',
                                      job_id=bulk_lead_export_id)
    assert enqueued_job_details[0]['status'] == 'Queued'

def test_cancel_lead_export_job():
    global bulk_lead_export_id
    cancelled_job = mc.execute(method='cancel_leads_export_job',
                               job_id=bulk_lead_export_id)
    assert cancelled_job[0]['status'] == 'Cancelled'

def test_segments():
    segments = mc.execute(method='get_segments', id=segmentation_id)
    assert segments

def test_get_files_folder_by_name():
    global files_folder_id
    files_folder = mc.execute(method='get_folder_by_name', name='Images and Files')
    print(files_folder )
    files_folder_id = files_folder [0]['id']
    print(list_folder_id)

def test_create_file():
    global file_id
    file = mc.execute(method='create_file', name='python-logo-master-v3-TM.png', file='python-logo-master-v3-TM.png',
                      folder=files_folder_id, description='test file', insertOnly=False)
    print(file)
    file_id = file[0]['id']
    assert file

def test_update_file_content():
    global file_id
    file = mc.execute(method='update_file_content', id=file_id, file='python-logo-master-v3-TM.png')
    assert file

def get_file_by_id():
    global file_id
    file = mc.execute(method='get_file_by_id', id=file_id)
    assert file

def list_files():
    files = mc.execute(method='list_files', folder=files_folder_id, offset=0, maxReturn=10)
    assert files

def test_error_handling():
    try:
        token = mc.execute(method='get_paging_token', sinceDatetime='2015-04-0113')
        e_dict = None
        print(token)
    except Exception as e:
        print('error: {}'.format(e))
        e_dict = eval(str(e))
        if 'code' in e_dict:
            print('code: {}'.format(e_dict['code']))
        else:
            print('e_dict: {}'.format(e_dict))
    assert e_dict['code'] == '1001'

def test_delete_list():
    global list_id
    list = mc.execute(method='delete_list', id = list_id)
    print(list)
    assert list

def test_delete_leads():
    global lead_id_1
    global lead_id_2
    response = mc.execute(method='delete_lead', id=[lead_id_1, lead_id_2])
    assert response[0]['status'] == 'deleted' and response[1]['status'] == 'deleted'

<commit_msg>fix duplicate list name issue<commit_after>import json, os, uuid
from marketorestpython.client import MarketoClient

try:
    # Travis testing
    MUNCHKIN_ID = os.environ['MUNCHKIN_ID']
    CLIENT_ID = os.environ['CLIENT_ID']
    CLIENT_SECRET = os.environ['CLIENT_SECRET']
except KeyError:
    # local testing
    with open('conf.json', 'r', encoding='utf-8') as f:
        creds = json.loads(f.read())
    MUNCHKIN_ID = creds['munchkin_id']
    CLIENT_ID = creds['client_id']
    CLIENT_SECRET = creds['client_secret']

mc = MarketoClient(munchkin_id=MUNCHKIN_ID, client_id=CLIENT_ID, client_secret=CLIENT_SECRET)

segmentation_id = 1001

lead_id_1 = None
lead_id_2 = None
file_id = None
list_folder_id = None
list_id = None
files_folder_id = None
bulk_lead_export_id = None
list_name = uuid.uuid4()

def test_create_update_leads():
    leads = [{"email": "joe@example.com", "firstName": "Joey"}, {"email": "jill@example.com", "firstName": "Jillian"}]
    response = mc.execute(method='create_update_leads', leads=leads)
    global lead_id_1
    lead_id_1 = response[0]['id']
    global lead_id_2
    lead_id_2 = response[1]['id']
    assert response[0]['status'] in ['created','updated'] and response[1]['status'] in ['created','updated']

def test_get_folder_by_name():
    global list_folder_id
    list_folder = mc.execute(method='get_folder_by_name', name='Group Lists')
    print(list_folder)
    list_folder_id = list_folder[0]['id']
    print(list_folder_id)

def test_create_list():
    global list_folder_id
    global list_id
    global list_name
    static_list = mc.execute(method='create_list', name=list_name, folderId=list_folder_id, folderType='Folder')
    print(static_list)
    list_id = static_list[0]['id']
    assert static_list

def test_update_list():
    global list_id
    global list_name
    static_list = mc.execute(method='update_list', id=list_id, name='{} (renamed)'.format(list_name),
                             description='added description')
    print(static_list)
    assert static_list

def test_add_leads_to_list():
    global lead_id_1
    global lead_id_2
    global list_id
    add_to_list = mc.execute(method='add_leads_to_list', listId=list_id, id=[lead_id_1, lead_id_2])
    assert add_to_list[0]['status'] == 'added' and add_to_list[1]['status'] == 'added'

def test_create_lead_export_job():
    global list_id
    global bulk_lead_export_id
    job = mc.execute(method='create_leads_export_job', fields=['firstName', 'lastName', 'email'],
                     filters={'staticListId': list_id})
    bulk_lead_export_id = job[0]['exportId']
    assert job[0]['status'] == 'Created'

def test_enqueue_lead_export_job():
    global bulk_lead_export_id
    enqueued_job_details = mc.execute(method='enqueue_leads_export_job',
                                      job_id=bulk_lead_export_id)
    assert enqueued_job_details[0]['status'] == 'Queued'

def test_cancel_lead_export_job():
    global bulk_lead_export_id
    cancelled_job = mc.execute(method='cancel_leads_export_job',
                               job_id=bulk_lead_export_id)
    assert cancelled_job[0]['status'] == 'Cancelled'

def test_segments():
    segments = mc.execute(method='get_segments', id=segmentation_id)
    assert segments

def test_get_files_folder_by_name():
    global files_folder_id
    files_folder = mc.execute(method='get_folder_by_name', name='Images and Files')
    print(files_folder )
    files_folder_id = files_folder [0]['id']
    print(list_folder_id)

def test_create_file():
    global file_id
    file = mc.execute(method='create_file', name='python-logo-master-v3-TM.png', file='python-logo-master-v3-TM.png',
                      folder=files_folder_id, description='test file', insertOnly=False)
    print(file)
    file_id = file[0]['id']
    assert file

def test_update_file_content():
    global file_id
    file = mc.execute(method='update_file_content', id=file_id, file='python-logo-master-v3-TM.png')
    assert file

def get_file_by_id():
    global file_id
    file = mc.execute(method='get_file_by_id', id=file_id)
    assert file

def list_files():
    files = mc.execute(method='list_files', folder=files_folder_id, offset=0, maxReturn=10)
    assert files

def test_error_handling():
    try:
        token = mc.execute(method='get_paging_token', sinceDatetime='2015-04-0113')
        e_dict = None
        print(token)
    except Exception as e:
        print('error: {}'.format(e))
        e_dict = eval(str(e))
        if 'code' in e_dict:
            print('code: {}'.format(e_dict['code']))
        else:
            print('e_dict: {}'.format(e_dict))
    assert e_dict['code'] == '1001'

def test_delete_list():
    global list_id
    list = mc.execute(method='delete_list', id = list_id)
    print(list)
    assert list

def test_delete_leads():
    global lead_id_1
    global lead_id_2
    response = mc.execute(method='delete_lead', id=[lead_id_1, lead_id_2])
    assert response[0]['status'] == 'deleted' and response[1]['status'] == 'deleted'

<|endoftext|>


<commit_before>import unittest

import upload

class TestUpload(unittest.TestCase):

    def test_is_included_same(self):
        a = {'a': 1}
        self.assert_(upload.is_included(a, a))

    def test_is_included_different(self):
        a = {'a': 1}
        b = {'a': 2}
        self.assert_(not upload.is_included(a, b))

    def test_is_included_different(self):
        a = {'a': 1, 'b': 2}
        b = {'a': 1, 'b': 2, 'c': 3}
        self.assert_(upload.is_included(a, b))

    def test_generate_ips(self):
        model = '192.168.1.10-12'
        self.assertEqual(list(upload._generate_values(model)),
                         ['192.168.1.10',
                          '192.168.1.11',
                          '192.168.1.12'])

    def test_generate_names(self):
        model = 'host10-12'
        self.assertEqual(list(upload._generate_values(model)),
                         ['host10', 'host11', 'host12'])

    def test_generate_range(self):
        self.assertEqual(list(upload._generate_range('10-12')),
                         ['10', '11', '12'])

    def test_generate_range_colon(self):
        self.assertEqual(list(upload._generate_range('1-3:10-12')),
                         ['1', '2', '3', '10', '11', '12'])

    def test_generate(self):
        model = {'gw': '192.168.1.1',
                 'ip': '192.168.1.10-12',
                 'hostname': 'host10-12'}
        self.assertEqual(
            upload.generate(model),
            [{'gw': '192.168.1.1', 'ip': '192.168.1.10', 'hostname': 'host10'},
             {'gw': '192.168.1.1', 'ip': '192.168.1.11', 'hostname': 'host11'},
             {'gw': '192.168.1.1', 'ip': '192.168.1.12', 'hostname': 'host12'}])

    def test_generate_253(self):
        result = upload.generate({'hostname': '10.0.1-2.2-254'})
        self.assertEqual(
            len(result),
            2 * 253,
            result)

if __name__ == "__main__":
    unittest.main()
<commit_msg>test_upload.py: fix duplicate test names<commit_after>import unittest

import upload

class TestUpload(unittest.TestCase):

    def test_is_included_same(self):
        a = {'a': 1}
        self.assert_(upload.is_included(a, a))

    def test_is_included_different(self):
        a = {'a': 1}
        b = {'a': 2}
        self.assert_(not upload.is_included(a, b))

    def test_is_included_more(self):
        a = {'a': 1, 'b': 2}
        b = {'a': 1, 'b': 2, 'c': 3}
        self.assert_(upload.is_included(a, b))

    def test_generate_ips(self):
        model = '192.168.1.10-12'
        self.assertEqual(list(upload._generate_values(model)),
                         ['192.168.1.10',
                          '192.168.1.11',
                          '192.168.1.12'])

    def test_generate_names(self):
        model = 'host10-12'
        self.assertEqual(list(upload._generate_values(model)),
                         ['host10', 'host11', 'host12'])

    def test_generate_range(self):
        self.assertEqual(list(upload._generate_range('10-12')),
                         ['10', '11', '12'])

    def test_generate_range_colon(self):
        self.assertEqual(list(upload._generate_range('1-3:10-12')),
                         ['1', '2', '3', '10', '11', '12'])

    def test_generate(self):
        model = {'gw': '192.168.1.1',
                 'ip': '192.168.1.10-12',
                 'hostname': 'host10-12'}
        self.assertEqual(
            upload.generate(model),
            [{'gw': '192.168.1.1', 'ip': '192.168.1.10', 'hostname': 'host10'},
             {'gw': '192.168.1.1', 'ip': '192.168.1.11', 'hostname': 'host11'},
             {'gw': '192.168.1.1', 'ip': '192.168.1.12', 'hostname': 'host12'}])

    def test_generate_253(self):
        result = upload.generate({'hostname': '10.0.1-2.2-254'})
        self.assertEqual(
            len(result),
            2 * 253,
            result)

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


<commit_before>import sys
import tensorflow as tf
from google.protobuf import text_format
from inputs import FeatureColumns, InputFn

VOCAB_FILE='/home/alan/Workspace/other/fastText/data/ag_news.train.vocab'
VOCAB_SIZE=95810
INPUT_FILE='/home/alan/Workspace/other/fastText/data/ag_news.train.tfrecords-1-of-1'

def test_parse_spec():
    fc = FeatureColumns(
        True,
        VOCAB_FILE,
        VOCAB_SIZE,
        10,
        10,
        1000,
        10)
    parse_spec = tf.feature_column.make_parse_example_spec(fc)
    print parse_spec
    reader = tf.python_io.tf_record_iterator(INPUT_FILE)
    sess = tf.Session()
    for record in reader:
        example = tf.parse_single_example(
            record,
            parse_spec)
        print sess.run(example)
        break

def test_reading_inputs():
    parse_spec = {
        "text": tf.VarLenFeature(tf.string),
        "label": tf.FixedLenFeature(shape=(1,), dtype=tf.int64,
                                    default_value=None)
    }
    sess = tf.Session()
    reader = tf.python_io.tf_record_iterator(INPUT_FILE)
    ESZ = 4
    HSZ = 100
    NC = 4
    n = 0
    text_lookup_table = tf.contrib.lookup.index_table_from_file(
        VOCAB_FILE, 10, VOCAB_SIZE)
    text_embedding_w = tf.Variable(tf.random_uniform(
        [VOCAB_SIZE, ESZ], -1.0, 1.0))
    sess.run([tf.tables_initializer()])
    for record in reader:
        example = tf.parse_single_example(
            record,
            parse_spec)
        labels = example["label"]
        labels1 = tf.subtract(labels, 1)
        dense = tf.sparse_tensor_to_dense(example["text"], default_value=" ")

        text_ids = text_lookup_table.lookup(dense)
        text_embedding = tf.reduce_mean(tf.nn.embedding_lookup(
            text_embedding_w, text_ids), axis=-2)
        text_embedding = tf.expand_dims(text_embedding, -2)
        num_classes = 2
        logits = tf.contrib.layers.fully_connected(
            inputs=text_embedding, num_outputs=4,
            activation_fn=None)
        sess.run([tf.global_variables_initializer()])
        loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=labels1, logits=logits)
        print sess.run([text_embedding, logits, labels, loss])
        if n > 50:
            break
        n += 1

if __name__ == '__main__':
    print "Test Parse Spec:"
    test_parse_spec()
    print "Test Input Fn"
    test_reading_inputs()
<commit_msg>trying bow_encoder<commit_after>import sys
import tensorflow as tf
from google.protobuf import text_format
from inputs import FeatureColumns, InputFn

VOCAB_FILE='/home/alan/Workspace/other/fastText/data/ag_news.train.vocab'
VOCAB_SIZE=95810
INPUT_FILE='/home/alan/Workspace/other/fastText/data/ag_news.train.tfrecords-1-of-1'

def test_parse_spec():
    fc = FeatureColumns(
        True,
        VOCAB_FILE,
        VOCAB_SIZE,
        10,
        10,
        1000,
        10)
    parse_spec = tf.feature_column.make_parse_example_spec(fc)
    print parse_spec
    reader = tf.python_io.tf_record_iterator(INPUT_FILE)
    sess = tf.Session()
    for record in reader:
        example = tf.parse_single_example(
            record,
            parse_spec)
        print sess.run(example)
        break

def test_reading_inputs():
    parse_spec = {
        "text": tf.VarLenFeature(tf.string),
        "label": tf.FixedLenFeature(shape=(1,), dtype=tf.int64,
                                    default_value=None)
    }
    sess = tf.Session()
    reader = tf.python_io.tf_record_iterator(INPUT_FILE)
    ESZ = 4
    HSZ = 100
    NC = 4
    n = 0
    text_lookup_table = tf.contrib.lookup.index_table_from_file(
        VOCAB_FILE, 10, VOCAB_SIZE)
    text_embedding_w = tf.Variable(tf.random_uniform(
        [VOCAB_SIZE, ESZ], -1.0, 1.0))
    sess.run([tf.tables_initializer()])
    for record in reader:
        example = tf.parse_single_example(
            record,
            parse_spec)
        labels = example["label"]
        labels1 = tf.subtract(labels, 1)
        dense = tf.sparse_tensor_to_dense(example["text"], default_value=" ")

        text_ids = text_lookup_table.lookup(dense)
        text_embedding = tf.reduce_mean(tf.nn.embedding_lookup(
            text_embedding_w, text_ids), axis=-2)
        print text_embedding.shape
        text_embedding = tf.expand_dims(text_embedding, -2)
        print text_embedding.shape
        num_classes = 2
        logits = tf.contrib.layers.fully_connected(
            inputs=text_embedding, num_outputs=4,
            activation_fn=None)
        sess.run([tf.global_variables_initializer()])
        loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=labels1, logits=logits)
        print sess.run([text_embedding, logits, labels, loss])
        if n > 50:
            break
        n += 1
        # Test bow_encoder
        text_embedding_2 = tf.contrib.layers.bow_encoder(
            text_ids, VOCAB_SIZE, ESZ)
        print text_embedding_2.shape
        print sess.run(text_embedding_2)

if __name__ == '__main__':
    print "Test Parse Spec:"
    test_parse_spec()
    print "Test Input Fn"
    test_reading_inputs()
<|endoftext|>


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

"""
MSG Install Script

This script is useful for updating an existing MSG software installation from an
active development code base.

This is an interface to the install process that automates the following
operations.

1. Create the distribution archive.
2. Extract the distribution archive.
2. Install the distribution from extracted archive.

This is useful to automatically maintain an existing installation based on
the most recent source code base.

Usage:

    python install-msg.py --sourcePath ${ROOT_PATH_TO_SOURCE}
    --installPathUser ${BASE_PATH_OF_USER_BASED_INSTALL}

The distribution archive is placed in ${ROOT_PATH_TO_SOURCE}/dist.

The software is installed to path given by the base path to a directory named
after the software including its version number. For example, if the user
install path is  given as ~/software the software will be installed to
~/software/Maui-Smart-Grid-1.x.x where 1.x.x represents the appropriate
version number.

"""

__author__ = 'Daniel Zhang (張道博)'
__copyright__ = 'Copyright (c) 2013, University of Hawaii Smart Energy Project'
__license__ = 'https://raw.github' \
              '.com/Hawaii-Smart-Energy-Project/Maui-Smart-Grid/master/BSD' \
              '-LICENSE.txt'

import subprocess
import argparse
import os
from msg_logger import MSGLogger
import tarfile

def processCommandLineArguments():
    global argParser, commandLineArgs
    argParser = argparse.ArgumentParser(description = '')
    argParser.add_argument('--sourcePath',
                           help = 'Path where the source code is located.',
                           required = True)
    argParser.add_argument('--installPathUser',
                           help = 'Path to be used for a user based install.',
                           required = True)

    commandLineArgs = argParser.parse_args()

def runCommand(cmd = None):
    if cmd is not None:
        try:
            subprocess.check_call(cmd, shell = True)
        except subprocess.CalledProcessError, e:
            logger.log("An exception occurred: %s" % e, 'error')

def softwareInstallName():
    cmd = "python %s/setup.py --name" % commandLineArgs.sourcePath
    try:
        softwareName = subprocess.check_output(cmd, shell = True)
    except subprocess.CalledProcessError, e:
        logger.log("An exception occurred: %s" % e, 'error')
    return softwareName.strip()

def softwareVersion():
    """
    There may be issues with retrieving the version number this way when
    dependencies are defined in __init.py__.
    """

    cmd = "python %s/setup.py --version" % commandLineArgs.sourcePath
    try:
        softwareVersion = subprocess.check_output(cmd, shell = True)
    except subprocess.CalledProcessError, e:
        logger.log("An exception occurred: %s" % e, 'error')
    return softwareVersion.strip()

commandLineArgs = None
logger = MSGLogger(__name__)
processCommandLineArguments()

os.chdir(commandLineArgs.sourcePath)

archiveCmd = """python setup.py sdist"""
runCommand(archiveCmd)

PROJECT_NAME = softwareInstallName()
VERSION = softwareVersion()

print "Current working directory is %s." % os.getcwd()
print "\nPerforming scripted install of %s-%s." % (PROJECT_NAME, VERSION)

# Extract the distribution archive into the dist directory.
os.chdir('%s/dist' % commandLineArgs.sourcePath)
print
logger.log("Current working directory is %s." % os.getcwd())
t = tarfile.open(name = '%s/dist/%s-%s.tar.gz' % (
    commandLineArgs.sourcePath, PROJECT_NAME, VERSION))

t.extractall()

# Change the working directory to the extracted archive path.
logger.log("Performing install.")
os.chdir('%s/dist/%s-%s' % (commandLineArgs.sourcePath, PROJECT_NAME, VERSION ))
logger.log("Current working directory is %s." % os.getcwd())

# Install the software.
installCmd = """python setup.py install --home=%s/%s-%s""" % (
    commandLineArgs.installPathUser, PROJECT_NAME, VERSION)
runCommand(installCmd)

print "\nInstallation of the MSG software to %s is complete." % \
      commandLineArgs.installPathUser
print "\nPlease add the path, %s/lib/python, to your PYTHONPATH if it is not " \
      "already there." % commandLineArgs.installPathUser
<commit_msg>removing circular dependency on msg_logger<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
MSG Install Script

This script is useful for updating an existing MSG software installation from an
active development code base.

This is an interface to the install process that automates the following
operations.

1. Create the distribution archive.
2. Extract the distribution archive.
2. Install the distribution from extracted archive.

This is useful to automatically maintain an existing installation based on
the most recent source code base.

Usage:

    python install-msg.py --sourcePath ${ROOT_PATH_TO_SOURCE}
    --installPathUser ${BASE_PATH_OF_USER_BASED_INSTALL}

The distribution archive is placed in ${ROOT_PATH_TO_SOURCE}/dist.

The software is installed to path given by the base path to a directory named
after the software including its version number. For example, if the user
install path is  given as ~/software the software will be installed to
~/software/Maui-Smart-Grid-1.x.x where 1.x.x represents the appropriate
version number.

The external dependencies, documented at

    https://github.com/Hawaii-Smart-Energy-Project/Maui-Smart-Grid
    #installation-and-updating

should be satisfied using a suitable method such as installation through pip.
"""

__author__ = 'Daniel Zhang (張道博)'
__copyright__ = 'Copyright (c) 2013, University of Hawaii Smart Energy Project'
__license__ = 'https://raw.github' \
              '.com/Hawaii-Smart-Energy-Project/Maui-Smart-Grid/master/BSD' \
              '-LICENSE.txt'

import subprocess
import argparse
import os
import tarfile

def processCommandLineArguments():
    global argParser, commandLineArgs
    argParser = argparse.ArgumentParser(description = '')
    argParser.add_argument('--sourcePath',
                           help = 'Path where the source code is located.',
                           required = True)
    argParser.add_argument('--installPathUser',
                           help = 'Path to be used for a user based install.',
                           required = True)

    commandLineArgs = argParser.parse_args()

def runCommand(cmd = None):
    if cmd is not None:
        try:
            subprocess.check_call(cmd, shell = True)
        except subprocess.CalledProcessError as error:
            print "An exception occurred: %s" % error

def softwareInstallName():
    cmd = "python %s/setup.py --name" % commandLineArgs.sourcePath
    try:
        softwareName = subprocess.check_output(cmd, shell = True)
    except subprocess.CalledProcessError as error:
        print "An exception occurred: %s" % error
    return softwareName.strip()

def softwareVersion():
    """
    There may be issues with retrieving the version number this way when
    dependencies are defined in __init.py__.
    """

    cmd = "python %s/setup.py --version" % commandLineArgs.sourcePath
    try:
        softwareVersion = subprocess.check_output(cmd, shell = True)
    except subprocess.CalledProcessError as error:
        print "An exception occurred: %s" % error
    return softwareVersion.strip()

commandLineArgs = None
processCommandLineArguments()

os.chdir(commandLineArgs.sourcePath)

archiveCmd = """python setup.py sdist"""
runCommand(archiveCmd)

PROJECT_NAME = softwareInstallName()
VERSION = softwareVersion()

print "Current working directory is %s." % os.getcwd()
print "\nPerforming scripted install of %s-%s." % (PROJECT_NAME, VERSION)

# Extract the distribution archive into the dist directory.
os.chdir('%s/dist' % commandLineArgs.sourcePath)
print
print "Current working directory is %s." % os.getcwd()
t = tarfile.open(name = '%s/dist/%s-%s.tar.gz' % (
    commandLineArgs.sourcePath, PROJECT_NAME, VERSION))

t.extractall()

# Change the working directory to the extracted archive path.
print "Performing install."
os.chdir('%s/dist/%s-%s' % (commandLineArgs.sourcePath, PROJECT_NAME, VERSION ))
print "Current working directory is %s." % os.getcwd()

# Install the software.
installCmd = """python setup.py install --home=%s/%s-%s""" % (
    commandLineArgs.installPathUser, PROJECT_NAME, VERSION)
runCommand(installCmd)

print "\nInstallation of the MSG software to %s is complete." % \
      commandLineArgs.installPathUser
print "\nPlease add the path, %s/lib/python, to your PYTHONPATH if it is not " \
      "already there." % commandLineArgs.installPathUser
<|endoftext|>


<commit_before>##############################################################################
#
#    Copyright (C) 2016 Comunitea Servicios Tecnológicos
#    $Omar Castiñeira Saavedra <omar@comunitea.com>$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from odoo import models, api, fields, exceptions, _
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
import time

class ClaimMakePicking(models.TransientModel):

    _inherit = 'claim_make_picking.wizard'

    odoo_management = fields.Boolean('Management in Odoo')
    not_sync = fields.Boolean("Not sync", help="This picking not will be "
                                               "synced with Vstock",
                              readonly=True)

    @api.onchange('claim_line_source_location', 'claim_line_dest_location')
    def onchange_locations(self):
        if self.claim_line_source_location.odoo_management:
            if self.claim_line_dest_location in \
                    [self.env.ref('stock.stock_location_customers'),
                     self.env.ref('stock.stock_location_suppliers')]:
                self.odoo_management = True
            else:
                self.odoo_management = False
        else:
            self.odoo_management = False
        if self.claim_line_dest_location.not_sync:
            self.not_sync = True
        else:
            self.not_sync = False

    @api.multi
    def create_move(self, claim_line, p_type, picking_id, claim, note, write_field):
        type_ids = self.env['stock.picking.type'].search([('code', '=', p_type)]).ids
        if claim_line.product_id.type == 'service':
            if claim_line.product_id.pack_line_ids:
                partner_id = claim.delivery_address_id and \
                    claim.delivery_address_id.id or claim.partner_id.id
                pack = claim_line.product_id.get_pack()
                for product_id in pack:
                    product = self.env['product.product'].browse(product_id)
                    move = self.env['stock.move'].create(
                        {'name': product.name_template,
                         'priority': '0',
                         'date': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT),
                         'date_expected': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT),
                         'product_id': product.id,
                         'picking_type_id': type_ids and type_ids[0],
                         'product_uom_qty': pack[product.id] * claim_line.product_returned_quantity,
                         'product_uom': product.uom_id.id,
                         'partner_id': partner_id,
                         'picking_id': picking_id,
                         'state': 'draft',
                         'company_id': claim.company_id.id,
                         'location_id': self.claim_line_source_location.id,
                         'location_dest_id': self.claim_line_dest_location.id,
                         'note': note,
                         'claim_line_id': claim_line.id
                         })
                    if p_type == 'outgoing' and product.type == 'product':
                        reserv_vals = {
                            'product_id': product.id,
                            'product_uom': product.uom_id.id,
                            'product_uom_qty': pack[product.id] * claim_line.product_returned_quantity,
                            'date_validity': False,
                            'name': u"{}".format(claim_line.claim_id.number),
                            'location_id': self.claim_line_source_location.id,
                            'location_dest_id': self.claim_line_dest_location.id,
                            'move_id': move.id,
                            'claim_id': claim_line.claim_id.id,
                        }
                        reserve = self.env['stock.reservation'].create(reserv_vals)
                        reserve.reserve()
                    claim_line.write({write_field: move.id})
        else:
            return super(ClaimMakePicking, self).create_move(
                claim_line, p_type, picking_id, claim, note, write_field)

    @api.multi
    def action_create_picking(self):
        res = super(ClaimMakePicking, self).action_create_picking()
        if self.odoo_management or (self.not_sync or
                                    self.claim_line_dest_location.not_sync):
            if self.odoo_management and \
                    not self.claim_line_source_location.odoo_management:
                raise exceptions.Warning(_("The origin location is not managed"
                                           " by Odoo."))
            if res.get('res_id', False):
                pick = self.env["stock.picking"].browse(res['res_id'])
                pick.odoo_management = self.odoo_management
                pick.not_sync = self.not_sync or \
                    self.claim_line_dest_location.not_sync

        return res

    @api.model
    def default_get(self, vals):
        if self.env.context.get('type', False) == 'customer' and \
                self.env.context.get('picking_type', False) == 'out' and \
                self.env.context.get('partner_id', False):
            partner_id = self.env.context.get('partner_id')
            partner = self.env['res.partner'].browse(partner_id)
            claim = self.env['crm.claim'].browse(
                self.env.context.get('active_id', False))
            if partner.commercial_partner_id.blocked_sales and not \
                    claim.allow_confirm_blocked:
                raise exceptions.Warning(
                    _("Warning for %s") % partner.commercial_partner_id.name,
                    _('Customer blocked by lack of payment. Check the maturity dates of their account move lines.'))
        return super(ClaimMakePicking, self).default_get(vals)
<commit_msg>[FIX]'crm_claim_rma_custom': arreglado warning problemático<commit_after>##############################################################################
#
#    Copyright (C) 2016 Comunitea Servicios Tecnológicos
#    $Omar Castiñeira Saavedra <omar@comunitea.com>$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from odoo import models, api, fields, exceptions, _
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
import time

class ClaimMakePicking(models.TransientModel):

    _inherit = 'claim_make_picking.wizard'

    odoo_management = fields.Boolean('Management in Odoo')
    not_sync = fields.Boolean("Not sync", help="This picking not will be "
                                               "synced with Vstock",
                              readonly=True)

    @api.onchange('claim_line_source_location', 'claim_line_dest_location')
    def onchange_locations(self):
        if self.claim_line_source_location.odoo_management:
            if self.claim_line_dest_location in \
                    [self.env.ref('stock.stock_location_customers'),
                     self.env.ref('stock.stock_location_suppliers')]:
                self.odoo_management = True
            else:
                self.odoo_management = False
        else:
            self.odoo_management = False
        if self.claim_line_dest_location.not_sync:
            self.not_sync = True
        else:
            self.not_sync = False

    @api.multi
    def create_move(self, claim_line, p_type, picking_id, claim, note, write_field):
        type_ids = self.env['stock.picking.type'].search([('code', '=', p_type)]).ids
        if claim_line.product_id.type == 'service':
            if claim_line.product_id.pack_line_ids:
                partner_id = claim.delivery_address_id and \
                    claim.delivery_address_id.id or claim.partner_id.id
                pack = claim_line.product_id.get_pack()
                for product_id in pack:
                    product = self.env['product.product'].browse(product_id)
                    move = self.env['stock.move'].create(
                        {'name': product.name_template,
                         'priority': '0',
                         'date': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT),
                         'date_expected': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT),
                         'product_id': product.id,
                         'picking_type_id': type_ids and type_ids[0],
                         'product_uom_qty': pack[product.id] * claim_line.product_returned_quantity,
                         'product_uom': product.uom_id.id,
                         'partner_id': partner_id,
                         'picking_id': picking_id,
                         'state': 'draft',
                         'company_id': claim.company_id.id,
                         'location_id': self.claim_line_source_location.id,
                         'location_dest_id': self.claim_line_dest_location.id,
                         'note': note,
                         'claim_line_id': claim_line.id
                         })
                    if p_type == 'outgoing' and product.type == 'product':
                        reserv_vals = {
                            'product_id': product.id,
                            'product_uom': product.uom_id.id,
                            'product_uom_qty': pack[product.id] * claim_line.product_returned_quantity,
                            'date_validity': False,
                            'name': u"{}".format(claim_line.claim_id.number),
                            'location_id': self.claim_line_source_location.id,
                            'location_dest_id': self.claim_line_dest_location.id,
                            'move_id': move.id,
                            'claim_id': claim_line.claim_id.id,
                        }
                        reserve = self.env['stock.reservation'].create(reserv_vals)
                        reserve.reserve()
                    claim_line.write({write_field: move.id})
        else:
            return super(ClaimMakePicking, self).create_move(
                claim_line, p_type, picking_id, claim, note, write_field)

    @api.multi
    def action_create_picking(self):
        res = super(ClaimMakePicking, self).action_create_picking()
        if self.odoo_management or (self.not_sync or
                                    self.claim_line_dest_location.not_sync):
            if self.odoo_management and \
                    not self.claim_line_source_location.odoo_management:
                raise exceptions.Warning(_("The origin location is not managed"
                                           " by Odoo."))
            if res.get('res_id', False):
                pick = self.env["stock.picking"].browse(res['res_id'])
                pick.odoo_management = self.odoo_management
                pick.not_sync = self.not_sync or \
                    self.claim_line_dest_location.not_sync

        return res

    @api.model
    def default_get(self, vals):
        if self.env.context.get('type', False) == 'customer' and \
                self.env.context.get('picking_type', False) == 'out' and \
                self.env.context.get('partner_id', False):
            partner_id = self.env.context.get('partner_id')
            partner = self.env['res.partner'].browse(partner_id)
            claim = self.env['crm.claim'].browse(
                self.env.context.get('active_id', False))
            if partner.commercial_partner_id.blocked_sales and not \
                    claim.allow_confirm_blocked:
                raise exceptions.Warning(
                    _('Customer blocked by lack of payment. Check the maturity dates of their account move lines.'))
        return super(ClaimMakePicking, self).default_get(vals)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2018 Visiotech All Rights Reserved
#    $Jesus Garcia Manzanas <jgmanzanas@visiotechsecurity.com>$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp import models
from openerp.addons.connector.event import on_record_create, on_record_write, \
    on_record_unlink
from openerp.addons.connector.queue.job import job
from .utils import _get_exporter
from ..backend import middleware
from openerp.addons.connector.unit.synchronizer import Exporter
from ..unit.backend_adapter import GenericAdapter

@middleware
class OrderExporter(Exporter):

    _model_name = ['sale.order']

    def update(self, binding_id, mode):
        order = self.model.browse(binding_id)
        vals = {"odoo_id": order.id,
                "name": order.name,
                "state": order.state,
                "partner_id": order.partner_id.id,
                "amount_total": order.amount_total,
                "   ": order.date_order,
                "client_order_ref": order.client_order_ref,
        }
        if mode == "insert":
            return self.backend_adapter.insert(vals)
        else:
            return self.backend_adapter.update(binding_id, vals)

    def delete(self, binding_id):
        return self.backend_adapter.remove(binding_id)

@middleware
class OrderAdapter(GenericAdapter):
    _model_name = 'sale.order'
    _middleware_model = 'order'

@on_record_create(model_names='sale.order')
def delay_export_order_create(session, model_name, record_id, vals):
    order = session.env[model_name].browse(record_id)
    if order.partner_id.web or order.partner_id.commercial_partner_id.web:
        if 'state' in vals.keys() and vals['state'] in ('draft', 'reserve'):
            export_order.delay(session, model_name, record_id, priority=2, eta=80)

@on_record_write(model_names='sale.order')
def delay_export_order_write(session, model_name, record_id, vals):
    order = session.env[model_name].browse(record_id)
    up_fields = ["name", "state", "partner_id", "amount_total", "date_order", "client_order_ref"]
    if order.partner_id.web or order.partner_id.commercial_partner_id.web:
        if 'state' in vals.keys() and vals['state'] not in ('done', 'progress', 'reserve'):
            unlink_order.delay(session, model_name, record_id, priority=7, eta=180)
        elif 'state' in vals.keys() and vals['state'] in  ('draft', 'reserve'):
            export_order.delay(session, model_name, record_id, priority=2, eta=80)
        for field in up_fields:
            if field in vals:
                update_order.delay(session, model_name, record_id, priority=5, eta=120)
                break

@on_record_unlink(model_names='sale.order')
def delay_export_order_unlink(session, model_name, record_id, vals):
    order = session.env[model_name].browse(record_id)
    if order.partner_id.web or order.partner_id.commercial_partner_id.web:
        unlink_order.delay(session, model_name, record_id, priority=7, eta=180)

@job(retry_pattern={1: 10 * 60, 2: 20 * 60, 3: 30 * 60, 4: 40 * 60,
                    5: 50 * 60})
def export_order(session, model_name, record_id):
    order_exporter = _get_exporter(session, model_name, record_id,
                                   OrderExporter)
    return order_exporter.update(record_id, "insert")

@job(retry_pattern={1: 10 * 60, 2: 20 * 60, 3: 30 * 60, 4: 40 * 60,
                    5: 50 * 60})
def update_order(session, model_name, record_id):
    order_exporter = _get_exporter(session, model_name, record_id,
                                   OrderExporter)
    return order_exporter.update(record_id, "update")

@job(retry_pattern={1: 10 * 60, 2: 20 * 60, 3: 30 * 60, 4: 40 * 60,
                    5: 50 * 60})
def unlink_order(session, model_name, record_id):
    order_exporter = _get_exporter(session, model_name, record_id,
                                   OrderExporter)
    return order_exporter.delete(record_id)

@middleware
class OrderProductExporter(Exporter):

    _model_name = ['sale.order.line']

    def update(self, binding_id, mode):
        orderproduct = self.model.browse(binding_id)
        vals = {"odoo_id": orderproduct.id,
                "product_id": orderproduct.product_id.id,
                "product_qty": orderproduct.product_uom_qty,
                "price_subtotal": orderproduct.price_subtotal,
                "order_id": orderproduct.order_id.id,
        }
        if mode == "insert":
            return self.backend_adapter.insert(vals)
        else:
            return self.backend_adapter.update(binding_id, vals)

    def delete(self, binding_id):
        return self.backend_adapter.remove(binding_id)

@middleware
class OrderProductAdapter(GenericAdapter):
    _model_name = 'sale.order.line'
    _middleware_model = 'orderproduct'

@on_record_create(model_names='sale.order.line')
def delay_export_orderproduct_create(session, model_name, record_id, vals):
    orderproduct = session.env[model_name].browse(record_id)
    if orderproduct.order_id.partner_id.web or orderproduct.order_id.partner_id.commercial_partner_id.web:
        export_orderproduct.delay(session, model_name, record_id, priority=2, eta=120)

@on_record_write(model_names='sale.order.line')
def delay_export_orderproduct_write(session, model_name, record_id, vals):
    orderproduct = session.env[model_name].browse(record_id)
    up_fields = ["product_id", "product_qty", "price_subtotal", "order_id"]
    if orderproduct.order_id.partner_id.web or orderproduct.order_id.partner_id.commercial_partner_id.web:
        for field in up_fields:
            if field in vals:
                update_orderproduct.delay(session, model_name, record_id, priority=5, eta=120)
                break

@on_record_unlink(model_names='sale.order.line')
def delay_export_orderproduct_unlink(session, model_name, record_id, vals):
    orderproduct = session.env[model_name].browse(record_id)
    if orderproduct.order_id.partner_id.web or orderproduct.order_id.partner_id.commercial_partner_id.web:
        unlink_orderproduct.delay(session, model_name, record_id, priority=7, eta=180)

@job(retry_pattern={1: 10 * 60, 2: 20 * 60, 3: 30 * 60, 4: 40 * 60,
                    5: 50 * 60})
def export_orderproduct(session, model_name, record_id):
    orderproduct_exporter = _get_exporter(session, model_name, record_id,
                                          OrderProductExporter)
    return orderproduct_exporter.update(record_id, "insert")

@job(retry_pattern={1: 10 * 60, 2: 20 * 60, 3: 30 * 60, 4: 40 * 60,
                    5: 50 * 60})
def update_orderproduct(session, model_name, record_id):
    orderproduct_exporter = _get_exporter(session, model_name, record_id,
                                          OrderProductExporter)
    return orderproduct_exporter.update(record_id, "update")

@job(retry_pattern={1: 10 * 60, 2: 20 * 60, 3: 30 * 60, 4: 40 * 60,
                    5: 50 * 60})
def unlink_orderproduct(session, model_name, record_id):
    orderproduct_exporter = _get_exporter(session, model_name, record_id,
                                          OrderProductExporter)
    return orderproduct_exporter.delete(record_id)

<commit_msg>[FIX] 'flask_middleware_connector': Optimizaciones en up_fields<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2018 Visiotech All Rights Reserved
#    $Jesus Garcia Manzanas <jgmanzanas@visiotechsecurity.com>$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp import models
from openerp.addons.connector.event import on_record_create, on_record_write, \
    on_record_unlink
from openerp.addons.connector.queue.job import job
from .utils import _get_exporter
from ..backend import middleware
from openerp.addons.connector.unit.synchronizer import Exporter
from ..unit.backend_adapter import GenericAdapter

@middleware
class OrderExporter(Exporter):

    _model_name = ['sale.order']

    def update(self, binding_id, mode):
        order = self.model.browse(binding_id)
        vals = {"odoo_id": order.id,
                "name": order.name,
                "state": order.state,
                "partner_id": order.partner_id.id,
                "amount_total": order.amount_total,
                "date_order": order.date_order,
                "amount_untaxed": order.amount_untaxed,
                "client_order_ref": order.client_order_ref,
        }
        if mode == "insert":
            return self.backend_adapter.insert(vals)
        else:
            return self.backend_adapter.update(binding_id, vals)

    def delete(self, binding_id):
        return self.backend_adapter.remove(binding_id)

@middleware
class OrderAdapter(GenericAdapter):
    _model_name = 'sale.order'
    _middleware_model = 'order'

@on_record_create(model_names='sale.order')
def delay_export_order_create(session, model_name, record_id, vals):
    order = session.env[model_name].browse(record_id)
    if order.partner_id.web or order.partner_id.commercial_partner_id.web:
        if 'state' in vals.keys() and vals['state'] in ('draft', 'reserve'):
            export_order.delay(session, model_name, record_id, priority=2, eta=80)

@on_record_write(model_names='sale.order')
def delay_export_order_write(session, model_name, record_id, vals):
    order = session.env[model_name].browse(record_id)
    #He cogido order_line porque no entra amount_total ni amount_untaxed en el write
    up_fields = ["name", "state", "partner_id", "date_order", "client_order_ref", "order_line"]
    if order.partner_id.web or order.partner_id.commercial_partner_id.web:
        if 'state' in vals.keys() and vals['state'] not in ('done', 'progress', 'reserve'):
            unlink_order.delay(session, model_name, record_id, priority=7, eta=180)
        elif 'state' in vals.keys() and vals['state'] in  ('draft', 'reserve'):
            export_order.delay(session, model_name, record_id, priority=2, eta=80)
        for field in up_fields:
            if field in vals:
                update_order.delay(session, model_name, record_id, priority=5, eta=120)
                break

@on_record_unlink(model_names='sale.order')
def delay_export_order_unlink(session, model_name, record_id, vals):
    order = session.env[model_name].browse(record_id)
    if order.partner_id.web or order.partner_id.commercial_partner_id.web:
        unlink_order.delay(session, model_name, record_id, priority=7, eta=180)

@job(retry_pattern={1: 10 * 60, 2: 20 * 60, 3: 30 * 60, 4: 40 * 60,
                    5: 50 * 60})
def export_order(session, model_name, record_id):
    order_exporter = _get_exporter(session, model_name, record_id,
                                   OrderExporter)
    return order_exporter.update(record_id, "insert")

@job(retry_pattern={1: 10 * 60, 2: 20 * 60, 3: 30 * 60, 4: 40 * 60,
                    5: 50 * 60})
def update_order(session, model_name, record_id):
    order_exporter = _get_exporter(session, model_name, record_id,
                                   OrderExporter)
    return order_exporter.update(record_id, "update")

@job(retry_pattern={1: 10 * 60, 2: 20 * 60, 3: 30 * 60, 4: 40 * 60,
                    5: 50 * 60})
def unlink_order(session, model_name, record_id):
    order_exporter = _get_exporter(session, model_name, record_id,
                                   OrderExporter)
    return order_exporter.delete(record_id)

@middleware
class OrderProductExporter(Exporter):

    _model_name = ['sale.order.line']

    def update(self, binding_id, mode):
        orderproduct = self.model.browse(binding_id)
        vals = {"odoo_id": orderproduct.id,
                "product_id": orderproduct.product_id.id,
                "product_qty": orderproduct.product_uom_qty,
                "price_subtotal": orderproduct.price_subtotal,
                "order_id": orderproduct.order_id.id,
        }
        if mode == "insert":
            return self.backend_adapter.insert(vals)
        else:
            return self.backend_adapter.update(binding_id, vals)

    def delete(self, binding_id):
        return self.backend_adapter.remove(binding_id)

@middleware
class OrderProductAdapter(GenericAdapter):
    _model_name = 'sale.order.line'
    _middleware_model = 'orderproduct'

@on_record_create(model_names='sale.order.line')
def delay_export_orderproduct_create(session, model_name, record_id, vals):
    orderproduct = session.env[model_name].browse(record_id)
    if orderproduct.order_id.partner_id.web or orderproduct.order_id.partner_id.commercial_partner_id.web:
        export_orderproduct.delay(session, model_name, record_id, priority=2, eta=120)

@on_record_write(model_names='sale.order.line')
def delay_export_orderproduct_write(session, model_name, record_id, vals):
    orderproduct = session.env[model_name].browse(record_id)
    up_fields = ["product_id", "product_uom_qty", "price_unit", "discount", "order_id"]
    if orderproduct.order_id.partner_id.web or orderproduct.order_id.partner_id.commercial_partner_id.web:
        for field in up_fields:
            if field in vals:
                update_orderproduct.delay(session, model_name, record_id, priority=5, eta=120)
                break

@on_record_unlink(model_names='sale.order.line')
def delay_export_orderproduct_unlink(session, model_name, record_id, vals):
    orderproduct = session.env[model_name].browse(record_id)
    if orderproduct.order_id.partner_id.web or orderproduct.order_id.partner_id.commercial_partner_id.web:
        unlink_orderproduct.delay(session, model_name, record_id, priority=7, eta=180)

@job(retry_pattern={1: 10 * 60, 2: 20 * 60, 3: 30 * 60, 4: 40 * 60,
                    5: 50 * 60})
def export_orderproduct(session, model_name, record_id):
    orderproduct_exporter = _get_exporter(session, model_name, record_id,
                                          OrderProductExporter)
    return orderproduct_exporter.update(record_id, "insert")

@job(retry_pattern={1: 10 * 60, 2: 20 * 60, 3: 30 * 60, 4: 40 * 60,
                    5: 50 * 60})
def update_orderproduct(session, model_name, record_id):
    orderproduct_exporter = _get_exporter(session, model_name, record_id,
                                          OrderProductExporter)
    return orderproduct_exporter.update(record_id, "update")

@job(retry_pattern={1: 10 * 60, 2: 20 * 60, 3: 30 * 60, 4: 40 * 60,
                    5: 50 * 60})
def unlink_orderproduct(session, model_name, record_id):
    orderproduct_exporter = _get_exporter(session, model_name, record_id,
                                          OrderProductExporter)
    return orderproduct_exporter.delete(record_id)

<|endoftext|>