<commit_before>"""Add Dockerfile instructions to install ANTs.

Project repository: https://github.com/stnava/ANTs/

ANTs recommends building from source. Versions 2.0.0 and newer are available
on GitHub, and older versions are available on SourceForge.

Build from source (takes approximately 40 minutes):
https://github.com/stnava/ANTs/wiki/Compiling-ANTs-on-Linux-and-Mac-OS
"""
# Author: Jakub Kaczmarzyk <jakubk@mit.edu>
from __future__ import absolute_import, division, print_function
import logging

from neurodocker.utils import indent, manage_pkgs

logger = logging.getLogger(__name__)

class ANTs(object):
    """Add Dockerfile instructions to install ANTs. Versions 2.x are
    supported.

    Inspired by the Dockerfile at https://hub.docker.com/r/nipype/workshops/
    `docker pull nipype/workshops:latest-nofsspm`

    Parameters
    ----------
    version : str
        ANTs version.
    pkg_manager : {'apt', 'yum'}
        Linux package manager.
    use_binaries : bool
        If true, uses pre-compiled ANTs binaries. If false, attempts to build
        from source.
    check_urls : bool
        If true, throw warning if a URL used by this class responds with a
        status code greater than 400.
    """
    VERSION_HASHES = {"latest": None,
                      "2.2.0": "0740f9111e5a9cd4768323dc5dfaa7c29481f9ef",
                      "2.1.0": "78931aa6c4943af25e0ee0644ac611d27127a01e",
                      "2.1.0rc3": "465cc8cdf0f8cc958edd2d298e05cc2d7f8a48d8",
                      "2.1.0rc2": "17160f72f5e1c9d6759dd32b7f2dc0b36ded338b",
                      "2.1.0rc1": "1593a7777d0e6c8be0b9462012328bde421510b9",
                      "2.0.3": "c9965390c1a302dfa9e63f6ca3cb88f68aab329f",
                      "2.0.2": "7b83036c987e481b2a04490b1554196cb2fc0dab",
                      "2.0.1": "dd23c394df9292bae4c5a4ece3023a7571791b7d",
                      "2.0.0": "7ae1107c102f7c6bcffa4df0355b90c323fcde92",
                      "HoneyPot": "7ae1107c102f7c6bcffa4df0355b90c323fcde92",}

    VERSION_TARBALLS = {"2.1.0": "https://www.dropbox.com/s/x7eyk125bhwiisu/ants-2.1.0_centos-5.tar.gz?dl=1",
                        "2.0.3": "https://www.dropbox.com/s/09yd0jbohcwl24z/ants-2.0.3_centos-5.tar.gz?dl=1",}

    def __init__(self, version, pkg_manager, use_binaries=True,
                 check_urls=True):
        self.version = version
        self.pkg_manager = pkg_manager
        self.use_binaries = use_binaries
        self.check_urls = check_urls

        self.cmd = self._create_cmd()

    def _create_cmd(self):
        """Return full command to install ANTs."""
        comment = ("#-------------------\n"
                   "# Install ANTs {}\n"
                   "#-------------------".format(self.version))
        if self.use_binaries:
            chunks = [comment, self.install_binaries()]
        else:
            chunks = [comment, self.build_from_source_github()]
        return "\n".join(chunks)

    def install_binaries(self):
        try:
            url = ANTs.VERSION_TARBALLS[self.version]
        except KeyError:
            raise ValueError("No tarball for version {}".format(self.version))

        workdir_cmd = "WORKDIR /opt"
        cmd = ("URL={url}\n"
               "&& curl -sSL $URL | tar zx\n".format(url=url))
        cmd = indent("RUN", cmd)
        # Requires two ENV instructions because the second uses one defined in
        # the first.
        env_cmd = ("ANTSPATH=/opt/ants\n"
                   "PATH=/opt/ants:$PATH")
        env_cmd = indent("ENV", env_cmd)
        return "\n".join((workdir_cmd, cmd, env_cmd))

    def build_from_source_github(self):
        """Return Dockerfile instructions to build ANTs from source."""
        pkgs = {'apt': 'cmake g++ gcc git make zlib1g-dev',
                'yum': 'cmake gcc-c++ git make zlib-devel'}

        if self.version == "latest":
            checkout = ""
        else:
            version_hash = ANTs.VERSION_HASHES[self.version]
            checkout = "&& git checkout {}\n".format(version_hash)

        workdir_cmd = "WORKDIR /tmp/ants-build"
        cmd = ("deps='{pkgs}'\n"
               "&& {install}\n"
               "&& {clean}\n"
               "&& git clone https://github.com/stnava/ANTs.git\n"
               "&& cd ANTs\n"
               "{checkout}"
               "&& cd .. && mkdir build && cd build\n"
               "&& cmake ../ANTs && make -j 2\n"
               "&& mkdir -p /opt/ants\n"
               "&& mv bin/* /opt/ants && mv ../ANTs/Scripts/* /opt/ants\n"
               "&& cd /tmp && rm -rf ants-build\n"
               "&& {remove}"
               "".format(pkgs=pkgs[self.pkg_manager], checkout=checkout,
                         **manage_pkgs[self.pkg_manager]))
        cmd = cmd.format(pkgs='$deps')
        cmd = indent("RUN", cmd)

        env_cmd = ("ENV ANTSPATH=/opt/ants\n"
                   "ENV PATH=$ANTSPATH:$PATH")

        return "\n".join((workdir_cmd, cmd, env_cmd))
<commit_msg>add option for custom binaries url; remove layer in dockerfile<commit_after>"""Add Dockerfile instructions to install ANTs.

Project repository: https://github.com/stnava/ANTs/

ANTs recommends building from source. Jakub Kaczmarzyk build several versions
on CentOS 6 Docker images. Those Docker images are located at
https://hub.docker.com/r/kaczmarj/ants/ and the binaries are on Dropbox. See
the ANTs class definition for the Dropbox URLs.

Source for ANTs versions 2.0.0 and newer are available on GitHub, and older
versions are available on SourceForge.

Instructions to build from source (takes approximately 40 minutes):
https://github.com/stnava/ANTs/wiki/Compiling-ANTs-on-Linux-and-Mac-OS
"""
# Author: Jakub Kaczmarzyk <jakubk@mit.edu>

from __future__ import absolute_import, division, print_function

from neurodocker.utils import check_url, indent, manage_pkgs

class ANTs(object):
    """Add Dockerfile instructions to install ANTs. Versions 2.0.0 and newer
    are supported. Pre-compiled binaries can be downloaded, or ANTs can be
    built from source. The pre-compiled binaries were compiled on a CentOS 6
    Docker image. The build log file is present in the ANTs tarball.

    Inspired by the Dockerfile at https://hub.docker.com/r/nipype/workshops/
    `docker pull nipype/workshops:latest-nofsspm`

    Parameters
    ----------
    version : str
        ANTs version.
    pkg_manager : {'apt', 'yum'}
        Linux package manager.
    use_binaries : bool, str
        If true, uses pre-compiled ANTs binaries. If false, attempts to build
        from source. Can also be URL of tarball containing binaries. If this is
        specified, overrides the default binaries for the specified version.
        The binaries must be in the folder `ants`, as ANTSPATH=`/opt/ants` and
        the tarball will be decompressed in /opt. URL must begin with http or
        ftp.
    git_hash : str
        If this is specified and use_binaries is false, build from source from
        this commit. If this is not specified and use_binaries is false, will
        use git hash of the specified version.
    check_urls : bool
        If true, raise error if a URL used by this class responds with a status
        code greater than 400.
    """
    VERSION_HASHES = {"latest": None,
                      "2.2.0": "0740f9111e5a9cd4768323dc5dfaa7c29481f9ef",
                      "2.1.0": "78931aa6c4943af25e0ee0644ac611d27127a01e",
                      "2.1.0rc3": "465cc8cdf0f8cc958edd2d298e05cc2d7f8a48d8",
                      "2.1.0rc2": "17160f72f5e1c9d6759dd32b7f2dc0b36ded338b",
                      "2.1.0rc1": "1593a7777d0e6c8be0b9462012328bde421510b9",
                      "2.0.3": "c9965390c1a302dfa9e63f6ca3cb88f68aab329f",
                      "2.0.2": "7b83036c987e481b2a04490b1554196cb2fc0dab",
                      "2.0.1": "dd23c394df9292bae4c5a4ece3023a7571791b7d",
                      "2.0.0": "7ae1107c102f7c6bcffa4df0355b90c323fcde92",
                      "HoneyPot": "7ae1107c102f7c6bcffa4df0355b90c323fcde92",}

    VERSION_TARBALLS = {"2.1.0": "https://www.dropbox.com/s/x7eyk125bhwiisu/ants-2.1.0_centos-5.tar.gz?dl=1",
                        "2.0.3": "https://www.dropbox.com/s/09yd0jbohcwl24z/ants-2.0.3_centos-5.tar.gz?dl=1",}

    def __init__(self, version, pkg_manager, use_binaries=True, git_hash=None,
                 check_urls=True):
        self.version = version
        self.pkg_manager = pkg_manager
        self.use_binaries = use_binaries
        self.git_hash = git_hash
        self.check_urls = check_urls

        self.cmd = self._create_cmd()

    def _create_cmd(self):
        """Return full command to install ANTs."""
        comment = ("#-------------------\n"
                   "# Install ANTs {}\n"
                   "#-------------------".format(self.version))

        if self.use_binaries:
            chunks = [comment, self.install_binaries()]
        else:
            chunks = [comment, self.build_from_source_github()]

        return "\n".join(chunks)

    def install_binaries(self):
        """Return command to download and install ANTs binaries. Supports
        custom URL with tarball of binaries.
        """
        bin_str = str(self.use_binaries)
        if bin_str.startswith("http") or bin_str.startswith("ftp"):
            url = self.use_binaries
        else:
            try:
                url = ANTs.VERSION_TARBALLS[self.version]
            except KeyError:
                raise ValueError("Tarball not available for version {}."
                                 "".format(self.version))

        if self.check_urls:
            check_url(url)

        cmd = ("RUN curl -sSL {} | tar zx -C /opt".format(url))

        env_cmd = ("ANTSPATH=/opt/ants"
                   "\nPATH=/opt/ants:$PATH")
        env_cmd = indent("ENV", env_cmd)

        return "\n".join((cmd, env_cmd))

    def build_from_source_github(self):
        """Return Dockerfile instructions to build ANTs from source. Checkout
        to commit based on git_hash or version. If 'latest', build from master.
        """
        pkgs = {'apt': 'cmake g++ gcc git make zlib1g-dev',
                'yum': 'cmake gcc-c++ git make zlib-devel'}

        if self.git_hash is None:
            self.git_hash = ANTs.VERSION_HASHES[self.version]

        if self.version == "latest":
            checkout = ""
        else:
            checkout = ("\n&& cd ANTs"
                        "\n&& git checkout {}"
                        "\n&& cd .."
                        "".format(self.git_hash))

        workdir_cmd = "WORKDIR /tmp/ants-build"
        cmd = ("deps='{pkgs}'"
               "\n&& {install}"
               "\n&& {clean}"
               "\n&& git clone https://github.com/stnava/ANTs.git"
               "{checkout}"
               "\n&& mkdir build && cd build"
               "\n&& cmake ../ANTs && make -j 1"
               "\n&& mkdir -p /opt/ants"
               "\n&& mv bin/* /opt/ants && mv ../ANTs/Scripts/* /opt/ants"
               "\n&& rm -rf /tmp/*"
               "\n&& {remove}"
               "".format(pkgs=pkgs[self.pkg_manager], checkout=checkout,
                         **manage_pkgs[self.pkg_manager]))
        cmd = cmd.format(pkgs='$deps')
        cmd = indent("RUN", cmd)

        env_cmd = ("ANTSPATH=/opt/ants\n"
                   "PATH=/opt/ants:$PATH")
        env_cmd = indent("ENV", env_cmd)

        return "\n".join((workdir_cmd, cmd, env_cmd))
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

