[enh] Lazy-load some module for perf improvements (#451)

* Lazy load some python imports (perfs improved a lot).

These commands became way faster:
- yunohost app setting ...
- yunohost app list
- yunohost domain list
- yunohost domain dns-conf
- yunohost dyndns installcron/removecron
- ... and maybe others

* [fix] Timeout wat not defined anymore
This commit is contained in:
pitchum 2018-04-26 16:49:38 +02:00 committed by Alexandre Aubin
parent de305ce18e
commit b9330d7501
5 changed files with 9 additions and 9 deletions

View file

@ -32,7 +32,6 @@ import re
import urlparse
import errno
import subprocess
import requests
import glob
import pwd
import grp
@ -129,6 +128,7 @@ def app_fetchlist(url=None, name=None):
else:
appslists_to_be_fetched = appslists.keys()
import requests # lazy loading this module for performance reasons
# Fetch all appslists to be fetched
for name in appslists_to_be_fetched:

View file

@ -29,14 +29,11 @@ import shutil
import pwd
import grp
import smtplib
import requests
import subprocess
import dns.resolver
import glob
from OpenSSL import crypto
from datetime import datetime
from requests.exceptions import Timeout
from yunohost.vendor.acme_tiny.acme_tiny import get_crt as sign_certificate
@ -575,9 +572,10 @@ def _fetch_and_enable_new_certificate(domain, staging=False):
raise MoulinetteError(errno.EINVAL, m18n.n(
'certmanager_cert_signing_failed'))
import requests # lazy loading this module for performance reasons
try:
intermediate_certificate = requests.get(INTERMEDIATE_CERTIFICATE_URL, timeout=30).text
except Timeout as e:
except requests.exceptions.Timeout as e:
raise MoulinetteError(errno.EINVAL, m18n.n('certmanager_couldnt_fetch_intermediate_cert'))
# Now save the key and signed certificate
@ -626,6 +624,7 @@ def _fetch_and_enable_new_certificate(domain, staging=False):
def _prepare_certificate_signing_request(domain, key_file, output_folder):
from OpenSSL import crypto # lazy loading this module for performance reasons
# Init a request
csr = crypto.X509Req()
@ -657,6 +656,7 @@ def _get_status(domain):
raise MoulinetteError(errno.EINVAL, m18n.n(
'certmanager_no_cert_file', domain=domain, file=cert_file))
from OpenSSL import crypto # lazy loading this module for performance reasons
try:
cert = crypto.load_certificate(
crypto.FILETYPE_PEM, open(cert_file).read())
@ -759,6 +759,7 @@ def _generate_account_key():
def _generate_key(destination_path):
from OpenSSL import crypto # lazy loading this module for performance reasons
k = crypto.PKey()
k.generate_key(crypto.TYPE_RSA, KEY_SIZE)
@ -842,9 +843,10 @@ def _dns_ip_match_public_ip(public_ip, domain):
def _domain_is_accessible_through_HTTP(ip, domain):
import requests # lazy loading this module for performance reasons
try:
requests.head("http://" + ip, headers={"Host": domain}, timeout=10)
except Timeout as e:
except requests.exceptions.Timeout as e:
logger.warning(m18n.n('certmanager_http_check_timeout', domain=domain, ip=ip))
return False
except Exception as e:

View file

@ -28,7 +28,6 @@ import re
import json
import yaml
import errno
import requests
from moulinette import m18n, msettings
from moulinette.core import MoulinetteError

View file

@ -30,7 +30,6 @@ import glob
import time
import base64
import errno
import requests
import subprocess
from moulinette import m18n
@ -152,6 +151,7 @@ def dyndns_subscribe(subscribe_host="dyndns.yunohost.org", domain=None, key=None
with open(key_file) as f:
key = f.readline().strip().split(' ', 6)[-1]
import requests # lazy loading this module for performance reasons
# Send subscription
try:
r = requests.post('https://%s/key/%s?key_algo=hmac-sha512' % (subscribe_host, base64.b64encode(key)), data={'subdomain': domain}, timeout=30)

View file

@ -26,7 +26,6 @@
import re
import os
import yaml
import requests
import json
import errno
import logging