|
From: Will S. <ws...@Pa...> - 2022-05-16 00:42:16
|
To whom it may concern,
We are finally porting our old webware application for Windows to w4py3, and I ran into a bit of ugliness that I wanted to report.
As a test, I had made the HelloWorld app in a subfolder to our main app, and verified my w4py3 install. I then wanted to just run it initially from the WindowsService wrapper I wrote around webware's WaitressServer.py script as a first step before tackling the port of the rest of our application.
Now, webware's default template WSGIScript.py file has the logic:
workDir = None
if workDir is None:
workDir = os.path.dirname(os.path.dirname(__file__))
if workDir:
os.chdir(workDir)
if '.' in libDirs:
sys.path.insert(0, workDir)
And the WaitressServer.py, among its many nifty options, has a -wsgi-script argument, so I provided the HelloWorld/Scripts/WSGIScript.py as the -wsgi-script argument to the WaitressServer main() function, expecting this to just work as written above. But the workDir did not change to the subfolder, and I spent hours trying to figure out why. What I finally discovered was the cause was the following unexpected code modification in WaitressServer.py:
# do not change working directory in the script
script = script.replace('workDir =', "workDir = '' #")
scriptVars = {}
exec(script, scriptVars)
Now, I suspect the reason that this hack above to was done because, given the way this script is "invoked" (via exec), if you didn't do this, when you tried to exec the WSGIScript.py content, you'd get:
NameError: name '__file__' is not defined
but then why have all that logic in the WSGIScript.py determining workDir in the first place? Is the WSGIScript.py file used in some other context?
And what was the point of adding -wsgi-script as an argument to WaitressServer.py if it didn't allow you to specify app location other than the default?
For what it's worth, I've hacked around this temporarily, by replacing the above line in WaitressServer.py with slightly different code modification logic, i.e.:
Import os
[...]
fullpath = "r'" + os.path.abspath(args.wsgi_script) + "'"
script = script.replace('__file__', fullpath)
But I'm not convinced this is the best solution, because I'm not sure what the intent was for the -wsgi-script argument if not to allow you to specify where the app you want to serve lives... Is there a better way to do that?
Regards,
/Will Sadkin
|