Skip to content

Commit 695ae3b

Browse files
Adds PackageManifest class for maven
Creates PackageManifest class for maven pom.xml manifests and overrides the methods for detection and PackageManifest creation. See #2748 Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
1 parent 9f9e3a6 commit 695ae3b

File tree

5 files changed

+4845
-1991
lines changed

5 files changed

+4845
-1991
lines changed

src/packagedcode/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
jar_manifest.JavaManifest,
4848
models.JavaEar,
4949
models.JavaWar,
50-
maven.MavenPomPackage,
50+
maven.PomXml,
5151
jar_manifest.IvyJar,
5252
models.JBossSar,
5353
models.Axis2Mar,

src/packagedcode/maven.py

Lines changed: 113 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,14 @@
4747

4848

4949
@attr.s()
50-
class MavenPomPackage(models.Package, models.PackageManifest):
51-
file_patterns = ('*.pom', 'pom.xml',)
52-
extensions = ('.pom',)
50+
class MavenPomPackage(models.Package):
5351
default_type = 'maven'
5452
default_primary_language = 'Java'
5553

5654
default_web_baseurl = 'https://repo1.maven.org/maven2'
5755
default_download_baseurl = 'https://repo1.maven.org/maven2'
5856
default_api_baseurl = 'https://repo1.maven.org/maven2'
5957

60-
@classmethod
61-
def recognize(cls, location):
62-
yield parse(location)
63-
6458
@classmethod
6559
def get_package_root(cls, manifest_resource, codebase):
6660
if manifest_resource.name.endswith(('pom.xml', '.pom',)):
@@ -822,36 +816,6 @@ def substring(s, start, end):
822816
return s[start:end]
823817

824818

825-
def is_pom(location):
826-
"""
827-
Return True if the file at location is highly likely to be a POM.
828-
"""
829-
if (not filetype.is_file(location)
830-
or not location.endswith(('.pom', 'pom.xml', 'project.xml',))):
831-
832-
if TRACE: logger.debug('is_pom: not a POM on name: {}'.format(location))
833-
return
834-
835-
T = contenttype.get_type(location)
836-
if T.is_text:
837-
838-
# check the POM version in the first 150 lines
839-
with open(location, 'rb') as pom:
840-
for n, line in enumerate(pom):
841-
if n > 150:
842-
break
843-
if any(x in line for x in
844-
(b'http://maven.apache.org/POM/4.0.0',
845-
b'http://maven.apache.org/xsd/maven-4.0.0.xsd',
846-
b'<modelVersion>',
847-
# somehow we can still parse version 3 poms too
848-
b'<pomVersion>',)
849-
):
850-
return True
851-
852-
if TRACE: logger.debug('is_pom: not a POM based on type: {}: {}'.format(T, location))
853-
854-
855819
def has_basic_pom_attributes(pom):
856820
"""
857821
Return True if a POM object has basic attributes needed to make this
@@ -870,7 +834,7 @@ def get_maven_pom(location=None, text=None, check_is_pom=False, extra_properties
870834
Return a MavenPom object from a POM file at `location` or provided as a
871835
`text` string.
872836
"""
873-
if location and check_is_pom and not is_pom(location):
837+
if location and check_is_pom and not PomXml.is_manifest(location):
874838
return
875839
pom = MavenPom(location, text)
876840
if not extra_properties:
@@ -1016,85 +980,122 @@ def get_parties(pom):
1016980
return parties
1017981

1018982

