18from b2test_utils 
import skip_test_if_central
 
   21Check that no light-release-breaking dependencies have been added. 
   23If you are failing this test you have managed to break light builds, please 
   24check with the light release manager for more information. 
   28def get_sconscripts(package):
 
   29    """glob to get the path to all sconscript files 
   32        package (str): the name of the package directory 
   35        list(str): a list of all SConscript files 
   37    path_to_package = basf2.find_file(package)
 
   38    return glob.glob(f
"{path_to_package}/**/SConscript", recursive=
True)
 
   41def get_dependencies(sconscript_filename):
 
   42    """grab all of the LIBS in the sconscript file 
   45        sconscript_filename (str): the path to the sconscript file 
   48        set(str): a set of packages in the env['LIBS'] in this file 
   52    with open(sconscript_filename) 
as fi:
 
   54        dependencies = re.search(
 
   55            r"env\['LIBS'\] *= *\[\n*((?:.*?|\n)*?)\n*\]", file_data
 
   58            dependencies = dependencies.group(1)
 
   65        lb.replace(
"'", 
"").replace(
'"', 
"").strip()
 
   66        for lb 
in re.split(
",|\n", dependencies)
 
   67        if not lb.count(
"#") 
and not lb == 
'' 
   71    package_dependencies = {lb.split(
"_")[0] 
for lb 
in dependencies}
 
   73    return package_dependencies
 
   76def check_dependencies(forbidden, sconscript_files, error=""):
 
   77    """Check that there are no dependencies that are forbidden. 
   80        forbidden set(str): a set of packages we don't want to depend on 
   81        sconscript_files list(str): a list of paths to sconscript files 
   82        error str: optionally specify the error message 
   85        int: count of the number of forbidden dependencies found 
   88    for sconscript_filename 
in sconscript_files:
 
   91        this_dependencies = get_dependencies(sconscript_filename)
 
   95        forbidden_packages_dependencies = forbidden.intersection(this_dependencies)
 
   98        if len(forbidden_packages_dependencies) != 0:
 
  100                f
"The sconscript file {sconscript_filename} depends on",
 
  101                forbidden_packages_dependencies,
 
  108if __name__ == 
"__main__":
 
  112    skip_test_if_central()
 
  116    light_file = basf2.find_file(
".light")
 
  117    with open(light_file) 
as fi:
 
  118        light_packages = [li.strip().replace(
"/", 
"") 
for li 
in fi 
if li.strip().endswith(
"/")]
 
  123    all_packages = subprocess.check_output(
 
  124        [
"b2code-package-list"], cwd=os.path.dirname(light_file),
 
  126    all_packages = all_packages.decode(
"utf-8").strip().split(
" ")
 
  129    non_light_packages = set(all_packages).difference(set(light_packages))
 
  136    for package 
in light_packages:
 
  137        sconscript_files = get_sconscripts(package)
 
  138        return_code_sum += check_dependencies(
 
  139            non_light_packages, sconscript_files, 
"This breaks light releases! Sorry." 
  143    print(
"Test of light dependencies, the loop took:", time() - start, 
"seconds to run")
 
  144    print(
"There were", return_code_sum, 
"forbidden dependencies")
 
  146    sys.exit(return_code_sum)