<commit_before># from hypothesis import composite
from hypothesis import event
from hypothesis.strategies import (
    text, integers, booleans, lists, composite, uuids, sampled_from, none, one_of,
    permutations)

RESOURCES = [
    'unit',
    'organization',
    'department',
    'ontologyword',
    'ontologytree',
]

def int_keys(draw):
    return draw(lists(
        integers(min_value=1, max_value=15),
        min_size=1, max_size=3, unique=True))

def uuid_keys(draw):
    return draw(lists(uuids(), min_size=2, max_size=3))

def translated_field(draw, name, allow_missing=True, languages=['fi', 'sv', 'en']):
    result = {}
    for lang in languages:
        if allow_missing:
            val = draw(one_of(text(), none()))
        else:
            val = draw(text())
        if val is not None:
            result['{}_{}'.format(name, lang)] = val
    return result

VIEWPOINTS = ['00', '11', '12', '13', '21', '22', '23', '31', '32', '33', '41',
              '51', '52', '61']

VIEWPOINT_STATES = ['green', 'red', 'unknown']

MUNICIPALITIES = [  # TODO: read from independent source
    {
        'fi': 'Helsinki',
        'sv': 'Helsingfors',
        'en': 'Helsinki'
    },
    {
        'fi': 'Espoo',
        'sv': 'Esbo',
        'en': 'Espoo'
    },
    {
        'fi': 'Kauniainen',
        'sv': 'Grankulla',
        'en': 'Kauniainen'
    },
    {
        'fi': 'Vantaa',
        'sv': 'Vanda',
        'en': 'Vantaa'
    }
]

def accessibility_viewpoints(draw):
    return ','.join([
        '{}:{}'.format(key, draw(sampled_from(VIEWPOINT_STATES))) for key in VIEWPOINTS])

PROVIDER_TYPES = [
    "CONTRACT_SCHOOL",
    "OTHER_PRODUCTION_METHOD",
    "PAYMENT_COMMITMENT",
    "PURCHASED_SERVICE",
    "SELF_PRODUCED",
    "SUPPORTED_OPERATIONS",
    "UNKNOWN_PRODUCTION_METHOD"]

ORGANIZER_TYPES = [
    "ASSOCIATION",
    "FOUNDATION",
    "GOVERNMENT",
    "GOVERNMENTAL_COMPANY",
    "JOINT_MUNICIPAL_AUTHORITY",
    "MUNICIPAL_ENTERPRISE_GROUP",
    "MUNICIPALITY",
    "MUNICIPALLY_OWNED_COMPANY",
    "ORGANIZATION",
    "OTHER_REGIONAL_COOPERATION_ORGANIZATION",
    "PRIVATE_ENTERPRISE",
    "UNKNOWN"
]

@composite
def make_source(draw):
    return {'id': draw(text()), 'source': draw(text())}

def unit_maker(draw, resource_ids):
    def make_unit(uid):
        # Required fields
        result = {
            'id': uid,
            'accessibility_viewpoints': accessibility_viewpoints(draw),
            'dept_id': str(draw(sampled_from(resource_ids['department']))),
            'org_id': str(draw(sampled_from(resource_ids['organization']))),
            'ontologyword_ids': draw(permutations(resource_ids['ontologyword'])),
            'ontologytree_ids': draw(permutations(resource_ids['ontologytree'])),
            'sources': draw(lists(make_source(), min_size=0, max_size=2)),
            'provider_type': draw(sampled_from(PROVIDER_TYPES)),
            'manual_coordinates': draw(booleans()),
            # TODO: cannot test is_public=False until there is a mechanism
            # for getting non-public units from the API.
            'is_public': True,
            # TODO: map to another field
        }
        result.update(translated_field(draw, 'name', allow_missing=False))

        def add_optional_field(name, strategy):
            val = draw(one_of(none(), strategy))
            if val is not None:
                event('unit.{}: optional field given value'.format(name))
                result[name] = val
            else:
                event('unit.{}: optional field missing'.format(name))

        add_optional_field('address_city', sampled_from(MUNICIPALITIES))
        add_optional_field('accessibility_email', text())
        add_optional_field('accessibility_www', text())
        add_optional_field('address_zip', text())
        add_optional_field('data_source_url', text())
        add_optional_field('email', text())
        add_optional_field('fax', text())
        add_optional_field('phone', text())
        add_optional_field('organizer_business_id', text(max_size=11))
        add_optional_field('source', text())

        result.update(translated_field(draw, 'address_postal_full', allow_missing=True))
        result.update(translated_field(draw, 'call_charge_info', allow_missing=True))
        result.update(translated_field(draw, 'desc', allow_missing=True))
        result.update(translated_field(draw, 'picture_caption', allow_missing=True))
        return result
    return make_unit

def organization_maker(*args):
    return lambda x: {'id': str(x)}

def department_maker(draw, resource_ids):
    def make_department(did):
        return {
            'id': str(did),
            'hierarchy_level': 0,
            'org_id': str(draw(sampled_from(resource_ids['organization'])))
        }
    return make_department

def ontologyword_maker(*args):
    return lambda x: {'id': x}

def ontologytree_maker(*args):
    return lambda x: {'id': x}

make_resource = {}
for r in RESOURCES:
    make_resource[r] = locals()['{}_maker'.format(r)]

@composite
def closed_object_set(draw):
    ids = {
        'unit': int_keys(draw),
        'organization': uuid_keys(draw),
        'department': uuid_keys(draw),
        'ontologyword': int_keys(draw),
        'ontologytree': int_keys(draw)
    }
    resources = {}
    for key, identifiers in ids.items():
        resources[key] = list(map(make_resource[key](draw, ids), ids[key]))
    return resources
<commit_msg>Increase required translated fields min length<commit_after># from hypothesis import composite
from hypothesis import event
from hypothesis.strategies import (
    text, integers, booleans, lists, composite, uuids, sampled_from, none, one_of,
    permutations)

RESOURCES = [
    'unit',
    'organization',
    'department',
    'ontologyword',
    'ontologytree',
]

def int_keys(draw):
    return draw(lists(
        integers(min_value=1, max_value=15),
        min_size=1, max_size=3, unique=True))

def uuid_keys(draw):
    return draw(lists(uuids(), min_size=2, max_size=3))

def translated_field(draw, name, allow_missing=True, languages=['fi', 'sv', 'en']):
    result = {}
    for lang in languages:
        if allow_missing:
            val = draw(one_of(text(), none()))
        else:
            val = draw(text(min_size=1))
        if val is not None:
            result['{}_{}'.format(name, lang)] = val
    return result

VIEWPOINTS = ['00', '11', '12', '13', '21', '22', '23', '31', '32', '33', '41',
              '51', '52', '61']

VIEWPOINT_STATES = ['green', 'red', 'unknown']

MUNICIPALITIES = [  # TODO: read from independent source
    {
        'fi': 'Helsinki',
        'sv': 'Helsingfors',
        'en': 'Helsinki'
    },
    {
        'fi': 'Espoo',
        'sv': 'Esbo',
        'en': 'Espoo'
    },
    {
        'fi': 'Kauniainen',
        'sv': 'Grankulla',
        'en': 'Kauniainen'
    },
    {
        'fi': 'Vantaa',
        'sv': 'Vanda',
        'en': 'Vantaa'
    }
]

def accessibility_viewpoints(draw):
    return ','.join([
        '{}:{}'.format(key, draw(sampled_from(VIEWPOINT_STATES))) for key in VIEWPOINTS])

PROVIDER_TYPES = [
    "CONTRACT_SCHOOL",
    "OTHER_PRODUCTION_METHOD",
    "PAYMENT_COMMITMENT",
    "PURCHASED_SERVICE",
    "SELF_PRODUCED",
    "SUPPORTED_OPERATIONS",
    "UNKNOWN_PRODUCTION_METHOD"]

ORGANIZER_TYPES = [
    "ASSOCIATION",
    "FOUNDATION",
    "GOVERNMENT",
    "GOVERNMENTAL_COMPANY",
    "JOINT_MUNICIPAL_AUTHORITY",
    "MUNICIPAL_ENTERPRISE_GROUP",
    "MUNICIPALITY",
    "MUNICIPALLY_OWNED_COMPANY",
    "ORGANIZATION",
    "OTHER_REGIONAL_COOPERATION_ORGANIZATION",
    "PRIVATE_ENTERPRISE",
    "UNKNOWN"
]

@composite
def make_source(draw):
    return {'id': draw(text()), 'source': draw(text())}

