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 -e --exclude arument for excluding path #876

Merged
merged 7 commits into from
Sep 2, 2020

Conversation

imsahil007
Copy link
Contributor

Closes #819
In reference to: #874
@Niraj-Kamdar

@codecov-commenter
Copy link

codecov-commenter commented Aug 18, 2020

Codecov Report

Merging #876 into master will increase coverage by 2.25%.
The diff coverage is 80.95%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #876      +/-   ##
==========================================
+ Coverage   84.70%   86.96%   +2.25%     
==========================================
  Files         167      167              
  Lines        2765     2784      +19     
  Branches      298      302       +4     
==========================================
+ Hits         2342     2421      +79     
+ Misses        344      292      -52     
+ Partials       79       71       -8     
Flag Coverage Δ
#longtests 86.96% <80.95%> (+2.25%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
test/test_config.py 0.00% <ø> (ø)
test/test_cli.py 96.00% <76.47%> (+15.54%) ⬆️
cve_bin_tool/cli.py 86.46% <100.00%> (+3.13%) ⬆️
cve_bin_tool/version_scanner.py 86.20% <100.00%> (+1.85%) ⬆️
test/test_extractor.py 97.80% <0.00%> (ø)
cve_bin_tool/cvedb.py 85.03% <0.00%> (+0.78%) ⬆️
cve_bin_tool/extractor.py 60.62% <0.00%> (+3.14%) ⬆️
test/test_scanner.py 91.01% <0.00%> (+11.23%) ⬆️
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 51696f0...b028714. Read the comment docs.

Copy link
Contributor

@terriko terriko left a comment

Choose a reason for hiding this comment

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

This looks like it's heading in the right direction, but I'd like to have some tests for it before we merge anything.

@imsahil007
Copy link
Contributor Author

This looks like it's heading in the right direction, but I'd like to have some tests for it before we merge anything.

What kind of tests does this argument require?

@Niraj-Kamdar
Copy link
Contributor

This looks like it's heading in the right direction, but I'd like to have some tests for it before we merge anything.

What kind of tests does this argument require?

You need to add tests in test_cli that make sure that excluded files or directories aren't being scanned but first You need to make it fnmatch compatible because users generally use Unix style pattern to exclude directory and files.

@@ -150,6 +150,9 @@ def walk(self, roots=None):
for i in filenames
if self.pattern_match(i, self.pattern)
and not self.pattern_match(i, self.file_exclude_pattern)
and not self.directory_pattern(
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we still using directory_pattern? Is fix mentioned by me in previous PR not working?

Copy link
Contributor Author

@imsahil007 imsahil007 Aug 19, 2020

Choose a reason for hiding this comment

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

Why are we still using directory_pattern? Is fix mentioned by me in the previous PR not working?

It wasn't working because there was not an * after the path which is to be excluded. It may contain any no. of the path. So, I thought it will be better just to match the string beginning. It won't work otherwise. fnmatch takes a regex type of input. If we were to exclude /home/temp/ in a directory /home/ .
We will have to append /home/temp/ with an *. So, that it will work for all the sub-dir as well as files inside that dir

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes we can easily append that by checking

if not string.endswith("*"):
    string += "*"

If you use fnmatch user can specify path like /home/temp/*.jpg this will only ignore jpg instead of whole directory. There are also other regex patterns supported by fnmatch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Niraj-Kamdar Check now

@imsahil007 imsahil007 force-pushed the exclude branch 3 times, most recently from 8475f2b to e4f6a44 Compare August 20, 2020 19:23
Copy link
Contributor

@Niraj-Kamdar Niraj-Kamdar left a comment

Choose a reason for hiding this comment

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

Overall it looks good just couple of small changes.

Comment on lines 151 to 152
if self.pattern_match(i, self.pattern)
and not self.pattern_match(i, self.file_exclude_pattern)
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's also replace i with os.path.join(dirpath, i) here and dirnames also because It was a bug in actual code.
It would be good if you rename this i variable to something useful like filename and dirname.

Comment on lines 304 to 313
if args["exclude"]:
version_scanner = VersionScanner(
should_extract=args["extract"],
exclude_folders=args["exclude"],
error_mode=error_mode,
)
else:
version_scanner = VersionScanner(
should_extract=args["extract"], error_mode=error_mode
)
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to add if...else... here, since by default it will be an empty list which is valid.

Comment on lines 44 to 49
self.exclude_folders = exclude_folders
self.walker = DirWalk(
folder_exclude_pattern="".join(
[exclude + "*;" for exclude in exclude_folders]
)
).walk
Copy link
Contributor

Choose a reason for hiding this comment

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

        self.exclude_folders = exclude_folders
        self.exclude_folder.append(".git")
        self.walker = DirWalk(
            folder_exclude_pattern=";".join(exclude if exclude.endswith("*") else exclude + "*" in exclude_folders)
        ).walk

Here we should include ".git" by default and we have to check if "*" doesn't exist in the end because if it exist there will be two stars at the end.

test/test_cli.py Outdated
""" Test that the exclude paths are not scanned """
test_path = os.path.abspath(os.path.dirname(__file__))
exclude_path = os.path.join(test_path, "assets/")
checkers = ["curl", "libcurl", "kerberos", "kerberos_5"]
Copy link
Contributor

Choose a reason for hiding this comment

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

You have to check that scanner isn't detecting any checker when we exclude a path.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can get list of all checkers from VersionScanner class

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did all the changes

Copy link
Contributor

Choose a reason for hiding this comment

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

Have you forgotten to push?

Comment on lines 162 to 163
if self.pattern_match(dirname, self.folder_include_pattern)
and not self.pattern_match(dirname, self.folder_exclude_pattern)
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to join path here also os.path.join(dirpath, dirname).

Copy link
Contributor

@Niraj-Kamdar Niraj-Kamdar left a comment

Choose a reason for hiding this comment

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

Awesome looks nice. Thanks for this again.

@imsahil007
Copy link
Contributor Author

Awesome looks nice. Thanks for this again.

Thank you! You have been very helpful and guiding.

@terriko
Copy link
Contributor

terriko commented Sep 2, 2020

Thank you! Sorry it took a little bit to get the final merge in.

@terriko terriko merged commit dec65fb into intel:master Sep 2, 2020
@imsahil007
Copy link
Contributor Author

Thank you! Sorry it took a little bit to get the final merge in.

Your welcome. It's okay, not an issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scanner: Add an option to exclude paths from scan
4 participants