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

feat: Allow filtering for EnvironmentParser #447

Closed
wants to merge 8 commits into from
Closed

feat: Allow filtering for EnvironmentParser #447

wants to merge 8 commits into from

Conversation

tmeckel
Copy link

@tmeckel tmeckel commented Nov 30, 2022

This PR contains changes to the EnvironmentParser to allow the filtering of packages read from pkg_resources.working_set.

The PR overlaps with #444

As requested from CONTRIBUTING.md:

  • commits are signed
  • documentation added
  • formatted with isort and autopep8
  • added new tests for implemented functionality
  • run tests locally successful with poetry run tox

@tmeckel tmeckel requested a review from a team as a code owner November 30, 2022 11:47
* added optional parameter location_filter to __init__ to allow filtering of packages by their installation location
* added optional parameter requirements_content to __init__ to allow filtering of packages by their package names read from a requirements file
* added filter expression for location_filter and requirements_content

Signed-off-by: Thomas Meckel <tmeckel@users.noreply.github.com>
* added test_simple_use_location_filter
* added test_simple_use_requiremets

Signed-off-by: Thomas Meckel <tmeckel@users.noreply.github.com>
* added argument --location-filter to input_method_group
* changed initialization for environment parser based on new filter arguments

Signed-off-by: Thomas Meckel <tmeckel@users.noreply.github.com>
* added missing single quotes to remove WARNING: Could not lex literal_block as "bash"
* added documentation for new filter arguments for environment parser

Signed-off-by: Thomas Meckel <tmeckel@users.noreply.github.com>
tests/test_parser_environment.py Show resolved Hide resolved
tests/test_parser_environment.py Show resolved Hide resolved
Use with -i to specify absolute path to a
`requirements.txt` you wish to use, else we'll look
`requirements.txt` you wish to use, else we''ll look
Copy link
Member

Choose a reason for hiding this comment

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

note for myself -> need to check if this is correctly rendered to RTD

@@ -206,6 +206,13 @@ def get_arg_parser(*, prog: Optional[str] = None) -> argparse.ArgumentParser:
default=None,
help='File to read input from. Use "-" to read from STDIN.', dest='input_source', required=False
)
input_method_group.add_argument(
'--location-filter', action='store',
default=None, nargs='+',
Copy link
Member

Choose a reason for hiding this comment

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

note to self: double-check some edge-cases

@jkowalleck
Copy link
Member

@tmeckel could you add the new CLI option to the https://github.com/CycloneDX/cyclonedx-python#basic-usage ?

@tmeckel
Copy link
Author

tmeckel commented Dec 1, 2022

@jkowalleck I updated the usage.rst in starting at lines 34 and 62. Do you want me to add some more documentation?
Sorry, it's the README.mdmy oversight. I'll add this today.

@tmeckel
Copy link
Author

tmeckel commented Dec 1, 2022

@jkowalleck will try to fix the failing test on Windows test_parser_environment.py:72 as well.

Traceback (most recent call last):
  File "D:\a\cyclonedx-python\cyclonedx-python\tests\test_parser_environment.py", line 72, in test_simple_use_location_filter
    self.assertGreater(parser_sys_path.component_count(), 1)
AssertionError: 0 not greater than 1

… README.md

Signed-off-by: Thomas Meckel <tmeckel@users.noreply.github.com>
Signed-off-by: Thomas Meckel <tmeckel@users.noreply.github.com>
…r location_filter to lowercase to match location properties from pkg_resources.working_set

Signed-off-by: Thomas Meckel <tmeckel@users.noreply.github.com>
@tmeckel
Copy link
Author

tmeckel commented Dec 1, 2022

@jkowalleck I pushed changes to add the requested additions to basic usage in README.md and fixed (hopefully) the failing test on Windows.


rf_names = set([req.name for req in parsed_rf.requirements])

if os.name == 'nt' and location_filter:
Copy link
Author

Choose a reason for hiding this comment

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

@jkowalleck had to add this code to get the location filter running on Windows. sys.path elements are Camel-Case on Windows. Location properties from pkg_resources.working_set are all lower case.

Copy link
Member

@jkowalleck jkowalleck Dec 1, 2022

Choose a reason for hiding this comment

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

note to self: need to double-check on that

@a1lu
Copy link
Contributor

a1lu commented Dec 21, 2022

Any progress on this? I would like to see this feature.

@jkowalleck
Copy link
Member

To move this forward, some git conflicts need to be sorted out first.
@tmeckel, could you look into this?

input_data_fh = self._arguments.input_source
with input_data_fh:
input_data = input_data_fh.read()
input_data_fh.close()
Copy link
Member

Choose a reason for hiding this comment

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


input_data_fh.close() not needed, as it will happen automatically on exit of the with block

@@ -251,7 +258,14 @@ def _error_and_exit(message: str, exit_code: int = 1) -> None:

def _get_input_parser(self) -> BaseParser:
if self._arguments.input_from_environment:
return EnvironmentParser(use_purl_bom_ref=self._arguments.use_purl_bom_ref)
input_data_fh = self._arguments.input_source
with input_data_fh:
Copy link
Member

Choose a reason for hiding this comment

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

what if input_source is None? will the with block crash?

@jkowalleck
Copy link
Member

jkowalleck commented Dec 22, 2022

@tmeckel @a1lu before putting more effort in this PR,
please read #435 (comment)

You will find good reasons why I will probably not merge this PR.
I see your solution working for your current use cases, but it appears to me not wholesome enough.
Therefore, i (temporary) closed this PR.

@jkowalleck jkowalleck closed this Dec 22, 2022
@tmeckel
Copy link
Author

tmeckel commented Dec 22, 2022

@jkowalleck I'm totally fine with your decision because in the meantime I fixed the issue that lead to this PR using a different approach.

@tmeckel tmeckel deleted the feature/environment-filters branch December 22, 2022 10:27
@jkowalleck
Copy link
Member

@tmeckel re: #447 (comment)
I am happy to hear.

Would you be so kind and open a new Q&A in https://github.com/CycloneDX/cyclonedx-python/discussions/categories/q-a
where you describe the problem and answer your own "Question" with the solution you found?

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.

None yet

3 participants