def unit_maker(draw, resource_ids):
    def make_unit(uid):
        # Required fields
        result = {
            'id': uid,
            'accessibility_viewpoints': accessibility_viewpoints(draw),
            'dept_id': str(draw(sampled_from(resource_ids['department']))),
            'org_id': str(draw(sampled_from(resource_ids['organization']))),
            'ontologyword_ids': draw(permutations(resource_ids['ontologyword'])),
            'ontologytree_ids': draw(permutations(resource_ids['ontologytree'])),
            'sources': draw(lists(make_source(), min_size=0, max_size=2)),
            'provider_type': draw(sampled_from(PROVIDER_TYPES)),
            'manual_coordinates': draw(booleans()),
            # TODO: cannot test is_public=False until there is a mechanism
            # for getting non-public units from the API.
            'is_public': True,
            # TODO: map to another field
        }
        result.update(translated_field(draw, 'name', allow_missing=False))

        def add_optional_field(name, strategy):
            val = draw(one_of(none(), strategy))
            if val is not None:
                event('unit.{}: optional field given value'.format(name))
                result[name] = val
            else:
                event('unit.{}: optional field missing'.format(name))

        add_optional_field('address_city', sampled_from(MUNICIPALITIES))
        add_optional_field('accessibility_email', text())
        add_optional_field('accessibility_www', text())
        add_optional_field('address_zip', text())
        add_optional_field('data_source_url', text())
        add_optional_field('email', text())
        add_optional_field('fax', text())
        add_optional_field('phone', text())
        add_optional_field('organizer_business_id', text(max_size=11))
        add_optional_field('source', text())

        result.update(translated_field(draw, 'address_postal_full', allow_missing=True))
        result.update(translated_field(draw, 'call_charge_info', allow_missing=True))
        result.update(translated_field(draw, 'desc', allow_missing=True))
        result.update(translated_field(draw, 'picture_caption', allow_missing=True))
        return result
    return make_unit

def organization_maker(*args):
    return lambda x: {'id': str(x)}

def department_maker(draw, resource_ids):
    def make_department(did):
        return {
            'id': str(did),
            'hierarchy_level': 0,
            'org_id': str(draw(sampled_from(resource_ids['organization'])))
        }
    return make_department

def ontologyword_maker(*args):
    return lambda x: {'id': x}

def ontologytree_maker(*args):
    return lambda x: {'id': x}

make_resource = {}
for r in RESOURCES:
    make_resource[r] = locals()['{}_maker'.format(r)]

@composite
def closed_object_set(draw):
    ids = {
        'unit': int_keys(draw),
        'organization': uuid_keys(draw),
        'department': uuid_keys(draw),
        'ontologyword': int_keys(draw),
        'ontologytree': int_keys(draw)
    }
    resources = {}
    for key, identifiers in ids.items():
        resources[key] = list(map(make_resource[key](draw, ids), ids[key]))
    return resources
<|endoftext|>


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

# Build a bunch of SRPMs

import sys
import os
import glob
import subprocess
import shutil

import demjson

SRPMS_DIR = "SRPMS"
RPMS_DIR = "RPMS"
BUILD_DIR = "BUILD"
TMP_RPM_PATH = "/tmp/RPMS"

class RpmError(Exception):
    pass

def exists(path):
    return os.access(path, os.F_OK)

def doexec(args, inputtext=None):
    """Execute a subprocess, then return its return code, stdout and stderr"""
    print "Executing: %s" % args
    proc = subprocess.Popen(args, stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                            close_fds=True)
    (stdout, stderr) = proc.communicate(inputtext)
    rc = proc.returncode
    return (rc, stdout, stderr)

def run_srpmutil(specfile, srpm):
    for x in ['i686', 'i386', 'noarch']:
        (rc, stdout, stderr) = doexec(["srpmutil/srpmutil", specfile, srpm, x])
        if rc == 0:
            return (stdout, x)
    raise RpmError

def get_srpm_info(srpm):
    """ Returns a dictionary of interesting info about an SRPM:

    {
    "arch": "i686",
    "deps": [
      "ocaml",
      "ocaml-findlib",
      "ocaml-fd-send-recv-devel",
      "ocaml-uuidm-devel"
    ],
    "packages": [
      {
        "arch": "i686",
        "name": "ocaml-stdext-devel",
        "noarch": "0",
        "release": "1",
        "version": "0.9.0"
      },
      {
        "arch": "i686",
        "name": "ocaml-stdext-debuginfo",
        "noarch": "0",
        "release": "1",
        "version": "0.9.0"
      }
    ],
    "srcrpm": "SRPMS/ocaml-stdext-0.9.0-1.src.rpm"
    }

    """
    if not os.path.exists("srpmutil/srpmutil"):
        (rc, stdout, stderr) = doexec(["make", "-C", "srpmutil"])
        if rc != 0:
            print "Error while building srpmutil:"
            print "stdout\n======\n%s\n\n" % stdout
            print "stderr\n======\n%s\n\n" % stderr
            sys.exit(1)

    for x in glob.glob("SPECS/*.spec"):
        os.unlink(x)
    (rc, stdout, stderr) = doexec(["rpm", "-i", srpm])
    myspecfile = glob.glob("./SPECS/*.spec")[0]
    try:
        (specfile, arch) = run_srpmutil(myspecfile, srpm)
        j = demjson.decode(specfile)
        (rc, stdout, stderr) = doexec(["rpm", "-qp", srpm, "-R"])
        lines = stdout.split('\n')
        alldeps = map(lambda x: x.split(' ')[0], lines)
        realdeps = filter(
            lambda x: len(x) and x != "rpmlib(CompressedFileNames)",
            alldeps)
        j['deps'] = realdeps
        j['arch'] = arch
        return j
    except:
        return {'broken': True, 'srcrpm': srpm}

def extract_target(srpm_infos, srpm_filename):
    """
    Given a list of srpm_info and an srpm filename, return the target arch
    """
    for srpm_info in srpm_infos:
        if srpm_info["srcrpm"] == srpm_filename:
            return srpm_info["arch"]

def get_package_to_srpm_map(srpm_info):
    m = {}
    for srpm in srpm_info:
        for package in srpm['packages']:
            m[package['name']] = srpm['srcrpm']
    return m

def get_deps(srpm_info):
    p_to_s_map = get_package_to_srpm_map(srpm_info)
    deps = {}
    for srpm in srpm_info:
        deps[srpm['srcrpm']] = set()
        for dep in srpm['deps']:
            if dep in p_to_s_map:
                deps[srpm['srcrpm']].add(p_to_s_map[dep])
    return deps

def toposort2(data):
    # Ignore self dependencies.
    for k, v in data.items():
        v.discard(k)
    # Find all items that don't depend on anything.
    extra_items_in_deps = reduce(set.union,
                                 data.itervalues()) - set(data.iterkeys())
    # Add empty dependences where needed
    extra = {}
    for item in extra_items_in_deps:
        extra[item] = set()
    data.update(extra)
    result = []
    while True:
        ordered = set(item for item, dep in data.iteritems() if not dep)
        if not ordered:
            break
        result.append(ordered)
        newdata = {}
        for item, dep in data.iteritems():
            if item not in ordered:
                newdata[item] = (dep - ordered)
        data = newdata
    assert not data, ("Cyclic dependencies exist among these items:\n%s" %
                      '\n'.join(repr(x) for x in data.iteritems()))
    return result

def write_rpmmacros():
    f = open('.rpmmacros', 'w')
    f.write('%%_topdir %s\n' % os.getcwd())
    f.close()

if __name__ == "__main__":
    packages = glob.glob(os.path.join(SRPMS_DIR, '*.src.rpm'))
    write_rpmmacros()
    srpm_infos = map(get_srpm_info, packages)
    deps = get_deps(srpm_infos)
    order = toposort2(deps)

    for path in [TMP_RPM_PATH, BUILD_DIR, SRPMS_DIR, RPMS_DIR]:
        if not os.path.exists(path):
            os.makedirs(path)

    for batch in order:
        for srpm in batch:
            target = extract_target(srpm_infos, srpm)
            print "Building %s" % srpm
            cmd = ["rpmbuild", "--rebuild", "-v", "%s" % srpm,
                   "--target", target, "--define", "_rpmdir %s" % TMP_RPM_PATH]
            (rc, stdout, stderr) = doexec(cmd)
            if rc == 0:
                print "Success"
                rpms = glob.glob(os.path.join(TMP_RPM_PATH, target, "*"))
                (rc, stdout, stderr) = doexec(["rpm", "-i"] + rpms)
                if rc != 0:
                    print "Ignoring failure installing rpm batch: %s" % rpms
                    print stderr
                dst_dir = os.path.join(RPMS_DIR, target)
                if not os.path.exists(dst_dir):
                    os.makedirs(dst_dir)
                for bin_rpm in rpms:
                    shutil.copy(bin_rpm, dst_dir)
                    os.unlink(bin_rpm)
            else:
                print "Failed to build rpm from srpm: %s" % srpm
                print "\nstdout\n======\n%s" % stdout
                print "\nstderr\n======\n%s" % stderr
                sys.exit(1)
<commit_msg>Print more clearly what python farms out to subprocesses<commit_after>#!/usr/bin/env python

# Build a bunch of SRPMs

import sys
import os
import glob
import subprocess
import shutil

import demjson

SRPMS_DIR = "SRPMS"
RPMS_DIR = "RPMS"
BUILD_DIR = "BUILD"
TMP_RPM_PATH = "/tmp/RPMS"

class RpmError(Exception):
    pass

def exists(path):
    return os.access(path, os.F_OK)

def doexec(args, inputtext=None):
    """Execute a subprocess, then return its return code, stdout and stderr"""
    print "Executing: %s" % " ".join(args)
    proc = subprocess.Popen(args, stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                            close_fds=True)
    (stdout, stderr) = proc.communicate(inputtext)
    rc = proc.returncode
    return (rc, stdout, stderr)

def run_srpmutil(specfile, srpm):
    for x in ['i686', 'i386', 'noarch']:
        (rc, stdout, stderr) = doexec(["srpmutil/srpmutil", specfile, srpm, x])
        if rc == 0:
            return (stdout, x)
    raise RpmError

def get_srpm_info(srpm):
    """ Returns a dictionary of interesting info about an SRPM:

    {
    "arch": "i686",
    "deps": [
      "ocaml",
      "ocaml-findlib",
      "ocaml-fd-send-recv-devel",
      "ocaml-uuidm-devel"
    ],
    "packages": [
      {
        "arch": "i686",
        "name": "ocaml-stdext-devel",
        "noarch": "0",
        "release": "1",
        "version": "0.9.0"
      },
      {
        "arch": "i686",
        "name": "ocaml-stdext-debuginfo",
        "noarch": "0",
        "release": "1",
        "version": "0.9.0"
      }
    ],
    "srcrpm": "SRPMS/ocaml-stdext-0.9.0-1.src.rpm"
    }

    """
    if not os.path.exists("srpmutil/srpmutil"):
        (rc, stdout, stderr) = doexec(["make", "-C", "srpmutil"])
        if rc != 0:
            print "Error while building srpmutil:"
            print "stdout\n======\n%s\n\n" % stdout
            print "stderr\n======\n%s\n\n" % stderr
            sys.exit(1)

    for x in glob.glob("SPECS/*.spec"):
        os.unlink(x)
    (rc, stdout, stderr) = doexec(["rpm", "-i", srpm])
    myspecfile = glob.glob("./SPECS/*.spec")[0]
    try:
        (specfile, arch) = run_srpmutil(myspecfile, srpm)
        j = demjson.decode(specfile)
        (rc, stdout, stderr) = doexec(["rpm", "-qp", srpm, "-R"])
        lines = stdout.split('\n')
        alldeps = map(lambda x: x.split(' ')[0], lines)
        realdeps = filter(
            lambda x: len(x) and x != "rpmlib(CompressedFileNames)",
            alldeps)
        j['deps'] = realdeps
        j['arch'] = arch
        return j
    except:
        return {'broken': True, 'srcrpm': srpm}

def extract_target(srpm_infos, srpm_filename):
    """
    Given a list of srpm_info and an srpm filename, return the target arch
    """
    for srpm_info in srpm_infos:
        if srpm_info["srcrpm"] == srpm_filename:
            return srpm_info["arch"]

def get_package_to_srpm_map(srpm_info):
    m = {}
    for srpm in srpm_info:
        for package in srpm['packages']:
            m[package['name']] = srpm['srcrpm']
    return m

def get_deps(srpm_info):
    p_to_s_map = get_package_to_srpm_map(srpm_info)
    deps = {}
    for srpm in srpm_info:
        deps[srpm['srcrpm']] = set()
        for dep in srpm['deps']:
            if dep in p_to_s_map:
                deps[srpm['srcrpm']].add(p_to_s_map[dep])
    return deps

def toposort2(data):
    # Ignore self dependencies.
    for k, v in data.items():
        v.discard(k)
    # Find all items that don't depend on anything.
    extra_items_in_deps = reduce(set.union,
                                 data.itervalues()) - set(data.iterkeys())
    # Add empty dependences where needed
    extra = {}
    for item in extra_items_in_deps:
        extra[item] = set()
    data.update(extra)
    result = []
    while True:
        ordered = set(item for item, dep in data.iteritems() if not dep)
        if not ordered:
            break
        result.append(ordered)
        newdata = {}
        for item, dep in data.iteritems():
            if item not in ordered:
                newdata[item] = (dep - ordered)
        data = newdata
    assert not data, ("Cyclic dependencies exist among these items:\n%s" %
                      '\n'.join(repr(x) for x in data.iteritems()))
    return result

def write_rpmmacros():
    f = open('.rpmmacros', 'w')
    f.write('%%_topdir %s\n' % os.getcwd())
    f.close()

if __name__ == "__main__":
    packages = glob.glob(os.path.join(SRPMS_DIR, '*.src.rpm'))
    write_rpmmacros()
    srpm_infos = map(get_srpm_info, packages)
    deps = get_deps(srpm_infos)
    order = toposort2(deps)

    for path in [TMP_RPM_PATH, BUILD_DIR, SRPMS_DIR, RPMS_DIR]:
        if not os.path.exists(path):
            os.makedirs(path)

    for batch in order:
        for srpm in batch:
            target = extract_target(srpm_infos, srpm)
            print "Building %s" % srpm
            cmd = ["rpmbuild", "--rebuild", "-v", "%s" % srpm,
                   "--target", target, "--define", "_rpmdir %s" % TMP_RPM_PATH]
            (rc, stdout, stderr) = doexec(cmd)
            if rc == 0:
                print "Success"
                rpms = glob.glob(os.path.join(TMP_RPM_PATH, target, "*"))
                (rc, stdout, stderr) = doexec(["rpm", "-i"] + rpms)
                if rc != 0:
                    print "Ignoring failure installing rpm batch: %s" % rpms
                    print stderr
                dst_dir = os.path.join(RPMS_DIR, target)
                if not os.path.exists(dst_dir):
                    os.makedirs(dst_dir)
                for bin_rpm in rpms:
                    shutil.copy(bin_rpm, dst_dir)
                    os.unlink(bin_rpm)
            else:
                print "Failed to build rpm from srpm: %s" % srpm
                print "\nstdout\n======\n%s" % stdout
                print "\nstderr\n======\n%s" % stderr
                sys.exit(1)
<|endoftext|>


<commit_before># Under MIT License, see LICENSE.txt
import math

from RULEngine.Debug.ui_debug_command import UIDebugCommand
from RULEngine.Util.Pose import Pose, Position
from ai.STA.Strategy.HumanControl import HumanControl
from ai.executors.executor import Executor
from ai.states.world_state import WorldState

class DebugExecutor(Executor):

    def __init__(self, p_world_state: WorldState):
        """initialise"""
        super().__init__(p_world_state)
        self.debug_in = []

    def exec(self):
        """ouvre les packets, parse et applique les packets detinés à l'IA"""
        self._execute_incoming_debug_commands()

    def set_reference(self, debug_ref) -> None:
        """ rentre la référence donné par le RULEngine"""
        self.debug_in = debug_ref

    def _execute_incoming_debug_commands(self) -> None:
        """
        change les packets en commandes correspondant, et les applique.

        :return: None
        """
        for command in self.debug_in:
            self._parse_command(UIDebugCommand(command))

    def _parse_command(self, cmd: UIDebugCommand)->None:
        """
        Choisit la méthode puor appliquer la commande de l'UI

        :param cmd: (UIDebugCommand) la commande de l'UIDebug
        :return: None
        """
        if cmd.is_strategy_cmd():
            self._parse_strategy(cmd)

        elif cmd.is_tactic_cmd():
            self._parse_tactic(cmd)

        elif cmd.is_auto_play_cmd():
            self.ws.play_state.autonomous_flag = cmd.data['status']

        else:
            pass

    def _parse_strategy(self, cmd: UIDebugCommand)->None:
        """
        Remplace la stratégie courante par celle demander dans la commande de l'UI

        :param cmd: (UIDebugCommand) la commande envoyée de l'UI
        :return: None
        """
        # TODO revise this function please, thank you!
        # TODO change this once UI-Debug send correct strategy names!

        strategy_key = cmd.data['strategy']

        if strategy_key == 'pStop':
            self.ws.play_state.set_strategy(self.ws.play_state.get_new_strategy("DoNothing")(self.ws.game_state))
        else:
            self.ws.play_state.set_strategy(self.ws.play_state.get_new_strategy(strategy_key)(self.ws.game_state))

    def _parse_tactic(self, cmd: UIDebugCommand)->None:
        """
        Remplace la tactique courante d'un robot par celle demander dans la commande de l'UI

        :param cmd: (UIDebugCommand) la commande envoyée de l'UI
        :return: None
        """
        assert isinstance(cmd, UIDebugCommand), "debug_executor->_parse_tactic is not the correct object!"
        # TODO make implementation for other tactic packets!
        # FIXME this pid thingy is getting out of control
        # find the player id in question
        # get the player if applicable!
        try:
            this_player = self.ws.game_state.get_player(cmd.data['id'])
        except KeyError as id:
            print("Invalid player id: {}".format(cmd.data['id']))
            return
        player_id = this_player.id
        tactic_name = cmd.data['tactic']
        # TODO ui must send better packets back with the args.
        target = cmd.data['target']
        target = Pose(Position(target[0], target[1]), this_player.pose.orientation)  # 3.92 - 2 * math.pi)
        args = cmd.data.get('args', "")
        tactic = self.ws.play_state.get_new_tactic('Idle')(self.ws.game_state,
                                                           this_player,
                                                           target,
                                                           args)
        try:
            tactic = self.ws.play_state.get_new_tactic(tactic_name)(self.ws.game_state, this_player, target, args)
        except Exception as e:
            print(e)
            print("La tactique n'a pas été appliquée par "
                  "cause de mauvais arguments.")
            raise e

        if isinstance(self.ws.play_state.current_strategy, HumanControl):
            hc = self.ws.play_state.current_strategy
            hc.assign_tactic(tactic, player_id)
        else:
            hc = HumanControl(self.ws.game_state)
            hc.assign_tactic(tactic, player_id)
            self.ws.play_state.set_strategy(hc)
<commit_msg>Fix of stop button in autoplay<commit_after># Under MIT License, see LICENSE.txt
import math

from RULEngine.Debug.ui_debug_command import UIDebugCommand
from RULEngine.Util.Pose import Pose, Position
from ai.STA.Strategy.HumanControl import HumanControl
from ai.executors.executor import Executor
from ai.states.world_state import WorldState

class DebugExecutor(Executor):

    def __init__(self, p_world_state: WorldState):
        """initialise"""
        super().__init__(p_world_state)
        self.debug_in = []

    def exec(self):
        """ouvre les packets, parse et applique les packets detinés à l'IA"""
        self._execute_incoming_debug_commands()

    def set_reference(self, debug_ref) -> None:
        """ rentre la référence donné par le RULEngine"""
        self.debug_in = debug_ref

    def _execute_incoming_debug_commands(self) -> None:
        """
        change les packets en commandes correspondant, et les applique.

        :return: None
        """
        for command in self.debug_in:
            self._parse_command(UIDebugCommand(command))

    def _parse_command(self, cmd: UIDebugCommand)->None:
        """
        Choisit la méthode puor appliquer la commande de l'UI

        :param cmd: (UIDebugCommand) la commande de l'UIDebug
        :return: None
        """
        if cmd.is_strategy_cmd():
            self._parse_strategy(cmd)

        elif cmd.is_tactic_cmd():
            self._parse_tactic(cmd)

        elif cmd.is_auto_play_cmd():
            self.ws.play_state.autonomous_flag = cmd.data['status']
            if not self.ws.play_state.autonomous_flag:
                self.ws.play_state.set_strategy(self.ws.play_state.get_new_strategy("DoNothing")(self.ws.game_state))

        else:
            pass

    def _parse_strategy(self, cmd: UIDebugCommand)->None:
        """
        Remplace la stratégie courante par celle demander dans la commande de l'UI

        :param cmd: (UIDebugCommand) la commande envoyée de l'UI
        :return: None
        """
        # TODO revise this function please, thank you!
        # TODO change this once UI-Debug send correct strategy names!

        strategy_key = cmd.data['strategy']

        if strategy_key == 'pStop':
            self.ws.play_state.set_strategy(self.ws.play_state.get_new_strategy("DoNothing")(self.ws.game_state))
        else:
            self.ws.play_state.set_strategy(self.ws.play_state.get_new_strategy(strategy_key)(self.ws.game_state))

    def _parse_tactic(self, cmd: UIDebugCommand)->None:
        """
        Remplace la tactique courante d'un robot par celle demander dans la commande de l'UI

        :param cmd: (UIDebugCommand) la commande envoyée de l'UI
        :return: None
        """
        assert isinstance(cmd, UIDebugCommand), "debug_executor->_parse_tactic is not the correct object!"
        # TODO make implementation for other tactic packets!
        # FIXME this pid thingy is getting out of control
        # find the player id in question
        # get the player if applicable!
        try:
            this_player = self.ws.game_state.get_player(cmd.data['id'])
        except KeyError as id:
            print("Invalid player id: {}".format(cmd.data['id']))
            return
        player_id = this_player.id
        tactic_name = cmd.data['tactic']
        # TODO ui must send better packets back with the args.
        target = cmd.data['target']
        target = Pose(Position(target[0], target[1]), this_player.pose.orientation)  # 3.92 - 2 * math.pi)
        args = cmd.data.get('args', "")
        tactic = self.ws.play_state.get_new_tactic('Idle')(self.ws.game_state,
                                                           this_player,
                                                           target,
                                                           args)
        try:
            tactic = self.ws.play_state.get_new_tactic(tactic_name)(self.ws.game_state, this_player, target, args)
        except Exception as e:
            print(e)
            print("La tactique n'a pas été appliquée par "
                  "cause de mauvais arguments.")
            raise e

        if isinstance(self.ws.play_state.current_strategy, HumanControl):
            hc = self.ws.play_state.current_strategy
            hc.assign_tactic(tactic, player_id)
        else:
            hc = HumanControl(self.ws.game_state)
            hc.assign_tactic(tactic, player_id)
            self.ws.play_state.set_strategy(hc)
<|endoftext|>


<commit_before># Module:   utils
# Date:     13th September 2007
# Author:   James Mills, prologic at shortcircuit dot net dot au

"""Utilities

This module implements utility functions.
"""

import re
import zlib
import time
import struct
from cgi import escape
try:
    from urllib.parse import urljoin as _urljoin
except ImportError:
    from urlparse import urljoin as _urljoin

try:
    from io import StringIO
except ImportError:
    from io import StringIO

try:
    from urllib.parse import parse_qs
except ImportError:
    from cgi import parse_qs

from .constants import HTTP_STATUS_CODES

quoted_slash = re.compile("(?i)%2F")
image_map_pattern = re.compile("[0-9]+,[0-9]+")

def parseQueryString(query_string, keep_blank_values=True):
    """parseQueryString(query_string) -> dict

    Build a params dictionary from a query_string.
    If keep_blank_values is True (the default), keep
    values that are blank.
    """

    if image_map_pattern.match(query_string):
        # Server-side image map. Map the coords to "x" and "y"
        # (like CGI::Request does).
        pm = query_string.split(",")
        return {"x": int(pm[0]), "y": int(pm[1])}
    else:
        pm = parse_qs(query_string, keep_blank_values)
        return dict((k, v[0]) for k, v in pm.items() if v)

def dictform(form):
    d = {}
    for key in list(form.keys()):
        values = form[key]
        if isinstance(values, list):
            d[key] = []
            for item in values:
                if item.filename is not None:
                    value = item # It's a file upload
                else:
                    value = item.value # It's a regular field
                d[key].append(value)
        else:
            if values.filename is not None:
                value = values # It's a file upload
            else:
                value = values.value # It's a regular field
            d[key] = value
    return d

def compress(body, compress_level):
    """Compress 'body' at the given compress_level."""

    # Header
    yield b"\037\213\010\0" \
            + struct.pack("<L", int(time.time())) \
            + b"\002\377"

    size = 0
    crc = zlib.crc32(b"")

    zobj = zlib.compressobj(
        compress_level,
        zlib.DEFLATED,
        -zlib.MAX_WBITS,
        zlib.DEF_MEM_LEVEL,
        0,
    )

    for chunk in body:
        if isinstance(chunk, str):
            chunk = chunk.encode("utf-8")

        size += len(chunk)
        crc = zlib.crc32(chunk, crc)
        yield zobj.compress(chunk)

    yield zobj.flush() \
            + struct.pack("<l", crc) \
            + struct.pack("<L", size & 0xFFFFFFFF)

def url(request, path="", qs="", script_name=None, base=None, relative=None):
    """Create an absolute URL for the given path.

    If 'path' starts with a slash ('/'), this will return
       - (base + script_name + path + qs).
    If it does not start with a slash, this returns
       - (base + script_name [+ request.path] + path + qs).

    If script_name is None, request will be used
    to find a script_name, if available.

    If base is None, request.base will be used (if available).

    Finally, note that this function can be used to obtain an absolute URL
    for the current request path (minus the querystring) by passing no args.
    If you call url(qs=request.qs), you should get the
    original browser URL (assuming no internal redirections).

    If relative is False the output will be an absolute URL
    (including the scheme, host, vhost, and script_name).
    If True, the output will instead be a URL that is relative to the
    current request path, perhaps including '..' atoms. If relative is
    the string 'server', the output will instead be a URL that is
    relative to the server root; i.e., it will start with a slash.
    """
    if qs:
        qs = '?' + qs

    if not path.startswith("/"):
        # Append/remove trailing slash from request.path as needed
        # (this is to support mistyped URL's without redirecting;
        # if you want to redirect, use tools.trailing_slash).
        pi = request.path
        if request.index is True:
            if not pi.endswith('/'):
                pi = pi + '/'
        elif request.index is False:
            if pi.endswith('/') and pi != '/':
                pi = pi[:-1]

        if path == "":
            path = pi
        else:
            path = _urljoin(pi, path)

    if script_name is None:
        script_name = request.script_name
    if base is None:
        base = request.base

    newurl = base + script_name + path + qs

    if './' in newurl:
        # Normalize the URL by removing ./ and ../
        atoms = []
        for atom in newurl.split('/'):
            if atom == '.':
                pass
            elif atom == '..':
                atoms.pop()
            else:
                atoms.append(atom)
        newurl = '/'.join(atoms)

    # At this point, we should have a fully-qualified absolute URL.

    # See http://www.ietf.org/rfc/rfc2396.txt
    if relative == 'server':
        # "A relative reference beginning with a single slash character is
        # termed an absolute-path reference, as defined by <abs_path>..."
        # This is also sometimes called "server-relative".
        newurl = '/' + '/'.join(newurl.split('/', 3)[3:])
    elif relative:
        # "A relative reference that does not begin with a scheme name
        # or a slash character is termed a relative-path reference."
        old = url().split('/')[:-1]
        new = newurl.split('/')
        while old and new:
            a, b = old[0], new[0]
            if a != b:
                break
            old.pop(0)
            new.pop(0)
        new = (['..'] * len(old)) + new
        newurl = '/'.join(new)

    return newurl

