[DONOTMERGE] Improve Python 3 compat, clean up code

Further improve compatibility with Python 3 by making the `urlparse` import
conditional.

Make some sed expressions use "|" as a separator instead of "/", thus allowing
all occurrences of "\/" to be removed.

Update some comments and shuffle around code to make it more compact and
readable. Add some FIXMEs.
Any special reason to not use chmod? As I can figure out but the changes it will do the `mkdir` and `chmod` as necessary, isn't it?