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

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

Related

how does jenkins recognize paths inside a python script?

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.

python-win32com excel com model started generating errors

Over the last few days, I have been working on automating the generation of some pivot tables for a number of reports.
Boiled down to the minimum, the following code was working without issue:
import win32com.client
objExcelApp = win32com.client.gencache.EnsureDispatch('Excel.Application')
objExcelApp.Visible = 1
This would pop-up an instance of excel and I could continue working in Python. But suddenly, today my scripts are failing with the following:
>>>import win32com.client
>>> objExcelApp = win32com.client.gencache.EnsureDispatch('Excel.Application')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files (x86)\Python37-32\lib\site-packages\win32com\client\gencache.py", line 534, in EnsureDispatch
mod = EnsureModule(tla[0], tla[1], tla[3], tla[4], bForDemand=bForDemand)
File "C:\Program Files (x86)\Python37-32\lib\site-packages\win32com\client\gencache.py", line 391, in EnsureModule
module = GetModuleForTypelib(typelibCLSID, lcid, major, minor)
File "C:\Program Files (x86)\Python37-32\lib\site-packages\win32com\client\gencache.py", line 266, in GetModuleForTypelib
AddModuleToCache(typelibCLSID, lcid, major, minor)
File "C:\Program Files (x86)\Python37-32\lib\site-packages\win32com\client\gencache.py", line 552, in AddModuleToCache
dict = mod.CLSIDToClassMap
AttributeError: module 'win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9' has no attribute 'CLSIDToClassMap'
The code has not changed from yesterday to today. I have no idea what is happening!!!.
Another interesting kicker. if I do the same code in the same session again I get a different error:
>>> objExcelApp = win32com.client.gencache.EnsureDispatch('Excel.Application')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files (x86)\Python37-32\lib\site-packages\win32com\client\gencache.py", line 534, in EnsureDispatch
mod = EnsureModule(tla[0], tla[1], tla[3], tla[4], bForDemand=bForDemand)
File "C:\Program Files (x86)\Python37-32\lib\site-packages\win32com\client\gencache.py", line 447, in EnsureModule
if module.MinorVersion != tlbAttributes[4] or genpy.makepy_version != module.makepy_version:
AttributeError: module 'win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9' has no attribute 'MinorVersion'
>>>
So I jump over to a windows machine with a fresh windows install, install python37 and pip install pypiwin32. Run the very same lines and excel opens just like it did yesterday on my original machine.
I tried un-installing and re-installing with no luck. Any idea what is going on here?
NOTE:
Dynamic dispatch still works:
import win32com.client
objExcelApp = win32com.client.Dispatch("Excel.Application")
objExcelApp.Visible = 1
But I specifically need static dispatch as Pivot Tables won't work with a dynamically dispatched object (much later in my code):
objExcelPivotCache = objExcelWorkbook.PivotCaches().Create(SourceType=win32c.xlDatabase, SourceData=objExcelPivotSourceRange)
I had the same issue and I resolved it by following the instructions here: https://mail.python.org/pipermail/python-win32/2007-August/006147.html
Deleting the gen_py output directory and re-running makepy SUCCEEDS
and subsequently the test application runs OK again.
So the symptom is resolved, but any clues as to how this could have
happened. This is a VERY long running application (think 24x7 for
years) and I'm concerned that whatever caused this might occur again.
To find the output directory, run this in your python console / python session:
import win32com
print(win32com.__gen_path__)
Based on the exception message in your post, the directory you need to remove will be titled '00020813-0000-0000-C000-000000000046x0x1x9'. So delete this directory and re-run the code. And if you're nervous about deleting it (like I was) just cut the directory and paste it somewhere else.
💡Note that this directory is usually in your "TEMP" directory (copy-paste %TEMP%/gen_py in Windows File Explorer and you will arrive there directly).
I have no idea why this happens nor do I know how to prevent it from happening again, but the directions in the link I provided seemed to work for me.
A more straightforward solution was posted in a related question Issue in using win32com to access Excel file.
Basically, you just need to delete the folder C:\Users\<your username>\AppData\Local\Temp\gen_py and rerun your code.
💡TIP: You can also put in your Windows file explorer %TEMP%\gen_py to access it directly, and then delete its content.
Execute this command line in a powershell or cmd (NOT in Administrator mode => wouldn't work for me)
python -m win32com.client.makepy "Excel.Application"
It fixes all errors and you don't have to change your python code.
And keep using
win32com.client.gencache.EnsureDispatch("Excel.Application")
With gencache.EnsureDispatch you have access to the constants of the application loaded dynamically by makepy which must have the registered application (in our case Excel.Application).
If you have the same problem with Outlook, use "Outlook.Application" in above.
If still not working, reinstall pywin32 of your python distribution
<path to python root or venv>\pip.exe uninstall pywin32
<path to python root or venv>\pip.exe install pywin32
What has worked for me is:
excel = win32.gencache.EnsureDispatch('Excel.Application')
#change to =>
excel = win32.Dispatch('Excel.Application')
For me, it seems, the issue was that I have multiple processes that interact with Windows apps through win32com.
Since win32com creates the "gen_py" directory in win32api.GetTempPath() this can cause conflicts and the cache getting corrupted.
My solution is to set a custom location for "gen_py" for each process. A simple example:
from pathlib import Path
import win32com
gen_py_path = '/some/custom/location/gen_py'
Path(gen_py_path).mkdir(parents=True, exist_ok=True)
win32com.__gen_path__ = gen_py_path
# Any other imports/code that uses win32com
This way you don't have to delete the default "gen_py" folder and wonder what issues might arise. But if you still find you need to delete, you can just delete the custom folder and know you're deleting the cache just for that process.
To add to this discussion, for those receiving this error as part of an unsupervised automated process, you can fully automating the recovery process and allow any processes to continue unsupervised.
As answers by Ian and Qin mention, we need to delete the gen_py output directory and restart the process. As far as automation goes, there are two problems with this: the gen_py output directory is a temporary directory that can change, and currently running processes that rely on gen_py continue to fail even after it is regenerated.
To address these problems, we can dynamically look up the location, then we can completely nuke the whole process and restart it. I've tried deleting and re-importing win32com, but it seems a reference to the corrupted cache is still maintained, so the whole process needs to be restarted.
temp_data_dir = os.environ.get('LOCALAPPDATA'))
gen_py_dir = ''
for curr_path, dirs, files, in walk(temp_data_dir)
if 'gen_py' in dirs:
gen_py_dir = Path(curr_path).joinpath('gen_py')
shutil.rmtree(gen_py_dir)
execv(restart_args)
The restarted process should call win32com.client.gencache.EnsureDispatch('Excel.Application') again and a fresh copy of gen_py will now be utilized, then we can retry the failed Excel automation code.
The issue happens sporadically in a completely unreliable way that I can't replicate. My best guess is that the gen_py cache is sometimes corrupted somehow and just needs to be refreshed.

