Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion fetchcode/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def get_cargo_data_from_purl(purl):
)
versions = response.get("versions", [])
for version in versions:
version_purl = PackageURL(type=purl.type, name=name, version=version.get("num"))
version_purl = PackageURL(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please don't change these lines :), they are out of scope of this PR

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Actually I have a setting of Format on save on my vscode, it happened unknowingly, will take care in the future. Thanks for the feedback.

type=purl.type, name=name, version=version.get("num"))
dl_path = version.get("dl_path")
if dl_path:
download_url = f"{base_url}/{dl_path}"
Expand Down Expand Up @@ -327,3 +328,26 @@ def get_rubygems_data_from_purl(purl):
download_url=download_url,
**purl.to_dict(),
)


@router.route("pkg:brew/.*")
def get_homebrew_data_from_purl(purl):
Comment thread
pombredanne marked this conversation as resolved.
"""
Generate `Package` object from the `purl` string of brew type
"""
purl = PackageURL.from_string(purl)
name = purl.name
base_path = "https://formulae.brew.sh/api/formula"
api_url = f"{base_path}/{name}.json"
response = get_response(api_url)
declared_license = response.get("license") or None

@TG1999 TG1999 Oct 12, 2020

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Don't use or None

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

will change it.

homepage_url = response.get("homepage")
version = response.get("versions") or None

@TG1999 TG1999 Oct 12, 2020

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Don't use or None

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

will change it.

download_url = response.get("urls")["stable"]["url"]

@TG1999 TG1999 Oct 12, 2020

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What if response does not have "urls" ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Should I add try and catch or would you suggest something else?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The documentation of the homebrew json api tells that the download URL will be present inside a stable of URLs, refer here. In the docs, as they have mentioned, if we are using the base path as https://formulae.brew.sh/api/formula.json, then the download URL will be present inside the stable of urls, so I thought of leaving the download URL as such. Please do suggest what should I add it to cover the edge cases, so that it does not fail.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Look how these things are done in the above block of code

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ok, will look into it. Thanks for the feedback, will do the changes by tomorrow.

yield Package(
homepage_url=homepage_url,
api_url=api_url,
declared_license=declared_license,
download_url=download_url,
**purl.to_dict(),
)
38 changes: 38 additions & 0 deletions tests/data/homebrew.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"0": {
"type": "brew",
"namespace": null,
"name": "node",
"version": null,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should collect version data; it seems like there is a bug in your code, as I see you attempt to collect this version data in package.py (on line 344-345)

@zaheerabbas21 zaheerabbas21 Oct 17, 2020

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@MaJuRG, Thanks for pointing out the bug, I know this is supposed to be a good first issue, but I am really new to the development side, I appreciate all the help.

But I am stuck here, as you said I changed the version in the tests/data/homebrew.json file. But I do not know how to pass the versions to the Package to yield it, I tried the key version, but did not work. So please tell me how to pass the version variable to the package.

Here is the diff of the test when I ran python -m pytest -vv

 Full diff:
E           [
E            {'api_data_url': None,
E             'api_url': 'https://formulae.brew.sh/api/formula/node.json',
E             'bug_tracking_url': None,
E             'code_view_url': None,
E             'contains_source_code': None,
E             'copyright': None,
E             'declared_license': 'MIT',
E             'dependencies': [],
E             'description': None,
E             'download_url': 'https://nodejs.org/dist/v14.13.1/node-v14.13.1.tar.gz',
E             'homepage_url': 'https://nodejs.org/',
E             'keywords': [],
E             'license_expression': None,
E             'md5': None,
E             'name': 'node',
E             'namespace': None,
E             'notice_text': None,
E             'parties': [],
E             'primary_language': None,
E             'purl': 'pkg:homebrew/node',
E         -   'qualifiers': {},
E         +   'qualifiers': OrderedDict(),
E             'release_date': None,
E             'repository_download_url': None,
E             'repository_homepage_url': None,
E             'root_path': None,
E             'sha1': None,
E             'sha256': None,
E             'sha512': None,
E             'size': None,
E             'source_packages': [],
E             'subpath': None,
E             'type': 'homebrew',
E             'vcs_url': None,
E         -   'version': '14.4.0'},
E         +   'version': None},
E           ]

I also tried to check the above code and see if I could implement it in a way it is implemented above, but did not understand that, I am really sorry for this.
I am really sorry to disturb you, appreciate all the help. Thank you

"qualifiers": {},
"subpath": null,
"primary_language": null,
"description": null,
"release_date": null,
"parties": [],
"keywords": [],
"homepage_url": "https://nodejs.org/",
"download_url": "https://nodejs.org/dist/v14.13.1/node-v14.13.1.tar.gz",
"api_url": "https://formulae.brew.sh/api/formula/node.json",
"size": null,
"sha1": null,
"md5": null,
"sha256": null,
"sha512": null,
"bug_tracking_url": null,
"code_view_url": null,
"vcs_url": null,
"copyright": null,
"license_expression": null,
"declared_license": "MIT",
"notice_text": null,
"root_path": null,
"dependencies": [],
"contains_source_code": null,
"source_packages": [],
"purl": "pkg:brew/node",
"repository_homepage_url": null,
"repository_download_url": null,
"api_data_url": null
}
}
124 changes: 124 additions & 0 deletions tests/data/homebrew_mock_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
{
"name": "node",
"full_name": "node",
"oldname": null,
"aliases": [
"node.js",
"node@14",
"nodejs",
"npm"
],
"versioned_formulae": [
"node@12",
"node@10"
],
"desc": "Platform built on V8 to build network applications",
"license": "MIT",
"homepage": "https://nodejs.org/",
"versions": {
"stable": "14.13.1",
"head": "HEAD",
"bottle": true
},
"urls": {
"stable": {
"url": "https://nodejs.org/dist/v14.13.1/node-v14.13.1.tar.gz",
"tag": null,
"revision": null
}
},
"revision": 0,
"version_scheme": 0,
"bottle": {
"stable": {
"rebuild": 0,
"cellar": ":any",
"prefix": "/usr/local",
"root_url": "https://homebrew.bintray.com/bottles",
"files": {
"catalina": {
"url": "https://homebrew.bintray.com/bottles/node-14.13.1.catalina.bottle.tar.gz",
"sha256": "c5574d926c10ecb8a18faa551f6a15c362512134577110c2678d52db3382ba42"
},
"mojave": {
"url": "https://homebrew.bintray.com/bottles/node-14.13.1.mojave.bottle.tar.gz",
"sha256": "3d42a543356788cb20a3cd91306e9e533c2939b7609c3dc91ffdd405fab1b25f"
},
"high_sierra": {
"url": "https://homebrew.bintray.com/bottles/node-14.13.1.high_sierra.bottle.tar.gz",
"sha256": "8c9d9dd8a341bd74aec4e8ba1a8abb8ba92e137d771290613676798f0b4a61ba"
}
}
}
},
"keg_only": false,
"bottle_disabled": false,
"options": [],
"build_dependencies": [
"pkg-config",
"python@3.8"
],
"dependencies": [
"icu4c"
],
"recommended_dependencies": [],
"optional_dependencies": [],
"uses_from_macos": [],
"requirements": [],
"conflicts_with": [],
"caveats": null,
"installed": [],
"linked_keg": null,
"pinned": false,
"outdated": false,
"deprecated": false,
"disabled": false,
"analytics": {
"install": {
"30d": {
"node": 405637,
"node --HEAD": 44,
"node --with-full-icu": 1,
"node --without-npm": 1
},
"90d": {
"node": 1074821,
"node --HEAD": 117,
"node --without-npm": 3,
"Node": 1,
"node --with-full-icu": 1
},
"365d": {
"node": 4314220,
"node --HEAD": 617,
"node --without-npm": 29,
"node --without-icu4c": 13,
"node --with-openssl@1.1": 6
}
},
"install_on_request": {
"30d": {
"node": 294250,
"node --HEAD": 15
},
"90d": {
"node": 787407,
"node --HEAD": 52,
"Node": 1
},
"365d": {
"node": 3134446,
"node --HEAD": 343,
"node --without-icu4c": 13,
"node --without-npm": 9,
"node --with-openssl@1.1": 6
}
},
"build_error": {
"30d": {
"node": 0
}
}
},
"generated_date": "2020-10-11"
}
10 changes: 10 additions & 0 deletions tests/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,13 @@ def test_rubygems_packages(mock_get):
mock_get.side_effect = side_effect
packages = list(info(purl))
match_data(packages, expected_data)


@mock.patch("fetchcode.package.get_response")
def test_homebrew_packages(mock_get):
side_effect = [file_data("tests/data/homebrew_mock_data.json")]
purl = "pkg:brew/node"
expected_data = file_data("tests/data/homebrew.json")
mock_get.side_effect = side_effect
packages = list(info(purl))
match_data(packages, expected_data)