2021-03-08 18:10:26 +01:00
|
|
|
#!/usr/bin/env python3
|
2023-08-16 00:20:26 +10:00
|
|
|
# SPDX-FileCopyrightText: 2021-2023 Blender Authors
|
2023-06-14 23:06:58 +10:00
|
|
|
#
|
2022-02-11 14:30:11 +11:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
2021-03-08 18:10:26 +01:00
|
|
|
|
2025-01-04 20:37:46 +11:00
|
|
|
__all__ = (
|
|
|
|
"main",
|
|
|
|
)
|
|
|
|
|
2021-04-13 17:47:48 +02:00
|
|
|
import argparse
|
2022-09-15 18:08:15 +02:00
|
|
|
import make_utils
|
2021-03-08 18:10:26 +01:00
|
|
|
import os
|
|
|
|
import subprocess
|
2024-02-11 02:47:26 +01:00
|
|
|
import sys
|
2021-03-08 18:10:26 +01:00
|
|
|
from pathlib import Path
|
2024-10-18 11:00:40 +11:00
|
|
|
|
|
|
|
from typing import (
|
|
|
|
TextIO,
|
|
|
|
Any,
|
2025-03-06 11:13:09 +11:00
|
|
|
Tuple,
|
2024-12-24 11:55:29 +01:00
|
|
|
Union,
|
|
|
|
# Proxies for `collections.abc`
|
2024-10-18 11:00:40 +11:00
|
|
|
Iterable,
|
2025-05-05 15:10:22 +02:00
|
|
|
List,
|
2024-10-18 11:00:40 +11:00
|
|
|
)
|
2021-03-08 18:10:26 +01:00
|
|
|
|
|
|
|
# This script can run from any location,
|
|
|
|
# output is created in the $CWD
|
2021-03-12 13:39:08 +11:00
|
|
|
#
|
|
|
|
# NOTE: while the Python part of this script is portable,
|
|
|
|
# it relies on external commands typically found on GNU/Linux.
|
|
|
|
# Support for other platforms could be added by moving GNU `tar` & `md5sum` use to Python.
|
2025-02-28 12:00:57 +01:00
|
|
|
# This also relies on having a Unix shell (sh) to run some git commands.
|
2021-03-08 18:10:26 +01:00
|
|
|
|
2025-03-06 11:13:09 +11:00
|
|
|
SKIP_NAMES: Tuple[str, ...] = (
|
2021-03-08 18:10:26 +01:00
|
|
|
".gitignore",
|
|
|
|
".gitmodules",
|
2022-09-16 15:34:53 +10:00
|
|
|
".gitattributes",
|
|
|
|
".git-blame-ignore-revs",
|
2021-03-08 18:10:26 +01:00
|
|
|
".arcconfig",
|
2021-04-13 17:47:48 +02:00
|
|
|
".svn",
|
2025-02-28 12:00:57 +01:00
|
|
|
)
|
|
|
|
|
2025-05-05 15:10:22 +02:00
|
|
|
# Tuple with folder names relative to the main repository root that are to be excluded.
|
2025-03-06 11:13:09 +11:00
|
|
|
SKIP_FOLDERS: Tuple[str, ...] = (
|
2025-02-28 12:00:57 +01:00
|
|
|
)
|
2021-03-08 18:10:26 +01:00
|
|
|
|
2025-05-05 15:10:22 +02:00
|
|
|
# Generated list of Paths to be ignored based on the SKIP_FOLDERS and some rum-time rules like
|
|
|
|
# the type of package that is being created.
|
|
|
|
SKIP_PATHS: List[Path] = []
|
|
|
|
|
2021-03-08 18:10:26 +01:00
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
blender_srcdir = Path(__file__).absolute().parent.parent.parent
|
2021-04-13 17:47:48 +02:00
|
|
|
|
|
|
|
cli_parser = argparse.ArgumentParser(
|
2024-04-01 16:50:23 +11:00
|
|
|
description="Create a tarball of the Blender sources, optionally including sources of dependencies.",
|
2021-04-13 17:47:48 +02:00
|
|
|
epilog="This script is intended to be run by `make source_archive_complete`.",
|
|
|
|
)
|
2025-02-28 17:53:36 +01:00
|
|
|
group = cli_parser.add_mutually_exclusive_group()
|
|
|
|
group.add_argument(
|
2021-04-13 17:47:48 +02:00
|
|
|
"-p",
|
|
|
|
"--include-packages",
|
|
|
|
type=Path,
|
|
|
|
default=None,
|
|
|
|
metavar="PACKAGE_PATH",
|
|
|
|
help="Include all source files from the given package directory as well.",
|
|
|
|
)
|
2025-02-28 17:53:36 +01:00
|
|
|
group.add_argument(
|
|
|
|
"-t",
|
|
|
|
"--package-test-data",
|
|
|
|
action='store_true',
|
|
|
|
help="Package all test data into its own archive",
|
|
|
|
)
|
2021-04-13 17:47:48 +02:00
|
|
|
|
|
|
|
cli_args = cli_parser.parse_args()
|
|
|
|
|
2021-03-08 18:10:26 +01:00
|
|
|
print(f"Source dir: {blender_srcdir}")
|
|
|
|
|
2021-04-13 17:47:48 +02:00
|
|
|
curdir = blender_srcdir.parent
|
|
|
|
os.chdir(curdir)
|
|
|
|
blender_srcdir = blender_srcdir.relative_to(curdir)
|
|
|
|
|
2025-02-28 12:00:57 +01:00
|
|
|
# Update our SKIP_FOLDERS blacklist with the source directory name
|
2025-05-05 15:10:22 +02:00
|
|
|
global SKIP_PATHS
|
|
|
|
SKIP_PATHS = [blender_srcdir / entry for entry in SKIP_FOLDERS]
|
2025-02-28 12:00:57 +01:00
|
|
|
|
2021-04-13 17:47:48 +02:00
|
|
|
print(f"Output dir: {curdir}")
|
|
|
|
|
2022-09-15 18:08:15 +02:00
|
|
|
version = make_utils.parse_blender_version()
|
2021-04-13 17:47:48 +02:00
|
|
|
tarball = tarball_path(curdir, version, cli_args)
|
|
|
|
manifest = manifest_path(tarball)
|
|
|
|
packages_dir = packages_path(curdir, cli_args)
|
2021-03-08 18:10:26 +01:00
|
|
|
|
2025-02-28 17:53:36 +01:00
|
|
|
if cli_args.package_test_data:
|
|
|
|
print("Creating an archive of all test data.")
|
2025-05-05 15:10:22 +02:00
|
|
|
create_manifest(version, manifest, blender_srcdir / "tests/files", packages_dir)
|
2025-02-28 17:53:36 +01:00
|
|
|
else:
|
2025-05-05 15:10:22 +02:00
|
|
|
SKIP_PATHS.append(blender_srcdir / "tests/files")
|
2025-02-28 17:53:36 +01:00
|
|
|
create_manifest(version, manifest, blender_srcdir, packages_dir)
|
|
|
|
|
2021-04-13 17:47:48 +02:00
|
|
|
create_tarball(version, tarball, manifest, blender_srcdir, packages_dir)
|
2021-03-08 18:10:26 +01:00
|
|
|
create_checksum_file(tarball)
|
|
|
|
cleanup(manifest)
|
|
|
|
print("Done!")
|
|
|
|
|
|
|
|
|
2022-09-15 18:08:15 +02:00
|
|
|
def tarball_path(output_dir: Path, version: make_utils.BlenderVersion, cli_args: Any) -> Path:
|
2021-04-13 17:47:48 +02:00
|
|
|
extra = ""
|
|
|
|
if cli_args.include_packages:
|
|
|
|
extra = "-with-libraries"
|
2025-02-28 17:53:36 +01:00
|
|
|
elif cli_args.package_test_data:
|
|
|
|
extra = "-test-data"
|
2021-04-13 17:47:48 +02:00
|
|
|
|
|
|
|
return output_dir / f"blender{extra}-{version}.tar.xz"
|
|
|
|
|
|
|
|
|
|
|
|
def manifest_path(tarball: Path) -> Path:
|
|
|
|
"""Return the manifest path for the given tarball path.
|
|
|
|
|
|
|
|
>>> from pathlib import Path
|
2025-02-02 13:56:44 +11:00
|
|
|
>>> tarball = Path("/home/user/workspace/blender-git/blender-test.tar.gz")
|
2021-04-13 17:47:48 +02:00
|
|
|
>>> manifest_path(tarball).as_posix()
|
2025-02-02 13:56:44 +11:00
|
|
|
'/home/user/workspace/blender-git/blender-test-manifest.txt'
|
2021-04-13 17:47:48 +02:00
|
|
|
"""
|
2025-02-02 13:56:44 +11:00
|
|
|
# Note that `.tar.gz` is seen as two suffixes.
|
2021-04-13 17:47:48 +02:00
|
|
|
without_suffix = tarball.with_suffix("").with_suffix("")
|
|
|
|
name = without_suffix.name
|
|
|
|
return without_suffix.with_name(f"{name}-manifest.txt")
|
|
|
|
|
|
|
|
|
2024-12-24 11:55:29 +01:00
|
|
|
def packages_path(current_directory: Path, cli_args: Any) -> Union[Path, None]:
|
2021-04-13 17:47:48 +02:00
|
|
|
if not cli_args.include_packages:
|
|
|
|
return None
|
|
|
|
|
|
|
|
abspath = cli_args.include_packages.absolute()
|
|
|
|
|
2025-02-02 13:56:44 +11:00
|
|
|
# `os.path.relpath()` can return paths like "../../packages", where
|
|
|
|
# `Path.relative_to()` will not go up directories (so its return value never
|
2021-04-13 17:47:48 +02:00
|
|
|
# has "../" in there).
|
|
|
|
relpath = os.path.relpath(abspath, current_directory)
|
|
|
|
|
|
|
|
return Path(relpath)
|
|
|
|
|
2022-04-20 15:08:46 +10:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Manifest creation
|
2021-03-08 18:10:26 +01:00
|
|
|
|
|
|
|
|
2021-04-13 17:47:48 +02:00
|
|
|
def create_manifest(
|
2022-09-15 18:08:15 +02:00
|
|
|
version: make_utils.BlenderVersion,
|
2021-04-13 17:47:48 +02:00
|
|
|
outpath: Path,
|
|
|
|
blender_srcdir: Path,
|
2024-12-24 11:55:29 +01:00
|
|
|
packages_dir: Union[Path, None],
|
2025-05-05 15:10:22 +02:00
|
|
|
exclude: List[Path] = []
|
2021-04-13 17:47:48 +02:00
|
|
|
) -> None:
|
2021-03-08 18:10:26 +01:00
|
|
|
print(f'Building manifest of files: "{outpath}"...', end="", flush=True)
|
|
|
|
with outpath.open("w", encoding="utf-8") as outfile:
|
2021-04-13 17:47:48 +02:00
|
|
|
main_files_to_manifest(blender_srcdir, outfile)
|
|
|
|
|
|
|
|
if packages_dir:
|
|
|
|
packages_to_manifest(outfile, packages_dir)
|
2021-03-08 18:10:26 +01:00
|
|
|
print("OK")
|
|
|
|
|
|
|
|
|
2021-04-13 17:47:48 +02:00
|
|
|
def main_files_to_manifest(blender_srcdir: Path, outfile: TextIO) -> None:
|
|
|
|
assert not blender_srcdir.is_absolute()
|
2025-02-28 12:00:57 +01:00
|
|
|
for git_repo in git_gather_all_folders_to_package(blender_srcdir):
|
|
|
|
for path in git_ls_files(git_repo):
|
|
|
|
print(path, file=outfile)
|
2023-04-05 09:27:42 +02:00
|
|
|
|
|
|
|
|
2021-04-13 17:47:48 +02:00
|
|
|
def packages_to_manifest(outfile: TextIO, packages_dir: Path) -> None:
|
|
|
|
for path in packages_dir.glob("*"):
|
|
|
|
if not path.is_file():
|
|
|
|
continue
|
|
|
|
if path.name in SKIP_NAMES:
|
|
|
|
continue
|
|
|
|
print(path, file=outfile)
|
|
|
|
|
|
|
|
|
2022-04-20 15:08:46 +10:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Higher-level functions
|
2021-04-13 17:47:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
def create_tarball(
|
2022-09-16 11:53:40 +10:00
|
|
|
version: make_utils.BlenderVersion,
|
|
|
|
tarball: Path,
|
|
|
|
manifest: Path,
|
|
|
|
blender_srcdir: Path,
|
2024-12-24 11:55:29 +01:00
|
|
|
packages_dir: Union[Path, None],
|
2021-04-13 17:47:48 +02:00
|
|
|
) -> None:
|
2021-03-08 18:10:26 +01:00
|
|
|
print(f'Creating archive: "{tarball}" ...', end="", flush=True)
|
2021-04-13 17:47:48 +02:00
|
|
|
|
2021-03-12 13:39:08 +11:00
|
|
|
# Requires GNU `tar`, since `--transform` is used.
|
2024-02-11 02:47:26 +01:00
|
|
|
if sys.platform == "darwin":
|
|
|
|
# Provided by `brew install gnu-tar`.
|
|
|
|
command = ["gtar"]
|
|
|
|
else:
|
|
|
|
command = ["tar"]
|
|
|
|
|
2021-04-13 17:47:48 +02:00
|
|
|
if packages_dir:
|
|
|
|
command += ["--transform", f"s,{packages_dir}/,packages/,g"]
|
|
|
|
|
|
|
|
command += [
|
2021-03-08 18:10:26 +01:00
|
|
|
"--transform",
|
2021-04-13 17:47:48 +02:00
|
|
|
f"s,^{blender_srcdir.name}/,blender-{version}/,g",
|
2023-04-05 09:27:42 +02:00
|
|
|
"--use-compress-program=xz -1",
|
2021-03-08 18:10:26 +01:00
|
|
|
"--create",
|
|
|
|
f"--file={tarball}",
|
|
|
|
f"--files-from={manifest}",
|
|
|
|
# Without owner/group args, extracting the files as root will
|
|
|
|
# use ownership from the tar archive:
|
|
|
|
"--owner=0",
|
|
|
|
"--group=0",
|
|
|
|
]
|
2021-04-13 17:47:48 +02:00
|
|
|
|
|
|
|
subprocess.run(command, check=True, timeout=3600)
|
2021-03-08 18:10:26 +01:00
|
|
|
print("OK")
|
|
|
|
|
|
|
|
|
|
|
|
def create_checksum_file(tarball: Path) -> None:
|
|
|
|
md5_path = tarball.with_name(tarball.name + ".md5sum")
|
|
|
|
print(f'Creating checksum: "{md5_path}" ...', end="", flush=True)
|
|
|
|
command = [
|
|
|
|
"md5sum",
|
|
|
|
# The name is enough, as the tarball resides in the same dir as the MD5
|
|
|
|
# file, and that's the current working directory.
|
|
|
|
tarball.name,
|
|
|
|
]
|
|
|
|
md5_cmd = subprocess.run(
|
|
|
|
command, stdout=subprocess.PIPE, check=True, text=True, timeout=300
|
|
|
|
)
|
2021-03-12 13:39:08 +11:00
|
|
|
with md5_path.open("w", encoding="utf-8") as outfile:
|
2021-03-08 18:10:26 +01:00
|
|
|
outfile.write(md5_cmd.stdout)
|
|
|
|
print("OK")
|
|
|
|
|
|
|
|
|
|
|
|
def cleanup(manifest: Path) -> None:
|
|
|
|
print("Cleaning up ...", end="", flush=True)
|
|
|
|
if manifest.exists():
|
|
|
|
manifest.unlink()
|
|
|
|
print("OK")
|
|
|
|
|
|
|
|
|
2022-04-20 15:08:46 +10:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Low-level commands
|
2021-03-08 18:10:26 +01:00
|
|
|
|
|
|
|
|
2025-02-28 12:00:57 +01:00
|
|
|
def git_gather_all_folders_to_package(directory: Path = Path(".")) -> Iterable[Path]:
|
|
|
|
"""Generator, yields lines which represents each directory to gather git files from.
|
|
|
|
|
|
|
|
Each directory represents either the top level git repository or a submodule.
|
|
|
|
All submodules that have the 'update = none' setting will be excluded from this list.
|
|
|
|
|
|
|
|
The directory path given to this function will be included in the yielded paths
|
|
|
|
"""
|
|
|
|
|
|
|
|
# For each submodule (recurse into submodules within submodules if they exist)
|
|
|
|
git_main_command = "submodule --quiet foreach --recursive"
|
|
|
|
# Return the path to the submodule and what the value is of their "update" setting
|
|
|
|
# If the "update" setting doesn't exist, only the path to the submodule is returned
|
|
|
|
git_command_args = "'echo $displaypath $(git config --file \"$toplevel/.gitmodules\" --get submodule.$name.update)'"
|
|
|
|
|
|
|
|
# Yield the root directory as this is our top level git repo
|
|
|
|
yield directory
|
|
|
|
|
|
|
|
for line in git_command(f"-C '{directory}' {git_main_command} {git_command_args}"):
|
|
|
|
# Check if we shouldn't include the directory on this line
|
|
|
|
split_line = line.rsplit(maxsplit=1)
|
|
|
|
if len(split_line) > 1 and split_line[-1] == "none":
|
|
|
|
continue
|
|
|
|
path = directory / split_line[0]
|
|
|
|
yield path
|
|
|
|
|
|
|
|
|
2025-05-05 15:10:22 +02:00
|
|
|
def is_path_ignored(file: Path) -> bool:
|
|
|
|
for skip_folder in SKIP_PATHS:
|
|
|
|
if file.is_relative_to(skip_folder):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2021-03-08 18:10:26 +01:00
|
|
|
def git_ls_files(directory: Path = Path(".")) -> Iterable[Path]:
|
|
|
|
"""Generator, yields lines of output from 'git ls-files'.
|
|
|
|
|
|
|
|
Only lines that are actually files (so no directories, sockets, etc.) are
|
|
|
|
returned, and never one from SKIP_NAMES.
|
|
|
|
"""
|
2025-02-28 12:00:57 +01:00
|
|
|
for line in git_command(f"-C '{directory}' ls-files"):
|
2021-03-08 18:10:26 +01:00
|
|
|
path = directory / line
|
|
|
|
if not path.is_file() or path.name in SKIP_NAMES:
|
|
|
|
continue
|
2025-05-05 15:10:22 +02:00
|
|
|
if not is_path_ignored(path):
|
|
|
|
yield path
|
2021-03-08 18:10:26 +01:00
|
|
|
|
|
|
|
|
2025-02-28 12:00:57 +01:00
|
|
|
def git_command(cli_args: str) -> Iterable[str]:
|
2021-03-08 18:10:26 +01:00
|
|
|
"""Generator, yields lines of output from a Git command."""
|
2025-02-28 12:00:57 +01:00
|
|
|
command = "git " + cli_args
|
2021-03-08 18:10:26 +01:00
|
|
|
|
|
|
|
# import shlex
|
|
|
|
# print(">", " ".join(shlex.quote(arg) for arg in command))
|
|
|
|
|
|
|
|
git = subprocess.run(
|
2025-02-28 12:00:57 +01:00
|
|
|
command, stdout=subprocess.PIPE, shell=True, check=True, text=True, timeout=30
|
2021-03-08 18:10:26 +01:00
|
|
|
)
|
|
|
|
for line in git.stdout.split("\n"):
|
|
|
|
if line:
|
|
|
|
yield line
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
if doctest.testmod().failed:
|
|
|
|
raise SystemExit("ERROR: Self-test failed, refusing to run")
|
|
|
|
|
|
|
|
main()
|