<commit_before>''' Convienence methods on VTK routines only '''

import director.vtkAll as vtk
import director.vtkNumpy as vnp
from director.shallowCopy import shallowCopy
import numpy as np

def thresholdPoints(polyData, arrayName, thresholdRange):
    assert(polyData.GetPointData().GetArray(arrayName))
    f = vtk.vtkThresholdPoints()
    f.SetInput(polyData)
    f.ThresholdBetween(thresholdRange[0], thresholdRange[1])
    f.SetInputArrayToProcess(0,0,0, vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS, arrayName)
    f.Update()
    return shallowCopy(f.GetOutput())

def transformPolyData(polyData, transform):

    t = vtk.vtkTransformPolyDataFilter()
    t.SetTransform(transform)
    t.SetInput(shallowCopy(polyData))
    t.Update()
    return shallowCopy(t.GetOutput())

def computeDelaunay3D(polyData):
    f = vtk.vtkDelaunay3D()
    f.SetInput(polyData)
    f.SetOffset(100.0)
    f.Update()

    surface = vtk.vtkGeometryFilter()
    surface.SetInput(f.GetOutput())
    surface.Update()

    clean = vtk.vtkCleanPolyData()
    clean.SetInput(surface.GetOutput())
    clean.Update()

    return shallowCopy(clean.GetOutput())

def computeDelaunay2D(polyData):
    f = vtk.vtkDelaunay2D()
    f.SetInput(polyData)
    f.Update()
    return shallowCopy(f.GetOutput())

def computeCentroid(polyData):
    return np.average(vnp.getNumpyFromVtk(polyData, 'Points'), axis=0)

def appendPolyData(polyDataList):
    assert len(polyDataList)
    append = vtk.vtkAppendPolyData()
    for polyData in polyDataList:
        append.AddInput(polyData)
    append.Update()
    return shallowCopy(append.GetOutput())

def computeNormals(polyData, featureAngle=45):
    normals = vtk.vtkPolyDataNormals()
    normals.SetFeatureAngle(featureAngle)
    normals.SetInput(polyData)
    normals.Update()
    return shallowCopy(normals.GetOutput())

def cleanPolyData(polyData):
    clean = vtk.vtkCleanPolyData()
    clean.SetInput(polyData)
    clean.Update()
    return shallowCopy(clean.GetOutput())

def triangulatePolyData(polyData):
    f = vtk.vtkTriangleFilter()
    f.SetInput(polyData)
    f.Update()
    return shallowCopy(f.GetOutput())

def decimateMesh(polyData, targetReduction=0.1):
    '''
    Reduce the number of  triangles in the input mesh by targetReduction.
    0.1 = 10% reduction (if there was 100 triangles, now there will be 90)
    '''
    f = vtk.vtkDecimatePro()
    f.SetInput(polyData)
    f.SetTargetReduction(targetReduction)
    f.Update()
    return shallowCopy(f.GetOutput())

def hasNonFinitePoints(polyData, arrayName='Points'):
    pts = vnp.getNumpyFromVtk(polyData, arrayName)
    return np.isfinite(pts).any()

def labelNonFinitePoints(polyData, arrayName='Points'):
    '''
    adds is_nonfinite label to polyData.  non finite includes nan and +/- inf.
    '''
    pts = vnp.getNumpyFromVtk(polyData, arrayName)
    labels = np.logical_not(np.isfinite(pts)).any(axis=1)
    vnp.addNumpyToVtk(polyData, np.array(labels, dtype=np.int32), 'is_nonfinite')

def removeNonFinitePoints(polyData, arrayName='Points'):
    polyData = shallowCopy(polyData)
    labelNonFinitePoints(polyData, arrayName)
    return thresholdPoints(polyData, 'is_nonfinite', [0, 0])

def rotateImage180(image):
    '''
    rotates an image by 180 degrees
    '''
    r1 = vtk.vtkImageFlip()
    r1.SetInput(image)
    r1.SetFilteredAxis(0)
    r1.Update()
    r2 = vtk.vtkImageFlip()
    r2.SetInput(r1.GetOutput())
    r2.SetFilteredAxis(1)
    r2.Update()
    return shallowCopy(r2.GetOutput())
<commit_msg>add utility function for thresholdingCells<commit_after>''' Convienence methods on VTK routines only '''

import director.vtkAll as vtk
import director.vtkNumpy as vnp
from director.shallowCopy import shallowCopy
import numpy as np

def thresholdPoints(polyData, arrayName, thresholdRange):
    assert(polyData.GetPointData().GetArray(arrayName))
    f = vtk.vtkThresholdPoints()
    f.SetInput(polyData)
    f.ThresholdBetween(thresholdRange[0], thresholdRange[1])
    f.SetInputArrayToProcess(0,0,0, vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS, arrayName)
    f.Update()
    return shallowCopy(f.GetOutput())

def thresholdCells(polyData, arrayName, thresholdRange):
    assert(polyData.GetCellData().GetArray(arrayName))
    f = vtk.vtkThreshold()
    # f.SetAttributeModeToUseCellData()
    f.SetInput(polyData)
    f.ThresholdBetween(thresholdRange[0], thresholdRange[1])
    f.SetInputArrayToProcess(0,0,0, vtk.vtkDataObject.FIELD_ASSOCIATION_CELLS, arrayName)
    f.Update()

    g = vtk.vtkGeometryFilter()
    g.SetInput(f.GetOutput())
    g.Update()
    return shallowCopy(g.GetOutput())

def transformPolyData(polyData, transform):

    t = vtk.vtkTransformPolyDataFilter()
    t.SetTransform(transform)
    t.SetInput(shallowCopy(polyData))
    t.Update()
    return shallowCopy(t.GetOutput())

def computeDelaunay3D(polyData):
    f = vtk.vtkDelaunay3D()
    f.SetInput(polyData)
    f.SetOffset(100.0)
    f.Update()

    surface = vtk.vtkGeometryFilter()
    surface.SetInput(f.GetOutput())
    surface.Update()

    clean = vtk.vtkCleanPolyData()
    clean.SetInput(surface.GetOutput())
    clean.Update()

    return shallowCopy(clean.GetOutput())

def computeDelaunay2D(polyData):
    f = vtk.vtkDelaunay2D()
    f.SetInput(polyData)
    f.Update()
    return shallowCopy(f.GetOutput())

def computeCentroid(polyData):
    return np.average(vnp.getNumpyFromVtk(polyData, 'Points'), axis=0)

def appendPolyData(polyDataList):
    assert len(polyDataList)
    append = vtk.vtkAppendPolyData()
    for polyData in polyDataList:
        append.AddInput(polyData)
    append.Update()
    return shallowCopy(append.GetOutput())

def computeNormals(polyData, featureAngle=45):
    normals = vtk.vtkPolyDataNormals()
    normals.SetFeatureAngle(featureAngle)
    normals.SetInput(polyData)
    normals.Update()
    return shallowCopy(normals.GetOutput())

def cleanPolyData(polyData):
    clean = vtk.vtkCleanPolyData()
    clean.SetInput(polyData)
    clean.Update()
    return shallowCopy(clean.GetOutput())

def triangulatePolyData(polyData):
    f = vtk.vtkTriangleFilter()
    f.SetInput(polyData)
    f.Update()
    return shallowCopy(f.GetOutput())

def decimateMesh(polyData, targetReduction=0.1):
    '''
    Reduce the number of  triangles in the input mesh by targetReduction.
    0.1 = 10% reduction (if there was 100 triangles, now there will be 90)
    '''
    f = vtk.vtkDecimatePro()
    f.SetInput(polyData)
    f.SetTargetReduction(targetReduction)
    f.Update()
    return shallowCopy(f.GetOutput())

def hasNonFinitePoints(polyData, arrayName='Points'):
    pts = vnp.getNumpyFromVtk(polyData, arrayName)
    return np.isfinite(pts).any()

def labelNonFinitePoints(polyData, arrayName='Points'):
    '''
    adds is_nonfinite label to polyData.  non finite includes nan and +/- inf.
    '''
    pts = vnp.getNumpyFromVtk(polyData, arrayName)
    labels = np.logical_not(np.isfinite(pts)).any(axis=1)
    vnp.addNumpyToVtk(polyData, np.array(labels, dtype=np.int32), 'is_nonfinite')

def removeNonFinitePoints(polyData, arrayName='Points'):
    polyData = shallowCopy(polyData)
    labelNonFinitePoints(polyData, arrayName)
    return thresholdPoints(polyData, 'is_nonfinite', [0, 0])

def rotateImage180(image):
    '''
    rotates an image by 180 degrees
    '''
    r1 = vtk.vtkImageFlip()
    r1.SetInput(image)
    r1.SetFilteredAxis(0)
    r1.Update()
    r2 = vtk.vtkImageFlip()
    r2.SetInput(r1.GetOutput())
    r2.SetFilteredAxis(1)
    r2.Update()
    return shallowCopy(r2.GetOutput())
<|endoftext|>


<commit_before># Copyright 2019 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import itertools
import logging
from dataclasses import dataclass
from typing import FrozenSet, Iterable, Iterator, List, NamedTuple, Optional, Sequence, Set, Tuple

from pkg_resources import Requirement

from pants.backend.python.rules.download_pex_bin import DownloadedPexBin
from pants.backend.python.rules.hermetic_pex import HermeticPex
from pants.backend.python.rules.targets import Compatibility
from pants.backend.python.subsystems.python_native_code import PexBuildEnvironment
from pants.backend.python.subsystems.subprocess_environment import SubprocessEncodingEnvironment
from pants.engine.fs import (
    EMPTY_DIRECTORY_DIGEST,
    EMPTY_SNAPSHOT,
    Digest,
    DirectoriesToMerge,
    DirectoryWithPrefixToAdd,
    GlobExpansionConjunction,
    GlobMatchErrorBehavior,
    PathGlobs,
    Snapshot,
)
from pants.engine.isolated_process import ExecuteProcessResult, MultiPlatformExecuteProcessRequest
from pants.engine.legacy.structs import PythonTargetAdaptor, TargetAdaptor
from pants.engine.platform import Platform, PlatformConstraint
from pants.engine.rules import rule, subsystem_rule
from pants.engine.selectors import Get
from pants.python.python_setup import PythonSetup
from pants.util.logging import LogLevel
from pants.util.memo import memoized_property
from pants.util.meta import frozen_after_init
from pants.util.ordered_set import FrozenOrderedSet

@frozen_after_init
@dataclass(unsafe_hash=True)
class PexRequirements:
    requirements: FrozenOrderedSet[str]

    def __init__(self, requirements: Optional[Iterable[str]] = None) -> None:
        self.requirements = FrozenOrderedSet(sorted(requirements or ()))

    @classmethod
    def create_from_adaptors(
        cls, adaptors: Iterable[TargetAdaptor], additional_requirements: Iterable[str] = ()
    ) -> "PexRequirements":
        all_target_requirements = set()
        for maybe_python_req_lib in adaptors:
            # This is a python_requirement()-like target.
            if hasattr(maybe_python_req_lib, "requirement"):
                all_target_requirements.add(str(maybe_python_req_lib.requirement))
            # This is a python_requirement_library()-like target.
            if hasattr(maybe_python_req_lib, "requirements"):
                for py_req in maybe_python_req_lib.requirements:
                    all_target_requirements.add(str(py_req.requirement))
        all_target_requirements.update(additional_requirements)
        return PexRequirements(all_target_requirements)

Spec = Tuple[str, str]  # e.g. (">=", "3.6")

class ParsedConstraint(NamedTuple):
    interpreter: str
    specs: FrozenSet[Spec]

    def __str__(self) -> str:
        specs = ",".join(
            f"{op}{version}" for op, version in sorted(self.specs, key=lambda spec: spec[1])
        )
        return f"{self.interpreter}{specs}"