Allure not able to read output.xml file generated by AllureReportLibrary in Robot Framework

I am using Ride (RobotFramework IDE) and I have imported Library AllureReportLibrary in my project.
Using the Set Output Dir, I am creating a Directory C:/AutomationLogs/Allure and all the allure properties and xml files are getting generated in that path.
Set Output Dir C:/AutomationLogs/
Then I am using the "allure serve C:\AutomationLogs\Allure" command to try and generate the html report file in command prompt, but it shows the below error -
"Could not read result
C:\AutomationLogs\Allure\f56f4796-d30a-47f3-a988-d17f6c4e13ca-testsuite.xml:
{} com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot
deserialize va lue of type
ru.yandex.qatools.allure.model.SeverityLevel from String "None":
value not one of declared Enum instance names: [trivial, blocker,
minor, normal, critical]"
The xml file "f56f4796-d30a-47f3-a988-d17f6c4e13ca-testsuite.xml" was generated using the AllureReportLibrary
Also the index.html file which is generated after the command opens after this command and shows Allure Report unknown
unknown - unknown (Unknown) 0 test cases NaN%
I am using the below -
Allure version - 2.4.1
Ride version - RIDE 1.5.2.1 running on Python 2.7.12.
I am new to Robot Framework and Allure. Please let me know whether I have implemented it correctly and why I am facing the above error.
-Ryan M
I'm using the 1.1.1 version of Allure Adaptor for Robot Framework and the severity is picked from the test case tags and added as a label under the test-case element of the report.
However, it seems that Allure 2.6.0 is also expecting a valid value for the severity attribute of the test-case element.
In order to use Allure2 with the current reports I have altered AllureListener.py to also add the severity to the test case:
elif tag in SEVERITIES:
test.severity = tag
test.labels.append(TestLabel(
name='severity',
value=tag
))
If your output.xml has severity = None for any testcase then the allure-robotframework-adaptor will give the error that you have mentioned. Creating TestCase() object with severity='' in start_suitesetup method of AllureListener.py will do the trick.
def start_suitesetup(self, name, attributes):
....
....
test = TestCase(name=name,
description=description,
start=now(),
attachments=[],
labels=[],
parameters=[],
steps=[],
severity='')
How to create the Allure reports in Robot Framework ?
Initially, Download the Command line and UNzip the file and save the path of the bin folder in environment.
Link : http://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/2.8.0/allure-commandline-2.8.0.zip
Unzip the above file then put it in the Environment folder.
Then Pip install the below modules
pip install allure-robotframework
pip install robotframework-allurereport
In robot file, Add the Library in Settings like,
Example :
Library AllureReportLibrary D:\eclipse\RobotFramework\results
Then Use the Below commands to run the robot code.
robot --listener allure_robotframework;D:\eclipse\RobotFramework\results
Example.txt
Finally,
Generate the HTML file by,
allure generate D:\eclipse\RobotFramework\results
Note : Use the same path what you used in the previous command to generate the HTml.file.
and
Open in Mozhila FireFox. It wont be work in Chrome. I dont know exactly why.
Regards,
Vijay

