github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/tests/suites/static_analysis/lint_go.py (about)

     1  """Check the api client imports."""
     2  
     3  import sys
     4  import getopt
     5  
     6  def check_all(argv):
     7  	try:
     8  		opts, _ = getopt.getopt(argv[1:], 'ha:g:', ['help', "allowed=", "got="])
     9  	except:
    10  		print("{0} -a <allowed> -g <got>".format(argv[0]))
    11  		sys.exit(2)
    12  
    13  	allowed = []
    14  	got = []
    15  	for opt, arg in opts:
    16  		if opt in ("-h", "--help"):
    17  			print("{0} -a <allowed>".format(argv[0]))
    18  			sys.exit()
    19  		elif opt in ("-a", "--allowed"):
    20  			allowed = arg.split("\n")
    21  		elif opt in ("-g", "--got"):
    22  			got = arg.split("\n")
    23  			
    24  	if len(allowed) == 0:
    25  		print("No allowed imports specified")
    26  		sys.exit(1)
    27  	if len(got) == 0:
    28  		print("No imports found")
    29  		sys.exit(1)
    30  
    31  	for g in got:
    32  		matched = False
    33  		for a in allowed:
    34  			if g.startswith(a):
    35  				matched = True
    36  				break
    37  		if matched == False:
    38  			print(f"Import not allowed: {g}")
    39  			print("Consult the list of allowed imports.")
    40  			sys.exit(1)
    41  	sys.exit(0)
    42  
    43  if __name__ == '__main__':
    44  	check_all(sys.argv)