@frozen_after_init
@dataclass(unsafe_hash=True)
class PexInterpreterConstraints:
    constraints: FrozenOrderedSet[str]

    def __init__(self, constraints: Optional[Iterable[str]] = None) -> None:
        self.constraints = FrozenOrderedSet(sorted(constraints or ()))

    @staticmethod
    def merge_constraint_sets(constraint_sets: Iterable[Iterable[str]]) -> List[str]:
        """Given a collection of constraints sets, merge by ORing within each individual constraint
        set and ANDing across each distinct constraint set.

        For example, given `[["CPython>=2.7", "CPython<=3"], ["CPython==3.6.*"]]`, return
        `["CPython>=2.7,==3.6.*", "CPython<=3,==3.6.*"]`.
        """
        # Each element (a Set[ParsedConstraint]) will get ANDed. We use sets to deduplicate
        # identical top-level parsed constraint sets.
        parsed_constraint_sets: Set[FrozenSet[ParsedConstraint]] = set()
        for constraint_set in constraint_sets:
            # Each element (a ParsedConstraint) will get ORed.
            parsed_constraint_set: Set[ParsedConstraint] = set()
            for constraint in constraint_set:
                try:
                    parsed_requirement = Requirement.parse(constraint)
                except ValueError:
                    # We allow the shorthand `>=3.7`, which gets expanded to `CPython>=3.7`. See
                    # Pex's interpreter.py's `parse_requirement()`.
                    parsed_requirement = Requirement.parse(f"CPython{constraint}")
                interpreter = parsed_requirement.project_name
                specs = frozenset(parsed_requirement.specs)
                parsed_constraint_set.add(ParsedConstraint(interpreter, specs))
            parsed_constraint_sets.add(frozenset(parsed_constraint_set))

        def and_constraints(parsed_constraints: Sequence[ParsedConstraint]) -> ParsedConstraint:
            merged_specs: Set[Spec] = set()
            expected_interpreter = parsed_constraints[0][0]
            for parsed_constraint in parsed_constraints:
                if parsed_constraint.interpreter != expected_interpreter:
                    attempted_interpreters = {
                        interpreter: sorted(
                            str(parsed_constraint) for parsed_constraint in parsed_constraints
                        )
                        for interpreter, parsed_constraints in itertools.groupby(
                            parsed_constraints,
                            key=lambda parsed_constraint: parsed_constraint.interpreter,
                        )
                    }
                    raise ValueError(
                        "Tried ANDing Python interpreter constraints with different interpreter "
                        "types. Please use only one interpreter type. Got "
                        f"{attempted_interpreters}."
                    )
                merged_specs.update(parsed_constraint.specs)
            return ParsedConstraint(expected_interpreter, frozenset(merged_specs))

        return sorted(
            {
                str(and_constraints(constraints_product))
                for constraints_product in itertools.product(*parsed_constraint_sets)
            }
        )

    @classmethod
    def create_from_compatibility_fields(
        cls, fields: Iterable[Compatibility], python_setup: PythonSetup
    ) -> "PexInterpreterConstraints":
        constraint_sets = {field.value_or_global_default(python_setup) for field in fields}
        # This will OR within each field and AND across fields.
        merged_constraints = cls.merge_constraint_sets(constraint_sets)
        return PexInterpreterConstraints(merged_constraints)

    @classmethod
    def create_from_adaptors(
        cls, adaptors: Iterable[TargetAdaptor], python_setup: PythonSetup
    ) -> "PexInterpreterConstraints":
        constraint_sets: Set[Iterable[str]] = set()
        for adaptor in adaptors:
            if not isinstance(adaptor, PythonTargetAdaptor):
                continue
            compatibility_field = Compatibility(adaptor.compatibility, address=adaptor.address)
            constraint_sets.add(compatibility_field.value_or_global_default(python_setup))
        # This will OR within each target and AND across targets.
        merged_constraints = cls.merge_constraint_sets(constraint_sets)
        return PexInterpreterConstraints(merged_constraints)

    def generate_pex_arg_list(self) -> List[str]:
        args = []
        for constraint in sorted(self.constraints):
            args.extend(["--interpreter-constraint", constraint])
        return args

@dataclass(frozen=True)
class CreatePex:
    """Represents a generic request to create a PEX from its inputs."""

    output_filename: str
    requirements: PexRequirements = PexRequirements()
    interpreter_constraints: PexInterpreterConstraints = PexInterpreterConstraints()
    entry_point: Optional[str] = None
    input_files_digest: Optional[Digest] = None
    additional_args: Tuple[str, ...] = ()

@dataclass(frozen=True)
class Pex(HermeticPex):
    """Wrapper for a digest containing a pex file created with some filename."""

    directory_digest: Digest
    output_filename: str

logger = logging.getLogger(__name__)

@dataclass(frozen=True)
class PexDebug:
    log_level: LogLevel

    _PEX_LEVEL_BY_PANTS_LEVEL = {
        LogLevel.TRACE: 9,
        LogLevel.DEBUG: 3,
    }

    @memoized_property
    def level(self) -> int:
        return self._PEX_LEVEL_BY_PANTS_LEVEL.get(self.log_level, 0)

    def iter_pex_args(self) -> Iterator[str]:
        yield "--no-emit-warnings"
        if self.level > 0:
            yield f"-{'v' * self.level}"

    @property
    def might_log(self):
        return self.level > 0

    def log(self, *args, **kwargs) -> None:
        self.log_level.log(logger, *args, **kwargs)

@rule(name="Create PEX")
async def create_pex(
    request: CreatePex,
    pex_bin: DownloadedPexBin,
    python_setup: PythonSetup,
    subprocess_encoding_environment: SubprocessEncodingEnvironment,
    pex_build_environment: PexBuildEnvironment,
    platform: Platform,
    log_level: LogLevel,
) -> Pex:
    """Returns a PEX with the given requirements, optional entry point, optional interpreter
    constraints, and optional requirement constraints."""

    argv = [
        "--output-file",
        request.output_filename,
        *request.interpreter_constraints.generate_pex_arg_list(),
        *request.additional_args,
    ]

    pex_debug = PexDebug(log_level)
    argv.extend(pex_debug.iter_pex_args())

    if python_setup.resolver_jobs:
        argv.extend(["--jobs", python_setup.resolver_jobs])

    if python_setup.manylinux:
        argv.extend(["--manylinux", python_setup.manylinux])
    else:
        argv.append("--no-manylinux")

    if request.entry_point is not None:
        argv.extend(["--entry-point", request.entry_point])

    if python_setup.requirement_constraints is not None:
        argv.extend(["--constraints", python_setup.requirement_constraints])

    source_dir_name = "source_files"
    argv.append(f"--sources-directory={source_dir_name}")

    argv.extend(request.requirements.requirements)

    constraint_file_snapshot = EMPTY_SNAPSHOT
    if python_setup.requirement_constraints is not None:
        constraint_file_snapshot = await Get[Snapshot](
            PathGlobs(
                [python_setup.requirement_constraints],
                glob_match_error_behavior=GlobMatchErrorBehavior.error,
                conjunction=GlobExpansionConjunction.all_match,
                description_of_origin="the option `--python-setup-requirement-constraints`",
            )
        )

    sources_digest = (
        request.input_files_digest if request.input_files_digest else EMPTY_DIRECTORY_DIGEST
    )
    sources_digest_as_subdir = await Get[Digest](
        DirectoryWithPrefixToAdd(sources_digest, source_dir_name)
    )

    merged_digest = await Get[Digest](
        DirectoriesToMerge(
            directories=(
                pex_bin.directory_digest,
                sources_digest_as_subdir,
                constraint_file_snapshot.directory_digest,
            )
        )
    )

    # NB: PEX outputs are platform dependent so in order to get a PEX that we can use locally, without
    # cross-building, we specify that our PEX command be run on the current local platform. When we
    # support cross-building through CLI flags we can configure requests that build a PEX for our
    # local platform that are able to execute on a different platform, but for now in order to
    # guarantee correct build we need to restrict this command to execute on the same platform type
    # that the output is intended for. The correct way to interpret the keys
    # (execution_platform_constraint, target_platform_constraint) of this dictionary is "The output of
    # this command is intended for `target_platform_constraint` iff it is run on `execution_platform
    # constraint`".
    execute_process_request = MultiPlatformExecuteProcessRequest(
        {
            (
                PlatformConstraint(platform.value),
                PlatformConstraint(platform.value),
            ): pex_bin.create_execute_request(
                python_setup=python_setup,
                subprocess_encoding_environment=subprocess_encoding_environment,
                pex_build_environment=pex_build_environment,
                pex_args=argv,
                input_files=merged_digest,
                description=f"Create a requirements PEX: {', '.join(request.requirements.requirements)}",
                output_files=(request.output_filename,),
            )
        }
    )

    result = await Get[ExecuteProcessResult](
        MultiPlatformExecuteProcessRequest, execute_process_request
    )

    if pex_debug.might_log:
        lines = result.stderr.decode().splitlines()
        if lines:
            pex_debug.log(f"Debug output from Pex for: {execute_process_request}")
            for line in lines:
                pex_debug.log(line)

    return Pex(
        directory_digest=result.output_directory_digest, output_filename=request.output_filename
    )

def rules():
    return [
        create_pex,
        subsystem_rule(PythonSetup),
    ]