# OpenFisca -- A versatile microsimulation software
# By: OpenFisca Team <contact@openfisca.fr>
#
# Copyright (C) 2011, 2012, 2013, 2014 OpenFisca Team
# https://github.com/openfisca
#
# This file is part of OpenFisca.
#
# OpenFisca is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# OpenFisca is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import collections

class Simulation(object):
    compact_legislation = None
    date = None
    debug = False
    debug_all = False  # When False, log only formula calls with non-default parameters.
    default_compact_legislation = None
    entity_by_column_name = None
    entity_by_key_plural = None
    entity_by_key_singular = None
    persons = None
    steps_count = 1
    tax_benefit_system = None
    trace = False
    traceback = None

    def __init__(self, compact_legislation = None, date = None, debug = False, debug_all = False,
            default_compact_legislation = None, tax_benefit_system = None, trace = False):
        assert date is not None
        self.date = date
        if debug:
            self.debug = True
        if debug_all:
            assert debug
            self.debug_all = True
        assert tax_benefit_system is not None
        self.tax_benefit_system = tax_benefit_system
        if trace:
            self.trace = True
            self.traceback = collections.OrderedDict()

        self.compact_legislation = compact_legislation \
            if compact_legislation is not None \
            else tax_benefit_system.get_compact_legislation(date)

        self.default_compact_legislation = default_compact_legislation \
            if default_compact_legislation is not None \
            else tax_benefit_system.get_compact_legislation(date)

        if tax_benefit_system.preprocess_legislation_parameters is not None:
            tax_benefit_system.preprocess_legislation_parameters(self.compact_legislation)
            tax_benefit_system.preprocess_legislation_parameters(self.default_compact_legislation)

        self.entity_by_key_plural = dict(
            (key_plural, entity_class(simulation = self))
            for key_plural, entity_class in tax_benefit_system.entity_class_by_key_plural.iteritems()
            )
        self.entity_by_column_name = dict(
            (column_name, entity)
            for entity in self.entity_by_key_plural.itervalues()
            for column_name in entity.column_by_name.iterkeys()
            )
        self.entity_by_key_singular = dict(
            (entity.key_singular, entity)
            for entity in self.entity_by_key_plural.itervalues()
            )
        for entity in self.entity_by_key_plural.itervalues():
            if entity.is_persons_entity:
                self.persons = entity
                break

    def calculate(self, column_name, lazy = False, requested_formulas = None):
        return self.compute(column_name, lazy = lazy, requested_formulas = requested_formulas).array

    def compute(self, column_name, lazy = False, requested_formulas = None):
        return self.entity_by_column_name[column_name].compute(
            column_name,
            lazy = lazy,
            requested_formulas = requested_formulas,
            )

    def get_holder(self, column_name, default = UnboundLocalError):
        entity = self.entity_by_column_name[column_name]
        if default is UnboundLocalError:
            return entity.holder_by_name[column_name]
        return entity.holder_by_name.get(column_name, default)

    def get_or_new_holder(self, column_name):
        entity = self.entity_by_column_name[column_name]
        return entity.get_or_new_holder(column_name)

    def graph(self, column_name, edges, nodes, visited):
        self.entity_by_column_name[column_name].graph(column_name, edges, nodes, visited)
<commit_msg>Adding tax rate computation<commit_after># -*- coding: utf-8 -*-

# OpenFisca -- A versatile microsimulation software
# By: OpenFisca Team <contact@openfisca.fr>
#
# Copyright (C) 2011, 2012, 2013, 2014 OpenFisca Team
# https://github.com/openfisca
#
# This file is part of OpenFisca.
#
# OpenFisca is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# OpenFisca is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import collections

class Simulation(object):
    compact_legislation = None
    date = None
    debug = False
    debug_all = False  # When False, log only formula calls with non-default parameters.
    default_compact_legislation = None
    entity_by_column_name = None
    entity_by_key_plural = None
    entity_by_key_singular = None
    persons = None
    steps_count = 1
    tax_benefit_system = None
    trace = False
    traceback = None

    def __init__(self, compact_legislation = None, date = None, debug = False, debug_all = False,
            default_compact_legislation = None, tax_benefit_system = None, trace = False):
        assert date is not None
        self.date = date
        if debug:
            self.debug = True
        if debug_all:
            assert debug
            self.debug_all = True
        assert tax_benefit_system is not None
        self.tax_benefit_system = tax_benefit_system
        if trace:
            self.trace = True
            self.traceback = collections.OrderedDict()

        self.compact_legislation = compact_legislation \
            if compact_legislation is not None \
            else tax_benefit_system.get_compact_legislation(date)

        self.default_compact_legislation = default_compact_legislation \
            if default_compact_legislation is not None \
            else tax_benefit_system.get_compact_legislation(date)

        if tax_benefit_system.preprocess_legislation_parameters is not None:
            tax_benefit_system.preprocess_legislation_parameters(self.compact_legislation)
            tax_benefit_system.preprocess_legislation_parameters(self.default_compact_legislation)

        self.entity_by_key_plural = dict(
            (key_plural, entity_class(simulation = self))
            for key_plural, entity_class in tax_benefit_system.entity_class_by_key_plural.iteritems()
            )
        self.entity_by_column_name = dict(
            (column_name, entity)
            for entity in self.entity_by_key_plural.itervalues()
            for column_name in entity.column_by_name.iterkeys()
            )
        self.entity_by_key_singular = dict(
            (entity.key_singular, entity)
            for entity in self.entity_by_key_plural.itervalues()
            )
        for entity in self.entity_by_key_plural.itervalues():
            if entity.is_persons_entity:
                self.persons = entity
                break

    def calculate(self, column_name, lazy = False, requested_formulas = None):
        return self.compute(column_name, lazy = lazy, requested_formulas = requested_formulas).array

    def compute(self, column_name, lazy = False, requested_formulas = None):
        return self.entity_by_column_name[column_name].compute(
            column_name,
            lazy = lazy,
            requested_formulas = requested_formulas,
            )

    def get_holder(self, column_name, default = UnboundLocalError):
        entity = self.entity_by_column_name[column_name]
        if default is UnboundLocalError:
            return entity.holder_by_name[column_name]
        return entity.holder_by_name.get(column_name, default)

    def get_or_new_holder(self, column_name):
        entity = self.entity_by_column_name[column_name]
        return entity.get_or_new_holder(column_name)

    def graph(self, column_name, edges, nodes, visited):
        self.entity_by_column_name[column_name].graph(column_name, edges, nodes, visited)

