fix nearly all undefined var errs reported by pyflakes
Merge pull request #4667 from hulu/fix-undefined-vars

fix nearly all undefined var errs reported by pyflakes