Cythonised application not updating - python

I'm using an updating script and trying to get it to work with cython. It seems the pyupdater script is ignored (no print and not updating). It works fine with standard python, but calling a pyx file it seems it skips over this so no print and update.
If python main.py (Calls a cython script there is no print and to a larger extent it does not update).
def main():
import collections
import os
import sys
import time
import bb.bb_module
import progressbar
import urllib3.poolmanager
from pyupdater.client import Client, AppUpdate
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
from client_config import ClientConfig
if __name__ == '__main__':
main()
Cython pyx file:
import urllib3.poolmanager
orig_urlopen = urllib3.poolmanager.PoolManager.urlopen
def new_urlopen(self, method, url, redirect=True, **kw):
if "s3.amazonaws.com" in url and 'authorization' in self.headers:
self.headers.pop('authorization')
return orig_urlopen(self, method, url, redirect, **kw)
import sys
urllib3.poolmanager.PoolManager.urlopen = new_urlopen
import logging
import http.client as http_client
import logging
from selenium import webdriver
from client_config import ClientConfig
from pyupdater.client import Client, AppUpdate
import progressbar
import sys
bar = None
import sys
import os
import sys
def check_for_update():
stdout_save = sys.stdout
sys.stdout = open(os.devnull, 'w')
def cb(status):
global bar
from tqdm import tqdm
if bar is None:
bar = progressbar.ProgressBar(widgets = [progressbar.Percentage(), progressbar.Bar(), progressbar.FileTransferSpeed(), ' ', progressbar.ETA()
], fd=sys.stdout).start()
zz = float(status['percent_complete'])
bar.update(zz)
stdout_save = sys.stdout
sys.stdout = open(os.devnull, 'w')
client = Client(ClientConfig(), refresh=True,
headers={'basic_auth': 'brofewfefwefewef:Er8qUc9c48LHAtH5mGz5'})
sys.stdout = stdout_save
client.platform = "win"
app_update = client.update_check(ClientConfig.APP_NAME, ClientConfig.APP_VERSION, channel='stable')
if app_update is not None:
app_update.progress_hooks.append(cb)
if app_update.download():
if isinstance(app_update, AppUpdate):
app_update.extract_restart()
return True
else:
app_update.extract()
return True
return False
def main():
import sys
class DevNull:
def write(self, msg):
pass
print('Current version is ', ClientConfig.APP_VERSION)
if check_for_update():
pass
else:
pass
import os
from contextlib import contextmanager
import sys, os
driver = webdriver.Firefox()
if __name__ == "__main__":
main()
driver = webdriver.Chrome()
sys.stdout = sys.__stdout__
print('This will not print anything')
Output:
DevTools listening on ws://127.0.0.1:12284/devtools/browser/b2f98849-8daa-4442-b594-6e7a904c2091
This will not print anything
It looks as if pyupdater is being ignored when calling cython script. It does not print or update.
I've created a repo to reproduce these issues. How can I get it to update when using cython? I have also included a working pythonic version to see the difference if needed
I suspect:
if __name__ == "__main__":
main()
used in the cython script may be the issue as other than pyupdater the cython script runs perfectly.

Related

Can not get the data from ROS Services, only entering the server but data is not out, why?