Flask on GAE - Connect to Google API - Can't use JSON file

I'm fairly new to Flask, GAE and the use of API. I'm trying to build a basic Web App that can connect to one of Google's API.
My folder structure looks like this (I've kept it to the main files):
app-webemotions:
-app.yaml
-main.py
-lib
--sentimentanalysis.py
-static
--credential.json
Everything is working but providing the json file for the credentials. My understanding is that there's a couple of ways to do it:
1) Setting up the GOOGLE_APPLICATION_CREDENTIALS environment variable to the destination of my file in app.yaml
2) Requesting the file through my script (sentimentanalysis.py)
Unfortunately, I haven't been able to make any of those work.
Option 1):
In app.yaml I have the line:
env_variables:
GOOGLE_APPLICATION_CREDENTIALS: static/key/credentials.json
I then run my code through dev_appserver.py . and get the following error:
ApplicationDefaultCredentialsError: File static/key/credentials.json (pointed by GOOGLE_APPLICATION_CREDENTIALS environment variable) does not exist!
Option 2):
I have a line of code in my script sentimentanalysis.py:
scope = ['https://www.googleapis.com/auth/cloud-platform']
credentials = ServiceAccountCredentials.from_json_keyfile_name('/static/credentials.json', scope)
And when running the code I get the following error:
raise IOError(errno.EACCES, 'file not accessible', filename)
IOError: [Errno 13] file not accessible: '/static/credentials.json'
INFO 2016-08-06 04:10:51,678 module.py:788] default: "POST /Sentiment-analysis HTTP/1.1" 500 -
Question:
So it looks like regardless of the method I'm using, I'm not able to provide the right path to the JSON file
My question is to know first if any of the above options is the right option and if yes, what am I doing wrong? If they are not the right options, what would you recommend?
Apologies if this has already been asked, I've tried to find an answer for a few hours now and haven't been able to crack it...
Thank you!
If you are running on Google App Engine, then your code automatically has the credentials it needs. Do not set GOOGLE_APPLICATION_CREDENTIALS and do not call .from_json_keyfile_name. Instead, call:
credentials = GoogleCredentials.get_application_default()
As shown here:
https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/bigquery/api/getting_started.py
set GOOGLE_APPLICATION_CREDENTIALS=credentials.json
use this command if you are using cmd

Python and Plone help

Im using the plone cms and am having trouble with a python script. I get a name error "the global name 'open' is not defined". When i put the code in a seperate python script it works fine and the information is being passed to the python script becuase i can print the query. Code is below:
#Import a standard function, and get the HTML request and response objects.
from Products.PythonScripts.standard import html_quote
request = container.REQUEST
RESPONSE = request.RESPONSE
# Insert data that was passed from the form
query=request.query
#print query
f = open("blast_query.txt","w")
for i in query:
f.write(i)
return printed
I also have a second question, can i tell python to open a file in in a certain directory for example, If the script is in a certain loaction i.e. home folder, but i want the script to open a file at home/some_directory/some_directory can it be done?
Python Scripts in Plone are restricted and have no access to the filesystem. The open call is thus not available. You'll have to use an External Method or full python module to have full access to the filesystem.

Categories