Exemplo n.º 1
0
def get_contents(meta_path):
    """
    Get the contents of the [meta.yaml|conda.yaml] file.
    If jinja is installed, then the template.render function is called
    before standard conda macro processors
    """
    try:
        import jinja2
    except ImportError:
        print("There was an error importing jinja2.", file=sys.stderr)
        print("Please run `conda install jinja2` to enable jinja template support", file=sys.stderr)
        with open(meta_path) as fd:
            return fd.read()

    from conda_build.jinja_context import context_processor

    path, filename = os.path.split(meta_path)
    loaders = [jinja2.PackageLoader("conda_build"), jinja2.FileSystemLoader(path)]
    env = jinja2.Environment(loader=jinja2.ChoiceLoader(loaders))
    env.globals.update(ns_cfg())
    env.globals.update(context_processor())

    template = env.get_or_select_template(filename)

    contents = template.render(environment=env)
    return contents
Exemplo n.º 2
0
def get_contents(meta_path):
    '''
    Get the contents of the [meta.yaml|conda.yaml] file.
    If jinja is installed, then the template.render function is called
    before standard conda macro processors
    '''
    try:
        import jinja2
    except ImportError:
        print("There was an error importing jinja2.")
        print("Please run `conda install jinja2` to enable jinja template support")
        with open(meta_path, encoding='utf-8') as fd:
            return fd.read()

    from conda_build.jinja_context import context_processor

    path, filename = os.path.split(meta_path)
    loaders = [jinja2.PackageLoader('conda_build'),
               jinja2.FileSystemLoader(path)
               ]
    env = jinja2.Environment(loader=jinja2.ChoiceLoader(loaders))
    env.globals.update(context_processor())
    env.globals.update(ns_cfg())

    template = env.get_or_select_template(filename)

    contents = template.render(environment=env)
    return contents
Exemplo n.º 3
0
    def _get_contents(self, permit_undefined_jinja):
        '''
        Get the contents of our [meta.yaml|conda.yaml] file.
        If jinja is installed, then the template.render function is called
        before standard conda macro processors.

        permit_undefined_jinja: If True, *any* use of undefined jinja variables will
                                evaluate to an emtpy string, without emitting an error.
        '''
        try:
            import jinja2
        except ImportError:
            print("There was an error importing jinja2.", file=sys.stderr)
            print("Please run `conda install jinja2` to enable jinja template support", file=sys.stderr)  # noqa
            with open(self.meta_path) as fd:
                return fd.read()

        from conda_build.jinja_context import context_processor, UndefinedNeverFail, FilteredLoader

        path, filename = os.path.split(self.meta_path)
        loaders = [  # search relative to '<conda_root>/Lib/site-packages/conda_build/templates'
                   jinja2.PackageLoader('conda_build'),
                   # search relative to RECIPE_DIR
                   jinja2.FileSystemLoader(path)
                   ]

        # search relative to current conda environment directory
        conda_env_path = os.environ.get('CONDA_DEFAULT_ENV')  # path to current conda environment
        if conda_env_path and os.path.isdir(conda_env_path):
            conda_env_path = os.path.abspath(conda_env_path)
            conda_env_path = conda_env_path.replace('\\', '/')  # need unix-style path
            env_loader = jinja2.FileSystemLoader(conda_env_path)
            loaders.append(jinja2.PrefixLoader({'$CONDA_DEFAULT_ENV': env_loader}))

        undefined_type = jinja2.StrictUndefined
        if permit_undefined_jinja:
            # The UndefinedNeverFail class keeps a global list of all undefined names
            # Clear any leftover names from the last parse.
            UndefinedNeverFail.all_undefined_names = []
            undefined_type = UndefinedNeverFail

        loader = FilteredLoader(jinja2.ChoiceLoader(loaders))
        env = jinja2.Environment(loader=loader, undefined=undefined_type)

        env.globals.update(ns_cfg())
        env.globals.update(context_processor(self, path))

        try:
            template = env.get_or_select_template(filename)
            rendered = template.render(environment=env)

            if permit_undefined_jinja:
                self.undefined_jinja_vars = UndefinedNeverFail.all_undefined_names
            else:
                self.undefined_jinja_vars = []

            return rendered
        except jinja2.TemplateError as ex:
            sys.exit("Error: Failed to render jinja template in {}:\n{}"
                     .format(self.meta_path, ex.message))
Exemplo n.º 4
0
def get_contents(meta_path):
    '''
    Get the contents of the [meta.yaml|conda.yaml] file.
    If jinja is installed, then the template.render function is called
    before standard conda macro processors
    '''
    try:
        import jinja2
    except ImportError:
        print("There was an error importing jinja2.", file=sys.stderr)
        print(
            "Please run `conda install jinja2` to enable jinja template support",
            file=sys.stderr)
        with open(meta_path) as fd:
            return fd.read()

    from conda_build.jinja_context import context_processor

    path, filename = os.path.split(meta_path)
    loaders = [  # search relative to '<conda_root>/Lib/site-packages/conda_build/templates'
        jinja2.PackageLoader('conda_build'),
        # search relative to RECIPE_DIR
        jinja2.FileSystemLoader(path)
    ]

    # search relative to current conda environment directory
    conda_env_path = os.environ.get(
        'CONDA_DEFAULT_ENV')  # path to current conda environment
    if conda_env_path and os.path.isdir(conda_env_path):
        conda_env_path = os.path.abspath(conda_env_path)
        conda_env_path = conda_env_path.replace('\\',
                                                '/')  # need unix-style path
        env_loader = jinja2.FileSystemLoader(conda_env_path)
        loaders.append(jinja2.PrefixLoader({'$CONDA_DEFAULT_ENV': env_loader}))

    env = jinja2.Environment(loader=jinja2.ChoiceLoader(loaders),
                             undefined=jinja2.StrictUndefined)
    env.globals.update(ns_cfg())
    env.globals.update(context_processor())

    template = env.get_or_select_template(filename)

    try:
        return template.render(environment=env)
    except jinja2.TemplateError as ex:
        sys.exit("Error: Failed to parse jinja template in {}:\n{}".format(
            meta_path, ex.message))
Exemplo n.º 5
0
def get_contents(meta_path):
    '''
    Get the contents of the [meta.yaml|conda.yaml] file.
    If jinja is installed, then the template.render function is called
    before standard conda macro processors
    '''
    try:
        import jinja2
    except ImportError:
        print("There was an error importing jinja2.", file=sys.stderr)
        print("Please run `conda install jinja2` to enable jinja template support", file=sys.stderr)
        with open(meta_path) as fd:
            return fd.read()

    from conda_build.jinja_context import context_processor

    path, filename = os.path.split(meta_path)
    loaders = [# search relative to '<conda_root>/Lib/site-packages/conda_build/templates'
               jinja2.PackageLoader('conda_build'),
               # search relative to RECIPE_DIR
               jinja2.FileSystemLoader(path)
               ]

    # search relative to current conda environment directory
    conda_env_path = os.environ.get('CONDA_DEFAULT_ENV')  # path to current conda environment
    if conda_env_path and os.path.isdir(conda_env_path):
        conda_env_path = os.path.abspath(conda_env_path)
        conda_env_path = conda_env_path.replace('\\', '/') # need unix-style path
        env_loader = jinja2.FileSystemLoader(conda_env_path)
        loaders.append(jinja2.PrefixLoader({'$CONDA_DEFAULT_ENV': env_loader}))

    env = jinja2.Environment(loader=jinja2.ChoiceLoader(loaders), undefined=jinja2.StrictUndefined)
    env.globals.update(ns_cfg())
    env.globals.update(context_processor())

    template = env.get_or_select_template(filename)

    try:
        return template.render(environment=env)
    except jinja2.TemplateError as ex:
        sys.exit("Error: Failed to parse jinja template in {}:\n{}".format(meta_path, ex.message))
