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
36 changes: 24 additions & 12 deletions cve_bin_tool/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,41 @@

# Python 2 compatibility stuff
from __future__ import print_function
import sys

import argparse
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

try:
from egg_updater import update_egg
Niraj-Kamdar marked this conversation as resolved.
Show resolved Hide resolved

DEVELOP = True
except ModuleNotFoundError:
DEVELOP = False


class InvalidFileError(Exception):
""" Filepath is invalid for scanning."""
Expand All @@ -55,6 +62,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 DEVELOP:
Niraj-Kamdar marked this conversation as resolved.
Show resolved Hide resolved
logger.info("Updating egg_info")
update_egg()
Niraj-Kamdar marked this conversation as resolved.
Show resolved Hide resolved

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

from setuptools.dist import Distribution


def update_egg():
with StringIO() as f:
sys.stdout = f
dist = Distribution(
Copy link
Member

@pdxjohnny pdxjohnny May 16, 2020

Choose a reason for hiding this comment

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

We should probably do a try: os.chdir to os.path.join(os.path.dirname(__file__), "..") and then a finally: os.chdir(wherever_os.getcwd()_reported_before_first_chdir). Since I'd guess that this fails if the user is not in the root of the source tree

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes this will work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have tried it, it's not working

Copy link
Member

Choose a reason for hiding this comment

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

What's not working?

Copy link
Member

Choose a reason for hiding this comment

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

In the above example: os.path.join(os.path.dirname(__file__), "..") should be os.path.dirname(__file__)

Copy link
Contributor Author

@Niraj-Kamdar Niraj-Kamdar May 18, 2020

Choose a reason for hiding this comment

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

I have put egg_updater inside cve_bin_tool and save cwd by cwd = os.getcwd() and then changed diectory to os.chdir(os.path.join(os.path.dirname(__file__), "..")) and after execution of script, I have reverted back to the previous cwd but it is giving same error as we get without applying chdir hack.

(venv) C:\Users\Root\Documents\cve-bin-tool>python -m cve_bin_tool.cli build
cve_bin_tool - WARNING -
                          **********************************************
                          Warning: this utility was developed for Linux.
                          You may need to install additional utilities
                          to use it on other operating systems.
                          **********************************************

cve_bin_tool.CVEDB - INFO - Using cached CVE data (<24h old). Use -u now to update immediately.
cve_bin_tool.Scanner - INFO - Updating egg_info
C:\Users\Root\Documents\cve-bin-tool C:\Users\Root\Documents\cve-bin-tool\cve_bin_tool
Traceback (most recent call last):
  File "C:\Users\Root\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Users\Root\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\Root\Documents\cve-bin-tool\cve_bin_tool\cli.py", line 587, in <module>
    sys.exit(main())
  File "C:\Users\Root\Documents\cve-bin-tool\cve_bin_tool\cli.py", line 471, in main
    scanner = Scanner(cvedb)
  File "C:\Users\Root\Documents\cve-bin-tool\cve_bin_tool\cli.py", line 72, in __init__
    egg_updater.update_egg()
  File "C:\Users\Root\Documents\cve-bin-tool\cve_bin_tool\egg_updater.py", line 35, in update_egg
    "checkers",
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\Root\\Documents\\cve-bin-tool\\cve_bin_tool\\cve_bin_tool\\c
heckers'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's solved.

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__)),
"cve_bin_tool",
"checkers",
)
)
if filename.endswith(".py") and "__init__" not in filename
],
},
)
)
dist.parse_command_line()
dist.run_commands()
sys.stdout = sys.__stdout__


if __name__ == "__main__":
update_egg()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"Programming Language :: Python :: Implementation :: PyPy",
],
install_requires=["jsonschema>=3.0.2", "pytest"],
packages=find_packages(),
packages=find_packages(exclude=["egg_updater.py"]),
entry_points={
"console_scripts": [
"cve-bin-tool = cve_bin_tool.cli:main",
Expand Down