What is the difference between eval $(opam env --switch={switch} --set-switch) and opam switch set SWITCH? - opam

The bounty expires in 5 days. Answers to this question are eligible for a +500 reputation bounty.
Charlie Parker is looking for an answer from a reputable source.
It seems we have two different ways to activate the current opam switch environment. So my questions are:
Which one is the preferred method?
What is the difference between the two? eval $(opam env --switch=$SWITCH --set-switch) vs opam switch set $SWITCH
Thanks!
Context
I need to change opam envs within python due to my applicaiton (no way around this 100%).
Usually I do:
eval $(opam env --switch={switch} --set-switch)
but this gives an issue (see end).
Thus, going to try:
opam switch set {switch}
are these truly equivalent?
(Note: in python opam switch set {switch} seems to work, but still like to understand why there are two version)
For context error:
Traceback (most recent call last):
File "/lfs/ampere4/0/brando9/iit-term-synthesis/iit-term-synthesis-src/data_pkg/data_gen.py", line 510, in <module>
main()
File "/lfs/ampere4/0/brando9/iit-term-synthesis/iit-term-synthesis-src/data_pkg/data_gen.py", line 497, in main
asyncio.run(create_dataset(path_2_save_new_dataset_all_splits=args.path_to_save_new_dataset,
File "/dfs/scratch0/brando9/anaconda/envs/iit_synthesis/lib/python3.9/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/dfs/scratch0/brando9/anaconda/envs/iit_synthesis/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
return future.result()
File "/lfs/ampere4/0/brando9/iit-term-synthesis/iit-term-synthesis-src/data_pkg/data_gen.py", line 437, in create_dataset
coq_proj_data: DataCoqProj = await get_coq_proj_data(coq_proj, split)
File "/lfs/ampere4/0/brando9/iit-term-synthesis/iit-term-synthesis-src/data_pkg/data_gen.py", line 194, in get_coq_proj_data
path2filenames_raw: list[str] = strace_build_coq_project_and_get_filenames(coq_proj)
File "/afs/cs.stanford.edu/u/brando9/pycoq/pycoq/opam.py", line 706, in strace_build_coq_project_and_get_filenames
activate_opam_switch(switch)
File "/afs/cs.stanford.edu/u/brando9/pycoq/pycoq/opam.py", line 892, in activate_opam_switch
raise e
File "/afs/cs.stanford.edu/u/brando9/pycoq/pycoq/opam.py", line 886, in activate_opam_switch
res = subprocess.run(command.split(), check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
File "/dfs/scratch0/brando9/anaconda/envs/iit_synthesis/lib/python3.9/subprocess.py", line 505, in run
with Popen(*popenargs, **kwargs) as process:
File "/dfs/scratch0/brando9/anaconda/envs/iit_synthesis/lib/python3.9/subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/dfs/scratch0/brando9/anaconda/envs/iit_synthesis/lib/python3.9/subprocess.py", line 1821, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'eval'
I think it has something to do with calling subprocesses from within python don't fully understand,
refs:
Is there a way to run the bash eval command in python?
Running `eval $(something)` commands using Python sub-process
Python check_output call to eval (with arguments) fails
eval $(opam env): What is the use of eval `opam config env` or eval $(opam env) and their difference?
https://discuss.ocaml.org/t/is-eval-opam-env-switch-switch-set-switch-equivalent-to-opam-switch-set-switch/10957
related: https://discuss.ocaml.org/t/if-i-want-to-switch-opam-switch-should-i-use-eval-opam-env-switch-switch-name-set-switch-or-opam-switch-set-switch-name-why-are-there-two-ways-to-do-this/11421/7

You seem to use a Python function which tries to find an eval program. Such a program doesn't exist. It is an internal command of a Bourne Shell. This explains the error.
Given the way opam works, what should be useful to do is:
Launch opam env --switch my_switch --set-switch,
Analyse its output (multiple lines with a VAR=value syntax)
Change the environment (os.environ[var] = value)
Afterward, the right ocaml will be found in the modified PATH and will be setup properly.

Related

Running locally with GAE Python second generation

I've been using GAE/P 1st gen for many years and am making the jump to my first GAE/P 2nd gen project.
I'm trying to run it locally like I did with dev_appserver.py for 1st gen apps, but the docs are very confusing in this respect.
I can run Flask on its own (python main.py) but static files don't work (since Flask knows nothing about app.yaml) and the datastore won't work either.
The GAE docs say that dev_appserver.py should work for 2nd gen (except on Windows but I'm on Mac) but when I run it, I get the error below.
This happens when I run dev_appserver.py with Python 2 or 3. I've also set CLOUDSDK_PYTHON to my Python 2 binary as instructed.
So how the heck can I run my GAE/P 2nd gen project locally in way that static files and the datastore work?!?
$ python ~/google-cloud-sdk/bin/dev_appserver.py --application=myapp --support_datastore_emulator=False ./app.yaml
INFO 2019-02-12 00:39:21,885 devappserver2.py:278] Skipping SDK update check.
INFO 2019-02-12 00:39:23,128 api_server.py:275] Starting API server at: http://localhost:51187
INFO 2019-02-12 00:39:23,139 instance_factory.py:71] Detected Python 3.6.5
INFO 2019-02-12 00:39:24,459 instance_factory.py:205] Using pip to install dependency libraries; pip stdout is redirected to /var/folders/yd/xr84fg0946l72fp_cc6j9l540000gr/T/tmpj3SEUZ
INFO 2019-02-12 00:39:24,459 instance_factory.py:211] Running /var/folders/yd/xr84fg0946l72fp_cc6j9l540000gr/T/tmpLtysT3/bin/pip install --upgrade pip
INFO 2019-02-12 00:39:24,469 stub_util.py:357] Applying all pending transactions and saving the datastore
INFO 2019-02-12 00:39:24,469 stub_util.py:360] Saving search indexes
Traceback (most recent call last):
File "/.../google-cloud-sdk/platform/google_appengine/dev_appserver.py", line 96, in <module>
_run_file(__file__, globals())
File "/.../google-cloud-sdk/platform/google_appengine/dev_appserver.py", line 90, in _run_file
execfile(_PATHS.script_file(script_name), globals_)
File "/.../google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 600, in <module>
main()
File "/.../google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 588, in main
dev_server.start(options)
File "/.../google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 360, in start
options.api_host, apiserver.port, wsgi_request_info_)
File "/.../google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/dispatcher.py", line 248, in start
ssl_port)
File "/.../google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/dispatcher.py", line 384, in _create_module
ssl_port=ssl_port)
File "/.../google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/module.py", line 1309, in __init__
super(AutoScalingModule, self).__init__(**kwargs)
File "/.../google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/module.py", line 598, in __init__
self._module_configuration)
File "/.../google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/module.py", line 231, in _create_instance_factory
module_configuration=module_configuration)
File "/.../google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/instance_factory.py", line 98, in __init__
self._SetupVirtualenvFromConfiguration()
File "/.../google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/instance_factory.py", line 143, in _SetupVirtualenvFromConfiguration
self._venv_dir, requirements_file.name)
File "/.../google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/instance_factory.py", line 228, in _SetupVirtualenv
cls._RunPipInstall(venv_dir, requirements_file_name)
File "/.../google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/instance_factory.py", line 212, in _RunPipInstall
pip_proc = subprocess.Popen(pip_cmd, stdout=pip_out)
File "/usr/local/Cellar/python#2/2.7.15/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 394, in __init__
errread, errwrite)
File "/usr/local/Cellar/python#2/2.7.15/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1047, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
After a good night's sleep, I was able to figure out the problem...
I was running dev_appserver.py from within my virtual environment. For some reason, this prevented dev_appserver.py from being able to find pip (needed to install the packages in my requirements.txt).
Exiting my virtual environment before running dev_appserver.py fixed the problem.

