-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[doc][build/01] BuildCache api class (#46780)
Move the logic of getting and zipping the doc build artifacts into a class, and add test cases. Subsequently, I'll add the logic to modify the pickle file to remove things such as site-packages dependency, before uploading. Test: - CI --------- Signed-off-by: can <[email protected]>
- Loading branch information
1 parent
eac96b5
commit 7af3ff0
Showing
4 changed files
with
131 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import tempfile | ||
import subprocess | ||
import os | ||
from typing import Set | ||
|
||
import boto3 | ||
|
||
from ci.ray_ci.utils import logger | ||
from ray_release.util import get_write_state_machine_aws_bucket | ||
|
||
|
||
AWS_CACHE_KEY = "doc_build" | ||
|
||
|
||
class BuildCache: | ||
""" | ||
BuildCache represents the build artifacts generated from the doc build process, | ||
massaged to be used as a cache for the next build process | ||
""" | ||
|
||
def __init__(self, cache_dir: str): | ||
""" | ||
Args: | ||
cache_dir: The directory where the build artifacts are stored | ||
""" | ||
self._cache_dir = cache_dir | ||
|
||
def upload(self) -> None: | ||
""" | ||
Upload the build artifacts to S3 | ||
""" | ||
logger.info("Obtaining the list of cache files.") | ||
cache_files = self._get_cache() | ||
|
||
logger.info("Creating a tarball of the cache files.") | ||
doc_tarball = self._zip_cache(cache_files) | ||
|
||
logger.info("Upload the tarball to S3.") | ||
self._upload_cache(doc_tarball) | ||
|
||
logger.info(f"Successfully uploaded {doc_tarball} to S3.") | ||
|
||
def _get_cache(self) -> Set[str]: | ||
""" | ||
Get the list of cache files | ||
""" | ||
untracked_files = ( | ||
subprocess.check_output( | ||
["git", "ls-files", "--others", "-z"], | ||
cwd=self._cache_dir, | ||
) | ||
.decode("utf-8") | ||
.split(os.linesep) | ||
) | ||
|
||
return {file for file in untracked_files if file} | ||
|
||
def _zip_cache(self, cache_files: Set[str]) -> str: | ||
""" | ||
Create a tarball of the cache files | ||
""" | ||
with tempfile.NamedTemporaryFile(mode="w+t") as temp_file: | ||
temp_file.write("\n".join(cache_files)) | ||
doc_tarball = f'{os.environ["BUILDKITE_COMMIT"]}.tgz' | ||
doc_tarball_path = os.path.join(self._cache_dir, doc_tarball) | ||
subprocess.run( | ||
["tar", "-cvzf", doc_tarball_path, "-T", temp_file.name], | ||
cwd=self._cache_dir, | ||
) | ||
|
||
return doc_tarball | ||
|
||
def _upload_cache(self, doc_tarball: str) -> None: | ||
boto3.client("s3").upload_file( | ||
os.path.join(self._cache_dir, doc_tarball), | ||
get_write_state_machine_aws_bucket(), | ||
f"{AWS_CACHE_KEY}/{doc_tarball}", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import sys | ||
import os | ||
import pytest | ||
import tempfile | ||
from unittest import mock | ||
|
||
from ci.ray_ci.doc.build_cache import BuildCache | ||
|
||
|
||
@mock.patch("subprocess.check_output") | ||
def test_get_cache(mock_check_output): | ||
mock_check_output.return_value = b"file1\nfile2\nfile3" | ||
assert BuildCache("/path/to/cache")._get_cache() == {"file1", "file2", "file3"} | ||
|
||
|
||
@mock.patch("os.environ", {"BUILDKITE_COMMIT": "12345"}) | ||
def test_zip_cache(): | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
files = set() | ||
for i in range(3): | ||
file_name = f"file_{i}.txt" | ||
with open(os.path.join(temp_dir, file_name), "w") as file: | ||
file.write("hi") | ||
files.add(file_name) | ||
|
||
assert BuildCache(temp_dir)._zip_cache(files) == "12345.tgz" | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(pytest.main(["-vv", __file__])) |