|
| 1 | +# Detailed Code Changes |
| 2 | + |
| 3 | +## File: src/cluecode/linux_credits.py |
| 4 | + |
| 5 | +### Change 1: Add `re` module import |
| 6 | +**Location**: Line 13 |
| 7 | +```python |
| 8 | +import os |
| 9 | +import sys |
| 10 | +import re # <-- NEW: Added for regex pattern matching |
| 11 | + |
| 12 | +from collections import deque |
| 13 | +``` |
| 14 | + |
| 15 | +### Change 2: Update Module Docstring |
| 16 | +**Location**: Lines 20-30 |
| 17 | +```python |
| 18 | +""" |
| 19 | +Detect and collect authors from a Linux-formatted CREDITS file. |
| 20 | +This used by Linux, but also Raku, Phasar, u-boot, LLVM, Botan and other projects. |
| 21 | +An enetry looks like this: |
| 22 | + N: Jack Lloyd |
| 23 | + E: lloyd@randombit.net |
| 24 | + W: http://www.randombit.net/ |
| 25 | + P: 3F69 2E64 6D92 3BBE E7AE 9258 5C0F 96E8 4EC1 6D6B |
| 26 | + B: 1DwxWb2J4vuX4vjsbzaCXW696rZfeamahz |
| 27 | +
|
| 28 | +We only consider the entries: N: name, E: email and W: web URL. |
| 29 | +Additionally, we support Author and Upstream Author formats: # <-- NEW |
| 30 | + Author: Author Name |
| 31 | + Author:Author Name (no space after colon) |
| 32 | + Upstream Author: Author Name |
| 33 | + Upstream-Author: Author Name |
| 34 | +""" |
| 35 | +``` |
| 36 | + |
| 37 | +### Change 3: Update `get_credit_lines_groups()` Function |
| 38 | +**Location**: Lines 138-168 |
| 39 | + |
| 40 | +**BEFORE**: |
| 41 | +```python |
| 42 | +if line.startswith(("N:", "E:", "W:")): |
| 43 | + has_credits = True |
| 44 | + lines_group_append((ln, line)) |
| 45 | +``` |
| 46 | + |
| 47 | +**AFTER**: |
| 48 | +```python |
| 49 | +# Support both standard format (N:, E:, W:) and Author: format (with or without space after colon) |
| 50 | +if line.startswith(("N:", "E:", "W:")) or re.match(r'^(?:Author|Upstream[-\s]*Author):\s*', line, re.IGNORECASE): |
| 51 | + has_credits = True |
| 52 | + lines_group_append((ln, line)) |
| 53 | +``` |
| 54 | + |
| 55 | +### Change 4: Update `detect_credits_authors_from_lines()` Function |
| 56 | +**Location**: Lines 85-127 |
| 57 | + |
| 58 | +**BEFORE**: |
| 59 | +```python |
| 60 | +for lines in get_credit_lines_groups(numbered_lines): |
| 61 | + if TRACE: |
| 62 | + logger_debug('detect_credits_authors_from_lines: credit_lines group:', lines) |
| 63 | + |
| 64 | + start_line, _ = lines[0] |
| 65 | + end_line, _ = lines[-1] |
| 66 | + names = [] |
| 67 | + emails = [] |
| 68 | + webs = [] |
| 69 | + for _, line in lines: |
| 70 | + ltype, _, line = line.partition(":") |
| 71 | + line = line.strip() |
| 72 | + if ltype == "N": |
| 73 | + names.append(line) |
| 74 | + elif ltype == "E": |
| 75 | + emails.append(line) |
| 76 | + elif ltype == "W": |
| 77 | + webs.append(line) |
| 78 | + |
| 79 | + items = list(" ".join(item) for item in (names, emails, webs) if item) |
| 80 | + if TRACE: |
| 81 | + logger_debug('detect_credits_authors_from_lines: items:', items) |
| 82 | + |
| 83 | + author = " ".join(items) |
| 84 | + if author: |
| 85 | + yield AuthorDetection(author=author, start_line=start_line, end_line=end_line) |
| 86 | +``` |
| 87 | + |
| 88 | +**AFTER**: |
| 89 | +```python |
| 90 | +for lines in get_credit_lines_groups(numbered_lines): |
| 91 | + if TRACE: |
| 92 | + logger_debug('detect_credits_authors_from_lines: credit_lines group:', lines) |
| 93 | + |
| 94 | + start_line, _ = lines[0] |
| 95 | + end_line, _ = lines[-1] |
| 96 | + names = [] |
| 97 | + emails = [] |
| 98 | + webs = [] |
| 99 | + authors = [] # <-- NEW: Added list to collect extracted authors |
| 100 | + |
| 101 | + for _, line in lines: |
| 102 | + # Extract the type and value using partition for N:, E:, W: format |
| 103 | + ltype, _, line_value = line.partition(":") |
| 104 | + line_value = line_value.strip() |
| 105 | + |
| 106 | + if ltype == "N": |
| 107 | + names.append(line_value) |
| 108 | + elif ltype == "E": |
| 109 | + emails.append(line_value) |
| 110 | + elif ltype == "W": |
| 111 | + webs.append(line_value) |
| 112 | + else: |
| 113 | + # <-- NEW: Handle Author: format (with or without space after colon) |
| 114 | + # Extract author name using regex to handle both "Author:Name" and "Author: Name" |
| 115 | + match = re.match(r'^(?:Author|Upstream[-\s]*Author):\s*(.+)$', line, re.IGNORECASE) |
| 116 | + if match: |
| 117 | + author_name = match.group(1).strip() |
| 118 | + if author_name: |
| 119 | + authors.append(author_name) |
| 120 | + |
| 121 | + items = list(" ".join(item) for item in (names, emails, webs, authors) if item) # <-- MODIFIED: Added authors to items |
| 122 | + if TRACE: |
| 123 | + logger_debug('detect_credits_authors_from_lines: items:', items) |
| 124 | + |
| 125 | + author = " ".join(items) |
| 126 | + if author: |
| 127 | + yield AuthorDetection(author=author, start_line=start_line, end_line=end_line) |
| 128 | +``` |
| 129 | + |
| 130 | +## Summary of Changes |
| 131 | + |
| 132 | +1. **Added import**: `import re` for regex pattern matching |
| 133 | +2. **Enhanced docstring**: Added documentation for new Author formats |
| 134 | +3. **Updated line detection**: Modified regex to detect Author: lines |
| 135 | +4. **Enhanced parsing logic**: Added extraction for Author: format |
| 136 | +5. **Maintained backward compatibility**: All existing functionality preserved |
| 137 | + |
| 138 | +## Regex Patterns Used |
| 139 | + |
| 140 | +### Pattern 1: Line Detection (in `get_credit_lines_groups`) |
| 141 | +```regex |
| 142 | +r'^(?:Author|Upstream[-\s]*Author):\s*' |
| 143 | +``` |
| 144 | +- Matches lines starting with "Author:" or "Upstream Author:" |
| 145 | +- Case-insensitive (re.IGNORECASE flag) |
| 146 | +- Allows optional space or hyphen variations |
| 147 | + |
| 148 | +### Pattern 2: Author Name Extraction (in `detect_credits_authors_from_lines`) |
| 149 | +```regex |
| 150 | +r'^(?:Author|Upstream[-\s]*Author):\s*(.+)$' |
| 151 | +``` |
| 152 | +- Captures the author name in group(1) |
| 153 | +- Extracts everything after the colon and optional whitespace |
| 154 | +- Case-insensitive (re.IGNORECASE flag) |
| 155 | + |
| 156 | +## Testing the Changes |
| 157 | + |
| 158 | +Run the existing tests to verify nothing is broken: |
| 159 | +```bash |
| 160 | +pytest tests/cluecode/test_linux_credits.py -xvs |
| 161 | +``` |
| 162 | + |
| 163 | +The implementation successfully handles: |
| 164 | +- ✓ Author: Name (with space) |
| 165 | +- ✓ Author:Name (without space) |
| 166 | +- ✓ author: name (lowercase) |
| 167 | +- ✓ Upstream Author: Name |
| 168 | +- ✓ Upstream-Author: Name |
| 169 | +- ✓ Case-insensitive matching |
| 170 | +- ✓ Backward compatibility with N:, E:, W: format |
0 commit comments