File size: 4,394 Bytes
fc0f7bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Script to dynamically update the ReadMe file for a particular release

Since PyPI and GitHub have slightly different ideas about markdown, we have to update
the ReadMe file when we upload to PyPI. This script makes the necessary changes.
Most of the updates performed should be fairly robust. The one which may give trouble
is in '_update_current_version' which looks for _CURRENT_RELEASE_PATTERN in the
text in order to update both the text and the link.

The produced file assumes that a tag 'vX' (where X corresponds to the current version
of `fairlearn`) exists in the repo. Otherwise, the links won't work.
"""

import argparse
import logging
import os
import re
import sys

from _utils import _LogWrapper

_logger = logging.getLogger(__name__)

_BASE_URI_FORMAT = "https://github.com/fairlearn/fairlearn/tree/v{0}"
_CURRENT_RELEASE_PATTERN = r"\[fairlearn v(\S+)\]\(https://github.com/fairlearn/fairlearn/tree/v\1\)"  # noqa: E501
_OTHER_MD_REF_PATTERN = r"\]\(\./(\w+\.md)"
_SAME_MD_REF_PATTERN = r"\]\((#.+)\)"


def _get_base_path(target_version):
    return _BASE_URI_FORMAT.format(target_version)


def _update_current_version(line, target_version):
    _logger.info("Starting %s", sys._getframe().f_code.co_name)
    current_release_pattern = re.compile(_CURRENT_RELEASE_PATTERN)

    # Extract the current version from the line
    match = current_release_pattern.search(line)
    result = line
    if match:
        _logger.info("Matched %s", match)
        # Replace with the updated version
        result = result.replace(match.groups()[0], target_version)
        _logger.info("Updated string: %s", result.rstrip())
    return result


def _update_other_markdown_references(line, target_version):
    _logger.info("Starting %s", sys._getframe().f_code.co_name)
    markdown_ref_pattern = re.compile(_OTHER_MD_REF_PATTERN)
    result = line
    match = markdown_ref_pattern.search(line)
    if match:
        _logger.info("Matched %s", match)
        for m in match.groups():
            old_str = "./{0}".format(m)
            new_str = "{0}/{1}".format(_get_base_path(target_version), m)
            result = result.replace(old_str, new_str)
        _logger.info("Updated string: %s", result.rstrip())

    return result


def _update_same_page_references(line, target_version):
    _logger.info("Starting %s", sys._getframe().f_code.co_name)
    same_page_ref_pattern = re.compile(_SAME_MD_REF_PATTERN)
    result = line
    match = same_page_ref_pattern.search(line)
    if match:
        _logger.info("Matched %s", match)
        for m in match.groups():
            old_str = m
            new_str = "{0}{1}".format(_get_base_path(target_version), m)
            result = result.replace(old_str, new_str)
        _logger.info("Updated string: %s", result.rstrip())

    return result


def _process_line(line, target_version):
    _logger.info("Starting %s", sys._getframe().f_code.co_name)
    result = _update_current_version(line, target_version)
    result = _update_other_markdown_references(result, target_version)
    result = _update_same_page_references(result, target_version)

    return result


def process_readme(input_file_name, output_file_name):
    sys.path.append(os.getcwd())
    import fairlearn
    target_version = fairlearn.__version__
    _logger.info("fairlearn version: %s", target_version)

    text_lines = []
    with _LogWrapper("reading file {}".format(input_file_name)):

        with open(input_file_name, 'r') as input_file:
            text_lines = input_file.readlines()

    result_lines = [_process_line(line, target_version) for line in text_lines]

    with _LogWrapper("writing file {}".format(output_file_name)):
        with open(output_file_name, 'w') as output_file:
            output_file.writelines(result_lines)


def build_argument_parser():
    desc = "Process ReadMe file for PyPI"

    parser = argparse.ArgumentParser(description=desc)
    parser.add_argument("--input-file-name", help="Path to the file to be processed",
                        required=True)
    parser.add_argument("--output-file-name", help="Path to store the processed file",
                        required=True)

    return parser


def main(argv):
    parser = build_argument_parser()
    args = parser.parse_args(argv)

    process_readme(args.input_file_name, args.output_file_name)


if __name__ == "__main__":
    main(sys.argv[1:])