Skip to content

Commit

Permalink
For simplicity define discovery methods as static
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Sep 17, 2022
1 parent 7888e31 commit c0cee6a
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/flake8_requirements/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,18 +391,19 @@ def parse_options(cls, options):
cls.requirements_file = options.requirements_file
cls.requirements_max_depth = options.requirements_max_depth
if options.scan_host_site_packages:
cls.discover_host_3rd_party_modules()
cls.discover_project_root_dir()
cls.known_host_3rd_parties = cls.discover_host_3rd_party_modules()
cls.root_dir = cls.discover_project_root_dir(os.getcwd())

@classmethod
def discover_host_3rd_party_modules(cls):
@staticmethod
def discover_host_3rd_party_modules():
"""Scan host site-packages for 3rd party modules."""
try:
site_packages_dirs = site.getsitepackagess()
site_packages_dirs.append(site.getusersitepackages())
except AttributeError as e:
LOG.error("Couldn't get site packages: %s", e)
return
mapping = {}
for site_dir in site_packages_dirs:
try:
dir_entries = os.listdir(site_dir)
Expand All @@ -421,20 +422,20 @@ def discover_host_3rd_party_modules(cls):
), "")
with open(modules_path) as f:
modules = list(yield_lines(f.readlines()))
cls.known_host_3rd_parties[project2module(name)] = modules
mapping[project2module(name)] = modules
return mapping

@classmethod
def discover_project_root_dir(cls):
"""Discover project's root directory."""
root_dir = os.getcwd()
@staticmethod
def discover_project_root_dir(path):
"""Discover project's root directory starting from given path."""
root_files = ["pyproject.toml", "requirements.txt", "setup.py"]
while root_dir != os.path.abspath(os.sep):
paths = [os.path.join(root_dir, x) for x in root_files]
while path != os.path.abspath(os.sep):
paths = [os.path.join(path, x) for x in root_files]
if any(map(os.path.exists, paths)):
LOG.info("Discovered root directory: %s", root_dir)
cls.root_dir = root_dir
break
root_dir = os.path.abspath(os.path.join(root_dir, ".."))
LOG.info("Discovered root directory: %s", path)
return path
path = os.path.abspath(os.path.join(path, ".."))
return ""

_requirement_match_option = re.compile(
r"(-[\w-]+)(.*)").match
Expand Down

0 comments on commit c0cee6a

Please sign in to comment.