I've got a strange problem with my automated minify tool. I had to replace yuicompressor with csso, since yuicompressor hasn't updated since 2013 and has many bugs with newer css selectors.
This has increased the complexity (as in amount of involved "stuff") to the point where I have no idea how to debug an issue with the command executed. (Or even if it got executed).
I've basically got my problem narrowed down to the following python code snippet (currently with a lot of added debug statements to help diagnose the problem):
print("node " + os.path.join(getScriptPath(), "css", "csso", "bin", "csso")
+ " -i " + os.path.join(root, name) + " -o " + os.path.join(root, outname) + " --debug")
os.remove(os.path.join(root, outname))
try:
result = subprocess.check_output(["node",
os.path.join(getScriptPath(), "css", "csso", "bin", "csso"),
"-i", os.path.join(root, name),
"-o", os.path.join(root, outname),
"--debug"
], stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as exc:
print("ERROR: Failed to execute CSSO with error:", exc.returncode, exc.output)
Note here that getScriptPath()
is a small helper function that returns the current path the script file is in. csso
is installed by copying the files manually under css/csso/bin/csso
. name
is a string holding the relative path to the original css file (usually something like ../../domains\mydomain.com\templates\css\style.css
), while outname
is always the same as name
, but with the extension changed to .min.css
.
If I copy & paste the result of the first print()
call directly in a cmd
terminal, csso
will run and minify my file. But, if I call a batch script that runs a python program that eventually runs the code above (as part of the things it does), nothing is done, with no output.
The subprocess.run
call used to be a simple shell
call, which would have no output. Yet, I still get no output. Here's some example output I'm getting by manually running it (in stderr):
## parsing done in 16 ms
Compress block #1
[0.000s] init
[0.000s] clean
[0.015s] replace
[0.000s] prepare
[0.000s] mergeAtrule
[0.000s] initialMergeRuleset
[0.000s] disjoinRuleset
[0.016s] restructShorthand
[0.000s] restructBlock
[0.000s] mergeRuleset
[0.016s] restructRuleset
## compress done in 47 ms
## generate done in 0 ms
(The current output from the python version appears empty. If it contains any data, all the bytes are printed as empty in the standard cmd
tool).
One idea: is there a way to get subprocess.run
(or shell
) to echo back the exact command it executed, and from exactly which path, so I can replicate it manually and at least exclude any mistakes in constructing the command or diffrences in quotes, etc? Ideally probably as a binary file, as this looks like 'two things appear exactly the same but aren't' fun.
Other relevant tags: batch, minify.
I've managed to diagnose the problem.
On windows, there are somehow multiple different PATH variables. The machine where the problem occurred had not rebooted after installing
node.js
, and the PATH was available forcmd
directly, but not topython
, which still used the old variable. Hence it got an error; node doesn't exist.For some reason still unknown to me that error was silenced with the original code. But, adding these two lines in between (indicated by
>>>
) would cause the program to print the results of both commands (instead of neither):