Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add egg updater script which fixes pytest initialization issue #672

Merged
merged 14 commits into from
May 19, 2020
40 changes: 28 additions & 12 deletions cve_bin_tool/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,46 @@

# Python 2 compatibility stuff
from __future__ import print_function
import sys

import argparse
import importlib.util
import logging
import multiprocessing
import os
import csv
import platform
import subprocess
import logging
import argparse
import sys
import textwrap
import threading
import pkg_resources
import multiprocessing
from collections import defaultdict

from .version import VERSION
from .util import DirWalk, inpath
from .extractor import Extractor
from .strings import Strings
from .file import is_binary
from .OutputEngine import OutputEngine
import pkg_resources

from .OutputEngine import OutputEngine
from .cvedb import CVEDB, OLD_CACHE_DIR
from .extractor import Extractor
from .file import is_binary
from .log import LOGGER
from .strings import Strings
from .util import DirWalk, inpath
from .version import VERSION

try:
import queue
except ImportError:
import Queue as queue


egg_updater_path = os.path.join(os.path.dirname(__file__), "egg_updater.py")
if os.path.isfile(egg_updater_path):
spec = importlib.util.spec_from_file_location("egg_updater", egg_updater_path)
egg_updater = importlib.util.module_from_spec(spec)
spec.loader.exec_module(egg_updater)
else:
egg_updater = None
DEVELOP = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block isn't necessary now that we've moved the module into the library, we can import with from . import egg_updater

Copy link
Contributor Author

@Niraj-Kamdar Niraj-Kamdar May 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have done it.



class InvalidFileError(Exception):
""" Filepath is invalid for scanning."""

Expand All @@ -55,6 +66,11 @@ class Scanner(object):
def __init__(self, cvedb, checkers=None, logger=None):
if logger is None:
logger = LOGGER.getChild(self.__class__.__name__)
# Update egg if installed in development mode
if egg_updater and egg_updater.IS_DEVELOP():
logger.info("Updating egg_info")
egg_updater.update_egg()

# Load checkers if not given
if checkers is None:
checkers = self.load_checkers()
Expand Down
63 changes: 63 additions & 0 deletions cve_bin_tool/egg_updater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import os
import sys
from io import StringIO

from setuptools.dist import Distribution


def IS_DEVELOP():
return any(
list(
map(
os.path.isfile,
list(
map(
lambda syspath: os.path.join(syspath, "cve-bin-tool.egg-link"),
sys.path,
)
),
)
)
)


def update_egg():
with StringIO() as f:
cwd = os.getcwd()
os.chdir(os.path.join(os.path.dirname(__file__), ".."))
sys.stdout = f
dist = Distribution(
dict(
script_name="setup.py",
script_args=["egg_info"],
name="cve-bin-tool",
entry_points={
"cve_bin_tool.checker": [
"{} = cve_bin_tool.checkers.{}:{}".format(
filename.replace(".py", ""),
filename.replace(".py", ""),
"".join(
(filename.replace(".py", "") + " checker")
.replace("_", " ")
.title()
.split()
),
)
for filename in os.listdir(
os.path.join(
os.path.abspath(os.path.dirname(__file__)), "checkers",
)
)
if filename.endswith(".py") and "__init__" not in filename
],
},
)
)
dist.parse_command_line()
dist.run_commands()
sys.stdout = sys.__stdout__
os.chdir(cwd)


if __name__ == "__main__":
update_egg()