python subprocess: how to run an app on OS X? - python

I am porting a windows application to OS X 10.6.8. It is a new platform for me and I am facing some difficulties.
The application is a small webserver (bottle+waitress) which is starting a browser (based on chromium embedded framework) thanks to a subprocess call.
The browser is an app file and runs ok when started from gui.
I am launching it this way:
subprocess.Popen([os.getcwd()+"/cef/cefclient.app", '--url=http://127.0.0.1:8100'])
Unfortunately, this fails with OSError: permission denied.
I tried to run the script with a sudo with similar result.
I can launch the app from shell with the following command:
open -a "cef/cefclient.app" --args --url-http://127.0.0.1:8100
But
subprocess.Popen(['open', '-a', os.getcwd()+'/cef/cefclient.app', '--args', '--url-http://127.0.0.1:8100'])
fails with the following error
FSPathMakeRef(/Users/.../cefclient.app) failed with error -43.
Any idea how to fix this issue?

The file cefclient.app is actually a directory (an application bundle, specifically), not the application executable. The real executable is located inside the bundle, with a path like Contents/MacOS/executable_name. So to launch it, you'd do this:
subprocess.Popen([os.getcwd()+"/cef/cefclient.app/Content/MacOS/executable_name",
"--url=http://127.0.0.1:8100"])

Alternatively,
os.system('open -a "cef/cefclient.app" --args --url-http://127.0.0.1:8100')
Just depends if you want to control stdin / stdout or if starting the app is enough.

Related

Using MobaXterm as Linux shell on Windows

I have a Python script which leverages subprocess to call MobaXterm and use it to run a command to my server through SSH. The script works fine when using the Windows Subsystem for Linux (WSL), but fails when using Moba. This is the code:
import subprocess
moba_path = "C:\Program Files (x86)\Mobatek\MobaXterm\MobaXterm.exe"
subprocess.run(f'{moba_path} -exec ssh my_server "mkdir test_dir"')
It opens the MobaXterm window but does not show any sign of command execution. I checked, and the command has not been executed (the folder has not been created).
Any ideas?
So I found a way to execute my commands using Moba in a programmatic way using the -newtab command. It starts a new tab which then runs the specified command.
import subprocess
moba_path = "C:\Program Files (x86)\Mobatek\MobaXterm\MobaXterm.exe"
subprocess.run(f'{moba_path} -newtab ssh my_server "mkdir test_dir"')

How to launch gnome-terminal from dbus plugin, a systemd service, using Python

I use Python to develop a simple dbus based plugin (dbus.service.Object) that takes IPC calls from a command-line script. The dbus service is configured to host in systemd. It is running as expected. However, when I add a capability in the plugin to launch a gnome-terminal it failed. It seems the execution is stuck in any of these subprocess calls, no matter it is call() or Popen(). Wonder how can I make the plugin launch gnome-terminal correctly?
#subprocess.call(['gnome-terminal', '--', '/bin/bash', '-c', 'ls', '-al', '&'])
#subprocess.call(['systemd-run', '--user', '--service-type=forking', 'gnome-terminal', '-t', "Test",'--','/bin/bash', '-c', 'ls', '-al', "&"])
#subprocess.call(['DISPLAY=:0', 'gnome-terminal', '--', '/bin/bash', '-c', 'ls', '-al', '&'])
Things I also tried to launch a python script from the plugin and have the script to launch gnome-terminal but still failed.
Fail means that the terminal does not open and the ps doesn't show terminal. I also tried with os.system. It works if running directly from a console.
os.system('gnome-terminal -- /bin/bash -c "python /data/scripts/test.a.1.py -f /data/station_profile_A.json"')
The journal log shows the error below:
Dec 21 09:34:20 ubuntu coordinator[5380]: Unable to init server: Could not connect: Connection refused
Dec 21 09:34:20 ubuntu coordinator[5380]: # Failed to parse arguments: Cannot open display:
If I add 'DISPLAY=:0' in front of gnome-terminal, it doesn't help.
To connect to X, you need to set both DISPLAY and XAUTHORITY.
For example XAUTHORITY=$HOME/.Xauthority and DISPLAY=:0.0.
The file named by XAUTHORITY must be readable. The file is mode 0600.
I would suggest that you have your DBUS API include handing over a copy of the magic cookie, the contents of the XAUTHORITY file, to the service which then would write it to a temporary file and then with DISPLAY and XAUTHORITY set would run the command.

Can't run PyCharm interpreter as root

I am trying to use Kubernetes python SDK.
I tried to run the following code:
from kubernetes import client, config
# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()
v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
It failed with lots of errors.
When I run the same code with python from a shell, the same issue.
When I run the same code with sudo python from shell, it works.
I am trying to run PyCharm interperte as root.
Following the instruction from JetBrains, I created a script shell with the name pythonCustomInt.sh that contains:
sudo python
I went to PyCharm settings > Project Interpreter and changed the Base interpreter to /<path>/pythonCutomInt.sh but it writes an error:
Environment location directory is not empty
I am not sure where I need to put the script.
Any idea?
I ran sudo -s and then from the pycharm folder (pycharm-community-2018.1.4/bin) I ran sh ./pycharm.sh and it worked.

Start django app as service

I want to create service that will be start with ubuntu and will have ability to use django models etc..
This service will create thread util.WorkerThread and wait some data in main.py
if __name__ == '__main__':
bot.polling(none_stop=True)
How I can to do this. I just don't know what I need to looking for.
If you also can say how I can create ubuntu autostart service with script like that, please tell me )
P.S. all django project run via uwsgi in emperor mode.
The easiest way in my opinion is create a script and run on crontab.
First of all create a script to start your django app.
#!/bin/bash
cd /path/to your/virtual environment #path to your virtual environment
. bin/activate #Activate your virtual environment
cd /path/to your/project directory #After that go to your project directory
python manage.py runserver #run django server
Save the script and open crontab with the command:
crontab -e
Now edit the crontab file and write on the last line:
#reboot path/to/your/script.sh
This way is not the best but the easiest, if you are not comfortable with Linux startup service creation.
I hope this help you :)
Take a look at supervisord. It is much easier than daemonizing python script.
Config it something like this:
[program:watcher]
command = /usr/bin/python /path/to/main.py
stdout_logfile = /var/log/main-stdout.log
stdout_logfile_maxbytes = 10MB
stdout_logfile_backups = 5
stderr_logfile = /var/log/main-stderr.log
stderr_logfile_maxbytes = 10MB
stderr_logfile_backups = 5
Ok, that is answer - https://www.raspberrypi-spy.co.uk/2015/10/how-to-autorun-a-python-script-on-boot-using-systemd/
In new versions ubuntu services .conf in /etc/init fail with error Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart: Connection refused
But services works using systemd

how to debug when flask blocking

I was running flask in a public_ip server(with some users)
run command is
(host='0.0.0.0', port=80, debug=True)
but flask server blocking somewhere one hour ago (the log show last request is one hour before)
so how can I debug on it?(figure out it's blocking on which python line?)
I have tried
gdb python3.4-dbg pid,but my flask app can't run by python3.4-dbg,for
from PIL import _imaging as core
ImportError: cannot import name '_imaging'
I believe the command is:
gdb -p pid
to attach to a running process.
oh,I find a way
after install python-dbg
use gdb python pid to attach flask
and use py-bt py-list py-locals to check the blocking stack

Categories