def average_tax_rate(simulation, target_column_name = None, varying_column_name = None):
    target = simulation.calculate(target_column_name)    # numerator
    varying = simulation.calculate(varying_column_name)  # denominator
    div = varying * (varying != 0) + (varying == 0)
    average_rate = (1 - target / div)
    return average_rate

def marginal_tax_rate(simulation, target_column_name = None, varying_column_name = None):
    target = simulation.calculate(target_column_name)    # numerator
    varying = simulation.calculate(varying_column_name)  # denominator
    marginal_rate = (1 - (target[:-1] - target[1:]) / (varying[:-1] - varying[1:]))
    return marginal_rate
<|endoftext|>


<commit_before># -*- coding:utf-8 -*-
#
# This file is part of OpenFisca.
# OpenFisca is a socio-fiscal microsimulation software
# Copyright © 2011 Clément Schaff, Mahdi Ben Jelloul
# Licensed under the terms of the GPL (version 3 or later) license
# (see openfisca/__init__.py for details)

from __future__ import division

from numpy import (zeros, maximum as max_, minimum as min_, logical_not as not_)

from openfisca_core.accessors import law
from openfisca_core.columns import BoolCol, FloatCol
from openfisca_core.formulas import SimpleFormulaColumn

from .base import QUIFAM, QUIFOY, reference_formula
from ..entities import Familles, Individus

CHEF = QUIFAM['chef']
PART = QUIFAM['part']
ENFS = [QUIFAM['enf1'], QUIFAM['enf2'], QUIFAM['enf3'], QUIFAM['enf4'], QUIFAM['enf5'], QUIFAM['enf6'], QUIFAM['enf7'], QUIFAM['enf8'], QUIFAM['enf9'], ]
VOUS = QUIFOY['vous']

def _acs_montant__2009(self, age_holder):
    '''
    Calcule le montant de l'ACS en cas d'éligibilité (jusqu'au 31 juillet 2009)
    '''
    # TODO
    ages = self.filter_role(age_holder, role = CHEF)
    return 0*ages

def _acs_montant_2009_(self, age_holder, P = law.cmu):
    '''
    Calcule le montant de l'ACS en cas d'éligibilité (à compter du 1 août 2009)
    '''
    ages_couple = self.split_by_roles(age_holder, roles = [CHEF, PART])
    ages_pac = self.split_by_roles(age_holder, roles = ENFS)
    return ((nb_par_age(ages_couple, 0, 15) + nb_par_age(ages_pac, 0, 15)) * P.acs_moins_16_ans +
       (nb_par_age(ages_couple, 16, 49) + nb_par_age(ages_pac, 16, 25)) * P.acs_16_49_ans +
       nb_par_age(ages_couple, 50, 59) * P.acs_50_59_ans +
       nb_par_age(ages_couple, 60, 200) * P.acs_plus_60_ans)

def _cmu_forfait_logement_base(cmu_nbp_foyer, P = law.cmu.forfait_logement, law_rsa = law.minim.rmi):
    '''
    Calcule le forfait logement applicable en cas de propriété ou d'occupation à titre gratuit
    '''
    return forfait_logement(cmu_nbp_foyer, P, law_rsa)

def _cmu_forfait_logement_al(cmu_nbp_foyer, P = law.cmu.forfait_logement_al, law_rsa = law.minim.rmi):
    '''
    Calcule le forfait logement applicable en cas d'aide au logement
    '''
    return forfait_logement(cmu_nbp_foyer, P, law_rsa)

def _cmu_nbp_foyer(nb_par, cmu_nb_pac):
    '''
    Calcule le nombre de personnes dans le foyer CMU
    '''
    return nb_par + cmu_nb_pac

def _cmu_c_plafond(cmu_nbp_foyer, P = law.cmu):
    '''
    Calcule le plafond de ressources pour la CMU complémentaire
    TODO: Rajouter la majoration pour les DOM
    '''
    return (P.plafond_base * (1 +
        (cmu_nbp_foyer >= 2) * P.coeff_p2 +
        max_(0, min_(2, cmu_nbp_foyer - 2)) * P.coeff_p3_p4 +
        max_(0, cmu_nbp_foyer - 4) * P.coeff_p5_plus
    ))

def _acs_plafond(cmu_c_plafond, P = law.cmu):
    '''
    Calcule le plafond de ressources pour l'ACS
    '''
    return cmu_c_plafond * (1 + P.majoration_plafond_acs)

@reference_formula
class cmu_br_i(SimpleFormulaColumn):
    column = FloatCol
    label = u"Base de ressources de l'individu prise en compte pour l'éligibilité à la CMU-C / ACS"
    entity_class = Individus
    period_unit = 'year'

    def function(self, activite, salnet, chonet, rstnet, alr, rsa_base_ressources_patrimoine_i, P = law.cmu):
        return salnet * (1 - (activite == 1) * P.abattement_chomage) + chonet + rstnet + alr + rsa_base_ressources_patrimoine_i

    def get_variable_period(self, output_period, variable_name):
        if variable_name in ['salnet', 'chonet', 'rstnet', 'alr', 'rsa_base_ressources_patrimoine_i']:
            return output_period.offset(-1)
        else:
            return output_period

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('year')

@reference_formula
class cmu_br(SimpleFormulaColumn):
    column = FloatCol
    label = u"Base de ressources prise en compte pour l'éligibilité à la CMU-C / ACS"
    entity_class = Familles
    period_unit = 'year'

    def function(self, so_holder, apl_holder, als_holder, alf_holder, cmu_forfait_logement_base, cmu_forfait_logement_al, age_holder, cmu_br_i_holder, P = law.cmu):
        so = self.cast_from_entity_to_roles(so_holder)
        so = self.filter_role(so, role = CHEF)
        apl = self.cast_from_entity_to_roles(apl_holder)
        apl = self.filter_role(apl, role = CHEF)
        als = self.cast_from_entity_to_roles(als_holder)
        als = self.filter_role(als, role = CHEF)
        alf = self.cast_from_entity_to_roles(alf_holder)
        alf = self.filter_role(alf, role = CHEF)

        cmu_br_i_par = self.split_by_roles(cmu_br_i_holder, roles = [CHEF, PART])
        cmu_br_i_pac = self.split_by_roles(cmu_br_i_holder, roles = ENFS)
        age_pac = self.split_by_roles(cmu_br_i_holder, roles = ENFS)

        res = (cmu_br_i_par[CHEF] + cmu_br_i_par[PART] +
            ((so == 2) + (so == 6)) * cmu_forfait_logement_base +
            ((alf + apl + als) > 0) * cmu_forfait_logement_al)

        for key, age in age_pac.iteritems():
            res += (0 <= age) * (age <= P.age_limite_pac) * cmu_br_i_pac[key]
        return res

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('year')

def _cmu_nb_pac(self, age_holder, P = law.cmu):
    '''
    Calcule de nombre d'enfants / personnes à charge à comptabiliser dans la famille CMU
    '''
    ages = self.split_by_roles(age_holder, roles = ENFS)
    return nb_par_age(ages, 0, P.age_limite_pac)

@reference_formula
class cmu_c(SimpleFormulaColumn):
    '''
    Détermine si le foyer a droit à la CMU complémentaire
    '''
    column = BoolCol
    label = u"Éligibilité à la CMU-C"
    entity_class = Familles
    period_unit = 'year'

    def function(self, cmu_c_plafond, cmu_br, period):
        return cmu_br <= cmu_c_plafond

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('year')

@reference_formula
class acs(SimpleFormulaColumn):
    '''
    Calcule le montant de l'ACS auquel le foyer a droit
    '''
    column = FloatCol
    label = u"Éligibilité à l'ACS"
    entity_class = Familles
    period_unit = 'year'

    def function(self, cmu_c, cmu_br, acs_plafond, acs_montant):
        return not_(cmu_c) * (cmu_br <= acs_plafond) * acs_montant

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('year')

############################################################################
# Helper functions
############################################################################
def nb_par_age(ages, min, max):
    '''
    Calcule le nombre d'individus ayant un âge compris entre min et max
    '''
    res = None
    for key, age in ages.iteritems():
        if res is None: res = zeros(len(age))
        res += (min <= age) & (age <= max)
    return res

def rsa_socle_base(nbp, P):
    '''
    Calcule le RSA socle du foyer pour nombre de personnes donné
    '''
    return P.rmi * (1 + P.txp2 * (nbp >= 2) + P.txp3 * (nbp >= 3) + P.txps * max_(0, nbp - 3))

def forfait_logement(nbp_foyer, P, law_rsa):
    '''
    Calcule le forfait logement en fonction du nombre de personnes dans le "foyer CMU" et d'un jeu de taux
    '''
    return (12 * rsa_socle_base(nbp_foyer, law_rsa) *
        ((nbp_foyer == 1) * P.taux_1p + (nbp_foyer == 2) * P.taux_2p + (nbp_foyer > 2) * P.taux_3p_plus))
<commit_msg>ajout des ressources incluses dans le calcul de la base de ressources cmuc-acs<commit_after># -*- coding:utf-8 -*-
#
# This file is part of OpenFisca.
# OpenFisca is a socio-fiscal microsimulation software
# Copyright © 2011 Clément Schaff, Mahdi Ben Jelloul
# Licensed under the terms of the GPL (version 3 or later) license
# (see openfisca/__init__.py for details)

from __future__ import division

