Created
August 14, 2018 07:20
-
-
Save weaming/6e2bbee1ba382e650e29bfc1e2d70538 to your computer and use it in GitHub Desktop.
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 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
shell function