def get_ranges(headervalue, content_length):
    """Return a list of (start, stop) indices from a Range header, or None.

    Each (start, stop) tuple will be composed of two ints, which are suitable
    for use in a slicing operation. That is, the header "Range: bytes=3-6",
    if applied against a Python string, is requesting resource[3:7]. This
    function will return the list [(3, 7)].

    If this function returns an empty list, you should return HTTP 416.
    """

    if not headervalue:
        return None

    result = []
    bytesunit, byteranges = headervalue.split("=", 1)
    for brange in byteranges.split(","):
        start, stop = [x.strip() for x in brange.split("-", 1)]
        if start:
            if not stop:
                stop = content_length - 1
            start, stop = list(map(int, (start, stop)))
            if start >= content_length:
                # From rfc 2616 sec 14.16:
                # "If the server receives a request (other than one
                # including an If-Range request-header field) with an
                # unsatisfiable Range request-header field (that is,
                # all of whose byte-range-spec values have a first-byte-pos
                # value greater than the current length of the selected
                # resource), it SHOULD return a response code of 416
                # (Requested range not satisfiable)."
                continue
            if stop < start:
                # From rfc 2616 sec 14.16:
                # "If the server ignores a byte-range-spec because it
                # is syntactically invalid, the server SHOULD treat
                # the request as if the invalid Range header field
                # did not exist. (Normally, this means return a 200
                # response containing the full entity)."
                return None
            result.append((start, stop + 1))
        else:
            if not stop:
                # See rfc quote above.
                return None
            # Negative subscript (last N bytes)
            result.append((content_length - int(stop), content_length))

    return result
<commit_msg>test for isinstance(bytes) instead of isinstance(str) before encoding<commit_after># Module:   utils
# Date:     13th September 2007
# Author:   James Mills, prologic at shortcircuit dot net dot au

"""Utilities

This module implements utility functions.
"""

import re
import zlib
import time
import struct
from cgi import escape
try:
    from urllib.parse import urljoin as _urljoin
except ImportError:
    from urlparse import urljoin as _urljoin

try:
    from io import StringIO
except ImportError:
    from io import StringIO

try:
    from urllib.parse import parse_qs
except ImportError:
    from cgi import parse_qs

from .constants import HTTP_STATUS_CODES

quoted_slash = re.compile("(?i)%2F")
image_map_pattern = re.compile("[0-9]+,[0-9]+")

def parseQueryString(query_string, keep_blank_values=True):
    """parseQueryString(query_string) -> dict

    Build a params dictionary from a query_string.
    If keep_blank_values is True (the default), keep
    values that are blank.
    """

    if image_map_pattern.match(query_string):
        # Server-side image map. Map the coords to "x" and "y"
        # (like CGI::Request does).
        pm = query_string.split(",")
        return {"x": int(pm[0]), "y": int(pm[1])}
    else:
        pm = parse_qs(query_string, keep_blank_values)
        return dict((k, v[0]) for k, v in pm.items() if v)

def dictform(form):
    d = {}
    for key in list(form.keys()):
        values = form[key]
        if isinstance(values, list):
            d[key] = []
            for item in values:
                if item.filename is not None:
                    value = item # It's a file upload
                else:
                    value = item.value # It's a regular field
                d[key].append(value)
        else:
            if values.filename is not None:
                value = values # It's a file upload
            else:
                value = values.value # It's a regular field
            d[key] = value
    return d

def compress(body, compress_level):
    """Compress 'body' at the given compress_level."""

    # Header
    yield b"\037\213\010\0" \
            + struct.pack("<L", int(time.time())) \
            + b"\002\377"

    size = 0
    crc = zlib.crc32(b"")

    zobj = zlib.compressobj(
        compress_level,
        zlib.DEFLATED,
        -zlib.MAX_WBITS,
        zlib.DEF_MEM_LEVEL,
        0,
    )

    for chunk in body:
        if not isinstance(chunk, bytes):
            chunk = chunk.encode("utf-8")

        size += len(chunk)
        crc = zlib.crc32(chunk, crc)
        yield zobj.compress(chunk)

    yield zobj.flush() \
            + struct.pack("<l", crc) \
            + struct.pack("<L", size & 0xFFFFFFFF)

def url(request, path="", qs="", script_name=None, base=None, relative=None):
    """Create an absolute URL for the given path.

    If 'path' starts with a slash ('/'), this will return
       - (base + script_name + path + qs).
    If it does not start with a slash, this returns
       - (base + script_name [+ request.path] + path + qs).

    If script_name is None, request will be used
    to find a script_name, if available.

    If base is None, request.base will be used (if available).

    Finally, note that this function can be used to obtain an absolute URL
    for the current request path (minus the querystring) by passing no args.
    If you call url(qs=request.qs), you should get the
    original browser URL (assuming no internal redirections).

    If relative is False the output will be an absolute URL
    (including the scheme, host, vhost, and script_name).
    If True, the output will instead be a URL that is relative to the
    current request path, perhaps including '..' atoms. If relative is
    the string 'server', the output will instead be a URL that is
    relative to the server root; i.e., it will start with a slash.
    """
    if qs:
        qs = '?' + qs

    if not path.startswith("/"):
        # Append/remove trailing slash from request.path as needed
        # (this is to support mistyped URL's without redirecting;
        # if you want to redirect, use tools.trailing_slash).
        pi = request.path
        if request.index is True:
            if not pi.endswith('/'):
                pi = pi + '/'
        elif request.index is False:
            if pi.endswith('/') and pi != '/':
                pi = pi[:-1]

        if path == "":
            path = pi
        else:
            path = _urljoin(pi, path)

    if script_name is None:
        script_name = request.script_name
    if base is None:
        base = request.base

    newurl = base + script_name + path + qs

    if './' in newurl:
        # Normalize the URL by removing ./ and ../
        atoms = []
        for atom in newurl.split('/'):
            if atom == '.':
                pass
            elif atom == '..':
                atoms.pop()
            else:
                atoms.append(atom)
        newurl = '/'.join(atoms)

    # At this point, we should have a fully-qualified absolute URL.

    # See http://www.ietf.org/rfc/rfc2396.txt
    if relative == 'server':
        # "A relative reference beginning with a single slash character is
        # termed an absolute-path reference, as defined by <abs_path>..."
        # This is also sometimes called "server-relative".
        newurl = '/' + '/'.join(newurl.split('/', 3)[3:])
    elif relative:
        # "A relative reference that does not begin with a scheme name
        # or a slash character is termed a relative-path reference."
        old = url().split('/')[:-1]
        new = newurl.split('/')
        while old and new:
            a, b = old[0], new[0]
            if a != b:
                break
            old.pop(0)
            new.pop(0)
        new = (['..'] * len(old)) + new
        newurl = '/'.join(new)

    return newurl

def get_ranges(headervalue, content_length):
    """Return a list of (start, stop) indices from a Range header, or None.

    Each (start, stop) tuple will be composed of two ints, which are suitable
    for use in a slicing operation. That is, the header "Range: bytes=3-6",
    if applied against a Python string, is requesting resource[3:7]. This
    function will return the list [(3, 7)].

    If this function returns an empty list, you should return HTTP 416.
    """

    if not headervalue:
        return None

    result = []
    bytesunit, byteranges = headervalue.split("=", 1)
    for brange in byteranges.split(","):
        start, stop = [x.strip() for x in brange.split("-", 1)]
        if start:
            if not stop:
                stop = content_length - 1
            start, stop = list(map(int, (start, stop)))
            if start >= content_length:
                # From rfc 2616 sec 14.16:
                # "If the server receives a request (other than one
                # including an If-Range request-header field) with an
                # unsatisfiable Range request-header field (that is,
                # all of whose byte-range-spec values have a first-byte-pos
                # value greater than the current length of the selected
                # resource), it SHOULD return a response code of 416
                # (Requested range not satisfiable)."
                continue
            if stop < start:
                # From rfc 2616 sec 14.16:
                # "If the server ignores a byte-range-spec because it
                # is syntactically invalid, the server SHOULD treat
                # the request as if the invalid Range header field
                # did not exist. (Normally, this means return a 200
                # response containing the full entity)."
                return None
            result.append((start, stop + 1))
        else:
            if not stop:
                # See rfc quote above.
                return None
            # Negative subscript (last N bytes)
            result.append((content_length - int(stop), content_length))

    return result
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# Copyright (C) 2011 Alexander Shorin
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
#

import couchdb
import getopt
import logging
import os
import platform
import socket
import sys
from getpass import getpass
from couchdb.http import extract_credentials, HTTPError
from couchdb_auditor import auditor

__version__ = '0.1'

_VERSION = 'couchdb-auditor %s' % __version__

