how does jenkins recognize paths inside a python script? - python

I have a python script that points to some file names and log files and I have jenkins to run the script, when run locally from my system the code works fine.
The way I access my folders in python:
folder_artifacts_data = 'C:/Users/Rhea/OneDrive -Area/Rhea/Metrics_Configuration/Artifacts/'
path_to_log_file ='C:/Users/Rhea/OneDrive -Area/Rhea/Metrics_Configuration/Logfiles/Logfile.log'
but when I try to run the same using jenkins, I get the following error:
No such file or directory:
/opt/jenkins/workspace/confluencetest_scheduled/C:/Users/Rhea/OneDrive -Area/Rhea/Metrics_Configuration/Logfiles/Logfile.log
Now, I tried different file paths and used r-strings
folder_artifacts_data = r'C:/Users/Rhea/OneDrive -Area/Rhea/Metrics_Configuration/Artifacts/'
path_to_log_file =r'C:/Users/Rhea/OneDrive -Area/Rhea/Metrics_Configuration/Logfiles/Logfile.log'
I see that jenkins has accepted the log file, because I see the logs written, but the moment it reaches folder_artifacts_data it throws the error that the file path do not exist.
Could someone help?
Update
Now I have added relative paths, like:
folder_artifacts_data0 = 'C:/Users/Rhea/OneDrive -Area/Rhea/Metrics_Configuration/Artifacts/'
folder_artifacts_data = os.path.relpath(folder_artifacts_data0)
path_to_log_file0 ='C:/Users/Rhea/OneDrive -Area/Rhea/Metrics_Configuration/Logfiles/Logfile.log'
path_to_log_file = os.path.relpath(path_to_log_file0)
that outputs paths like:
..............\Rhea\OneDrive -Area\Rhea\Metrics_Configuration\Artifacts
and
..............\Rhea\OneDrive -Area\Rhea\Metrics_Configuration\Logfiles\Logfile.log
this works well in my local, again I get
No such file or directory:
/opt/jenkins/workspace/confluencetest_scheduled/C:/Users/Rhea/OneDrive -Area/Rhea/Metrics_Configuration/Logfiles/Logfile.log while running from jenkins.

Related

Bash Bad Substitution Error: Trying to Execute Python Script

I'm following the tutorial here to convert MacJournal journal entries to DayOne journal entries: https://basilsalad.com/how-to/migrate-mac-journal-day-one/
And I am having issues on the very last part.
I moved my MacJournal exported text file to the same directory as MacJournalToDayOne.py so in terminal, when I run the command "ls" it lists the python file and the MacJournal file which is "dreams.txt" in the same directory.
Everything works fine up until this part here:
./MacJournalToDayOne.py "${export-file}" --journal="${journal-name}"
When I add in the file names and the journal I want to import into, I run it as:
./MacJournalToDayOne.py "${dreams.txt}" --journal="${MJImport}"
Which gives the error: -bash: ${dreams.txt}: bad substitution
What am I doing wrong here?

FileNotFound Error in python when running .py file from visual basic

I was trying to make a assistant which can perform simple task like shutting down the computer etc. For this i chose python and visual basic..... visual basic for displaying(frontend applicaton) and python for performing tasks(backend application). So i create a py file named main.py and created a folder named query and in it created a file named query.jarvis which can simply be opened as a text file. The vb(visual basic) program just write text into query.jarvis and then run the main.py file. When I run it manually by double clicking the main.py file it works fine(like in query was "shutdown" and after running main.py file by double clicking my computer shutdown) but when I try to run it from vb it shows the error file not found query\query.jarvis . I even tried to convert py file to exe by pyinstaller but it again showed the same error but only when I run it from vb.
*main.py()
def check(q):
#here was performing task according to query
f=open("query\query.jarvis")
#here the error occured
x=f.readlines()
d=x[0]
d=d.strip()
q=d.lower()
check(q)*
*vb.net
objWriter123.Close()
Dim objWriter As New System.IO.StreamWriter(moddir + "query\query.jarvis")
#here moddir is the directory of main.py file
objWriter.Write(UserQuery.Text)
objWriter.Close()
UserQuery.Text = ""
Process.Start(moddir + "main.py", AppWinStyle.MinimizedNoFocus)*
process.start sometimes has weird outcomes. i usually manage to fix it by adding explorer.exe into the mix.
Process.Start("explorer.exe", moddir & "\main.py")
also note this extra backslash, that you might have missed. in \main.py
note
in vb you concat strings with the & symbol instead of the + symbol.

Unable to generate a share link via a script and Synology API

