Welcome to mirror list, hosted at ThFree Co, Russian Federation.

make_source_archive.py « utils « build_files - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 271ca358f7e6c338d91f4616a6e744acb8931f3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python3

import dataclasses
import os
import re
import subprocess
from pathlib import Path
from typing import Iterable, TextIO

# This script can run from any location,
# output is created in the $CWD
#
# 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.

SKIP_NAMES = {
    ".gitignore",
    ".gitmodules",
    ".arcconfig",
}


def main() -> None:
    output_dir = Path(".").absolute()
    blender_srcdir = Path(__file__).absolute().parent.parent.parent
    print(f"Source dir: {blender_srcdir}")

    version = parse_blender_version(blender_srcdir)
    manifest = output_dir / f"blender-{version}-manifest.txt"
    tarball = output_dir / f"blender-{version}.tar.xz"

    os.chdir(blender_srcdir)
    create_manifest(version, manifest)
    create_tarball(version, tarball, manifest)
    create_checksum_file(tarball)
    cleanup(manifest)
    print("Done!")


@dataclasses.dataclass
class BlenderVersion:
    version: int  # 293 for 2.93.1
    patch: int  # 1 for 2.93.1
    cycle: str  # 'alpha', 'beta', 'release', maybe others.

    @property
    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}"


def parse_blender_version(blender_srcdir: Path) -> BlenderVersion:
    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"],
    )


### Manifest creation


def create_manifest(version: BlenderVersion, outpath: Path) -> None:
    print(f'Building manifest of files:  "{outpath}"...', end="", flush=True)
    with outpath.open("w", encoding="utf-8") as outfile:
        main_files_to_manifest(outfile)
        submodules_to_manifest(version, outfile)
    print("OK")


def main_files_to_manifest(outfile: TextIO) -> None:
    for path in git_ls_files():
        print(path, file=outfile)


def submodules_to_manifest(version: BlenderVersion, outfile: TextIO) -> None:
    skip_addon_contrib = version.is_release

    for line in git_command("submodule"):
        submodule = line.split()[1]

        # Don't use native slashes as GIT for MS-Windows outputs forward slashes.
        if skip_addon_contrib and submodule == "release/scripts/addons_contrib":
            continue

        for path in git_ls_files(Path(submodule)):
            print(path, file=outfile)


def create_tarball(version: BlenderVersion, tarball: Path, manifest: Path) -> None:
    print(f'Creating archive:            "{tarball}" ...', end="", flush=True)
    # Requires GNU `tar`, since `--transform` is used.
    command = [
        "tar",
        "--transform",
        f"s,^,blender-{version}/,g",
        "--use-compress-program=xz -9",
        "--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",
    ]
    subprocess.run(command, check=True, timeout=300)
    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
    )
    with md5_path.open("w", encoding="utf-8") as outfile:
        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")


## Low-level commands


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.
    """
    for line in git_command("-C", str(directory), "ls-files"):
        path = directory / line
        if not path.is_file() or path.name in SKIP_NAMES:
            continue
        yield path


def git_command(*cli_args) -> Iterable[str]:
    """Generator, yields lines of output from a Git command."""
    command = ("git", *cli_args)

    # import shlex
    # print(">", " ".join(shlex.quote(arg) for arg in command))

    git = subprocess.run(
        command, stdout=subprocess.PIPE, check=True, text=True, timeout=30
    )
    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()