1019-
def parse(location=None, text=None, check_is_pom=True, extra_properties=None):
1020-
"""
1021-
Return a MavenPomPackage or None.
1022-
Parse a pom file at `location` or using the provided `text` (one or
1023-
the other but not both).
1024-
Check if the location is a POM if `check_is_pom` is True.
1025-
When resolving the POM, use an optional `extra_properties` mapping
1026-
of name/value pairs to resolve properties.
1027-
"""
1028-
pom = get_maven_pom(location, text, check_is_pom, extra_properties)
1029-
if not pom:
1030-
return
983+
@attr.s()
984+
class PomXml(MavenPomPackage, models.PackageManifest):
985+
986+
file_patterns = ('*.pom', 'pom.xml',)
987+
extensions = ('.pom',)
988+
989+
@classmethod
990+
def is_manifest(cls, location):
991+
"""
992+
Return True if the file at location is highly likely to be a POM.
993+
"""
994+
if (not filetype.is_file(location)
995+
or not location.endswith(('.pom', 'pom.xml', 'project.xml',))):
1031996

1032-
if TRACE:
1033-
logger.debug('parse: pom:.to_dict()\n{}'.format(pformat(pom.to_dict())))
997+
if TRACE: logger.debug('is_pom: not a POM on name: {}'.format(location))
998+
return
1034999

1035-
version = pom.version
1036-
# pymaven whart
1037-
if version == 'latest.release':
1038-
version = None
1000+
T = contenttype.get_type(location)
1001+
if T.is_text:
1002+
1003+
# check the POM version in the first 150 lines
1004+
with open(location, 'rb') as pom:
1005+
for n, line in enumerate(pom):
1006+
if n > 150:
1007+
break
1008+
if any(x in line for x in
1009+
(b'http://maven.apache.org/POM/4.0.0',
1010+
b'http://maven.apache.org/xsd/maven-4.0.0.xsd',
1011+
b'<modelVersion>',
1012+
# somehow we can still parse version 3 poms too
1013+
b'<pomVersion>',)
1014+
):
1015+
return True
1016+
1017+
if TRACE: logger.debug('is_pom: not a POM based on type: {}: {}'.format(T, location))
10391018

