Skip to content

Commit 287bbc2

Browse files
author
Li
committed
#395 Make generic code and avoid dupication
1 parent a3506a8 commit 287bbc2

File tree

4 files changed

+128
-140
lines changed

4 files changed

+128
-140
lines changed

src/packagedcode/npm.py

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from commoncode import fileutils
3737

3838
from packagedcode import models
39+
from packagedcode.utils import parse_repo_url
3940

4041
"""
4142
Handle Node.js NPM packages
@@ -299,79 +300,6 @@ def repository_mapper(repo, package):
299300
return package
300301

301302

302-
VCS_URLS = (
303-
'https://',
304-
'http://',
305-
'git://',
306-
'git+git://',
307-
'hg+https://',
308-
'hg+http://',
309-
'git+https://',
310-
'git+http://',
311-
'svn+https://',
312-
'svn+http://',
313-
'svn://',
314-
)
315-
316-
317-
def parse_repo_url(repo_url):
318-
"""
319-
Validate a repo_ulr and handle shortcuts for GitHub, GitHub gist,
320-
Bitbucket, or GitLab repositories (same syntax as npm install):
321-
322-
See https://docs.npmjs.com/files/package.json#repository
323-
This is done here in npm:
324-
https://github.com/npm/npm/blob/d3c858ce4cfb3aee515bb299eb034fe1b5e44344/node_modules/hosted-git-info/git-host-info.js
325-
326-
These should be resolved:
327-
npm/npm
328-
gist:11081aaa281
329-
bitbucket:example/repo
330-
gitlab:another/repo
331-
expressjs/serve-static
332-
git://github.com/angular/di.js.git
333-
git://github.com/hapijs/boom
334-
[email protected]:balderdashy/waterline-criteria.git
335-
http://github.com/ariya/esprima.git
336-
http://github.com/isaacs/nopt
337-
https://github.com/chaijs/chai
338-
https://github.com/christkv/kerberos.git
339-
https://gitlab.com/foo/private.git
340-
[email protected]:foo/private.git
341-
"""
342-
343-
# TODO: Improve this and use outside of NPMs
344-
is_vcs_url = repo_url.startswith(VCS_URLS)
345-
if is_vcs_url:
346-
# TODO: ensure the .git suffix is present if needed
347-
return repo_url
348-
349-
if repo_url.startswith('git@'):
350-
left, right = repo_url.split('@', 1)
351-
host, repo = right.split(':', 1)
352-
# may be we should
353-
if any(h in host for h in ['github', 'bitbucket', 'gitlab']):
354-
return 'https://%(host)s/%(repo)s' % locals()
355-
else:
356-
return repo_url
357-
358-
if repo_url.startswith('gist:'):
359-
return repo_url
360-
361-
elif repo_url.startswith(('bitbucket:', 'gitlab:', 'github:')):
362-
hoster_urls = {
363-
'bitbucket:': 'https://bitbucket.org/%(repo)s',
364-
'github:': 'https://github.com/%(repo)s',
365-
'gitlab:': 'https://gitlab.com/%(repo)s',
366-
}
367-
hoster, repo = repo_url.split(':', 1)
368-
return hoster_urls[hoster] % locals()
369-
elif len(repo_url.split('/')) == 2:
370-
# implicit github
371-
return 'https://github.com/%(repo_url)s' % locals()
372-
return repo_url
373-
374-
375303
def url_mapper(url, package):
376304
"""
377305
In a package.json, the "url" field is a redirection to a package download

src/packagedcode/phpcomposer.py

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from commoncode import fileutils
3636

3737
from packagedcode import models
38+
from packagedcode.utils import parse_repo_url
3839

