detect app path when running in pyinstaller

pyinstaller creates a temporary app environment
This commit is contained in:
dj2ls 2022-01-12 07:27:42 +01:00
parent 5804d62036
commit 28c9c673fa
5 changed files with 72 additions and 33 deletions

View file

@ -4,7 +4,8 @@
import ctypes
from ctypes import *
import sys
import pathlib
import os
#import pathlib
from enum import Enum
import numpy as np
#print("loading codec2 module", file=sys.stderr)
@ -19,6 +20,14 @@ class FREEDV_MODE(Enum):
def freedv_get_mode(mode):
return FREEDV_MODE[mode].value
# check if we are running in a pyinstaller environment
try:
app_path = sys._MEIPASS
except:
app_path = os.path.abspath(".")
sys.path.append(app_path)
# -------------------------------------------- LOAD FREEDV
# codec2 search pathes in descending order
# libcodec2.so ctests
@ -27,22 +36,16 @@ def freedv_get_mode(mode):
# pathlib.Path("../../tnc/codec2/build_linux/src/libcodec2.so.1.0") external loading manual build
# pathlib.Path("../../tnc/lib/codec2/linux/libcodec2.so.1.0") external loading precompiled
# append local search path
# Possibly this resolves problems when using ms-windows
sys.path.append('.')
if sys.platform == 'linux':
libname = ["libcodec2.so", \
pathlib.Path("codec2/build_linux/src/libcodec2.so.1.0"), \
pathlib.Path("lib/codec2/linux/libcodec2.so.1.0"), \
pathlib.Path("../tnc/codec2/build_linux/src/libcodec2.so.1.0"), \
pathlib.Path("../tnc/lib/codec2/linux/libcodec2.so.1.0"), \
os.path.join(app_path, "codec2/build_linux/src/libcodec2.so.1.0"), \
os.path.join(app_path, "lib/codec2/linux/libcodec2.so.1.0"), \
os.path.join(app_path, "../tnc/codec2/build_linux/src/libcodec2.so.1.0"), \
os.path.join(app_path, "../tnc/lib/codec2/linux/libcodec2.so.1.0"), \
]
elif sys.platform == 'win32' or sys.platform == 'win64':
libname = ["libcodec2.dll", \
pathlib.Path("lib\\codec2\\windows\\libcodec2.dll"), \
os.path.join(base_path, "lib\\codec2\\windows\\libcodec2.dll")
]
else:
print(f"[C2 ] Platform not supported {sys.platform}", file=sys.stderr)

View file

@ -22,7 +22,7 @@ import re
import structlog
import log_handler
import helpers
import os
log_handler.setup_logging("daemon")
structlog.get_logger("structlog").info("[DMN] Starting FreeDATA daemon", author="DJ2LS", year="2022", version="0.1")
@ -60,6 +60,13 @@ def noalsaerr():
# p = pyaudio.PyAudio()
######################################################
# check if we are running in a pyinstaller environment
try:
app_path = sys._MEIPASS
except:
app_path = os.path.abspath(".")
sys.path.append(app_path)
# try importing hamlib
try:
# installation path for Ubuntu 20.04 LTS python modules
@ -254,13 +261,21 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
# this helps running the tnc in a developer environment
try:
command = []
command.append('./tnc')
if sys.platform == 'linux' or sys.platform == 'darwin':
command.append('./tnc')
elif sys.platform == 'win32' or sys.platform == 'win64':
command.append('tnc.exe')
command += options
p = subprocess.Popen(command)
structlog.get_logger("structlog").info("[DMN] TNC started", path="binary")
except:
command = []
command.append('python3')
if sys.platform == 'linux' or sys.platform == 'darwin':
command.append('python3')
elif sys.platform == 'win32' or sys.platform == 'win64':
command.append('python')
command.append('main.py')
command += options
p = subprocess.Popen(command)

View file

