Skip to content

Reference

High Level API¤

extract_release_comments(tags, path, output_filename) ¤

A high-level API for extracting comments and docstrings added in between 2 (ideally 2) tags or commits.

Parameters:

Name Type Description Default
tags list[str]

A list of tags or commits (ideally 2 most recent) to find diff between.

required
path str

Path of the sub-directory to find the diff of.

required
output_filename str

Path of the file to store the diff in.

required

Returns:

Name Type Description
comments list[str]

A list of comments and docstrings added in between the provided tags.

Source code in releaseup/high_level.py
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
def extract_release_comments(
    tags: list[str], path: str, output_filename: str
) -> list[str]:
    """
    A high-level API for extracting comments and docstrings added in between
    2 (ideally 2) tags or commits.

    Args:
        tags:
            A list of tags or commits (ideally 2 most recent) to find diff between.
        path:
            Path of the sub-directory to find the diff of.
        output_filename:
            Path of the file to store the diff in.

    Returns:
        comments:
            A list of comments and docstrings added in between the provided tags.
    """
    get_diff(tags, path, output_filename)
    extracted_additions = extract_additions(output_filename)
    preprocessed_additions = preprocess_additions(extracted_additions)
    comments = get_comments_and_docstrings(preprocessed_additions)

    return comments

Low Level API¤

extract_additions(filename) ¤

Extracts all the additions made to a codebase from a file containing git diff.

Parameters:

Name Type Description Default
filename str

File containing the git diff.

required

Returns:

Name Type Description
extracted_additions list[str]

A list of additions made to the codebase.

Source code in releaseup/extract.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def extract_additions(filename: str) -> list[str]:
    """
    Extracts all the additions made to a codebase from a file containing `git diff`.

    Args:
        filename:
            File containing the `git diff`.

    Returns:
        extracted_additions:
            A list of additions made to the codebase.
    """
    with open(filename) as f:
        lines = f.readlines()
        extracted_additions = [line for line in lines if line[0] == "+"]

    return extracted_additions

get_comments_and_docstrings(preprocessed_additions) ¤

Extracts comments and docstrings from preprocessed git diff output.

Parameters:

Name Type Description Default
preprocessed_additions list[str]

A list of preprocessed additions from git diff.

required

Returns:

Name Type Description
extracted_docs list[str]

Extracted comments and docstrings from the preprocessed_additions.

Source code in releaseup/extract.py
 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
def get_comments_and_docstrings(preprocessed_additions: list[str]) -> list[str]:
    """
    Extracts comments and docstrings from preprocessed `git diff` output.

    Args:
        preprocessed_additions:
            A list of preprocessed additions from `git diff`.

    Returns:
        extracted_docs:
            Extracted comments and docstrings from the preprocessed_additions.
    """
    comments_and_docstrings = [
        line[line.find("#") + 1 :].replace("\n", "").strip()
        for line in preprocessed_additions
        if "#" in line
    ]
    lines = " ".join(preprocessed_additions).replace("\n", " ")
    i = 0

    while i < len(lines):
        if lines[i : i + 3] == '"""':
            idx_next_quotes = lines[i + 3 :].find('"""')
            # args = (
            #     lines[i + 3 :].find("Args:")
            #     if lines[i + 3 :].find("Args:") != -1
            #     else sys.maxsize
            # )
            # returns = (
            #     lines[i + 3 :].find("Returns:")
            #     if lines[i + 3 :].find("Returns:") != -1
            #     else sys.maxsize
            # )
            # examples = (
            #     lines[i + 3 :].find("Examples:")
            #     if lines[i + 3 :].find("Examples:") != -1
            #     else sys.maxsize
            # )
            # min(args, returns, examples)
            # if idx != sys.maxsize:
            comments_and_docstrings.append(lines[i + 3 : i + idx_next_quotes + 3])
            i += idx_next_quotes + 6
            continue
        i += 1

    extracted_docs = [x.strip() for x in comments_and_docstrings]
    extracted_docs = [doc.replace("\n", "").replace("\t", "") for doc in extracted_docs]
    extracted_docs = [f"{x.strip()}\n" for x in comments_and_docstrings]

    i = 0
    while i < len(extracted_docs):
        line = extracted_docs[i]
        if "noqa" in line or "type:" in line or line == "\n" or "todo" in line.lower():
            extracted_docs.pop(i)
        else:
            i += 1

    return extracted_docs

get_diff(tags, path='./', output_filename='diff.txt') ¤

Writes git diff to a file.

Parameters:

Name Type Description Default
tags list[str]

A list of tags or commits (ideally 2 most recent) to find diff between.

required
path str

Path of the sub-directory to find the diff of.

'./'
output_filename str

Path of the file to store the diff in.

'diff.txt'
Source code in releaseup/extract.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
def get_diff(
    tags: list[str], path: str = "./", output_filename: str = "diff.txt"
) -> None:
    """
    Writes `git diff` to a file.

    Args:
        tags:
            A list of tags or commits (ideally 2 most recent) to find diff between.
        path:
            Path of the sub-directory to find the diff of.
        output_filename:
            Path of the file to store the diff in.
    """
    with open(output_filename, "w+") as f:
        subprocess.run(["git", "diff", tags[0], tags[1], "--", path], stdout=f)

preprocess_additions(extracted_additions) ¤

Preprocesses the extracted additions from git diff.

Parameters:

Name Type Description Default
extracted_additions list[str]

A git diff containing only the additions.

required

Returns:

Name Type Description
preprocessed_additions list[str]

Preprocessed additions.

Source code in releaseup/extract.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def preprocess_additions(extracted_additions: list[str]) -> list[str]:
    """
    Preprocesses the extracted additions from `git diff`.

    Args:
        extracted_additions:
            A `git diff` containing only the additions.

    Returns:
        preprocessed_additions:
            Preprocessed additions.
    """
    preprocessed_additions = extracted_additions.copy()
    preprocessed_additions = [f"{line.strip()}\n" for line in preprocessed_additions]
    for line in preprocessed_additions:
        if line[:3] == "+++":
            preprocessed_additions.pop(preprocessed_additions.index(line))

    preprocessed_additions = [line[1:] for line in preprocessed_additions]
    preprocessed_additions = [f"{line.strip()}\n" for line in preprocessed_additions]

    return preprocessed_additions

save(content, filename) ¤

Save contents in a file.

Parameters:

Name Type Description Default
content list[str]

A list of contents to be saved.

required
filename str

Path of the file in which the contents have to be saved

required
Source code in releaseup/extract.py
127
128
129
130
131
132
133
134
135
136
137
138
def save(content: list[str], filename: str) -> None:
    """
    Save contents in a file.

    Args:
        content:
            A list of contents to be saved.
        filename:
            Path of the file in which the contents have to be saved
    """
    with open(filename, "w+") as f:
        f.write("".join(content))