Error in creating array variable in Debian 8.4

I have a very basic shell script containing
#!/bin/sh
NAME[0]="Hello"
echo ${NAME[0]}
So when I run this script an error occurs stating
./test.sh: 2: ./test.sh: NAME[0]=Hello: not found
./test.sh: 3: ./test.sh: Bad substitution
So basically I looked through a few tutorials and found this to be the basic way to declare arrays. So I am confused as to why this is an error. Any ideas?
You are starting your script as #!/bin/sh, which has a soft link to dash (The current version of sh which conforms with the POSIX 1003.2 and 1003.2a specifications for the shell) and dash doesn't support arrays. In debian 8 onwards dash has become default shell, so if you run ls -la /bin/sh the output will be /bin/sh -> dash
However bash still remains the default login shell, only the default /bin/sh used in shell scripts has been changed. So if you run your code on terminal, it will work just fine. More information on why this switch was made in Ubuntu can be found here.
If you want to use arrays in your script then you must start your script with #!/bin/bash
So your script works perfectly if modified like this
#!/bin/bash
NAME[0]="Hello"
echo ${NAME[0]}
More information on Dash as Sh DashAsBinSh

Redirect lldb output to file

I'm using lldb inside Xcode, and one of my variables contains a huge chunk of JSON data. Using po myVar isn't much helpful to analyse this data, as it will output in the tiny Xcode debug console.
Is there a way to redirect lldb output to a file ?
I saw here that such a feature seems to be available on gdb as :
(gdb) set logging on
(gdb) set logging file /tmp/mem.txt
(gdb) x/512bx 0xbffff3c0
(gdb) set logging off
and is "translated" in lldb as :
(lldb) memory read --outfile /tmp/mem.txt --count 512 0xbffff3c0
(lldb) me r -o/tmp/mem.txt -c512 0xbffff3c0
(lldb) x/512bx -o/tmp/mem.txt 0xbffff3c0
However, the memory read command will not help in my case, and apparently, --outfile is not available for the print command.
You can use a Python script to do so (and much more), as explained here:
LLDB Python scripting in Xcode
Create a file named po.py in a directory of your choice (for example "~/.lldb"):
import lldb
def print_to_file(debugger, command, result, dict):
#Change the output file to a path/name of your choice
f=open("/Users/user/temp.txt","w")
debugger.SetOutputFileHandle(f,True);
#Change command to the command you want the output of
command = "po self"
debugger.HandleCommand(command)
def __lldb_init_module (debugger, dict):
debugger.HandleCommand('command script add -f po.print_to_file print_to_file ')
Then in lldb write:
command script import ~/.lldb/po.py
print_to_file
I found session save <filename> to be a much better, easier option than those listed here. It's not quite the same as you can't use it (to my knowledge selectively) but for generating logs, it's quite handy.
Here is a slight modification incorporating some of the comments from above:
def toFile(debugger, command, result, dict):
f=open("/Users/user/temp.txt","w")
debugger.SetOutputFileHandle(f,True);
debugger.HandleCommand(command)
f.close()
debugger.SetOutputFileHandle(sys.stdout, True)
This allows the command to be supplied as an argument, and reverts the output file handle to stdout after the command is run.
Assuming that you have a variable named jsonData (which has a Data type) you can save it to a file with this command:
expr jsonData.write(to: URL(fileURLWithPath: "/tmp/datadump.bin"))
Alternatively instead of above command you could dump memory used by this variable to a file as in the example below:
(lldb) po jsonData
▿ Optional<Data>
▿ some : 32547 bytes
- count : 32547
▿ pointer : 0x00007fe8b69bb410
- pointerValue : 140637472797712
(lldb) memory read --force --binary --outfile /tmp/datadump.bin --count 32547 0x00007fe8b69bb410
32547 bytes written to '/tmp/datadump.bin'

