Coverage for glotter / containerfactory.py: 95%

59 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-10-29 13:08 +0000

1import os 

2import shutil 

3import tempfile 

4from datetime import datetime, timedelta 

5from functools import cache 

6from uuid import uuid4 as uuid 

7 

8import docker 

9 

10 

11@cache 

12def get_container_factory(): 

13 """ 

14 Get ContainerFactory as a singleton 

15 """ 

16 return ContainerFactory() 

17 

18 

19class ContainerFactory: 

20 def __init__(self): 

21 """ 

22 Initialize a ContainerFactory 

23 """ 

24 self._containers = {} 

25 self._volume_dis = {} 

26 self._client = docker.from_env() 

27 self._api_client = self._client.api 

28 

29 def get_container(self, source): 

30 """ 

31 Returns a running container for a give source. This will return an existing container if one exists 

32 or create a new one if necessary 

33 

34 :param source: the source to use inside the container 

35 :return: a running container specific to the source 

36 """ 

37 key = source.full_path 

38 

39 tmp_dir = tempfile.mkdtemp() 

40 os.chmod(tmp_dir, 0o777) 

41 shutil.copy(source.full_path, tmp_dir) 

42 self._volume_dis[key] = tmp_dir 

43 

44 image = self.get_image(source.test_info.container_info) 

45 volume_info = {tmp_dir: {"bind": "/src", "mode": "rw"}} 

46 if key not in self._containers: 

47 self._containers[key] = self._client.containers.run( 

48 image=image, 

49 name=f"{source.name}_{uuid().hex}", 

50 command="sleep 1h", 

51 working_dir="/src", 

52 volumes=volume_info, 

53 detach=True, 

54 entrypoint="", 

55 security_opt=["seccomp=unconfined"], 

56 ) 

57 return self._containers[key] 

58 

59 def get_image(self, container_info, quiet=False, parallel=False): 

60 """ 

61 Pull a docker image 

62 

63 :param container_info: metadata about the image to pull 

64 :param quiet: whether to print output while downloading 

65 :param parallel: whether image download is occurring in parallel 

66 :return: a docker image 

67 """ 

68 images = self._client.images.list(name=f"{container_info.image}:{container_info.tag!s}") 

69 if len(images) == 1: 

70 return images[0] 

71 if not quiet: 

72 end_char = "\n" if parallel else "" 

73 print( 

74 f"Pulling {container_info.image}:{container_info.tag}... ", 

75 end=end_char, 

76 flush=True, 

77 ) 

78 last_update = datetime.now() 

79 for _ in self._api_client.pull( 

80 repository=container_info.image, 

81 tag=str(container_info.tag), 

82 stream=True, 

83 decode=True, 

84 ): 

85 if not quiet and not parallel and datetime.now() - last_update > timedelta(seconds=5): 

86 print("... ", end="", flush=True) 

87 last_update = datetime.now() 

88 if not quiet: 

89 if parallel: 89 ↛ 90line 89 didn't jump to line 90 because the condition on line 89 was never true

90 print( 

91 f"... done pulling {container_info.image}:{container_info.tag}", 

92 flush=True, 

93 ) 

94 else: 

95 print("done", flush=True) 

96 

97 images = self._client.images.list(name=f"{container_info.image}:{container_info.tag!s}") 

98 if len(images) == 1: 98 ↛ 101line 98 didn't jump to line 101 because the condition on line 98 was always true

99 return images[0] 

100 

101 return None 

102 

103 def remove_image(self, container_info): 

104 """ 

105 Remove a docker image 

106 

107 :param container_info: metadata about the image to remove 

108 """ 

109 

110 image_name = f"{container_info.image}:{container_info.tag!s}" 

111 images = self._client.images.list(name=f"{container_info.image}:{container_info.tag!s}") 

112 if len(images) == 1: 

113 print(f"Removing {image_name}", flush=True) 

114 self._client.images.remove(image=image_name, force=True) 

115 

116 def cleanup(self, source): 

117 """ 

118 Cleanup docker container and temporary folder. Also remove both from their 

119 respective dictionaries 

120 

121 :param source: source for determining what to cleanup 

122 """ 

123 key = source.full_path 

124 

125 self._containers[key].remove(v=True, force=True) 

126 shutil.rmtree(self._volume_dis[key], ignore_errors=True) 

127 

128 del self._volume_dis[key] 

129 del self._containers[key]