Skip to content

feat: parse requirements.txt with locally referenced packages #284

Closed

Description

@Jonas-vdb

Version
cyclonedx-bom==1.5.3

Executed command
cyclonedx-py -r -o bom.xml

Use case
Our requirements.txt file references other local python packages. Example of requirements file:

../local-python-package1
../local-python-package2
../local-python-package3

package1==0.0.0
package2==0.0.0
package3==0.0.0

Exception

Traceback (most recent call last):
  File "/home/jvdb/repos/venv-tmp/lib/python3.6/site-packages/pkg_resources/_vendor/packaging/requirements.py", line 98, in __init__
    req = REQUIREMENT.parseString(requirement_string)
  File "/home/jvdb/repos/venv-tmp/lib/python3.6/site-packages/pkg_resources/_vendor/pyparsing.py", line 1654, in parseString
    raise exc
  File "/home/jvdb/repos/venv-tmp/lib/python3.6/site-packages/pkg_resources/_vendor/pyparsing.py", line 1644, in parseString
    loc, tokens = self._parse( instring, 0 )
  File "/home/jvdb/repos/venv-tmp/lib/python3.6/site-packages/pkg_resources/_vendor/pyparsing.py", line 1402, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/home/jvdb/repos/venv-tmp/lib/python3.6/site-packages/pkg_resources/_vendor/pyparsing.py", line 3417, in parseImpl
    loc, exprtokens = e._parse( instring, loc, doActions )
  File "/home/jvdb/repos/venv-tmp/lib/python3.6/site-packages/pkg_resources/_vendor/pyparsing.py", line 1402, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/home/jvdb/repos/venv-tmp/lib/python3.6/site-packages/pkg_resources/_vendor/pyparsing.py", line 3739, in parseImpl
    return self.expr._parse( instring, loc, doActions, callPreParse=False )
  File "/home/jvdb/repos/venv-tmp/lib/python3.6/site-packages/pkg_resources/_vendor/pyparsing.py", line 1402, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/home/jvdb/repos/venv-tmp/lib/python3.6/site-packages/pkg_resources/_vendor/pyparsing.py", line 3400, in parseImpl
    loc, resultlist = self.exprs[0]._parse( instring, loc, doActions, callPreParse=False )
  File "/home/jvdb/repos/venv-tmp/lib/python3.6/site-packages/pkg_resources/_vendor/pyparsing.py", line 1406, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/home/jvdb/repos/venv-tmp/lib/python3.6/site-packages/pkg_resources/_vendor/pyparsing.py", line 2711, in parseImpl
    raise ParseException(instring, loc, self.errmsg, self)
pkg_resources._vendor.pyparsing.ParseException: Expected W:(abcd...) (at char 0), (line:1, col:1)

Activity

  1. madpah commented on Dec 15, 2021

    @madpah
    Collaborator

    Thanks for the report @Jonas-vdb - we'll look into addressing this in the child library cyclonedx-python-lib.

  2. madpah commented on Dec 15, 2021

    @madpah
    Collaborator

    @Jonas-vdb - after a quick review, it appears that the root cause is an internal package/method in Python that does not support parsing requirements.txt file lines that reference local packages.

    We see two options, and we are keen to get your views based on your use case:

    1. We can skip/ignore any locally referenced packages in requirements.txt files. This would be valid in once sense as we would not know the package version (given your example above), and version is a mandatory field for defining a Component in a CyloneDX BOM
    2. Depending on your workflow/use case, could you not generate your CycloneDX BOM after you have installed your Python Application into a temporary Python Environment? If so, you could use the EnvironmentParser functionality which would:
      1. Not suffer from the same parse error
      2. The resulting CycloneDX BOM would also include your referenced local packages (i.e a more complete and accurate picture than the first option)

    Appreciate your feedback, thanks.

    FYI: @jkowalleck

  3. Jonas-vdb commented on Dec 15, 2021

    @Jonas-vdb
    Author

    Thank you for your quick review.

    Our current workaround is to remove those packages programmatically and append the content of the requirement files of the linked packages.

    I do agree that generating the CycloneDX BOM file from the installed application is the better option. Even though the workaround works at this point, we will probably update our implementation to use the EnvironmentParser.

  4. jkowalleck commented on Dec 20, 2021

    @jkowalleck
    Member

    the requirements-parser in use in this project is not the same pip install -r uses.
    For a simple reason: this project is not pip. but it supports the output of pip freeze - see https://github.com/CycloneDX/cyclonedx-python/#requirements

    👍 I welcome your remark very much, @Jonas-vdb : #284 (comment)

    I do agree that generating the CycloneDX BOM file from the installed application is the better option. Even though the workaround works at this point, we will probably update our implementation to use the EnvironmentParser.

    Just as the README of this project states:

    • Use --environment will find all installed/shipped packages and is most likely the best solution for most users.
    • Every other option is a filter/modification of the actual SBoM and you need to use it as described in the readme.
      and regarding requirements-file it clearly described to use pip freeze

    that being said, you could help understand your environment better:
    Does the --environment switch work for you, @Jonas-vdb ? Does it find your local packages properly and add them to the SBoM ?

  5. AnalogJ commented on Jan 29, 2022

    @AnalogJ

    fyi, this is the same error that occurs when the requirements.txt file contains -e .

  6. changed the title [-]Requirements file referencing local packages[/-] [+]feat: parse `requirements.txt` with locally referenced packages[/+] on Feb 23, 2022
  7. added
    enhancement
    on Feb 23, 2022
  8. jkowalleck commented on Feb 23, 2022

    @jkowalleck
    Member

    for discussions, feel free to switch to #319

  9. linked a pull request that will close this issuefeat: Change requirements parser #327on Mar 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions