mirror of
				https://github.com/bytecodealliance/wasm-micro-runtime.git
				synced 2025-10-30 12:51:16 +00:00 
			
		
		
		
	ci/coding_guidelines_check.py: Allow some well-known file names to contain '-' (#3428)
And shorten pathlib.Path to Path.
This commit is contained in:
		
							parent
							
								
									267eaa1747
								
							
						
					
					
						commit
						df82ae8774
					
				|  | @ -5,7 +5,7 @@ | ||||||
| # | # | ||||||
| import argparse | import argparse | ||||||
| import re | import re | ||||||
| import pathlib | from pathlib import Path | ||||||
| import re | import re | ||||||
| import shlex | import shlex | ||||||
| import shutil | import shutil | ||||||
|  | @ -46,18 +46,18 @@ def locate_command(command: str) -> bool: | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def is_excluded(path: str) -> bool: | def is_excluded(path: str) -> bool: | ||||||
|     path = pathlib.Path(path).resolve() |     path = Path(path).resolve() | ||||||
|     for exclude_path in EXCLUDE_PATHS: |     for exclude_path in EXCLUDE_PATHS: | ||||||
|         if path.match(exclude_path): |         if path.match(exclude_path): | ||||||
|             return True |             return True | ||||||
|     return False |     return False | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def pre_flight_check(root: pathlib) -> bool: | def pre_flight_check(root: Path) -> bool: | ||||||
|     def check_aspell(root): |     def check_aspell(root): | ||||||
|         return True |         return True | ||||||
| 
 | 
 | ||||||
|     def check_clang_format(root: pathlib) -> bool: |     def check_clang_format(root: Path) -> bool: | ||||||
|         if not locate_command(CLANG_FORMAT_CMD): |         if not locate_command(CLANG_FORMAT_CMD): | ||||||
|             return False |             return False | ||||||
| 
 | 
 | ||||||
|  | @ -77,7 +77,7 @@ def pre_flight_check(root: pathlib) -> bool: | ||||||
|     return check_aspell(root) and check_clang_format(root) and check_git_clang_format() |     return check_aspell(root) and check_clang_format(root) and check_git_clang_format() | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def run_clang_format(file_path: pathlib, root: pathlib) -> bool: | def run_clang_format(file_path: Path, root: Path) -> bool: | ||||||
|     try: |     try: | ||||||
|         subprocess.check_call( |         subprocess.check_call( | ||||||
|             shlex.split( |             shlex.split( | ||||||
|  | @ -91,7 +91,7 @@ def run_clang_format(file_path: pathlib, root: pathlib) -> bool: | ||||||
|         return False |         return False | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def run_clang_format_diff(root: pathlib, commits: str) -> bool: | def run_clang_format_diff(root: Path, commits: str) -> bool: | ||||||
|     """ |     """ | ||||||
|     Use `clang-format-12` or `git-clang-format-12` to check code format of |     Use `clang-format-12` or `git-clang-format-12` to check code format of | ||||||
|     the PR, with a commit range specified. It is required to format the |     the PR, with a commit range specified. It is required to format the | ||||||
|  | @ -155,11 +155,11 @@ def run_clang_format_diff(root: pathlib, commits: str) -> bool: | ||||||
|         return False |         return False | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def run_aspell(file_path: pathlib, root: pathlib) -> bool: | def run_aspell(file_path: Path, root: Path) -> bool: | ||||||
|     return True |     return True | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def check_dir_name(path: pathlib, root: pathlib) -> bool: | def check_dir_name(path: Path, root: Path) -> bool: | ||||||
|     m = re.search(INVALID_DIR_NAME_SEGMENT, str(path.relative_to(root).parent)) |     m = re.search(INVALID_DIR_NAME_SEGMENT, str(path.relative_to(root).parent)) | ||||||
|     if m: |     if m: | ||||||
|         print(f"--- found a character '_' in {m.groups()} in {path}") |         print(f"--- found a character '_' in {m.groups()} in {path}") | ||||||
|  | @ -167,7 +167,22 @@ def check_dir_name(path: pathlib, root: pathlib) -> bool: | ||||||
|     return not m |     return not m | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def check_file_name(path: pathlib) -> bool: | def check_file_name(path: Path) -> bool: | ||||||
|  |     """ | ||||||
|  |     file names should not contain any character '-' | ||||||
|  | 
 | ||||||
|  |     but some names are well known and use '-' as the separator, e.g.: | ||||||
|  |     - docker-compose | ||||||
|  |     - package-lock | ||||||
|  |     - vite-env.d | ||||||
|  |     """ | ||||||
|  |     if path.stem in [ | ||||||
|  |         "docker-compose", | ||||||
|  |         "package-lock", | ||||||
|  |         "vite-env.d", | ||||||
|  |     ]: | ||||||
|  |         return True | ||||||
|  | 
 | ||||||
|     m = re.search(INVALID_FILE_NAME_SEGMENT, path.stem) |     m = re.search(INVALID_FILE_NAME_SEGMENT, path.stem) | ||||||
|     if m: |     if m: | ||||||
|         print(f"--- found a character '-' in {m.groups()} in {path}") |         print(f"--- found a character '-' in {m.groups()} in {path}") | ||||||
|  | @ -175,7 +190,7 @@ def check_file_name(path: pathlib) -> bool: | ||||||
|     return not m |     return not m | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def parse_commits_range(root: pathlib, commits: str) -> list: | def parse_commits_range(root: Path, commits: str) -> list: | ||||||
|     GIT_LOG_CMD = f"git log --pretty='%H' {commits}" |     GIT_LOG_CMD = f"git log --pretty='%H' {commits}" | ||||||
|     try: |     try: | ||||||
|         ret = subprocess.check_output( |         ret = subprocess.check_output( | ||||||
|  | @ -187,7 +202,7 @@ def parse_commits_range(root: pathlib, commits: str) -> list: | ||||||
|         return [] |         return [] | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def analysis_new_item_name(root: pathlib, commit: str) -> bool: | def analysis_new_item_name(root: Path, commit: str) -> bool: | ||||||
|     """ |     """ | ||||||
|     For any file name in the repo, it is required to use '_' to replace '-'. |     For any file name in the repo, it is required to use '_' to replace '-'. | ||||||
| 
 | 
 | ||||||
|  | @ -216,7 +231,7 @@ def analysis_new_item_name(root: pathlib, commit: str) -> bool: | ||||||
|                 continue |                 continue | ||||||
| 
 | 
 | ||||||
|             new_item = match.group(1) |             new_item = match.group(1) | ||||||
|             new_item = pathlib.Path(new_item).resolve() |             new_item = Path(new_item).resolve() | ||||||
| 
 | 
 | ||||||
|             if new_item.is_file(): |             if new_item.is_file(): | ||||||
|                 if not check_file_name(new_item): |                 if not check_file_name(new_item): | ||||||
|  | @ -235,7 +250,7 @@ def analysis_new_item_name(root: pathlib, commit: str) -> bool: | ||||||
|         return False |         return False | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def process_entire_pr(root: pathlib, commits: str) -> bool: | def process_entire_pr(root: Path, commits: str) -> bool: | ||||||
|     if not commits: |     if not commits: | ||||||
|         print("Please provide a commits range") |         print("Please provide a commits range") | ||||||
|         return False |         return False | ||||||
|  | @ -268,7 +283,7 @@ def main() -> int: | ||||||
|     ) |     ) | ||||||
|     options = parser.parse_args() |     options = parser.parse_args() | ||||||
| 
 | 
 | ||||||
|     wamr_root = pathlib.Path(__file__).parent.joinpath("..").resolve() |     wamr_root = Path(__file__).parent.joinpath("..").resolve() | ||||||
| 
 | 
 | ||||||
|     if not pre_flight_check(wamr_root): |     if not pre_flight_check(wamr_root): | ||||||
|         return False |         return False | ||||||
|  | @ -279,23 +294,23 @@ def main() -> int: | ||||||
| # run with python3 -m unitest ci/coding_guidelines_check.py | # run with python3 -m unitest ci/coding_guidelines_check.py | ||||||
| class TestCheck(unittest.TestCase): | class TestCheck(unittest.TestCase): | ||||||
|     def test_check_dir_name_failed(self): |     def test_check_dir_name_failed(self): | ||||||
|         root = pathlib.Path("/root/Workspace/") |         root = Path("/root/Workspace/") | ||||||
|         new_file_path = root.joinpath("core/shared/platform/esp_idf/espid_memmap.c") |         new_file_path = root.joinpath("core/shared/platform/esp_idf/espid_memmap.c") | ||||||
|         self.assertFalse(check_dir_name(new_file_path, root)) |         self.assertFalse(check_dir_name(new_file_path, root)) | ||||||
| 
 | 
 | ||||||
|     def test_check_dir_name_pass(self): |     def test_check_dir_name_pass(self): | ||||||
|         root = pathlib.Path("/root/Workspace/") |         root = Path("/root/Workspace/") | ||||||
|         new_file_path = root.joinpath("core/shared/platform/esp-idf/espid_memmap.c") |         new_file_path = root.joinpath("core/shared/platform/esp-idf/espid_memmap.c") | ||||||
|         self.assertTrue(check_dir_name(new_file_path, root)) |         self.assertTrue(check_dir_name(new_file_path, root)) | ||||||
| 
 | 
 | ||||||
|     def test_check_file_name_failed(self): |     def test_check_file_name_failed(self): | ||||||
|         new_file_path = pathlib.Path( |         new_file_path = Path( | ||||||
|             "/root/Workspace/core/shared/platform/esp-idf/espid-memmap.c" |             "/root/Workspace/core/shared/platform/esp-idf/espid-memmap.c" | ||||||
|         ) |         ) | ||||||
|         self.assertFalse(check_file_name(new_file_path)) |         self.assertFalse(check_file_name(new_file_path)) | ||||||
| 
 | 
 | ||||||
|     def test_check_file_name_pass(self): |     def test_check_file_name_pass(self): | ||||||
|         new_file_path = pathlib.Path( |         new_file_path = Path( | ||||||
|             "/root/Workspace/core/shared/platform/esp-idf/espid_memmap.c" |             "/root/Workspace/core/shared/platform/esp-idf/espid_memmap.c" | ||||||
|         ) |         ) | ||||||
|         self.assertTrue(check_file_name(new_file_path)) |         self.assertTrue(check_file_name(new_file_path)) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 liang.he
						liang.he