Skip to content

Commit

Permalink
2025-02-05T21:40:02Z
Browse files Browse the repository at this point in the history
  • Loading branch information
wrmsr committed Feb 5, 2025
1 parent 0aada59 commit a8aef74
Show file tree
Hide file tree
Showing 6 changed files with 346 additions and 161 deletions.
4 changes: 0 additions & 4 deletions omdev/ci/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from omlish.lite.contextmanagers import AsyncExitStacked
from omlish.os.temp import temp_file_context

from .cache import FileCache
from .compose import DockerComposeRun
from .compose import get_compose_service_dependencies
from .docker.buildcaching import DockerBuildCaching
Expand Down Expand Up @@ -58,15 +57,12 @@ def __init__(
self,
config: Config,
*,
file_cache: ta.Optional[FileCache] = None,

docker_build_caching: DockerBuildCaching,
docker_image_pulling: DockerImagePulling,
) -> None:
super().__init__()

self._config = config
self._file_cache = file_cache

self._docker_build_caching = docker_build_caching
self._docker_image_pulling = docker_image_pulling
Expand Down
2 changes: 1 addition & 1 deletion omdev/ci/docker/buildcaching.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DockerBuildCaching(abc.ABC):
def cached_build_docker_image(
self,
cache_key: str,
build_and_tag: ta.Callable[[str], ta.Awaitable[str]],
build_and_tag: ta.Callable[[str], ta.Awaitable[str]], # image_tag -> image_id
) -> ta.Awaitable[str]:
raise NotImplementedError

Expand Down
136 changes: 136 additions & 0 deletions omdev/ci/tests/harness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# ruff: noqa: PT009 UP006 UP007
import datetime
import os.path
import shlex
import shutil

from omlish.lite.cached import cached_nullary
from omlish.lite.contextmanagers import AsyncExitStacked
from omlish.os.temp import temp_dir_context

from ..cache import DirectoryFileCache
from ..cache import FileCache
from ..ci import Ci
from ..docker.buildcaching import DockerBuildCachingImpl
from ..docker.cache import DockerCacheImpl
from ..docker.imagepulling import DockerImagePullingImpl
from ..shell import ShellCmd


class CiHarness(AsyncExitStacked):
@cached_nullary
def self_dir(self) -> str:
return os.path.dirname(__file__)

@cached_nullary
def self_project_dir(self) -> str:
return os.path.join(self.self_dir(), 'project')

#

@cached_nullary
def temp_dir(self) -> str:
return self._enter_context(temp_dir_context()) # noqa

@cached_nullary
def temp_project_dir(self) -> str:
temp_project_dir = os.path.join(self.temp_dir(), 'project')
shutil.copytree(self.self_project_dir(), temp_project_dir)
return temp_project_dir

#

@cached_nullary
def now_str(self) -> str:
return datetime.datetime.now(tz=datetime.timezone.utc).isoformat() # noqa

#

@cached_nullary
def docker_file(self) -> str:
docker_file = os.path.join(self.temp_project_dir(), 'Dockerfile')
with open(docker_file) as f: # noqa
docker_file_src = f.read()
docker_file_src += f'\nRUN echo {shlex.quote(self.now_str())} > /.timestamp\n'
with open(docker_file, 'w') as f: # noqa
f.write(docker_file_src)
return docker_file

#

@cached_nullary
def cache_dir(self) -> str:
return os.path.join(self.temp_dir(), 'cache')

#

@cached_nullary
def ci_config(self) -> Ci.Config:
return Ci.Config(
project_dir=self.temp_project_dir(),

docker_file=self.docker_file(),

compose_file=os.path.join(self.temp_project_dir(), 'compose.yml'),
service='omlish-ci',

cmd=ShellCmd('true'),

requirements_txts=[
'requirements-dev.txt',
'requirements.txt',
],
)

#

@cached_nullary
def directory_file_cache(self) -> DirectoryFileCache:
return DirectoryFileCache(DirectoryFileCache.Config(
dir=self.cache_dir(),
))

@cached_nullary
def file_cache(self) -> FileCache:
return self.directory_file_cache()

#

@cached_nullary
def docker_cache_impl(self) -> DockerCacheImpl:
return DockerCacheImpl(
file_cache=self.file_cache(),
)

@cached_nullary
def docker_image_pulling_impl(self) -> DockerImagePullingImpl:
return DockerImagePullingImpl(
config=DockerImagePullingImpl.Config(
always_pull=self.ci_config().always_pull,
),

file_cache=self.file_cache(),
docker_cache=self.docker_cache_impl(),
)

