| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# SPDX-FileCopyrightText: 2016 Olivier Churlaud <[email protected]> |
| 4 |
# SPDX-FileCopyrightText: 2014 Alex Merry <[email protected]> |
| 5 |
# SPDX-FileCopyrightText: 2014 Aurélien Gâteau <[email protected]> |
| 6 |
# SPDX-FileCopyrightText: 2014 Alex Turbov <[email protected]> |
| 7 |
# |
| 8 |
# SPDX-License-Identifier: BSD-2-Clause |
| 9 |
|
| 10 |
import logging |
| 11 |
import os |
| 12 |
import sys |
| 13 |
from typing import Any, Dict, Optional |
| 14 |
|
| 15 |
from urllib.request import Request, urlopen |
| 16 |
from urllib.error import HTTPError |
| 17 |
|
| 18 |
import yaml |
| 19 |
|
| 20 |
from kapidox import utils |
| 21 |
from kapidox.models import Library, Product |
| 22 |
|
| 23 |
__all__ = ( |
| 24 |
"create_metainfo", |
| 25 |
"parse_tree") |
| 26 |
|
| 27 |
PLATFORM_ALL = "All" |
| 28 |
PLATFORM_UNKNOWN = "UNKNOWN" |
| 29 |
|
| 30 |
|
| 31 |
## @package kapidox.preprocessing |
| 32 |
# |
| 33 |
# Preprocessing of the needed information. |
| 34 |
# |
| 35 |
# The module allow to walk through folders, read metainfo files and create |
| 36 |
# products, subgroups and libraries representing the projects. |
| 37 |
# |
| 38 |
|
| 39 |
def expand_platform_all(dct, available_platforms): |
| 40 |
"""If one of the keys of dct is `PLATFORM_ALL` (or `PLATFORM_UNKNOWN`), |
| 41 |
remove it and add entries for all available platforms to dct |
| 42 |
|
| 43 |
Args: |
| 44 |
dct: (dictionary) dictionary to expand |
| 45 |
available_platforms: (list of string) name of platforms |
| 46 |
""" |
| 47 |
|
| 48 |
add_all_platforms = False |
| 49 |
if PLATFORM_ALL in dct: |
| 50 |
note = dct[PLATFORM_ALL] |
| 51 |
add_all_platforms = True |
| 52 |
del dct[PLATFORM_ALL] |
| 53 |
if PLATFORM_UNKNOWN in dct: |
| 54 |
add_all_platforms = True |
| 55 |
note = dct[PLATFORM_UNKNOWN] |
| 56 |
del dct[PLATFORM_UNKNOWN] |
| 57 |
if add_all_platforms: |
| 58 |
for platform in available_platforms: |
| 59 |
if platform not in dct: |
| 60 |
dct[platform] = note |
| 61 |
|
| 62 |
|
| 63 |
def create_metainfo(path) -> Optional[Dict[str, Any]]: |
| 64 |
"""Look for a `metadata.yaml` file and create a dictionary out it. |
| 65 |
|
| 66 |
Args: |
| 67 |
path: (string) the current path to search. |
| 68 |
Returns: |
| 69 |
A dictionary containing all the parsed information, or `None` if it |
| 70 |
did not fulfill some conditions. |
| 71 |
""" |
| 72 |
|
| 73 |
metainfo: Optional[Dict[str, Any]] |
| 74 |
|
| 75 |
if not os.path.isdir(path): |
| 76 |
return None |
| 77 |
|
| 78 |
try: |
| 79 |
metainfo_file = os.path.join(path, 'metainfo.yaml') |
| 80 |
except UnicodeDecodeError as e: |
| 81 |
logging.warning(f"Unusual base path {path!r} for metainfo.yaml") |
| 82 |
return None |
| 83 |
if not os.path.isfile(metainfo_file): |
| 84 |
return None |
| 85 |
|
| 86 |
try: |
| 87 |
metainfo = yaml.safe_load(open(metainfo_file)) |
| 88 |
except Exception as e: |
| 89 |
print(e) |
| 90 |
logging.warning(f'Could not load metainfo.yaml for {path}, skipping it') |
| 91 |
return None |
| 92 |
|
| 93 |
if metainfo is None: |
| 94 |
logging.warning(f'Empty metainfo.yaml for {path}, skipping it') |
| 95 |
return None |
| 96 |
|
| 97 |
if 'subgroup' in metainfo and 'group' not in metainfo: |
| 98 |
logging.warning(f'Subgroup but no group in {path}, skipping it') |
| 99 |
return None |
| 100 |
|
| 101 |
# Suppose we get a relative path passed in (e.g. on the command-line, |
| 102 |
# path ".." because we're building the documentation in a subdirectory of a source |
| 103 |
# checkout) then we don't want dirname to be "..", but the name that |
| 104 |
# that resolves to. |
| 105 |
dirname = os.path.basename(os.path.abspath(path)) |
| 106 |
if 'fancyname' in metainfo: |
| 107 |
fancyname = metainfo['fancyname'] |
| 108 |
else: |
| 109 |
fancyname = utils.parse_fancyname(path) |
| 110 |
|
| 111 |
if not fancyname: |
| 112 |
logging.warning(f'Could not find fancy name for {path}, skipping it') |
| 113 |
return None |
| 114 |
# A fancyname has 1st char capitalized |
| 115 |
fancyname = fancyname[0].capitalize() + fancyname[1:] |
| 116 |
|
| 117 |
if 'repo_id' in metainfo: |
| 118 |
repo_id = metainfo['repo_id'] |
| 119 |
else: |
| 120 |
repo_id = dirname |
| 121 |
|
| 122 |
qdoc: bool = False |
| 123 |
|
| 124 |
if 'qdoc' in metainfo: |
| 125 |
qdoc = metainfo['qdoc'] |
| 126 |
|
| 127 |
metainfo.update({ |
| 128 |
'fancyname': fancyname, |
| 129 |
'name': dirname, |
| 130 |
'repo_id': repo_id, |
| 131 |
'public_lib': metainfo.get('public_lib', False), |
| 132 |
'dependency_diagram': None, |
| 133 |
'path': path, |
| 134 |
'qdoc': qdoc, |
| 135 |
}) |
| 136 |
|
| 137 |
# replace legacy platform names |
| 138 |
if 'platforms' in metainfo: |
| 139 |
platforms = metainfo['platforms'] |
| 140 |
for index, x in enumerate(platforms): |
| 141 |
if x['name'] == "MacOSX": |
| 142 |
x['name'] = "macOS" |
| 143 |
platforms[index] = x |
| 144 |
logging.warning('{fancyname} uses outdated platform name, please replace "MacOSX" with "macOS".' |
| 145 |
.format_map(metainfo)) |
| 146 |
metainfo.update({'platforms': platforms}) |
| 147 |
if 'group_info' in metainfo: |
| 148 |
group_info = metainfo['group_info'] |
| 149 |
if 'platforms' in group_info: |
| 150 |
platforms = group_info['platforms'] |
| 151 |
for index, x in enumerate(platforms): |
| 152 |
if "MacOSX" in x: |
| 153 |
x = x.replace("MacOSX", "macOS") |
| 154 |
platforms[index] = x |
| 155 |
logging.warning('Group {fancyname} uses outdated platform name, please replace "MacOSX" with "macOS".' |
| 156 |
.format_map(group_info)) |
| 157 |
group_info.update({'platforms': platforms}) |
| 158 |
|
| 159 |
return metainfo |
| 160 |
|
| 161 |
|
| 162 |
def parse_tree(rootdir): |
| 163 |
"""Recursively call create_metainfo() in subdirs of rootdir |
| 164 |
|
| 165 |
Args: |
| 166 |
rootdir: (string) Top level directory containing the libraries. |
| 167 |
|
| 168 |
Returns: |
| 169 |
A list of metainfo dictionary (see create_metainfo()). |
| 170 |
|
| 171 |
""" |
| 172 |
metalist = [] |
| 173 |
for path, dirs, _ in os.walk(rootdir, topdown=True): |
| 174 |
# We don't want to do the recursion in the dotdirs |
| 175 |
dirs[:] = [d for d in dirs if not d[0] == '.'] |
| 176 |
metainfo = create_metainfo(path) |
| 177 |
if metainfo is not None: |
| 178 |
# There was a metainfo.yaml, which means it was |
| 179 |
# the top of a checked-out repository. Stop processing, |
| 180 |
# because we do not support having repo B checked out (even |
| 181 |
# as a submodule) inside repo A. |
| 182 |
# |
| 183 |
# There are exceptions: messagelib (KDE PIM) contains |
| 184 |
# multiple subdirectories with their own metainfo.yaml, |
| 185 |
# which are listed as public sources. |
| 186 |
dirs[:] = [d for d in dirs if d in metainfo.get('public_source_dirs', [])] |
| 187 |
if metainfo['public_lib'] or 'group_info' in metainfo: |
| 188 |
metalist.append(metainfo) |
| 189 |
else: |
| 190 |
logging.warning('{name} has no public libraries'.format_map(metainfo)) |
| 191 |
|
| 192 |
return metalist |
| 193 |
|
| 194 |
|
| 195 |
def sort_metainfo(metalist, all_maintainers): |
| 196 |
"""Extract the structure (Product/Subproduct/Library) from the metainfo |
| 197 |
list. |
| 198 |
|
| 199 |
Args: |
| 200 |
metalist: (list of dict) lists of the metainfo extracted in parse_tree(). |
| 201 |
all_maintainers: (dict of dict) all possible maintainers. |
| 202 |
|
| 203 |
Returns: |
| 204 |
A list of Products, a list of groups (which are products containing |
| 205 |
several libraries), a list of Libraries and the available platforms. |
| 206 |
""" |
| 207 |
products = dict() |
| 208 |
groups = [] |
| 209 |
libraries = [] |
| 210 |
available_platforms = {'Windows', 'macOS', 'Linux', 'Android', 'FreeBSD'} |
| 211 |
|
| 212 |
# First extract the structural info |
| 213 |
for metainfo in metalist: |
| 214 |
product = extract_product(metainfo, all_maintainers) |
| 215 |
if product is not None: |
| 216 |
products[product.name] = product |
| 217 |
|
| 218 |
# Second extract the libraries |
| 219 |
for metainfo in metalist: |
| 220 |
try: |
| 221 |
platforms = metainfo['platforms'] |
| 222 |
platform_lst = [x['name'] for x in platforms |
| 223 |
if x['name'] not in (PLATFORM_ALL, |
| 224 |
PLATFORM_UNKNOWN)] |
| 225 |
|
| 226 |
available_platforms.update(set(platform_lst)) |
| 227 |
except (KeyError, TypeError): |
| 228 |
logging.warning('{fancyname} library lacks valid platform definitions' |
| 229 |
.format_map(metainfo)) |
| 230 |
platforms = [dict(name=PLATFORM_UNKNOWN)] |
| 231 |
|
| 232 |
dct = dict((x['name'], x.get('note', '')) for x in platforms) |
| 233 |
|
| 234 |
expand_platform_all(dct, available_platforms) |
| 235 |
platforms = dct |
| 236 |
|
| 237 |
if metainfo['public_lib']: |
| 238 |
lib = Library(metainfo, products, platforms, all_maintainers) |
| 239 |
libraries.append(lib) |
| 240 |
|
| 241 |
for key in products.copy(): |
| 242 |
if len(products[key].libraries) == 0: |
| 243 |
del products[key] |
| 244 |
elif products[key].part_of_group: |
| 245 |
groups.append(products[key]) |
| 246 |
|
| 247 |
return list(products.values()), groups, libraries, available_platforms |
| 248 |
|
| 249 |
|
| 250 |
def extract_product(metainfo, all_maintainers): |
| 251 |
"""Extract a product from a metainfo dictionary. |
| 252 |
|
| 253 |
Args: |
| 254 |
metainfo: (dict) metainfo created by the create_metainfo() function. |
| 255 |
all_maintainers: (dict of dict) all possible maintainers |
| 256 |
|
| 257 |
Returns: |
| 258 |
A Product or None if the metainfo does not describe a product. |
| 259 |
""" |
| 260 |
|
| 261 |
if 'group_info' not in metainfo and 'group' in metainfo: |
| 262 |
# This is not a product but a simple lib |
| 263 |
return None |
| 264 |
|
| 265 |
try: |
| 266 |
product = Product(metainfo, all_maintainers) |
| 267 |
return product |
| 268 |
except ValueError as e: |
| 269 |
logging.error(e) |
| 270 |
return None |