Skip to content

[WIP] Add support for converting PURLS to URLS - #27

Closed
TG1999 wants to merge 1 commit into
aboutcode-org:masterfrom
TG1999:purl_url
Closed

[WIP] Add support for converting PURLS to URLS#27
TG1999 wants to merge 1 commit into
aboutcode-org:masterfrom
TG1999:purl_url

Conversation

@TG1999

@TG1999 TG1999 commented Jun 23, 2020

Copy link
Copy Markdown
Collaborator

Signed-off-by: TG1999 tushar.goel.dav@gmail.com

@TG1999 TG1999 changed the title Add support converting PURLS to URLS [WIP] Add support converting PURLS to URLS Jun 23, 2020
@TG1999 TG1999 changed the title [WIP] Add support converting PURLS to URLS [WIP] Add support for converting PURLS to URLS Jun 23, 2020
@TG1999

TG1999 commented Jun 25, 2020

Copy link
Copy Markdown
Collaborator Author

@pombredanne @MaJuRG please review :)

Comment thread fetchcode/purl2url/__init__.py Outdated

def build_bitbucket(name, namespace, version, qualifiers, subpath):

if name and namespace:

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.

Why not check the inverse of this and return right away? This would remove this big indented block.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

What do you mean by inverse :), please can you explain a little for me :D

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.

for example, you would do

if not name or not namespace:
    return

# rest of code at normal indentation level.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Ohhkay, thanks got it

Comment thread fetchcode/purl2url/__init__.py Outdated


def build_cargo(name, namespace, version, qualifiers, subpath):
if name and version:

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.

Why not check the inverse of this and return right away? This would remove this big indented block.

Comment thread fetchcode/purl2url/__init__.py Outdated

def build_github(name, namespace, version, qualifiers, subpath):

if name and namespace:

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.

Why not check the inverse of this and return right away? This would remove this big indented block.

Comment thread fetchcode/purl2url/__init__.py Outdated


def build_rubygems(name, namespace, version, qualifiers, subpath):
if name and version:

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.

Why not check the inverse of this and return right away? This would remove this big indented block.

Comment thread tests/test_purl2url.py
Comment on lines +22 to +29
"pkg:github/tg1999/fetchcode": "https://github.com/tg1999/fetchcode",
"pkg:github/tg1999/fetchcode@master": "https://github.com/tg1999/fetchcode/tree/master",
"pkg:github/tg1999/fetchcode@master#tests": "https://github.com/tg1999/fetchcode/tree/master/tests",
"pkg:github/tg1999": None,
"pkg:cargo/clap@2.3.3": "https://crates.io/api/v1/crates/clap/2.3.3/download",
"pkg:cargo/rand@0.7.2": "https://crates.io/api/v1/crates/rand/0.7.2/download",
"pkg:cargo/structopt@0.3.11": "https://crates.io/api/v1/crates/structopt/0.3.11/download",

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 probably want to have test cases that contain all possible combos of purl fields for these tests.

Such as purls with namespace, subpath, qualifiers etc

@steven-esser steven-esser left a comment

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.

@TG1999 This is looking very good. Just see a few nitpicking details I have left.

Comment thread fetchcode/purl2url/__init__.py Outdated
return url


def build_cargo(name, namespace, version, qualifiers, subpath):

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.

Why do we only use a subset of the arguments here to build a url?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Can you please explain a little bit, what should be done here, it will be helpful for me 😅

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.

This function accepts 5 different arguments, but you only use a subset of these to build the URL. You need to either:

  1. use all the args in some way
  2. remove extraneous args that aren't used

It is confusing otherwise

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Okay I am getting your point :), I am passing a kwarg from the purl2url function and unpacking it here, can you suggest me a better alternative, I was also finding some way for that, but not able to do so

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.

Just make the function accept kwargs. See: https://www.digitalocean.com/community/tutorials/how-to-use-args-and-kwargs-in-python-3

There are other resources out there as well.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Gotcha, thanks

Comment thread fetchcode/purl2url/__init__.py Outdated
)


def build_github(name, namespace, version, qualifiers, subpath):

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.

Why do we only use a subset of the arguments here to build a url?

Comment thread fetchcode/purl2url/__init__.py Outdated
return url


def build_gitlab(name, namespace, version, qualifiers, subpath):

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.

Why do we only use a subset of the arguments here to build a url?

Comment thread fetchcode/purl2url/__init__.py Outdated
return url


def build_gem(name, namespace, version, qualifiers, subpath):

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.

Why do we only use a subset of the arguments here to build a url?

subpath = url_parts.fragment if url_parts.fragment != "" else None
qualifiers = url_parts.query if url_parts.query != "" else None

path = url_parts.path

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.

Can we use a more descriptive name for path here? what kind of path is it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah sure, agreed

Comment thread fetchcode/purl2url/__init__.py Outdated
return url


def build_cargo(name, namespace, version, qualifiers, subpath):

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.

Can we add some descriptive docstring for this function

Comment thread fetchcode/purl2url/__init__.py Outdated
)


def build_github(name, namespace, version, qualifiers, subpath):

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.

Can we add some descriptive docstring for this function

Comment thread fetchcode/purl2url/__init__.py Outdated
return url


def build_gitlab(name, namespace, version, qualifiers, subpath):

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.

Can we add some descriptive docstring for this function

Comment thread fetchcode/purl2url/__init__.py Outdated
return url


def build_gem(name, namespace, version, qualifiers, subpath):

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.

Can we add some descriptive docstring for this function

)


def purl2url(purl):

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.

Can we add some descriptive docstring for this function

Signed-off-by: TG1999 <tushar.goel.dav@gmail.com>

@pombredanne pombredanne left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

  1. I think this should be in the packageurl library instead
  2. you are missing unit-level test for several functions.

"""

name = data["name"]
namespace = data["namespace"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please use single quotes throughout unless this is a docstring

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I ran black, due to which all these single quotes converted into double quotes, apologies for that 😅

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's great then. Stick to black and make it part of the CI and tests too.

from urllib.parse import urlparse


def index_in_list(index, list):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do not create a function for something so trivial, also in general using indexes on a list is often a sign that there must be a better way to do the work.


def build_bitbucket(data):
"""
Take dictionary `data` as input and returns a valid bitbucket URL string `url`

@pombredanne pombredanne Jul 1, 2020

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Use imperative style for docstrings.
Return a Bitbucket URL from a mapping of Package URL data

But there is a larger problem... why use a dict here rather than the packageurl library and object that has nice named attributes?
Also should this be code that lives in the packageurl library instead?

"""
Take PackageURL `purl` as input and return a valid URL string `url` depending on the type of purl
"""
url_parts = urlparse(purl)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why parsing a purl yourself? please use the packageurl library instead

name = path[1]

type = path[0]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

All the code above is parsing a purl which already exists in a tested and more robust form in the packaeurl library. Please use that library instead.

Comment thread tests/test_purl2url.py
from fetchcode.purl2url import purl2url


def test_convert_with_purls_string():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

purl2url should be part of the packageurl library IMHO

@TG1999 TG1999 closed this Jul 9, 2020
@pombredanne

Copy link
Copy Markdown
Member

@TG1999 why did you close this? can you explain?

@pombredanne pombredanne reopened this Jul 9, 2020
@TG1999

TG1999 commented Jul 9, 2020

Copy link
Copy Markdown
Collaborator Author

I have shifted it to packageul-python as suggested by you

@TG1999 TG1999 closed this Jul 28, 2020
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.

3 participants