Last active
October 21, 2017 06:55
-
-
Save itkr/fd789d30f98967d892f0 to your computer and use it in GitHub Desktop.
format-python.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import shlex | |
import subprocess | |
_env = None | |
def get_env(): | |
global _env | |
if _env is None: | |
_env = os.environ.copy() | |
path = os.sep.join(_env['_'].split(os.sep)[0: -1]) | |
_env['PATH'] = ':'.join([path, _env['PATH']]) | |
return _env | |
def execute(cmd): | |
env = get_env() | |
popen = subprocess.Popen( | |
shlex.split(cmd) if not isinstance(cmd, list) else cmd, | |
stdout=subprocess.PIPE, | |
env=env) | |
output = popen.communicate()[0] | |
return output | |
def ls(): | |
# return execute('find $PWD/* -type f -print | grep py').split('\n') | |
return execute(['find', os.environ['PWD'], '-type', 'f', '-print']).split('\n') | |
def get_python_files(): | |
return [path for path in ls() if os.path.splitext(path)[1] == '.py'] | |
def main(): | |
for path in get_python_files(): | |
execute('pyformat -i %s' % path) | |
execute('isort %s' % path) | |
print path | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment