Skip to content

Instantly share code, notes, and snippets.

@weaming
Created August 14, 2018 07:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weaming/6e2bbee1ba382e650e29bfc1e2d70538 to your computer and use it in GitHub Desktop.
Save weaming/6e2bbee1ba382e650e29bfc1e2d70538 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# coding: utf-8
"""
Author : weaming
Created Time : 2018-07-12 14:09:32
Parse python source code syntax to determine is python2 or python3
"""
import os
import ast
VERBOSE = os.getenv('VERBOSE')
def test_source_code_compatible(code_data):
try:
return ast.parse(code_data)
except SyntaxError as e:
if VERBOSE:
print('exception: {}'.format(e))
return False
if __name__ == '__main__':
import sys
fp_list = sys.argv[1:]
result = True
for fp in fp_list:
if test_source_code_compatible(open(fp).read()):
if VERBOSE:
print(fp, True, file=sys.stderr)
else:
result = False
if VERBOSE:
print(fp, False, file=sys.stderr)
if result:
sys.exit(0)
else:
sys.exit(1)
@weaming
Copy link
Author

weaming commented Aug 14, 2018

shell function

is-python3-project () {
	find ${1:=.} -type f -name '*.py' | xargs ispy3
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment