2019-08-30 14:20:29 +02:00
|
|
|
#!/usr/bin/env python3
|
2022-02-11 04:30:11 +01:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
"""
|
|
|
|
Utility functions for make update and make tests.
|
|
|
|
"""
|
2019-08-30 14:20:29 +02:00
|
|
|
|
2019-08-29 11:13:41 +02:00
|
|
|
import re
|
2019-09-30 10:19:55 +02:00
|
|
|
import shutil
|
2019-08-30 14:20:29 +02:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2023-02-21 16:39:58 +01:00
|
|
|
import os
|
2022-09-15 18:08:15 +02:00
|
|
|
from pathlib import Path
|
2023-02-21 16:39:58 +01:00
|
|
|
from urllib.parse import urljoin
|
2019-08-30 14:20:29 +02:00
|
|
|
|
2022-09-16 02:49:49 +02:00
|
|
|
from typing import (
|
|
|
|
Sequence,
|
|
|
|
Optional,
|
|
|
|
)
|
2020-10-02 02:10:01 +02:00
|
|
|
|
2022-09-16 02:49:49 +02:00
|
|
|
|
|
|
|
def call(cmd: Sequence[str], exit_on_error: bool = True, silent: bool = False) -> int:
|
2021-09-20 13:17:01 +02:00
|
|
|
if not silent:
|
2023-02-21 16:39:58 +01:00
|
|
|
print(" ".join([str(x) for x in cmd]))
|
2019-08-30 14:20:29 +02:00
|
|
|
|
|
|
|
# Flush to ensure correct order output on Windows.
|
|
|
|
sys.stdout.flush()
|
|
|
|
sys.stderr.flush()
|
|
|
|
|
2021-09-20 13:17:01 +02:00
|
|
|
if silent:
|
|
|
|
retcode = subprocess.call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
|
|
else:
|
|
|
|
retcode = subprocess.call(cmd)
|
|
|
|
|
2019-09-08 14:17:58 +02:00
|
|
|
if exit_on_error and retcode != 0:
|
|
|
|
sys.exit(retcode)
|
|
|
|
return retcode
|
|
|
|
|
2020-10-02 02:10:01 +02:00
|
|
|
|
2022-09-16 02:49:49 +02:00
|
|
|
def check_output(cmd: Sequence[str], exit_on_error: bool = True) -> str:
|
2019-09-08 14:17:58 +02:00
|
|
|
# Flush to ensure correct order output on Windows.
|
|
|
|
sys.stdout.flush()
|
|
|
|
sys.stderr.flush()
|
|
|
|
|
|
|
|
try:
|
2019-09-08 18:44:47 +02:00
|
|
|
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, universal_newlines=True)
|
2019-09-08 14:17:58 +02:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
if exit_on_error:
|
|
|
|
sys.stderr.write(" ".join(cmd))
|
|
|
|
sys.stderr.write(e.output + "\n")
|
|
|
|
sys.exit(e.returncode)
|
|
|
|
output = ""
|
|
|
|
|
|
|
|
return output.strip()
|
2019-08-29 11:13:41 +02:00
|
|
|
|
2020-10-02 02:10:01 +02:00
|
|
|
|
2022-09-16 02:49:49 +02:00
|
|
|
def git_branch_exists(git_command: str, branch: str) -> bool:
|
2022-04-20 07:08:46 +02:00
|
|
|
return (
|
|
|
|
call([git_command, "rev-parse", "--verify", branch], exit_on_error=False, silent=True) == 0 or
|
2023-02-21 16:39:58 +01:00
|
|
|
call([git_command, "rev-parse", "--verify", "remotes/upstream/" + branch], exit_on_error=False, silent=True) == 0 or
|
2022-04-20 07:08:46 +02:00
|
|
|
call([git_command, "rev-parse", "--verify", "remotes/origin/" + branch], exit_on_error=False, silent=True) == 0
|
|
|
|
)
|
2021-09-20 13:17:01 +02:00
|
|
|
|
|
|
|
|
2023-02-21 16:39:58 +01:00
|
|
|
def git_get_remote_url(git_command: str, remote_name: str) -> bool:
|
|
|
|
return check_output((git_command, "ls-remote", "--get-url", remote_name))
|
|
|
|
|
|
|
|
|
|
|
|
def git_remote_exist(git_command: str, remote_name: str) -> bool:
|
|
|
|
"""Check whether there is a remote with the given name"""
|
|
|
|
# `git ls-remote --get-url upstream` will print an URL if there is such remote configured, and
|
|
|
|
# otherwise will print "upstream".
|
|
|
|
remote_url = check_output((git_command, "ls-remote", "--get-url", remote_name))
|
|
|
|
return remote_url != remote_name
|
|
|
|
|
|
|
|
|
|
|
|
def git_get_resolved_submodule_url(git_command: str, blender_url: str, submodule_path: str) -> str:
|
|
|
|
git_root = check_output([git_command, "rev-parse", "--show-toplevel"])
|
|
|
|
dot_gitmodules = os.path.join(git_root, ".gitmodules")
|
|
|
|
|
|
|
|
submodule_key_prefix = f"submodule.{submodule_path}"
|
|
|
|
submodule_key_url = f"{submodule_key_prefix}.url"
|
|
|
|
|
|
|
|
gitmodule_url = git_get_config(
|
|
|
|
git_command, submodule_key_url, file=dot_gitmodules)
|
|
|
|
|
|
|
|
# A bit of a trickery to construct final URL.
|
|
|
|
# Only works for the relative submodule URLs.
|
|
|
|
#
|
|
|
|
# Note that unless the LHS URL ends up with a slash urljoin treats the last component as a
|
|
|
|
# file.
|
|
|
|
assert gitmodule_url.startswith('..')
|
|
|
|
return urljoin(blender_url + "/", gitmodule_url)
|
|
|
|
|
|
|
|
|
|
|
|
def git_is_remote_repository(git_command: str, repo: str) -> bool:
|
|
|
|
"""Returns true if the given repository is a valid/clonable git repo"""
|
|
|
|
exit_code = call((git_command, "ls-remote", repo, "HEAD"), exit_on_error=False, silent=True)
|
|
|
|
return exit_code == 0
|
|
|
|
|
|
|
|
|
2022-09-16 02:49:49 +02:00
|
|
|
def git_branch(git_command: str) -> str:
|
2020-07-13 15:42:47 +02:00
|
|
|
# Get current branch name.
|
2019-08-29 11:13:41 +02:00
|
|
|
try:
|
|
|
|
branch = subprocess.check_output([git_command, "rev-parse", "--abbrev-ref", "HEAD"])
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
sys.stderr.write("Failed to get Blender git branch\n")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2019-10-09 14:41:34 +02:00
|
|
|
return branch.strip().decode('utf8')
|
|
|
|
|
2020-10-02 02:10:01 +02:00
|
|
|
|
2023-02-21 16:39:58 +01:00
|
|
|
def git_get_config(git_command: str, key: str, file: Optional[str] = None) -> str:
|
|
|
|
if file:
|
|
|
|
return check_output([git_command, "config", "--file", file, "--get", key])
|
|
|
|
|
|
|
|
return check_output([git_command, "config", "--get", key])
|
|
|
|
|
|
|
|
|
|
|
|
def git_set_config(git_command: str, key: str, value: str, file: Optional[str] = None) -> str:
|
|
|
|
if file:
|
|
|
|
return check_output([git_command, "config", "--file", file, key, value])
|
|
|
|
|
|
|
|
return check_output([git_command, "config", key, value])
|
|
|
|
|
|
|
|
|
2022-09-16 02:49:49 +02:00
|
|
|
def git_tag(git_command: str) -> Optional[str]:
|
2020-07-13 15:42:47 +02:00
|
|
|
# Get current tag name.
|
|
|
|
try:
|
2020-07-16 12:47:28 +02:00
|
|
|
tag = subprocess.check_output([git_command, "describe", "--exact-match"], stderr=subprocess.STDOUT)
|
2020-07-13 15:42:47 +02:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return tag.strip().decode('utf8')
|
|
|
|
|
2020-10-02 02:10:01 +02:00
|
|
|
|
2022-10-31 02:54:36 +01:00
|
|
|
def git_branch_release_version(branch: str, tag: Optional[str]) -> Optional[str]:
|
2022-09-16 02:49:49 +02:00
|
|
|
re_match = re.search("^blender-v(.*)-release$", branch)
|
|
|
|
release_version = None
|
|
|
|
if re_match:
|
|
|
|
release_version = re_match.group(1)
|
2020-07-13 15:42:47 +02:00
|
|
|
elif tag:
|
2022-09-16 02:49:49 +02:00
|
|
|
re_match = re.search(r"^v([0-9]*\.[0-9]*).*", tag)
|
|
|
|
if re_match:
|
|
|
|
release_version = re_match.group(1)
|
2019-08-29 11:13:41 +02:00
|
|
|
return release_version
|
|
|
|
|
2020-10-02 02:10:01 +02:00
|
|
|
|
2022-09-16 02:49:49 +02:00
|
|
|
def svn_libraries_base_url(release_version: Optional[str], branch: Optional[str] = None) -> str:
|
2019-08-29 11:13:41 +02:00
|
|
|
if release_version:
|
|
|
|
svn_branch = "tags/blender-" + release_version + "-release"
|
2021-09-02 12:11:32 +02:00
|
|
|
elif branch:
|
|
|
|
svn_branch = "branches/" + branch
|
2019-08-29 11:13:41 +02:00
|
|
|
else:
|
|
|
|
svn_branch = "trunk"
|
|
|
|
return "https://svn.blender.org/svnroot/bf-blender/" + svn_branch + "/lib/"
|
2019-09-30 10:19:55 +02:00
|
|
|
|
2020-10-02 02:10:01 +02:00
|
|
|
|
2022-09-16 02:49:49 +02:00
|
|
|
def command_missing(command: str) -> bool:
|
2019-09-30 10:19:55 +02:00
|
|
|
# Support running with Python 2 for macOS
|
|
|
|
if sys.version_info >= (3, 0):
|
|
|
|
return shutil.which(command) is None
|
|
|
|
else:
|
|
|
|
return False
|
2022-09-15 18:08:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BlenderVersion:
|
2022-09-16 02:49:49 +02:00
|
|
|
def __init__(self, version: int, patch: int, cycle: str):
|
2022-09-15 18:08:15 +02:00
|
|
|
# 293 for 2.93.1
|
|
|
|
self.version = version
|
|
|
|
# 1 for 2.93.1
|
|
|
|
self.patch = patch
|
|
|
|
# 'alpha', 'beta', 'release', maybe others.
|
|
|
|
self.cycle = cycle
|
|
|
|
|
|
|
|
def is_release(self) -> bool:
|
|
|
|
return self.cycle == "release"
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
"""Convert to version string.
|
|
|
|
|
|
|
|
>>> str(BlenderVersion(293, 1, "alpha"))
|
|
|
|
'2.93.1-alpha'
|
|
|
|
>>> str(BlenderVersion(327, 0, "release"))
|
|
|
|
'3.27.0'
|
|
|
|
"""
|
|
|
|
version_major = self.version // 100
|
|
|
|
version_minor = self.version % 100
|
|
|
|
as_string = f"{version_major}.{version_minor}.{self.patch}"
|
|
|
|
if self.is_release():
|
|
|
|
return as_string
|
|
|
|
return f"{as_string}-{self.cycle}"
|
|
|
|
|
2022-09-16 02:49:49 +02:00
|
|
|
|
2022-09-15 18:08:15 +02:00
|
|
|
def parse_blender_version() -> BlenderVersion:
|
|
|
|
blender_srcdir = Path(__file__).absolute().parent.parent.parent
|
|
|
|
version_path = blender_srcdir / "source/blender/blenkernel/BKE_blender_version.h"
|
|
|
|
|
|
|
|
version_info = {}
|
|
|
|
line_re = re.compile(r"^#define (BLENDER_VERSION[A-Z_]*)\s+([0-9a-z]+)$")
|
|
|
|
|
|
|
|
with version_path.open(encoding="utf-8") as version_file:
|
|
|
|
for line in version_file:
|
|
|
|
match = line_re.match(line.strip())
|
|
|
|
if not match:
|
|
|
|
continue
|
|
|
|
version_info[match.group(1)] = match.group(2)
|
|
|
|
|
|
|
|
return BlenderVersion(
|
|
|
|
int(version_info["BLENDER_VERSION"]),
|
|
|
|
int(version_info["BLENDER_VERSION_PATCH"]),
|
|
|
|
version_info["BLENDER_VERSION_CYCLE"],
|
|
|
|
)
|