summaryrefslogtreecommitdiffstats
path: root/check.py
blob: eac897f4150c1553702a5aa2e46bff75aef259e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# -*- coding: utf8 -*-
# org.onap.vnfrqts/requirements
# ============LICENSE_START====================================================
# Copyright © 2018 AT&T Intellectual Property. All rights reserved.
#
# Unless otherwise specified, all software contained herein is licensed
# under the Apache License, Version 2.0 (the "License");
# you may not use this software except in compliance with the License.
# You may obtain a copy of the License at
#
#             http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Unless otherwise specified, all documentation contained herein is licensed
# under the Creative Commons License, Attribution 4.0 Intl. (the "License");
# you may not use this documentation except in compliance with the License.
# You may obtain a copy of the License at
#
#             https://creativecommons.org/licenses/by/4.0/
#
# Unless required by applicable law or agreed to in writing, documentation
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ============LICENSE_END============================================

"""
This script should be run before every commit to ensure proper
standards are being followed within the project.  The script
will also automatically fix certain issues when they are encountered, and
warn about other issues it cannot automatically resolve.

Warnings:
- Requirement missing required attributes
- Invalid values for attributes
- Invalid section header usage in any file
- :keyword: and requirement mismatch

Auto Updates:
- Assigning :id: on new requirements where an ID missing
- Adding :introduced: attribute on new requirements
- Adding/correcting :updated: attribute on changed requirements
"""

import os
import random
import re
import sys
from abc import ABC, abstractmethod
from collections import OrderedDict, deque
from pathlib import Path
from typing import Deque, List, Mapping, Callable, Set

import requests

THIS_DIR = Path(__file__).parent
CONF_PATH = THIS_DIR / "docs/conf.py"

NEEDS_JSON_URL = (
    "https://nexus.onap.org/service/local/repositories/raw/content"
    "/org.onap.vnfrqts.requirements/master/needs.json"
)

HEADING_LEVELS = ("-", "^", "~", "+", "*", '"')

SPACES = re.compile(r"\s+")
REQ_DIRECTIVE_PATTERN = re.compile(r"\.\.\s+req::.*")
ATTRIBUTE_PATTERN = re.compile(r"^\s+(:\w+:)\s+(.*)$")
VERSION_PATTERN = re.compile(r"version\s+=\s+'(.*?)'")

VALID_KEYWORDS = ("MUST", "MUST NOT", "SHOULD", "SHOULD NOT", "MAY", "MAY NOT")
VALID_VERSIONS = (
    "amsterdam",
    "beijing",
    "casablanca",
    "dublin",
    "el alto",
    "frankfurt",
    "guilin",
)
REQUIRED_ATTRIBUTES = (":keyword:", ":target:", ":id:")
VALID_TARGETS = (
    "VNF",
    "PNF",
    "VNF or PNF",
    "VNF DOCUMENTATION PACKAGE",
    "PNF DOCUMENTATION PACKAGE",
    "VNF or PNF DOCUMENTATION PACKAGE",
    "VNF PROVIDER",
    "PNF PROVIDER",
    "VNF or PNF PROVIDER",
    "VNF CSAR PACKAGE",
    "PNF CSAR PACKAGE",
    "VNF or PNF CSAR PACKAGE",
    "VNF HEAT PACKAGE",
)
VALID_VALIDATION_MODES = ("static", "none", "in_service")


def check(predicate: bool, msg: str):
    """
    Raises RuntimeError with given msg if predicate is False
    """
    if not predicate:
        raise RuntimeError(msg)


def get_version() -> str:
    """
    Returns the version value from conf.py
    """
    with open(CONF_PATH) as f:
        for line in f:
            m = VERSION_PATTERN.match(line)
            if m:
                version = m.groups()[0]
                if version not in VALID_VERSIONS:
                    print(
                        f"ERROR: {version} in conf.py is not defined in "
                        f"VALID_VERSIONS. Update the script to continue"
                    )
                    sys.exit(1)
                return version


VERSION = get_version()


def normalize(text: str):
    """
    Strips out formatting, line breaks, and repeated spaces to normalize
    the string for comparison.  This ensures minor formatting changes
    are not tagged as meaningful changes
    """
    s = text.lower()
    s = s.replace("\n", " ")
    s = re.sub(r'[`*\'"]', "", s)
    s = re.sub(r"\s+", " ", s)
    return s


def warn(path: str, msg: str, req: "RequirementDirective" = None):
    """
    Log a warning
    """
    req_id = req.requirement_id or "UNKNOWN" if req else "UNKNOWN"
    print(f"WARNING: {path} | {req_id} | {msg}")


