Skip to content

Commit

Permalink
Update deploy-agent future deps
Browse files Browse the repository at this point in the history
The "future" packages may have been useful for python 2/3
compatibility, but the current targets are: 3.8, 3.12

Drop the unnecessary dependencies:
- print_function - Feature is included in 3.0
- absolute_import - Feature is included in 3.0
- with_metaclass - In 3.0, inheriting object makes no difference
- PY3 - Evaluates to True in 3.0

References:
- https://docs.python.org/3/library/__future__.html#module-contents
  • Loading branch information
osoriano committed Jul 31, 2024
1 parent a6ead08 commit 542e09a
Show file tree
Hide file tree
Showing 11 changed files with 15 additions and 51 deletions.
5 changes: 2 additions & 3 deletions deploy-agent/deployd/client/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from abc import ABCMeta, abstractmethod
from future.utils import with_metaclass
from abc import ABC, abstractmethod


class BaseClient(with_metaclass(ABCMeta, object)):
class BaseClient(ABC):
"""This class plays a role as an interface defining methods for agent to
communicate with teletraan service.
"""
Expand Down
10 changes: 2 additions & 8 deletions deploy-agent/deployd/common/caller.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import time
from typing import Optional, Tuple

from future.utils import PY3

log = logging.getLogger(__name__)


Expand All @@ -32,12 +30,8 @@ def call_and_log(cmd, **kwargs) -> Tuple[Optional[str], str, Optional[int]]:
output = ""
start = time.time()
try:
if PY3:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, encoding='utf-8', **kwargs)
else:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, **kwargs)
process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, encoding='utf-8', **kwargs)
while process.poll() is None:
line = process.stdout.readline()
if line:
Expand Down
1 change: 0 additions & 1 deletion deploy-agent/deployd/common/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
# Copyright 2016 Pinterest, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
24 changes: 6 additions & 18 deletions deploy-agent/deployd/common/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import traceback
from typing import Tuple

from future.utils import PY3

from deployd.common.types import DeployReport, PingStatus, PRE_STAGE_STEPS, AgentStatus

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -111,14 +109,11 @@ def run_cmd(self, cmd, **kw) -> DeployReport:

# sleep some seconds before next poll
sleep_time = self._get_sleep_interval(start, self.PROCESS_POLL_INTERVAL)
if PY3:
# Wait up to sleep_time for the process to terminate (new in Python 3.3)
try:
process.wait(sleep_time)
except subprocess.TimeoutExpired:
pass
else:
time.sleep(sleep_time)
# Wait up to sleep_time for the process to terminate (new in Python 3.3)
try:
process.wait(sleep_time)
except subprocess.TimeoutExpired:
pass

# finish executing sub process
deploy_report.error_code = process.returncode
Expand Down Expand Up @@ -202,14 +197,7 @@ def _graceful_shutdown(self, process) -> None:
try:
log.info('Gracefully shutdown currently running process with timeout {}'.format(self.TERMINATE_TIMEOUT))
os.killpg(process.pid, signal.SIGTERM)
if PY3:
process.wait(self.TERMINATE_TIMEOUT)
else:
start_time = datetime.datetime.now()
while process.poll() is None:
if (datetime.datetime.now() - start_time).seconds > self.TERMINATE_TIMEOUT:
raise Exception('Timed out while waiting for the process to shutdown')
time.sleep(min(self.PROCESS_POLL_INTERVAL, self.TERMINATE_TIMEOUT))
process.wait(self.TERMINATE_TIMEOUT)
except Exception as e:
log.debug('Failed to gracefully shutdown: {}'.format(e))
Executor._kill_process(process)
Expand Down
13 changes: 1 addition & 12 deletions deploy-agent/deployd/common/single_instance.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function
from __future__ import absolute_import
# Copyright 2016 Pinterest, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -20,7 +18,6 @@
import errno
import fcntl
from . import utils
from future.utils import PY3
import tempfile
log = logging.getLogger(__name__)
LOCKFILE_DIR = '/var/lock'
Expand Down Expand Up @@ -61,12 +58,4 @@ def __init__(self) -> None:
utils.exit_abruptly(1)

def _create_lock_dir(self) -> None:
if PY3:
os.makedirs(LOCKFILE_DIR, exist_ok=True)
else:
# Need to handle the case when lock dir exists in py2
try:
os.makedirs(LOCKFILE_DIR) # py2
except OSError as e:
if e.errno != errno.EEXIST:
raise
os.makedirs(LOCKFILE_DIR, exist_ok=True)
1 change: 0 additions & 1 deletion deploy-agent/deployd/common/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
# Copyright 2016 Pinterest, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 1 addition & 1 deletion deploy-agent/deployd/download/download_helper_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from typing import Optional

import boto3
from future.moves.urllib.parse import urlparse
from urllib.parse import urlparse

from deployd.download.download_helper import DownloadHelper
from deployd.download.s3_download_helper import S3DownloadHelper
Expand Down
1 change: 0 additions & 1 deletion deploy-agent/deployd/download/http_download_helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import absolute_import
# Copyright 2016 Pinterest, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
1 change: 0 additions & 1 deletion deploy-agent/deployd/staging/stager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import absolute_import
# Copyright 2016 Pinterest, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
3 changes: 1 addition & 2 deletions deploy-agent/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
"gevent==24.2.1; python_version >= '3.12'",
"lockfile==0.10.2",
"boto3==1.34.134",
"python-daemon==2.0.6",
"future==0.18.2"
"python-daemon==2.0.6"
]

setup(
Expand Down
5 changes: 2 additions & 3 deletions deploy-agent/tests/unit/deploy/client/test_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
# limitations under the License.

import unittest
from abc import ABCMeta, abstractmethod
from future.utils import with_metaclass
from abc import ABC, abstractmethod
from tests import TestCase

from deployd.client.base_client import BaseClient
Expand All @@ -34,7 +33,7 @@ def test_abc_equivalent_to_old(self):
"""
Make sure that new changes to base client extend the original class
"""
class OldBaseClient(with_metaclass(ABCMeta, object)):
class OldBaseClient(ABC):
@abstractmethod
def send_reports(self, env_reports=None):
pass
Expand Down

0 comments on commit 542e09a

Please sign in to comment.