ipython %run testscript_in_pythonpath.py returns "no file found" error

I would like to use the %run magic command to run a script in a directory which is in the pythonpath variable. The script reads some files in the working directory. However, when I try to run the script using the command: %run "testscript_in_pythonpath.py ", it returns an error. I thought files in pythonpath would be accessible to the interpreter, no ??
(Reposting as an answer)
$PYTHONPATH is what Python uses to look up modules to import, not scripts to run.
To run a file from $PYTHONPATH, you can do import testscript_in_pythonpath. Or, in IPython:
%run -m testscript_in_pythonpath
The difference is that if the file has an if __name__ == '__main__': section, %run will trigger that.
From a system shell, you can do the same thing as:
python -m testscript_in_pythonpath

tcshrc setting path getting error

im trying to set the environment path to run pintos command like this in my home floder under ubuntu
set path = ($path /home/pintos/src/utils)
and I type terminal command try to compile this
:~$ source .tcshrc
but it seems get error like this
bash: .tcshrc: line 1: syntax error near unexpected token `('
bash: .tcshrc: line 1: `set path = ($path /home/pintos/src/utils)'
I dont know where is the syntax error is ...
You source your script, which is perfectly valid for [t]csh, into your running shell, which happens to be bash (and not tcsh).
If you're going to use tcsh, just run it (by typing tcsh) and ensure that your ~/.tcshrc has the desired effect. (Then, maybe, use chsh to change your login shell).
If you're going to use bash, set path using PATH=$PATH:/home/pintos/src/utils, in ~/.bashrc and/or in ~/.bash_profile.

Resources