Show the list of all existing contribs. :param name: filter to search the contribs :param ret: whether the function should return a dict instead of printing it :returns: None or a dictionary containing the results if ret=True
(name=None, # type: Optional[str]
ret=False, # type: bool
_debug=False # type: bool
)
| 370 | |
| 371 | |
| 372 | def list_contrib(name=None, # type: Optional[str] |
| 373 | ret=False, # type: bool |
| 374 | _debug=False # type: bool |
| 375 | ): |
| 376 | # type: (...) -> Optional[List[Dict[str, str]]] |
| 377 | """Show the list of all existing contribs. |
| 378 | |
| 379 | :param name: filter to search the contribs |
| 380 | :param ret: whether the function should return a dict instead of |
| 381 | printing it |
| 382 | :returns: None or a dictionary containing the results if ret=True |
| 383 | """ |
| 384 | # _debug: checks that all contrib modules have correctly defined: |
| 385 | # # scapy.contrib.description = [...] |
| 386 | # # scapy.contrib.status = [...] |
| 387 | # # scapy.contrib.name = [...] (optional) |
| 388 | # or set the flag: |
| 389 | # # scapy.contrib.description = skip |
| 390 | # to skip the file |
| 391 | if name is None: |
| 392 | name = "*.py" |
| 393 | elif "*" not in name and "?" not in name and not name.endswith(".py"): |
| 394 | name += ".py" |
| 395 | results = [] # type: List[Dict[str, str]] |
| 396 | dir_path = os.path.join(os.path.dirname(__file__), "contrib") |
| 397 | if sys.version_info >= (3, 5): |
| 398 | name = os.path.join(dir_path, "**", name) |
| 399 | iterator = glob.iglob(name, recursive=True) |
| 400 | else: |
| 401 | name = os.path.join(dir_path, name) |
| 402 | iterator = glob.iglob(name) |
| 403 | for f in iterator: |
| 404 | mod = f.replace(os.path.sep, ".").partition("contrib.")[2] |
| 405 | if mod.startswith("__"): |
| 406 | continue |
| 407 | if mod.endswith(".py"): |
| 408 | mod = mod[:-3] |
| 409 | desc = {"description": "", "status": "", "name": mod} |
| 410 | with io.open(f, errors="replace") as fd: |
| 411 | for line in fd: |
| 412 | if line[0] != "#": |
| 413 | continue |
| 414 | p = line.find("scapy.contrib.") |
| 415 | if p >= 0: |
| 416 | p += 14 |
| 417 | q = line.find("=", p) |
| 418 | key = line[p:q].strip() |
| 419 | value = line[q + 1:].strip() |
| 420 | desc[key] = value |
| 421 | if desc["status"] == "skip": |
| 422 | break |
| 423 | if desc["description"] and desc["status"]: |
| 424 | results.append(desc) |
| 425 | break |
| 426 | if _debug: |
| 427 | if desc["status"] == "skip": |
| 428 | pass |
| 429 | elif not desc["description"] or not desc["status"]: |