Exemplo n.º 6
0
    def _get_contents(self, permit_undefined_jinja, config):
        '''
        Get the contents of our [meta.yaml|conda.yaml] file.
        If jinja is installed, then the template.render function is called
        before standard conda macro processors.

        permit_undefined_jinja: If True, *any* use of undefined jinja variables will
                                evaluate to an emtpy string, without emitting an error.
        '''
        try:
            import jinja2
        except ImportError:
            print("There was an error importing jinja2.", file=sys.stderr)
            print(
                "Please run `conda install jinja2` to enable jinja template support",
                file=sys.stderr)  # noqa
            with open(self.meta_path) as fd:
                return fd.read()

        from conda_build.jinja_context import context_processor, UndefinedNeverFail, FilteredLoader

        path, filename = os.path.split(self.meta_path)
        loaders = [  # search relative to '<conda_root>/Lib/site-packages/conda_build/templates'
            jinja2.PackageLoader('conda_build'),
            # search relative to RECIPE_DIR
            jinja2.FileSystemLoader(path)
        ]

        # search relative to current conda environment directory
        conda_env_path = os.environ.get(
            'CONDA_DEFAULT_ENV')  # path to current conda environment
        if conda_env_path and os.path.isdir(conda_env_path):
            conda_env_path = os.path.abspath(conda_env_path)
            conda_env_path = conda_env_path.replace(
                '\\', '/')  # need unix-style path
            env_loader = jinja2.FileSystemLoader(conda_env_path)
            loaders.append(
                jinja2.PrefixLoader({'$CONDA_DEFAULT_ENV': env_loader}))

        undefined_type = jinja2.StrictUndefined
        if permit_undefined_jinja:
            # The UndefinedNeverFail class keeps a global list of all undefined names
            # Clear any leftover names from the last parse.
            UndefinedNeverFail.all_undefined_names = []
            undefined_type = UndefinedNeverFail

        loader = FilteredLoader(jinja2.ChoiceLoader(loaders), config=config)
        env = jinja2.Environment(loader=loader, undefined=undefined_type)

        env.globals.update(ns_cfg(config))
        env.globals.update(
            context_processor(self,
                              path,
                              config=config,
                              permit_undefined_jinja=permit_undefined_jinja))

        try:
            template = env.get_or_select_template(filename)
            rendered = template.render(environment=env)

            if permit_undefined_jinja:
                self.undefined_jinja_vars = UndefinedNeverFail.all_undefined_names
            else:
                self.undefined_jinja_vars = []
            return rendered

        except jinja2.TemplateError as ex:
            if "'None' has not attribute" in str(ex):
                ex = "Failed to run jinja context function"
            sys.exit(
                "Error: Failed to render jinja template in {}:\n{}".format(
                    self.meta_path, str(ex)))
Exemplo n.º 7
0
def get_contents(meta_path, permit_undefined_jinja):
    '''
    Get the contents of the [meta.yaml|conda.yaml] file.
    If jinja is installed, then the template.render function is called
    before standard conda macro processors.

    permit_undefined_jinja: If True, *any* use of undefined jinja variables will
                            evaluate to an emtpy string, without emitting an error.
    '''
    try:
        import jinja2
    except ImportError:
        print("There was an error importing jinja2.", file=sys.stderr)
        print("Please run `conda install jinja2` to enable jinja template support", file=sys.stderr)
        with open(meta_path) as fd:
            return fd.read()

    from conda_build.jinja_context import context_processor

    path, filename = os.path.split(meta_path)
    loaders = [# search relative to '<conda_root>/Lib/site-packages/conda_build/templates'
               jinja2.PackageLoader('conda_build'),
               # search relative to RECIPE_DIR
               jinja2.FileSystemLoader(path)
               ]

    # search relative to current conda environment directory
    conda_env_path = os.environ.get('CONDA_DEFAULT_ENV')  # path to current conda environment
    if conda_env_path and os.path.isdir(conda_env_path):
        conda_env_path = os.path.abspath(conda_env_path)
        conda_env_path = conda_env_path.replace('\\', '/') # need unix-style path
        env_loader = jinja2.FileSystemLoader(conda_env_path)
        loaders.append(jinja2.PrefixLoader({'$CONDA_DEFAULT_ENV': env_loader}))

    undefined_type = jinja2.StrictUndefined
    if permit_undefined_jinja:
        class UndefinedNeverFail(jinja2.Undefined):
            """
            A class for Undefined jinja variables.
            This is even less strict than the default jinja2.Undefined class,
            because we permits things like {{ MY_UNDEFINED_VAR[:2] }} and {{ float(MY_UNDEFINED_VAR) }}.
            This can mask lots of errors in jinja templates, so it should only be used for a first-pass
            parse, when you plan on running a 'strict' second pass later.
            """
            __add__ = __radd__ = __mul__ = __rmul__ = __div__ = __rdiv__ = \
            __truediv__ = __rtruediv__ = __floordiv__ = __rfloordiv__ = \
            __mod__ = __rmod__ = __pos__ = __neg__ = __call__ = \
            __getitem__ = __lt__ = __le__ = __gt__ = __ge__ = __int__ = \
            __complex__ = __pow__ = __rpow__ = \
                lambda *args, **kwargs: ''

            __int__ = lambda _: 0
            __float__ = lambda _: 0.0

        undefined_type = UndefinedNeverFail

    env = jinja2.Environment(loader=jinja2.ChoiceLoader(loaders), undefined=undefined_type)
    env.globals.update(ns_cfg())
    env.globals.update(context_processor())

    try:
        template = env.get_or_select_template(filename)
        return template.render(environment=env)
    except jinja2.TemplateError as ex:
        sys.exit("Error: Failed to render jinja template in {}:\n{}".format(meta_path, ex.message))