class RequirementRepository:
    """
    Pulls needs.json and provides various options to interact with the data.
    """

    def __init__(self, data=None):
        self.data = data or requests.get(NEEDS_JSON_URL).json()
        self.all_ids = {
            r["id"]
            for version in self.data["versions"].values()
            for r in version["needs"].values()
        }

    @property
    def current_requirements(self) -> Mapping:
        """
        Returns the requirements specified by current_version in needs.json.
        """
        version = self.data["current_version"]
        return self.data["versions"][version]["needs"]

    @property
    def unique_targets(self) -> Set[str]:
        return {r["target"] for r in self.current_requirements.values()}

    @property
    def unique_validation_modes(self) -> Set[str]:
        return {r["validation_mode"] for r in self.current_requirements.values()}

    def create_id(self) -> str:
        """
        Generates a requirements ID that has not been used in any version
        of the requirements.
        """
        while True:
            new_id = "R-{:0>5d}".format(random.randint(0, 99999))
            if new_id in self.all_ids:
                continue  # skip this one and generate another one
            self.all_ids.add(new_id)
            return new_id

    def is_new_requirement(self, req: "RequirementDirective") -> bool:
        return req.requirement_id not in self.current_requirements

    def has_changed(self, req: "RequirementDirective") -> bool:
        """
        Returns True if the requirement already exists and the contents has
        meaningfully changed. Small changes in spaces or formatting are not considered.
        """
        current_req = self.current_requirements.get(req.requirement_id)
        if not current_req:
            return False
        return normalize(current_req["description"]) == normalize("".join(req.content))


class RequirementDirective:
    """
    Data structure to hold a .. req:: directive
    """

    ATTRIBUTE_ORDER = (
        ":id:",
        ":target:",
        ":keyword:",
        ":introduced:",
        ":updated:",
        ":validation_mode:",
        ":impacts:",
    )

    def __init__(self, path: str):
        self.path = path
        self.attributes = OrderedDict()
        self.content = []
        self.indent = None

    @property
    def requirement_id(self) -> str:
        return self.attributes.get(":id:", "")

    @requirement_id.setter
    def requirement_id(self, r_id: str):
        self._update(":id:", r_id)

    @property
    def keyword(self) -> str:
        return self.attributes.get(":keyword:", "")

    @keyword.setter
    def keyword(self, k: str):
        self._update(":keyword:", k)

    @property
    def target(self) -> str:
        return self.attributes.get(":target:", "")

    @target.setter
    def target(self, value: str):
        self._update(":target", value)

    @property
    def introduced(self) -> str:
        return self.attributes.get(":introduced:", "")

    @introduced.setter
    def introduced(self, version: str):
        self._update(":introduced:", version)

    @property
    def updated(self) -> str:
        return self.attributes.get(":updated:", "")

    @updated.setter
    def updated(self, version: str):
        self._update(":updated:", version)

    @property
    def validation_mode(self) -> str:
        return self.attributes.get(":validation_mode:", "")

    @validation_mode.setter
    def validation_mode(self, value: str):
        self._update(":validation_mode:", value)

    def parse(self, lines: Deque[str]):
        """
        Parses a ..req:: directive and populates the data structre
        """
        parsing_attrs = True
        while lines:
            line = lines.popleft()
            match = ATTRIBUTE_PATTERN.match(line) if parsing_attrs else None
            if match:
                self.indent = self.indent or self.calc_indent(line)
                attr, value = match.groups()
                self.attributes[attr] = value
            else:
                parsing_attrs = False  # passed attributes, capture content
                if line.strip() and self.calc_indent(line) < self.indent:
                    # past end of the directive so we'll put this line back
                    lines.appendleft(line)
                    break
                else:
                    self.content.append(line)

    def format_attributes(self) -> List[str]:
        """
        Converts a dict back to properly indented lines using ATTRIBUTE_ORDER
        """
        spaces = " " * self.indent
        attr_lines = []
        for key in self.ATTRIBUTE_ORDER:
            value = self.attributes.get(key)
            if value:
                attr_lines.append(f"{spaces}{key} {value}\n")
        return attr_lines

    @staticmethod
    def calc_indent(line: str) -> int:
        """
        Number of leading spaces of the line
        """
        return len(line) - len(line.lstrip())

    def __str__(self):
        return "".join(self.format_attributes() + self.content)

    def _notify(self, field, value):
        req_id = self.requirement_id or "UNKNOWN"
        print(f"UPDATE: {self.path} | {req_id} | Setting {field} to {value}")

    def _update(self, attr, value):
        self.attributes[attr] = value
        self._notify(attr, value)