from numpy import (zeros, maximum as max_, minimum as min_, logical_not as not_)

from openfisca_core.accessors import law
from openfisca_core.columns import BoolCol, FloatCol
from openfisca_core.formulas import SimpleFormulaColumn

from .base import QUIFAM, QUIFOY, reference_formula
from ..entities import Familles, Individus

CHEF = QUIFAM['chef']
PART = QUIFAM['part']
ENFS = [QUIFAM['enf1'], QUIFAM['enf2'], QUIFAM['enf3'], QUIFAM['enf4'], QUIFAM['enf5'], QUIFAM['enf6'], QUIFAM['enf7'], QUIFAM['enf8'], QUIFAM['enf9'], ]
VOUS = QUIFOY['vous']

def _acs_montant__2009(self, age_holder):
    '''
    Calcule le montant de l'ACS en cas d'éligibilité (jusqu'au 31 juillet 2009)
    '''
    # TODO
    ages = self.filter_role(age_holder, role = CHEF)
    return 0*ages

def _acs_montant_2009_(self, age_holder, P = law.cmu):
    '''
    Calcule le montant de l'ACS en cas d'éligibilité (à compter du 1 août 2009)
    '''
    ages_couple = self.split_by_roles(age_holder, roles = [CHEF, PART])
    ages_pac = self.split_by_roles(age_holder, roles = ENFS)
    return ((nb_par_age(ages_couple, 0, 15) + nb_par_age(ages_pac, 0, 15)) * P.acs_moins_16_ans +
       (nb_par_age(ages_couple, 16, 49) + nb_par_age(ages_pac, 16, 25)) * P.acs_16_49_ans +
       nb_par_age(ages_couple, 50, 59) * P.acs_50_59_ans +
       nb_par_age(ages_couple, 60, 200) * P.acs_plus_60_ans)

def _cmu_forfait_logement_base(cmu_nbp_foyer, P = law.cmu.forfait_logement, law_rsa = law.minim.rmi):
    '''
    Calcule le forfait logement applicable en cas de propriété ou d'occupation à titre gratuit
    '''
    return forfait_logement(cmu_nbp_foyer, P, law_rsa)

def _cmu_forfait_logement_al(cmu_nbp_foyer, P = law.cmu.forfait_logement_al, law_rsa = law.minim.rmi):
    '''
    Calcule le forfait logement applicable en cas d'aide au logement
    '''
    return forfait_logement(cmu_nbp_foyer, P, law_rsa)

def _cmu_nbp_foyer(nb_par, cmu_nb_pac):
    '''
    Calcule le nombre de personnes dans le foyer CMU
    '''
    return nb_par + cmu_nb_pac

def _cmu_c_plafond(cmu_nbp_foyer, P = law.cmu):
    '''
    Calcule le plafond de ressources pour la CMU complémentaire
    TODO: Rajouter la majoration pour les DOM
    '''
    return (P.plafond_base * (1 +
        (cmu_nbp_foyer >= 2) * P.coeff_p2 +
        max_(0, min_(2, cmu_nbp_foyer - 2)) * P.coeff_p3_p4 +
        max_(0, cmu_nbp_foyer - 4) * P.coeff_p5_plus
    ))

def _acs_plafond(cmu_c_plafond, P = law.cmu):
    '''
    Calcule le plafond de ressources pour l'ACS
    '''
    return cmu_c_plafond * (1 + P.majoration_plafond_acs)

@reference_formula
class cmu_br_i(SimpleFormulaColumn):
    column = FloatCol
    label = u"Base de ressources de l'individu prise en compte pour l'éligibilité à la CMU-C / ACS"
    entity_class = Individus
    period_unit = 'year'

    def function(self, activite, salnet, chonet, rstnet, alr, rsa_base_ressources_patrimoine_i, aah, indemnites_journalieres_maternite,
                 indemnites_journalieres_maladie, indemnites_journalieres_maladie_professionnelle, indemnites_journalieres_accident_travail,
                 revenus_stage_formation_pro, allocation_securisation_professionnelle, prime_forfaitaire_mensuelle_reprise_activite,
                 dedommagement_victime_amiante, prestation_compensatoire, retraite_combattant, pensions_invalidite,
                 indemnites_chomage_partiel, bourse_enseignement_sup, bourse_recherche, gains_exceptionnels, P = law.cmu):
        return ((salnet + revenus_stage_formation_pro + indemnites_chomage_partiel) * (1 - (activite == 1) * P.abattement_chomage) +
            aah + chonet + rstnet + alr + rsa_base_ressources_patrimoine_i + allocation_securisation_professionnelle +
            indemnites_journalieres_maternite + indemnites_journalieres_accident_travail + indemnites_journalieres_maladie + indemnites_journalieres_maladie_professionnelle +
            prime_forfaitaire_mensuelle_reprise_activite + dedommagement_victime_amiante + prestation_compensatoire +
            retraite_combattant + pensions_invalidite + bourse_enseignement_sup + bourse_recherche + gains_exceptionnels)

    def get_variable_period(self, output_period, variable_name):
        if variable_name == 'activite':
            return output_period
        else:
            return output_period.offset(-1)

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('year')

@reference_formula
class cmu_br(SimpleFormulaColumn):
    column = FloatCol
    label = u"Base de ressources prise en compte pour l'éligibilité à la CMU-C / ACS"
    entity_class = Familles
    period_unit = 'year'

    def function(self, so_holder, apl_holder, als_holder, alf_holder, cmu_forfait_logement_base, cmu_forfait_logement_al, age_holder, cmu_br_i_holder, P = law.cmu):
        so = self.cast_from_entity_to_roles(so_holder)
        so = self.filter_role(so, role = CHEF)
        apl = self.cast_from_entity_to_roles(apl_holder)
        apl = self.filter_role(apl, role = CHEF)
        als = self.cast_from_entity_to_roles(als_holder)
        als = self.filter_role(als, role = CHEF)
        alf = self.cast_from_entity_to_roles(alf_holder)
        alf = self.filter_role(alf, role = CHEF)

        cmu_br_i_par = self.split_by_roles(cmu_br_i_holder, roles = [CHEF, PART])
        cmu_br_i_pac = self.split_by_roles(cmu_br_i_holder, roles = ENFS)
        age_pac = self.split_by_roles(cmu_br_i_holder, roles = ENFS)

        res = (cmu_br_i_par[CHEF] + cmu_br_i_par[PART] +
            ((so == 2) + (so == 6)) * cmu_forfait_logement_base +
            ((alf + apl + als) > 0) * cmu_forfait_logement_al)

        for key, age in age_pac.iteritems():
            res += (0 <= age) * (age <= P.age_limite_pac) * cmu_br_i_pac[key]
        return res

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('year')

def _cmu_nb_pac(self, age_holder, P = law.cmu):
    '''
    Calcule de nombre d'enfants / personnes à charge à comptabiliser dans la famille CMU
    '''
    ages = self.split_by_roles(age_holder, roles = ENFS)
    return nb_par_age(ages, 0, P.age_limite_pac)

@reference_formula
class cmu_c(SimpleFormulaColumn):
    '''
    Détermine si le foyer a droit à la CMU complémentaire
    '''
    column = BoolCol
    label = u"Éligibilité à la CMU-C"
    entity_class = Familles
    period_unit = 'year'

    def function(self, cmu_c_plafond, cmu_br, period):
        return cmu_br <= cmu_c_plafond

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('year')

@reference_formula
class acs(SimpleFormulaColumn):
    '''
    Calcule le montant de l'ACS auquel le foyer a droit
    '''
    column = FloatCol
    label = u"Éligibilité à l'ACS"
    entity_class = Familles
    period_unit = 'year'

    def function(self, cmu_c, cmu_br, acs_plafond, acs_montant):
        return not_(cmu_c) * (cmu_br <= acs_plafond) * acs_montant

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('year')

############################################################################
# Helper functions
############################################################################
def nb_par_age(ages, min, max):
    '''
    Calcule le nombre d'individus ayant un âge compris entre min et max
    '''
    res = None
    for key, age in ages.iteritems():
        if res is None: res = zeros(len(age))
        res += (min <= age) & (age <= max)
    return res

def rsa_socle_base(nbp, P):
    '''
    Calcule le RSA socle du foyer pour nombre de personnes donné
    '''
    return P.rmi * (1 + P.txp2 * (nbp >= 2) + P.txp3 * (nbp >= 3) + P.txps * max_(0, nbp - 3))

def forfait_logement(nbp_foyer, P, law_rsa):
    '''
    Calcule le forfait logement en fonction du nombre de personnes dans le "foyer CMU" et d'un jeu de taux
    '''
    return (12 * rsa_socle_base(nbp_foyer, law_rsa) *
        ((nbp_foyer == 1) * P.taux_1p + (nbp_foyer == 2) * P.taux_2p + (nbp_foyer > 2) * P.taux_3p_plus))
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

# OpenFisca -- A versatile microsimulation software
# By: OpenFisca Team <contact@openfisca.fr>
#
# Copyright (C) 2011, 2012, 2013, 2014, 2015 OpenFisca Team
# https://github.com/openfisca
#
# This file is part of OpenFisca.
#
# OpenFisca is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# OpenFisca is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os

COUNTRY_DIR = os.path.dirname(os.path.abspath(__file__))
CURRENCY = u"DT"
REV_TYP = {
    'brut': ['salbrut'],
    'imposable': ['sal'],
    'superbrut': ['salsuperbrut'],
    }