Exemplo n.º 8
0
    def _get_contents(self, permit_undefined_jinja):
        '''
        Get the contents of our [meta.yaml|conda.yaml] file.
        If jinja is installed, then the template.render function is called
        before standard conda macro processors.

        permit_undefined_jinja: If True, *any* use of undefined jinja variables will
                                evaluate to an emtpy string, without emitting an error.
        '''
        try:
            import jinja2
        except ImportError:
            print("There was an error importing jinja2.", file=sys.stderr)
            print(
                "Please run `conda install jinja2` to enable jinja template support",
                file=sys.stderr)
            with open(self.meta_path) as fd:
                return fd.read()

        from conda_build.jinja_context import context_processor

        path, filename = os.path.split(self.meta_path)
        loaders = [  # search relative to '<conda_root>/Lib/site-packages/conda_build/templates'
            jinja2.PackageLoader('conda_build'),
            # search relative to RECIPE_DIR
            jinja2.FileSystemLoader(path)
        ]

        # search relative to current conda environment directory
        conda_env_path = os.environ.get(
            'CONDA_DEFAULT_ENV')  # path to current conda environment
        if conda_env_path and os.path.isdir(conda_env_path):
            conda_env_path = os.path.abspath(conda_env_path)
            conda_env_path = conda_env_path.replace(
                '\\', '/')  # need unix-style path
            env_loader = jinja2.FileSystemLoader(conda_env_path)
            loaders.append(
                jinja2.PrefixLoader({'$CONDA_DEFAULT_ENV': env_loader}))

        undefined_type = jinja2.StrictUndefined
        if permit_undefined_jinja:

            class UndefinedNeverFail(jinja2.Undefined):
                """
                A class for Undefined jinja variables.
                This is even less strict than the default jinja2.Undefined class,
                because it permits things like {{ MY_UNDEFINED_VAR[:2] }} and {{ MY_UNDEFINED_VAR|int }}.
                This can mask lots of errors in jinja templates, so it should only be used for a first-pass
                parse, when you plan on running a 'strict' second pass later.
                """
                __add__ = __radd__ = __mul__ = __rmul__ = __div__ = __rdiv__ = \
                __truediv__ = __rtruediv__ = __floordiv__ = __rfloordiv__ = \
                __mod__ = __rmod__ = __pos__ = __neg__ = __call__ = \
                __getitem__ = __lt__ = __le__ = __gt__ = __ge__ = \
                __complex__ = __pow__ = __rpow__ = \
                    lambda *args, **kwargs: UndefinedNeverFail()

                __str__ = __repr__ = \
                    lambda *args, **kwargs: u''

                __int__ = lambda _: 0
                __float__ = lambda _: 0.0

                def __getattr__(self, k):
                    try:
                        return object.__getattr__(self, k)
                    except AttributeError:
                        return UndefinedNeverFail()

                def __setattr__(self, k, v):
                    pass

            undefined_type = UndefinedNeverFail

        env = jinja2.Environment(loader=jinja2.ChoiceLoader(loaders),
                                 undefined=undefined_type)
        env.globals.update(ns_cfg())
        env.globals.update(context_processor(self, path))

        try:
            template = env.get_or_select_template(filename)
            return template.render(environment=env)
        except jinja2.TemplateError as ex:
            sys.exit(
                "Error: Failed to render jinja template in {}:\n{}".format(
                    self.meta_path, ex.message))