_HELP = """Usage: %(name)s [OPTIONS] URL

Arguments:

  URL              CouchDB server URL in next form:
                   http[s]://[user[:password]@]host[:port]
                   Note that setting password in URL will make it visible in
                   shell history.

Options:

  --version        Display version information and exit.
  -h, --help       Display a this help message and exit.
  -u, --user=      Set CouchDB user that would inspect server
                   User could be also defined in URL.
                   Password will be requested.
  -d, --database   Audit database by specified URL, not server.

""" % dict(name=os.path.basename(sys.argv[0]))

_NO_URL = """URL argument must be specified. See --help for more information.
"""

_USER_DUPLICATE = """Multiple users defined, couldn't decide which one to use:
%s or %s
"""

SYSTEM = platform.system().lower()

class ColoredFormatter(logging.Formatter):

    def __init__(self, fmt=None, datefmt=None):
        self.colors = {
            'CRITICAL': '\x1b[1;31m',
            'ERROR': '\x1b[1;31m',
            'INFO': '\x1b[1;32m',
            'WARN': '\x1b[1;33m',
            'WARNING': '\x1b[1;33m',
            'DEBUG': '\x1b[1;37m',
        }
        self.reset = '\033[0m'
        logging.Formatter.__init__(self, fmt, datefmt)

    def format(self, record):
        if SYSTEM == 'linux':
            record.reset = self.reset
        else:
            record.reset = ''
        record.funcName = '[%s]' % record.funcName
        levelname = record.levelname
        if SYSTEM == 'linux' and levelname in self.colors:
            color = self.colors[levelname]
            levelname = ''.join((color, levelname[0] * 2, self.reset))
            record.levelname = '[%s]' % levelname
        else:
            record.levelname = '[%s]' % (levelname[0] * 2)
        return logging.Formatter.format(self, record)

def get_logger(name, level=logging.DEBUG):
    fmt = '%(reset)s%(levelname)s  %(funcName)-24s  %(message)s'
    instance = logging.Logger('couchdb.audit.%s' % name, level)
    handler = logging.StreamHandler(sys.stdout)
    handler.setFormatter(ColoredFormatter(fmt))
    instance.addHandler(handler)
    instance.propagate = False
    return instance

def run(url, credentials, target='server'):
    root = logging.Logger('couchdb.audit')
    handler = logging.StreamHandler(sys.stdout)
    root.addHandler(handler)

    def audit_server(url, credentials):
        server = couchdb.Server(url)
        server.resource.credentials = credentials
        root.info(' * Server: %s', server.resource.url)

        try:
            server.resource.head()
        except (HTTPError, socket.error), err:
            sys.stdout.write('%s: %s\n' % (err.__class__.__name__, err))
            sys.stdout.flush()
            sys.exit(1)

        cache = {}
        log = get_logger('couchdb.audit.server')
        auditor.audit_server(server, log, cache)

        try:
            dblist = list(server)
        except HTTPError, err:
            sys.stdout.write('Unable to get database list.\n')
            sys.stdout.write('%s: %s\n' % (err.__class__.__name__, err.args[0][1]))
            sys.stdout.flush()
            sys.exit(1)

        for dbname in sorted(dblist):
            url = server.resource(dbname).url
            audit_database(url, credentials, name=dbname, cache=cache)

    def audit_database(url, credentials, name=None, cache=None):
        if cache is None:
            cache = {}
        log = get_logger('couchdb.audit.database')
        db = couchdb.Database(url, name=name)
        db.resource.credentials = credentials
        root.info('\n * Database: %s', db.resource.url)

        try:
            db.info()['db_name']
        except socket.error, err:
            log.error('%s: %s', err.__class__.__name__, err)
            return
        except couchdb.HTTPError, err:
            log.error('%s: %s', err.__class__.__name__, err.args[0][1])
            return
        except KeyError, err:
            log.critical('%s: %s; Possible not database?',
                         err.__class__.__name__, err)
            return

        auditor.audit_database(db, log, cache)

        try:
            rows = db.view('_all_docs', startkey='_design/',  endkey='_design0')
            rows = list(rows)
        except HTTPError, err:
            log.critical('Unable to get design documents list: %s',
                         err.args[0][1])
            return

        for row in rows:
            root.info('\n * DDoc: %s', db.resource(*row.id.split('/')).url)
            log = get_logger('couchdb.audit.ddoc')
            ddoc = db[row.id]
            auditor.audit_ddoc(ddoc, log, cache)

    if target == 'server':
        audit_server(url, credentials)
    elif target == 'database':
        audit_database(url, credentials)

    return 0

def main():
    try:
        options, arguments = getopt.gnu_getopt(
            sys.argv[1:], 'hvu:d',
            ['version', 'help', 'user=', 'database']
        )
    except getopt.GetoptError, err:
        sys.stdout.write(('%s\n\n' % err).capitalize())
        sys.stdout.write(_HELP)
        sys.stdout.flush()
        sys.exit(1)
    message = None

    if not arguments:
        sys.stdout.write(_NO_URL)
        sys.stdout.flush()
        sys.exit(1)

    target = 'server'
    url = arguments[0]
    if not url.startswith('http://'):
        url = 'http://' + url
    _, credentials = extract_credentials(url)
    if credentials:
        credentials = list(credentials)

    for option, value in options:
        if option in ['--version']:
            message = _VERSION
        elif option in ['-h', '--help']:
            message = _HELP
        elif option in ['-u', '--user']:
            if credentials and credentials[0] != value:
                message = _USER_DUPLICATE % (credentials[0], value)
            elif not credentials:
                credentials = [value]
        elif option in ['-d', '--database']:
            target = 'database'

    if message:
        sys.stdout.write(message)
        sys.stdout.flush()
        sys.exit(0)

    if credentials and len(credentials) == 1:
        credentials.append(getpass('Enter password for %s: ' % credentials[0]))
        credentials = tuple(credentials)

    sys.exit(run(url, credentials, target))

if __name__ == '__main__':
    main()
<commit_msg>Fix version and help information showing. Just never had test it.<commit_after># -*- coding: utf-8 -*-
#
# Copyright (C) 2011 Alexander Shorin
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
#

import couchdb
import getopt
import logging
import os
import platform
import socket
import sys
from getpass import getpass
from couchdb.http import extract_credentials, HTTPError
from couchdb_auditor import auditor

__version__ = '0.1'

_VERSION = 'couchdb-auditor %s\n' % __version__

_HELP = """Usage: %(name)s [OPTIONS] URL

Arguments:

  URL              CouchDB server URL in next form:
                   http[s]://[user[:password]@]host[:port]
                   Note that setting password in URL will make it visible in
                   shell history.

Options:

  --version        Display version information and exit.
  -h, --help       Display a this help message and exit.
  -u, --user=      Set CouchDB user that would inspect server
                   User could be also defined in URL.
                   Password will be requested.
  -d, --database   Audit database by specified URL, not server.

""" % dict(name=os.path.basename(sys.argv[0]))

_NO_URL = """URL argument must be specified.
""" + _HELP

_USER_DUPLICATE = """Multiple users defined, couldn't decide which one to use:
%s or %s
"""

SYSTEM = platform.system().lower()

class ColoredFormatter(logging.Formatter):

    def __init__(self, fmt=None, datefmt=None):
        self.colors = {
            'CRITICAL': '\x1b[1;31m',
            'ERROR': '\x1b[1;31m',
            'INFO': '\x1b[1;32m',
            'WARN': '\x1b[1;33m',
            'WARNING': '\x1b[1;33m',
            'DEBUG': '\x1b[1;37m',
        }
        self.reset = '\033[0m'
        logging.Formatter.__init__(self, fmt, datefmt)

    def format(self, record):
        if SYSTEM == 'linux':
            record.reset = self.reset
        else:
            record.reset = ''
        record.funcName = '[%s]' % record.funcName
        levelname = record.levelname
        if SYSTEM == 'linux' and levelname in self.colors:
            color = self.colors[levelname]
            levelname = ''.join((color, levelname[0] * 2, self.reset))
            record.levelname = '[%s]' % levelname
        else:
            record.levelname = '[%s]' % (levelname[0] * 2)
        return logging.Formatter.format(self, record)

def get_logger(name, level=logging.DEBUG):
    fmt = '%(reset)s%(levelname)s  %(funcName)-24s  %(message)s'
    instance = logging.Logger('couchdb.audit.%s' % name, level)
    handler = logging.StreamHandler(sys.stdout)
    handler.setFormatter(ColoredFormatter(fmt))
    instance.addHandler(handler)
    instance.propagate = False
    return instance

