Giter Club home page Giter Club logo

Comments (10)

nicoddemus avatar nicoddemus commented on May 28, 2024

Hi @phausamann,

I like the idea! Indeed we also use environment variables to control certain aspects of the devenv file.

Perhaps we should also think about how to validate/require such arguments.

For example, we often use an environment variable to specify a Python version to use. Also, we often want that option to be mandatory, so users don't forget it:

{% set CONDA_PY = os.environ['CONDA_PY'] %}

...

{% if CONDA_PY == '36' %}
  - python ==3.6.7
{% else %}
  - python ==CONDA_PY_INVALID
{% endif %}

So if the user forgets to set CONDA_PY, he will get a KeyError, and if he specifies an invalid CONDA_PY, it will get an error from conda saying that CONDA_PY_INVALID is not a valid Python version.

This of course are all home-grown workarounds, which are fine for usage inside a development team which knows what's going on, but we might brainstorm a a more robust/builtin solution.

from conda-devenv.

phausamann avatar phausamann commented on May 28, 2024

An option with minimal implementation effort would probably be to have:

parser.add_argument(
    "-e",
    "--env_var",
    action="append",
)

and add those arguments to os.environ, so users could specify environment variables like this:

$ conda devenv -e PKG_DEV -e CONDA_PY=3.6

Alternatively, something like this would probably be more readable:

$ conda devenv -a dev -a py=3.6

with

- python ={{ required_arg("py", valid=("3.6", "3.7")) }}
{% if optional_arg("dev") %}
- ...
{% endif %}

from conda-devenv.

nicoddemus avatar nicoddemus commented on May 28, 2024

I like the second option, definitely!

@tadeu @prusse-martin what you guys think?

from conda-devenv.

prusse-martin avatar prusse-martin commented on May 28, 2024

I agree that the second option looks better =)
Also, I like the required_arg/optional_arg idea.

from conda-devenv.

tadeu avatar tadeu commented on May 28, 2024

I liked the first solution better (the one that allows defining extra environment variables through -e flags), because the implementation is simple and because it's an extension of a mechanism that is already used, instead of a new different mechanism.

Environment variables are nice because we can define them once and use conda devenv many times in the same shell session without having to pass extra arguments. The -e flags would be a nice extension for when we need to be more explicit about the variables being passed down.

from conda-devenv.

tadeu avatar tadeu commented on May 28, 2024

Now about a different issue, but if it's a problem how environment variables are handled by jinja (too "raw", needing to use os.environ), we could try to improve how they are handled, for example adding better error treatment, allowing selectors based on environment variables, also allowing pre-defined jinja variables based on environment variables, etc. It would be yet another extension complementing the -e feature.

from conda-devenv.

prusse-martin avatar prusse-martin commented on May 28, 2024

Using the -e approach how about:

  • has_env(env_var_name) check if variable is defined;
  • get_env(env_var_name, default=SENTRY, *, accepted_values=SENTRY) get the variable value, if variable is not defined return the default value, if not defined and no default given give an error (required variable?), if accepted values is defined and the return value is not one in that list give an error (not accepted value?);

I would say no to selectors (I assume you are referring to conda build meta.yaml like selectors), but since I do not remember any good argument against it, that is not a "hard no".

from conda-devenv.

nicoddemus avatar nicoddemus commented on May 28, 2024

get the variable value, if variable is not defined return the default value, if not defined and no default given give an error (required variable?), if accepted values is defined and the return value is not one in that list give an error (not accepted value?);

Yes to the inlined questions.

get_env is a nice shortcut that adds the opportunity to provide better error messages for enumerated and required variables.

has_env doesn't add much, being just a shortcut:

{% if "DEV" in os.environ %}

vs:

{% if has_env("DEV") %}

So I would suggest to don't bother with has_env for now, unless there's a use case where it simplifies things that I'm not seeing here.

Back to the original example then:

- python ={{ get_env("PY", valid=["3.6", "3.7"]) }}
{% if "DEV" in os.environ %}
- ...
{% endif %}

With that, we can also accept -e in the command line to set environment variables in the context of Jinja evaluation as @tadeu suggested:

conda devenv -e DEV=1 PY=3.6

@phausamann what do you think?

from conda-devenv.

phausamann avatar phausamann commented on May 28, 2024

I think get_env is a good solution, I'll start working on a PR.

from conda-devenv.

nicoddemus avatar nicoddemus commented on May 28, 2024

Awesome @phausamann, thanks! If you hit any roadblocks, feel free to open a PR anyway and we can discuss it over there.

from conda-devenv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.