Skip to content

Commit 533a8df

Browse files
authored
Gaiaplat 1838 : Applying PyLint and Resolving any warnings from Docker_Dev (#1360)
* GAIAPLAT-1838 - Starting with initial GDEV https://gaiaplatform.atlassian.net/browse/GAIAPLAT-1838 * Fixing Pylint Warnings
1 parent c405b80 commit 533a8df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+656
-445
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,5 @@ repos:
185185
rev: v2.11.1
186186
hooks:
187187
- id: pylint
188-
exclude: ^(dev_tools/gdev/|dev_tools/docker_dev/|production/tools/tests/gaiat/lit.cfg.py|production/tests/python/ps_mem.py)
188+
exclude: ^(dev_tools/gdev/|production/tools/tests/gaiat/lit.cfg.py|production/tests/python/ps_mem.py)
189189
args: [--disable=R0801]

dev_tools/docker_dev/gdev/__main__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
from gdev.main import DockerDev
1616

1717
def main() -> int:
18+
"""
19+
Module main entry point into the application.
20+
"""
1821
DockerDev().main()
1922
return 0
2023

dev_tools/docker_dev/gdev/cmd/gen/_abc/build.py

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
# All rights reserved.
66
#############################################
77

8+
"""
9+
Module to help build a docker image from its component pieces.
10+
"""
11+
812
from __future__ import annotations
913
from abc import ABC, abstractmethod
1014
import os
11-
from typing import Awaitable, Callable, Iterable, Mapping, Union
15+
from typing import Iterable, Mapping
1216

13-
from gdev.custom.pathlib import Path
17+
from gdev.custom.gaia_path import GaiaPath
1418
from gdev.dependency import Dependency
1519
from gdev.host import Host
1620
from gdev.third_party.atools import memoize
17-
from .cfg import GenAbcCfg
18-
from .dockerfile import GenAbcDockerfile
21+
from gdev.cmd.gen._abc.cfg import GenAbcCfg
22+
from gdev.cmd.gen._abc.dockerfile import GenAbcDockerfile
1923

2024

2125
# We require buildkit support for inline caching of multi-stage dockerfiles. It's also way faster
@@ -44,15 +48,6 @@ def dockerfile(self) -> GenAbcDockerfile:
4448
"""
4549
raise NotImplementedError
4650

47-
# TODO: Used?
48-
@memoize
49-
def __get_actual_git_hash(self) -> str:
50-
actual_git_hash = self._get_actual_label_value('GitHash')
51-
52-
self.log.debug(f'{actual_git_hash = }')
53-
54-
return actual_git_hash
55-
5651
@memoize
5752
def _get_actual_label_value(self, name: str) -> str:
5853
"""
@@ -72,18 +67,20 @@ def _get_actual_label_value(self, name: str) -> str:
7267
@memoize
7368
def _get_actual_label_value_by_name(self) -> Mapping[str, str]:
7469
"""
75-
Get the hash of an image with the actual label values that are called for by the configuration.
70+
Get the hash of an image with the actual label values that are called
71+
for by the configuration.
7672
"""
7773
return {'GitHash': self._get_actual_label_value(name='GitHash')}
7874

7975
@memoize
8076
def get_actual_label_value_by_name(self) -> Mapping[str, str]:
8177
"""
82-
Get the hash of an image with the actual label values that are called for by the configuration.
78+
Get the hash of an image with the actual label values that are called
79+
for by the configuration.
8380
"""
8481

8582
actual_label_value_by_name = self._get_actual_label_value_by_name()
86-
self.log.debug(f'{actual_label_value_by_name = }')
83+
self.log.debug('actual_label_value_by_name = %s', actual_label_value_by_name)
8784
return actual_label_value_by_name
8885

8986
@memoize
@@ -102,7 +99,7 @@ def inner(dockerfile: GenAbcDockerfile) -> Iterable[str]:
10299

103100
base_build_names = tuple(inner(self.dockerfile))
104101

105-
self.log.debug(f'{base_build_names = }')
102+
self.log.debug('base_build_names = %s', base_build_names)
106103

107104
return base_build_names
108105

@@ -119,7 +116,7 @@ def get_sha(self) -> str:
119116
else:
120117
sha = ''
121118

122-
self.log.debug(f'{sha = }')
119+
self.log.debug('sha = %s', sha)
123120

124121
return sha
125122

@@ -131,44 +128,18 @@ def get_tag(self) -> str:
131128

132129
tag = f'{self.dockerfile.get_name()}:latest'
133130

134-
self.log.debug(f'{tag = }')
131+
self.log.debug('tag = %s', tag)
135132

136133
return tag
137134

138-
# TODO unused?
139-
@memoize
140-
def __get_uncommitted(self) -> str:
141-
seen_dockerfiles = set()
142-
143-
def inner(dockerfile: GenAbcDockerfile) -> Iterable[Path]:
144-
paths = set()
145-
if dockerfile not in seen_dockerfiles:
146-
seen_dockerfiles.add(dockerfile)
147-
paths.add( Path.repo() / Path(dockerfile.options.target))
148-
for input_dockerfile in dockerfile.get_input_dockerfiles():
149-
paths |= inner(input_dockerfile)
150-
return paths
151-
152-
uncommitted = '\n'.join(
153-
Host.execute_and_get_lines_sync(
154-
f'git status --short ' + ' '.join(
155-
f'{path.parent}' for path in inner(self.dockerfile)
156-
)
157-
)
158-
)
159-
160-
self.log.debug(f'{uncommitted = }')
161-
162-
return uncommitted
163-
164135
@memoize
165136
def __get_wanted_git_hash(self) -> str:
166137
"""
167138
Request that GIT provides information about the SHA of the HEAD node.
168139
"""
169140
wanted_git_hash = Host.execute_and_get_line_sync('git rev-parse HEAD')
170141

171-
self.log.debug(f'{wanted_git_hash = }')
142+
self.log.debug('wanted_git_hash = %s', wanted_git_hash)
172143

173144
return wanted_git_hash
174145

@@ -185,7 +156,7 @@ def get_wanted_label_value_by_name(self) -> Mapping[str, str]:
185156
"""
186157
wanted_label_value_by_name = self._get_wanted_label_value_by_name()
187158

188-
self.log.debug(f'{wanted_label_value_by_name = }')
159+
self.log.debug('wanted_label_value_by_name = %s', wanted_label_value_by_name)
189160

190161
return wanted_label_value_by_name
191162

@@ -203,8 +174,7 @@ def main(self) -> None:
203174
for base_build_name in self.__get_base_build_names()])
204175
cached_images = f"--cache-from {cached_images}"
205176

206-
# TODO query remotely for cached build sources.
207-
self.log.info(f'Creating image "{self.get_tag()}"')
177+
self.log.info('Creating image "%s"', self.get_tag())
208178
Host.execute_sync(
209179
f'docker buildx build'
210180
f' -f {self.dockerfile.path}'
@@ -229,10 +199,13 @@ def main(self) -> None:
229199

230200
f' {cached_images}'
231201

232-
f' {Path.repo()}'
202+
f' {GaiaPath.repo()}'
233203
)
234-
Host.execute_sync(f'docker image prune -f')
204+
Host.execute_sync('docker image prune -f')
235205

206+
# pylint: disable=import-outside-toplevel
207+
#
208+
# Required to resolve cyclical dependency issues.
236209
@memoize
237210
def cli_entrypoint(self) -> None:
238211
"""
@@ -242,7 +215,8 @@ def cli_entrypoint(self) -> None:
242215
if not self.options.mixins:
243216
build = self
244217
else:
245-
from .._custom.build import GenCustomBuild
218+
from gdev.cmd.gen._custom.build import GenCustomBuild
246219
build = GenCustomBuild(options=self.options, base_build=self)
247220

248221
build.run()
222+
# pylint: enable=import-outside-toplevel

dev_tools/docker_dev/gdev/cmd/gen/_abc/cfg.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,31 @@
1313
import re
1414
from typing import FrozenSet, Iterable, Pattern
1515

16-
from gdev.custom.pathlib import Path
16+
from gdev.custom.gaia_path import GaiaPath
1717
from gdev.dependency import Dependency
1818
from gdev.third_party.atools import memoize
1919

20-
2120
class GenAbcCfg(Dependency, ABC):
2221
"""
2322
Class to parse the target gdev.cfg for build rules.
2423
"""
2524

2625
@property
2726
@memoize
28-
def path(self) -> Path:
27+
def path(self) -> GaiaPath:
2928
"""
3029
Determine the path to the configuration file that we are observing.
3130
"""
32-
path = Path.repo() / self.options.target / 'gdev.cfg'
33-
self.log.debug(f'{path = }')
31+
path = GaiaPath.repo() / self.options.target / 'gdev.cfg'
32+
self.log.debug('path = %s', path)
3433
return path
3534

3635
@property
3736
def section_name(self) -> str:
3837
"""
3938
Determine the section name in the configuration based on the type of the class.
4039
"""
41-
return Path(getfile(type(self))).parent.name.strip('_')
40+
return GaiaPath(getfile(type(self))).parent.name.strip('_')
4241

4342
@memoize
4443
def __get_begin_pattern(self) -> Pattern:
@@ -47,30 +46,36 @@ def __get_begin_pattern(self) -> Pattern:
4746
"""
4847

4948
begin_pattern = re.compile(fr'^\[{self.section_name}]$')
50-
self.log.debug(f'{begin_pattern = }')
49+
self.log.debug('begin_pattern = %s', begin_pattern)
5150
return begin_pattern
5251

5352
@memoize
5453
def __get_end_pattern(self) -> Pattern:
5554
"""
5655
Get the regex pattern that identifies the end of the section.
5756
"""
58-
end_pattern = re.compile(fr'^(# .*|)\[.+]$')
59-
self.log.debug(f'{end_pattern = }')
57+
end_pattern = re.compile(r'^(# .*|)\[.+]$')
58+
self.log.debug('end_pattern = %s', end_pattern)
6059
return end_pattern
6160

61+
62+
# pylint: disable=eval-used
6263
@staticmethod
6364
@memoize
64-
def get_lines(cfg_enables: FrozenSet[str], path: Path) -> Iterable[str]:
65+
def get_lines(cfg_enables: FrozenSet[str], path: GaiaPath) -> Iterable[str]:
6566
"""
66-
Get the various lines for the section with the inline notations like `{enable_if('CI_GitHub')}`
67-
replaced with `# enable by setting "{'CI_GitHub'}": `.
67+
Get the various lines for the section with the inline notations like
68+
`{enable_if('CI_GitHub')}` replaced with `# enable by setting "{'CI_GitHub'}": `.
6869
"""
69-
return tuple([
70+
# Per suggestions from https://realpython.com/python-eval-function/:
71+
# - `__locals` field set to an empty dictionary
72+
# - `__globals` field set to contain empty `__builtins__` item
73+
74+
return tuple((
7075
eval(
7176
f'fr""" {line} """',
7277
{
73-
'build_dir': Path.build,
78+
'build_dir': GaiaPath.build,
7479
'enable_if': lambda enable:
7580
'' if enable in cfg_enables
7681
else f'# enable by setting "{enable}": ',
@@ -89,15 +94,17 @@ def get_lines(cfg_enables: FrozenSet[str], path: Path) -> Iterable[str]:
8994
'enable_if_not_all': lambda *enables:
9095
'' if not (set(enables) in cfg_enables)
9196
else f'# enable by not setting all of "{sorted(set(enables))}": ',
92-
'source_dir': Path.source
93-
}
97+
'source_dir': GaiaPath.source,
98+
"__builtins__": {}
99+
}, {}
94100
)[1:-1]
95101
for line in (GenAbcCfg.__get_raw_text(path)).splitlines()
96-
])
102+
))
103+
# pylint: enable=eval-used
97104

98105
@staticmethod
99106
@memoize
100-
def __get_raw_text(path: Path) -> str:
107+
def __get_raw_text(path: GaiaPath) -> str:
101108
if not path.is_file():
102109
raise Dependency.Abort(f'File "<repo_root>/{path.context()}" must exist.')
103110

@@ -142,7 +149,7 @@ def get_section_lines(self) -> Iterable[str]:
142149

143150
section_lines = tuple(section_lines)
144151

145-
self.log.debug(f'{section_lines = }')
152+
self.log.debug('section_lines = %s', section_lines)
146153

147154
return section_lines
148155

0 commit comments

Comments
 (0)