Skip to content

Commit

Permalink
Add support for npm-registry purls
Browse files Browse the repository at this point in the history
  • Loading branch information
vBarbaros committed Nov 30, 2021
1 parent 0c1f6d8 commit ade514b
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 22 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Gitlab
* BitBucket
* PyPi
* npm

### Main concepts:

Expand Down Expand Up @@ -118,11 +119,11 @@ $ pip install -e .
$ python -m unittest tests/*.py
# Expected output
.......................................................................................................
----------------------------------------------------------------------
Ran 103 tests in 0.004s
...
Ran 138 tests in 0.009s
OK
```


Expand Down
45 changes: 37 additions & 8 deletions pypurl/purlutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ def set_type_namespace_name(host_list, purl_dict):
if i == 1 and purl_dict['type'] not in __get_types_no_namespaces():
purl_dict['namespace'] = host_list[i]
elif i == 1 and purl_dict['type'] in __get_types_no_namespaces():
purl_dict['namespace'] = ''
if len(host_list) == 3:
purl_dict['namespace'] = host_list[i]
else:
purl_dict['namespace'] = ''
purl_postname = host_list[i].split('@')
purl_dict['name'] = purl_postname[0]

Expand All @@ -69,7 +72,7 @@ def build_host_part(purl_dict):
if check_is_not_valid_type_namespace_name(purl_dict):
return ''

if purl_dict['type'] not in __get_types_no_namespaces():
if purl_dict['type'] not in __get_types_no_namespaces() or purl_dict['namespace'] != '':
purl = 'pkg:' + str(purl_dict['type']) + '/' + str(purl_dict['namespace']) + '/' + str(purl_dict['name'])
else:
purl = 'pkg:' + str(purl_dict['type']) + '/' + str(purl_dict['name'])
Expand Down Expand Up @@ -112,23 +115,31 @@ def parse_durl(download_url):
purl_dict = {}
purl_dict['scheme'] = 'pkg'

type_lst = durl_info_list[0].split('.')
if len(type_lst) < 2:
type_list = durl_info_list[0].split('.')
if len(type_list) < 2:
return {}
purl_dict['type'] = __type_from_host(type_list)
if not purl_dict['type']:
return {}
purl_dict['type'] = type_lst[0]

if durl_info_list[1] == '':
return {}

if purl_dict['type'] in __get_types_no_namespaces():
purl_dict['namespace'] = ''
if __is_npm_with_namespace(durl_info_list, purl_dict):
purl_dict['namespace'] = durl_info_list[2]
else:
purl_dict['namespace'] = ''
else:
purl_dict['namespace'] = durl_info_list[1]

if durl_info_list[2] == '':
return {}

purl_dict['name'] = durl_info_list[2]
if __is_npm_with_namespace(durl_info_list, purl_dict):
purl_dict['name'] = durl_info_list[3]
else:
purl_dict['name'] = durl_info_list[2]
return purl_dict


Expand Down Expand Up @@ -179,5 +190,23 @@ def build_purl_dict_from_params_optionals(purl_dict, version, qualifiers, subpat
return purl_dict


def __is_npm_with_namespace(durl_info_list, purl_dict):
return purl_dict['type'] == 'npm' and len(durl_info_list) == 6


def __get_types_no_namespaces():
return ['pypi']
return ['pypi', 'npm']

def __type_from_host(type_list):
if 'npmjs' in type_list:
return 'npm'
elif 'github' in type_list:
return 'github'
elif 'gitlab' in type_list:
return 'gitlab'
elif 'pypi' in type_list:
return 'pypi'
elif 'bitbucket' in type_list:
return 'bitbucket'
else:
return ''
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from setuptools import setup

setup(name='pypurl',
version='0.0.2',
version='v0.5.0',
python_requires='>=3.6',
description='Implementation of an easy way of building and handling the Package URLs.',
description='Implementation of an easy way to build and handle Package URLs.',
author='Victor Barbaros',
url='https://github.com/vBarbaros/pypurl',
)
6 changes: 5 additions & 1 deletion tests/test_bitbucket_purl.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import unittest
from pypurl.purl import Purl


class BitbucketPurlTestCase(unittest.TestCase):
def setUp(self):
self.purl_obj = Purl()
print('Running BitBucket-repository tests... OK')

def test_bitbucket_purl_to_dict_version_success(self):
purl_with_version = 'pkg:bitbucket/pypy/numpy@4f9778cd49a4'
Expand Down Expand Up @@ -153,7 +155,8 @@ def test_bitbucket_durl_to_purl_main_success(self):
self.assertEqual(r, p)

def test_bitbucket_params_to_purl_all_success(self):
r = self.purl_obj.params_to_purl('bitbucket', 'pypy', 'numpy', '4f9778cd49a4', 'param1=one,param2=two', '/src/main/sub-path')
r = self.purl_obj.params_to_purl('bitbucket', 'pypy', 'numpy', '4f9778cd49a4', 'param1=one,param2=two',
'/src/main/sub-path')
p = 'pkg:bitbucket/pypy/numpy@4f9778cd49a4?param1=one,param2=two#/src/main/sub-path'
self.assertEqual(r, p)

Expand All @@ -172,5 +175,6 @@ def test_bitbucket_params_to_purl_main_success(self):
p = 'pkg:bitbucket/pypy/numpy'
self.assertEqual(r, p)


if __name__ == '__main__':
unittest.main()
9 changes: 7 additions & 2 deletions tests/test_github_purl.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import unittest
from pypurl.purl import Purl


class GithubPurlTestCase(unittest.TestCase):
def setUp(self):
self.purl_obj = Purl()
print('Running GitHub-repository tests... OK')

def test_github_purl_to_dict_version_success(self):
purl_with_version = 'pkg:github/package-url/pypurl-spec@244fd47e07d1004f0aed9c'
Expand Down Expand Up @@ -153,12 +155,14 @@ def test_github_durl_to_purl_main_success(self):
self.assertEqual(r, p)

def test_github_params_to_purl_all_success(self):
r = self.purl_obj.params_to_purl('github', 'vBarbaros', 'pypurl', '244fd47e07d1004f0aed9c', 'param1=one,param2=two', '/src/main/sub-path')
r = self.purl_obj.params_to_purl('github', 'vBarbaros', 'pypurl', '244fd47e07d1004f0aed9c',
'param1=one,param2=two', '/src/main/sub-path')
p = 'pkg:github/vBarbaros/pypurl@244fd47e07d1004f0aed9c?param1=one,param2=two#/src/main/sub-path'
self.assertEqual(r, p)

def test_github_params_to_purl_version_qualifier_success(self):
r = self.purl_obj.params_to_purl('github', 'vBarbaros', 'pypurl', '244fd47e07d1004f0aed9c', 'param1=one,param2=two')
r = self.purl_obj.params_to_purl('github', 'vBarbaros', 'pypurl', '244fd47e07d1004f0aed9c',
'param1=one,param2=two')
p = 'pkg:github/vBarbaros/pypurl@244fd47e07d1004f0aed9c?param1=one,param2=two'
self.assertEqual(r, p)

Expand All @@ -172,5 +176,6 @@ def test_github_params_to_purl_main_success(self):
p = 'pkg:github/vBarbaros/pypurl'
self.assertEqual(r, p)


if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions tests/test_gitlab_purl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class GitlabPurlTestCase(unittest.TestCase):
def setUp(self):
self.purl_obj = Purl()
print('Running GitLab-repository tests... OK')

def test_gitlab_purl_to_dict_version_success(self):
purl_with_version = 'pkg:gitlab/gitlab-org/gitlab-foss@v14.1.8'
Expand Down
Loading

0 comments on commit ade514b

Please sign in to comment.