REVENUES_CATEGORIES = {
    'imposable': ['sal'],
    }
WEIGHT = "wprm"
WEIGHT_INI = "wprm_init"
X_AXES_PROPERTIES = {
    'sali': {
        'name': 'sal',
        'typ_tot': {
            'sal': 'Salaire imposable',
            'salbrut': 'Salaire brut',
            'salnet': 'Salaire net',
            'salsuperbrut': 'Salaire super brut',
            },
        'typ_tot_default': 'sal',
        },
    }

def init_country(qt = False):
    """Create a country-specific TaxBenefitSystem."""
    from openfisca_core.taxbenefitsystems import LegacyTaxBenefitSystem

    from . import decompositions, entities, scenarios
    from .model import datatrees
    from .model import data  # Load input variables into entities. # noqa
    from .model import model  # Load output variables into entities. # noqa

    class TaxBenefitSystem(LegacyTaxBenefitSystem):
        """Tunisian tax benefit system"""
        # AGGREGATES_DEFAULT_VARS = AGGREGATES_DEFAULT_VARS
        check_consistency = None  # staticmethod(utils.check_consistency)
        columns_name_tree_by_entity = datatrees.columns_name_tree_by_entity
        CURRENCY = CURRENCY
        # DATA_DIR = DATA_DIR
        # DATA_SOURCES_DIR = os.path.join(COUNTRY_DIR, 'data', 'sources')
        DECOMP_DIR = os.path.dirname(os.path.abspath(decompositions.__file__))
        DEFAULT_DECOMP_FILE = decompositions.DEFAULT_DECOMP_FILE
        entity_class_by_key_plural = dict(
            (entity_class.key_plural, entity_class)
            for entity_class in entities.entity_class_by_symbol.itervalues()
            )
        # FILTERING_VARS = FILTERING_VARS
        # column_by_name = column_by_name  # Done below to avoid "name is not defined" exception
        # columns_name_tree_by_entity = columns_name_tree_by_entity  # Done below to avoid "name is not defined"
        # exception
        legislation_xml_file_path = os.path.join(COUNTRY_DIR, 'param', 'param.xml')
        # preprocess_compact_legislation = preprocess_compact_legislation  # Done below to avoid "name is not defined"
        # exception
        REFORMS_DIR = os.path.join(COUNTRY_DIR, 'reformes')
        REV_TYP = REV_TYP
        REVENUES_CATEGORIES = REVENUES_CATEGORIES
        Scenario = scenarios.Scenario
        WEIGHT = WEIGHT
        WEIGHT_INI = WEIGHT_INI

        def preproc_inputs(self, datatable):
            """Preprocess inputs table: country specific manipulations

            Parameters
            ----------
            datatable : a DataTable object
                        the DataTable containing the input variables of the model

            """
            try:
                datatable.propagate_to_members(WEIGHT, 'ind')
            #    datatable.propagate_to_members('rfr_n_2', 'ind')
            #    datatable.propagate_to_members('nbptr_n_2', 'ind')
            except:
                pass

    return TaxBenefitSystem
<commit_msg>Rename salnet to salaire_net<commit_after># -*- coding: utf-8 -*-

# OpenFisca -- A versatile microsimulation software
# By: OpenFisca Team <contact@openfisca.fr>
#
# Copyright (C) 2011, 2012, 2013, 2014, 2015 OpenFisca Team
# https://github.com/openfisca
#
# This file is part of OpenFisca.
#
# OpenFisca is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# OpenFisca is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os

COUNTRY_DIR = os.path.dirname(os.path.abspath(__file__))
CURRENCY = u"DT"
REV_TYP = {
    'brut': ['salbrut'],
    'imposable': ['sal'],
    'superbrut': ['salsuperbrut'],
    }
REVENUES_CATEGORIES = {
    'imposable': ['sal'],
    }
WEIGHT = "wprm"
WEIGHT_INI = "wprm_init"
X_AXES_PROPERTIES = {
    'sali': {
        'name': 'sal',
        'typ_tot': {
            'sal': 'Salaire imposable',
            'salbrut': 'Salaire brut',
            'salaire_net': 'Salaire net',
            'salsuperbrut': 'Salaire super brut',
            },
        'typ_tot_default': 'sal',
        },
    }

def init_country(qt = False):
    """Create a country-specific TaxBenefitSystem."""
    from openfisca_core.taxbenefitsystems import LegacyTaxBenefitSystem

    from . import decompositions, entities, scenarios
    from .model import datatrees
    from .model import data  # Load input variables into entities. # noqa
    from .model import model  # Load output variables into entities. # noqa

    class TaxBenefitSystem(LegacyTaxBenefitSystem):
        """Tunisian tax benefit system"""
        # AGGREGATES_DEFAULT_VARS = AGGREGATES_DEFAULT_VARS
        check_consistency = None  # staticmethod(utils.check_consistency)
        columns_name_tree_by_entity = datatrees.columns_name_tree_by_entity
        CURRENCY = CURRENCY
        # DATA_DIR = DATA_DIR
        # DATA_SOURCES_DIR = os.path.join(COUNTRY_DIR, 'data', 'sources')
        DECOMP_DIR = os.path.dirname(os.path.abspath(decompositions.__file__))
        DEFAULT_DECOMP_FILE = decompositions.DEFAULT_DECOMP_FILE
        entity_class_by_key_plural = dict(
            (entity_class.key_plural, entity_class)
            for entity_class in entities.entity_class_by_symbol.itervalues()
            )
        # FILTERING_VARS = FILTERING_VARS
        # column_by_name = column_by_name  # Done below to avoid "name is not defined" exception
        # columns_name_tree_by_entity = columns_name_tree_by_entity  # Done below to avoid "name is not defined"
        # exception
        legislation_xml_file_path = os.path.join(COUNTRY_DIR, 'param', 'param.xml')
        # preprocess_compact_legislation = preprocess_compact_legislation  # Done below to avoid "name is not defined"
        # exception
        REFORMS_DIR = os.path.join(COUNTRY_DIR, 'reformes')
        REV_TYP = REV_TYP
        REVENUES_CATEGORIES = REVENUES_CATEGORIES
        Scenario = scenarios.Scenario
        WEIGHT = WEIGHT
        WEIGHT_INI = WEIGHT_INI

        def preproc_inputs(self, datatable):
            """Preprocess inputs table: country specific manipulations

            Parameters
            ----------
            datatable : a DataTable object
                        the DataTable containing the input variables of the model

            """
            try:
                datatable.propagate_to_members(WEIGHT, 'ind')
            #    datatable.propagate_to_members('rfr_n_2', 'ind')
            #    datatable.propagate_to_members('nbptr_n_2', 'ind')
            except:
                pass

    return TaxBenefitSystem
<|endoftext|>


<commit_before># Copyright (c) 2001, Stanford University
# All rights reserved.
#
# See the file LICENSE.txt for information on redistributing this software.

import sys,os;
import cPickle;
import types;
import string;
import re;
import alias_exports;
import stub_common;

parsed_file = open( "../glapi_parser/gl_header.parsed", "rb" )
gl_mapping = cPickle.load( parsed_file )

keys = gl_mapping.keys()
keys.sort();

stub_common.CopyrightC()

print """
/* DO NOT EDIT - THIS FILE GENERATED BY THE getprocaddress.py SCRIPT */
#include "chromium.h"
#include "api_templates.h"
#include "cr_string.h"
#include "cr_error.h"
#include "cr_spu.h"
#include "stub.h"

#include <stdio.h>

#ifdef WINDOWS
#pragma warning( disable: 4055 )
#endif
"""

#for func_name in stub_common.AllSpecials( "getprocaddress" ):
#	( return_type, arg_names, arg_types ) = gl_mapping[func_name];
#	print 'extern %s APIENTRY stub_%s%s;' % (return_type, func_name, stub_common.ArgumentString( arg_names, arg_types ) )

print """
CR_PROC CR_APIENTRY crGetProcAddress( const char *name )
{
	stubInit();
"""

bonus_functions = [
	"crCreateContext",
	"crDestroyContext",
	"crMakeCurrent",
	"crGetCurrentContext",
	"crGetCurrentWindow",
	"crSwapBuffers",
	"crWindowCreate",
	"crWindowDestroy",
	"crWindowSize",
	"crWindowPosition",
	"crWindowShow"
]

for func_name in keys:
	if stub_common.FindSpecial( "noexport", func_name ):
		continue
	if stub_common.FindSpecial( "getprocaddress", func_name ):
		print '\tif (!crStrcmp( name, "gl%s" )) return (CR_PROC) stub_%s;' % ( func_name, func_name )
		continue
	print '\tif (!crStrcmp( name, "gl%s" )) return (CR_PROC) glim.%s;' % ( func_name, func_name )
	real_func_name = alias_exports.AliasMap( func_name )
	if real_func_name:
		print '\tif (!crStrcmp( name, "gl%s" )) return (CR_PROC) glim.%s;' % ( real_func_name, func_name )

for func_name in bonus_functions:
	print '\tif (!crStrcmp( name, "%s" )) return (CR_PROC) %s;' % ( func_name, func_name )

