MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / parse_opam_from_text

Function parse_opam_from_text

src/packagedcode/opam.py:178–254  ·  view source on GitHub ↗

Return a mapping of package data collected from the opam OCaml package manifest ``text``.

(text)

Source from the content-addressed store, hash-verified

176
177
178def parse_opam_from_text(text):
179 """
180 Return a mapping of package data collected from the opam OCaml package
181 manifest ``text``.
182 """
183
184 opam_data = {}
185
186 lines = text.splitlines()
187 for i, line in enumerate(lines):
188 parsed_line = parse_file_line(line)
189 if not parsed_line:
190 continue
191 key = parsed_line.group('key').strip()
192 value = parsed_line.group('value').strip()
193 if key == 'description': # Get multiline description
194 value = ''
195 for cont in lines[i + 1:]:
196 value += ' ' + cont.strip()
197 if '"""' in cont:
198 break
199
200 opam_data[key] = clean_data(value)
201
202 if key == 'maintainer':
203 stripped_val = value.strip('["] ')
204 stripped_val = stripped_val.split('" "')
205 opam_data[key] = stripped_val
206 elif key == 'authors':
207 if '[' in line: # If authors are present in multiple lines
208 for authors in lines[i + 1:]:
209 value += ' ' + authors.strip()
210 if ']' in authors:
211 break
212 value = value.strip('["] ')
213 else:
214 value = clean_data(value)
215 value = value.split('" "')
216 opam_data[key] = value
217 elif key == 'depends': # Get multiline dependencies
218 value = []
219 for dep in lines[i + 1:]:
220 if ']' in dep:
221 break
222 parsed_dep = parse_dep(dep)
223 if parsed_dep:
224 version = parsed_dep.group('version').strip('{ } ').replace('"', '')
225 name = parsed_dep.group('name').strip()
226 value.append(dict(
227 purl=PackageURL(type='opam', name=name).to_string(),
228 version=version,
229 ))
230 opam_data[key] = value
231
232 elif key == 'src': # Get multiline src
233 if not value:
234 value = lines[i + 1].strip()
235 opam_data[key] = clean_data(value)

Callers 1

parse_opamFunction · 0.85

Calls 6

clean_dataFunction · 0.85
groupMethod · 0.80
to_stringMethod · 0.80
splitMethod · 0.45
replaceMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected