Docker + Django executing external node.js script - python

I have a problem with executing my node js script. When i run this script normally from command line everything is ok.
But when i want to execute that script directly from django it will print error like this:
the first line says that you are in the code before that line of code what execute that script. Second is path and is correct as you see. Third and to the error are files in that directory (I was checking if executing commands from command line works)
I recall that this django project runs in docker container so i think that error might be there.
Here is my line of code where i run that script in my view.py:
def prepare_log_img(picture):
string_without_header = re.sub('^data:image/.+;base64,', '', picture)
img_data = base64.b64decode(string_without_header)
filename = SITE_ROOT + "/faceId/logged.png"
with open(filename, 'wb') as f:
f.write(img_data)
os.chdir(SITE_ROOT + "/static/scripts")
print("Pred face detection")
print(SITE_ROOT + "/static/scripts")
print(os.system("ls"))
os.system("node face-detection.js logged.png")
The last line is that executing. Anyone knows what is the problem? Thank you.

The operating system executing that node face-detection.js logged.png line doesn't know where to go to find your node installation. You can probably fix that by giving the full path to your node installation.
In a regular terminal window type which node, to find out where it is installed, for me the result was /usr/sbin/node. So if I were to run that command it should probably be:
os.system("/usr/sbin/node face-detection.js logged.png")

Related

Getting permission error when calling a program from another drive with subprocess