print """
/* As these are Windows specific (i.e. wgl), define these now.... */
#ifdef WINDOWS
	{
		wglGetExtensionsStringEXTFunc_t wglGetExtensionsStringEXT = NULL;
		wglChoosePixelFormatFunc_t wglChoosePixelFormatEXT = NULL;
		wglGetPixelFormatAttribivEXTFunc_t wglGetPixelFormatAttribivEXT = NULL;
		wglGetPixelFormatAttribfvEXTFunc_t wglGetPixelFormatAttribfvEXT = NULL;
		if (!crStrcmp( name, "wglGetExtensionsStringEXT" )) return (CR_PROC) wglGetExtensionsStringEXT;
		if (!crStrcmp( name, "wglChoosePixelFormatEXT" )) return (CR_PROC) wglChoosePixelFormatEXT;
		if (!crStrcmp( name, "wglGetPixelFormatAttribivEXT" )) return (CR_PROC) wglGetPixelFormatAttribivEXT;
		if (!crStrcmp( name, "wglGetPixelFormatAttribfvEXT" )) return (CR_PROC) wglGetPixelFormatAttribfvEXT;
	}
#endif
"""
print '\treturn NULL;'
print '}'
<commit_msg>include cr_version.h<commit_after># Copyright (c) 2001, Stanford University
# All rights reserved.
#
# See the file LICENSE.txt for information on redistributing this software.

import sys,os;
import cPickle;
import types;
import string;
import re;
import alias_exports;
import stub_common;

parsed_file = open( "../glapi_parser/gl_header.parsed", "rb" )
gl_mapping = cPickle.load( parsed_file )

keys = gl_mapping.keys()
keys.sort();

stub_common.CopyrightC()

print """
/* DO NOT EDIT - THIS FILE GENERATED BY THE getprocaddress.py SCRIPT */
#include "chromium.h"
#include "api_templates.h"
#include "cr_string.h"
#include "cr_error.h"
#include "cr_spu.h"
#include "cr_version.h"
#include "stub.h"

#include <stdio.h>

#ifdef WINDOWS
#pragma warning( disable: 4055 )
#endif
"""

#for func_name in stub_common.AllSpecials( "getprocaddress" ):
#	( return_type, arg_names, arg_types ) = gl_mapping[func_name];
#	print 'extern %s APIENTRY stub_%s%s;' % (return_type, func_name, stub_common.ArgumentString( arg_names, arg_types ) )

print """
CR_PROC CR_APIENTRY crGetProcAddress( const char *name )
{
	stubInit();
"""

bonus_functions = [
	"crCreateContext",
	"crDestroyContext",
	"crMakeCurrent",
	"crGetCurrentContext",
	"crGetCurrentWindow",
	"crSwapBuffers",
	"crWindowCreate",
	"crWindowDestroy",
	"crWindowSize",
	"crWindowPosition",
	"crWindowShow"
]

for func_name in keys:
	if stub_common.FindSpecial( "noexport", func_name ):
		continue
	if stub_common.FindSpecial( "getprocaddress", func_name ):
		print '\tif (!crStrcmp( name, "gl%s" )) return (CR_PROC) stub_%s;' % ( func_name, func_name )
		continue
	print '\tif (!crStrcmp( name, "gl%s" )) return (CR_PROC) glim.%s;' % ( func_name, func_name )
	real_func_name = alias_exports.AliasMap( func_name )
	if real_func_name:
		print '\tif (!crStrcmp( name, "gl%s" )) return (CR_PROC) glim.%s;' % ( real_func_name, func_name )

for func_name in bonus_functions:
	print '\tif (!crStrcmp( name, "%s" )) return (CR_PROC) %s;' % ( func_name, func_name )

print """
/* As these are Windows specific (i.e. wgl), define these now.... */
#ifdef WINDOWS
	{
		wglGetExtensionsStringEXTFunc_t wglGetExtensionsStringEXT = NULL;
		wglChoosePixelFormatFunc_t wglChoosePixelFormatEXT = NULL;
		wglGetPixelFormatAttribivEXTFunc_t wglGetPixelFormatAttribivEXT = NULL;
		wglGetPixelFormatAttribfvEXTFunc_t wglGetPixelFormatAttribfvEXT = NULL;
		if (!crStrcmp( name, "wglGetExtensionsStringEXT" )) return (CR_PROC) wglGetExtensionsStringEXT;
		if (!crStrcmp( name, "wglChoosePixelFormatEXT" )) return (CR_PROC) wglChoosePixelFormatEXT;
		if (!crStrcmp( name, "wglGetPixelFormatAttribivEXT" )) return (CR_PROC) wglGetPixelFormatAttribivEXT;
		if (!crStrcmp( name, "wglGetPixelFormatAttribfvEXT" )) return (CR_PROC) wglGetPixelFormatAttribfvEXT;
	}
#endif
"""
print '\treturn NULL;'
print '}'
<|endoftext|>


<commit_before># Copyright (c) 2019, Djaodjin Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from __future__ import absolute_import
from __future__ import unicode_literals

import logging, os, tempfile, shutil, subprocess, sys, zipfile

from django.core.files.storage import FileSystemStorage
from django.core.exceptions import PermissionDenied
from django.contrib.staticfiles.templatetags.staticfiles import do_static
from django.template.base import Parser, NodeList
from django.template.backends.jinja2 import get_exception_info
from django.template.context import Context
from django.template.exceptions import TemplateDoesNotExist, TemplateSyntaxError
from django.template.loader import _engine_list, get_template
from django.utils import six
from django.utils._os import safe_join
from django.utils.encoding import force_text
from django_assets.templatetags.assets import assets
import jinja2
import requests

from . import settings
from .compat import TokenType, get_html_engine

#pylint:disable=no-name-in-module,import-error
from django.utils.six.moves.urllib.parse import urlparse

LOGGER = logging.getLogger(__name__)

class URLRewriteWrapper(object):

    def __init__(self, file_obj, path_prefix=None):
        self.wrapped = file_obj
        self.path_prefix = path_prefix

    def write(self, text):
        if self.path_prefix:
            text = text.replace(
                '="/static', '="/%s/static' % self.path_prefix)
        return self.wrapped.write(text)

class Template(object):

    def __init__(self, engine):
        self.engine = engine

class AssetsParser(Parser):

    def __init__(self, tokens, dest_stream,
                 libraries=None, builtins=None, origin=None):
        #pylint:disable=too-many-arguments
        super(AssetsParser, self).__init__(tokens,
            libraries=libraries, builtins=builtins, origin=origin)
        self.dest_stream = dest_stream
        self.context = Context()
        engine, _, _ = get_html_engine()
        self.context.template = Template(engine)

    def parse_through(self, parse_until=None):
        if parse_until is None:
            parse_until = []
        nodelist = NodeList()
        while self.tokens:
            token = self.next_token()
            if six.PY2:
                contents = token.contents.encode('utf8')
            else:
                contents = token.contents
            if token.token_type == TokenType.TEXT:
                self.dest_stream.write(contents)
            elif token.token_type == TokenType.VAR:
                self.dest_stream.write("{{%s}}" % contents)
            elif token.token_type == TokenType.BLOCK:
                try:
                    command = token.contents.split()[0]
                except IndexError:
                    self.empty_block_tag(token)
                if command in parse_until:
                    # put token back on token list so calling
                    # code knows why it terminated
                    self.prepend_token(token)
                    return nodelist
                if command == 'assets':
                    try:
                        # XXX This should work but for some reason debug does
                        # not get propagated.
                        # Lost in webassets.bundle.resolve_contents
                        token.contents += ' debug=False'
                        assets_string = str(
                            assets(self, token).render(self.context))
                        self.dest_stream.write(assets_string)
                    except TemplateSyntaxError as err:
                        if hasattr(self, 'error'):
                            raise self.error(token, err)
                        # Django < 1.8
                        elif not self.compile_function_error(token, err):
                            raise
                elif command == 'static':
                    self.dest_stream.write(
                        do_static(self, token).render(self.context))
                else:
                    self.dest_stream.write("{%% %s %%}" % contents)
            elif token.token_type == TokenType.COMMENT:
                pass

def check_template(template_source, using=None):
    """
    Loads and returns a template for the given name.

    Raises TemplateDoesNotExist if no such template exists.
    """
    errs = {}
    engines = _engine_list(using)
    # We should find at least one engine that does not raise an error.
    for engine in engines:
        try:
            try:
                return engine.from_string(template_source)
            except jinja2.TemplateSyntaxError as exc:
                new = TemplateSyntaxError(exc.args)
                new.template_debug = get_exception_info(exc)
                six.reraise(TemplateSyntaxError, new, sys.exc_info()[2])
        except TemplateSyntaxError as err:
            errs.update({engine: err})
    if errs:
        raise TemplateSyntaxError(errs)

def get_template_path(template=None, relative_path=None):
    if template is None:
        template = get_template(relative_path)
    try:
        return template.template.filename
    except AttributeError:
        return template.origin.name

def get_theme_dir(theme_name):
    if isinstance(settings.THEME_DIR_CALLABLE, six.string_types):
        from .compat import import_string
        settings.THEME_DIR_CALLABLE = import_string(
            settings.THEME_DIR_CALLABLE)
    theme_dir = settings.THEME_DIR_CALLABLE(theme_name)
    return theme_dir

