From 2b9b9efa71a6f7b090238c761d54c27d03520f93 Mon Sep 17 00:00:00 2001
From: Lucio Zambon <lucio.zambon@elettra.eu>
Date: Fri, 4 Apr 2025 01:11:17 +0000
Subject: [PATCH] Update launcher.py

---
 launcher.py | 39 ++++++++++++++++++++++++++++++---------
 1 file changed, 30 insertions(+), 9 deletions(-)

diff --git a/launcher.py b/launcher.py
index 186f20d..390a1a0 100644
--- a/launcher.py
+++ b/launcher.py
@@ -1,18 +1,36 @@
+'''
+cd ~/.local/share/applications/
+cat puma.desktop
+[Desktop Entry]
+Name=Puma
+NoDisplay=true
+Exec=/usr/bin/python3 /home/elettra/tmp/lz/launcher.py %u
+Type=Application
+
+xdg-mime default puma.desktop x-scheme-handler/puma
+'''
 import subprocess
 import sys
 import psutil
 import socket
+import time
 host = socket.gethostname()
 import urllib.request
 
 if len(sys.argv)<2:
     sys.exit('Error: missing executable name')
 
-sys.argv[1] = sys.argv[1].replace('puma://', '')
+argv = ' '.join(sys.argv).replace('%20', ' ').split(' ')
+argv[1] = argv[1].replace('puma://', '')
 feedback = "http://puma-01.elettra.eu/knob/index.php?host="+host+"&pid="
 
+with open('/home/elettra/tmp/lz/launcher_'+host+'.log', 'a') as mylog:
+        # mylog.write(process_name+' '+' '.join(params)+"\n")
+        mylog.write(time.strftime("%Y-%m-%d %H:%M:%S ")+' '.join(argv)+"\n")
+        mylog.flush()
+
 # Path to the executable
-executable_path = ('/runtime/bin/'+sys.argv[1]).replace('/bin/elettra/', '/elettra/bin/')
+executable_path = ('/runtime/bin/'+argv[1]).replace('/bin/elettra/', '/elettra/bin/')
 
 def is_process_running(process_name, params):
     """Check if a process with the given name and parameters is running."""
@@ -27,20 +45,22 @@ def is_process_running(process_name, params):
             pass
     return False
 
-process_name = sys.argv[1].replace('elettra/', '')
-params = sys.argv[2:]
+process_name = argv[1].replace('elettra/', '')
+params = argv[2:]
+
 if is_process_running(process_name, params):
         print(f"The process '{process_name}' with parameters {params} is already running.")
-        sys.argv[1] = executable_path
-        # processInfo = subprocess.Popen('/usr/bin/python3 /home/elettra/tmp/lz/vibration.py '+' '.join(sys.argv[1:]), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-        # print(processInfo.stdout.read())
+        argv[1] = executable_path
+        print('/usr/bin/python3 /home/elettra/tmp/lz/vibration.py '+' '.join(argv[1:]))
+        processInfo = subprocess.Popen('/usr/bin/python3 /home/elettra/tmp/lz/vibration.py '+' '.join(argv[1:]), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        print(processInfo.stdout.read())
         f = urllib.request.urlopen(feedback+'0')
         myfile = f.read()
 else:
         print(f"The process '{process_name}' with parameters {params} is not running.")
         try:
-                sys.argv[1] = executable_path
-                process = subprocess.Popen(sys.argv[1:])
+                argv[1] = executable_path
+                process = subprocess.Popen(argv[1:])
                 pid = process.pid
                 print(f"The PID of the launched process is: {pid}")
                 f = urllib.request.urlopen(feedback+str(pid))
@@ -52,3 +72,4 @@ else:
                 f = urllib.request.urlopen(feedback+'-1&err='+str(e))
                 myfile = f.read()
                 #print(myfile)
+
-- 
GitLab