style: apply black and isort

This commit is contained in:
Gabriel Ferreira
2023-11-19 20:07:19 -03:00
parent af98671fbe
commit 8f6a3413a4
68 changed files with 7848 additions and 4299 deletions

View File

@@ -1,10 +1,10 @@
import builtins
from functools import lru_cache
import glob
import os.path
import re
import sys
import sysconfig
import re
from functools import lru_cache
DEFAULT_INCLUDE_DIR = sysconfig.get_config_var("INCLUDEDIR")
DEFAULT_LIB_DIR = sysconfig.get_config_var("LIBDIR")
@@ -14,7 +14,7 @@ def find_ns3_lock() -> str:
# Get the absolute path to this file
path_to_this_init_file = os.path.dirname(os.path.abspath(__file__))
path_to_lock = path_to_this_init_file
lock_file = (".lock-ns3_%s_build" % sys.platform)
lock_file = ".lock-ns3_%s_build" % sys.platform
# Move upwards until we reach the directory with the ns3 script
prev_path = None
@@ -32,15 +32,17 @@ def find_ns3_lock() -> str:
return path_to_lock
SYSTEM_LIBRARY_DIRECTORIES = (DEFAULT_LIB_DIR,
os.path.dirname(DEFAULT_LIB_DIR),
"/usr/lib64",
"/usr/lib",
)
DYNAMIC_LIBRARY_EXTENSIONS = {"linux": "so",
"win32": "dll",
"darwin": "dylib",
}
SYSTEM_LIBRARY_DIRECTORIES = (
DEFAULT_LIB_DIR,
os.path.dirname(DEFAULT_LIB_DIR),
"/usr/lib64",
"/usr/lib",
)
DYNAMIC_LIBRARY_EXTENSIONS = {
"linux": "so",
"win32": "dll",
"darwin": "dylib",
}
LIBRARY_EXTENSION = DYNAMIC_LIBRARY_EXTENSIONS[sys.platform]
@@ -76,8 +78,9 @@ def _search_libraries() -> dict:
library_search_paths += [os.path.dirname(library_search_paths[-1])]
# Filter unique search paths and those that are not part of system directories
library_search_paths = list(filter(lambda x: x not in SYSTEM_LIBRARY_DIRECTORIES,
set(library_search_paths)))
library_search_paths = list(
filter(lambda x: x not in SYSTEM_LIBRARY_DIRECTORIES, set(library_search_paths))
)
# Search for the core library in the search paths
libraries = []
@@ -88,7 +91,9 @@ def _search_libraries() -> dict:
# Search system library directories (too slow for recursive search)
for search_path in SYSTEM_LIBRARY_DIRECTORIES:
if os.path.exists(search_path):
libraries += glob.glob("%s/**/*.%s*" % (search_path, LIBRARY_EXTENSION), recursive=False)
libraries += glob.glob(
"%s/**/*.%s*" % (search_path, LIBRARY_EXTENSION), recursive=False
)
libraries += glob.glob("%s/*.%s*" % (search_path, LIBRARY_EXTENSION), recursive=False)
del search_path, library_search_paths
@@ -106,7 +111,7 @@ def _search_libraries() -> dict:
library_map[library_infix].add(library)
# Replace sets with lists
for (key, values) in library_map.items():
for key, values in library_map.items():
library_map[key] = list(values)
return library_map
@@ -128,7 +133,7 @@ LIBRARY_AND_DEFINES = {
"libxml2": ["HAVE_LIBXML2"],
"libsqlite3": ["HAVE_SQLITE3"],
"openflow": ["NS3_OPENFLOW", "ENABLE_OPENFLOW"],
"click": ["NS3_CLICK"]
"click": ["NS3_CLICK"],
}
@@ -137,11 +142,11 @@ def add_library_defines(library_name: str):
defines = ""
if len(has_defines):
for define in LIBRARY_AND_DEFINES[has_defines[0]]:
defines += (f"""
defines += f"""
#ifndef {define}
#define {define} 1
#endif
""")
"""
return defines
@@ -156,7 +161,9 @@ def extract_linked_libraries(library_name: str, prefix: str) -> tuple:
# First discover which 3rd-party libraries are used by the current module
try:
with open(os.path.abspath(library_path), "rb") as f:
linked_libs = re.findall(b"\x00(lib.*?.%b)" % LIBRARY_EXTENSION.encode("utf-8"), f.read())
linked_libs = re.findall(
b"\x00(lib.*?.%b)" % LIBRARY_EXTENSION.encode("utf-8"), f.read()
)
except Exception as e:
print(f"Failed to extract libraries used by {library_path} with exception:{e}")
exit(-1)
@@ -181,7 +188,8 @@ def extract_library_include_dirs(library_name: str, prefix: str) -> tuple:
# Raise error in case the library can't be found
if len(linked_library_path) == 0:
raise Exception(
f"Failed to find {linked_library}. Make sure its library directory is in LD_LIBRARY_PATH.")
f"Failed to find {linked_library}. Make sure its library directory is in LD_LIBRARY_PATH."
)
# Get path with the shortest length
linked_library_path = sorted(linked_library_path, key=lambda x: len(x))[0]
@@ -199,7 +207,9 @@ def extract_library_include_dirs(library_name: str, prefix: str) -> tuple:
linked_libs_include_dirs.add(system_include_dir)
# If system_include_dir/library_name exists, we add it too
linked_library_name = linked_library.replace("lib", "").replace("." + LIBRARY_EXTENSION, "")
linked_library_name = linked_library.replace("lib", "").replace(
"." + LIBRARY_EXTENSION, ""
)
if os.path.exists(os.path.join(system_include_dir, linked_library_name)):
linked_libs_include_dirs.add(os.path.join(system_include_dir, linked_library_name))
@@ -230,19 +240,26 @@ def find_ns3_from_lock_file(lock_file: str) -> (str, list, str):
suffix = "-" + values["BUILD_PROFILE"] if values["BUILD_PROFILE"] != "release" else ""
modules = [module.replace("ns3-", "") for module in values["NS3_ENABLED_MODULES"]]
prefix = values["out_dir"]
libraries = {os.path.splitext(os.path.basename(x))[0]: x for x in os.listdir(os.path.join(prefix, "lib"))}
libraries = {
os.path.splitext(os.path.basename(x))[0]: x for x in os.listdir(os.path.join(prefix, "lib"))
}
version = values["VERSION"]
# Filter out test libraries and incorrect versions
def filter_in_matching_ns3_libraries(libraries_to_filter: dict,
modules_to_filter: list,
version: str,
suffix: str,) -> dict:
def filter_in_matching_ns3_libraries(
libraries_to_filter: dict,
modules_to_filter: list,
version: str,
suffix: str,
) -> dict:
suffix = [suffix[1:]] if len(suffix) > 1 else []
filtered_in_modules = []
for module in modules_to_filter:
filtered_in_modules += list(filter(lambda x: "-".join([version, module, *suffix]) in x,
libraries_to_filter.keys()))
filtered_in_modules += list(
filter(
lambda x: "-".join([version, module, *suffix]) in x, libraries_to_filter.keys()
)
)
for library in list(libraries_to_filter.keys()):
if library not in filtered_in_modules:
libraries_to_filter.pop(library)
@@ -255,9 +272,9 @@ def find_ns3_from_lock_file(lock_file: str) -> (str, list, str):
for module in modules:
library_name = f"libns{version}-{module}{suffix}"
if library_name not in libraries:
raise Exception(f"Missing library {library_name}\n",
"Build all modules with './ns3 build'"
)
raise Exception(
f"Missing library {library_name}\n", "Build all modules with './ns3 build'"
)
libraries_to_load.append(libraries[library_name])
return prefix, libraries_to_load, version
@@ -276,7 +293,14 @@ def filter_module_name(library: str) -> str:
components.pop(0)
# Drop build profile suffix and test libraries
if components[-1] in ["debug", "default", "optimized", "release", "relwithdebinfo", "minsizerel"]:
if components[-1] in [
"debug",
"default",
"optimized",
"release",
"relwithdebinfo",
"minsizerel",
]:
components.pop(-1)
return "-".join(components)
@@ -331,10 +355,14 @@ def find_ns3_from_search() -> (str, list, str):
# Filter out older ns-3 libraries
for module in list(modules_to_filter):
# Filter duplicates of modules, while excluding test libraries
conflicting_libraries = list(filter(lambda x: module == filter_module_name(x), libraries_to_filter))
conflicting_libraries = list(
filter(lambda x: module == filter_module_name(x), libraries_to_filter)
)
# Extract versions from conflicting libraries
conflicting_libraries_versions = list(map(lambda x: extract_version(x, module), conflicting_libraries))
conflicting_libraries_versions = list(
map(lambda x: extract_version(x, module), conflicting_libraries)
)
# Get the newest version found for that library
newest_version = get_newest_version(conflicting_libraries_versions)
@@ -345,7 +373,9 @@ def find_ns3_from_search() -> (str, list, str):
else:
newest_version_found = get_newest_version([newest_version, newest_version_found])
if newest_version != newest_version_found:
raise Exception(f"Incompatible versions of the ns-3 module '{module}' were found: {newest_version} != {newest_version_found}.")
raise Exception(
f"Incompatible versions of the ns-3 module '{module}' were found: {newest_version} != {newest_version_found}."
)
for conflicting_library in list(conflicting_libraries):
if "-".join([newest_version, module]) not in conflicting_library:
@@ -353,7 +383,9 @@ def find_ns3_from_search() -> (str, list, str):
conflicting_libraries.remove(conflicting_library)
if len(conflicting_libraries) > 1:
raise Exception(f"There are multiple build profiles for module '{module}'.\nDelete one to continue: {', '.join(conflicting_libraries)}")
raise Exception(
f"There are multiple build profiles for module '{module}'.\nDelete one to continue: {', '.join(conflicting_libraries)}"
)
return libraries_to_filter, newest_version_found
@@ -379,7 +411,9 @@ def load_modules():
libraries = list(map(lambda x: os.path.basename(x), libraries))
for ns3_library in libraries:
_, _, linked_libraries = extract_linked_libraries(ns3_library, prefix)
linked_libraries = list(filter(lambda x: "libns3" in x and ns3_library not in x, linked_libraries))
linked_libraries = list(
filter(lambda x: "libns3" in x and ns3_library not in x, linked_libraries)
)
linked_libraries = list(map(lambda x: os.path.basename(x), linked_libraries))
module_dependencies[os.path.basename(ns3_library)] = linked_libraries
@@ -396,19 +430,27 @@ def load_modules():
modules.append(pending_module)
return modules
def dependency_order(module_dependencies, pending_modules, current_modules, step_number=0, steps={}):
def dependency_order(
module_dependencies, pending_modules, current_modules, step_number=0, steps={}
):
if len(pending_modules) == 0:
return steps
if step_number not in steps:
steps[step_number] = []
for module in modules_that_can_be_loaded(module_dependencies, pending_modules, current_modules):
for module in modules_that_can_be_loaded(
module_dependencies, pending_modules, current_modules
):
steps[step_number].append(module)
pending_modules.remove(module)
current_modules.append(module)
return dependency_order(module_dependencies, pending_modules, current_modules, step_number + 1, steps)
return dependency_order(
module_dependencies, pending_modules, current_modules, step_number + 1, steps
)
sorted_libraries = []
for step in dependency_order(module_dependencies, list(module_dependencies.keys()), [], 0).values():
for step in dependency_order(
module_dependencies, list(module_dependencies.keys()), [], 0
).values():
sorted_libraries.extend(step)
return sorted_libraries
@@ -433,7 +475,8 @@ def load_modules():
# Register Ptr<> as a smart pointer
import libcppyy
libcppyy.AddSmartPtrType('Ptr')
libcppyy.AddSmartPtrType("Ptr")
# Import ns-3 libraries
for variant in ["lib", "lib64"]:
@@ -471,7 +514,8 @@ def load_modules():
setattr(cppyy.gbl.ns3, module.replace("-", "_"), cppyy.gbl.ns3)
# Set up a few tricks
cppyy.cppdef("""
cppyy.cppdef(
"""
using namespace ns3;
bool Time_ge(Time& a, Time& b){ return a >= b;}
bool Time_eq(Time& a, Time& b){ return a == b;}
@@ -479,7 +523,8 @@ def load_modules():
bool Time_le(Time& a, Time& b){ return a <= b;}
bool Time_gt(Time& a, Time& b){ return a > b;}
bool Time_lt(Time& a, Time& b){ return a < b;}
""")
"""
)
cppyy.gbl.ns3.Time.__ge__ = cppyy.gbl.Time_ge
cppyy.gbl.ns3.Time.__eq__ = cppyy.gbl.Time_eq
cppyy.gbl.ns3.Time.__ne__ = cppyy.gbl.Time_ne
@@ -500,7 +545,8 @@ def load_modules():
cppyy.gbl.ns3.Node.__del__ = Node_del
cppyy.cppdef("""
cppyy.cppdef(
"""
using namespace ns3;
std::tuple<bool, TypeId> LookupByNameFailSafe(std::string name)
{
@@ -508,44 +554,49 @@ def load_modules():
bool ok = TypeId::LookupByNameFailSafe(name, &id);
return std::make_tuple(ok, id);
}
""")
"""
)
setattr(cppyy.gbl.ns3, "LookupByNameFailSafe", cppyy.gbl.LookupByNameFailSafe)
def CreateObject(className):
try:
try:
func = "CreateObject%s" % re.sub('[<|>]', '_', className)
func = "CreateObject%s" % re.sub("[<|>]", "_", className)
return getattr(cppyy.gbl, func)()
except AttributeError:
pass
try:
func = "Create%s" % re.sub('[<|>]', '_', className)
func = "Create%s" % re.sub("[<|>]", "_", className)
return getattr(cppyy.gbl, func)()
except AttributeError:
pass
raise AttributeError
except AttributeError:
try:
func = "CreateObject%s" % re.sub('[<|>]', '_', className)
cppyy.cppdef("""
func = "CreateObject%s" % re.sub("[<|>]", "_", className)
cppyy.cppdef(
"""
using namespace ns3;
Ptr<%s> %s(){
Ptr<%s> object = CreateObject<%s>();
return object;
}
""" % (className, func, className, className)
)
"""
% (className, func, className, className)
)
except Exception as e:
try:
func = "Create%s" % re.sub('[<|>]', '_', className)
cppyy.cppdef("""
func = "Create%s" % re.sub("[<|>]", "_", className)
cppyy.cppdef(
"""
using namespace ns3;
%s %s(){
%s object = %s();
return object;
}
""" % (className, func, className, className)
)
"""
% (className, func, className, className)
)
except Exception as e:
exit(-1)
return getattr(cppyy.gbl, func)()
@@ -574,10 +625,12 @@ def load_modules():
{
return parentPtr->GetObject<%s>();
}
""" % (aggregatedType, aggregatedType, aggregatedType, aggregatedType)
"""
% (aggregatedType, aggregatedType, aggregatedType, aggregatedType)
)
return cppyy.gbl.getAggregatedObject(
parentObject, aggregatedObject if aggregatedIsClass else aggregatedObject.__class__
)
return cppyy.gbl.getAggregatedObject(parentObject,
aggregatedObject if aggregatedIsClass else aggregatedObject.__class__)
setattr(cppyy.gbl.ns3, "GetObject", GetObject)
return cppyy.gbl.ns3
@@ -585,4 +638,4 @@ def load_modules():
# Load all modules and make them available via a built-in
ns = load_modules() # can be imported via 'from ns import ns'
builtins.__dict__['ns'] = ns # or be made widely available with 'from ns import *'
builtins.__dict__["ns"] = ns # or be made widely available with 'from ns import *'