I need to read data ( lets say pressure) from serial Port Microcontroller by Client Request. I check the tutorial for ROS Services in Python but still my code is not giving the data value to to client. Here first the Service Server Python node
#!/usr/bin/env python3
from __future__ import print_function
import rospy
import numpy as np
from os import system
import time
import threading
import Microcontroller_Manager_Serial as Serial
import IMU_Functions as IMU
import Motors_Functions as Motor
import Pressure_Functions as Pressure
from geometry_msgs.msg import Vector3
import Modem_Functions as Modem
import threading
import time
import serial
import serial.tools.list_ports
from time import sleep
from std_msgs.msg import Float32
from std_msgs.msg import String
from demo_teleop.srv import ImuValue
Communication_Mode_ = 0
def handle_ros_services():
global P0
data_received = Pressure.Pressure_Get_Final_Values(1,1)
print("Server Read Data:")
P0 = (np.int16((data_received[6]<<24) | (data_received[7]<<16) | (data_received[8]<<8) | (data_received[9])))/10000
P=P0
pressure = P/9.81
current_x_orientation_s=pressure
print("Returning ", current_x_orientation_s)
#return ImuValue(current_x_orientation_s)
def ros_serice_server():
#rospy.init_node('ros_serice_server')
s = rospy.Service('imu_value', ImuValue, handle_ros_services)
print("Ready to get_value")
rospy.spin()
if __name__ == '__main__':
rospy.init_node('server_node_f')
Serial.Serial_Port_Standard()
while not rospy.is_shutdown():
try:
print("entering service")
ros_serice_server()
except:
print("pass")
When I call the server I got this output
entering service
Ready to get_value
And here the client node
#!/usr/bin/env python3
from __future__ import print_function
import rospy
import sys
import numpy as np
from os import system
import time
import threading
import Microcontroller_Manager_Serial as Serial
import IMU_Functions as IMU
import Pressure_Functions as Pressure
import time
import serial
import serial.tools.list_ports
from time import sleep
from std_msgs.msg import Float32
from std_msgs.msg import String
from demo_teleop.srv import ImuValue
Communication_Mode_ = 0
def imu_client():
rospy.wait_for_service('handle_ros_services')
print("Request call send")
imu_value = rospy.ServiceProxy('imu_value', ImuValue)
#resp1 = imu_value
#return imu_value.current_x_orientation_s
if __name__ == "__main__":
rospy.init_node('client_node_f')
while not rospy.is_shutdown():
try:
print("entering client")
imu_client()
except:
print("pass")
When i call the client only got
entering client
So means the server never enter the handle_ros_services()and the client never enter imu_client(): functions. What is wrong with the code?
You have the naming wrong when calling wait_for_service. Your service callback is called handle_ros_services but the service name itself is imu_value. Because of this your client will wait forever, because the former service name never actually gets brought up. Instead inside imu_client() you want the line
rospy.wait_for_service('imu_value')

Cannot start multiprocessing when file compiled using Pyinstaller

This is the imported lib in my code
from tkinter import *
from BacaRupa import main_detect, check_temp
from multiprocessing import Value
from ctypes import Structure, c_double
from PIL import Image, ImageTk
from Logo import *
import tkinter.font as tkFont
import os
import sys
import serial.tools.list_ports
import multiprocessing
I'm using this code to run multiprocessing.
try:
# Python 3.4+
if sys.platform.startswith('win'):
import multiprocessing.popen_spawn_win32 as forking
else:
import multiprocessing.popen_fork as forking
except ImportError:
import multiprocessing.forking as forking
if sys.platform.startswith('win'):
class _Popen(forking.Popen):
def __init__(self, *args, **kw):
if hasattr(sys, 'frozen'):
# We have to set original _MEIPASS2 value from sys._MEIPASS
# to get --onefile mode working.
os.putenv('_MEIPASS2', sys._MEIPASS)
try:
super(_Popen, self).__init__(*args, **kw)
finally:
if hasattr(sys, 'frozen'):
# On some platforms (e.g. AIX) 'os.unsetenv()' is not
# available. In those cases we cannot delete the variable
# but only set it to the empty string. The bootloader
# can handle this case.
if hasattr(os, 'unsetenv'):
os.unsetenv('_MEIPASS2')
else:
os.putenv('_MEIPASS2', '')
# Second override 'Popen' class with our modified version.
forking.Popen = _Popen
class Process(multiprocessing.Process):
_Popen = _Popen
This function get called by if name == 'main':
def PanggilBacaRupa():
import time
y = Value(c_double, 0)
is_stop = Value('i',0)
multiprocessing.freeze_support()
p = Process(target=check_temp, args=(y,is_stop))
p.start()
if entrySuhu.get() == "":
main_detect(y, is_stop, 0)
else:
main_detect(y, is_stop, entrySuhu.get())
When runned using python this program run successfully, but when compiled to .exe this program can't do multiprocessing. What should i do? i compiled with pyinstaller GUI.spec --windowed --onefile.

Run Python Windows service when not logged in

My Flask service stops because I logged out in Windows. When I reload my username and password, it works again. How can I make it always on, even when I reboot or log out?
import time
import random
import os
from pathlib import Path
from SMWinservice import SMWinservice
import sys
import win32serviceutil
import win32service
import win32event
import servicemanager
class FlaskService(SMWinservice):
_svc_name_ = "FlaskServiceSmartFactory01"
_svc_display_name_ = "Flask Service Smart Factory01"
_svc_description_ = "Python service framework Smart Factory01"
def start(self):
self.isrunning = True
def stop(self):
self.isrunning = False
def main(self):
os.chdir(os.environ['SMARTFACTORYPATH'])
os.system('C02_RestAPI.py')
if __name__ == '__main__':
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(FlaskService)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(FlaskService)
Windows services can run in background when Windows is running but you can't run it if Windows is off.
You can set this service to be load at Windows start if you want.

Running through def main function after a variable reaches len(ADCDATA) == 100000

The code only stops when a keyboardinterrupt is used.
A break was used though I took it out and the adc data was read and then the spi.close was skipped along with the update_sheet("PCEM SHT.1", ADCDATA)
I've tried using a different exception, raise exception based on the number of variables in ADCDATA, but with no effect
# import many libraries
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from oauth2client.service_account import ServiceAccountCredentials
import datetime
import spidev # import the SPI driver
def read_mcp3002(channel):
### Working ADC Code
return adc_data
def update_sheet(sheetname, my_list):
###Working Update google sheet code
try:
while True:
Result=1##3.23#for 3302 [mV]..... Voltage=(High Voltage-Low Voltage)/(2^(# of conversion bits))
voltage1=(read_mcp3002(0))
voltage1k=voltage1*Result
voltage2=(read_mcp3002(1))
voltage2k=voltage2*Result # This one #
ADCDATA += [[voltage1k, voltage2k]]
if len(ADCDATA) == 100000:
#print(ADCDATA)
ADCDATA = []
print("+10000")
except KeyboardInterrupt: # Ctrl-C
spi.close()
def main():
update_sheet("PCEM SHT.1", ADCDATA)
if __name__ == '__main__':
main()
The desired result would be to automatically stop at len(ADCDATA)=100000: if len(ADCDATA)==100000, it would run the code in the If statement, and also execute spi.close() and lastly run through def main()'s update_sheet("PCEM SHT1", ADCDATA).
I was able to deal with my problem by importing sys and placing a system exit(1) at the bottom of the & inside the if len(ADCDATA)==100000
if len(ADCDATA)==100000
print("PCEM DATA")
spi.close()
update_sheet"........")
sys.exit(1)

Pyinstaller doesn't generate my file config-file

I'm using Pyinstaller to make an .app bundle on Mac OS X, my app generates a config.ini, the script version worked flawlessly, but when it's an .app, it doesn't work.
I'm using ConfigParser to read and write.
I'm using Pyside for GUI.
It doesn't generate my .ini file, hence it doesn't read or write.
Yes, it's an Anime Notifier, got sick of checking it every time.
Code:
import sys
import urllib2
import ConfigParser
import re
import time
import thread
import atexit
from datetime import date
import lxml.html as lx
from PySide.QtCore import *
from PySide.QtGui import *
from pync import Notifier
def layout_widgets(self, layout):
return (layout.itemAt(i) for i in range(layout.count()))
def notify(self):
while True:
config.read('animeConfig.ini')
for newAnime in animeNotify.xpath('//*[#id="frontpage_left_col"]//*[#class="blue"]/text()'):
if config.has_section(newAnime):
for newEp in animeNotify.xpath('//*[#id="frontpage_left_col"]//*[text()="'+newAnime+'"]/parent::h2/following-sibling::h3/a/text()'):
if not config.has_option(newAnime, newEp):
Notifier.notify(newEp+' has been uploaded!', title=newAnime+' '+newEp.lower(), open='http://www.animeseason.com' +
animeNotify.xpath('//*[#id="frontpage_left_col"]//*[text()="'+newEp+'"]/#href')[0])
m = re.findall('\d+', newEp)
config.set(newAnime, newEp, int(m[0]))
with open('animeConfig.ini', 'wb') as configFile:
config.write(configFile)
time.sleep(300)
def checkChecked(self):
while True:
config.read('animeConfig.ini')
for checkbox in self.layout_widgets(self.vLayout):
if checkbox.widget().isChecked() and not config.has_section(checkbox.widget().text()):
config.add_section(checkbox.widget().text())
for anime in animeList.xpath('//*[#class="series_alpha"]/li/span/preceding-sibling::a/text()'):
if config.has_section(anime):
self.EUrl = animeList.xpath('//*[#class="series_alpha"]/li/*[text()="'+anime+'"]/#href')[0]
self.EUrl = lx.parse(urllib2.urlopen("http://www.animeseason.com" + self.EUrl))
for ep in self.EUrl.xpath('//tr/*[#class="text_center"]/a/text()'):
config.set(anime, 'episode '+ep, ep)
with open('animeConfig.ini', 'wb') as configFile:
config.write(configFile)
elif not checkbox.widget().isChecked() and config.has_section(checkbox.widget().text()):
config.remove_section(checkbox.widget().text())
with open('animeConfig.ini', 'wb') as configFile:
config.write(configFile)
time.sleep(300)
I'm also using the thread module so they can operate simultaneously.
Just like so:
thread.start_new_thread(self.notify, ())
thread.start_new_thread(self.checkChecked, ())
Left out the GUI part, because that isn't very interesting, I guess.

Categories