Jenkins executing python script - python-3.x

I am trying to call python script using Jenkins. I am able to execute the python script over command promt successfully. However when same script is called over Jenkins, I am facing problem of dll. My script is as follow
import isystem.connect as ic
import time
import os
print('isystem.connect version: ' + ic.getModuleVersion())
# 1. connect to winIDEA Application
pathTowinIDEA = 'C:/winIDEA/iConnect.dll'
cmgr_APPL = ic.ConnectionMgr(pathTowinIDEA)
cmgr_APPL.connectMRU('U:/winIDEA/Winidea_cust_config.xjrf.xjrf')
debug_APPL = ic.CDebugFacade(cmgr_APPL)
ec = ic.CExecutionController(cmgr_APPL)
print('APPL Configuration launched')
Error is appearing at line 9:
cmgr_APPL.connectMRU('U:/winIDEA/Winidea_cust_config.xjrf.xjrf')
Error message in Jenkins:
E:6 Can not load iConnect.dll: 'C:/winIDEA/iConnect64.dll'
SystemError: 'The specified module could not be found.
I have verified that iConnect64.dll is present at this path. How to resolve it ?

Related

How to check if the environment variable "PROJ_LIB" is defined and how to unset it ? (PyQGIS Standalone Script Executer)

I just tried the standalone PyQGIS application by running the custom script "Proximity.py"* in a VS Code project without the need of a GUI (such as QGIS).
But, when I run the python-program I get the following message:
proj_create_from_database: C:\Program Files\PostgreSQL\14\share\contrib\postgis-3.2\proj\proj.db contains DATABASE.LAYOUT.VERSION.MINOR = 0 whereas a number >= 2 is expected. It comes from another PROJ installation. (see also: Error Message after launching the configuration (launch.json) from VS Code (when pressing F5))
I'm trying this online example with the following installations:
PostgreSQL 14
Python39
.vscode\extensions\ms-python.python-2022.4.1\pythonFiles\lib\python\debugpy\launcher
osgeo4w-setup.exe (including QGIS LTR)
I read that there is a solution by undefining [PROJ_LIB] before importing pyproj or osgeo: del os.environ ['PROJ_LIB'] as described under this link. If this is also supposed to be the correct solution in this case, can someone help me with step-by-step instructions (for dummies)?
. * The "Proximity.py" script is a pyqgis standalone example from "https://github.com/MarByteBeep/pyqgis-standalone"
Finally, I got a solution to be able to run the "standalone PyQGIS"* example "Proximity" (provided by MarByteBeep).
This solution was possible without needing to launch the configuration file "launch.json" as above described. And so, avoiding the need to make any configuration to the environment variable "PROJ_LIB" by trying to circumvent the above issue.
I just first added the following two code-lines (see here line 2 and 3) in the python file "main.py" so as to be able to use the plugin "PROCESSING" (initially line 8 of the "main.py" file), then I store it and finally I ran it.
Line 1: from qgis.core import
Line 2: import sys
Line 3: sys.path.append('C:\Program Files\QGIS 3.24.1\apps\qgis\python\plugins')
Line 4: qgs = QgsApplication([], False)
Line 5: ...
The Proximity example is based on the answer of "Mar Tjin" to the following Question: "Looking for manual on how to properly setup standalone PyQGIS without GUI"
. * By "Standalone PyQGIS" I refer to code/scripts that can be run outside the QGIS-GUI (=> QGIS-Desktop/Server Application). In my case under the external Editor VS Code

Python "No module named mysql.connector" when running script on Raspberry Pi

I wrote a Python script to test the MariaDB package using Python-3.7.3 running on a Raspberry Pi 3. The script is shown below:
#============================================================================
# Test Snippet #1
import mysql.connector
from mysql.connector
import connect, errorcode, Error
############################################################################
# Begin execution here:
print (">> Program Start >>")
#---------------------------------------------------------------------------
# Connect to the test database:
try:
dbConn=\
connect (user='WebServer', password='$$$$$$$$', host='localhost', \
database='HamRadio')
except Error as err:
print ("! Error #", err.errno, \
" encountered when connecting to test database !")
#---------------------------------------------------------------------------
# Search the test table for certain entries:
curSel=dbConn.cursor ()
strStatement="SELECT * FROM HamRadio.Log WHERE Call_Sign like 'OK%' " + \
"ORDER BY Call_Sign"
try:
curSel.execute (strStatement)
except Error as err:
print ("! Error #", err.errno, " encountered when executing SELECT command !")
#---------------------------------------------------------------------------
# Display the rows selected:
for row in curSel:
print ("Row Contents=", row)
#---------------------------------------------------------------------------
# Close the database table:
curSel.close ()
If I execute this script using the Python3 IDE (Python shell), the script works correctly.
However, if I attempt to execute the same script from the command line:
pi#raspberrypi:~ $ sudo python TestSnippet_1.py
The following error message is displayed:
'''
Traceback (most recent call last):
File "TestSnippet_1.py", line 4, in <module>
import mysql.connector
ImportError: No module named mysql.connector*
'''
I suspect that the issue may be related to some sort of environment path but I lack the experience using Python in a Raspberry Pi environment to fully understand what is happening.
Any help would greatly be appreciated because I am at a standstill at this point.
After many hours of investigation, I determined that the cause of the error was the use of 'sudo' in the command line (which invokes a different set of path variables)!! Typing just: python TestSnippet_1.py instead of sudo python TestSnippet_1.py enables the program to execute with no errors!! Very subtle but very deadly!!

