33
44from __future__ import annotations
55
6+ import http .client
67import json
78import os
89import re
9- import subprocess
1010import sys
11- import urllib .request
1211from pathlib import Path
1312
1413
15- WORDPRESS_VERSION_CHECK_URL = os .environ .get (
16- "WORDPRESS_VERSION_CHECK_URL" ,
17- "https://api.wordpress.org/core/version-check/1.7/" ,
18- )
14+ WORDPRESS_API_HOST = "api.wordpress.org"
15+ WORDPRESS_VERSION_CHECK_PATH = "/core/version-check/1.7/"
1916WORDPRESS_LATEST_VERSION = os .environ .get ("WORDPRESS_LATEST_VERSION" )
2017SCAN_EXTENSIONS = {".php" , ".md" , ".txt" }
2118DEFAULT_EXCLUDED_DIRS = {
2522 "dist" ,
2623 "node_modules" ,
2724 "plugin-check-build" ,
25+ "__pycache__" ,
2826 "vendor" ,
2927}
3028TESTED_UP_TO_PATTERN = re .compile (
@@ -41,7 +39,7 @@ def main() -> int:
4139 findings = find_tested_up_to_entries (excluded_dirs )
4240
4341 if not findings :
44- message = "No Tested up to metadata was found in tracked PHP, Markdown, or text files."
42+ message = "No Tested up to metadata was found in PHP, Markdown, or text files."
4543 print_github_error (message )
4644 write_summary (latest_version , [], [message ])
4745 return 1
@@ -85,13 +83,26 @@ def get_latest_wordpress_major_minor() -> str:
8583 if WORDPRESS_LATEST_VERSION :
8684 return normalize_major_minor (WORDPRESS_LATEST_VERSION )
8785
88- request = urllib .request .Request (
89- WORDPRESS_VERSION_CHECK_URL ,
90- headers = {"User-Agent" : "wordpress-tested-up-to-check/1.0" },
86+ connection = http .client .HTTPSConnection (WORDPRESS_API_HOST , timeout = 20 )
87+ connection .request (
88+ "GET" ,
89+ WORDPRESS_VERSION_CHECK_PATH ,
90+ headers = {
91+ "Accept" : "application/json" ,
92+ "User-Agent" : "wordpress-tested-up-to-check/1.0" ,
93+ },
9194 )
9295
93- with urllib .request .urlopen (request , timeout = 20 ) as response :
94- payload = json .load (response )
96+ response = connection .getresponse ()
97+ try :
98+ if response .status != http .client .OK :
99+ raise RuntimeError (
100+ f"WordPress version-check API returned HTTP { response .status } ."
101+ )
102+
103+ payload = json .loads (response .read ().decode ("utf-8" ))
104+ finally :
105+ connection .close ()
95106
96107 versions = []
97108 for offer in payload .get ("offers" , []):
@@ -130,7 +141,7 @@ def find_tested_up_to_entries(
130141) -> list [dict [str , str | int | None ]]:
131142 findings = []
132143
133- for path in get_tracked_files ( ):
144+ for path in get_scanned_files ( excluded_dirs ):
134145 if not should_scan (path , excluded_dirs ):
135146 continue
136147
@@ -152,16 +163,24 @@ def find_tested_up_to_entries(
152163 return findings
153164
154165
155- def get_tracked_files () -> list [Path ]:
156- result = subprocess .run (
157- ["git" , "ls-files" , "*.php" , "*.md" , "*.txt" ],
158- check = True ,
159- stdout = subprocess .PIPE ,
160- stderr = subprocess .PIPE ,
161- text = True ,
162- )
166+ def get_scanned_files (excluded_dirs : set [str ]) -> list [Path ]:
167+ root = Path .cwd ()
168+ paths = []
169+
170+ for current_dir , dirnames , filenames in os .walk (root ):
171+ dirnames [:] = [
172+ dirname for dirname in dirnames if dirname not in excluded_dirs
173+ ]
174+
175+ current_path = Path (current_dir )
176+ for filename in filenames :
177+ path = current_path / filename
178+ relative_path = path .relative_to (root )
179+
180+ if should_scan (relative_path , excluded_dirs ):
181+ paths .append (relative_path )
163182
164- return [ Path ( line ) for line in result . stdout . splitlines () if line . strip ()]
183+ return sorted ( paths )
165184
166185
167186def should_scan (path : Path , excluded_dirs : set [str ]) -> bool :
0 commit comments