@ -227,12 +227,13 @@ class RF():
if not self.datac3_buffer.nbuffer+len(x) > self.datac3_buffer.size:
self.datac3_buffer.push(x)
self.fft_data = bytes(x)
if self.modoutqueue.empty():
data_out48k = bytes(self.AUDIO_FRAMES_PER_BUFFER_TX*2)
self.fft_data = bytes(x)
else:
data_out48k = self.modoutqueue.get()
self.fft_data = bytes(data_out48k)
return (data_out48k, pyaudio.paContinue)
@ -241,6 +242,8 @@ class RF():
def transmit(self, mode, repeats, repeat_delay, frames):
static.TRANSMITTING = True
# toggle ptt early to save some time
static.PTT_STATE = self.hamlib.set_ptt(True)
# open codec2 instance
#self.MODE = codec2.freedv_get_mode_value_by_name(mode)
self.MODE = mode
@ -326,7 +329,7 @@ class RF():
self.modoutqueue.put(c)
# maybe we need to toggle PTT before craeting modulation because of queue processing
static.PTT_STATE = self.hamlib.set_ptt(True)
#static.PTT_STATE = self.hamlib.set_ptt(True)
while not self.modoutqueue.empty():
pass
static.PTT_STATE = self.hamlib.set_ptt(False)
@ -368,19 +371,19 @@ class RF():
self.calculate_snr(self.datac1_freedv)
def audio_datac3(self):
nbytes_datac3 = 0
while self.audio_stream.is_active():
threading.Event().wait(0.01)
while self.datac3_buffer.nbuffer >= self.datac3_nin:
nbytes_datac3 = 0
while self.audio_stream.is_active():
threading.Event().wait(0.01)
while self.datac3_buffer.nbuffer >= self.datac3_nin:
# demodulate audio
nbytes_datac3 = codec2.api.freedv_rawdatarx(self.datac3_freedv, self.datac3_bytes_out, self.datac3_buffer.buffer.ctypes)
self.datac3_buffer.pop(self.datac3_nin)
self.datac3_nin = codec2.api.freedv_nin(self.datac3_freedv)
if nbytes_datac3 == self.datac3_bytes_per_frame:
self.modem_received_queue.put([self.datac3_bytes_out, self.datac3_freedv ,self.datac3_bytes_per_frame])
self.get_scatter(self.datac3_freedv)
self.calculate_snr(self.datac3_freedv)
# demodulate audio
nbytes_datac3 = codec2.api.freedv_rawdatarx(self.datac3_freedv, self.datac3_bytes_out, self.datac3_buffer.buffer.ctypes)
self.datac3_buffer.pop(self.datac3_nin)
self.datac3_nin = codec2.api.freedv_nin(self.datac3_freedv)
if nbytes_datac3 == self.datac3_bytes_per_frame:
self.modem_received_queue.put([self.datac3_bytes_out, self.datac3_freedv ,self.datac3_bytes_per_frame])
self.get_scatter(self.datac3_freedv)
self.calculate_snr(self.datac3_freedv)
@ -472,6 +475,7 @@ class RF():
if len(self.fft_data) >= 128:
data_in = self.fft_data
# delte fft_buffer
self.fft_data = bytes()
# https://gist.github.com/ZWMiller/53232427efc5088007cab6feee7c6e4c

View file

@ -7,8 +7,13 @@ import atexit
import subprocess
# append local search path
# Possibly this resolves problems when using ms-windows
sys.path.append('.')
# check if we are running in a pyinstaller environment
try:
app_path = sys._MEIPASS
except:
app_path = os.path.abspath(".")
sys.path.append(app_path)
# try importing hamlib
try:
@ -25,6 +30,7 @@ try:
sys.path.append('/usr/local/lib64/python'+ python_version +'/site-packages')
# everything else... not nice, but an attempt to see how its running within app bundle
# this is not needed as python will be shipped with app bundle
sys.path.append('/usr/local/lib/python3.6/site-packages')
sys.path.append('/usr/local/lib/python3.7/site-packages')
sys.path.append('/usr/local/lib/python3.8/site-packages')

View file

@ -10,7 +10,7 @@ import subprocess
import structlog
import time
import sys
import os
# for rig_model -> rig_number only
@ -38,6 +38,14 @@ class radio:
self.data_bits = data_bits
self.stop_bits = stop_bits
self.handshake = handshake
# check if we are running in a pyinstaller environment
try:
app_path = sys._MEIPASS
except:
app_path = os.path.abspath(".")
sys.path.append(app_path)
# get devicenumber by looking for deviceobject in Hamlib module
try:
@ -52,10 +60,13 @@ class radio:
print(self.devicenumber, self.deviceport, self.serialspeed)
# select precompiled executable for win32/win64 rigctl
# this is really a hack...somewhen we need a native hamlib integration for windows
if sys.platform == 'win32' or sys.platform == 'win64':
self.cmd = 'lib\\hamlib\\'+sys.platform+'\\rigctl -m %d -r %s -s %d ' % (int(self.devicenumber), self.deviceport, int(self.serialspeed))
self.cmd = app_path + 'lib\\hamlib\\'+sys.platform+'\\rigctl -m %d -r %s -s %d ' % (int(self.devicenumber), self.deviceport, int(self.serialspeed))
else:
self.cmd = 'rigctl -m %d -r %s -s %d ' % (int(self.devicenumber), self.deviceport, int(self.serialspeed))