I'm hitting a wall with the python script at https://stackoverflow.com/a/60946539/1641112. I've added the script to a file called test.py3.
I'm running this command directly on my Synology drive with an admin account: python test.py3 /volume2/dir/key-basic-preferred.pdf --debug
The relevant part of the debug output:
Get(): "request = entry.cgi?api=SYNO.FileStation.Sharing&version=3&method=create&path="/volume2/dir/key-basic-preferred.pdf""
GET: "http://127.0.0.1:5000/webapi/entry.cgi?api=SYNO.FileStation.Sharing&version=3&method=create&path="/volume2/dir/key-basic-preferred.pdf"&_sid=011o6xaJaxvLsKCJ4N91278"
GET: "<Response [200]>"
ERROR: Get (entry.cgi?api=SYNO.FileStation.Sharing&version=3&method=create&path="/volume2/dir/key-basic-preferred.pdf"&_sid=011o6xaJaxvLsKCJ4N91278):
Error: 408: Unknown error
The 408 error is an unknown file according to the API docs.
However, I know that file exists. I've tried URL encoding the path. I've tried with and without quotes. I've tried noodling with the python code (don't know python) to change the quotations and api version to 1 instead of 3 but I'm not having any luck.
Finally figured it out. The file paths are relative to the File Station. So if you have a folder in file station called "backup" and a file in "backup" call "blah.txt", the path to the file is /backup/blah.txt

ValueError: need more than 0 values to unpack (Python 2)

I am trying to replicate another researcher's findings by using the Python file that he added as a supplement to his paper. It is the first time I am diving into Python, so the error might be extremely simple to fix, yet after two days I haven't still. For context, in the Readme file there's the following instruction:
"To run the script, make sure Python2 is installed. Put all files into one folder designated as “cf_dir”.
In the script I get an error at the following lines:
if __name__ == '__main__':
cf_dir, cf_file, cf_phys_file = sys.argv[1:4]
os.chdir(cf_dir)
cf = pd.read_csv(cf_file)
cf_phys = pd.read_csv(cf_phys_file)
ValueError: need more than 0 values to unpack
The "cf_file" and "cf_phys_file" are two major components of all files that are in the one folder named "cf_dir". The "cf_phys_file" relates only to two survey question's (Q22 and Q23), and the "cf_file" includes all other questions 1-21. Now it seems that the code is meant to retrieve those two files from the directory? Only for the "cf_phys_file" the columns 1:4 are needed. The current working directory is already set at the right location.
The path where I located "cf_dir" is as follows:
C:\Users\Marc-Marijn Ossel\Documents\RSM\Thesis\Data\Suitable for ML\Data en Artikelen\Per task Suitability for Machine Learning score readme\cf_dir
Alternative option in readme file,
In the readme file there's this option, but also here I cannot understand how to direct the path to the right location:
"Run the following command in an open terminal (substituting for file names
below): python cfProcessor_AEAPnP.py cf_dir cf_file cf_phys_file task_file jobTaskRatingFile
jobDataFile OESfile
This should generate the data and plots as necessary."
When I run that in "Command Prompt", I get the following error, and I am not sure how to set the working directory correctly.
- python: can't open file 'cfProcessor_AEAPnP.py': [Errno 2] No such file or directory
Thanks for the reading, and I hope there's someone who could help me!
Best regards & stay safe out there during Corona!!
Marc
cf_dir, cf_file, cf_phys_file = sys.argv[1:4]
means, the python file expects few arguments when called.
In order to run
python cfProcessor_AEAPnP.py cf_dir cf_file cf_phys_file task_file jobTaskRatingFile jobDataFile OESfile
the command prompt should be in that folder.
So, open command prompt and type
cd path_to_the_folder_where_ur_python_file_is_located
Now, you would have reached the path of the python file.
Also, make sure you give full path in double quotes for the arguments.

Using Osmconvert with Python

I want to use osmconvert to parse down the size of my diff files for just the area I'm interested in because osmconvert is way faster than osm2pgsql, which loads the data.
When I call the command using os.system() like such:
cmd = r"""c:\temp\osmconvert.exe 770.osc.gz -b=1,1,3,3 -o=extract.o5m"""
os.system(cmd)
I get osmconvert error: cannot open file
When I run the same exact command from my command prompt in Windows 7, it runs fine. What is python doing to prevent this function from running? The 770.osc.gz file lives in the same directory as osmconvert.exe and the output extract.05m should populate in the same directory as the osmconvert.exe exists.
If I put the command in a batch file, it works, but I want to use python to download the file from the server so I can automate the updates of the database.
Thank you
The 770.osc.gz file lives in the same directory as osmconvert.exe and the output extract.05m should populate in the same directory as the osmconvert.exe exists.
That's not what your code is saying. The code says "execute osmconvert.exe from inside c:\temp\ but read 770.osc.gz and write extract.o5m from the current working directory".
If you want everything to run inside c:\temp\ then you either have change to this directory before executing osmconvert or you have to preprend the path to every file you are passing to osmconvert.
Try this call instead:
cmd = r"""c:\temp\osmconvert.exe c:\temp\770.osc.gz -b=1,1,3,3 -o=c:\temp\extract.o5m"""

Categories