Starting Node server from Scala app with environment variables

I am trying to start a Node server from a Scala app using a shell script but when I try to add environment variables I am getting the following error:
Exception in thread "main" java.io.IOException: Cannot run program "TEST_ENV_VAR=fake-key": error=2, No such file or directory
The code looks like this:
import java.nio.file.{Files, Path}
import scala.language.postfixOps
#main def app() =
val path = Path.of(scala.sys.props.get("user.home").get + "/projects")
s"TEST_ENV_VAR=fake-key node $path/node-server/server.js" !!
It looks like it is trying to run the environment variables instead of the server. Is there a way to get it to recognize them correctly? I have been able to run a node server successfully using this method when I don't include the environment variable.
Thank you!
Here is an example that uses the scala.sys.process.Process object:
import java.io.File
import scala.sys.process.*
#main def app() =
val file = File(scala.sys.props.get("user.home").get + "/projects")
Process("node node-server/server.js", Some(file), "TEST_ENV_VAR" -> "fake-key").!!

cronjob not able to access config.ini when running python3 script

I have a crontab entry which is running a python3 script. This python script uses a config.ini file to get some tokens for use in the script.
The crontab entry is:
*/15 * * * * /usr/bin/python3 /opt/scripts/tf_state_backup/tf_state_backup.py >> ~/cron.out 2>&1
The config.ini file has the following:
[terraform]
token = <base64 encoded API key>
[gitlab]
token = <base64 encoded API key>
The relevant part of the python script is as follows:
import configparser
## read config file and decode api keys
config = configparser.ConfigParser()
config.read(os.path.abspath('config.ini'))
tfc_token = base64.b64decode(config['terraform']['token']).decode('utf-8')
gitlab_token = base64.b64decode(config['gitlab']['token']).decode('utf-8')
When this runs I can check the cron.out file for any errors. I get the following error every time it runs.
SyntaxError: invalid syntax
Traceback (most recent call last):
File "/opt/scripts/tf_state_backup/tf_state_backup.py", line 17, in <module>
tfc_token = base64.b64decode(config['terraform']['token']).decode('utf-8')
File "/usr/lib64/python3.6/configparser.py", line 959, in __getitem__
raise KeyError(key)
KeyError: 'terraform'
I have checked the following:
Ensured script and config has correct permissions & +x permission
Ran script exactly as it is in the cron tab, it runs fine without any problems
Ensured the config.ini was being referenced by it's absolute path, not relative path
Any help on this would be excellent.
Your should use the get method of ConfigParser object. The first parameter is the section name the second is the variable name. If the raw parameter is set to True then special characters will be read as string (Eg.: %).
I have written a working version.
test.ini:
[terraform]
token = aGVsbG93b3JsZA==
[gitlab]
token = bm90X2hlbGxvd29ybGQ=
test.py:
import configparser
import base64
config = configparser.ConfigParser()
config.read("test.ini")
tfc_token = base64.b64decode(config.get('terraform', 'token', raw=True)).decode('utf-8')
gitlab_token = base64.b64decode(config.get('gitlab', 'token', raw=True)).decode('utf-8')
print(tfc_token)
print(gitlab_token)
Output:
>>> python3 test.py
helloworld
not_helloworld
FYI:
I have used Python3.6.6 and Linux OS for testing.
I have generated the base64s on this site: https://www.base64encode.org/
I managed to get this running by updating the cron job to the following:
*/15 * * * * cd /opt/scripts/tf_state_backup/ && /usr/bin/python3 /opt/scripts/tf_state_backup/tf_state_backup.py
I am perhaps not getting the path correctly for the config file?
Regardless it is now working.

fatal python error :py_Initialize : unable to get the locale encodings import error: no module named encodings

the important part of the error message:
I am getting the following error
starting uWSGI 2.0.18
setting pythonHome to /var/www/demo/venv
python version :3.5.3
Fatal Python error :unable to get the locale encoding
import error : no module named 'encodings'
It shows python version :3.5.3
however inside my venv/lib folder , there is only one package python 2.7
does this have something to do with my error?
please help me out with this.
this is my demo_uwsgi.ini file
#application's base folder
base = /var/www/demo
#python module to import
app = flaskfile //flaskfile is my flask file
module = %(app)
home = %(base)/venv
pythonpath = %(base)
#socket file's location
socket = /var/www/demo/%n.sock
#permissions for the socket file
chmod-socket = 666
#the variable that holds a flask application inside the module imported at line #6
callable = app
#location of log files
logto = /var/log/uwsgi/%n.log```
Am I missing plugins or something? I added plugins = python32 in my demo_uwsgi.ini file and it shows no such file or directory. Do I need to change or unset python path or something?
figured it out myself. Delete the default Nginx configuration file and add your new configuration file at the /etc/nginx. Then follow the instructions in this link https://vladikk.com/20.13/09/12/serving-flask-with-nginx-on-ubuntu/ step by step. change the ownership from root to user. It works perfectly

Resources