123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- from __future__ import print_function
- import argparse
- import os
- import re
- import sys
- parser = argparse.ArgumentParser()
- parser.add_argument("filenames", help="list of files to check, all files if unspecified", nargs='*')
- args = parser.parse_args()
- def is_binary(pathname):
- """Return true if the given filename is binary.
- @raise EnvironmentError: if the file does not exist or cannot be accessed.
- @attention: found @ http://bytes.com/topic/python/answers/21222-determine-file-type-binary-text on 6/08/2010
- @author: Trent Mick <TrentM@ActiveState.com>
- @author: Jorge Orpinel <jorge@orpinel.com>"""
- try:
- with open(pathname, 'r') as f:
- CHUNKSIZE = 1024
- while 1:
- chunk = f.read(CHUNKSIZE)
- if '\0' in chunk:
- return True
- if len(chunk) < CHUNKSIZE:
- break
- except:
- return True
- return False
- def get_all_files(rootdir):
- all_files = []
- for root, dirs, files in os.walk(rootdir):
-
- if 'vendor' in dirs:
- dirs.remove('vendor')
- if 'staging' in dirs:
- dirs.remove('staging')
- if '_output' in dirs:
- dirs.remove('_output')
- if '_gopath' in dirs:
- dirs.remove('_gopath')
- if 'third_party' in dirs:
- dirs.remove('third_party')
- if '.git' in dirs:
- dirs.remove('.git')
- if '.make' in dirs:
- dirs.remove('.make')
- if 'BUILD' in files:
- files.remove('BUILD')
- for name in files:
- pathname = os.path.join(root, name)
- if is_binary(pathname):
- continue
- all_files.append(pathname)
- return all_files
- def check_underscore_in_flags(rootdir, files):
-
- pathname = os.path.join(rootdir, "hack/verify-flags/excluded-flags.txt")
- f = open(pathname, 'r')
- excluded_flags = set(f.read().splitlines())
- f.close()
- regexs = [ re.compile('Var[P]?\([^,]*, "([^"]*)"'),
- re.compile('.String[P]?\("([^"]*)",[^,]+,[^)]+\)'),
- re.compile('.Int[P]?\("([^"]*)",[^,]+,[^)]+\)'),
- re.compile('.Bool[P]?\("([^"]*)",[^,]+,[^)]+\)'),
- re.compile('.Duration[P]?\("([^"]*)",[^,]+,[^)]+\)'),
- re.compile('.StringSlice[P]?\("([^"]*)",[^,]+,[^)]+\)') ]
- new_excluded_flags = set()
-
- for pathname in files:
- if not pathname.endswith(".go"):
- continue
- f = open(pathname, 'r')
- data = f.read()
- f.close()
- matches = []
- for regex in regexs:
- matches = matches + regex.findall(data)
- for flag in matches:
- if any(x in flag for x in excluded_flags):
- continue
- if "_" in flag:
- new_excluded_flags.add(flag)
- if len(new_excluded_flags) != 0:
- print("Found a flag declared with an _ but which is not explicitly listed as a valid flag name in hack/verify-flags/excluded-flags.txt")
- print("Are you certain this flag should not have been declared with an - instead?")
- l = list(new_excluded_flags)
- l.sort()
- print("%s" % "\n".join(l))
- sys.exit(1)
- def main():
- rootdir = os.path.dirname(__file__) + "/../"
- rootdir = os.path.abspath(rootdir)
- if len(args.filenames) > 0:
- files = args.filenames
- else:
- files = get_all_files(rootdir)
- check_underscore_in_flags(rootdir, files)
- if __name__ == "__main__":
- sys.exit(main())
|