Skip to content

Commit

Permalink
loader: avoid eager imports of some backend-related code
Browse files Browse the repository at this point in the history
  • Loading branch information
williballenthin committed Feb 14, 2024
1 parent 4e2f175 commit bd3cc18
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions capa/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
import capa.render.result_document
import capa.render.result_document as rdoc
import capa.features.extractors.common
import capa.features.extractors.pefile
import capa.features.extractors.elffile
import capa.features.extractors.dotnetfile
import capa.features.extractors.base_extractor
import capa.features.extractors.cape.extractor
from capa.rules import RuleSet
Expand Down Expand Up @@ -276,17 +273,26 @@ def get_extractor(
def get_file_extractors(input_file: Path, input_format: str) -> List[FeatureExtractor]:
file_extractors: List[FeatureExtractor] = []

# we use lazy importing here to avoid eagerly loading dependencies
# that some specialized environments may not have,
# e.g., those that run capa without vivisect.

if input_format == FORMAT_PE:
import capa.features.extractors.pefile
file_extractors.append(capa.features.extractors.pefile.PefileFeatureExtractor(input_file))

elif input_format == FORMAT_DOTNET:
import capa.features.extractors.pefile
import capa.features.extractors.dotnetfile
file_extractors.append(capa.features.extractors.pefile.PefileFeatureExtractor(input_file))
file_extractors.append(capa.features.extractors.dotnetfile.DotnetFileFeatureExtractor(input_file))

elif input_format == FORMAT_ELF:
import capa.features.extractors.elffile
file_extractors.append(capa.features.extractors.elffile.ElfFeatureExtractor(input_file))

elif input_format == FORMAT_CAPE:
import capa.features.extractors.cape.extractor
report = json.loads(input_file.read_text(encoding="utf-8"))
file_extractors.append(capa.features.extractors.cape.extractor.CapeExtractor.from_report(report))

Expand Down

0 comments on commit bd3cc18

Please sign in to comment.