I am trying to create a function in my automation python code that will open an editor or other text viewer with the test log file while it's been written.
My automation is based on pytest so I am executing my tests with pytest test_name.py command.
I searched in the Internet and found that with subprocess python package I can do it but when I wrote the following code:
def __open_visual_logging(self):
try:
return subprocess.Popen(["vim",self._filename])
except Exception:
return None
Nothing is happening when I'm running my tests.
(The return value is 1)
Is there other way to do this (packages etc.) ? or what am I doing wrong?
Related
I'm not sure, if this is related to Can't find module when running python script from another python script.
However, if I run my python script directly I have no issues, but when I start it from another script, I get an error:
'Non-existent config key: model.kwargs.backbone_config.kwargs.input_height'
Even though the key can be found if I run the file directly.
However, if I try to execute the following code:
try:
config.merge_from_file('{base}/HoHoNet/config/s2d3d_sem/HOHO_depth_dct_efficienthc_TransEn1_h1024_fold1_resnet101rgb.yaml'.format(base = os.getcwd()))
except Exception as e:
print(e)
return 1
where my config is taken from yacs.config.
CI newbie here. I'm currently working on the python track for Exercism.io. I'm looking for a way to automate the process of running tests from pytest, committing and pushing to github, and finally submitting to exercism if all tests pass. I've implemented a pre-commit hook to invoke tests on commit but I'm not sure how to pass the filename back to exercism for submission. Any help is greatly appreciated!
I put together a solution for anyone else looking for a similar workflow. It utilizes a python script in the root directory and handles the git commands via subprocess. It also supports autocomplete for the file directory (based on this gist). This script assumes you've already initialized the git repo and setup your remote repo as well.
#!/usr/bin/env python
import pytest
import subprocess
import readline
import glob
class tabCompleter(object):
"""
A tab completer that can complete filepaths from the filesystem.
Partially taken from:
http://stackoverflow.com/questions/5637124/tab-completion-in-pythons-raw-input
"""
def pathCompleter(self,text,state):
"""
This is the tab completer for systems paths.
Only tested on *nix systems
"""
return [x for x in glob.glob(text+'*')][state]
if __name__=="__main__":
# Take user input for commit message
commit_msg = raw_input('Enter the commit message: ')
t = tabCompleter()
readline.set_completer_delims('\t')
# Necessary for MacOS, Linux uses tab: complete syntax
if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")
#Use the new pathCompleter
readline.set_completer(t.pathCompleter)
#Take user input for exercise file for submission
ans = raw_input("Select the file to submit to Exercism: ")
if pytest.main([]) == 0:
"""
If all tests pass:
add files to index,
commit locally,
submit to exercism.io,
then finally push to remote repo.
"""
subprocess.call(["git","add","."])
subprocess.call(["git","commit","-m", commit_msg])
subprocess.call(["exercism","submit",ans])
subprocess.call(["git","push","origin","master"])
The objective is to receive an image path and pass that to a Python program as an argument, then receive the results.
This is done through a web app using VB (on the IIS server) and it works perfectly except when I import the python module OpenCV (imported in Python as cv2, more specifically).
What's even more confusing is that the script runs perfectly with the imported cv2 module when executed directly from cmd. It only fails to work when the VB code runs the script including the line import cv2.
I'll show some code below, for clarity.
VB code running Python script with image path as an argument:
Dim Processtask As New System.Diagnostics.Process()
Processtask.StartInfo.FileName = "cmd.exe"
Processtask.StartInfo.Arguments = "/c python " + path.ToString + " " + ImageURL.ToString
Processtask.StartInfo.UseShellExecute = False
Processtask.StartInfo.RedirectStandardOutput = True
Processtask.StartInfo.RedirectStandardError = True
Processtask.StartInfo.CreateNoWindow = True
Processtask.Start()
Processtask.WaitForExit()
output = Processtask.StandardOutput.ReadToEnd()
Python code snippet receiving image path:
import sys
import cv2
if __name__ == "__main__":
im = str(sys.argv[1])
print(im)
I have run out of possible ideas as to what could cause this problem. Any advice would be greatly appreciated.
EDIT
I managed to find the full error message which reads as follows:
System.Exception: System.IO.StreamReader
System.InvalidOperationException: Process has exited, so the requested
information is not available.
at System.Diagnostics.Process.EnsureState(State state) at
System.Diagnostics.Process.get_ProcessName()
at System.Diagnostics.Process.ToString()
Got the solution eventually, I'll post it here in case anyone else ever runs into this problem:
The dll files of opencv installed onto the server which hosted the web app had different access rights. The files were denied access when being called from the web application, whereas the rest of the modules called had no issue.
I used sysinternals process monitor to trace which files were being denied access and was able to change the rights by hand. Not very elegant but it worked out.
I am using python 2.4 for a program which imports scripts from the internet and executes them so a script could be changed by the author and the user wouldn't have to re-download the script.
This is the part of the program that downloads the script:
def downloadScript(self,script):
myfile=open('#A file path/'+script['name']+'.txt','w')
try:
downloadedScript=urllib.urlopen(script['location']).read()
except:
#raise error
return
myfile.write(downloadedScript)
myfile.close()
def loadScript(self):
if not self.scriptCurrentlyLoaded:
script=self.scripts[self.scroller.listPos]
if script['location']=='None':
#raise error
return
self.downloadScript(script)
myfile=open('#A file path/'+script['name']+'.txt','r')
for line in myfile:
if line.startswith('from') or line.startswith('import'):
exec(line.strip()) #This was added because of the name errors
#being produced but to no affect
myfile.close()
execfile('#A file path/'+script['name']+'.txt')
self.scriptCurrentlyLoaded=True
self.scriptLoaded=script
else:
#raise error
The very odd thing is that when I run
execfile(script path)
outside of the function, after the script has been downloaded, the script is executed correctly. But trying to run the loadScript function raises name errors in the script even though the names have been imported in the script and before the execfile which I find very odd.
So my question is: Am I using a very bad method to download and execute these scripts?
Sorry if this question was answered before but I can't seem to find anyone else who is trying to run python scripts by downloading them from the internet.
Edit: adding globals as another argument to the execfile has seemed to fix the problem for now. I don't know if any other problems will occur later though.
In R you can simply 'source(url)'. Here's the closest I have found so far in python:
import urllib
(fn,hd) = urllib.urlretrieve('http://host.com/file.py')
execfile(fn)
I am trying to have a Gtk.Application which stays unique and handles opening files. I am using python 2.7.3 on Ubuntu 12.04 with Gtk3 (fairly new to both python and Gtk)
The application runs fine without parameters, but it fails to get the file list when I run it trying to open a file. Here is the code, as minimalistic as I could make it:
#!/usr/bin/env python
import sys
from gi.repository import Gtk, Gio
def do_open(app, files, *hint):
print(app)
print(files)
print(hint)
def do_activate(app):
print "activate"
test = Gtk.Application(application_id="a.b", flags=Gio.ApplicationFlags.HANDLES_OPEN)
test.set_inactivity_timeout(10000)
test.connect("open", do_open)
test.connect("activate", do_activate)
test.run(sys.argv)
When I run the program without arguments it just prints "activate", which is fine. When I run it with a parameter (like ./test.py test.py) I get the following:
/usr/lib/python2.7/dist-packages/gi/types.py:43: Warning: g_value_get_boxed: assertion `G_VALUE_HOLDS_BOXED (value)' failed
return info.invoke(*args, **kwargs)
<Application object at 0x1c75230 (GtkApplication at 0x1cba0b0)>
[]
(1, '')
Does anyone understand why that assertion is failing and why I am getting an empty list of files?
As common as this task appears to be, I couldn't find any working example online either.
There is a bug in PyGObject. It is already reported in GNOME Bugzilla, check the bug report titled "Does not handle GFile in signal arguments".
Update: The bug was fixed in 2013. No more assertion and it returns the list of files (GFiles). In other words, the code works as expected (at least using 3.14):
$ python test.py test.py
<Application object at 0x7fcaf18f5d20 (GtkApplication at 0x11892b0)>
[<__main__.GLocalFile object at 0x7fcaf18a6050 (GLocalFile at 0x11aea00)>]
(1, '')