def install_theme(app_name, package_uri, force=False):
    parts = urlparse(package_uri)
    package_file = None
    try:
        if parts.scheme == 's3':
            from .backends.s3 import get_package_file_from_s3
            package_file = get_package_file_from_s3(package_uri)
        elif parts.scheme in ['http', 'https']:
            basename = os.path.basename(parts.path)
            resp = requests.get(package_uri, stream=True)
            if resp.status_code == 200:
                package_file = tempfile.NamedTemporaryFile()
                shutil.copyfileobj(resp.raw, package_file)
                package_file.seek(0)
            else:
                raise RuntimeError(
                    "requests status code: %d" % resp.status_code)
        else:
            basename = os.path.basename(package_uri)
            package_storage = FileSystemStorage(
                os.path.dirname(package_uri))
            #pylint:disable=redefined-variable-type
            package_file = package_storage.open(basename)
        if not app_name:
            app_name = os.path.splitext(basename)[0]
        LOGGER.info("install %s to %s\n", package_uri, app_name)
        with zipfile.ZipFile(package_file, 'r') as zip_file:
            install_theme_fileobj(app_name, zip_file, force=force)
    finally:
        if hasattr(package_file, 'close'):
            package_file.close()

def install_theme_fileobj(theme_name, zip_file, force=False):
    """
    Extract resources and templates from an opened ``ZipFile``
    and install them at a place they can be picked by the multitier
    logic in ``template_loader.Loader.get_template_sources``.
    """
    #pylint:disable=too-many-statements,too-many-locals
    assert theme_name is not None
    LOGGER.info("install theme %s%s", theme_name, " (force)" if force else "")
    theme_dir = get_theme_dir(theme_name)
    public_dir = safe_join(settings.PUBLIC_ROOT, theme_name)
    templates_dir = safe_join(theme_dir, 'templates')

    if not force and os.path.exists(public_dir):
        LOGGER.warning("install theme '%s' but '%s' already exists.",
            theme_name, public_dir)
        raise PermissionDenied("Theme public assets already exists.")

    if not force and os.path.exists(templates_dir):
        LOGGER.warning("install theme '%s' but '%s' already exists.",
            theme_name, templates_dir)
        raise PermissionDenied("Theme templates already exists.")

    # We rely on the assumption that ``public_dir`` and ``templates_dir``
    # are on the same filesystem. We create a temporary directory on that
    # common filesystem, which guarentees that:
    #   1. If the disk is full, we will find on extract, not when we try
    #      to move the directory in place.
    #   2. If the filesystem is encrypted, we don't inadvertently leak
    #      information by creating "temporary" files.
    tmp_base = safe_join(
        os.path.commonprefix([public_dir, templates_dir]), '.cache')
    if not os.path.exists(tmp_base):
        os.makedirs(tmp_base)
    if not os.path.isdir(os.path.dirname(templates_dir)):
        os.makedirs(os.path.dirname(templates_dir))
    tmp_dir = tempfile.mkdtemp(dir=tmp_base)

    #pylint: disable=too-many-nested-blocks
    try:
        for info in zip_file.infolist():
            if info.file_size == 0:
                # Crude way to detect directories
                continue
            tmp_path = None
            test_parts = os.path.normpath(info.filename).split(os.sep)[1:]
            if test_parts:
                base = test_parts.pop(0)
                if base == 'public':
                    if settings.PUBLIC_WHITELIST is not None:
                        if (os.path.join(*test_parts)
                            in settings.PUBLIC_WHITELIST):
                            tmp_path = safe_join(tmp_dir, base, *test_parts)
                    else:
                        tmp_path = safe_join(tmp_dir, base, *test_parts)
                    if tmp_path:
                        if not os.path.isdir(os.path.dirname(tmp_path)):
                            os.makedirs(os.path.dirname(tmp_path))
                        with open(tmp_path, 'wb') as extracted_file:
                            extracted_file.write(zip_file.read(info.filename))
                elif base == 'templates':
                    relative_path = os.path.join(*test_parts)
                    if settings.TEMPLATES_WHITELIST is not None:
                        if (os.path.join(*test_parts)
                            in settings.TEMPLATES_WHITELIST):
                            tmp_path = safe_join(tmp_dir, base, relative_path)
                    else:
                        tmp_path = safe_join(tmp_dir, base, relative_path)
                    if tmp_path:
                        if not os.path.isdir(os.path.dirname(tmp_path)):
                            os.makedirs(os.path.dirname(tmp_path))
                        template_string = zip_file.read(info.filename)
                        if hasattr(template_string, 'decode'):
                            template_string = template_string.decode('utf-8')
                        template_string = force_text(template_string)
                        try:
                            check_template(template_string)
                            with open(tmp_path, 'w') as extracted_file:
                                extracted_file.write(template_string)
                            try:
                                default_path = get_template_path(
                                    relative_path=relative_path)
                                if (default_path and
                                    not default_path.startswith(theme_dir)):
                                    cmdline = [
                                        'diff', '-u', default_path, tmp_path]
                                    cmd = subprocess.Popen(
                                        cmdline, stdout=subprocess.PIPE)
                                    lines = cmd.stdout.readlines()
                                    cmd.wait()
                                    # Non-zero error codes are ok here.
                                    # That's how diff indicates the files
                                    # are different.
                                    if not lines:
                                        LOGGER.info(
                                            "%s: no differences", relative_path)
                                        os.remove(tmp_path)
                            except TemplateDoesNotExist:
                                # We are installing a template which is not
                                # in the default theme, so no diff is ok.
                                pass
                        except TemplateSyntaxError as err:
                            LOGGER.info("error:%s: %s", relative_path, err)

        # Should be safe to move in-place at this point.
        # Templates are necessary while public resources (css, js)
        # are optional.
        tmp_public = safe_join(tmp_dir, 'public')
        tmp_templates = safe_join(tmp_dir, 'templates')
        mkdirs = []
        renames = []
        for paths in [(tmp_templates, templates_dir),
                     (tmp_public, public_dir)]:
            if os.path.exists(paths[0]):
                if not os.path.exists(os.path.dirname(paths[1])):
                    mkdirs += [os.path.exists(os.path.dirname(paths[1]))]
                renames += [paths]
        for path in mkdirs:
            os.makedirs(path)
        for paths in renames:
            if os.path.exists(paths[1]):
                LOGGER.info("remove previous path %s", paths[1])
                shutil.rmtree(paths[1])
            os.rename(paths[0], paths[1])
    finally:
        # Always delete the temporary directory, exception raised or not.
        shutil.rmtree(tmp_dir)

def remove_theme(theme_name):
    """
    Remove assets and templates directories.
    """
    theme_dir = get_theme_dir(theme_name)
    public_dir = safe_join(settings.PUBLIC_ROOT, theme_name)
    LOGGER.info("remove theme '%s', that is directories %s and %s.",
                   theme_name, theme_dir, public_dir)
    try:
        shutil.rmtree(theme_dir)
    except FileNotFoundError:
        pass
    try:
        shutil.rmtree(public_dir)
    except FileNotFoundError:
        pass
<commit_msg>compatibles with py2<commit_after># Copyright (c) 2019, Djaodjin Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from __future__ import absolute_import
from __future__ import unicode_literals

import logging, os, tempfile, shutil, subprocess, sys, zipfile

from django.core.files.storage import FileSystemStorage
from django.core.exceptions import PermissionDenied
from django.contrib.staticfiles.templatetags.staticfiles import do_static
from django.template.base import Parser, NodeList
from django.template.backends.jinja2 import get_exception_info
from django.template.context import Context
from django.template.exceptions import TemplateDoesNotExist, TemplateSyntaxError
from django.template.loader import _engine_list, get_template
from django.utils import six
from django.utils._os import safe_join
from django.utils.encoding import force_text
from django_assets.templatetags.assets import assets
import jinja2
import requests

from . import settings
from .compat import TokenType, get_html_engine

#pylint:disable=no-name-in-module,import-error
from django.utils.six.moves.urllib.parse import urlparse

try:
    FileNotFoundError
except NameError:
    # py2
    FileNotFoundError = IOError

LOGGER = logging.getLogger(__name__)

class URLRewriteWrapper(object):

    def __init__(self, file_obj, path_prefix=None):
        self.wrapped = file_obj
        self.path_prefix = path_prefix

    def write(self, text):
        if self.path_prefix:
            text = text.replace(
                '="/static', '="/%s/static' % self.path_prefix)
        return self.wrapped.write(text)

class Template(object):

    def __init__(self, engine):
        self.engine = engine

