Skip to content

Commit

Permalink
[deploy-agent] Return type hints in staging & types folders (#1405)
Browse files Browse the repository at this point in the history
* Add type hints
  • Loading branch information
gzpcho authored Feb 16, 2024
1 parent c2d64a6 commit cc1d6fd
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 26 deletions.
11 changes: 6 additions & 5 deletions deploy-agent/deployd/staging/stager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import shutil
import traceback
import logging
from typing import Optional

from deployd.common import LOG_FORMAT
from deployd.common.caller import Caller
Expand All @@ -35,7 +36,7 @@ class Stager(object):
_script_dirname = "teletraan"
_template_dirname = "teletraan_template"

def __init__(self, config, build, target, env_name, transformer=None):
def __init__(self, config, build, target, env_name, transformer=None) -> None:
self._build_dir = config.get_builds_directory()
self._user_role = config.get_user_role()
agent_dir = config.get_agent_directory()
Expand All @@ -45,7 +46,7 @@ def __init__(self, config, build, target, env_name, transformer=None):
self._target = target
self._env_name = env_name

def enable_package(self):
def enable_package(self) -> int:
"""Set the enabled build.
"""
old_build = self.get_enabled_build()
Expand Down Expand Up @@ -90,7 +91,7 @@ def enable_package(self):
finally:
return status_code

def get_enabled_build(self):
def get_enabled_build(self) -> Optional[str]:
"""Figure out what build is enabled by looking at symlinks."""
if not os.path.exists(self._target):
if (os.path.islink(self._target) and not
Expand All @@ -110,7 +111,7 @@ def get_enabled_build(self):

return symlink_target.rsplit("/", 1)[-1]

def transform_script(self):
def transform_script(self) -> None:
script_dir = os.path.join(self._target, self._script_dirname)
if not os.path.exists(script_dir):
return
Expand All @@ -125,7 +126,7 @@ def transform_script(self):
script_dirname=self._script_dirname)


def main():
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-f', '--config-file', dest='config_file', default=None,
help="the deploy agent conf file filename path. If none, "
Expand Down
11 changes: 6 additions & 5 deletions deploy-agent/deployd/staging/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from string import Template
import re
import logging
from typing import List
from deployd import IS_PINTEREST

log = logging.getLogger(__name__)
Expand All @@ -29,12 +30,12 @@ class TeletraanTemplate(Template):

class Transformer(object):

def __init__(self, agent_dir, env_name, dict_fn=None):
def __init__(self, agent_dir, env_name, dict_fn=None) -> None:
self._agent_dir = agent_dir
self._env_name = env_name
self._load_config(dict_fn)

def _load_config(self, fn):
def _load_config(self, fn) -> None:
if not fn:
fn = os.path.join(self._agent_dir, "{}_SCRIPT_CONFIG".format(self._env_name))

Expand All @@ -45,10 +46,10 @@ def _load_config(self, fn):
with open(fn, 'r') as f:
self._dictionary = dict((n.strip('\"\n\' ') for n in line.split("=", 1)) for line in f)

def dict_size(self):
def dict_size(self) -> int:
return len(self._dictionary)

def _translate(self, from_path, to_path):
def _translate(self, from_path, to_path) -> None:
try:
with open(from_path, 'r') as f:
res = f.read()
Expand All @@ -73,7 +74,7 @@ def _translate(self, from_path, to_path):
log.error('Fail to translate script {}, stacktrace: {}'.format(from_path,
traceback.format_exc()))

def transform_scripts(self, script_dir, template_dirname, script_dirname):
def transform_scripts(self, script_dir, template_dirname, script_dirname) -> List:
scripts = []
suffix = ".tmpl"
try:
Expand Down
15 changes: 9 additions & 6 deletions deploy-agent/deployd/types/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
# limitations under the License.


from typing import Tuple


class Build(object):

def __init__(self, jsonValue=None):
def __init__(self, jsonValue=None) -> None:
self.buildId = None
self.buildName = None
self.buildVersion = None
Expand Down Expand Up @@ -43,7 +46,7 @@ def __init__(self, jsonValue=None):
self.publishInfo = jsonValue.get('publishInfo')
self.publishDate = jsonValue.get('publishDate')

def __key(self):
def __key(self) -> Tuple:
return (self.buildId,
self.buildName,
self.buildVersion,
Expand All @@ -57,20 +60,20 @@ def __key(self):
self.publishInfo,
self.publishDate)

def __hash__(self):
def __hash__(self) -> int:
return hash(self.__key())

def __eq__(self, other):
def __eq__(self, other) -> bool:
""" compare Builds """
return isinstance(other, Build) \
and self.__key() == other.__key()

def __ne__(self, other):
def __ne__(self, other) -> bool:
""" compare Builds """
return not (isinstance(other, Build)
and self.__key() == other.__key())

def __str__(self):
def __str__(self) -> str:
return "Build(buildId={}, buildName={}, buildVersion={}, artifactUrl={}, scm={}, " \
"scmRepo={}, scmBranch={}, scmCommit={}, scmInfo={}, commitDate={}, publishInfo={}, " \
"publishDate={})".format(self.buildId, self.buildName, self.buildVersion,
Expand Down
13 changes: 7 additions & 6 deletions deploy-agent/deployd/types/deploy_goal.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Tuple
from deployd.types.build import Build
from deployd.types.deploy_stage import DeployStage

class DeployGoal(object):
def __init__(self, jsonValue=None):
def __init__(self, jsonValue=None) -> None:
self.deployId = None
self.envId = None
self.envName = None
Expand Down Expand Up @@ -51,7 +52,7 @@ def __init__(self, jsonValue=None):
self.firstDeploy = jsonValue.get('firstDeploy')
self.isDocker = jsonValue.get('isDocker')

def __key(self):
def __key(self) -> Tuple:
return (self.deployId,
self.envId,
self.envName,
Expand All @@ -65,20 +66,20 @@ def __key(self):
self.firstDeploy,
self.isDocker)

def __hash__(self):
def __hash__(self) -> int:
return hash(self.__key())

def __eq__(self, other):
def __eq__(self, other) -> bool:
""" compare DeployGoals """
return isinstance(other, DeployGoal) \
and self.__key() == other.__key()

def __ne__(self, other):
def __ne__(self, other) -> bool:
""" compare DeployGoals """
return not (isinstance(other, DeployGoal)
and self.__key() == other.__key())

def __str__(self):
def __str__(self) -> str:
return "DeployGoal(deployId={}, envId={}, envName={}, stageName={}, stageType={}, " \
"deployStage={}, build={}, deployAlias={}, agentConfig={}," \
"scriptVariables={}, firstDeploy={}, isDocker={})".format(self.deployId, self.envId, self.envName,
Expand Down
4 changes: 2 additions & 2 deletions deploy-agent/deployd/types/ping_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class PingReport(object):

def __init__(self, jsonValue=None):
def __init__(self, jsonValue=None) -> None:
self.deployId = None
self.envId = None
self.envName = None
Expand Down Expand Up @@ -62,7 +62,7 @@ def __init__(self, jsonValue=None):
self.redeploy = jsonValue.get('redeploy')
self.wait = jsonValue.get('wait')

def __str__(self):
def __str__(self) -> str:
return "PingReport(deployId={}, envId={}, deployStage={}, status={}, " \
"errorCode={}, errorMessage={}, failCount={}, extraInfo={}, " \
"deployAlias={}, containerHealthStatus={}, agentState={})".format(self.deployId, self.envId, self.deployStage,
Expand Down
4 changes: 2 additions & 2 deletions deploy-agent/deployd/types/ping_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


class PingResponse(object):
def __init__(self, jsonValue=None):
def __init__(self, jsonValue=None) -> None:
self.opCode = OpCode.NOOP
self.deployGoal = None

Expand All @@ -32,5 +32,5 @@ def __init__(self, jsonValue=None):
if jsonValue.get('deployGoal'):
self.deployGoal = DeployGoal(jsonValue=jsonValue.get('deployGoal'))

def __str__(self):
def __str__(self) -> str:
return "PingResponse(opCode={}, deployGoal={})".format(self.opCode, self.deployGoal)

0 comments on commit cc1d6fd

Please sign in to comment.