def run(url, credentials, target='server'):
    if credentials:
        credentials = tuple(credentials)
    root = logging.Logger('couchdb.audit')
    handler = logging.StreamHandler(sys.stdout)
    root.addHandler(handler)

    def audit_server(url, credentials):
        server = couchdb.Server(url)
        server.resource.credentials = credentials
        root.info(' * Server: %s', server.resource.url)

        try:
            server.resource.head()
        except (HTTPError, socket.error), err:
            sys.stdout.write('%s: %s\n' % (err.__class__.__name__, err))
            sys.stdout.flush()
            sys.exit(1)

        cache = {}
        log = get_logger('couchdb.audit.server')
        auditor.audit_server(server, log, cache)

        try:
            dblist = list(server)
        except HTTPError, err:
            sys.stdout.write('Unable to get database list.\n')
            sys.stdout.write('%s: %s\n' % (err.__class__.__name__, err.args[0][1]))
            sys.stdout.flush()
            sys.exit(1)

        for dbname in sorted(dblist):
            url = server.resource(dbname).url
            audit_database(url, credentials, name=dbname, cache=cache)

    def audit_database(url, credentials, name=None, cache=None):
        if cache is None:
            cache = {}
        log = get_logger('couchdb.audit.database')
        db = couchdb.Database(url, name=name)
        db.resource.credentials = credentials
        root.info('\n * Database: %s', db.resource.url)

        try:
            db.info()['db_name']
        except socket.error, err:
            log.error('%s: %s', err.__class__.__name__, err)
            return
        except couchdb.HTTPError, err:
            log.error('%s: %s', err.__class__.__name__, err.args[0][1])
            return
        except KeyError, err:
            log.critical('%s: %s; Possible not database?',
                         err.__class__.__name__, err)
            return

        auditor.audit_database(db, log, cache)

        try:
            rows = db.view('_all_docs', startkey='_design/',  endkey='_design0')
            rows = list(rows)
        except HTTPError, err:
            log.critical('Unable to get design documents list: %s',
                         err.args[0][1])
            return

        for row in rows:
            root.info('\n * DDoc: %s', db.resource(*row.id.split('/')).url)
            log = get_logger('couchdb.audit.ddoc')
            ddoc = db[row.id]
            auditor.audit_ddoc(ddoc, log, cache)

    if target == 'server':
        audit_server(url, credentials)
    elif target == 'database':
        audit_database(url, credentials)

    return 0

def main():
    try:
        options, arguments = getopt.gnu_getopt(
            sys.argv[1:], 'hvu:d',
            ['version', 'help', 'user=', 'database']
        )
    except getopt.GetoptError, err:
        sys.stdout.write(('%s\n\n' % err).capitalize())
        sys.stdout.write(_HELP)
        sys.stdout.flush()
        sys.exit(1)
    message = None

    target = 'server'
    user = None
    for option, value in options:
        if option in ['--version']:
            message = _VERSION
        elif option in ['-h', '--help']:
            message = _HELP
        elif option in ['-u', '--user']:
            user = value
        elif option in ['-d', '--database']:
            target = 'database'

    if message:
        sys.stdout.write(message)
        sys.stdout.flush()
        sys.exit(0)

    if not arguments:
        sys.stdout.write(_NO_URL)
        sys.stdout.flush()
        sys.exit(1)

    url = arguments[0]
    if not url.startswith('http://'):
        url = 'http://' + url
    _, credentials = extract_credentials(url)

    if credentials:
        if user is not None and credentials[0] != user:
            sys.stdout.write(_USER_DUPLICATE % (credentials[0], user))
            sys.stdout.flush()
            sys.exit(1)
        credentials = list(credentials)
    elif user is not None:
        credentials = [user]

    if credentials and len(credentials) == 1:
        credentials.append(getpass('Enter password for %s: ' % credentials[0]))

    sys.exit(run(url, credentials, target))

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


<commit_before># Ridiculously simple test of the winsound module for Windows.

import unittest
from test import support
support.requires('audio')
import time
import os
import subprocess

winsound = support.import_module('winsound')
ctypes = support.import_module('ctypes')
import winreg

def has_sound(sound):
    """Find out if a particular event is configured with a default sound"""
    try:
        # Ask the mixer API for the number of devices it knows about.
        # When there are no devices, PlaySound will fail.
        if ctypes.windll.winmm.mixerGetNumDevs() is 0:
            return False

        key = winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER,
                "AppEvents\Schemes\Apps\.Default\{0}\.Default".format(sound))
        value = winreg.EnumValue(key, 0)[1]
        if value is not "":
            return True
        else:
            return False
    except WindowsError:
        return False

class BeepTest(unittest.TestCase):
    # As with PlaySoundTest, incorporate the _have_soundcard() check
    # into our test methods.  If there's no audio device present,
    # winsound.Beep returns 0 and GetLastError() returns 127, which
    # is: ERROR_PROC_NOT_FOUND ("The specified procedure could not
    # be found").  (FWIW, virtual/Hyper-V systems fall under this
    # scenario as they have no sound devices whatsoever  (not even
    # a legacy Beep device).)

    def test_errors(self):
        self.assertRaises(TypeError, winsound.Beep)
        self.assertRaises(ValueError, winsound.Beep, 36, 75)
        self.assertRaises(ValueError, winsound.Beep, 32768, 75)

    def test_extremes(self):
        self._beep(37, 75)
        self._beep(32767, 75)

    def test_increasingfrequency(self):
        for i in range(100, 2000, 100):
            self._beep(i, 75)

    def _beep(self, *args):
        # these tests used to use _have_soundcard(), but it's quite
        # possible to have a soundcard, and yet have the beep driver
        # disabled. So basically, we have no way of knowing whether
        # a beep should be produced or not, so currently if these
        # tests fail we're ignoring them
        #
        # XXX the right fix for this is to define something like
        # _have_enabled_beep_driver() and use that instead of the
        # try/except below
        try:
            winsound.Beep(*args)
        except RuntimeError:
            pass

class MessageBeepTest(unittest.TestCase):

    def tearDown(self):
        time.sleep(0.5)

    def test_default(self):
        self.assertRaises(TypeError, winsound.MessageBeep, "bad")
        self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
        winsound.MessageBeep()

    def test_ok(self):
        winsound.MessageBeep(winsound.MB_OK)

    def test_asterisk(self):
        winsound.MessageBeep(winsound.MB_ICONASTERISK)

    def test_exclamation(self):
        winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)

    def test_hand(self):
        winsound.MessageBeep(winsound.MB_ICONHAND)

    def test_question(self):
        winsound.MessageBeep(winsound.MB_ICONQUESTION)

class PlaySoundTest(unittest.TestCase):

    def test_errors(self):
        self.assertRaises(TypeError, winsound.PlaySound)
        self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
        self.assertRaises(
            RuntimeError,
            winsound.PlaySound,
            "none", winsound.SND_ASYNC | winsound.SND_MEMORY
        )

    @unittest.skipUnless(has_sound("SystemAsterisk"),
                         "No default SystemAsterisk")
    def test_alias_asterisk(self):
        if _have_soundcard():
            winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
        else:
            self.assertRaises(
                RuntimeError,
                winsound.PlaySound,
                'SystemAsterisk', winsound.SND_ALIAS
            )

    @unittest.skipUnless(has_sound("SystemExclamation"),
                         "No default SystemExclamation")
    def test_alias_exclamation(self):
        if _have_soundcard():
            winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
        else:
            self.assertRaises(
                RuntimeError,
                winsound.PlaySound,
                'SystemExclamation', winsound.SND_ALIAS
            )

    @unittest.skipUnless(has_sound("SystemExit"), "No default SystemExit")
    def test_alias_exit(self):
        if _have_soundcard():
            winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
        else:
            self.assertRaises(
                RuntimeError,
                winsound.PlaySound,
                'SystemExit', winsound.SND_ALIAS
            )

    @unittest.skipUnless(has_sound("SystemHand"), "No default SystemHand")
    def test_alias_hand(self):
        if _have_soundcard():
            winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
        else:
            self.assertRaises(
                RuntimeError,
                winsound.PlaySound,
                'SystemHand', winsound.SND_ALIAS
            )

    @unittest.skipUnless(has_sound("SystemQuestion"),
                         "No default SystemQuestion")
    def test_alias_question(self):
        if _have_soundcard():
            winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
        else:
            self.assertRaises(
                RuntimeError,
                winsound.PlaySound,
                'SystemQuestion', winsound.SND_ALIAS
            )

    def test_alias_fallback(self):
        # This test can't be expected to work on all systems.  The MS
        # PlaySound() docs say:
        #
        #     If it cannot find the specified sound, PlaySound uses the
        #     default system event sound entry instead.  If the function
        #     can find neither the system default entry nor the default
        #     sound, it makes no sound and returns FALSE.
        #
        # It's known to return FALSE on some real systems.

        # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
        return

    def test_alias_nofallback(self):
        if _have_soundcard():
            # Note that this is not the same as asserting RuntimeError
            # will get raised:  you cannot convert this to
            # self.assertRaises(...) form.  The attempt may or may not
            # raise RuntimeError, but it shouldn't raise anything other
            # than RuntimeError, and that's all we're trying to test
            # here.  The MS docs aren't clear about whether the SDK
            # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
            # True or False when the alias is unknown.  On Tim's WinXP
            # box today, it returns True (no exception is raised).  What
            # we'd really like to test is that no sound is played, but
            # that requires first wiring an eardrum class into unittest
            # <wink>.
            try:
                winsound.PlaySound(
                    '!"$%&/(#+*',
                    winsound.SND_ALIAS | winsound.SND_NODEFAULT
                )
            except RuntimeError:
                pass
        else:
            self.assertRaises(
                RuntimeError,
                winsound.PlaySound,
                '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
            )

    def test_stopasync(self):
        if _have_soundcard():
            winsound.PlaySound(
                'SystemQuestion',
                winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
            )
            time.sleep(0.5)
            try:
                winsound.PlaySound(
                    'SystemQuestion',
                    winsound.SND_ALIAS | winsound.SND_NOSTOP
                )
            except RuntimeError:
                pass
            else: # the first sound might already be finished
                pass
            winsound.PlaySound(None, winsound.SND_PURGE)
        else:
            # Issue 8367: PlaySound(None, winsound.SND_PURGE)
            # does not raise on systems without a sound card.
            pass