class AssetsParser(Parser):

    def __init__(self, tokens, dest_stream,
                 libraries=None, builtins=None, origin=None):
        #pylint:disable=too-many-arguments
        super(AssetsParser, self).__init__(tokens,
            libraries=libraries, builtins=builtins, origin=origin)
        self.dest_stream = dest_stream
        self.context = Context()
        engine, _, _ = get_html_engine()
        self.context.template = Template(engine)

    def parse_through(self, parse_until=None):
        if parse_until is None:
            parse_until = []
        nodelist = NodeList()
        while self.tokens:
            token = self.next_token()
            if six.PY2:
                contents = token.contents.encode('utf8')
            else:
                contents = token.contents
            if token.token_type == TokenType.TEXT:
                self.dest_stream.write(contents)
            elif token.token_type == TokenType.VAR:
                self.dest_stream.write("{{%s}}" % contents)
            elif token.token_type == TokenType.BLOCK:
                try:
                    command = token.contents.split()[0]
                except IndexError:
                    self.empty_block_tag(token)
                if command in parse_until:
                    # put token back on token list so calling
                    # code knows why it terminated
                    self.prepend_token(token)
                    return nodelist
                if command == 'assets':
                    try:
                        # XXX This should work but for some reason debug does
                        # not get propagated.
                        # Lost in webassets.bundle.resolve_contents
                        token.contents += ' debug=False'
                        assets_string = str(
                            assets(self, token).render(self.context))
                        self.dest_stream.write(assets_string)
                    except TemplateSyntaxError as err:
                        if hasattr(self, 'error'):
                            raise self.error(token, err)
                        # Django < 1.8
                        elif not self.compile_function_error(token, err):
                            raise
                elif command == 'static':
                    self.dest_stream.write(
                        do_static(self, token).render(self.context))
                else:
                    self.dest_stream.write("{%% %s %%}" % contents)
            elif token.token_type == TokenType.COMMENT:
                pass

def check_template(template_source, using=None):
    """
    Loads and returns a template for the given name.

    Raises TemplateDoesNotExist if no such template exists.
    """
    errs = {}
    engines = _engine_list(using)
    # We should find at least one engine that does not raise an error.
    for engine in engines:
        try:
            try:
                return engine.from_string(template_source)
            except jinja2.TemplateSyntaxError as exc:
                new = TemplateSyntaxError(exc.args)
                new.template_debug = get_exception_info(exc)
                six.reraise(TemplateSyntaxError, new, sys.exc_info()[2])
        except TemplateSyntaxError as err:
            errs.update({engine: err})
    if errs:
        raise TemplateSyntaxError(errs)

def get_template_path(template=None, relative_path=None):
    if template is None:
        template = get_template(relative_path)
    try:
        return template.template.filename
    except AttributeError:
        return template.origin.name

def get_theme_dir(theme_name):
    if isinstance(settings.THEME_DIR_CALLABLE, six.string_types):
        from .compat import import_string
        settings.THEME_DIR_CALLABLE = import_string(
            settings.THEME_DIR_CALLABLE)
    theme_dir = settings.THEME_DIR_CALLABLE(theme_name)
    return theme_dir

def install_theme(app_name, package_uri, force=False):
    parts = urlparse(package_uri)
    package_file = None
    try:
        if parts.scheme == 's3':
            from .backends.s3 import get_package_file_from_s3
            package_file = get_package_file_from_s3(package_uri)
        elif parts.scheme in ['http', 'https']:
            basename = os.path.basename(parts.path)
            resp = requests.get(package_uri, stream=True)
            if resp.status_code == 200:
                package_file = tempfile.NamedTemporaryFile()
                shutil.copyfileobj(resp.raw, package_file)
                package_file.seek(0)
            else:
                raise RuntimeError(
                    "requests status code: %d" % resp.status_code)
        else:
            basename = os.path.basename(package_uri)
            package_storage = FileSystemStorage(
                os.path.dirname(package_uri))
            #pylint:disable=redefined-variable-type
            package_file = package_storage.open(basename)
        if not app_name:
            app_name = os.path.splitext(basename)[0]
        LOGGER.info("install %s to %s\n", package_uri, app_name)
        with zipfile.ZipFile(package_file, 'r') as zip_file:
            install_theme_fileobj(app_name, zip_file, force=force)
    finally:
        if hasattr(package_file, 'close'):
            package_file.close()

def install_theme_fileobj(theme_name, zip_file, force=False):
    """
    Extract resources and templates from an opened ``ZipFile``
    and install them at a place they can be picked by the multitier
    logic in ``template_loader.Loader.get_template_sources``.
    """
    #pylint:disable=too-many-statements,too-many-locals
    assert theme_name is not None
    LOGGER.info("install theme %s%s", theme_name, " (force)" if force else "")
    theme_dir = get_theme_dir(theme_name)
    public_dir = safe_join(settings.PUBLIC_ROOT, theme_name)
    templates_dir = safe_join(theme_dir, 'templates')

    if not force and os.path.exists(public_dir):
        LOGGER.warning("install theme '%s' but '%s' already exists.",
            theme_name, public_dir)
        raise PermissionDenied("Theme public assets already exists.")

    if not force and os.path.exists(templates_dir):
        LOGGER.warning("install theme '%s' but '%s' already exists.",
            theme_name, templates_dir)
        raise PermissionDenied("Theme templates already exists.")

    # We rely on the assumption that ``public_dir`` and ``templates_dir``
    # are on the same filesystem. We create a temporary directory on that
    # common filesystem, which guarentees that:
    #   1. If the disk is full, we will find on extract, not when we try
    #      to move the directory in place.
    #   2. If the filesystem is encrypted, we don't inadvertently leak
    #      information by creating "temporary" files.
    tmp_base = safe_join(
        os.path.commonprefix([public_dir, templates_dir]), '.cache')
    if not os.path.exists(tmp_base):
        os.makedirs(tmp_base)
    if not os.path.isdir(os.path.dirname(templates_dir)):
        os.makedirs(os.path.dirname(templates_dir))
    tmp_dir = tempfile.mkdtemp(dir=tmp_base)

    #pylint: disable=too-many-nested-blocks
    try:
        for info in zip_file.infolist():
            if info.file_size == 0:
                # Crude way to detect directories
                continue
            tmp_path = None
            test_parts = os.path.normpath(info.filename).split(os.sep)[1:]
            if test_parts:
                base = test_parts.pop(0)
                if base == 'public':
                    if settings.PUBLIC_WHITELIST is not None:
                        if (os.path.join(*test_parts)
                            in settings.PUBLIC_WHITELIST):
                            tmp_path = safe_join(tmp_dir, base, *test_parts)
                    else:
                        tmp_path = safe_join(tmp_dir, base, *test_parts)
                    if tmp_path:
                        if not os.path.isdir(os.path.dirname(tmp_path)):
                            os.makedirs(os.path.dirname(tmp_path))
                        with open(tmp_path, 'wb') as extracted_file:
                            extracted_file.write(zip_file.read(info.filename))
                elif base == 'templates':
                    relative_path = os.path.join(*test_parts)
                    if settings.TEMPLATES_WHITELIST is not None:
                        if (os.path.join(*test_parts)
                            in settings.TEMPLATES_WHITELIST):
                            tmp_path = safe_join(tmp_dir, base, relative_path)
                    else:
                        tmp_path = safe_join(tmp_dir, base, relative_path)
                    if tmp_path:
                        if not os.path.isdir(os.path.dirname(tmp_path)):
                            os.makedirs(os.path.dirname(tmp_path))
                        template_string = zip_file.read(info.filename)
                        if hasattr(template_string, 'decode'):
                            template_string = template_string.decode('utf-8')
                        template_string = force_text(template_string)
                        try:
                            check_template(template_string)
                            with open(tmp_path, 'w') as extracted_file:
                                extracted_file.write(template_string)
                            try:
                                default_path = get_template_path(
                                    relative_path=relative_path)
                                if (default_path and
                                    not default_path.startswith(theme_dir)):
                                    cmdline = [
                                        'diff', '-u', default_path, tmp_path]
                                    cmd = subprocess.Popen(
                                        cmdline, stdout=subprocess.PIPE)
                                    lines = cmd.stdout.readlines()
                                    cmd.wait()
                                    # Non-zero error codes are ok here.
                                    # That's how diff indicates the files
                                    # are different.
                                    if not lines:
                                        LOGGER.info(
                                            "%s: no differences", relative_path)
                                        os.remove(tmp_path)
                            except TemplateDoesNotExist:
                                # We are installing a template which is not
                                # in the default theme, so no diff is ok.
                                pass
                        except TemplateSyntaxError as err:
                            LOGGER.info("error:%s: %s", relative_path, err)

        # Should be safe to move in-place at this point.
        # Templates are necessary while public resources (css, js)
        # are optional.
        tmp_public = safe_join(tmp_dir, 'public')
        tmp_templates = safe_join(tmp_dir, 'templates')
        mkdirs = []
        renames = []
        for paths in [(tmp_templates, templates_dir),
                     (tmp_public, public_dir)]:
            if os.path.exists(paths[0]):
                if not os.path.exists(os.path.dirname(paths[1])):
                    mkdirs += [os.path.exists(os.path.dirname(paths[1]))]
                renames += [paths]
        for path in mkdirs:
            os.makedirs(path)
        for paths in renames:
            if os.path.exists(paths[1]):
                LOGGER.info("remove previous path %s", paths[1])
                shutil.rmtree(paths[1])
            os.rename(paths[0], paths[1])
    finally:
        # Always delete the temporary directory, exception raised or not.
        shutil.rmtree(tmp_dir)

def remove_theme(theme_name):
    """
    Remove assets and templates directories.
    """
    theme_dir = get_theme_dir(theme_name)
    public_dir = safe_join(settings.PUBLIC_ROOT, theme_name)
    LOGGER.info("remove theme '%s', that is directories %s and %s.",
                   theme_name, theme_dir, public_dir)
    try:
        shutil.rmtree(theme_dir)
    except FileNotFoundError:
        pass
    try:
        shutil.rmtree(public_dir)
    except FileNotFoundError:
        pass
<|endoftext|>