mirror of
https://github.com/opencv/opencv.git
synced 2026-09-12 13:23:03 -05:00
Fix Python utility compatibility issues - #29755 ### Problem Several repository Python utilities emit invalid escape sequence SyntaxWarnings under Python 3.13. The Java test checker also attempts to parse non-Java assets as UTF-8, causing UnicodeDecodeError, and relies on a global parser instance. The Apple build utility accepts malformed CMake version strings because one version separator is an unescaped regex wildcard. ### Changes - Use raw strings for regular expressions and replacement templates. - Skip non-Java files in the Java test checker. - Use the current JavaParser instance instead of global state. - Require literal dots in parsed CMake versions. ### Verification - Compiled every tracked Python file with SyntaxWarning treated as an error. - Ran the Java checker against modules/java/test successfully. - Verified valid CMake versions are accepted and malformed versions rejected. - Ran git diff --check.
80 lines
2.6 KiB
Python
Executable File
80 lines
2.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Common utilities. These should be compatible with Python3.
|
|
"""
|
|
|
|
from __future__ import print_function
|
|
import sys, re
|
|
from subprocess import check_call, check_output, CalledProcessError
|
|
|
|
def execute(cmd, cwd = None):
|
|
print("Executing: %s in %s" % (cmd, cwd), file=sys.stderr)
|
|
print('Executing: ' + ' '.join(cmd))
|
|
retcode = check_call(cmd, cwd = cwd)
|
|
if retcode != 0:
|
|
raise Exception("Child returned:", retcode)
|
|
|
|
def print_header(text):
|
|
print("="*60)
|
|
print(text)
|
|
print("="*60)
|
|
|
|
def print_error(text):
|
|
print("="*60, file=sys.stderr)
|
|
print("ERROR: %s" % text, file=sys.stderr)
|
|
print("="*60, file=sys.stderr)
|
|
|
|
def get_xcode_major():
|
|
ret = check_output(["xcodebuild", "-version"]).decode('utf-8')
|
|
m = re.match(r'Xcode\s+(\d+)\..*', ret, flags=re.IGNORECASE)
|
|
if m:
|
|
return int(m.group(1))
|
|
else:
|
|
raise Exception("Failed to parse Xcode version")
|
|
|
|
def get_xcode_version():
|
|
"""
|
|
Returns the major and minor version of the current Xcode
|
|
command line tools as a tuple of (major, minor)
|
|
"""
|
|
ret = check_output(["xcodebuild", "-version"]).decode('utf-8')
|
|
m = re.match(r'Xcode\s+(\d+)\.(\d+)', ret, flags=re.IGNORECASE)
|
|
if m:
|
|
return (int(m.group(1)), int(m.group(2)))
|
|
else:
|
|
raise Exception("Failed to parse Xcode version")
|
|
|
|
def get_xcode_setting(var, projectdir):
|
|
ret = check_output(["xcodebuild", "-showBuildSettings"], cwd = projectdir).decode('utf-8')
|
|
m = re.search(r"\s" + var + r" = (.*)", ret)
|
|
if m:
|
|
return m.group(1)
|
|
else:
|
|
raise Exception("Failed to parse Xcode settings")
|
|
|
|
def get_cmake_version():
|
|
"""
|
|
Returns the major and minor version of the current CMake
|
|
command line tools as a tuple of (major, minor, revision)
|
|
"""
|
|
ret = check_output(["cmake", "--version"]).decode('utf-8')
|
|
m = re.match(r'cmake\s+version\s+(\d+)\.(\d+)\.(\d+)', ret, flags=re.IGNORECASE)
|
|
if m:
|
|
return (int(m.group(1)), int(m.group(2)), int(m.group(3)))
|
|
else:
|
|
raise Exception("Failed to parse CMake version")
|
|
|
|
def get_current_branch(opencv_dir):
|
|
ret = check_output(["git", "branch", "--show-current"], cwd = opencv_dir).decode('utf-8').strip()
|
|
if ret != "":
|
|
return ret
|
|
else:
|
|
raise Exception("Failed to get current branch")
|
|
|
|
def find_directory(base_dir, search_dir):
|
|
dirs = check_output(["find", base_dir, "-type", "d", "-name", search_dir]).decode('utf-8').splitlines()
|
|
if dirs and len(dirs) > 0:
|
|
return dirs[0]
|
|
else:
|
|
raise Exception("Failed to find directory: " + search_dir)
|