class RequirementVisitor:
    """
    Walks a directory for reStructuredText files and and passes contents to
    visitors when the content is encountered.

    Types of visitors supported:

    - Requirement:    Take the path and a RequirementDirective which may be modified
                      If modified, the file will be updated using the modified directive
    - Post Processor: Take the path and all lines for processing; returning a
                      potentially changed set of lines
    """

    def __init__(
        self,
        req_visitors: List[Callable[[str, RequirementDirective], None]],
        post_processors: List[Callable[[str, List[str]], List[str]]],
    ):
        self.req_visitors = req_visitors or []
        self.post_processors = post_processors or []

    def process(self, root_dir: Path):
        """
        Walks the `root_dir` looking for rst to files to parse
        """
        for dir_path, sub_dirs, filenames in os.walk(root_dir.as_posix()):
            for filename in filenames:
                if filename.lower().endswith(".rst"):
                    self.handle_rst_file(os.path.join(dir_path, filename))

    @staticmethod
    def read(path):
        """Read file at `path` and return lines as list"""
        with open(path, "r") as f:
            return list(f)

    @staticmethod
    def write(path, content):
        """Write a content to the given path"""
        with open(path, "w") as f:
            for line in content:
                f.write(line)

    def handle_rst_file(self, path):
        """
        Parse the RST file notifying the registered visitors
        """
        lines = deque(self.read(path))
        new_contents = []
        while lines:
            line = lines.popleft()
            if self.is_req_directive(line):
                req = RequirementDirective(path)
                req.parse(lines)
                for func in self.req_visitors:
                    func(path, req)
                # Put the lines back for processing by the line visitor
                lines.extendleft(reversed(req.format_attributes() + req.content))
            new_contents.append(line)
        for processor in self.post_processors:
            new_contents = processor(path, new_contents) or new_contents
        self.write(path, new_contents)

    @staticmethod
    def is_req_directive(line):
        """Returns True if the line denotes the start of a needs directive"""
        return bool(REQ_DIRECTIVE_PATTERN.match(line))


class AbstractRequirementVisitor(ABC):
    @abstractmethod
    def __call__(self, path: str, req: RequirementDirective):
        raise NotImplementedError()


class MetadataFixer(AbstractRequirementVisitor):
    """
    Updates metadata based on the status of the requirement and contents of
    the metadata
    """

    def __init__(self, repos: RequirementRepository):
        self.repos = repos

    def __call__(self, path: str, req: RequirementDirective):
        if not req.requirement_id:
            req.requirement_id = self.repos.create_id()
        if self.repos.is_new_requirement(req) and req.introduced != VERSION:
            req.introduced = VERSION
        if self.repos.has_changed(req) and req.updated != VERSION:
            req.updated = VERSION


class MetadataValidator(AbstractRequirementVisitor):
    def __init__(self, repos: RequirementRepository):
        self.repos = repos

    def __call__(self, path: str, req: RequirementDirective):
        for attr in REQUIRED_ATTRIBUTES:
            if attr not in req.attributes:
                warn(path, f"Missing required attribute {attr}", req)
        if req.keyword and req.keyword not in VALID_KEYWORDS:
            warn(path, f"Invalid :keyword: value ({req.keyword})", req)
        if repository.is_new_requirement(req) and req.introduced != VERSION:
            warn(path, f":introduced: is not {VERSION} on new requirement", req)
        if req.introduced and req.introduced not in VALID_VERSIONS:
            warn(path, f"Invalid :introduced: value ({req.introduced})", req)
        if req.updated and req.updated not in VALID_VERSIONS:
            warn(path, f"Invalid :updated: value ({req.updated})", req)
        if req.target and req.target not in VALID_TARGETS:
            warn(path, f"Invalid :target: value ({req.target})", req)
        if req.validation_mode and req.validation_mode not in VALID_VALIDATION_MODES:
            warn(path, f"Invalid :validation_mode: value ({req.validation_mode})", req)


def check_section_headers(path: str, lines: List[str]) -> List[str]:
    """
    Ensure hierarchy of section headers follows the expected progression as defined
    by `HEADING_LEVELS`, and that section heading marks match the length of the
    section title.
    """
    current_heading_level = 0
    for i, line in enumerate(lines):
        if any(line.startswith(char * 3) for char in HEADING_LEVELS):
            # heading level should go down, stay the same, or be next level
            expected = HEADING_LEVELS[0 : current_heading_level + 2]
            if line[0] not in expected:
                warn(
                    path,
                    f"Unexpected heading char ({line[0]}) on line {i+1}. "
                    f"Expected one of {' '.join(expected)}",
                )
            if len(line.strip()) != len(lines[i - 1].strip()):
                lines[i] = (line[0] * len(lines[i - 1].strip())) + "\n"
                print(
                    f"UPDATE: {path} | Matching section mark to title length "
                    f"on line {i+1}"
                )
            current_heading_level = HEADING_LEVELS.index(line[0])
    return lines


def check_keyword_text_alignment(path: str, req: RequirementDirective):
    if not req.keyword:
        return req
    keyword = f"**{req.keyword}**"
    if not any(keyword in line for line in req.content):
        warn(path, f"Keyword is {req.keyword}, but {keyword} not in requirement", req)


if __name__ == "__main__":
    print("Valid Versions")
    print("-----------------------")
    print("\n".join(VALID_VERSIONS))
    print()
    print("Valid Keywords")
    print("-----------------------")
    print("\n".join(VALID_KEYWORDS))
    print()
    print("Valid Targets")
    print("-----------------------")
    print("\n".join(VALID_TARGETS))
    print()
    print("Valid Validation Modes")
    print("-----------------------")
    print("\n".join(VALID_VALIDATION_MODES))
    print()
    print("Check-up Report")
    print("-" * 100)
    repository = RequirementRepository()
    visitor = RequirementVisitor(
        req_visitors=[
            MetadataFixer(repository),
            MetadataValidator(repository),
            check_keyword_text_alignment,
        ],
        post_processors=[check_section_headers],
    )
    visitor.process(THIS_DIR / "docs")