3940
"""
4041
Handle PHP composer packages, refer to https://getcomposer.org/
@@ -82,7 +83,7 @@ def parse(location):
8283
])
8384

8485
# mapping of top level composer.json items to a function accepting as arguments
85-
# the package.json element value and returning an iterable of key, values Package Object to update
86+
# the composer.json element value and returning an iterable of key, values Package Object to update
8687
field_mappers = OrderedDict([
8788
('authors', author_mapper),
8889
('license', licensing_mapper),
@@ -251,72 +252,6 @@ def repository_mapper(repos, package):
251252
return package
252253

253254

254-
VCS_URLS = (
255-
'https://',
256-
'http://',
257-
'git://',
258-
'git+git://',
259-
'hg+https://',
260-
'hg+http://',
261-
'git+https://',
262-
'git+http://',
263-
'svn+https://',
264-
'svn+http://',
265-
'svn://',
266-
)
267-
268-
269-
def parse_repo_url(repo_url):
270-
"""
271-
Validate a repo_url and handle shortcuts for GitHub, GitHub gist,
272-
Bitbucket, or GitLab repositories:
273-
274-
See https://getcomposer.org/doc/04-schema.md#repositories
275-
276-
These should be resolved:
277-
gist:11081aaa281
278-
bitbucket:example/repo
279-
gitlab:another/repo
280-
expressjs/serve-static
281-
git://github.com/angular/di.js.git
282-
git://github.com/hapijs/boom
283-
[email protected]:balderdashy/waterline-criteria.git
284-
http://github.com/ariya/esprima.git
285-
http://github.com/isaacs/nopt
286-
https://github.com/chaijs/chai
287-
https://github.com/christkv/kerberos.git
288-
https://gitlab.com/foo/private.git
289-
[email protected]:foo/private.git
290-
"""
291-
292-
is_vcs_url = repo_url.startswith(VCS_URLS)
293-
if is_vcs_url:
294-
return repo_url
295-
296-
if repo_url.startswith('git@'):
297-
left, right = repo_url.split('@', 1)
298-
host, repo = right.split(':', 1)
299-
if any(h in host for h in ['github', 'bitbucket', 'gitlab']):
300-
return 'https://%(host)s/%(repo)s' % locals()
301-
else:
302-
return repo_url
303-
304-
if repo_url.startswith('gist:'):
305-
return repo_url
306-
307-
elif repo_url.startswith(('bitbucket:', 'gitlab:', 'github:')):
308-
hoster_urls = {
309-
'bitbucket:': 'https://bitbucket.org/%(repo)s',
310-
'github:': 'https://github.com/%(repo)s',
311-
'gitlab:': 'https://gitlab.com/%(repo)s',
312-
}
313-
hoster, repo = repo_url.split(':', 1)
314-
return hoster_urls[hoster] % locals()
315-
elif len(repo_url.split('/')) == 2:
316-
return 'https://github.com/%(repo_url)s' % locals()
317-
return repo_url
318-
319-
320255
def deps_mapper(deps, package, field_name):
321256
"""
322257
Handle deps such as dependencies, devDependencies