1040-
qualifiers = {}
1041-
classifier = pom.classifier
1042-
if classifier:
1043-
qualifiers['classifier'] = classifier
1044-
1045-
packaging = pom.packaging
1046-
if packaging:
1047-
extension = get_extension(packaging)
1048-
if extension and extension not in ('jar', 'pom'):
1049-
# we use type as in the PURL spec: this is a problematic field with
1050-
# complex defeinition in Maven
1051-
qualifiers['type'] = extension
1052-
1053-
declared_license = pom.licenses
1054-
1055-
source_packages = []
1056-
# TODO: what does this mean????
1057-
if not classifier and all([pom.group_id, pom.artifact_id, version]):
1058-
spurl = PackageURL(
1059-
type=MavenPomPackage.default_type,
1019+
@classmethod
1020+
def recognize(cls, location, text=None, check_is_pom=True, extra_properties=None):
1021+
"""
1022+
Yield one or more PomXml objects or None.
1023+
1024+
Parse a pom file at `location` or using the provided `text` (one or
1025+
the other but not both).
1026+
Check if the location is a POM if `check_is_pom` is True.
1027+
When resolving the POM, use an optional `extra_properties` mapping
1028+
of name/value pairs to resolve properties.
1029+
"""
1030+
pom = get_maven_pom(location, text, check_is_pom, extra_properties)
1031+
if not pom:
1032+
return
1033+
1034+
if TRACE:
1035+
logger.debug('parse: pom:.to_dict()\n{}'.format(pformat(pom.to_dict())))
1036+
1037+
version = pom.version
1038+
# pymaven whart
1039+
if version == 'latest.release':
1040+
version = None
1041+
1042+
qualifiers = {}
1043+
classifier = pom.classifier
1044+
if classifier:
1045+
qualifiers['classifier'] = classifier
1046+
1047+
packaging = pom.packaging
1048+
if packaging:
1049+
extension = get_extension(packaging)
1050+
if extension and extension not in ('jar', 'pom'):
1051+
# we use type as in the PURL spec: this is a problematic field with
1052+
# complex defeinition in Maven
1053+
qualifiers['type'] = extension
1054+
1055+
declared_license = pom.licenses
1056+
1057+
source_packages = []
1058+
# TODO: what does this mean????
1059+
if not classifier and all([pom.group_id, pom.artifact_id, version]):
1060+
spurl = PackageURL(
1061+
type=MavenPomPackage.default_type,
1062+
namespace=pom.group_id,
1063+
name=pom.artifact_id,
1064+
version=version,
1065+
# we hardcode the source qualifier for now...
1066+
qualifiers=dict(classifier='sources'))
1067+
source_packages = [spurl.to_string()]
1068+
1069+
pname = pom.name or ''
1070+
pdesc = pom.description or ''
1071+
if pname == pdesc:
1072+
description = pname
1073+
else:
1074+
description = [d for d in (pname, pdesc) if d]
1075+
description = '\n'.join(description)
1076+
1077+
issue_mngt = pom.issue_management or {}
1078+
bug_tracking_url = issue_mngt.get('url')
1079+
1080+
scm = pom.scm or {}
1081+
vcs_url, code_view_url = build_vcs_and_code_view_urls(scm)
1082+
1083+
# FIXME: there are still other data to map in a Package
1084+
yield cls(
10601085
namespace=pom.group_id,
10611086
name=pom.artifact_id,
10621087
version=version,
1063-
# we hardcode the source qualifier for now...
1064-
qualifiers=dict(classifier='sources'))
1065-
source_packages = [spurl.to_string()]
1066-
1067-
pname = pom.name or ''
1068-
pdesc = pom.description or ''
1069-
if pname == pdesc:
1070-
description = pname
1071-
else:
1072-
description = [d for d in (pname, pdesc) if d]
1073-
description = '\n'.join(description)
1074-
1075-
issue_mngt = pom.issue_management or {}
1076-
bug_tracking_url = issue_mngt.get('url')
1077-
1078-
scm = pom.scm or {}
1079-
vcs_url, code_view_url = build_vcs_and_code_view_urls(scm)
1080-
1081-
# FIXME: there are still other data to map in a Package
1082-
package = MavenPomPackage(
1083-
namespace=pom.group_id,
1084-
name=pom.artifact_id,
1085-
version=version,
1086-
qualifiers=qualifiers or None,
1087-
description=description or None,
1088-
homepage_url=pom.url or None,
1089-
declared_license=declared_license or None,
1090-
parties=get_parties(pom),
1091-
dependencies=get_dependencies(pom),
1092-
source_packages=source_packages,
1093-
bug_tracking_url=bug_tracking_url,
1094-
vcs_url=vcs_url,
1095-
code_view_url=code_view_url,
1096-
)
1097-
return package
1088+
qualifiers=qualifiers or None,
1089+
description=description or None,
1090+
homepage_url=pom.url or None,
1091+
declared_license=declared_license or None,
1092+
parties=get_parties(pom),
1093+
dependencies=get_dependencies(pom),
1094+
source_packages=source_packages,
1095+
bug_tracking_url=bug_tracking_url,
1096+
vcs_url=vcs_url,
1097+
code_view_url=code_view_url,
1098+
)
10981099

10991100

11001101
def build_vcs_and_code_view_urls(scm):
@@ -1175,7 +1176,7 @@ def recon(self, location):
11751176
if not filetype.is_file(loc):
11761177
continue
11771178
# a pom is an xml doc
1178-
if not is_pom(location):
1179+
if not PomXml.is_manifest(location):
11791180
continue
11801181

11811182
if f == 'pom.xml':

tests/packagedcode/data/plugin/help.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ Package: jboss
229229

230230
--------------------------------------------
231231
Package: maven
232-
class: packagedcode.maven:MavenPomPackage
232+
class: packagedcode.maven:PomXml
233233
file_patterns: *.pom, pom.xml
234234
extensions: .pom
235235

0 commit comments

Comments
 (0)