0

I am aware that one can't easily run eval from python (though I admit I don't fully understand why). I've been trying a bunch of things -- even using opem exec --switch $SWITCH $command_substitution_output to do it but I can't get it to work.

This is my attempt:

def run_eval_opam_env(switch: str,
                      ):
    """
Tries to run through opam exec command arg https://opam.ocaml.org/doc/man/opam-exec.html.
eval $(opam env --switch=switch --set-switch)
    note:
        - command substituion: replace the command ran here $(cmd) with its output (string).
        It turns out, $() is called a command substitution. The command in between $() or backticks (“) is run and the
        output replaces $().
    ref:
        - command sub: https://blog.wplauncher.com/what-is-in-linux/#:~:text=Example%20of%20command%20substitution%20using%20%24()%20in%20Linux%3A&text=Again%2C%20%24()%20is%20a%20command,another%20context%E2%80%9D%20(Source).
        - eval $(opam env): https://stackoverflow.com/questions/30155960/what-is-the-use-of-eval-opam-config-env-or-eval-opam-env-and-their-differen?noredirect=1&lq=1
        - there is a way with opam exec: https://discuss.ocaml.org/t/is-eval-opam-env-switch-switch-set-switch-equivalent-to-opam-switch-set-switch/10957/4
    """
    logging.info(f'{run_eval_opam_env=}')
    # - get cmd sub output
    try:
        cmd_sub_output: str = subprocess.check_output(f'opam env --switch={switch} --set-switch'.split())
    except Exception as e:
        logging.critical(f'Error: {e=}')
        raise e
    # - run command to eval
    # command: str = f'eval {cmd_sub_output}'  # doesn't seem possible https://stackoverflow.com/questions/53950225/python-check-output-call-to-eval-with-arguments-fails
    command: list[str] = f'opam exec --switch {switch}'.split()
    # command: list[str] = command + cmd_sub_output.split()
    command: list[str] = command + [cmd_sub_output]
    logging.info(f"-> {command=}")
    try:
        # res = subprocess.run(command.split(), check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        res = subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        logging.info(f'{res.stdout.decode()=}')
        logging.info(f'{res.stderr.decode()=}')
    except Exception as e:
        logging.critical(f'Error: {e=}')
        raise e

the output of the command substitution is a bunch of env stuff. How do I excute it in a subprocess or in the opam exec command?

See it's:

b"OPAMSWITCH='ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1'; export OPAMSWITCH;\nOPAM_SWITCH_PREFIX='/lfs/ampere4/0/brando9/.opam/ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1'; export OPAM_SWITCH_PREFIX;\nCAML_LD_LIBRARY_PATH='/lfs/ampere4/0/brando9/.opam/ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1/lib/stublibs:/lfs/ampere4/0/brando9/.opam/ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1/lib/ocaml/stublibs:/lfs/ampere4/0/brando9/.opam/ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1/lib/ocaml'; export CAML_LD_LIBRARY_PATH;\nOCAML_TOPLEVEL_PATH='/lfs/ampere4/0/brando9/.opam/ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1/lib/toplevel'; export OCAML_TOPLEVEL_PATH;\nMANPATH=':/usr/kerberos/man:/usr/share/man:/usr/local/man:/usr/man:/opt/puppetlabs/puppet/share/man:/lfs/ampere4/0/brando9/.opam/ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1/man'; export MANPATH;\nPATH='/lfs/ampere4/0/brando9/.opam/ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1/bin:/dfs/scratch0/brando9/anaconda/envs/iit_synthesis/bin:/lfs/ampere4/0/brando9/.local/bin:/lfs/ampere4/0/brando9/.ruby-build/bin:/lfs/ampere4/0/brando9/.rbenv/shims:/lfs/ampere4/0/brando9/.rbenv/bin:/usr/local/cuda-11.7/bin:/dfs/scratch0/brando9/anaconda/bin:/dfs/scratch0/brando9/anaconda/condabin:/dfs/scratch0/brando9/anaconda/bin:/dfs/scratch0/brando9/.local/bin:/dfs/scratch0/brando9/.ruby-build/bin:/dfs/scratch0/brando9/.rbenv/shims:/dfs/scratch0/brando9/.rbenv/bin:/usr/local/cuda-11.7/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/afs/cs/software/sbin:/afs/cs/software/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/opt/puppetlabs/bin'; export PATH;\n"

but the cmd seems like nonsense:

subprocess.CalledProcessError: Command '['opam', 'exec', '--switch', 'ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1', b"OPAMSWITCH='ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1'; export OPAMSWITCH;\nOPAM_SWITCH_PREFIX='/lfs/ampere4/0/brando9/.opam/ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1'; export OPAM_SWITCH_PREFIX;\nCAML_LD_LIBRARY_PATH='/lfs/ampere4/0/brando9/.opam/ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1/lib/stublibs:/lfs/ampere4/0/brando9/.opam/ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1/lib/ocaml/stublibs:/lfs/ampere4/0/brando9/.opam/ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1/lib/ocaml'; export CAML_LD_LIBRARY_PATH;\nOCAML_TOPLEVEL_PATH='/lfs/ampere4/0/brando9/.opam/ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1/lib/toplevel'; export OCAML_TOPLEVEL_PATH;\nMANPATH=':/usr/kerberos/man:/usr/share/man:/usr/local/man:/usr/man:/opt/puppetlabs/puppet/share/man:/lfs/ampere4/0/brando9/.opam/ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1/man'; export MANPATH;\nPATH='/lfs/ampere4/0/brando9/.opam/ocaml-variants.4.07.1+flambda_coq-serapi.8.11.0+0.11.1/bin:/dfs/scratch0/brando9/anaconda/envs/iit_synthesis/bin:/lfs/ampere4/0/brando9/.local/bin:/lfs/ampere4/0/brando9/.ruby-build/bin:/lfs/ampere4/0/brando9/.rbenv/shims:/lfs/ampere4/0/brando9/.rbenv/bin:/usr/local/cuda-11.7/bin:/dfs/scratch0/brando9/anaconda/bin:/dfs/scratch0/brando9/anaconda/condabin:/dfs/scratch0/brando9/anaconda/bin:/dfs/scratch0/brando9/.local/bin:/dfs/scratch0/brando9/.ruby-build/bin:/dfs/scratch0/brando9/.rbenv/shims:/dfs/scratch0/brando9/.rbenv/bin:/usr/local/cuda-11.7/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/afs/cs/software/sbin:/afs/cs/software/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/opt/puppetlabs/bin'; export PATH;\n"]'

I also tried just doing feval {cmd_sub_output} without success.


related:

Charlie Parker
  • 5,884
  • 57
  • 198
  • 323

0 Answers0