@cached_nullary
def docker_build_caching_impl(self) -> DockerBuildCachingImpl:
return DockerBuildCachingImpl(
config=DockerBuildCachingImpl.Config(
service=self.ci_config().service,

always_build=self.ci_config().always_build,
),

docker_cache=self.docker_cache_impl(),
)

#

def make_ci(self) -> Ci:
return Ci(
self.ci_config(),

docker_build_caching=self.docker_build_caching_impl(),
docker_image_pulling=self.docker_image_pulling_impl(),
)
133 changes: 2 additions & 131 deletions omdev/ci/tests/test_ci.py
Original file line number Diff line number Diff line change
@@ -1,137 +1,8 @@
# ruff: noqa: PT009
import datetime
import os.path
import shlex
# ruff: noqa: PT009 UP006 UP007
import shutil
import unittest

from omlish.lite.cached import cached_nullary
from omlish.lite.contextmanagers import AsyncExitStacked
from omlish.os.temp import temp_dir_context

from ..cache import DirectoryFileCache
from ..ci import Ci
from ..docker.buildcaching import DockerBuildCachingImpl
from ..docker.cache import DockerCacheImpl
from ..docker.imagepulling import DockerImagePullingImpl
from ..shell import ShellCmd


class CiHarness(AsyncExitStacked):
@cached_nullary
def self_dir(self) -> str:
return os.path.dirname(__file__)

@cached_nullary
def self_project_dir(self) -> str:
return os.path.join(self.self_dir(), 'project')

#

@cached_nullary
def temp_dir(self) -> str:
return self._enter_context(temp_dir_context()) # noqa

@cached_nullary
def temp_project_dir(self) -> str:
temp_project_dir = os.path.join(self.temp_dir(), 'project')
shutil.copytree(self.self_project_dir(), temp_project_dir)
return temp_project_dir

#

@cached_nullary
def now_str(self) -> str:
return datetime.datetime.now(tz=datetime.timezone.utc).isoformat() # noqa

#

@cached_nullary
def docker_file(self) -> str:
docker_file = os.path.join(self.temp_project_dir(), 'Dockerfile')
with open(docker_file) as f: # noqa
docker_file_src = f.read()
docker_file_src += f'\nRUN echo {shlex.quote(self.now_str())} > /.timestamp\n'
with open(docker_file, 'w') as f: # noqa
f.write(docker_file_src)
return docker_file

#

@cached_nullary
def cache_dir(self) -> str:
return os.path.join(self.temp_dir(), 'cache')

#

@cached_nullary
def ci_config(self) -> Ci.Config:
return Ci.Config(
project_dir=self.temp_project_dir(),

docker_file=self.docker_file(),

compose_file=os.path.join(self.temp_project_dir(), 'compose.yml'),
service='omlish-ci',

cmd=ShellCmd('true'),

requirements_txts=[
'requirements-dev.txt',
'requirements.txt',
],
)

#

@cached_nullary
def directory_file_cache(self) -> DirectoryFileCache:
return DirectoryFileCache(DirectoryFileCache.Config(
dir=self.cache_dir(),
))

#

@cached_nullary
def docker_cache_impl(self) -> DockerCacheImpl:
return DockerCacheImpl(
file_cache=self.directory_file_cache(),
)

@cached_nullary
def docker_image_pulling_impl(self) -> DockerImagePullingImpl:
return DockerImagePullingImpl(
config=DockerImagePullingImpl.Config(
always_pull=self.ci_config().always_pull,
),

file_cache=self.directory_file_cache(),
docker_cache=self.docker_cache_impl(),
)

@cached_nullary
def docker_build_caching_impl(self) -> DockerBuildCachingImpl:
return DockerBuildCachingImpl(
config=DockerBuildCachingImpl.Config(
service=self.ci_config().service,

always_build=self.ci_config().always_build,
),

docker_cache=self.docker_cache_impl(),
)

#

def make_ci(self) -> Ci:
return Ci(
self.ci_config(),

file_cache=self.directory_file_cache(),

docker_build_caching=self.docker_build_caching_impl(),
docker_image_pulling=self.docker_image_pulling_impl(),
)
from .harness import CiHarness


class TestCi(unittest.IsolatedAsyncioTestCase):
Expand Down
Loading

0 comments on commit a8aef74

Please sign in to comment.