<commit_msg>Change the workunit description for requirement pex creation. (#9370)<commit_after># Copyright 2019 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import itertools
import logging
from dataclasses import dataclass
from typing import FrozenSet, Iterable, Iterator, List, NamedTuple, Optional, Sequence, Set, Tuple

from pkg_resources import Requirement

from pants.backend.python.rules.download_pex_bin import DownloadedPexBin
from pants.backend.python.rules.hermetic_pex import HermeticPex
from pants.backend.python.rules.targets import Compatibility
from pants.backend.python.subsystems.python_native_code import PexBuildEnvironment
from pants.backend.python.subsystems.subprocess_environment import SubprocessEncodingEnvironment
from pants.engine.fs import (
    EMPTY_DIRECTORY_DIGEST,
    EMPTY_SNAPSHOT,
    Digest,
    DirectoriesToMerge,
    DirectoryWithPrefixToAdd,
    GlobExpansionConjunction,
    GlobMatchErrorBehavior,
    PathGlobs,
    Snapshot,
)
from pants.engine.isolated_process import ExecuteProcessResult, MultiPlatformExecuteProcessRequest
from pants.engine.legacy.structs import PythonTargetAdaptor, TargetAdaptor
from pants.engine.platform import Platform, PlatformConstraint
from pants.engine.rules import rule, subsystem_rule
from pants.engine.selectors import Get
from pants.python.python_setup import PythonSetup
from pants.util.logging import LogLevel
from pants.util.memo import memoized_property
from pants.util.meta import frozen_after_init
from pants.util.ordered_set import FrozenOrderedSet

@frozen_after_init
@dataclass(unsafe_hash=True)
class PexRequirements:
    requirements: FrozenOrderedSet[str]

    def __init__(self, requirements: Optional[Iterable[str]] = None) -> None:
        self.requirements = FrozenOrderedSet(sorted(requirements or ()))

    @classmethod
    def create_from_adaptors(
        cls, adaptors: Iterable[TargetAdaptor], additional_requirements: Iterable[str] = ()
    ) -> "PexRequirements":
        all_target_requirements = set()
        for maybe_python_req_lib in adaptors:
            # This is a python_requirement()-like target.
            if hasattr(maybe_python_req_lib, "requirement"):
                all_target_requirements.add(str(maybe_python_req_lib.requirement))
            # This is a python_requirement_library()-like target.
            if hasattr(maybe_python_req_lib, "requirements"):
                for py_req in maybe_python_req_lib.requirements:
                    all_target_requirements.add(str(py_req.requirement))
        all_target_requirements.update(additional_requirements)
        return PexRequirements(all_target_requirements)

Spec = Tuple[str, str]  # e.g. (">=", "3.6")

class ParsedConstraint(NamedTuple):
    interpreter: str
    specs: FrozenSet[Spec]

    def __str__(self) -> str:
        specs = ",".join(
            f"{op}{version}" for op, version in sorted(self.specs, key=lambda spec: spec[1])
        )
        return f"{self.interpreter}{specs}"

@frozen_after_init
@dataclass(unsafe_hash=True)
class PexInterpreterConstraints:
    constraints: FrozenOrderedSet[str]

    def __init__(self, constraints: Optional[Iterable[str]] = None) -> None:
        self.constraints = FrozenOrderedSet(sorted(constraints or ()))

    @staticmethod
    def merge_constraint_sets(constraint_sets: Iterable[Iterable[str]]) -> List[str]:
        """Given a collection of constraints sets, merge by ORing within each individual constraint
        set and ANDing across each distinct constraint set.

        For example, given `[["CPython>=2.7", "CPython<=3"], ["CPython==3.6.*"]]`, return
        `["CPython>=2.7,==3.6.*", "CPython<=3,==3.6.*"]`.
        """
        # Each element (a Set[ParsedConstraint]) will get ANDed. We use sets to deduplicate
        # identical top-level parsed constraint sets.
        parsed_constraint_sets: Set[FrozenSet[ParsedConstraint]] = set()
        for constraint_set in constraint_sets:
            # Each element (a ParsedConstraint) will get ORed.
            parsed_constraint_set: Set[ParsedConstraint] = set()
            for constraint in constraint_set:
                try:
                    parsed_requirement = Requirement.parse(constraint)
                except ValueError:
                    # We allow the shorthand `>=3.7`, which gets expanded to `CPython>=3.7`. See
                    # Pex's interpreter.py's `parse_requirement()`.
                    parsed_requirement = Requirement.parse(f"CPython{constraint}")
                interpreter = parsed_requirement.project_name
                specs = frozenset(parsed_requirement.specs)
                parsed_constraint_set.add(ParsedConstraint(interpreter, specs))
            parsed_constraint_sets.add(frozenset(parsed_constraint_set))

        def and_constraints(parsed_constraints: Sequence[ParsedConstraint]) -> ParsedConstraint:
            merged_specs: Set[Spec] = set()
            expected_interpreter = parsed_constraints[0][0]
            for parsed_constraint in parsed_constraints:
                if parsed_constraint.interpreter != expected_interpreter:
                    attempted_interpreters = {
                        interpreter: sorted(
                            str(parsed_constraint) for parsed_constraint in parsed_constraints
                        )
                        for interpreter, parsed_constraints in itertools.groupby(
                            parsed_constraints,
                            key=lambda parsed_constraint: parsed_constraint.interpreter,
                        )
                    }
                    raise ValueError(
                        "Tried ANDing Python interpreter constraints with different interpreter "
                        "types. Please use only one interpreter type. Got "
                        f"{attempted_interpreters}."
                    )
                merged_specs.update(parsed_constraint.specs)
            return ParsedConstraint(expected_interpreter, frozenset(merged_specs))

        return sorted(
            {
                str(and_constraints(constraints_product))
                for constraints_product in itertools.product(*parsed_constraint_sets)
            }
        )

    @classmethod
    def create_from_compatibility_fields(
        cls, fields: Iterable[Compatibility], python_setup: PythonSetup
    ) -> "PexInterpreterConstraints":
        constraint_sets = {field.value_or_global_default(python_setup) for field in fields}
        # This will OR within each field and AND across fields.
        merged_constraints = cls.merge_constraint_sets(constraint_sets)
        return PexInterpreterConstraints(merged_constraints)

    @classmethod
    def create_from_adaptors(
        cls, adaptors: Iterable[TargetAdaptor], python_setup: PythonSetup
    ) -> "PexInterpreterConstraints":
        constraint_sets: Set[Iterable[str]] = set()
        for adaptor in adaptors:
            if not isinstance(adaptor, PythonTargetAdaptor):
                continue
            compatibility_field = Compatibility(adaptor.compatibility, address=adaptor.address)
            constraint_sets.add(compatibility_field.value_or_global_default(python_setup))
        # This will OR within each target and AND across targets.
        merged_constraints = cls.merge_constraint_sets(constraint_sets)
        return PexInterpreterConstraints(merged_constraints)

    def generate_pex_arg_list(self) -> List[str]:
        args = []
        for constraint in sorted(self.constraints):
            args.extend(["--interpreter-constraint", constraint])
        return args

@dataclass(frozen=True)
class CreatePex:
    """Represents a generic request to create a PEX from its inputs."""

    output_filename: str
    requirements: PexRequirements = PexRequirements()
    interpreter_constraints: PexInterpreterConstraints = PexInterpreterConstraints()
    entry_point: Optional[str] = None
    input_files_digest: Optional[Digest] = None
    additional_args: Tuple[str, ...] = ()

@dataclass(frozen=True)
class Pex(HermeticPex):
    """Wrapper for a digest containing a pex file created with some filename."""

    directory_digest: Digest
    output_filename: str

logger = logging.getLogger(__name__)

@dataclass(frozen=True)
class PexDebug:
    log_level: LogLevel

    _PEX_LEVEL_BY_PANTS_LEVEL = {
        LogLevel.TRACE: 9,
        LogLevel.DEBUG: 3,
    }

    @memoized_property
    def level(self) -> int:
        return self._PEX_LEVEL_BY_PANTS_LEVEL.get(self.log_level, 0)

    def iter_pex_args(self) -> Iterator[str]:
        yield "--no-emit-warnings"
        if self.level > 0:
            yield f"-{'v' * self.level}"

    @property
    def might_log(self):
        return self.level > 0

    def log(self, *args, **kwargs) -> None:
        self.log_level.log(logger, *args, **kwargs)

@rule(name="Create PEX")
async def create_pex(
    request: CreatePex,
    pex_bin: DownloadedPexBin,
    python_setup: PythonSetup,
    subprocess_encoding_environment: SubprocessEncodingEnvironment,
    pex_build_environment: PexBuildEnvironment,
    platform: Platform,
    log_level: LogLevel,
) -> Pex:
    """Returns a PEX with the given requirements, optional entry point, optional interpreter
    constraints, and optional requirement constraints."""

    argv = [
        "--output-file",
        request.output_filename,
        *request.interpreter_constraints.generate_pex_arg_list(),
        *request.additional_args,
    ]

    pex_debug = PexDebug(log_level)
    argv.extend(pex_debug.iter_pex_args())

    if python_setup.resolver_jobs:
        argv.extend(["--jobs", python_setup.resolver_jobs])

    if python_setup.manylinux:
        argv.extend(["--manylinux", python_setup.manylinux])
    else:
        argv.append("--no-manylinux")

    if request.entry_point is not None:
        argv.extend(["--entry-point", request.entry_point])

    if python_setup.requirement_constraints is not None:
        argv.extend(["--constraints", python_setup.requirement_constraints])

    source_dir_name = "source_files"
    argv.append(f"--sources-directory={source_dir_name}")

    argv.extend(request.requirements.requirements)

    constraint_file_snapshot = EMPTY_SNAPSHOT
    if python_setup.requirement_constraints is not None:
        constraint_file_snapshot = await Get[Snapshot](
            PathGlobs(
                [python_setup.requirement_constraints],
                glob_match_error_behavior=GlobMatchErrorBehavior.error,
                conjunction=GlobExpansionConjunction.all_match,
                description_of_origin="the option `--python-setup-requirement-constraints`",
            )
        )

    sources_digest = (
        request.input_files_digest if request.input_files_digest else EMPTY_DIRECTORY_DIGEST
    )
    sources_digest_as_subdir = await Get[Digest](
        DirectoryWithPrefixToAdd(sources_digest, source_dir_name)
    )

    merged_digest = await Get[Digest](
        DirectoriesToMerge(
            directories=(
                pex_bin.directory_digest,
                sources_digest_as_subdir,
                constraint_file_snapshot.directory_digest,
            )
        )
    )

    # NB: PEX outputs are platform dependent so in order to get a PEX that we can use locally, without
    # cross-building, we specify that our PEX command be run on the current local platform. When we
    # support cross-building through CLI flags we can configure requests that build a PEX for our
    # local platform that are able to execute on a different platform, but for now in order to
    # guarantee correct build we need to restrict this command to execute on the same platform type
    # that the output is intended for. The correct way to interpret the keys
    # (execution_platform_constraint, target_platform_constraint) of this dictionary is "The output of
    # this command is intended for `target_platform_constraint` iff it is run on `execution_platform
    # constraint`".
    execute_process_request = MultiPlatformExecuteProcessRequest(
        {
            (
                PlatformConstraint(platform.value),
                PlatformConstraint(platform.value),
            ): pex_bin.create_execute_request(
                python_setup=python_setup,
                subprocess_encoding_environment=subprocess_encoding_environment,
                pex_build_environment=pex_build_environment,
                pex_args=argv,
                input_files=merged_digest,
                description=f"Resolving {', '.join(request.requirements.requirements)}",
                output_files=(request.output_filename,),
            )
        }
    )

    result = await Get[ExecuteProcessResult](
        MultiPlatformExecuteProcessRequest, execute_process_request
    )

    if pex_debug.might_log:
        lines = result.stderr.decode().splitlines()
        if lines:
            pex_debug.log(f"Debug output from Pex for: {execute_process_request}")
            for line in lines:
                pex_debug.log(line)

    return Pex(
        directory_digest=result.output_directory_digest, output_filename=request.output_filename
    )

def rules():
    return [
        create_pex,
        subsystem_rule(PythonSetup),
    ]
<|endoftext|>


<commit_before>
"""
This module provides all the general functions required by dotdesk.

get_args()          -- return command line args
is_NOT_root_user()  -- return True if not root user
file_exists()       -- return True if file exists
file_type()         -- return file extension e.g. '.png'
valid_file_type()   -- return True if file type supported by dotdesk
proc_file()         -- copy, write and remove files
"""

import const

import argparse
import getpass
import shutil
import os
import sys

    # ---------------------------- session ---------------------------- #

def get_args():
    """
    Returns any command line arguments passed by the user.

    This function returns a dictionary containing the flag and program name
    passed by the user. You can access the flag using the 'flag' key and
    program name using the 'name' key.
    """
    args = sys.argv
    len_args = len(args)

    if len_args == 0:
        print "Try: 'dotdesk -h' for more info."
        sys.exit()
    if len_args > 1:
        flag = args[1]
        name = ""
    if len_args > 2:
        name = _args_to_name(args[2:])

    return {"flag":flag, "name":name}

def _args_to_name(args_list):
    """ Return a space separated string. """
    name = "".join(arg + " " for arg in args_list)
    return name.rstrip()

def is_NOT_root_user():
    """ Return True if user is not root user. """
    return getpass.getuser() != "root"

    # ------------------------------ file ----------------------------- #

def file_exists(path):
    """ Return True if this file exists. """
    return os.path.isfile(path)

def file_type(path):
    """ Return this files extension. """
    try:
        start = path.rindex(".")
        return path[start:len(path)]
    except ValueError as e:
        print "\nNo file type found!"
        return ""

def valid_file_type(path):
    """ Return True if this file type is supported by dotdesk. """
    f_type = file_type(path)
    for valid_type in const.ICON_TYPES:
        if f_type == valid_type:
            return True
    return False

def copy_file(src, dst, msg, error):
    try:
        shutil.copyfile(src, dst)
        print msg
    except IOError as e:
        print str(e)
        print error
        sys.exit()

def write_file(path, contents, msg, error):
    try:
        doc = open(path, "w")
        doc.write(contents)
        print msg
    except IOError as e:
        print str(e)
        print error
        sys.exit()
    finally:
        doc.close()

def remove_file(path, msg, error):
    try:
        os.remove(path)
        print msg
    except OSError as e:
        print str(e)
        print error
        sys.exit()

def proc_file(action, path, dest=None, cont=None, msg=None, error=None):
    """
    Write, copy and remove files.

    This function provides all the file processing functionality required by
    dotdesk.
    If an error is raised within this function it will exit the
    program; a failure here means the script cannot complete its primary
    functions of installation and removal.

    Keyword arguments:
    action -- string "w" for write, "c" for copy or "r" for remove
    path   -- string representing the path to the file to be processed
    dest   -- used by copy; string representing destination path (default None)
    cont   -- used by write; string representing content to be written (default None)
    msg    -- string printed if the action is successfully completed (default no msg)
    error  -- string printed if the action fails (default no msg)
    """
    try:
        if action in ("c", "copy"):
            shutil.copyfile(path, dest)
        elif action in ("w", "write"):
            with open(path, "w") as doc:
                doc.write(cont)
        elif action in ("r", "remove"):
            os.remove(path)
        else:
            raise Exception("Invalid arg passed to action")
        if msg is not None:
            print msg
    except Exception as e:
        if error is not None:
            print str(e)
            print error
        sys.exit()
<commit_msg>Removed all old file processing functions<commit_after>
"""
This module provides all the general functions required by dotdesk.

get_args()          -- return command line args
is_NOT_root_user()  -- return True if not root user
file_exists()       -- return True if file exists
file_type()         -- return file extension e.g. '.png'
valid_file_type()   -- return True if file type supported by dotdesk
proc_file()         -- copy, write and remove files
"""

import const

import argparse
import getpass
import shutil
import os
import sys

    # ---------------------------- session ---------------------------- #

def get_args():
    """
    Returns any command line arguments passed by the user.

    This function returns a dictionary containing the flag and program name
    passed by the user. You can access the flag using the 'flag' key and
    program name using the 'name' key.
    """
    args = sys.argv
    len_args = len(args)

    if len_args == 0:
        print "Try: 'dotdesk -h' for more info."
        sys.exit()
    if len_args > 1:
        flag = args[1]
        name = ""
    if len_args > 2:
        name = _args_to_name(args[2:])

    return {"flag":flag, "name":name}

def _args_to_name(args_list):
    """ Returns the lists contents as a space separated string. """
    name = "".join(arg + " " for arg in args_list)
    return name.rstrip()

def is_NOT_root_user():
    """ Returns True if user is not root. """
    return getpass.getuser() != "root"

    # ------------------------------ file ----------------------------- #

def file_exists(path):
    """ Returns True if this file exists. """
    return os.path.isfile(path)

def file_type(path):
    """ Returns this files extension e.g. '.png' """
    try:
        start = path.rindex(".")
        return path[start:len(path)]
    except ValueError as e:
        print "\nNo file type found!"
        return ""

def valid_file_type(path):
    """ Returns True if this file type is supported by dotdesk. """
    f_type = file_type(path)
    for valid_type in const.ICON_TYPES:
        if f_type == valid_type:
            return True
    return False

def proc_file(action, path, dest=None, cont=None, msg=None, error=None):
    """
    Write, copy and remove files.

    This function provides all the file processing functionality required by
    dotdesk.
    If an error is raised within this function it will exit the
    program; a failure here means the script cannot complete its primary
    functions of installation and removal.

    Keyword arguments:
    action -- string "w" for write, "c" for copy or "r" for remove
    path   -- string representing the path to the file to be processed
    dest   -- used by copy; string representing destination path (default None)
    cont   -- used by write; string representing content to be written (default None)
    msg    -- string printed if the action is successfully completed (default no msg)
    error  -- string printed if the action fails (default no msg)
    """
    try:
        if action in ("c", "copy"):
            shutil.copyfile(path, dest)
        elif action in ("w", "write"):
            with open(path, "w") as doc:
                doc.write(cont)
        elif action in ("r", "remove"):
            os.remove(path)
        else:
            raise Exception("Invalid arg passed to action")
        if msg is not None:
            print msg
    except Exception as e:
        if error is not None:
            print str(e)
            print error
        sys.exit()
<|endoftext|>


<commit_before>from mock import MagicMock
import pyaem
from pyaem import bagofrequests as bag
import unittest
from .util import HandlersMatcher

class TestPackageManager(unittest.TestCase):

    def test_init(self):

        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html', foo='bar')

        self.assertEqual(packagemanager.url, 'http://localhost:4502/.cqactions.html')
        self.assertEqual(packagemanager.kwargs['foo'], 'bar')

        self.assertTrue(200 in packagemanager.handlers)
        self.assertTrue(401 in packagemanager.handlers)
        self.assertTrue(405 in packagemanager.handlers)

    def test_init_ok_success(self):

        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html', foo='bar')
        handler = packagemanager.handlers[200]
        response = {'body': '{ "success": true, "msg": "some message" }'}
        result = handler(response)

        self.assertEquals(result['status'], 'success')
        self.assertEquals(result['message'], 'some message')

    def test_init_ok_failure(self):

        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html', foo='bar')
        handler = packagemanager.handlers[200]
        response = {'body': '{ "success": false, "msg": "some message" }'}
        result = handler(response)

        self.assertEquals(result['status'], 'failure')
        self.assertEquals(result['message'], 'some message')

    def test_create_package(self):

        bag.request = MagicMock()
        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html')
        packagemanager.create_package('mygroup', 'mypackage', '1.2.3', foo='bar')
        bag.request.assert_called_once_with(
            'post',
            'http://localhost:4502/.cqactions.html/crx/packmgr/service/.json/etc/packages/mypackage',
            {'packageName': 'mypackage',
             'cmd': 'create',
             'groupName': 'mygroup',
             '_charset_': 'utf-8',
             'packageVersion': '1.2.3',
             'foo': 'bar'},
            HandlersMatcher([200, 401, 405]))

    def test_update_package(self):

        bag.request = MagicMock()
        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html')
        packagemanager.update_package('mygroup', 'mypackage', '1.2.3', foo='bar')
        bag.request.assert_called_once_with(
            'get',
            'http://localhost:4502/.cqactions.html/crx/packmgr/update.jsp',
            {'packageName': 'mypackage',
             'groupName': 'mygroup',
             'version': '1.2.3',
             '_charset_': 'utf-8',
             'path': '/etc/packages/mygroup/mypackage-1.2.3.zip',
             'foo': 'bar'},
            HandlersMatcher([200, 401, 405]))

    def test_build_package(self):

        bag.request = MagicMock()
        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html')
        packagemanager.build_package('mygroup', 'mypackage', '1.2.3', foo='bar')
        bag.request.assert_called_once_with(
            'post',
            'http://localhost:4502/.cqactions.html/crx/packmgr/service/.json/etc/packages/mygroup/mypackage-1.2.3.zip',
            {'cmd': 'build',
             'foo': 'bar'},
            HandlersMatcher([200, 401, 405]))

    def test_download_package(self):

        _self = self
        class DownloadPackageHandlerMatcher(HandlersMatcher):
            def __eq__(self, handlers):
                result = handlers[200]({}, file='/tmp/somepath/mypackage-1.2.3.zip')
                _self.assertEquals(result['status'], 'success')
                _self.assertEquals(result['message'], '/tmp/somepath/mypackage-1.2.3.zip was successfully downloaded')
                return handlers.keys() == self.handler_keys

        bag.download_file = MagicMock()
        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html')
        packagemanager.download_package('mygroup', 'mypackage', '1.2.3', '/tmp/somepath', foo='bar')
        bag.download_file.assert_called_once_with(
            'http://localhost:4502/.cqactions.html/etc/packages/mygroup/mypackage-1.2.3.zip',
            {'foo': 'bar'},
            DownloadPackageHandlerMatcher([200, 401, 405]),
            file='/tmp/somepath/mypackage-1.2.3.zip')

    def test_upload_package(self):

        _self = self
        class UploadPackageHandlerMatcher(HandlersMatcher):
            def __eq__(self, handlers):
                result = handlers[200](
                    {'body': '{"success": true, "msg": "some message"}'},
                    file='/tmp/somepath/mypackage-1.2.3.zip')
                _self.assertEquals(result['status'], 'success')
                _self.assertEquals(result['message'], 'some message')
                return handlers.keys() == self.handler_keys

        bag.upload_file = MagicMock()
        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html')
        packagemanager.upload_package('mygroup', 'mypackage', '1.2.3', '/tmp/somepath', foo='bar')
        bag.upload_file.assert_called_once_with(
            'http://localhost:4502/.cqactions.html/crx/packmgr/service/.json/',
            {'cmd': 'upload',
             'foo': 'bar',
             'package': (10, '/tmp/somepath/mypackage-1.2.3.zip')},
            UploadPackageHandlerMatcher([200, 401, 405]),
            file_name='mypackage-1.2.3.zip')

    def test_install_package(self):

        bag.request = MagicMock()
        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html')
        packagemanager.install_package('mygroup', 'mypackage', '1.2.3', foo='bar')
        bag.request.assert_called_once_with(
            'post',
            'http://localhost:4502/.cqactions.html/crx/packmgr/service/.json/etc/packages/mygroup/mypackage-1.2.3.zip',
            {'cmd': 'install',
             'foo': 'bar'},
            HandlersMatcher([200, 401, 405]))

    def test_replicate_package(self):

        bag.request = MagicMock()
        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html')
        packagemanager.replicate_package('mygroup', 'mypackage', '1.2.3', foo='bar')
        bag.request.assert_called_once_with(
            'post',
            'http://localhost:4502/.cqactions.html/crx/packmgr/service/.json/etc/packages/mygroup/mypackage-1.2.3.zip',
            {'cmd': 'replicate',
             'foo': 'bar'},
            HandlersMatcher([200, 401, 405]))

    def test_delete_package(self):

        bag.request = MagicMock()
        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html')
        packagemanager.delete_package('mygroup', 'mypackage', '1.2.3', foo='bar')
        bag.request.assert_called_once_with(
            'post',
            'http://localhost:4502/.cqactions.html/crx/packmgr/service/.json/etc/packages/mygroup/mypackage-1.2.3.zip',
            {'cmd': 'delete',
             'foo': 'bar'},
            HandlersMatcher([200, 401, 405]))

if __name__ == '__main__':
    unittest.main()
    <commit_msg>Add failure status handling for upload package test.<commit_after>from mock import MagicMock
import pyaem
from pyaem import bagofrequests as bag
import unittest
from .util import HandlersMatcher

class TestPackageManager(unittest.TestCase):

    def test_init(self):

        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html', foo='bar')

        self.assertEqual(packagemanager.url, 'http://localhost:4502/.cqactions.html')
        self.assertEqual(packagemanager.kwargs['foo'], 'bar')

        self.assertTrue(200 in packagemanager.handlers)
        self.assertTrue(401 in packagemanager.handlers)
        self.assertTrue(405 in packagemanager.handlers)

    def test_init_ok_success(self):

        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html', foo='bar')
        handler = packagemanager.handlers[200]
        response = {'body': '{ "success": true, "msg": "some message" }'}
        result = handler(response)

        self.assertEquals(result['status'], 'success')
        self.assertEquals(result['message'], 'some message')

    def test_init_ok_failure(self):

        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html', foo='bar')
        handler = packagemanager.handlers[200]
        response = {'body': '{ "success": false, "msg": "some message" }'}
        result = handler(response)

        self.assertEquals(result['status'], 'failure')
        self.assertEquals(result['message'], 'some message')

    def test_create_package(self):

        bag.request = MagicMock()
        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html')
        packagemanager.create_package('mygroup', 'mypackage', '1.2.3', foo='bar')
        bag.request.assert_called_once_with(
            'post',
            'http://localhost:4502/.cqactions.html/crx/packmgr/service/.json/etc/packages/mypackage',
            {'packageName': 'mypackage',
             'cmd': 'create',
             'groupName': 'mygroup',
             '_charset_': 'utf-8',
             'packageVersion': '1.2.3',
             'foo': 'bar'},
            HandlersMatcher([200, 401, 405]))

    def test_update_package(self):

        bag.request = MagicMock()
        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html')
        packagemanager.update_package('mygroup', 'mypackage', '1.2.3', foo='bar')
        bag.request.assert_called_once_with(
            'get',
            'http://localhost:4502/.cqactions.html/crx/packmgr/update.jsp',
            {'packageName': 'mypackage',
             'groupName': 'mygroup',
             'version': '1.2.3',
             '_charset_': 'utf-8',
             'path': '/etc/packages/mygroup/mypackage-1.2.3.zip',
             'foo': 'bar'},
            HandlersMatcher([200, 401, 405]))

    def test_build_package(self):

        bag.request = MagicMock()
        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html')
        packagemanager.build_package('mygroup', 'mypackage', '1.2.3', foo='bar')
        bag.request.assert_called_once_with(
            'post',
            'http://localhost:4502/.cqactions.html/crx/packmgr/service/.json/etc/packages/mygroup/mypackage-1.2.3.zip',
            {'cmd': 'build',
             'foo': 'bar'},
            HandlersMatcher([200, 401, 405]))

    def test_download_package(self):

        _self = self
        class DownloadPackageHandlerMatcher(HandlersMatcher):
            def __eq__(self, handlers):
                result = handlers[200]({}, file='/tmp/somepath/mypackage-1.2.3.zip')
                _self.assertEquals(result['status'], 'success')
                _self.assertEquals(result['message'], '/tmp/somepath/mypackage-1.2.3.zip was successfully downloaded')
                return handlers.keys() == self.handler_keys

        bag.download_file = MagicMock()
        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html')
        packagemanager.download_package('mygroup', 'mypackage', '1.2.3', '/tmp/somepath', foo='bar')
        bag.download_file.assert_called_once_with(
            'http://localhost:4502/.cqactions.html/etc/packages/mygroup/mypackage-1.2.3.zip',
            {'foo': 'bar'},
            DownloadPackageHandlerMatcher([200, 401, 405]),
            file='/tmp/somepath/mypackage-1.2.3.zip')

    def test_upload_package_success(self):

        _self = self
        class UploadPackageHandlerMatcher(HandlersMatcher):
            def __eq__(self, handlers):
                result = handlers[200](
                    {'body': '{"success": true, "msg": "some message"}'},
                    file='/tmp/somepath/mypackage-1.2.3.zip')
                _self.assertEquals(result['status'], 'success')
                _self.assertEquals(result['message'], 'some message')
                return handlers.keys() == self.handler_keys

        bag.upload_file = MagicMock()
        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html')
        packagemanager.upload_package('mygroup', 'mypackage', '1.2.3', '/tmp/somepath', foo='bar')
        bag.upload_file.assert_called_once_with(
            'http://localhost:4502/.cqactions.html/crx/packmgr/service/.json/',
            {'cmd': 'upload',
             'foo': 'bar',
             'package': (10, '/tmp/somepath/mypackage-1.2.3.zip')},
            UploadPackageHandlerMatcher([200, 401, 405]),
            file_name='mypackage-1.2.3.zip')

    def test_upload_package_failure(self):

        _self = self
        class UploadPackageHandlerMatcher(HandlersMatcher):
            def __eq__(self, handlers):
                result = handlers[200](
                    {'body': '{"success": false, "msg": "some message"}'},
                    file='/tmp/somepath/mypackage-1.2.3.zip')
                _self.assertEquals(result['status'], 'failure')
                _self.assertEquals(result['message'], 'some message')
                return handlers.keys() == self.handler_keys

        bag.upload_file = MagicMock()
        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html')
        packagemanager.upload_package('mygroup', 'mypackage', '1.2.3', '/tmp/somepath', foo='bar')
        bag.upload_file.assert_called_once_with(
            'http://localhost:4502/.cqactions.html/crx/packmgr/service/.json/',
            {'cmd': 'upload',
             'foo': 'bar',
             'package': (10, '/tmp/somepath/mypackage-1.2.3.zip')},
            UploadPackageHandlerMatcher([200, 401, 405]),
            file_name='mypackage-1.2.3.zip')

    def test_install_package(self):

        bag.request = MagicMock()
        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html')
        packagemanager.install_package('mygroup', 'mypackage', '1.2.3', foo='bar')
        bag.request.assert_called_once_with(
            'post',
            'http://localhost:4502/.cqactions.html/crx/packmgr/service/.json/etc/packages/mygroup/mypackage-1.2.3.zip',
            {'cmd': 'install',
             'foo': 'bar'},
            HandlersMatcher([200, 401, 405]))

    def test_replicate_package(self):

        bag.request = MagicMock()
        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html')
        packagemanager.replicate_package('mygroup', 'mypackage', '1.2.3', foo='bar')
        bag.request.assert_called_once_with(
            'post',
            'http://localhost:4502/.cqactions.html/crx/packmgr/service/.json/etc/packages/mygroup/mypackage-1.2.3.zip',
            {'cmd': 'replicate',
             'foo': 'bar'},
            HandlersMatcher([200, 401, 405]))

    def test_delete_package(self):

        bag.request = MagicMock()
        packagemanager = pyaem.packagemanager.PackageManager('http://localhost:4502/.cqactions.html')
        packagemanager.delete_package('mygroup', 'mypackage', '1.2.3', foo='bar')
        bag.request.assert_called_once_with(
            'post',
            'http://localhost:4502/.cqactions.html/crx/packmgr/service/.json/etc/packages/mygroup/mypackage-1.2.3.zip',
            {'cmd': 'delete',
             'foo': 'bar'},
            HandlersMatcher([200, 401, 405]))

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


<commit_before>from sublime import Region
from sublime import set_clipboard
from sublime import status_message
from sublime_plugin import EventListener
from sublime_plugin import TextCommand
from sublime_plugin import WindowCommand

from .lib.color_scheme import ColorSchemeStyle
from .lib.generator import generate_color_scheme_assertions
from .lib.generator import generate_syntax_assertions
from .lib.runner import ColorSchemeUnit
from .lib.runner import get_color_scheme_test_params_color_scheme
from .lib.test import is_valid_color_scheme_test_file_name

class ColorSchemeUnitInsertAssertions(TextCommand):

    def run(self, edit):
        pt = self.view.sel()[0].begin()
        line = self.view.line(pt)

        self.view.insert(edit, line.end(), '\n' + generate_color_scheme_assertions(self.view, pt))

# TODO Should this helper be in its own package because it's useful for syntax devs?
class ColorSchemeUnitInsertSyntaxAssertions(TextCommand):

    def run(self, edit):
        pt = self.view.sel()[0].begin()
        line = self.view.line(pt)

        self.view.insert(edit, line.end(), '\n' + generate_syntax_assertions(self.view, pt))

class ColorSchemeUnitSetColorSchemeOnLoadEvent(EventListener):

    def on_load_async(self, view):
        if is_valid_color_scheme_test_file_name(view.file_name()):
            color_scheme = get_color_scheme_test_params_color_scheme(view)
            if color_scheme:
                view.settings().set('color_scheme', color_scheme)

class ColorSchemeUnitSetupTestFixtureCommand(TextCommand):

    def run(self, edit, content):
        self.view.erase(edit, Region(0, self.view.size()))
        self.view.insert(edit, 0, content)

class ColorSchemeUnitShowScopeNameAndStylesCommand(TextCommand):

    def run(self, edit):
        scope = self.view.scope_name(self.view.sel()[-1].b)
        style = ColorSchemeStyle(self.view).at_point(self.view.sel()[-1].b)

        style_html = '
'
        if 'foreground' in style:
            style_html += "
foreground: {0}
".format(style['foreground'].strip('#'))
            del style['foreground']
        if 'background' in style:
            style_html += "
background: {0}
".format(style['background'].strip('#'))
            del style['background']
        if 'fontStyle' in style:
            style_html += "
fontStyle: {0}
".format(style['fontStyle'].strip('#'))
            del style['fontStyle']
        for x in sorted(style):
            style_html += "
{0}: {1}
".format(x, style[x])
        style_html += '
'

        html = """

%s

                Copy
                %s

        """ % (scope.replace(' ', '
'), scope.rstrip(), style_html)

        def copy(view, text):
            set_clipboard(text)
            view.hide_popup()
            status_message('Scope name copied to clipboard')

        self.view.show_popup(html, max_width=512, max_height=700, on_navigate=lambda x: copy(self.view, x))

class ColorSchemeUnitTestSuiteCommand(WindowCommand):

    def run(self):
        ColorSchemeUnit(self.window).run()

class ColorSchemeUnitTestFileCommand(WindowCommand):

    def run(self):
        ColorSchemeUnit(self.window).run_file()

class ColorSchemeUnitTestResultsCommand(WindowCommand):

    def run(self):
        ColorSchemeUnit(self.window).results()

class ColorSchemeUnitUnitTestingCommand(WindowCommand):
    def run(self, *args, **kwargs):
        output = kwargs.get('output')
        stream = open(output, "w")

        ColorSchemeUnit(self.window).run(output=stream)
<commit_msg>Remove unused command<commit_after>from sublime import Region
from sublime import set_clipboard
from sublime import status_message
from sublime_plugin import EventListener
from sublime_plugin import TextCommand
from sublime_plugin import WindowCommand

from .lib.color_scheme import ColorSchemeStyle
from .lib.generator import generate_color_scheme_assertions
from .lib.generator import generate_syntax_assertions
from .lib.runner import ColorSchemeUnit
from .lib.runner import get_color_scheme_test_params_color_scheme
from .lib.test import is_valid_color_scheme_test_file_name

class ColorSchemeUnitInsertAssertions(TextCommand):

    def run(self, edit):
        pt = self.view.sel()[0].begin()
        line = self.view.line(pt)

        self.view.insert(edit, line.end(), '\n' + generate_color_scheme_assertions(self.view, pt))

# TODO Should this helper be in its own package because it's useful for syntax devs?
class ColorSchemeUnitInsertSyntaxAssertions(TextCommand):

    def run(self, edit):
        pt = self.view.sel()[0].begin()
        line = self.view.line(pt)

        self.view.insert(edit, line.end(), '\n' + generate_syntax_assertions(self.view, pt))

class ColorSchemeUnitSetColorSchemeOnLoadEvent(EventListener):

    def on_load_async(self, view):
        if is_valid_color_scheme_test_file_name(view.file_name()):
            color_scheme = get_color_scheme_test_params_color_scheme(view)
            if color_scheme:
                view.settings().set('color_scheme', color_scheme)

class ColorSchemeUnitSetupTestFixtureCommand(TextCommand):

    def run(self, edit, content):
        self.view.erase(edit, Region(0, self.view.size()))
        self.view.insert(edit, 0, content)

class ColorSchemeUnitShowScopeNameAndStylesCommand(TextCommand):

    def run(self, edit):
        scope = self.view.scope_name(self.view.sel()[-1].b)
        style = ColorSchemeStyle(self.view).at_point(self.view.sel()[-1].b)

        style_html = '
'
        if 'foreground' in style:
            style_html += "
foreground: {0}
".format(style['foreground'].strip('#'))
            del style['foreground']
        if 'background' in style:
            style_html += "
background: {0}
".format(style['background'].strip('#'))
            del style['background']
        if 'fontStyle' in style:
            style_html += "
fontStyle: {0}
".format(style['fontStyle'].strip('#'))
            del style['fontStyle']
        for x in sorted(style):
            style_html += "
{0}: {1}
".format(x, style[x])
        style_html += '
'

        html = """

%s

                Copy
                %s

        """ % (scope.replace(' ', '
'), scope.rstrip(), style_html)

        def copy(view, text):
            set_clipboard(text)
            view.hide_popup()
            status_message('Scope name copied to clipboard')

        self.view.show_popup(html, max_width=512, max_height=700, on_navigate=lambda x: copy(self.view, x))

class ColorSchemeUnitTestSuiteCommand(WindowCommand):

    def run(self):
        ColorSchemeUnit(self.window).run()

class ColorSchemeUnitTestFileCommand(WindowCommand):

    def run(self):
        ColorSchemeUnit(self.window).run_file()

class ColorSchemeUnitTestResultsCommand(WindowCommand):

    def run(self):
        ColorSchemeUnit(self.window).results()
<|endoftext|>


<commit_before>#!/usr/bin/env python
from EPPs.common import StepEPP, step_argparser, get_workflow_stage

class ApplyFreezerLocation(StepEPP):
    """
    Updates submitted sample UDFs for Freezer and Shelf location based on the values provided in step UDFs Freezer and Shelf
    """

    def _run(self):

        for art in self.output_artifacts:
            sample = art.samples[0]
            if  sample_container_name== self.process.udf['Container Name']:
                sample.udf['Freezer'] = self.process.udf['Freezer']
                sample.udf['Shelf'] = self.process.udf['Shelf']
                sample.put()
            else:
                raise ValueError('Container scanned does not match container in step')

def main():
    p = step_argparser()
    args = p.parse_args()
    action = ApplyFreezerLocation(args.step_uri, args.username, args.password, args.log_file)
    action.run()

if __name__ == "__main__":
    main()
<commit_msg>New script for assigning the next in User Prepared Library Plate receipt based on the answers to UDFs completed by the lab<commit_after>#!/opt/gls/clarity/users/glsai/Applications/clarity_scripts/UserPreparedLibrary20180523/bin/python3.4
from EPPs.common import StepEPP, step_argparser, get_workflow_stage

class ApplyFreezerLocation(StepEPP):
    """
    Updates submitted sample UDFs for Freezer and Shelf location based on the values provided in step UDFs Freezer and Shelf
    """

    def _run(self):

        for art in self.artifacts:
            sample = art.samples[0]
            if sample.artifact.container.name == self.process.udf['Container Name']:
                sample.udf['Freezer'] = self.process.udf['Freezer']
                sample.udf['Shelf'] = self.process.udf['Shelf']
                sample.put()

                print(art)
            else:
                print("Container '%s' not present in step" %self.process.udf['Container Name'])
                sys.exit(1)

def main():
    p = step_argparser()
    args = p.parse_args()
    action = ApplyFreezerLocation(args.step_uri, args.username, args.password, args.log_file)
    action.run()

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


<commit_before>import threading
import socket
import select
from time import time, ctime
import sys
import signal
from socket import *
import random, pickle
sock = socket(AF_INET, SOCK_STREAM)
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
sock.bind(("",8000))
sock.listen(100)

clients = []

def thread_test(newSocket):

	newSocket.sendall("GuessBS - Reverse Quizz - v0.2 \nLet\'s find someone from the BS dept. by answering a few questions!\nTo quit the game please type \'exit\'. \nEnter your nickname:")
	pseudo = newSocket.recv(1024)
	print pseudo, "connected."

# Number of questions asked
	question_count = 1
# Put questions from a file into an array
	questions_file = open('rsc/questions')
	questions = questions_file.readlines()
# ID of the current question
	question_id = random.randint(0, len(questions)-1)

# Read the score dictionary
	try:
		score_file_r = open('rsc/score', 'rb')
		score_dict = pickle.load(score_file_r)
		score_file_r.close()
	except IOError:
		print "No score file detected..."
		score_dict = {}
	except EOFError:
		print "No data detected in the score file..."
		score_dict = {}

# Key = "FirstName LastName"
# Value = {QuestionID : [Score, cardinal]}
# Complexity for n characters, p questions answered k times in average :
# Comp = O(n*p*k)

# Creating a similar structure for the character to guess
	guess_dict = {}
# Format : {QuestionID : Score}, to be compared to known characters

# Ask 8 questions
	for i in xrange(8):
# If all questions have already been asked, stop
		if question_count > len(questions):
			print "\nThat's all the questions I know, sorry..."
			break

# Chose randomly a question
		while(question_id in guess_dict):
			question_id = random.randint(0, len(questions)-1)
		actual_quest = questions[question_id]

# Ask the question
		X = actual_quest.replace('\n', '')
		newSocket.sendall('\nQuestion number #%d: \n%s' % (question_count,X,))

# Get the answer (Y/N/M)
		ans =  ""
		while (ans not in ['Y', 'N', 'M', 'y', 'n', 'm']):
			ans = newSocket.recv(1024)
# Set in uppercase
		ans = ans.upper()

		if ans == 'M':
			guess_dict[question_id] = 0
		if ans == 'Y':
			guess_dict[question_id] = 1
		if ans == 'N':
			guess_dict[question_id] = -1

# One more question has been asked
		question_count += 1

	newSocket.sendall('end')
# Comparison
# Distance from the guess vector to each known character, initialized to 0
	distance_dict = dict.fromkeys(score_dict.keys(), 0)

	for question_index, guess_score in guess_dict.iteritems():

		question_mean = 0

		answers_count = 0
		for character, character_dict in score_dict.iteritems():
			if question_index in character_dict.keys():
				answers_count += 1
				question_mean += (character_dict[question_index])[0]
		if (answers_count != 0):
			question_mean = float(question_mean)/float(answers_count)

		for character, character_dict in score_dict.iteritems():

			if question_index in character_dict.keys():

				distance_dict[character] += abs(guess_score - (character_dict[question_index])[0])
			else:

				distance_dict[character] += abs(guess_score - question_mean)

# Guessing
# Used to store the best candidate
	best_guess = "nobody"
	best_dist = 2*len(questions)
	for character, character_dist in distance_dict.iteritems():
		if character_dist < best_dist:
			best_dist = character_dist
			best_guess = character

	newSocket.sendall( "\nI think of %s, am I right ?"%best_guess)
	# Get the answer (Y/N)
	ans =  ""
	while (ans not in ['Y', 'N', 'y', 'n']):
		ans =newSocket.recv(1024)
	# Set in uppercase
	ans = ans.upper()

	if ans == 'Y':
		newSocket.sendall("Cool ! \nThanks for Playing GuessBS! \n")
		# It is not the first time the character have been chosen by the user
		# Store the answer for each question in his dictionary
		for question_id in guess_dict.keys():
		   	score_val = guess_dict[question_id]
		   	if question_id in score_dict[best_guess] :
		   		# Not the first time this question is aked about this character
		   		# Do a weighted average
		   		old_score = (score_dict[best_guess])[question_id]
		   		new_card = old_score[1] + 1
		   		(score_dict[best_guess])[question_id] = [ float(score_val+(old_score[0]*old_score[1]))/float(new_card), new_card]
		   	else :
		   		# First time this question is answered about that character
				(score_dict[best_guess])[question_id] = [score_val, 1]

	if ans == 'N':
		newSocket.sendall("Maybe next time... \nWho was he? (Please do not make any orthographic mistake...)\n")
		ans = newSocket.recv(1024)
		newSocket.sendall("Maybe next time...\nThanks for Playing GuessBS! \n")
		# Add the character of this run to the dictionary
		if ans in score_dict:
			# If it is not the first time the character have been chosen by the user
			# Store the answer for each question in his dictionary
		    for question_id in guess_dict.keys():
		    	score_val = guess_dict[question_id]
		    	if question_id in score_dict[ans] :
		    		# Not the first time this question is aked about this character
		    		# Do a weighted average
		    		old_score = (score_dict[ans])[question_id]
		    		new_card = old_score[1] + 1
		    		(score_dict[ans])[question_id] = [ float(score_val+(old_score[0]*old_score[1]))/float(new_card), new_card]
		    	else :
		    		# First time this question is answered about that character
			    	(score_dict[ans])[question_id] = [score_val, 1]
		else:
			# If it is the first time
			# Create a dictionary for this character
		    score_dict[ans] = {}
		    # Store the answer for each question in his dictionary
		    for question_id in guess_dict.keys():
		    	(score_dict[ans])[question_id] = [guess_dict[question_id], 1]

	# Write the score dictionary
	score_file_w = open('rsc/score', 'wb')
	pickle.dump(score_dict, score_file_w)
	score_file_w.close()

	# Game is done
	# Remove client from the list
	newSocket.shutdown(0)
	clients.remove(newSocket)
	print "Connection with", pseudo, "closed properly."

try:
	threads = []
	while 1:
		newSocket, address = sock.accept()
   		t = threading.Thread(target = thread_test, args = (newSocket,))
   		threads.append(t)
   		clients.append(newSocket)
   		t.start()
		print "Connection from", address
finally:
	sock.close()<commit_msg>little comment<commit_after>import threading
import socket
import select
from time import time, ctime
import sys
import signal
from socket import *
import random, pickle

#definition of socket type TCP
sock = socket(AF_INET, SOCK_STREAM)
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
sock.bind(("",8000))
sock.listen(100)

clients = []

def thread_test(newSocket):

	#Initialisation of game
	newSocket.sendall("GuessBS - Reverse Quizz - v0.2 \nLet\'s find someone from the BS dept. by answering a few questions!\nTo quit the game please type \'exit\'. \nEnter your nickname:")
	pseudo = newSocket.recv(1024)
	print pseudo, "connected."

# Number of questions asked
	question_count = 1
# Put questions from a file into an array
	questions_file = open('rsc/questions')
	questions = questions_file.readlines()
# ID of the current question
	question_id = random.randint(0, len(questions)-1)

# Read the score dictionary
	try:
		score_file_r = open('rsc/score', 'rb')
		score_dict = pickle.load(score_file_r)
		score_file_r.close()
	except IOError:
		print "No score file detected..."
		score_dict = {}
	except EOFError:
		print "No data detected in the score file..."
		score_dict = {}

# Key = "FirstName LastName"
# Value = {QuestionID : [Score, cardinal]}
# Complexity for n characters, p questions answered k times in average :
# Comp = O(n*p*k)

# Creating a similar structure for the character to guess
	guess_dict = {}
# Format : {QuestionID : Score}, to be compared to known characters

# Ask 8 questions
	for i in xrange(8):
# If all questions have already been asked, stop
		if question_count > len(questions):
			print "\nThat's all the questions I know, sorry..."
			break

# Chose randomly a question
		while(question_id in guess_dict):
			question_id = random.randint(0, len(questions)-1)
		actual_quest = questions[question_id]

# Ask the question
		X = actual_quest.replace('\n', '')
		newSocket.sendall('\nQuestion number #%d: \n%s' % (question_count,X,))

# Get the answer (Y/N/M)
		ans =  ""
		while (ans not in ['Y', 'N', 'M', 'y', 'n', 'm']):
			ans = newSocket.recv(1024)
# Set in uppercase
		ans = ans.upper()

		if ans == 'M':
			guess_dict[question_id] = 0
		if ans == 'Y':
			guess_dict[question_id] = 1
		if ans == 'N':
			guess_dict[question_id] = -1

# One more question has been asked
		question_count += 1

	newSocket.sendall('end')
# Comparison
# Distance from the guess vector to each known character, initialized to 0
	distance_dict = dict.fromkeys(score_dict.keys(), 0)

	for question_index, guess_score in guess_dict.iteritems():

		question_mean = 0

		answers_count = 0
		for character, character_dict in score_dict.iteritems():
			if question_index in character_dict.keys():
				answers_count += 1
				question_mean += (character_dict[question_index])[0]
		if (answers_count != 0):
			question_mean = float(question_mean)/float(answers_count)

		for character, character_dict in score_dict.iteritems():

			if question_index in character_dict.keys():

				distance_dict[character] += abs(guess_score - (character_dict[question_index])[0])
			else:

				distance_dict[character] += abs(guess_score - question_mean)

# Guessing
# Used to store the best candidate
	best_guess = "nobody"
	best_dist = 2*len(questions)
	for character, character_dist in distance_dict.iteritems():
		if character_dist < best_dist:
			best_dist = character_dist
			best_guess = character

	newSocket.sendall( "\nI think of %s, am I right ?"%best_guess)
	# Get the answer (Y/N)
	ans =  ""
	while (ans not in ['Y', 'N', 'y', 'n']):
		ans =newSocket.recv(1024)
	# Set in uppercase
	ans = ans.upper()

	if ans == 'Y':
		newSocket.sendall("Cool ! \nThanks for Playing GuessBS! \n")
		# It is not the first time the character have been chosen by the user
		# Store the answer for each question in his dictionary
		for question_id in guess_dict.keys():
		   	score_val = guess_dict[question_id]
		   	if question_id in score_dict[best_guess] :
		   		# Not the first time this question is aked about this character
		   		# Do a weighted average
		   		old_score = (score_dict[best_guess])[question_id]
		   		new_card = old_score[1] + 1
		   		(score_dict[best_guess])[question_id] = [ float(score_val+(old_score[0]*old_score[1]))/float(new_card), new_card]
		   	else :
		   		# First time this question is answered about that character
				(score_dict[best_guess])[question_id] = [score_val, 1]

	if ans == 'N':
		newSocket.sendall("Maybe next time... \nWho was he? (Please do not make any orthographic mistake...)\n")
		ans = newSocket.recv(1024)
		newSocket.sendall("\nThanks for Playing GuessBS! \n")
		# Add the character of this run to the dictionary
		if ans in score_dict:
			# If it is not the first time the character have been chosen by the user
			# Store the answer for each question in his dictionary
		    for question_id in guess_dict.keys():
		    	score_val = guess_dict[question_id]
		    	if question_id in score_dict[ans] :
		    		# Not the first time this question is aked about this character
		    		# Do a weighted average
		    		old_score = (score_dict[ans])[question_id]
		    		new_card = old_score[1] + 1
		    		(score_dict[ans])[question_id] = [ float(score_val+(old_score[0]*old_score[1]))/float(new_card), new_card]
		    	else :
		    		# First time this question is answered about that character
			    	(score_dict[ans])[question_id] = [score_val, 1]
		else:
			# If it is the first time
			# Create a dictionary for this character
		    score_dict[ans] = {}
		    # Store the answer for each question in his dictionary
		    for question_id in guess_dict.keys():
		    	(score_dict[ans])[question_id] = [guess_dict[question_id], 1]

	# Write the score dictionary
	score_file_w = open('rsc/score', 'wb')
	pickle.dump(score_dict, score_file_w)
	score_file_w.close()

	# Game is done
	# Remove client from the list
	newSocket.shutdown(0)
	clients.remove(newSocket)
	print "Connection with", pseudo, "closed properly."

try:
	threads = []
	while 1:
		newSocket, address = sock.accept()
   		t = threading.Thread(target = thread_test, args = (newSocket,))
   		threads.append(t)
   		clients.append(newSocket)
   		t.start()
		print "Connection from", address
finally:
	sock.close()<|endoftext|>


<commit_before>import sys
import os
import json
import base64
from PIL import Image
from PIL import ImageChops

def get_db(locale):
  json_file = open('locales/' + locale + '/db.json')
  db = json.load(json_file)
  json_file.close()
  return db

def set_db(locale, db):
  json_file = open('locales/' + locale + '/db.json', 'w')
  json.dump(db, json_file, indent = 2)
  json_file.close()

def check_duplicate (db, data_id, data_l10n, app, app_categ, screenshot_entry):
  found = False
  for entry in db[data_id]:
    if entry['l10n'] == data_l10n:
      if app in entry['position']:
        for screenshot in entry['screenshots']:
          if screenshot['id'] == screenshot_entry['id']:
            found = True
            screenshot['changed'] = screenshot_entry['changed']
            old_gaia_hash = screenshot['gaia']
            new_gaia_hash = screenshot_entry['gaia']
            old_hg_hash = screenshot['hg']
            new_hg_hash = screenshot_entry['hg']
            screenshot['old_gaia'] = old_gaia_hash
            screenshot['old_hg'] = old_hg_hash
            if old_gaia_hash != new_gaia_hash:
              screenshot['gaia'] = new_gaia_hash
            if old_hg_hash != new_hg_hash:
              screenshot['hg'] = new_hg_hash
            break
        if not found:
          entry['screenshots'] = [screenshot_entry] + entry['screenshots']
          found = True
          break
        else:
          break
  if not found:
    for entry in db[data_id]:
      if entry['l10n'] == data_l10n:
        if app_categ in entry['position']:
          for screenshot in entry['screenshots']:
            if screenshot['id'] == screenshot_entry['id']:
              found = True
              screenshot['changed'] = screenshot_entry['changed']
              old_gaia_hash = screenshot['gaia']
              new_gaia_hash = screenshot_entry['gaia']
              old_hg_hash = screenshot['hg']
              new_hg_hash = screenshot_entry['hg']
              screenshot['old_gaia'] = old_gaia_hash
              screenshot['old_hg'] = old_hg_hash
              if old_gaia_hash != new_gaia_hash:
                screenshot['gaia'] = new_gaia_hash
              if old_hg_hash != new_hg_hash:
                screenshot['hg'] = new_hg_hash
              break
          if not found:
            entry['screenshots'] = [screenshot_entry] + entry['screenshots']
            found = True
            break
          else:
            break
  if not found:
    print '\n***not added to db: ' + data_id + ' ***'

def add_screenshot_to_id(db, data_id, data_l10n, app, app_categ, screenshot_entry):
  print data_id, data_l10n.encode('utf-8'), screenshot_entry
  found = False
  if len(db[data_id]) == 1:
    for screenshot in db[data_id][0]['screenshots']:
      if screenshot['id'] == screenshot_entry['id']:
        found=True
        screenshot['changed'] = screenshot_entry['changed']
        old_gaia_hash = screenshot['gaia']
        new_gaia_hash = screenshot_entry['gaia']
        old_hg_hash = screenshot['hg']
        new_hg_hash = screenshot_entry['hg']
        screenshot['old_gaia'] = old_gaia_hash
        screenshot['old_hg'] = old_hg_hash
        if old_gaia_hash != new_gaia_hash:
          screenshot['gaia'] = new_gaia_hash
        if old_hg_hash != new_hg_hash:
          screenshot['hg'] = new_hg_hash
        break
    if not found:
      db[data_id][0]['screenshots'] = [screenshot_entry] + db[data_id][0]['screenshots']
  else:
    check_duplicate(db, data_id, data_l10n, app, app_categ, screenshot_entry)

def log_displayed_l10n_strings(client, db, app, app_categ, screenshot_entry, section=''):
  l10n_elems = client.find_elements('css selector', section + ' [data-l10n-id]')

  for i in l10n_elems:
    if i.is_displayed():
      data_id = i.get_attribute('data-l10n-id')
      data_l10n = i.text

      if db.has_key(data_id):
        add_screenshot_to_id(db, data_id, data_l10n, app, app_categ, screenshot_entry)
      else:
        if i.get_attribute('placeholder') and db.has_key(data_id + '.placeholder'):
          add_screenshot_to_id(db, data_id + '.placeholder', data_l10n, app, app_categ, screenshot_entry)
        else:
          #TO DO raise proper error
          print 'String with id "' + data_id + '" does not exist into db.json'

def create_screenshot_dir(locale, path):
  directory = 'results/screenshots/' + locale + '/' + path + '/'
  if not os.path.exists(directory):
    os.makedirs(directory)

def take_screenshot(locale, path, client, file_name):
  screenshot_name = 'results/screenshots/' + locale + '/' + path + '/' + file_name
  if os.path.exists(screenshot_name + '.jpeg'):
    os.rename(screenshot_name + '.jpeg', screenshot_name + '.old.jpeg')
  file = open(screenshot_name + '.jpeg', 'w')
  screenshot = client.screenshot()[22:]
  file.write(base64.decodestring(screenshot))
  file.close()
  if os.path.exists(screenshot_name + '.old.jpeg'):
    return screenshot_changed(screenshot_name)
  return False

def check_flags(test_flags, device_flags):
  for flag in test_flags:
    if test_flags[flag]:
      if not device_flags[flag]:
        return False
  return True

def screenshot_changed(screenshot_name):
  new = Image.open(screenshot_name + '.jpeg')
  old = Image.open(screenshot_name + '.old.jpeg')
  diff = ImageChops.difference(new, old)
  if diff.getbbox():
    diff.save(screenshot_name + '.diff.jpeg', 'JPEG')
    print "-----Screenshot " + screenshot_name + ' changed-----'
    return True
  else:
    if os.path.exists(screenshot_name + '.diff.jpeg'):
      os.remove(screenshot_name + '.diff.jpeg')
    return False

def change_screen_timeout(client):
    client.execute_script("""
      let setlock = window.wrappedJSObject.SettingsListener.getSettingsLock();
      let obj = {'screen.timeout': 0};
      setlock.set(obj);
    """)

def set_time(client):
    client.set_context(Marionette.CONTEXT_CHROME)
    # Set date at 1 Jan 2013 15:10:00 GMT
    c.execute_script("window.navigator.mozTime.set(1357053000000)")
    client.set_context(Marionette.CONTEXT_CONTENT)
<commit_msg>Import Marionette into toolkit<commit_after>import sys
import os
import json
import base64
from marionette import Marionette
from PIL import Image
from PIL import ImageChops

def get_db(locale):
  json_file = open('locales/' + locale + '/db.json')
  db = json.load(json_file)
  json_file.close()
  return db

def set_db(locale, db):
  json_file = open('locales/' + locale + '/db.json', 'w')
  json.dump(db, json_file, indent = 2)
  json_file.close()

def check_duplicate (db, data_id, data_l10n, app, app_categ, screenshot_entry):
  found = False
  for entry in db[data_id]:
    if entry['l10n'] == data_l10n:
      if app in entry['position']:
        for screenshot in entry['screenshots']:
          if screenshot['id'] == screenshot_entry['id']:
            found = True
            screenshot['changed'] = screenshot_entry['changed']
            old_gaia_hash = screenshot['gaia']
            new_gaia_hash = screenshot_entry['gaia']
            old_hg_hash = screenshot['hg']
            new_hg_hash = screenshot_entry['hg']
            screenshot['old_gaia'] = old_gaia_hash
            screenshot['old_hg'] = old_hg_hash
            if old_gaia_hash != new_gaia_hash:
              screenshot['gaia'] = new_gaia_hash
            if old_hg_hash != new_hg_hash:
              screenshot['hg'] = new_hg_hash
            break
        if not found:
          entry['screenshots'] = [screenshot_entry] + entry['screenshots']
          found = True
          break
        else:
          break
  if not found:
    for entry in db[data_id]:
      if entry['l10n'] == data_l10n:
        if app_categ in entry['position']:
          for screenshot in entry['screenshots']:
            if screenshot['id'] == screenshot_entry['id']:
              found = True
              screenshot['changed'] = screenshot_entry['changed']
              old_gaia_hash = screenshot['gaia']
              new_gaia_hash = screenshot_entry['gaia']
              old_hg_hash = screenshot['hg']
              new_hg_hash = screenshot_entry['hg']
              screenshot['old_gaia'] = old_gaia_hash
              screenshot['old_hg'] = old_hg_hash
              if old_gaia_hash != new_gaia_hash:
                screenshot['gaia'] = new_gaia_hash
              if old_hg_hash != new_hg_hash:
                screenshot['hg'] = new_hg_hash
              break
          if not found:
            entry['screenshots'] = [screenshot_entry] + entry['screenshots']
            found = True
            break
          else:
            break
  if not found:
    print '\n***not added to db: ' + data_id + ' ***'

def add_screenshot_to_id(db, data_id, data_l10n, app, app_categ, screenshot_entry):
  print data_id, data_l10n.encode('utf-8'), screenshot_entry
  found = False
  if len(db[data_id]) == 1:
    for screenshot in db[data_id][0]['screenshots']:
      if screenshot['id'] == screenshot_entry['id']:
        found=True
        screenshot['changed'] = screenshot_entry['changed']
        old_gaia_hash = screenshot['gaia']
        new_gaia_hash = screenshot_entry['gaia']
        old_hg_hash = screenshot['hg']
        new_hg_hash = screenshot_entry['hg']
        screenshot['old_gaia'] = old_gaia_hash
        screenshot['old_hg'] = old_hg_hash
        if old_gaia_hash != new_gaia_hash:
          screenshot['gaia'] = new_gaia_hash
        if old_hg_hash != new_hg_hash:
          screenshot['hg'] = new_hg_hash
        break
    if not found:
      db[data_id][0]['screenshots'] = [screenshot_entry] + db[data_id][0]['screenshots']
  else:
    check_duplicate(db, data_id, data_l10n, app, app_categ, screenshot_entry)

def log_displayed_l10n_strings(client, db, app, app_categ, screenshot_entry, section=''):
  l10n_elems = client.find_elements('css selector', section + ' [data-l10n-id]')

  for i in l10n_elems:
    if i.is_displayed():
      data_id = i.get_attribute('data-l10n-id')
      data_l10n = i.text

      if db.has_key(data_id):
        add_screenshot_to_id(db, data_id, data_l10n, app, app_categ, screenshot_entry)
      else:
        if i.get_attribute('placeholder') and db.has_key(data_id + '.placeholder'):
          add_screenshot_to_id(db, data_id + '.placeholder', data_l10n, app, app_categ, screenshot_entry)
        else:
          #TO DO raise proper error
          print 'String with id "' + data_id + '" does not exist into db.json'

def create_screenshot_dir(locale, path):
  directory = 'results/screenshots/' + locale + '/' + path + '/'
  if not os.path.exists(directory):
    os.makedirs(directory)

def take_screenshot(locale, path, client, file_name):
  screenshot_name = 'results/screenshots/' + locale + '/' + path + '/' + file_name
  if os.path.exists(screenshot_name + '.jpeg'):
    os.rename(screenshot_name + '.jpeg', screenshot_name + '.old.jpeg')
  file = open(screenshot_name + '.jpeg', 'w')
  screenshot = client.screenshot()[22:]
  file.write(base64.decodestring(screenshot))
  file.close()
  if os.path.exists(screenshot_name + '.old.jpeg'):
    return screenshot_changed(screenshot_name)
  return False

def check_flags(test_flags, device_flags):
  for flag in test_flags:
    if test_flags[flag]:
      if not device_flags[flag]:
        return False
  return True

def screenshot_changed(screenshot_name):
  new = Image.open(screenshot_name + '.jpeg')
  old = Image.open(screenshot_name + '.old.jpeg')
  diff = ImageChops.difference(new, old)
  if diff.getbbox():
    diff.save(screenshot_name + '.diff.jpeg', 'JPEG')
    print "-----Screenshot " + screenshot_name + ' changed-----'
    return True
  else:
    if os.path.exists(screenshot_name + '.diff.jpeg'):
      os.remove(screenshot_name + '.diff.jpeg')
    return False

def change_screen_timeout(client):
    client.execute_script("""
      let setlock = window.wrappedJSObject.SettingsListener.getSettingsLock();
      let obj = {'screen.timeout': 0};
      setlock.set(obj);
    """)

def set_time(client):
    client.set_context(Marionette.CONTEXT_CHROME)
    # Set date at 1 Jan 2013 15:10:00 GMT
    c.execute_script("window.navigator.mozTime.set(1357053000000)")
    client.set_context(Marionette.CONTEXT_CONTENT)
<|endoftext|>


<commit_before>#/usr/bin/env python3

'''
Utilidades para reconocimiento de parámetros de ecuaciones
'''
from numpy import *
ln = log
prop = log #Solo importa el nombre de la función para comprobación...

def esalfanum(caracter):
    '''Verifica que el caracter es alfanumérico'''
    suma = 0
    suma += (caracter >= '\x30' and caracter <= '\x39')
    suma += (caracter >= '\x41' and caracter <= '\x5a')
    suma += (caracter >= '\x61' and caracter <= '\x7a')
    return bool(suma)

def buscainds(texto, busq):
    '''Busca los índices de busq dentro de texto'''
    index = []
    end = 0
    res = 0
    while res != -1:
        res = texto.find(busq, end)
        end = res + 1
        index.append(res)
    index.pop(-1)
    return index

def probar(texto_var):
    '''
    Devuelve si texto_var es o no una variable del sistema de ecuaciones
    '''
    try:
        eval(texto_var)
        return False
    except:
        return True

def variables(texto_eqn):
    '''
    Regresa en texto las variables del string texto_eqn
    '''
    #TODO
    #Reconocer lo que hay entre paréntesis
    #Cambiar todos los nombres de las funciones por números
    #   Buscar las posiciones de "("
    #   Comprobar que el término anterior es número o letra
    #   Retroceder hasta el símbolo anterior de operación o "(" y cortar
    #Cambiar todos los paréntesis por +'s
    #Separar lo que esté separado por símbolos de operación:
    #   + - * / **
    #Cambiar todos los símbolos por +'s
    #Ahora comprobar con la función probar() cada uno de los elementos separados
    #por los +'s
    #Ir agregando los válidos siempre que no se repitan a la lista de salida
    #Return esa lista
    texto_eqn = '1*' + texto_eqn
    #print(texto_eqn)
    parent_abrir = buscainds(texto_eqn, '(') #Índices donde hay paréntesis open
    #print(parent_abrir)
    cambios = [] #Lista con los cambios a realizar
    for posicion in parent_abrir:
        if posicion > 0:
            #Esto para evitar que se devuelva en la búsqueda
            pos_check = posicion - 1
            if esalfanum(texto_eqn[pos_check]):
                #Se comprueba que es una función
                EsParada = False #¿Finalizó el nombre de la función?
                while EsParada == False:
                    #Mientras no sea el comienzo del nombre de la función:
                    pos_check -= 1
                    if pos_check == 0:
                        EsParada == True
                        start_check = 0
                    else:
                        EsParada = not esalfanum(texto_eqn[pos_check])
                        start_check = pos_check + 1
                cambios.append(texto_eqn[start_check:posicion])
    #Ahora se efectúan todos los cambios:
    for cambio in cambios:
        texto_eqn = texto_eqn.replace(cambio, '1+')
    #Reemplazos:
    A_reemplazar = ['(', ')', '-', '*', '/', '^', ',']
    for termino in A_reemplazar:
        texto_eqn = texto_eqn.replace(termino, '+')
    posibles = texto_eqn.split('+')
    #Verificar que cada término es una variable del sistema de ecuaciones
    salida = []
    for variable in posibles:
        if variable != '':
            if probar(variable) == True:
                if variable not in salida:
                    salida.append(variable)

    return salida
<commit_msg>Corrección para soportar exponentes negativos notación científica<commit_after>#/usr/bin/env python3

'''
Utilidades para reconocimiento de parámetros de ecuaciones
'''
from numpy import *
ln = log
prop = log #Solo importa el nombre de la función para comprobación...

def ajustes(texto):
    '''
    Ajusta el contenido del texto de cada ecuación por si se necesitan hacer
    arreglos, tales como soporte para números en notación científica con
    exponentes negativos
    '''
    pos_e_menos = buscainds(texto, 'e-')
    for posicion in pos_e_menos:
        #Verificar que lo que hay antes y después del e- son números.
        t1 = texto[posicion - 1]
        t2 = texto[posicion + 2]
        try:
            int(t1) #Comprueba que son números
            int(t2) #Si no, entonces es excepción y chao
            texto = texto.replace(t1 + 'e-' + t2,t1 + 'e ' + t2)
        except:
            pass
    texto = texto.replace('e ', 'e')
    return texto

def esalfanum(caracter):
    '''Verifica que el caracter es alfanumérico'''
    suma = 0
    suma += (caracter >= '\x30' and caracter <= '\x39')
    suma += (caracter >= '\x41' and caracter <= '\x5a')
    suma += (caracter >= '\x61' and caracter <= '\x7a')
    return bool(suma)

def buscainds(texto, busq):
    '''Busca los índices de busq dentro de texto'''
    index = []
    end = 0
    res = 0
    while res != -1:
        res = texto.find(busq, end)
        end = res + 1
        index.append(res)
    index.pop(-1)
    return index

def probar(texto_var):
    '''
    Devuelve si texto_var es o no una variable del sistema de ecuaciones
    '''
    try:
        eval(texto_var)
        return False
    except:
        return True

def variables(texto_eqn):
    '''
    Regresa en texto las variables del string texto_eqn
    '''
    #TODO
    #Reconocer lo que hay entre paréntesis
    #Cambiar todos los nombres de las funciones por números
    #   Buscar las posiciones de "("
    #   Comprobar que el término anterior es número o letra
    #   Retroceder hasta el símbolo anterior de operación o "(" y cortar
    #Cambiar todos los paréntesis por +'s
    #Separar lo que esté separado por símbolos de operación:
    #   + - * / **
    #Cambiar todos los símbolos por +'s
    #Ahora comprobar con la función probar() cada uno de los elementos separados
    #por los +'s
    #Ir agregando los válidos siempre que no se repitan a la lista de salida
    #Return esa lista
    texto_eqn = '1*' + texto_eqn
    texto_eqn = ajustes(texto_eqn)
    #print(texto_eqn)
    parent_abrir = buscainds(texto_eqn, '(') #Índices donde hay paréntesis open
    #print(parent_abrir)
    cambios = [] #Lista con los cambios a realizar
    for posicion in parent_abrir:
        if posicion > 0:
            #Esto para evitar que se devuelva en la búsqueda
            pos_check = posicion - 1
            if esalfanum(texto_eqn[pos_check]):
                #Se comprueba que es una función
                EsParada = False #¿Finalizó el nombre de la función?
                while EsParada == False:
                    #Mientras no sea el comienzo del nombre de la función:
                    pos_check -= 1
                    if pos_check == 0:
                        EsParada == True
                        start_check = 0
                    else:
                        EsParada = not esalfanum(texto_eqn[pos_check])
                        start_check = pos_check + 1
                cambios.append(texto_eqn[start_check:posicion])
    #Ahora se efectúan todos los cambios:
    for cambio in cambios:
        texto_eqn = texto_eqn.replace(cambio, '1+')
    #Reemplazos:
    A_reemplazar = ['(', ')', '-', '*', '/', '^', ',']
    for termino in A_reemplazar:
        texto_eqn = texto_eqn.replace(termino, '+')
    posibles = texto_eqn.split('+')
    #Verificar que cada término es una variable del sistema de ecuaciones
    salida = []
    for variable in posibles:
        if variable != '':
            if probar(variable) == True:
                if variable not in salida:
                    salida.append(variable)

    return salida
<|endoftext|>