src/packagedcode/utils.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#
2+
# Copyright (c) 2015 nexB Inc. and others. All rights reserved.
3+
# http://nexb.com and https://github.com/nexB/scancode-toolkit/
4+
# The ScanCode software is licensed under the Apache License version 2.0.
5+
# Data generated with ScanCode require an acknowledgment.
6+
# ScanCode is a trademark of nexB Inc.
7+
#
8+
# You may not use this software except in compliance with the License.
9+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
10+
# Unless required by applicable law or agreed to in writing, software distributed
11+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+
# specific language governing permissions and limitations under the License.
14+
#
15+
# When you publish or redistribute any data created with ScanCode or any ScanCode
16+
# derivative work, you must accompany this data with the following acknowledgment:
17+
#
18+
# Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
19+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
20+
# ScanCode should be considered or used as legal advice. Consult an Attorney
21+
# for any legal advice.
22+
# ScanCode is a free software code scanning tool from nexB Inc. and others.
23+
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.
24+
25+
from __future__ import print_function, absolute_import
26+
27+
28+
VCS_URLS = (
29+
'https://',
30+
'http://',
31+
'git://',
32+
'git+git://',
33+
'hg+https://',
34+
'hg+http://',
35+
'git+https://',
36+
'git+http://',
37+
'svn+https://',
38+
'svn+http://',
39+
'svn://',
40+
)
41+
42+
43+
def parse_repo_url(repo_url):
44+
"""
45+
Validate a repo_ulr and handle shortcuts for GitHub, GitHub gist,
46+
Bitbucket, or GitLab repositories (same syntax as npm install):
47+
48+
See https://docs.npmjs.com/files/package.json#repository
49+
or https://getcomposer.org/doc/05-repositories.md
50+
51+
This is done here in npm:
52+
https://github.com/npm/npm/blob/d3c858ce4cfb3aee515bb299eb034fe1b5e44344/node_modules/hosted-git-info/git-host-info.js
53+
54+
These should be resolved:
55+
npm/npm
56+
gist:11081aaa281
57+
bitbucket:example/repo
58+
gitlab:another/repo
59+
expressjs/serve-static
60+
git://github.com/angular/di.js.git
61+
git://github.com/hapijs/boom
62+
[email protected]:balderdashy/waterline-criteria.git
63+
http://github.com/ariya/esprima.git
64+
http://github.com/isaacs/nopt
65+
https://github.com/chaijs/chai
66+
https://github.com/christkv/kerberos.git
67+
https://gitlab.com/foo/private.git
68+
[email protected]:foo/private.git
69+
"""
70+
71+
# TODO: Improve this and use outside of NPMs
72+
is_vcs_url = repo_url.startswith(VCS_URLS)
73+
if is_vcs_url:
74+
# TODO: ensure the .git suffix is present if needed
75+
return repo_url
76+
77+
if repo_url.startswith('git@'):
78+
left, right = repo_url.split('@', 1)
79+
host, repo = right.split(':', 1)
80+
# may be we should
81+
if any(h in host for h in ['github', 'bitbucket', 'gitlab']):
82+
return 'https://%(host)s/%(repo)s' % locals()
83+
else:
84+
return repo_url
85+
86+
if repo_url.startswith('gist:'):
87+
return repo_url
88+
89+
elif repo_url.startswith(('bitbucket:', 'gitlab:', 'github:')):
90+
hoster_urls = {
91+
'bitbucket:': 'https://bitbucket.org/%(repo)s',
92+
'github:': 'https://github.com/%(repo)s',
93+
'gitlab:': 'https://gitlab.com/%(repo)s',
94+
}
95+
hoster, repo = repo_url.split(':', 1)
96+
return hoster_urls[hoster] % locals()
97+
elif len(repo_url.split('/')) == 2:
98+
# implicit github
99+
return 'https://github.com/%(repo_url)s' % locals()
100+
return repo_url

tests/packagedcode/test_phpcomposer.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from packages_test_utils import PackageTester
3131

3232
from packagedcode import phpcomposer
33+
from packagedcode.utils import parse_repo_url
3334

3435

3536
class TestPHPcomposer(PackageTester):
@@ -55,6 +56,30 @@ def test_parse_person(self):
5556
]
5657
assert expected == list(phpcomposer.parse_person(test))
5758

59+
def test_parse_repo_url_basic(self):
60+
url = 'https://pear2.php.net'
61+
result = parse_repo_url(url)
62+
expected = 'https://pear2.php.net'
63+
assert expected == result
64+
65+
def test_parse_repo_url_svn(self):
66+
url = 'http://svn.example.org/projectA/'
67+
result = parse_repo_url(url)
68+
expected = 'http://svn.example.org/projectA/'
69+
assert expected == result
70+
71+
def test_parse_repo_url_github(self):
72+
url = 'https://github.com/igorw/monolog'
73+
result = parse_repo_url(url)
74+
expected = 'https://github.com/igorw/monolog'
75+
assert expected == result
76+
77+
def test_parse_repo_url_bitbucket(self):
78+
url = '[email protected]:vendor/my-private-repo.git'
79+
result = parse_repo_url(url)
80+
expected = 'https://bitbucket.org/vendor/my-private-repo.git'
81+
assert expected == result
82+
5883
def test_parse_atimer(self):
5984
test_file = self.get_test_loc('phpcomposer/a-timer/composer.json')
6085
expected_loc = self.get_test_loc('phpcomposer/a-timer/composer.json.expected')

0 commit comments

Comments
 (0)