def _get_cscript_path():
    """Return the full path to cscript.exe or None."""
    for dir in os.environ.get("PATH", "").split(os.pathsep):
        cscript_path = os.path.join(dir, "cscript.exe")
        if os.path.exists(cscript_path):
            return cscript_path

__have_soundcard_cache = None
def _have_soundcard():
    """Return True iff this computer has a soundcard."""
    global __have_soundcard_cache
    if __have_soundcard_cache is None:
        cscript_path = _get_cscript_path()
        if cscript_path is None:
            # Could not find cscript.exe to run our VBScript helper. Default
            # to True: most computers these days *do* have a soundcard.
            return True

        check_script = os.path.join(os.path.dirname(__file__),
                                    "check_soundcard.vbs")
        p = subprocess.Popen([cscript_path, check_script],
                             stdout=subprocess.PIPE)
        __have_soundcard_cache = not p.wait()
    return __have_soundcard_cache

def test_main():
    support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)

if __name__=="__main__":
    test_main()
<commit_msg>Close stdout, clear ResourceWarning<commit_after># Ridiculously simple test of the winsound module for Windows.

import unittest
from test import support
support.requires('audio')
import time
import os
import subprocess

winsound = support.import_module('winsound')
ctypes = support.import_module('ctypes')
import winreg

def has_sound(sound):
    """Find out if a particular event is configured with a default sound"""
    try:
        # Ask the mixer API for the number of devices it knows about.
        # When there are no devices, PlaySound will fail.
        if ctypes.windll.winmm.mixerGetNumDevs() is 0:
            return False

        key = winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER,
                "AppEvents\Schemes\Apps\.Default\{0}\.Default".format(sound))
        value = winreg.EnumValue(key, 0)[1]
        if value is not "":
            return True
        else:
            return False
    except WindowsError:
        return False

class BeepTest(unittest.TestCase):
    # As with PlaySoundTest, incorporate the _have_soundcard() check
    # into our test methods.  If there's no audio device present,
    # winsound.Beep returns 0 and GetLastError() returns 127, which
    # is: ERROR_PROC_NOT_FOUND ("The specified procedure could not
    # be found").  (FWIW, virtual/Hyper-V systems fall under this
    # scenario as they have no sound devices whatsoever  (not even
    # a legacy Beep device).)

    def test_errors(self):
        self.assertRaises(TypeError, winsound.Beep)
        self.assertRaises(ValueError, winsound.Beep, 36, 75)
        self.assertRaises(ValueError, winsound.Beep, 32768, 75)

    def test_extremes(self):
        self._beep(37, 75)
        self._beep(32767, 75)

    def test_increasingfrequency(self):
        for i in range(100, 2000, 100):
            self._beep(i, 75)

    def _beep(self, *args):
        # these tests used to use _have_soundcard(), but it's quite
        # possible to have a soundcard, and yet have the beep driver
        # disabled. So basically, we have no way of knowing whether
        # a beep should be produced or not, so currently if these
        # tests fail we're ignoring them
        #
        # XXX the right fix for this is to define something like
        # _have_enabled_beep_driver() and use that instead of the
        # try/except below
        try:
            winsound.Beep(*args)
        except RuntimeError:
            pass

class MessageBeepTest(unittest.TestCase):

    def tearDown(self):
        time.sleep(0.5)

    def test_default(self):
        self.assertRaises(TypeError, winsound.MessageBeep, "bad")
        self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
        winsound.MessageBeep()

    def test_ok(self):
        winsound.MessageBeep(winsound.MB_OK)

    def test_asterisk(self):
        winsound.MessageBeep(winsound.MB_ICONASTERISK)

    def test_exclamation(self):
        winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)

    def test_hand(self):
        winsound.MessageBeep(winsound.MB_ICONHAND)

    def test_question(self):
        winsound.MessageBeep(winsound.MB_ICONQUESTION)

class PlaySoundTest(unittest.TestCase):

    def test_errors(self):
        self.assertRaises(TypeError, winsound.PlaySound)
        self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
        self.assertRaises(
            RuntimeError,
            winsound.PlaySound,
            "none", winsound.SND_ASYNC | winsound.SND_MEMORY
        )

    @unittest.skipUnless(has_sound("SystemAsterisk"),
                         "No default SystemAsterisk")
    def test_alias_asterisk(self):
        if _have_soundcard():
            winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
        else:
            self.assertRaises(
                RuntimeError,
                winsound.PlaySound,
                'SystemAsterisk', winsound.SND_ALIAS
            )

    @unittest.skipUnless(has_sound("SystemExclamation"),
                         "No default SystemExclamation")
    def test_alias_exclamation(self):
        if _have_soundcard():
            winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
        else:
            self.assertRaises(
                RuntimeError,
                winsound.PlaySound,
                'SystemExclamation', winsound.SND_ALIAS
            )

    @unittest.skipUnless(has_sound("SystemExit"), "No default SystemExit")
    def test_alias_exit(self):
        if _have_soundcard():
            winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
        else:
            self.assertRaises(
                RuntimeError,
                winsound.PlaySound,
                'SystemExit', winsound.SND_ALIAS
            )

    @unittest.skipUnless(has_sound("SystemHand"), "No default SystemHand")
    def test_alias_hand(self):
        if _have_soundcard():
            winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
        else:
            self.assertRaises(
                RuntimeError,
                winsound.PlaySound,
                'SystemHand', winsound.SND_ALIAS
            )

    @unittest.skipUnless(has_sound("SystemQuestion"),
                         "No default SystemQuestion")
    def test_alias_question(self):
        if _have_soundcard():
            winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
        else:
            self.assertRaises(
                RuntimeError,
                winsound.PlaySound,
                'SystemQuestion', winsound.SND_ALIAS
            )

    def test_alias_fallback(self):
        # This test can't be expected to work on all systems.  The MS
        # PlaySound() docs say:
        #
        #     If it cannot find the specified sound, PlaySound uses the
        #     default system event sound entry instead.  If the function
        #     can find neither the system default entry nor the default
        #     sound, it makes no sound and returns FALSE.
        #
        # It's known to return FALSE on some real systems.

        # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
        return

    def test_alias_nofallback(self):
        if _have_soundcard():
            # Note that this is not the same as asserting RuntimeError
            # will get raised:  you cannot convert this to
            # self.assertRaises(...) form.  The attempt may or may not
            # raise RuntimeError, but it shouldn't raise anything other
            # than RuntimeError, and that's all we're trying to test
            # here.  The MS docs aren't clear about whether the SDK
            # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
            # True or False when the alias is unknown.  On Tim's WinXP
            # box today, it returns True (no exception is raised).  What
            # we'd really like to test is that no sound is played, but
            # that requires first wiring an eardrum class into unittest
            # <wink>.
            try:
                winsound.PlaySound(
                    '!"$%&/(#+*',
                    winsound.SND_ALIAS | winsound.SND_NODEFAULT
                )
            except RuntimeError:
                pass
        else:
            self.assertRaises(
                RuntimeError,
                winsound.PlaySound,
                '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
            )

    def test_stopasync(self):
        if _have_soundcard():
            winsound.PlaySound(
                'SystemQuestion',
                winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
            )
            time.sleep(0.5)
            try:
                winsound.PlaySound(
                    'SystemQuestion',
                    winsound.SND_ALIAS | winsound.SND_NOSTOP
                )
            except RuntimeError:
                pass
            else: # the first sound might already be finished
                pass
            winsound.PlaySound(None, winsound.SND_PURGE)
        else:
            # Issue 8367: PlaySound(None, winsound.SND_PURGE)
            # does not raise on systems without a sound card.
            pass

def _get_cscript_path():
    """Return the full path to cscript.exe or None."""
    for dir in os.environ.get("PATH", "").split(os.pathsep):
        cscript_path = os.path.join(dir, "cscript.exe")
        if os.path.exists(cscript_path):
            return cscript_path

__have_soundcard_cache = None
def _have_soundcard():
    """Return True iff this computer has a soundcard."""
    global __have_soundcard_cache
    if __have_soundcard_cache is None:
        cscript_path = _get_cscript_path()
        if cscript_path is None:
            # Could not find cscript.exe to run our VBScript helper. Default
            # to True: most computers these days *do* have a soundcard.
            return True

        check_script = os.path.join(os.path.dirname(__file__),
                                    "check_soundcard.vbs")
        p = subprocess.Popen([cscript_path, check_script],
                             stdout=subprocess.PIPE)
        __have_soundcard_cache = not p.wait()
        p.stdout.close()
    return __have_soundcard_cache

def test_main():
    support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)

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