|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# |
| 5 | +# Copyright 2024-2026 the original author or authors. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | + |
| 20 | +""" |
| 21 | +Spring AI Alibaba Jmanus Chinese Content Checker |
| 22 | +Tool for checking Chinese content in Java code for GitHub Actions |
| 23 | +""" |
| 24 | + |
| 25 | +import os |
| 26 | +import re |
| 27 | +import sys |
| 28 | +import json |
| 29 | +import argparse |
| 30 | +from pathlib import Path |
| 31 | +from typing import List, Dict, Set |
| 32 | +from collections import defaultdict |
| 33 | + |
| 34 | +class ChineseContentChecker: |
| 35 | + def __init__(self, target_dir: str): |
| 36 | + self.target_dir = Path(target_dir) |
| 37 | + # Detect Chinese characters, excluding Chinese punctuation to avoid false positives |
| 38 | + self.chinese_pattern = re.compile(r'[\u4e00-\u9fff]') |
| 39 | + # Chinese punctuation detection |
| 40 | + self.chinese_punctuation = re.compile(r',。!?;:""()【】《》') |
| 41 | + |
| 42 | + # Exclude common English phrases to avoid false positives |
| 43 | + self.exclude_patterns = [ |
| 44 | + r'\bAS IS\b', # "AS IS" in Apache License |
| 45 | + r'\bIS NULL\b', # "IS NULL" in SQL |
| 46 | + r'\bIS NOT\b', # "IS NOT" in SQL |
| 47 | + r'@author\s+\w+', # Author information |
| 48 | + r'@time\s+\d{4}/\d{1,2}/\d{1,2}', # Time information |
| 49 | + ] |
| 50 | + |
| 51 | + self.issues = [] |
| 52 | + |
| 53 | + def has_real_chinese_content(self, text: str) -> bool: |
| 54 | + """Check if text contains real Chinese content (excluding false positives)""" |
| 55 | + # First check if there are Chinese characters or Chinese punctuation |
| 56 | + if not (self.chinese_pattern.search(text) or self.chinese_punctuation.search(text)): |
| 57 | + return False |
| 58 | + |
| 59 | + # Exclude common English phrases |
| 60 | + for pattern in self.exclude_patterns: |
| 61 | + if re.search(pattern, text, re.IGNORECASE): |
| 62 | + # If matched exclude pattern, further check if it really contains Chinese |
| 63 | + temp_text = re.sub(pattern, '', text, flags=re.IGNORECASE) |
| 64 | + if not (self.chinese_pattern.search(temp_text) or self.chinese_punctuation.search(temp_text)): |
| 65 | + return False |
| 66 | + |
| 67 | + return True |
| 68 | + |
| 69 | + def check_file(self, file_path: Path) -> List[Dict]: |
| 70 | + """Check single file for Chinese content, return list of issues""" |
| 71 | + issues = [] |
| 72 | + |
| 73 | + try: |
| 74 | + with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: |
| 75 | + lines = f.readlines() |
| 76 | + |
| 77 | + in_multiline_comment = False |
| 78 | + |
| 79 | + for line_num, line in enumerate(lines, 1): |
| 80 | + original_line = line.rstrip() |
| 81 | + line_stripped = line.strip() |
| 82 | + |
| 83 | + if not line_stripped: |
| 84 | + continue |
| 85 | + |
| 86 | + # Check if contains real Chinese content |
| 87 | + if not self.has_real_chinese_content(line_stripped): |
| 88 | + continue |
| 89 | + |
| 90 | + # Analyze the type of location where Chinese content appears |
| 91 | + content_type = self._analyze_content_type(line_stripped, in_multiline_comment) |
| 92 | + |
| 93 | + # Update multiline comment status |
| 94 | + if '/*' in line_stripped: |
| 95 | + in_multiline_comment = True |
| 96 | + if '*/' in line_stripped: |
| 97 | + in_multiline_comment = False |
| 98 | + |
| 99 | + issues.append({ |
| 100 | + 'file': str(file_path.relative_to(self.target_dir.parent)), |
| 101 | + 'line': line_num, |
| 102 | + 'content': original_line, |
| 103 | + 'type': content_type, |
| 104 | + 'message': f"Found Chinese content in {content_type}" |
| 105 | + }) |
| 106 | + |
| 107 | + except Exception as e: |
| 108 | + print(f"Warning: Unable to read file {file_path}: {e}", file=sys.stderr) |
| 109 | + |
| 110 | + return issues |
| 111 | + |
| 112 | + def _analyze_content_type(self, line: str, in_multiline_comment: bool) -> str: |
| 113 | + """Analyze the type of Chinese content location""" |
| 114 | + if in_multiline_comment or line.startswith('/*'): |
| 115 | + return "multiline comment" |
| 116 | + |
| 117 | + if line.startswith('//'): |
| 118 | + return "single line comment" |
| 119 | + |
| 120 | + if '//' in line: |
| 121 | + comment_part = line[line.find('//'):] |
| 122 | + if self.has_real_chinese_content(comment_part): |
| 123 | + return "inline comment" |
| 124 | + |
| 125 | + # Check string literals |
| 126 | + string_matches = re.finditer(r'"([^"]*)"', line) |
| 127 | + for match in string_matches: |
| 128 | + if self.has_real_chinese_content(match.group(1)): |
| 129 | + return "string literal" |
| 130 | + |
| 131 | + # Check character literals |
| 132 | + char_matches = re.finditer(r"'([^']*)'", line) |
| 133 | + for match in char_matches: |
| 134 | + if self.has_real_chinese_content(match.group(1)): |
| 135 | + return "character literal" |
| 136 | + |
| 137 | + # Check identifiers |
| 138 | + temp_line = re.sub(r'"[^"]*"', '', line) # Remove strings |
| 139 | + temp_line = re.sub(r"'[^']*'", '', temp_line) # Remove characters |
| 140 | + temp_line = re.sub(r'//.*$', '', temp_line) # Remove single line comments |
| 141 | + |
| 142 | + if self.has_real_chinese_content(temp_line): |
| 143 | + return "identifier or code" |
| 144 | + |
| 145 | + return "unknown location" |
| 146 | + |
| 147 | + def check_directory(self) -> bool: |
| 148 | + """Check entire directory, return whether there are issues""" |
| 149 | + if not self.target_dir.exists(): |
| 150 | + print(f"::error::Directory does not exist: {self.target_dir}") |
| 151 | + return False |
| 152 | + |
| 153 | + java_files = list(self.target_dir.rglob("*.java")) |
| 154 | + |
| 155 | + if not java_files: |
| 156 | + print(f"::notice::No Java files found in {self.target_dir}") |
| 157 | + return True |
| 158 | + |
| 159 | + print(f"::notice::Found {len(java_files)} Java files, starting check...") |
| 160 | + |
| 161 | + for java_file in java_files: |
| 162 | + file_issues = self.check_file(java_file) |
| 163 | + self.issues.extend(file_issues) |
| 164 | + |
| 165 | + return len(self.issues) == 0 |
| 166 | + |
| 167 | + def report_issues(self) -> None: |
| 168 | + """Report discovered issues""" |
| 169 | + if not self.issues: |
| 170 | + print("::notice::✅ No Java files with Chinese content found") |
| 171 | + return |
| 172 | + |
| 173 | + print(f"::error::❌ Found {len(self.issues)} Chinese content issues") |
| 174 | + |
| 175 | + # Group issues by file |
| 176 | + files_with_issues = defaultdict(list) |
| 177 | + for issue in self.issues: |
| 178 | + files_with_issues[issue['file']].append(issue) |
| 179 | + |
| 180 | + for file_path, file_issues in files_with_issues.items(): |
| 181 | + print(f"::error file={file_path}::File contains {len(file_issues)} Chinese content issues") |
| 182 | + |
| 183 | + for issue in file_issues: |
| 184 | + print(f"::error file={file_path},line={issue['line']}::{issue['message']}: {issue['content'][:100]}") |
| 185 | + |
| 186 | + # Output modification suggestions |
| 187 | + print("\n::notice::Modification suggestions:") |
| 188 | + print("::notice::1. Change Chinese comments to English comments") |
| 189 | + print("::notice::2. Extract Chinese strings to resource files or configuration files") |
| 190 | + print("::notice::3. Change Chinese identifiers to English identifiers") |
| 191 | + print("::notice::4. For test data, consider using English or placeholders") |
| 192 | + |
| 193 | +def main(): |
| 194 | + parser = argparse.ArgumentParser(description='Check Chinese content in Java code') |
| 195 | + parser.add_argument('--dir', '-d', |
| 196 | + default='src/main/java', |
| 197 | + help='Directory path to check (relative to current directory)') |
| 198 | + parser.add_argument('--fail-on-found', '-f', |
| 199 | + action='store_true', |
| 200 | + help='Return non-zero exit code when Chinese content is found') |
| 201 | + |
| 202 | + args = parser.parse_args() |
| 203 | + |
| 204 | + try: |
| 205 | + checker = ChineseContentChecker(args.dir) |
| 206 | + is_clean = checker.check_directory() |
| 207 | + checker.report_issues() |
| 208 | + |
| 209 | + if args.fail_on_found and not is_clean: |
| 210 | + print(f"::error::Check failed: Found {len(checker.issues)} Chinese content issues") |
| 211 | + return 1 |
| 212 | + |
| 213 | + return 0 |
| 214 | + |
| 215 | + except Exception as e: |
| 216 | + print(f"::error::Error occurred during check: {e}") |
| 217 | + return 1 |
| 218 | + |
| 219 | +if __name__ == "__main__": |
| 220 | + sys.exit(main()) |
0 commit comments