-
-
Notifications
You must be signed in to change notification settings - Fork 563
/
maven.py
1458 lines (1238 loc) · 50.5 KB
/
maven.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import logging
import os.path
from pprint import pformat
import javaproperties
import lxml
from fnmatch import fnmatchcase
from packageurl import PackageURL
from pymaven import artifact
from pymaven import pom
from pymaven.pom import strip_namespace
from commoncode import fileutils
from packagedcode import models
from packagedcode.jar_manifest import parse_scm_connection
from packagedcode.jar_manifest import parse_manifest
from packagedcode.jar_manifest import get_normalized_java_manifest_data
from packagedcode.utils import normalize_vcs_url
from textcode import analysis
from typecode import contenttype
import saneyaml
TRACE = False
logger = logging.getLogger(__name__)
if TRACE:
import sys
logging.basicConfig(stream=sys.stdout)
logger.setLevel(logging.DEBUG)
"""
Support for Maven POMs including resolution of variables using Maven properties
when possible.
We have seen Maven pom in three layout syles:
First case: a pom.xml inside a META-INF directory such as in
/META-INF/maven/log4j/log4j/pom.xml possibly with a pom.properties
Second case: a pom.xml at the root of a package codebase development tree
possibly with a pom.properties
Third case: a maven repo layout: the jars are side-by-side with a .pom and
there is no pom.properties check if there are side-by-side artifacts
"""
class MavenBasePackageHandler(models.DatafileHandler):
@classmethod
def assemble(cls, package_data, resource, codebase, package_adder=models.add_to_package):
"""
Assembles from codebases where both a pom.xml and a MANIFEST.MF is present,
otherwise uses the default assemble function and `assign_package_to_resources`
function from respective DatafileHandlers.
"""
if codebase.has_single_resource:
yield from models.DatafileHandler.assemble(package_data, resource, codebase)
return
datafile_path = resource.path
# This order is important as we want pom.xml to be used for package
# creation and then to update from MANIFEST later
manifest_path_pattern = '*/META-INF/MANIFEST.MF'
nested_pom_xml_path_pattern = '*/META-INF/maven/**/pom.xml'
datafile_name_patterns = (nested_pom_xml_path_pattern, manifest_path_pattern)
is_manifest = fnmatchcase(datafile_path, manifest_path_pattern)
is_pom_xml = fnmatchcase(datafile_path, nested_pom_xml_path_pattern)
if is_manifest:
meta_inf_resource = resource.parent(codebase)
elif is_pom_xml:
upward_segments = 4
root = resource
for _ in range(upward_segments):
root = root.parent(codebase)
meta_inf_resource = root
else:
yield from MavenPomXmlHandlerMixin.assemble(package_data, resource, codebase)
return
pom_xmls = []
manifests = []
for r in meta_inf_resource.walk(codebase):
if fnmatchcase(r.path, manifest_path_pattern):
manifests.append(r)
if fnmatchcase(r.path, nested_pom_xml_path_pattern):
pom_xmls.append(r)
if len(pom_xmls) > 1:
yield from MavenPomXmlHandlerMixin.assemble(package_data, resource, codebase)
return
if manifests and pom_xmls:
# raise Exception(resource.path, meta_inf_resource, datafile_name_patterns, package_adder)
parent_resource = meta_inf_resource.parent(codebase)
if not parent_resource:
parent_resource = meta_inf_resource
yield from cls.assemble_from_many_datafiles_in_directory(
datafile_name_patterns=datafile_name_patterns,
directory=meta_inf_resource,
codebase=codebase,
package_adder=package_adder,
ignore_name_check=True,
parent_resource=parent_resource,
)
elif manifests and not pom_xmls:
yield from JavaJarManifestHandlerMixin.assemble(package_data, resource, codebase)
elif pom_xmls and not manifests:
yield from MavenPomXmlHandlerMixin.assemble(package_data, resource, codebase)
else:
yield from models.DatafileHandler.assemble(package_data, resource, codebase)
class JavaJarManifestHandler(MavenBasePackageHandler):
datasource_id = 'java_jar_manifest'
path_patterns = ('*/META-INF/MANIFEST.MF',)
default_package_type = 'jar'
default_primary_language = 'Java'
description = 'Java JAR MANIFEST.MF'
documentation_url = 'https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html'
@classmethod
def parse(cls, location, package_only=False):
sections = parse_manifest(location)
if sections:
main_section = sections[0]
manifest = get_normalized_java_manifest_data(main_section)
if manifest:
package_data = dict(**manifest,)
yield models.PackageData.from_data(package_data, package_only)
class JavaJarManifestHandlerMixin(models.DatafileHandler):
@classmethod
def assign_package_to_resources(cls, package, resource, codebase, package_adder):
# we want to root of the jar, two levels up
parent = resource.parent(codebase)
if parent:
parent = resource.parent(codebase)
if parent:
models.DatafileHandler.assign_package_to_resources(
package,
resource=parent,
codebase=codebase,
package_adder=package_adder,
)
class JavaOSGiManifestHandler(JavaJarManifestHandler):
datasource_id = 'java_osgi_manifest'
# This is an empty tuple to avoid getting back two packages
# essentially this is the same as JavaJarManifestHandler
path_patterns = ()
default_primary_language = 'Java'
description = 'Java OSGi MANIFEST.MF'
default_package_type = 'osgi'
class MavenPomXmlHandler(MavenBasePackageHandler):
datasource_id = 'maven_pom'
# NOTE: Maven 1.x used project.xml
path_patterns = ('*.pom', '*pom.xml',)
default_package_type = 'maven'
default_primary_language = 'Java'
description = 'Apache Maven pom'
documentation_url = 'https://maven.apache.org/pom.html'
@classmethod
def is_datafile(cls, location, filetypes=tuple()):
"""
Return True if the file at location is highly likely to be a POM.
"""
if super().is_datafile(location, filetypes=filetypes):
return True
T = contenttype.get_type(location)
if not T.is_text:
return
maven_declarations = (
b'http://maven.apache.org/POM/4.0.0',
b'http://maven.apache.org/xsd/maven-4.0.0.xsd',
b'<modelVersion>',
# somehow we can still parse version 3 poms too
b'<pomVersion>',
)
# check the POM version in the first 150 lines
with open(location, 'rb') as pom:
for n, line in enumerate(pom):
if n > 150:
break
if any(x in line for x in maven_declarations):
return True
@classmethod
def parse(cls, location, package_only=False, base_url='https://repo1.maven.org/maven2'):
package_data = parse(
location=location,
datasource_id=cls.datasource_id,
package_type=cls.default_package_type,
primary_language=cls.default_primary_language,
base_url=base_url,
package_only=package_only,
)
if package_data:
yield package_data
@classmethod
def get_top_level_resources(cls, manifest_resource, codebase):
"""
Yield Resources that are top-level based on a JAR's directory structure
"""
if 'META-INF' in manifest_resource.path:
path_segments = manifest_resource.path.split('META-INF')
leading_segment = path_segments[0].strip()
meta_inf_dir_path = f'{leading_segment}/META-INF'
meta_inf_resource = codebase.get_resource(meta_inf_dir_path)
if meta_inf_resource:
yield meta_inf_resource
yield from meta_inf_resource.walk(codebase)
class MavenPomXmlHandlerMixin(models.DatafileHandler):
@classmethod
def assign_package_to_resources(cls, package, resource, codebase, package_adder):
"""
Set the "for_packages" attributes to ``package`` for the whole
resource tree of a ``resource`` object in the ``codebase``.
handle the various cases where we can find a POM.
"""
if resource.path.endswith('.pom'):
# we only treat the parent as the root
return models.DatafileHandler.assign_package_to_parent_tree(
package,
resource,
codebase,
package_adder
)
# the root is either the parent or further up for poms stored under
# a META-INF dir
# assert resource.name.endswith('pom.xml')
root = None
if resource.path.endswith(f'META-INF/maven/{package.namespace}/{package.name}/pom.xml'):
# First case: a pom.xml inside a META-INF directory such as in
# /META-INF/maven/log4j/log4j/pom.xml: This requires going up 5 times
upward_segments = 4
root = resource
for _ in range(upward_segments):
root = root.parent(codebase)
number_poms = 0
for child in root.walk(codebase):
if 'pom.xml' in child.path:
number_poms += 1
if number_poms > 1:
root = resource
else:
root = root.parent(codebase)
else:
# Second case: a pom.xml at the root of codebase tree
# FIXME: handle the cases opf parent POMs and nested POMs
root = resource.parent(codebase)
# get a root in all cases
if not root:
root = resource.parent(codebase)
return models.DatafileHandler.assign_package_to_resources(
package,
resource=root,
codebase=codebase,
package_adder=package_adder
)
# TODO: assemble with its pom!!
class MavenPomPropertiesHandler(models.NonAssemblableDatafileHandler):
datasource_id = 'maven_pom_properties'
path_patterns = ('*/pom.properties',)
default_package_type = 'maven'
default_primary_language = 'Java'
description = 'Apache Maven pom properties file'
documentation_url = 'https://maven.apache.org/pom.html'
@classmethod
def parse(cls, location, package_only=False):
"""
Yield PackageData from a pom.properties file (which is typically side-
by-side with its pom file.)
"""
with open(location) as props:
properties = javaproperties.load(props) or {}
if TRACE:
logger.debug(f'MavenPomPropertiesHandler.parse: properties: {properties!r}')
if properties:
yield from cls.parse_pom_properties(properties=properties, package_only=package_only)
@classmethod
def parse_pom_properties(cls, properties, package_only=False):
namespace = properties.pop("groupId", None)
name = properties.pop("artifactId", None)
version = properties.pop("version", None)
if properties:
extra_data = dict(pom_properties=properties)
else:
extra_data = {}
package_data = dict(
datasource_id=cls.datasource_id,
type=cls.default_package_type,
primary_language=cls.default_primary_language,
name=name,
namespace=namespace,
version=version,
extra_data=extra_data,
)
yield models.PackageData.from_data(package_data, package_only)
def build_url(
group_id,
artifact_id,
version,
filename=None,
base_url='https://repo1.maven.org/maven2',
):
"""
Return a download URL for a Maven artifact built from its POM "coordinates".
"""
filename = filename or ''
if group_id:
group_id = group_id.replace('.', '/')
path = f'{group_id}/{artifact_id}/{version}'
else:
path = f'{artifact_id}/{version}'
return f'{base_url}/{path}/{filename}'
def build_filename(artifact_id, version, extension, classifier=None):
"""
Return a filename for a Maven artifact built from its coordinates.
"""
extension = extension or ''
classifier = classifier or ''
if classifier:
classifier = f'-{classifier}'
return f'{artifact_id}-{version}{classifier}.{extension}'
class ParentPom(artifact.Artifact):
"""
A minimal Artifact subclass used to store parent poms when no POM
file is available for these.
"""
def __init__(self, coordinate):
super().__init__(coordinate)
# add empty, pom.Pom-class-like empty attributes
self.client = None
self.dependencies = {}
self.dependency_management = {}
self.parent = None
self.properties = {}
# TODO: ????
# self.pom_data/self.pom_data = None
def resolve(self, **properties):
"""
Resolve possible parent POM properties using the provided
properties.
"""
if not properties:
return
self.group_id = MavenPom._replace_props(self.group_id, properties)
self.artifact_id = MavenPom._replace_props(self.artifact_id, properties)
self.version = MavenPom._replace_props(self.version, properties)
def to_dict(self):
"""
Return a mapping representing this POM
"""
return {
'group_id': self.group_id,
'artifact_id': self.artifact_id,
'version': str(self.version) if self.version else None,
'classifier': self.classifier,
'type': self.type,
}
class MavenPom(pom.Pom):
def __init__(self, location=None, text=None):
"""
Build a POM from a location or unicode text.
"""
assert (location or text) and (not (location and text))
if location:
try:
with open(location) as fh:
xml_text = fh.read()
except UnicodeDecodeError as _a:
xml_text = analysis.unicode_text(location)
else:
xml_text = text
xml_text = strip_namespace(xml_text)
xml_text = xml_text.encode('utf-8')
if TRACE:
logger.debug('MavenPom.__init__: xml_text: {}'.format(xml_text))
self._pom_data = lxml.etree.fromstring(xml_text, parser=pom.POM_PARSER) # NOQA
# collect and then remove XML comments from the XML elements tree
self.comments = self._get_comments()
lxml.etree.strip_tags(self._pom_data, lxml.etree.Comment) # NOQA
# FIXME: we do not use a client for now.
# There are pending issues at pymaven to address this
self._client = None
self.model_version = self._get_attribute('modelVersion')
if not self.model_version:
# for older POM version 3
self.model_version = self._get_attribute('pomVersion')
self.group_id = self._get_attribute('groupId')
self.artifact_id = self._get_attribute('artifactId')
if TRACE:
logger.debug('MavenPom.__init__: self.artifact_id: {}'.format(self.artifact_id))
self.version = self._get_attribute('version')
self.classifier = self._get_attribute('classifier')
self.packaging = self._get_attribute('packaging') or 'jar'
self.name = self._get_attribute('name')
self.description = self._get_attribute('description')
self.inception_year = self._get_attribute('inceptionYear')
self.url = self._get_attribute('url')
self.organization_name = self._get_attribute('organization/name')
self.organization_url = self._get_attribute('organization/url')
self.licenses = list(self._find_licenses())
self.developers = list(self._find_parties('developers/developer'))
self.contributors = list(self._find_parties('contributors/contributor'))
self.mailing_lists = list(self._find_mailing_lists())
self.scm = self._find_scm()
self.issue_management = self._find_issue_management()
self.ci_management = self._find_ci_management()
self.distribution_management = self._find_distribution_management()
self.repositories = list(self._find_repositories('repositories/repository'))
self.plugin_repositories = list(self._find_repositories('pluginRepositories/pluginRepository'))
self.modules = self._get_attributes_list('modules/module')
# FIXME: this attribute should be collected with the parent but
# is not retrieved yet by pymaven it points to the relative path
# where to find the full parent POM
self.parent_relative_path = self._get_attribute('relativePath') # or '../pom.xml_text'
# FIXME: Other types that are not collected for now (or
# indirectly through dependencies management) include: build,
# reporting, profiles, etc
# dynamic attributes
self._parent = None
self._dep_mgmt = None
self._dependencies = None
self._properties = None
def _extra_properties(self):
"""
Return a mapping of extra properties
"""
properties = {}
for name in build_property_variants('classifier'):
properties[name] = self.classifier
for name in build_property_variants('packaging'):
properties[name] = self.packaging
for name in build_property_variants('organization.name'):
properties[name] = self.organization_name
for name in build_property_variants('organization.url'):
properties[name] = self.organization_url
# TODO: collect props defined in a properties file side-by-side the pom file
# see https://maven.apache.org/shared/maven-archiver/#class_archive
# this applies to POMs stored inside a JAR or in a plain directory
return properties
@classmethod
def _replace_props(cls, text, properties):
if not text:
return text
def subfunc(matchobj):
"""Return the replacement value for a matched property key."""
key = matchobj.group(1)
# does this key contain a substring?
real_key, start_end = _get_substring_expression(key)
if not start_end:
value = properties.get(key)
return value
# apply the substring transform
value = properties.get(real_key)
if not value:
return value
start, end = start_end
return substring(value, start, end)
result = pom.PROPERTY_RE.sub(subfunc, text)
# we loop a maximum of 5 times to avoid cases of infinite recursion
cycles = 5
while cycles > 0 and result and pom.PROPERTY_RE.match(result):
result = pom.PROPERTY_RE.sub(subfunc, result)
cycles -= 1
if not result:
result = text
return result.strip()
def _replace_properties(self, text, properties=None):
# copied from pymavem.pom.Pom
if not text:
return text
if properties is None:
properties = self.properties
return MavenPom._replace_props(text, properties)
def resolve(self, **extra_properties):
"""
Resolve POM Maven "properties" in attribute values and inherit
from parent. Update the POM attributes in place. Use the extra
keywords to override and supplement existing properties.
"""
# inherit first to get essential parent properties
# FIXME: the parent needs to be resolved first!? but we have a chicken and egg problem then
if self.parent:
pass
self._inherit_from_parent()
# then collect properties + extra
properties = dict(self.properties)
properties.update(self._extra_properties())
# "extra" properties are things that would be loaded from a
# properties files and provided as is they overwrite any
# existing property
properties.update(extra_properties)
if TRACE:
logger.debug('MavenPom.resolve: properties before self-resolution:\n{}'.format(pformat(properties)))
# FIXME: we could remove any property that itself contains
# ${property} as we do not know how to resolve these
# recursively. Or we could do a single pass on properties to
# resolve values against themselves
for key, value in list(properties.items()):
properties[key] = MavenPom._replace_props(value, properties)
if TRACE:
logger.debug('MavenPom.resolve: used properties:\n{}'.format(pformat(properties)))
# these attributes are plain strings
plain_attributes = [
'group_id',
'version',
'classifier',
'packaging',
'name',
'description',
'inception_year',
'url',
'organization_name',
'organization_url',
]
for attr in plain_attributes:
attr_val = getattr(self, attr, None)
if not attr_val:
continue
resolved = self._replace_properties(attr_val, properties)
setattr(self, attr, resolved)
# these attributes are mappings
mapping_attributes = [
'scm',
'issue_management',
'ci_management',
]
for map_attr in mapping_attributes:
mapping = getattr(self, map_attr, {})
if not mapping:
continue
for key, value in mapping.items():
if not value:
continue
mapping[key] = self._replace_properties(value, properties)
# these attributes are lists of mappings
mappings_list_attributes = [
'repositories',
'plugin_repositories',
]
for lmap_attr in mappings_list_attributes:
lmapping = getattr(self, lmap_attr, [])
if not lmapping:
continue
for mapping in lmapping:
for key, value in mapping.items():
if not value:
continue
mapping[key] = self._replace_properties(value, properties)
# these attributes below are complex nested dicts and/or lists mappings
for scope, dependencies in self.dependencies.items():
resolved_deps = []
# FIXME: this is missing the packaging/type and classifier
for (group, artifact, version,), required in dependencies:
group = self._replace_properties(group, properties)
artifact = self._replace_properties(artifact, properties)
version = self._replace_properties(version, properties)
# skip weird damaged POMs such as
# http://repo1.maven.org/maven2/net/sourceforge/findbugs/coreplugin/1.0.0/coreplugin-1.0.0.pom
if not group or not artifact:
continue
resolved_deps.append(((group, artifact, version,), required))
self._dependencies[scope] = resolved_deps
for (group, artifact), (version, scope, optional) in self.dependency_management.items():
group = self._replace_properties(group, properties)
artifact = self._replace_properties(artifact, properties)
version = self._replace_properties(version, properties)
required = not optional
resolved_dep = (group, artifact, version,), required
# we craft a scope prefix for these
scope = f'dependencyManagement'
if scope in self.dependencies:
scope_deps = self._dependencies[scope]
# TODO: Check we don't insert duplicates
scope_deps.append(resolved_dep)
else:
self._dependencies[scope] = [resolved_dep]
if TRACE:
logger.debug('MavenPom.resolve: artifactId after resolve: {}'.format(self.artifact_id))
# TODO: add:
# nest dict: 'distribution_management',
# nest list: 'mailing_lists',
def _inherit_from_parent(self):
"""
Update attributes using inheritance from parent attributes. For
instance, the parent group_id is used if group_id is not defined.
"""
# TODO: there are more attributes (all) that can be inherited
if not self.parent:
return
if self.group_id is None and self.parent.group_id:
self.group_id = self.parent.group_id
if TRACE: logger.debug('_inherit_from_parent: group_id: {}'.format(self.parent.group_id))
if self.version is None and self.parent.version:
self.version = str(self.parent.version)
if TRACE: logger.debug('_inherit_from_parent: version: {}'.format(self.parent.version))
if not self.classifier is None and self.parent.classifier:
self.classifier = self.parent.classifier
if TRACE: logger.debug('_inherit_from_parent: classifier: {}'.format(self.parent.classifier))
# FIXME: the parent may need to be resolved too?
# special handling for URLs: see
# http://maven.apache.org/ref/3.5.0/maven-model-builder/index.html#Inheritance_Assembly
# Notice that the 5 URLs from the model:
# project.url,
# project.scm.connection, project.scm.developerConnection, project.scm.url
# project.distributionManagement.site.url)
# ... have a special inheritance handling: if not configured in
# current model, the inherited value is the parent's one with
# current artifact id appended.
if (self.url is None
and hasattr(self.parent, 'url')
and getattr(self.parent, 'url', None)
and self.artifact_id):
# FIXME: this is not the way to join URLs parts!
self.url = self.parent.url + self.artifact_id
# FIXME: this is not the way to join URLs parts!
parent_scm = getattr(self.parent, 'scm', None)
if self.scm and parent_scm and self.artifact_id:
ps_url = parent_scm.get('url')
if not self.scm.get('url') and ps_url:
# FIXME: this is not the way to join URLs parts!
self.scm['url'] = ps_url + self.artifact_id
ps_connection = parent_scm.get('connection')
if not self.scm.get('connection') and ps_connection:
# FIXME: this is not the way to join URLs parts!
self.scm['connection'] = ps_connection + self.artifact_id
ps_devconnection = parent_scm.get('developer_connection')
if not self.scm.get('developer_connection') and ps_devconnection:
# FIXME: this is not the way to join URLs parts!
self.scm['developer_connection'] = ps_devconnection + self.artifact_id
# TODO: distribution_management.site.url
def _pom_factory(self, group_id, artifact_id, version):
return ParentPom('%s:%s:pom:%s' % (group_id, artifact_id, version))
def _get_attribute(self, xpath, xml=None):
"""Return a single value text attribute for a given xpath or None."""
if xml is None:
xml = self.pom_data
attr = xml.findtext(xpath)
val = attr and attr.strip() or None
if TRACE:
if 'artifactId' in xpath:
logger.debug('MavenPom._get_attribute: xpath: {}'.format(xpath))
logger.debug('MavenPom._get_attribute: xml: {}'.format(xml))
return val
def _get_attributes_list(self, xpath, xml=None):
"""Return a list of text attributes for a given xpath or empty list."""
if xml is None:
xml = self.pom_data
attrs = xml.findall(xpath)
attrs = [attr.text for attr in attrs]
return [attr.strip() for attr in attrs if attr and attr.strip()]
def _get_comments(self, xml=None):
"""Return a list of comment texts or an empty list."""
if xml is None:
xml = self.pom_data
comments = [c.text for c in xml.xpath('//comment()')]
return [c.strip() for c in comments if c and c.strip()]
def _find_licenses(self):
"""Return an iterable of license mappings."""
for lic in self.pom_data.findall('licenses/license'):
yield dict([
('name', self._get_attribute('name', lic)),
('url', self._get_attribute('url', lic)),
('comments', self._get_attribute('comments', lic)),
# arcane and seldom used
('distribution', self._get_attribute('distribution', lic)),
])
def _find_parties(self, key='developers/developer'):
"""Return an iterable of party mappings for a given xpath."""
for party in self.pom_data.findall(key):
yield dict([
('id', self._get_attribute('id', party)),
('name', self._get_attribute('name', party)),
('email', self._get_attribute('email', party)),
('url', self._get_attribute('url', party)),
('organization', self._get_attribute('organization', party)),
('organization_url', self._get_attribute('organizationUrl', party)),
('roles', [role.findtext('.') for role in party.findall('roles/role')]),
])
def _find_mailing_lists(self):
"""Return an iterable of mailing lists mappings."""
for ml in self.pom_data.findall('mailingLists/mailingList'):
archive_url = self._get_attribute('archive', ml)
# TODO: add 'otherArchives/otherArchive' as lists?
yield dict([
('name', self._get_attribute('name', ml)),
('archive_url', archive_url),
])
def _find_scm(self):
"""Return a version control/scm mapping."""
scm = self.pom_data.find('scm')
if scm is None:
return {}
return dict([
('connection', self._get_attribute('connection', scm)),
('developer_connection', self._get_attribute('developer_connection', scm)),
('url', self._get_attribute('url', scm)),
('tag', self._get_attribute('tag', scm)),
])
def _find_issue_management(self):
"""Return an issue management mapping."""
imgt = self.pom_data.find('issueManagement')
if imgt is None:
return {}
return dict([
('system', self._get_attribute('system', imgt)),
('url', self._get_attribute('url', imgt)),
])
def _find_ci_management(self):
"""Return a CI mapping."""
cimgt = self.pom_data.find('ciManagement')
if cimgt is None:
return {}
return dict([
('system', self._get_attribute('system', cimgt)),
('url', self._get_attribute('url', cimgt)),
])
def _find_repository(self, xpath, xml=None):
"""Return a repository mapping for an xpath."""
if xml is None:
xml = self.pom_data
repo = xml.find(xpath)
if repo is None:
return {}
return dict([
('id', self._get_attribute('id', repo)),
('name', self._get_attribute('name', repo)),
('url', self._get_attribute('url', repo)),
])
def _find_distribution_management(self):
"""Return a distribution management mapping."""
dmgt = self.pom_data.find('distributionManagement')
if dmgt is None:
return {}
return dict([
('download_url', self._get_attribute('distributionManagement/downloadUrl')),
('site', self._find_repository('distributionManagement/site')),
('repository', self._find_repository('distributionManagement/repository')),
('snapshot_repository', self._find_repository('distributionManagement/snapshotRepository'))
])
def _find_repositories(self, key='repositories/repository'):
"""Return an iterable or repository mappings for an xpath."""
for repo in self.pom_data.findall(key):
rep = self._find_repository('.', repo)
if rep:
yield rep
def to_dict(self):
"""
Return a mapping representing this POM.
"""
dependencies = {}
for scope, deps in self.dependencies.items():
dependencies[scope] = [
dict([
('group_id', gid),
('artifact_id', aid),
('version', version),
('required', required),
])
for ((gid, aid, version), required) in deps]
return dict([
('model_version', self.model_version),
('group_id', self.group_id),
('artifact_id', self.artifact_id),
('version', self.version),
('classifier', self.classifier),
('packaging', self.packaging),
('parent', self.parent.to_dict() if self.parent else {}),
('name', self.name),
('description', self.description),
('inception_year', self.inception_year),
('url', self.url),
('organization_name', self.organization_name),
('organization_url', self.organization_url),
('licenses', self.licenses or []),
('developers', self.developers or []),
('contributors', self.contributors or []),
('modules', self.modules or []),
('mailing_lists', self.mailing_lists),
('scm', self.scm),
('issue_management', self.issue_management),
('ci_management', self.ci_management),
('distribution_management', self.distribution_management),
('repositories', self.repositories),
('plugin_repositories', self.plugin_repositories),
('dependencies', dependencies or {}),
])
def build_property_variants(name):
"""
Return an iterable of property variant names given a a property name.
"""
yield name
yield 'project.{}'.format(name)
yield 'pom.{}'.format(name)
def _get_substring_expression(text):
"""
Return a tuple of (text, start/end) such that:
- if there is a substring() expression in text, the returned text
has been stripped from it and start/end is a tuple representing
slice indexes for the substring expression.
- if there is no substring() expression in text, text is returned
as-is and start/end is None.
For example:
>>> assert ('pom.artifactId', (8, None)) == _get_substring_expression('pom.artifactId.substring(8)')
>>> assert ('pom.artifactId', None) == _get_substring_expression('pom.artifactId')
"""
key, _, start_end = text.partition('.substring(')
if not start_end:
return text, None
start_end = start_end.rstrip(')')
start_end = [se.strip() for se in start_end.split(',')]
# we cannot parse less than 1 and more than 2 slice indexes
if len(start_end) not in (1, 2):
return text, None
# we cannot parse slice indexes that are not numbers
if not all(se.isdigit() for se in start_end):
return text, None
start_end = [int(se) for se in start_end]
if len(start_end) == 1:
start = start_end[0]
end = None
else:
start, end = start_end
return key, (start, end)
def substring(s, start, end):
"""
Return a slice of s based on start and end indexes (that can be None).
"""
startless = start is None
endless = end is None
if startless and endless:
return s
if endless:
return s[start:]
if startless:
return s[:end]
return s[start:end]
def has_basic_pom_attributes(pom):
"""
Return True if a POM object has basic attributes needed to make this
a POM.
"""
basics = pom.model_version and pom.group_id and pom.artifact_id
if TRACE and not basics:
logger.debug(
'has_basic_pom_attributes: not a POM, incomplete GAV: '
'"{}":"{}":"{}"'.format(pom.model_version, pom.group_id, pom.artifact_id))
return basics
def get_maven_pom(location=None, text=None):
"""
Return a MavenPom object from a POM file at `location` or provided as a
`text` string.
"""
pom = MavenPom(location=location, text=text)
extra_properties = {}
# do we have a pom.properties file side-by-side?
# FIXME: we should treat pom.properties as a datafile
if location and os.path.exists(location):
parent = fileutils.parent_directory(location)
pom_properties = os.path.join(parent, 'pom.properties')