Skip to content

Commit

Permalink
Merge pull request #398 from yarikoptic/opt-imports-lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleam committed Mar 22, 2019
2 parents 0925189 + 4c75936 commit c199c04
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
5 changes: 2 additions & 3 deletions reproman/distributions/debian.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import attr
from collections import defaultdict

# OPT: requests is imported at the point of use

import pytz

Expand All @@ -25,8 +25,6 @@

import logging

import requests

from reproman.support.distributions.debian import \
parse_apt_cache_show_pkgs_output, parse_apt_cache_policy_pkgs_output, \
parse_apt_cache_policy_source_info, get_apt_release_file_names, \
Expand Down Expand Up @@ -184,6 +182,7 @@ def _init_apt_sources(self, session,
date.strftime("%Y%m%dT%H%M%SZ"),
source.codename
)
import requests # OPT
r = requests.get(url)
m = re.search(
'<a href="/archive/\w*debian/(\w+)/dists/\w+/">next change</a>',
Expand Down
5 changes: 3 additions & 2 deletions reproman/resource/docker_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import io
import json
import os
import requests
import tarfile

from reproman import utils
from ..cmd import Runner
from ..support.exceptions import CommandError, ResourceError
Expand Down Expand Up @@ -66,10 +66,11 @@ def is_engine_running(base_url=None):
-------
boolean
"""
from requests.exceptions import ConnectionError
try:
session = docker.Client(base_url=base_url)
session.info()
except (requests.exceptions.ConnectionError,
except (ConnectionError,
docker.errors.DockerException):
return False
return True
Expand Down
10 changes: 5 additions & 5 deletions reproman/resource/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@

import attr
import getpass
import invoke
import uuid
from fabric import Connection
from ..log import LoggerHelper
from paramiko import AuthenticationException
# OPT: invoke, fabric and paramiko is imported at the point of use

import logging
lgr = logging.getLogger('reproman.resource.ssh')
Expand Down Expand Up @@ -71,6 +69,8 @@ def connect(self, password=None):
"""
# Convert key_filename to a list
# See: https://github.com/ReproNim/reproman/commit/3807f1287c39ea2393bae26803e6da8122ac5cff
from fabric import Connection
from paramiko import AuthenticationException
key_filename = None
if self.key_filename:
key_filename = [self.key_filename]
Expand All @@ -96,7 +96,6 @@ def connect(self, password=None):
self._connection.user, self._connection.host,
self._connection.port, # Fabric defaults to 22.
auth)

try:
self._connection.open()
except AuthenticationException:
Expand Down Expand Up @@ -170,6 +169,7 @@ class SSHSession(POSIXSession):
def _execute_command(self, command, env=None, cwd=None, handle_permission_denied=True):
# TODO -- command_env is not used etc...
# command_env = self.get_updated_env(env)
from invoke.exceptions import UnexpectedExit
command = command_as_string(command)
if env:
command = ' '.join(['%s=%s' % k for k in env.items()]) \
Expand All @@ -180,7 +180,7 @@ def _execute_command(self, command, env=None, cwd=None, handle_permission_denied

try:
result = self.connection.run(command, hide=True)
except invoke.exceptions.UnexpectedExit as e:
except UnexpectedExit as e:
if 'permission denied' in e.result.stderr.lower() and handle_permission_denied:
# Issue warning once
if not getattr(self, '_use_sudo_warning', False):
Expand Down
4 changes: 3 additions & 1 deletion reproman/support/jobs/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import os.path as op
import logging

import jinja2
# jinja2 is imported at the point of use for faster startup

from shlex import quote as shlex_quote

lgr = logging.getLogger("reproman.support.jobs.template")
Expand All @@ -31,6 +32,7 @@ def __init__(self, **kwds):
self.kwds = kwds

def _render(self, template_name, subdir):
import jinja2
lgr.debug("Using template %s", template_name)
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(
Expand Down
31 changes: 29 additions & 2 deletions reproman/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
'''Unit tests for Python API functionality.'''

import os
import re
from reproman.utils import getargspec
import pytest
import sys

from ..utils import getargspec
from ..cmd import Runner

import pytest
from .utils import assert_true, assert_false, eq_


Expand Down Expand Up @@ -59,3 +63,26 @@ def test_consistent_order_of_args(intf, spec_posargs):
if not spec_posargs:
pytest.skip("no positional args")
eq_(set(args[:len(spec_posargs)]), spec_posargs)


def test_no_heavy_imports():

def get_modules(extra=''):
out, err = Runner().run([
sys.executable,
'-c',
'import os, sys%s; print(os.linesep.join(sys.modules))' % extra
])
return set(out.split(os.linesep))

# Establish baseline
modules0 = get_modules() # e.g. currently ~60

# new modules brought by import of our .api
modules = get_modules(', reproman.api').difference(modules0)
assert 'requests' not in modules
assert 'boto' not in modules
assert 'jinja2' not in modules
assert 'paramiko' not in modules
# and catch it all! Raise the boundary as needed
assert len(modules) < 230 # currently 203

0 comments on commit c199c04

Please sign in to comment.