annotate lib/python3.8/site-packages/pip/_vendor/distlib/metadata.py @ 0:9e54283cc701 draft

"planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
author guerler
date Mon, 27 Jul 2020 03:47:31 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1 # -*- coding: utf-8 -*-
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
2 #
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
3 # Copyright (C) 2012 The Python Software Foundation.
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
4 # See LICENSE.txt and CONTRIBUTORS.txt.
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
5 #
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
6 """Implementation of the Metadata for Python packages PEPs.
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
7
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
8 Supports all metadata formats (1.0, 1.1, 1.2, and 2.0 experimental).
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
9 """
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
10 from __future__ import unicode_literals
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
11
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
12 import codecs
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
13 from email import message_from_file
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
14 import json
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
15 import logging
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
16 import re
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
17
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
18
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
19 from . import DistlibException, __version__
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
20 from .compat import StringIO, string_types, text_type
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
21 from .markers import interpret
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
22 from .util import extract_by_key, get_extras
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
23 from .version import get_scheme, PEP440_VERSION_RE
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
24
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
25 logger = logging.getLogger(__name__)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
26
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
27
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
28 class MetadataMissingError(DistlibException):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
29 """A required metadata is missing"""
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
30
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
31
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
32 class MetadataConflictError(DistlibException):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
33 """Attempt to read or write metadata fields that are conflictual."""
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
34
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
35
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
36 class MetadataUnrecognizedVersionError(DistlibException):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
37 """Unknown metadata version number."""
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
38
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
39
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
40 class MetadataInvalidError(DistlibException):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
41 """A metadata value is invalid"""
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
42
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
43 # public API of this module
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
44 __all__ = ['Metadata', 'PKG_INFO_ENCODING', 'PKG_INFO_PREFERRED_VERSION']
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
45
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
46 # Encoding used for the PKG-INFO files
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
47 PKG_INFO_ENCODING = 'utf-8'
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
48
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
49 # preferred version. Hopefully will be changed
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
50 # to 1.2 once PEP 345 is supported everywhere
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
51 PKG_INFO_PREFERRED_VERSION = '1.1'
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
52
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
53 _LINE_PREFIX_1_2 = re.compile('\n \\|')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
54 _LINE_PREFIX_PRE_1_2 = re.compile('\n ')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
55 _241_FIELDS = ('Metadata-Version', 'Name', 'Version', 'Platform',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
56 'Summary', 'Description',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
57 'Keywords', 'Home-page', 'Author', 'Author-email',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
58 'License')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
59
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
60 _314_FIELDS = ('Metadata-Version', 'Name', 'Version', 'Platform',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
61 'Supported-Platform', 'Summary', 'Description',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
62 'Keywords', 'Home-page', 'Author', 'Author-email',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
63 'License', 'Classifier', 'Download-URL', 'Obsoletes',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
64 'Provides', 'Requires')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
65
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
66 _314_MARKERS = ('Obsoletes', 'Provides', 'Requires', 'Classifier',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
67 'Download-URL')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
68
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
69 _345_FIELDS = ('Metadata-Version', 'Name', 'Version', 'Platform',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
70 'Supported-Platform', 'Summary', 'Description',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
71 'Keywords', 'Home-page', 'Author', 'Author-email',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
72 'Maintainer', 'Maintainer-email', 'License',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
73 'Classifier', 'Download-URL', 'Obsoletes-Dist',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
74 'Project-URL', 'Provides-Dist', 'Requires-Dist',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
75 'Requires-Python', 'Requires-External')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
76
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
77 _345_MARKERS = ('Provides-Dist', 'Requires-Dist', 'Requires-Python',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
78 'Obsoletes-Dist', 'Requires-External', 'Maintainer',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
79 'Maintainer-email', 'Project-URL')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
80
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
81 _426_FIELDS = ('Metadata-Version', 'Name', 'Version', 'Platform',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
82 'Supported-Platform', 'Summary', 'Description',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
83 'Keywords', 'Home-page', 'Author', 'Author-email',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
84 'Maintainer', 'Maintainer-email', 'License',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
85 'Classifier', 'Download-URL', 'Obsoletes-Dist',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
86 'Project-URL', 'Provides-Dist', 'Requires-Dist',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
87 'Requires-Python', 'Requires-External', 'Private-Version',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
88 'Obsoleted-By', 'Setup-Requires-Dist', 'Extension',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
89 'Provides-Extra')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
90
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
91 _426_MARKERS = ('Private-Version', 'Provides-Extra', 'Obsoleted-By',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
92 'Setup-Requires-Dist', 'Extension')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
93
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
94 # See issue #106: Sometimes 'Requires' and 'Provides' occur wrongly in
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
95 # the metadata. Include them in the tuple literal below to allow them
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
96 # (for now).
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
97 _566_FIELDS = _426_FIELDS + ('Description-Content-Type',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
98 'Requires', 'Provides')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
99
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
100 _566_MARKERS = ('Description-Content-Type',)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
101
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
102 _ALL_FIELDS = set()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
103 _ALL_FIELDS.update(_241_FIELDS)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
104 _ALL_FIELDS.update(_314_FIELDS)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
105 _ALL_FIELDS.update(_345_FIELDS)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
106 _ALL_FIELDS.update(_426_FIELDS)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
107 _ALL_FIELDS.update(_566_FIELDS)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
108
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
109 EXTRA_RE = re.compile(r'''extra\s*==\s*("([^"]+)"|'([^']+)')''')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
110
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
111
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
112 def _version2fieldlist(version):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
113 if version == '1.0':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
114 return _241_FIELDS
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
115 elif version == '1.1':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
116 return _314_FIELDS
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
117 elif version == '1.2':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
118 return _345_FIELDS
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
119 elif version in ('1.3', '2.1'):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
120 return _345_FIELDS + _566_FIELDS
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
121 elif version == '2.0':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
122 return _426_FIELDS
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
123 raise MetadataUnrecognizedVersionError(version)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
124
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
125
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
126 def _best_version(fields):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
127 """Detect the best version depending on the fields used."""
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
128 def _has_marker(keys, markers):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
129 for marker in markers:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
130 if marker in keys:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
131 return True
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
132 return False
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
133
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
134 keys = []
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
135 for key, value in fields.items():
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
136 if value in ([], 'UNKNOWN', None):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
137 continue
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
138 keys.append(key)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
139
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
140 possible_versions = ['1.0', '1.1', '1.2', '1.3', '2.0', '2.1']
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
141
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
142 # first let's try to see if a field is not part of one of the version
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
143 for key in keys:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
144 if key not in _241_FIELDS and '1.0' in possible_versions:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
145 possible_versions.remove('1.0')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
146 logger.debug('Removed 1.0 due to %s', key)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
147 if key not in _314_FIELDS and '1.1' in possible_versions:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
148 possible_versions.remove('1.1')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
149 logger.debug('Removed 1.1 due to %s', key)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
150 if key not in _345_FIELDS and '1.2' in possible_versions:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
151 possible_versions.remove('1.2')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
152 logger.debug('Removed 1.2 due to %s', key)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
153 if key not in _566_FIELDS and '1.3' in possible_versions:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
154 possible_versions.remove('1.3')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
155 logger.debug('Removed 1.3 due to %s', key)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
156 if key not in _566_FIELDS and '2.1' in possible_versions:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
157 if key != 'Description': # In 2.1, description allowed after headers
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
158 possible_versions.remove('2.1')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
159 logger.debug('Removed 2.1 due to %s', key)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
160 if key not in _426_FIELDS and '2.0' in possible_versions:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
161 possible_versions.remove('2.0')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
162 logger.debug('Removed 2.0 due to %s', key)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
163
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
164 # possible_version contains qualified versions
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
165 if len(possible_versions) == 1:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
166 return possible_versions[0] # found !
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
167 elif len(possible_versions) == 0:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
168 logger.debug('Out of options - unknown metadata set: %s', fields)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
169 raise MetadataConflictError('Unknown metadata set')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
170
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
171 # let's see if one unique marker is found
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
172 is_1_1 = '1.1' in possible_versions and _has_marker(keys, _314_MARKERS)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
173 is_1_2 = '1.2' in possible_versions and _has_marker(keys, _345_MARKERS)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
174 is_2_1 = '2.1' in possible_versions and _has_marker(keys, _566_MARKERS)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
175 is_2_0 = '2.0' in possible_versions and _has_marker(keys, _426_MARKERS)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
176 if int(is_1_1) + int(is_1_2) + int(is_2_1) + int(is_2_0) > 1:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
177 raise MetadataConflictError('You used incompatible 1.1/1.2/2.0/2.1 fields')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
178
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
179 # we have the choice, 1.0, or 1.2, or 2.0
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
180 # - 1.0 has a broken Summary field but works with all tools
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
181 # - 1.1 is to avoid
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
182 # - 1.2 fixes Summary but has little adoption
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
183 # - 2.0 adds more features and is very new
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
184 if not is_1_1 and not is_1_2 and not is_2_1 and not is_2_0:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
185 # we couldn't find any specific marker
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
186 if PKG_INFO_PREFERRED_VERSION in possible_versions:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
187 return PKG_INFO_PREFERRED_VERSION
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
188 if is_1_1:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
189 return '1.1'
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
190 if is_1_2:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
191 return '1.2'
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
192 if is_2_1:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
193 return '2.1'
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
194
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
195 return '2.0'
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
196
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
197 _ATTR2FIELD = {
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
198 'metadata_version': 'Metadata-Version',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
199 'name': 'Name',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
200 'version': 'Version',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
201 'platform': 'Platform',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
202 'supported_platform': 'Supported-Platform',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
203 'summary': 'Summary',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
204 'description': 'Description',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
205 'keywords': 'Keywords',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
206 'home_page': 'Home-page',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
207 'author': 'Author',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
208 'author_email': 'Author-email',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
209 'maintainer': 'Maintainer',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
210 'maintainer_email': 'Maintainer-email',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
211 'license': 'License',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
212 'classifier': 'Classifier',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
213 'download_url': 'Download-URL',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
214 'obsoletes_dist': 'Obsoletes-Dist',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
215 'provides_dist': 'Provides-Dist',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
216 'requires_dist': 'Requires-Dist',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
217 'setup_requires_dist': 'Setup-Requires-Dist',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
218 'requires_python': 'Requires-Python',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
219 'requires_external': 'Requires-External',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
220 'requires': 'Requires',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
221 'provides': 'Provides',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
222 'obsoletes': 'Obsoletes',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
223 'project_url': 'Project-URL',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
224 'private_version': 'Private-Version',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
225 'obsoleted_by': 'Obsoleted-By',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
226 'extension': 'Extension',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
227 'provides_extra': 'Provides-Extra',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
228 }
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
229
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
230 _PREDICATE_FIELDS = ('Requires-Dist', 'Obsoletes-Dist', 'Provides-Dist')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
231 _VERSIONS_FIELDS = ('Requires-Python',)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
232 _VERSION_FIELDS = ('Version',)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
233 _LISTFIELDS = ('Platform', 'Classifier', 'Obsoletes',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
234 'Requires', 'Provides', 'Obsoletes-Dist',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
235 'Provides-Dist', 'Requires-Dist', 'Requires-External',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
236 'Project-URL', 'Supported-Platform', 'Setup-Requires-Dist',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
237 'Provides-Extra', 'Extension')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
238 _LISTTUPLEFIELDS = ('Project-URL',)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
239
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
240 _ELEMENTSFIELD = ('Keywords',)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
241
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
242 _UNICODEFIELDS = ('Author', 'Maintainer', 'Summary', 'Description')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
243
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
244 _MISSING = object()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
245
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
246 _FILESAFE = re.compile('[^A-Za-z0-9.]+')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
247
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
248
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
249 def _get_name_and_version(name, version, for_filename=False):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
250 """Return the distribution name with version.
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
251
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
252 If for_filename is true, return a filename-escaped form."""
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
253 if for_filename:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
254 # For both name and version any runs of non-alphanumeric or '.'
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
255 # characters are replaced with a single '-'. Additionally any
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
256 # spaces in the version string become '.'
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
257 name = _FILESAFE.sub('-', name)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
258 version = _FILESAFE.sub('-', version.replace(' ', '.'))
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
259 return '%s-%s' % (name, version)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
260
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
261
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
262 class LegacyMetadata(object):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
263 """The legacy metadata of a release.
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
264
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
265 Supports versions 1.0, 1.1 and 1.2 (auto-detected). You can
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
266 instantiate the class with one of these arguments (or none):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
267 - *path*, the path to a metadata file
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
268 - *fileobj* give a file-like object with metadata as content
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
269 - *mapping* is a dict-like object
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
270 - *scheme* is a version scheme name
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
271 """
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
272 # TODO document the mapping API and UNKNOWN default key
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
273
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
274 def __init__(self, path=None, fileobj=None, mapping=None,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
275 scheme='default'):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
276 if [path, fileobj, mapping].count(None) < 2:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
277 raise TypeError('path, fileobj and mapping are exclusive')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
278 self._fields = {}
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
279 self.requires_files = []
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
280 self._dependencies = None
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
281 self.scheme = scheme
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
282 if path is not None:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
283 self.read(path)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
284 elif fileobj is not None:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
285 self.read_file(fileobj)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
286 elif mapping is not None:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
287 self.update(mapping)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
288 self.set_metadata_version()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
289
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
290 def set_metadata_version(self):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
291 self._fields['Metadata-Version'] = _best_version(self._fields)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
292
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
293 def _write_field(self, fileobj, name, value):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
294 fileobj.write('%s: %s\n' % (name, value))
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
295
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
296 def __getitem__(self, name):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
297 return self.get(name)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
298
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
299 def __setitem__(self, name, value):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
300 return self.set(name, value)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
301
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
302 def __delitem__(self, name):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
303 field_name = self._convert_name(name)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
304 try:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
305 del self._fields[field_name]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
306 except KeyError:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
307 raise KeyError(name)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
308
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
309 def __contains__(self, name):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
310 return (name in self._fields or
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
311 self._convert_name(name) in self._fields)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
312
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
313 def _convert_name(self, name):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
314 if name in _ALL_FIELDS:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
315 return name
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
316 name = name.replace('-', '_').lower()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
317 return _ATTR2FIELD.get(name, name)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
318
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
319 def _default_value(self, name):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
320 if name in _LISTFIELDS or name in _ELEMENTSFIELD:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
321 return []
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
322 return 'UNKNOWN'
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
323
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
324 def _remove_line_prefix(self, value):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
325 if self.metadata_version in ('1.0', '1.1'):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
326 return _LINE_PREFIX_PRE_1_2.sub('\n', value)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
327 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
328 return _LINE_PREFIX_1_2.sub('\n', value)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
329
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
330 def __getattr__(self, name):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
331 if name in _ATTR2FIELD:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
332 return self[name]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
333 raise AttributeError(name)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
334
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
335 #
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
336 # Public API
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
337 #
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
338
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
339 # dependencies = property(_get_dependencies, _set_dependencies)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
340
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
341 def get_fullname(self, filesafe=False):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
342 """Return the distribution name with version.
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
343
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
344 If filesafe is true, return a filename-escaped form."""
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
345 return _get_name_and_version(self['Name'], self['Version'], filesafe)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
346
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
347 def is_field(self, name):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
348 """return True if name is a valid metadata key"""
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
349 name = self._convert_name(name)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
350 return name in _ALL_FIELDS
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
351
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
352 def is_multi_field(self, name):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
353 name = self._convert_name(name)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
354 return name in _LISTFIELDS
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
355
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
356 def read(self, filepath):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
357 """Read the metadata values from a file path."""
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
358 fp = codecs.open(filepath, 'r', encoding='utf-8')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
359 try:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
360 self.read_file(fp)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
361 finally:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
362 fp.close()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
363
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
364 def read_file(self, fileob):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
365 """Read the metadata values from a file object."""
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
366 msg = message_from_file(fileob)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
367 self._fields['Metadata-Version'] = msg['metadata-version']
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
368
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
369 # When reading, get all the fields we can
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
370 for field in _ALL_FIELDS:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
371 if field not in msg:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
372 continue
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
373 if field in _LISTFIELDS:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
374 # we can have multiple lines
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
375 values = msg.get_all(field)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
376 if field in _LISTTUPLEFIELDS and values is not None:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
377 values = [tuple(value.split(',')) for value in values]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
378 self.set(field, values)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
379 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
380 # single line
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
381 value = msg[field]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
382 if value is not None and value != 'UNKNOWN':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
383 self.set(field, value)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
384 # logger.debug('Attempting to set metadata for %s', self)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
385 # self.set_metadata_version()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
386
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
387 def write(self, filepath, skip_unknown=False):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
388 """Write the metadata fields to filepath."""
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
389 fp = codecs.open(filepath, 'w', encoding='utf-8')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
390 try:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
391 self.write_file(fp, skip_unknown)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
392 finally:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
393 fp.close()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
394
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
395 def write_file(self, fileobject, skip_unknown=False):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
396 """Write the PKG-INFO format data to a file object."""
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
397 self.set_metadata_version()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
398
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
399 for field in _version2fieldlist(self['Metadata-Version']):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
400 values = self.get(field)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
401 if skip_unknown and values in ('UNKNOWN', [], ['UNKNOWN']):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
402 continue
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
403 if field in _ELEMENTSFIELD:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
404 self._write_field(fileobject, field, ','.join(values))
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
405 continue
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
406 if field not in _LISTFIELDS:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
407 if field == 'Description':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
408 if self.metadata_version in ('1.0', '1.1'):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
409 values = values.replace('\n', '\n ')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
410 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
411 values = values.replace('\n', '\n |')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
412 values = [values]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
413
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
414 if field in _LISTTUPLEFIELDS:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
415 values = [','.join(value) for value in values]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
416
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
417 for value in values:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
418 self._write_field(fileobject, field, value)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
419
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
420 def update(self, other=None, **kwargs):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
421 """Set metadata values from the given iterable `other` and kwargs.
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
422
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
423 Behavior is like `dict.update`: If `other` has a ``keys`` method,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
424 they are looped over and ``self[key]`` is assigned ``other[key]``.
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
425 Else, ``other`` is an iterable of ``(key, value)`` iterables.
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
426
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
427 Keys that don't match a metadata field or that have an empty value are
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
428 dropped.
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
429 """
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
430 def _set(key, value):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
431 if key in _ATTR2FIELD and value:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
432 self.set(self._convert_name(key), value)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
433
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
434 if not other:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
435 # other is None or empty container
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
436 pass
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
437 elif hasattr(other, 'keys'):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
438 for k in other.keys():
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
439 _set(k, other[k])
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
440 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
441 for k, v in other:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
442 _set(k, v)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
443
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
444 if kwargs:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
445 for k, v in kwargs.items():
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
446 _set(k, v)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
447
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
448 def set(self, name, value):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
449 """Control then set a metadata field."""
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
450 name = self._convert_name(name)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
451
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
452 if ((name in _ELEMENTSFIELD or name == 'Platform') and
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
453 not isinstance(value, (list, tuple))):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
454 if isinstance(value, string_types):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
455 value = [v.strip() for v in value.split(',')]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
456 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
457 value = []
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
458 elif (name in _LISTFIELDS and
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
459 not isinstance(value, (list, tuple))):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
460 if isinstance(value, string_types):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
461 value = [value]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
462 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
463 value = []
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
464
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
465 if logger.isEnabledFor(logging.WARNING):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
466 project_name = self['Name']
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
467
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
468 scheme = get_scheme(self.scheme)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
469 if name in _PREDICATE_FIELDS and value is not None:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
470 for v in value:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
471 # check that the values are valid
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
472 if not scheme.is_valid_matcher(v.split(';')[0]):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
473 logger.warning(
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
474 "'%s': '%s' is not valid (field '%s')",
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
475 project_name, v, name)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
476 # FIXME this rejects UNKNOWN, is that right?
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
477 elif name in _VERSIONS_FIELDS and value is not None:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
478 if not scheme.is_valid_constraint_list(value):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
479 logger.warning("'%s': '%s' is not a valid version (field '%s')",
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
480 project_name, value, name)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
481 elif name in _VERSION_FIELDS and value is not None:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
482 if not scheme.is_valid_version(value):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
483 logger.warning("'%s': '%s' is not a valid version (field '%s')",
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
484 project_name, value, name)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
485
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
486 if name in _UNICODEFIELDS:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
487 if name == 'Description':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
488 value = self._remove_line_prefix(value)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
489
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
490 self._fields[name] = value
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
491
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
492 def get(self, name, default=_MISSING):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
493 """Get a metadata field."""
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
494 name = self._convert_name(name)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
495 if name not in self._fields:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
496 if default is _MISSING:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
497 default = self._default_value(name)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
498 return default
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
499 if name in _UNICODEFIELDS:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
500 value = self._fields[name]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
501 return value
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
502 elif name in _LISTFIELDS:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
503 value = self._fields[name]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
504 if value is None:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
505 return []
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
506 res = []
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
507 for val in value:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
508 if name not in _LISTTUPLEFIELDS:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
509 res.append(val)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
510 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
511 # That's for Project-URL
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
512 res.append((val[0], val[1]))
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
513 return res
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
514
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
515 elif name in _ELEMENTSFIELD:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
516 value = self._fields[name]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
517 if isinstance(value, string_types):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
518 return value.split(',')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
519 return self._fields[name]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
520
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
521 def check(self, strict=False):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
522 """Check if the metadata is compliant. If strict is True then raise if
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
523 no Name or Version are provided"""
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
524 self.set_metadata_version()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
525
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
526 # XXX should check the versions (if the file was loaded)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
527 missing, warnings = [], []
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
528
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
529 for attr in ('Name', 'Version'): # required by PEP 345
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
530 if attr not in self:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
531 missing.append(attr)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
532
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
533 if strict and missing != []:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
534 msg = 'missing required metadata: %s' % ', '.join(missing)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
535 raise MetadataMissingError(msg)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
536
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
537 for attr in ('Home-page', 'Author'):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
538 if attr not in self:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
539 missing.append(attr)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
540
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
541 # checking metadata 1.2 (XXX needs to check 1.1, 1.0)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
542 if self['Metadata-Version'] != '1.2':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
543 return missing, warnings
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
544
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
545 scheme = get_scheme(self.scheme)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
546
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
547 def are_valid_constraints(value):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
548 for v in value:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
549 if not scheme.is_valid_matcher(v.split(';')[0]):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
550 return False
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
551 return True
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
552
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
553 for fields, controller in ((_PREDICATE_FIELDS, are_valid_constraints),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
554 (_VERSIONS_FIELDS,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
555 scheme.is_valid_constraint_list),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
556 (_VERSION_FIELDS,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
557 scheme.is_valid_version)):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
558 for field in fields:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
559 value = self.get(field, None)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
560 if value is not None and not controller(value):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
561 warnings.append("Wrong value for '%s': %s" % (field, value))
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
562
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
563 return missing, warnings
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
564
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
565 def todict(self, skip_missing=False):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
566 """Return fields as a dict.
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
567
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
568 Field names will be converted to use the underscore-lowercase style
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
569 instead of hyphen-mixed case (i.e. home_page instead of Home-page).
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
570 """
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
571 self.set_metadata_version()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
572
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
573 mapping_1_0 = (
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
574 ('metadata_version', 'Metadata-Version'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
575 ('name', 'Name'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
576 ('version', 'Version'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
577 ('summary', 'Summary'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
578 ('home_page', 'Home-page'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
579 ('author', 'Author'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
580 ('author_email', 'Author-email'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
581 ('license', 'License'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
582 ('description', 'Description'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
583 ('keywords', 'Keywords'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
584 ('platform', 'Platform'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
585 ('classifiers', 'Classifier'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
586 ('download_url', 'Download-URL'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
587 )
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
588
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
589 data = {}
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
590 for key, field_name in mapping_1_0:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
591 if not skip_missing or field_name in self._fields:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
592 data[key] = self[field_name]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
593
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
594 if self['Metadata-Version'] == '1.2':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
595 mapping_1_2 = (
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
596 ('requires_dist', 'Requires-Dist'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
597 ('requires_python', 'Requires-Python'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
598 ('requires_external', 'Requires-External'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
599 ('provides_dist', 'Provides-Dist'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
600 ('obsoletes_dist', 'Obsoletes-Dist'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
601 ('project_url', 'Project-URL'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
602 ('maintainer', 'Maintainer'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
603 ('maintainer_email', 'Maintainer-email'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
604 )
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
605 for key, field_name in mapping_1_2:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
606 if not skip_missing or field_name in self._fields:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
607 if key != 'project_url':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
608 data[key] = self[field_name]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
609 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
610 data[key] = [','.join(u) for u in self[field_name]]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
611
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
612 elif self['Metadata-Version'] == '1.1':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
613 mapping_1_1 = (
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
614 ('provides', 'Provides'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
615 ('requires', 'Requires'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
616 ('obsoletes', 'Obsoletes'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
617 )
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
618 for key, field_name in mapping_1_1:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
619 if not skip_missing or field_name in self._fields:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
620 data[key] = self[field_name]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
621
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
622 return data
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
623
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
624 def add_requirements(self, requirements):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
625 if self['Metadata-Version'] == '1.1':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
626 # we can't have 1.1 metadata *and* Setuptools requires
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
627 for field in ('Obsoletes', 'Requires', 'Provides'):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
628 if field in self:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
629 del self[field]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
630 self['Requires-Dist'] += requirements
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
631
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
632 # Mapping API
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
633 # TODO could add iter* variants
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
634
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
635 def keys(self):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
636 return list(_version2fieldlist(self['Metadata-Version']))
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
637
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
638 def __iter__(self):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
639 for key in self.keys():
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
640 yield key
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
641
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
642 def values(self):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
643 return [self[key] for key in self.keys()]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
644
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
645 def items(self):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
646 return [(key, self[key]) for key in self.keys()]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
647
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
648 def __repr__(self):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
649 return '<%s %s %s>' % (self.__class__.__name__, self.name,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
650 self.version)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
651
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
652
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
653 METADATA_FILENAME = 'pydist.json'
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
654 WHEEL_METADATA_FILENAME = 'metadata.json'
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
655 LEGACY_METADATA_FILENAME = 'METADATA'
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
656
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
657
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
658 class Metadata(object):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
659 """
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
660 The metadata of a release. This implementation uses 2.0 (JSON)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
661 metadata where possible. If not possible, it wraps a LegacyMetadata
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
662 instance which handles the key-value metadata format.
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
663 """
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
664
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
665 METADATA_VERSION_MATCHER = re.compile(r'^\d+(\.\d+)*$')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
666
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
667 NAME_MATCHER = re.compile('^[0-9A-Z]([0-9A-Z_.-]*[0-9A-Z])?$', re.I)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
668
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
669 VERSION_MATCHER = PEP440_VERSION_RE
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
670
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
671 SUMMARY_MATCHER = re.compile('.{1,2047}')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
672
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
673 METADATA_VERSION = '2.0'
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
674
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
675 GENERATOR = 'distlib (%s)' % __version__
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
676
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
677 MANDATORY_KEYS = {
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
678 'name': (),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
679 'version': (),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
680 'summary': ('legacy',),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
681 }
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
682
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
683 INDEX_KEYS = ('name version license summary description author '
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
684 'author_email keywords platform home_page classifiers '
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
685 'download_url')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
686
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
687 DEPENDENCY_KEYS = ('extras run_requires test_requires build_requires '
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
688 'dev_requires provides meta_requires obsoleted_by '
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
689 'supports_environments')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
690
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
691 SYNTAX_VALIDATORS = {
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
692 'metadata_version': (METADATA_VERSION_MATCHER, ()),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
693 'name': (NAME_MATCHER, ('legacy',)),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
694 'version': (VERSION_MATCHER, ('legacy',)),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
695 'summary': (SUMMARY_MATCHER, ('legacy',)),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
696 }
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
697
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
698 __slots__ = ('_legacy', '_data', 'scheme')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
699
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
700 def __init__(self, path=None, fileobj=None, mapping=None,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
701 scheme='default'):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
702 if [path, fileobj, mapping].count(None) < 2:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
703 raise TypeError('path, fileobj and mapping are exclusive')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
704 self._legacy = None
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
705 self._data = None
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
706 self.scheme = scheme
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
707 #import pdb; pdb.set_trace()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
708 if mapping is not None:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
709 try:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
710 self._validate_mapping(mapping, scheme)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
711 self._data = mapping
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
712 except MetadataUnrecognizedVersionError:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
713 self._legacy = LegacyMetadata(mapping=mapping, scheme=scheme)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
714 self.validate()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
715 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
716 data = None
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
717 if path:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
718 with open(path, 'rb') as f:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
719 data = f.read()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
720 elif fileobj:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
721 data = fileobj.read()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
722 if data is None:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
723 # Initialised with no args - to be added
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
724 self._data = {
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
725 'metadata_version': self.METADATA_VERSION,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
726 'generator': self.GENERATOR,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
727 }
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
728 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
729 if not isinstance(data, text_type):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
730 data = data.decode('utf-8')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
731 try:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
732 self._data = json.loads(data)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
733 self._validate_mapping(self._data, scheme)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
734 except ValueError:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
735 # Note: MetadataUnrecognizedVersionError does not
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
736 # inherit from ValueError (it's a DistlibException,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
737 # which should not inherit from ValueError).
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
738 # The ValueError comes from the json.load - if that
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
739 # succeeds and we get a validation error, we want
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
740 # that to propagate
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
741 self._legacy = LegacyMetadata(fileobj=StringIO(data),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
742 scheme=scheme)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
743 self.validate()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
744
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
745 common_keys = set(('name', 'version', 'license', 'keywords', 'summary'))
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
746
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
747 none_list = (None, list)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
748 none_dict = (None, dict)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
749
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
750 mapped_keys = {
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
751 'run_requires': ('Requires-Dist', list),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
752 'build_requires': ('Setup-Requires-Dist', list),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
753 'dev_requires': none_list,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
754 'test_requires': none_list,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
755 'meta_requires': none_list,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
756 'extras': ('Provides-Extra', list),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
757 'modules': none_list,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
758 'namespaces': none_list,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
759 'exports': none_dict,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
760 'commands': none_dict,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
761 'classifiers': ('Classifier', list),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
762 'source_url': ('Download-URL', None),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
763 'metadata_version': ('Metadata-Version', None),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
764 }
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
765
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
766 del none_list, none_dict
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
767
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
768 def __getattribute__(self, key):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
769 common = object.__getattribute__(self, 'common_keys')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
770 mapped = object.__getattribute__(self, 'mapped_keys')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
771 if key in mapped:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
772 lk, maker = mapped[key]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
773 if self._legacy:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
774 if lk is None:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
775 result = None if maker is None else maker()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
776 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
777 result = self._legacy.get(lk)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
778 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
779 value = None if maker is None else maker()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
780 if key not in ('commands', 'exports', 'modules', 'namespaces',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
781 'classifiers'):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
782 result = self._data.get(key, value)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
783 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
784 # special cases for PEP 459
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
785 sentinel = object()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
786 result = sentinel
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
787 d = self._data.get('extensions')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
788 if d:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
789 if key == 'commands':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
790 result = d.get('python.commands', value)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
791 elif key == 'classifiers':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
792 d = d.get('python.details')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
793 if d:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
794 result = d.get(key, value)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
795 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
796 d = d.get('python.exports')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
797 if not d:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
798 d = self._data.get('python.exports')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
799 if d:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
800 result = d.get(key, value)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
801 if result is sentinel:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
802 result = value
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
803 elif key not in common:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
804 result = object.__getattribute__(self, key)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
805 elif self._legacy:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
806 result = self._legacy.get(key)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
807 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
808 result = self._data.get(key)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
809 return result
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
810
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
811 def _validate_value(self, key, value, scheme=None):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
812 if key in self.SYNTAX_VALIDATORS:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
813 pattern, exclusions = self.SYNTAX_VALIDATORS[key]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
814 if (scheme or self.scheme) not in exclusions:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
815 m = pattern.match(value)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
816 if not m:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
817 raise MetadataInvalidError("'%s' is an invalid value for "
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
818 "the '%s' property" % (value,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
819 key))
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
820
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
821 def __setattr__(self, key, value):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
822 self._validate_value(key, value)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
823 common = object.__getattribute__(self, 'common_keys')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
824 mapped = object.__getattribute__(self, 'mapped_keys')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
825 if key in mapped:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
826 lk, _ = mapped[key]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
827 if self._legacy:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
828 if lk is None:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
829 raise NotImplementedError
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
830 self._legacy[lk] = value
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
831 elif key not in ('commands', 'exports', 'modules', 'namespaces',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
832 'classifiers'):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
833 self._data[key] = value
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
834 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
835 # special cases for PEP 459
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
836 d = self._data.setdefault('extensions', {})
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
837 if key == 'commands':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
838 d['python.commands'] = value
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
839 elif key == 'classifiers':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
840 d = d.setdefault('python.details', {})
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
841 d[key] = value
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
842 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
843 d = d.setdefault('python.exports', {})
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
844 d[key] = value
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
845 elif key not in common:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
846 object.__setattr__(self, key, value)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
847 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
848 if key == 'keywords':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
849 if isinstance(value, string_types):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
850 value = value.strip()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
851 if value:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
852 value = value.split()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
853 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
854 value = []
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
855 if self._legacy:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
856 self._legacy[key] = value
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
857 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
858 self._data[key] = value
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
859
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
860 @property
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
861 def name_and_version(self):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
862 return _get_name_and_version(self.name, self.version, True)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
863
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
864 @property
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
865 def provides(self):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
866 if self._legacy:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
867 result = self._legacy['Provides-Dist']
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
868 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
869 result = self._data.setdefault('provides', [])
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
870 s = '%s (%s)' % (self.name, self.version)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
871 if s not in result:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
872 result.append(s)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
873 return result
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
874
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
875 @provides.setter
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
876 def provides(self, value):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
877 if self._legacy:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
878 self._legacy['Provides-Dist'] = value
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
879 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
880 self._data['provides'] = value
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
881
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
882 def get_requirements(self, reqts, extras=None, env=None):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
883 """
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
884 Base method to get dependencies, given a set of extras
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
885 to satisfy and an optional environment context.
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
886 :param reqts: A list of sometimes-wanted dependencies,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
887 perhaps dependent on extras and environment.
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
888 :param extras: A list of optional components being requested.
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
889 :param env: An optional environment for marker evaluation.
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
890 """
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
891 if self._legacy:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
892 result = reqts
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
893 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
894 result = []
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
895 extras = get_extras(extras or [], self.extras)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
896 for d in reqts:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
897 if 'extra' not in d and 'environment' not in d:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
898 # unconditional
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
899 include = True
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
900 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
901 if 'extra' not in d:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
902 # Not extra-dependent - only environment-dependent
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
903 include = True
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
904 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
905 include = d.get('extra') in extras
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
906 if include:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
907 # Not excluded because of extras, check environment
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
908 marker = d.get('environment')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
909 if marker:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
910 include = interpret(marker, env)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
911 if include:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
912 result.extend(d['requires'])
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
913 for key in ('build', 'dev', 'test'):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
914 e = ':%s:' % key
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
915 if e in extras:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
916 extras.remove(e)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
917 # A recursive call, but it should terminate since 'test'
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
918 # has been removed from the extras
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
919 reqts = self._data.get('%s_requires' % key, [])
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
920 result.extend(self.get_requirements(reqts, extras=extras,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
921 env=env))
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
922 return result
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
923
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
924 @property
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
925 def dictionary(self):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
926 if self._legacy:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
927 return self._from_legacy()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
928 return self._data
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
929
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
930 @property
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
931 def dependencies(self):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
932 if self._legacy:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
933 raise NotImplementedError
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
934 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
935 return extract_by_key(self._data, self.DEPENDENCY_KEYS)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
936
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
937 @dependencies.setter
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
938 def dependencies(self, value):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
939 if self._legacy:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
940 raise NotImplementedError
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
941 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
942 self._data.update(value)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
943
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
944 def _validate_mapping(self, mapping, scheme):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
945 if mapping.get('metadata_version') != self.METADATA_VERSION:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
946 raise MetadataUnrecognizedVersionError()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
947 missing = []
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
948 for key, exclusions in self.MANDATORY_KEYS.items():
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
949 if key not in mapping:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
950 if scheme not in exclusions:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
951 missing.append(key)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
952 if missing:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
953 msg = 'Missing metadata items: %s' % ', '.join(missing)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
954 raise MetadataMissingError(msg)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
955 for k, v in mapping.items():
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
956 self._validate_value(k, v, scheme)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
957
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
958 def validate(self):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
959 if self._legacy:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
960 missing, warnings = self._legacy.check(True)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
961 if missing or warnings:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
962 logger.warning('Metadata: missing: %s, warnings: %s',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
963 missing, warnings)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
964 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
965 self._validate_mapping(self._data, self.scheme)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
966
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
967 def todict(self):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
968 if self._legacy:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
969 return self._legacy.todict(True)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
970 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
971 result = extract_by_key(self._data, self.INDEX_KEYS)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
972 return result
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
973
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
974 def _from_legacy(self):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
975 assert self._legacy and not self._data
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
976 result = {
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
977 'metadata_version': self.METADATA_VERSION,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
978 'generator': self.GENERATOR,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
979 }
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
980 lmd = self._legacy.todict(True) # skip missing ones
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
981 for k in ('name', 'version', 'license', 'summary', 'description',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
982 'classifier'):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
983 if k in lmd:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
984 if k == 'classifier':
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
985 nk = 'classifiers'
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
986 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
987 nk = k
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
988 result[nk] = lmd[k]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
989 kw = lmd.get('Keywords', [])
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
990 if kw == ['']:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
991 kw = []
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
992 result['keywords'] = kw
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
993 keys = (('requires_dist', 'run_requires'),
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
994 ('setup_requires_dist', 'build_requires'))
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
995 for ok, nk in keys:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
996 if ok in lmd and lmd[ok]:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
997 result[nk] = [{'requires': lmd[ok]}]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
998 result['provides'] = self.provides
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
999 author = {}
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1000 maintainer = {}
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1001 return result
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1002
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1003 LEGACY_MAPPING = {
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1004 'name': 'Name',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1005 'version': 'Version',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1006 'license': 'License',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1007 'summary': 'Summary',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1008 'description': 'Description',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1009 'classifiers': 'Classifier',
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1010 }
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1011
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1012 def _to_legacy(self):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1013 def process_entries(entries):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1014 reqts = set()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1015 for e in entries:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1016 extra = e.get('extra')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1017 env = e.get('environment')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1018 rlist = e['requires']
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1019 for r in rlist:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1020 if not env and not extra:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1021 reqts.add(r)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1022 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1023 marker = ''
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1024 if extra:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1025 marker = 'extra == "%s"' % extra
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1026 if env:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1027 if marker:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1028 marker = '(%s) and %s' % (env, marker)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1029 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1030 marker = env
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1031 reqts.add(';'.join((r, marker)))
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1032 return reqts
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1033
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1034 assert self._data and not self._legacy
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1035 result = LegacyMetadata()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1036 nmd = self._data
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1037 for nk, ok in self.LEGACY_MAPPING.items():
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1038 if nk in nmd:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1039 result[ok] = nmd[nk]
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1040 r1 = process_entries(self.run_requires + self.meta_requires)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1041 r2 = process_entries(self.build_requires + self.dev_requires)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1042 if self.extras:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1043 result['Provides-Extra'] = sorted(self.extras)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1044 result['Requires-Dist'] = sorted(r1)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1045 result['Setup-Requires-Dist'] = sorted(r2)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1046 # TODO: other fields such as contacts
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1047 return result
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1048
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1049 def write(self, path=None, fileobj=None, legacy=False, skip_unknown=True):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1050 if [path, fileobj].count(None) != 1:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1051 raise ValueError('Exactly one of path and fileobj is needed')
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1052 self.validate()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1053 if legacy:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1054 if self._legacy:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1055 legacy_md = self._legacy
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1056 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1057 legacy_md = self._to_legacy()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1058 if path:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1059 legacy_md.write(path, skip_unknown=skip_unknown)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1060 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1061 legacy_md.write_file(fileobj, skip_unknown=skip_unknown)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1062 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1063 if self._legacy:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1064 d = self._from_legacy()
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1065 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1066 d = self._data
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1067 if fileobj:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1068 json.dump(d, fileobj, ensure_ascii=True, indent=2,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1069 sort_keys=True)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1070 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1071 with codecs.open(path, 'w', 'utf-8') as f:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1072 json.dump(d, f, ensure_ascii=True, indent=2,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1073 sort_keys=True)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1074
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1075 def add_requirements(self, requirements):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1076 if self._legacy:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1077 self._legacy.add_requirements(requirements)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1078 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1079 run_requires = self._data.setdefault('run_requires', [])
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1080 always = None
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1081 for entry in run_requires:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1082 if 'environment' not in entry and 'extra' not in entry:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1083 always = entry
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1084 break
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1085 if always is None:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1086 always = { 'requires': requirements }
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1087 run_requires.insert(0, always)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1088 else:
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1089 rset = set(always['requires']) | set(requirements)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1090 always['requires'] = sorted(rset)
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1091
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1092 def __repr__(self):
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1093 name = self.name or '(no name)'
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1094 version = self.version or 'no version'
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1095 return '<%s %s %s (%s)>' % (self.__class__.__name__,
9e54283cc701 "planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
guerler
parents:
diff changeset
1096 self.metadata_version, name, version)