mirror of
https://github.com/DJ2LS/FreeDATA
synced 2024-05-14 08:04:33 +00:00
detect app path when running in pyinstaller
pyinstaller creates a temporary app environment
This commit is contained in:
parent
5804d62036
commit
28c9c673fa
5 changed files with 72 additions and 33 deletions
|
@ -4,7 +4,8 @@
|
||||||
import ctypes
|
import ctypes
|
||||||
from ctypes import *
|
from ctypes import *
|
||||||
import sys
|
import sys
|
||||||
import pathlib
|
import os
|
||||||
|
#import pathlib
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
import numpy as np
|
import numpy as np
|
||||||
#print("loading codec2 module", file=sys.stderr)
|
#print("loading codec2 module", file=sys.stderr)
|
||||||
|
@ -19,6 +20,14 @@ class FREEDV_MODE(Enum):
|
||||||
def freedv_get_mode(mode):
|
def freedv_get_mode(mode):
|
||||||
return FREEDV_MODE[mode].value
|
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
|
# -------------------------------------------- LOAD FREEDV
|
||||||
# codec2 search pathes in descending order
|
# codec2 search pathes in descending order
|
||||||
# libcodec2.so ctests
|
# 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/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
|
# 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':
|
if sys.platform == 'linux':
|
||||||
libname = ["libcodec2.so", \
|
libname = ["libcodec2.so", \
|
||||||
pathlib.Path("codec2/build_linux/src/libcodec2.so.1.0"), \
|
os.path.join(app_path, "codec2/build_linux/src/libcodec2.so.1.0"), \
|
||||||
pathlib.Path("lib/codec2/linux/libcodec2.so.1.0"), \
|
os.path.join(app_path, "lib/codec2/linux/libcodec2.so.1.0"), \
|
||||||
pathlib.Path("../tnc/codec2/build_linux/src/libcodec2.so.1.0"), \
|
os.path.join(app_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, "../tnc/lib/codec2/linux/libcodec2.so.1.0"), \
|
||||||
]
|
]
|
||||||
elif sys.platform == 'win32' or sys.platform == 'win64':
|
elif sys.platform == 'win32' or sys.platform == 'win64':
|
||||||
libname = ["libcodec2.dll", \
|
libname = ["libcodec2.dll", \
|
||||||
pathlib.Path("lib\\codec2\\windows\\libcodec2.dll"), \
|
os.path.join(base_path, "lib\\codec2\\windows\\libcodec2.dll")
|
||||||
|
|
||||||
]
|
]
|
||||||
else:
|
else:
|
||||||
print(f"[C2 ] Platform not supported {sys.platform}", file=sys.stderr)
|
print(f"[C2 ] Platform not supported {sys.platform}", file=sys.stderr)
|
||||||
|
|
|
@ -22,7 +22,7 @@ import re
|
||||||
import structlog
|
import structlog
|
||||||
import log_handler
|
import log_handler
|
||||||
import helpers
|
import helpers
|
||||||
|
import os
|
||||||
|
|
||||||
log_handler.setup_logging("daemon")
|
log_handler.setup_logging("daemon")
|
||||||
structlog.get_logger("structlog").info("[DMN] Starting FreeDATA daemon", author="DJ2LS", year="2022", version="0.1")
|
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()
|
# 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 importing hamlib
|
||||||
try:
|
try:
|
||||||
# installation path for Ubuntu 20.04 LTS python modules
|
# 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
|
# this helps running the tnc in a developer environment
|
||||||
try:
|
try:
|
||||||
command = []
|
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
|
command += options
|
||||||
p = subprocess.Popen(command)
|
p = subprocess.Popen(command)
|
||||||
structlog.get_logger("structlog").info("[DMN] TNC started", path="binary")
|
structlog.get_logger("structlog").info("[DMN] TNC started", path="binary")
|
||||||
except:
|
except:
|
||||||
command = []
|
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.append('main.py')
|
||||||
command += options
|
command += options
|
||||||
p = subprocess.Popen(command)
|
p = subprocess.Popen(command)
|
||||||
|
|
32
tnc/modem.py
32
tnc/modem.py
|
@ -227,12 +227,13 @@ class RF():
|
||||||
if not self.datac3_buffer.nbuffer+len(x) > self.datac3_buffer.size:
|
if not self.datac3_buffer.nbuffer+len(x) > self.datac3_buffer.size:
|
||||||
self.datac3_buffer.push(x)
|
self.datac3_buffer.push(x)
|
||||||
|
|
||||||
self.fft_data = bytes(x)
|
|
||||||
|
|
||||||
if self.modoutqueue.empty():
|
if self.modoutqueue.empty():
|
||||||
data_out48k = bytes(self.AUDIO_FRAMES_PER_BUFFER_TX*2)
|
data_out48k = bytes(self.AUDIO_FRAMES_PER_BUFFER_TX*2)
|
||||||
|
self.fft_data = bytes(x)
|
||||||
else:
|
else:
|
||||||
data_out48k = self.modoutqueue.get()
|
data_out48k = self.modoutqueue.get()
|
||||||
|
self.fft_data = bytes(data_out48k)
|
||||||
|
|
||||||
return (data_out48k, pyaudio.paContinue)
|
return (data_out48k, pyaudio.paContinue)
|
||||||
|
|
||||||
|
@ -241,6 +242,8 @@ class RF():
|
||||||
|
|
||||||
def transmit(self, mode, repeats, repeat_delay, frames):
|
def transmit(self, mode, repeats, repeat_delay, frames):
|
||||||
static.TRANSMITTING = True
|
static.TRANSMITTING = True
|
||||||
|
# toggle ptt early to save some time
|
||||||
|
static.PTT_STATE = self.hamlib.set_ptt(True)
|
||||||
# open codec2 instance
|
# open codec2 instance
|
||||||
#self.MODE = codec2.freedv_get_mode_value_by_name(mode)
|
#self.MODE = codec2.freedv_get_mode_value_by_name(mode)
|
||||||
self.MODE = mode
|
self.MODE = mode
|
||||||
|
@ -326,7 +329,7 @@ class RF():
|
||||||
self.modoutqueue.put(c)
|
self.modoutqueue.put(c)
|
||||||
|
|
||||||
# maybe we need to toggle PTT before craeting modulation because of queue processing
|
# 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():
|
while not self.modoutqueue.empty():
|
||||||
pass
|
pass
|
||||||
static.PTT_STATE = self.hamlib.set_ptt(False)
|
static.PTT_STATE = self.hamlib.set_ptt(False)
|
||||||
|
@ -368,19 +371,19 @@ class RF():
|
||||||
self.calculate_snr(self.datac1_freedv)
|
self.calculate_snr(self.datac1_freedv)
|
||||||
|
|
||||||
def audio_datac3(self):
|
def audio_datac3(self):
|
||||||
nbytes_datac3 = 0
|
nbytes_datac3 = 0
|
||||||
while self.audio_stream.is_active():
|
while self.audio_stream.is_active():
|
||||||
threading.Event().wait(0.01)
|
threading.Event().wait(0.01)
|
||||||
while self.datac3_buffer.nbuffer >= self.datac3_nin:
|
while self.datac3_buffer.nbuffer >= self.datac3_nin:
|
||||||
|
|
||||||
# demodulate audio
|
# demodulate audio
|
||||||
nbytes_datac3 = codec2.api.freedv_rawdatarx(self.datac3_freedv, self.datac3_bytes_out, self.datac3_buffer.buffer.ctypes)
|
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_buffer.pop(self.datac3_nin)
|
||||||
self.datac3_nin = codec2.api.freedv_nin(self.datac3_freedv)
|
self.datac3_nin = codec2.api.freedv_nin(self.datac3_freedv)
|
||||||
if nbytes_datac3 == self.datac3_bytes_per_frame:
|
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.modem_received_queue.put([self.datac3_bytes_out, self.datac3_freedv ,self.datac3_bytes_per_frame])
|
||||||
self.get_scatter(self.datac3_freedv)
|
self.get_scatter(self.datac3_freedv)
|
||||||
self.calculate_snr(self.datac3_freedv)
|
self.calculate_snr(self.datac3_freedv)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -472,6 +475,7 @@ class RF():
|
||||||
if len(self.fft_data) >= 128:
|
if len(self.fft_data) >= 128:
|
||||||
|
|
||||||
data_in = self.fft_data
|
data_in = self.fft_data
|
||||||
|
# delte fft_buffer
|
||||||
self.fft_data = bytes()
|
self.fft_data = bytes()
|
||||||
|
|
||||||
# https://gist.github.com/ZWMiller/53232427efc5088007cab6feee7c6e4c
|
# https://gist.github.com/ZWMiller/53232427efc5088007cab6feee7c6e4c
|
||||||
|
|
10
tnc/rig.py
10
tnc/rig.py
|
@ -7,8 +7,13 @@ import atexit
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
# append local search path
|
# append local search path
|
||||||
# Possibly this resolves problems when using ms-windows
|
# check if we are running in a pyinstaller environment
|
||||||
sys.path.append('.')
|
try:
|
||||||
|
app_path = sys._MEIPASS
|
||||||
|
except:
|
||||||
|
app_path = os.path.abspath(".")
|
||||||
|
sys.path.append(app_path)
|
||||||
|
|
||||||
|
|
||||||
# try importing hamlib
|
# try importing hamlib
|
||||||
try:
|
try:
|
||||||
|
@ -25,6 +30,7 @@ try:
|
||||||
sys.path.append('/usr/local/lib64/python'+ python_version +'/site-packages')
|
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
|
# 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.6/site-packages')
|
||||||
sys.path.append('/usr/local/lib/python3.7/site-packages')
|
sys.path.append('/usr/local/lib/python3.7/site-packages')
|
||||||
sys.path.append('/usr/local/lib/python3.8/site-packages')
|
sys.path.append('/usr/local/lib/python3.8/site-packages')
|
||||||
|
|
|
@ -10,7 +10,7 @@ import subprocess
|
||||||
import structlog
|
import structlog
|
||||||
import time
|
import time
|
||||||
import sys
|
import sys
|
||||||
|
import os
|
||||||
# for rig_model -> rig_number only
|
# for rig_model -> rig_number only
|
||||||
|
|
||||||
|
|
||||||
|
@ -39,6 +39,14 @@ class radio:
|
||||||
self.stop_bits = stop_bits
|
self.stop_bits = stop_bits
|
||||||
self.handshake = handshake
|
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
|
# get devicenumber by looking for deviceobject in Hamlib module
|
||||||
try:
|
try:
|
||||||
import Hamlib
|
import Hamlib
|
||||||
|
@ -52,10 +60,13 @@ class radio:
|
||||||
|
|
||||||
print(self.devicenumber, self.deviceport, self.serialspeed)
|
print(self.devicenumber, self.deviceport, self.serialspeed)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# select precompiled executable for win32/win64 rigctl
|
# select precompiled executable for win32/win64 rigctl
|
||||||
# this is really a hack...somewhen we need a native hamlib integration for windows
|
# this is really a hack...somewhen we need a native hamlib integration for windows
|
||||||
if sys.platform == 'win32' or sys.platform == 'win64':
|
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:
|
else:
|
||||||
self.cmd = 'rigctl -m %d -r %s -s %d ' % (int(self.devicenumber), self.deviceport, int(self.serialspeed))
|
self.cmd = 'rigctl -m %d -r %s -s %d ' % (int(self.devicenumber), self.deviceport, int(self.serialspeed))
|
||||||
|
|
Loading…
Reference in a new issue