No promises, but this seems to be working for me. I stayed strictly within Python ... so it's going to leave the original epub unchanged and create a new one with the desired changes.
Just like your original script, it's launched as follows:
Code:
./cm2em.py old.epub new.epub
There's not much in the way of error-checking, so that might need some work. And the script doesn't "fix" anything. If it wasn't a valid epub archive before, it's not going to be valid after. But by the same token ... if the original was a validated epub to begin with, the script shouldn't break it (structurally speaking, anyway). It just runs your conversion routine on any css files found in any zip archive (epub) you feed to it.
The file extension is .txt so I could upload it easily. Change it to .py if it amuses you.
EDIT: Got rid of an unnecessary conditional and changed it so if the stylesheet didn't need altering, it isn't unnecessarily rewritten. That way, all file properties (modification date, etc...) should stay the same (unless they were altered). Can't guarantee that Python's zlib compression implementation will result in a compressed size that exactly matches whatever was used to create the epub originally, but it should be close.