I have a script that has 4 lines of code:
import subprocess
folderAdd = r"D:\Program Files (x86)\someapp\"
result = subprocess.run(['"' + folderAdd +'someProgram.exe"','"' + folderAdd +'somefile.ext"'], stdout=subprocess.PIPE)
print(result.stdout)
I've edited the code slightly to remove the specifics of the files/folders (since I assume that's not the issue?). The someProgram.exe is a go lang program I've made, the somefile.ext is a file that I pass to the go land program using the command line (eg the syntax in command line is: "someProgram.ext somefile.ext". The issue I'm having is when I run this script (which is stored on my E drive - so that's the working directory) I get the following error:
PermissionError: [WinError 5] Access is denied
I've tried running this python script from within spyder (my ide of choice) and from command line. Both of which I've tried running as administrator (based on this question/answer). I still get the same permission error. Is there any other way around this?
you're adding double quotes that should not be here.
Also you should join path using python's facility : os.path.join
(https://docs.python.org/3.8/library/os.path.html#os.path.join)
If you only what stdout, you can use subprocess.check_outout
(https://docs.python.org/3.8/library/subprocess.html#subprocess.check_output)
import subprocess
folderAdd = r"D:\Program Files (x86)\someapp"
print(subprocess.check_output([os.path.join(folderAdd, 'someProgram.exe'), os.path.join(folderAdd, 'somefile.ext'])

How to run Open Pose binary (.exe) from within a Python script?

I am making a body tracking application where I want to run Open Pose if the user chooses to track their body movements. The OpenPose binary file can be run like so:
bin\OpenPoseDemo.exe --write_json 'path\to\dump\output'
So, in my Python script, I want to have a line of code that would run Open Pose, instead of having to ask the user to manually run OpenPose by opening a separate command line window. For that, I have tried:
import os
os.popen(r"C:\path\to\bin\OpenPoseDemo.exe --write_json 'C:\path\to\dump\output'")
But this gives the following error:
Error:
Could not create directory: 'C:\Users\Admin\Documents\Openpose\. Status error = -1. Does the parent folder exist and/or do you have writing access to that path?
Which I guess means that OpenPose can be opened only by going inside the openpose directory where the bin subdirectory resides. So, I wrote a shell script containing this line:
bin\OpenPoseDemo.exe --write_json 'C:\path\to\dump\output'
and saved it as run_openpose_binary.sh in the openpose directory (i.e., the same directory where bin is located).
I then tried to run this shell script from within my Python script like so:
import subprocess
subprocess.call(['sh', r'C:\path\to\openpose\run_openpose_binary.sh'])
and this gives the following error:
FileNotFoundError: [WinError 2] The system cannot find the file specified
I also tried the following:
os.popen(r"C:\path\to\openpose\run_openpose_binary.sh")
and
os.system(r"C:\path\to\openpose\run_openpose_binary.sh")
These do not produce any error, but instead just pop up a blank window and closes.
So, my question is, how do I run the OpenPoseDemo.exe from within my Python script?
For your last method, you're missing the return value from os.popen, which is a pipe. So, what you need is something like:
# untested as I don't have access to a Windows system
import os
with os.popen(r"/full/path/to/sh C:/path/to/openpose/run_openpose_binary.sh") as p:
# pipes work like files
output_of_command = p.read().strip() # this is a string
or, if you want to future-proof yourself, the alternative is:
# untested as I don't have access to a Windows system
popen = subprocess.Popen([r'/full/path/to/sh.exe', r'/full/path/to/run_openpose_binary.sh')], stdin=subprocess.PIPE, stdout=subprocess.PIPE,encoding='utf-8')
stdout, stderr = popen.communicate(input='')
Leave a comment if you have further difficulty.
I've had to fight this battle several times and I've found a solution. It's likely not the most elegant solution but it does work, and I'll explain it using an example of how to run OpenPose on a video.
You've got your path to the openpose download and your path to the video, and from there it's a 3-line solution. First, change the current working directory to that openpose folder, and then create your command, then call subprocess.run (I tried using subprocess.call and that did not work. I did not try shell=False but I have heard it's a safer way to do so. I'll leave that up to you.)
import os
import subprocess
openpose_path = "C:\\Users\\me\\Desktop\\openpose-1.7.0-binaries-win64-gpu-python3.7-flir-3d_recommended\\openpose\\"
video_path = "C:\\Users\\me\\Desktop\\myvideo.mp4"
os.chdir(openpose_path)
command = "".join(["bin\\OpenPoseDemo.exe", " -video ", video_path])
subprocess.run(command, shell=True)

python on linux can't find file inside function

I'm writing a script on python 2.7.12 on linux with a bunch of functions that run external programs...
at some point, I have a function that runs a program, which generates an external file. The next step of the function is to read the external file and do some processing.
The function is
def RunProgram(input, options)
input_file=str(input)
options=str(options)
cmd=str('program + ' input_file + '--flag ' + options + ' --out temp.log &> /dev/null')
#print(cmd)
#print(cmd)
os.system(cmd)
with open('path_to_file/temp.log') as fp:
for i, line in enumerate(fp):
if i == 2:
#print(line)
solution=str(line) #stores 3rd line of log file
elif i > 2:
break
return solution
Somehow, although the external program runs and I see temp.log created from a bash shell, the function exits with the error
IOError: [Errno 2] No such file or directory: 'path_to_file/temp.log'
if in the function after os.system(cmd) I place
print(os.path.exists('path_to_file/temp.log'))
print(os.path.isfile('path_to_file/temp.log'))
I get 2 false, but if I run those commands after running the function and getting the error i get True for both and I see the file in the directory using ls.
Once I run the function again with temp.log already existing, the function works.
I have checked with os.getcwd() and the session is running in the correct dirrectory. I also checked I have read permissions on temp.log
..any ideas?
This is probably a race condition: When your code wants to read the output from the file, the program isn't finished yet and the output file doesn't exist yet. Try a loop with "sleep" waiting for the file you want to read to become available. Another possibility is that that the file permissions don't match.

Trying to run a python script with crontab every hour, but one section of the python code does not execute

I wrote a script using python and selenium that tries to register for a class called puppy play. Crontab runs the script every hour and sends any output to a file called "cronpup.log". This section of code is in my python script and it just checks to see if the registration was successful or not then appends the results to the file "pup.log".
# Pup Logger
f = open("pup.log", "a+")
f.write(time.strftime("%Y-%m-%d %H:%M:%S "))
if pups == 1:
f.write("Pups!\n")
elif pups == 0:
f.write("No Pups\n")
else:
f.write("Ruh Roh, Something is wrong\n")
f.close()
This creates the "pup.log" file with entries like the following
$ pup.log
2014-10-17 17:49:18 No Pups
2014-10-17 19:37:28 No Pups
I can run the python script just fine from the terminal, but when crontab executes the script no new entries are made in "pup.log". I've checked the output from crontab and have found nothing. Here is crontab's output
$ cronpup.log
.
----------------------------------------------------------------------
Ran 1 test in 81.314s
OK
It seems like crontab is just ignoring that section of the code, but that seems pretty silly. Any ideas how to get this working?
The line
f = open("pup.log", "a+")
is your problem. Open is looking the the current working directory for pup.log, creating it if necessary, and appending to it. If you run from the terminal while in the same directory as the python script, that's where pup.log will appear. The cwd when running from cron is the home directory of the user the job is running as, so when run from cron it's dropping a pup.log file somewhere else on your system.
You can either hardcode a full path, or use
os.chdir(os.path.dirname(os.path.abspath(__file__)))
to set the current working directory to the directory the python file is in, or modify the above to put pup.log whereever you like.

Runing bcdedit from python in Windows 2008 SP2

I do not know windows well, so that may explain my dilemma ...
I am trying to run bcdedit in Windows 2008R2 from Python 2.6.
My Python routine to run a command looks like this:
def run_program(cmd_str):
"""Run the specified command, returning its output as an array of lines"""
dprint("run_program(%s): entering" % cmd_str)
cmd_args = cmd_str.split()
subproc = subprocess.Popen(cmd_args, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
(outf, errf) = (subproc.stdout, subproc.stderr)
olines = outf.readlines()
elines = errf.readlines()
if Options.debug:
if elines:
dprint('Error output:')
for line in elines:
dprint(line.rstrip())
if olines:
dprint('Normal output:')
for line in olines:
dprint(line.rstrip())
errf.close()
outf.close()
res = subproc.wait()
dprint('wait result=', res)
return (res, olines)
I call this function thusly:
(res, o) = run_program('bcdedit /set {current} MSI forcedisable')
This command works when I type it from a cmd window, and it works when I put it in a batch file and run it from a command window (as Administrator, of course).
But when I run it from Python (as Administrator), Python claims it can't find the command, returning:
bcdedit is not recognized as an internal or external command,
operable program or batch file
Also, if I trying running my batch file from Python (which works from the command line), it also fails. I've also tried it with the full path to bcdedit, with the same results.
What is it about calling bcdedit from Python that makes it not found?
Note that I can call other EXE files from Python, so I have some level of confidence that my Python code is sane ... but who knows.
Any help would be most appreciated.
Windows 2008 R2 is 64-bit-only, yes? Python's a 32-bit process. When a 32-bit app runs something from C:\Windows\System32, Windows actually looks in C:\Windows\SysWOW64. Use C:\Windows\SysNative.
Perhaps the path to bcdedit.exe isn't in your system path when Python is running for some reason (a different user account, for example). You can find this out by printing:
os.environ.get("PATH")
It's semicolon-delimited, so os.environ.get("PATH").split(';') might be more useful.
I can't see any reason why it wouldn't be there, but just in case, you should be looking for C:\Windows\System32, where C is your Windows drive letter.
Check your PATH variable and see if C:\windows\system32 is there. (use set in DOS)
For some reason I experiment the same trouble from c#. If I list the files it was not here, but when I was looking from Explorer it was there. maybe it is some kind of protected file. To call bcdedit.exe, I manually copied it from system32 to my application folder and it worked. There is also another one in windows\winsxs folder. I can start it from my application, but I`m not sure it is the same path on all computers.
Hope it helps!

Categories

Resources