From bdc6e6b85b0de13ff772d4e434b96c5212b4a758 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 03:55:00 +0200 Subject: [PATCH 01/35] [mod] remove useless imports --- src/yunohost/domain.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index b41891fb9..39eb15676 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -24,10 +24,7 @@ Manage domains """ import os -import sys import datetime -import re -import shutil import json import yaml import errno From bbc5e00c3568aa65f02b47e2184301a562b43404 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 03:55:27 +0200 Subject: [PATCH 02/35] [mod] we don't use timestamp variable --- src/yunohost/domain.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index 39eb15676..9856a46ba 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -81,9 +81,6 @@ def domain_add(auth, domain, dyndns=False): attr_dict = { 'objectClass' : ['mailDomain', 'top'] } - now = datetime.datetime.now() - timestamp = str(now.year) + str(now.month) + str(now.day) - if domain in domain_list(auth)['domains']: raise MoulinetteError(errno.EEXIST, m18n.n('domain_exists')) From aa3193d1e2393885067b05e2107ece42583af4ab Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 03:55:44 +0200 Subject: [PATCH 03/35] [mod] remove useless import --- src/yunohost/domain.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index 9856a46ba..74a836532 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -24,7 +24,6 @@ Manage domains """ import os -import datetime import json import yaml import errno From f1b0be43d7979d12089802a84b2c4e04d31062aa Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 03:56:54 +0200 Subject: [PATCH 04/35] [mod] put import at top of the file --- src/yunohost/domain.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index 74a836532..e02058ac0 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -30,6 +30,8 @@ import errno import requests from urllib import urlopen +import requests + from moulinette.core import MoulinetteError from moulinette.utils.log import getActionLogger From aff6740288f405560f8d596bd5c5344d1fab2c2e Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 03:58:34 +0200 Subject: [PATCH 05/35] [mod] put import at the top of the file --- src/yunohost/domain.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index e02058ac0..42d23fb64 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -24,18 +24,19 @@ Manage domains """ import os +import re import json import yaml import errno import requests from urllib import urlopen -import requests - from moulinette.core import MoulinetteError from moulinette.utils.log import getActionLogger from yunohost.service import service_regen_conf +from yunohost.hook import hook_callback +from yunohost.dyndns import dyndns_subscribe logger = getActionLogger('yunohost.domain') @@ -78,7 +79,6 @@ def domain_add(auth, domain, dyndns=False): dyndns -- Subscribe to DynDNS """ - from yunohost.hook import hook_callback attr_dict = { 'objectClass' : ['mailDomain', 'top'] } @@ -89,7 +89,6 @@ def domain_add(auth, domain, dyndns=False): if dyndns: if len(domain.split('.')) < 3: raise MoulinetteError(errno.EINVAL, m18n.n('domain_dyndns_invalid')) - from yunohost.dyndns import dyndns_subscribe try: r = requests.get('https://dyndns.yunohost.org/domains') @@ -177,8 +176,6 @@ def domain_remove(auth, domain, force=False): force -- Force the domain removal """ - from yunohost.hook import hook_callback - if not force and domain not in domain_list(auth)['domains']: raise MoulinetteError(errno.EINVAL, m18n.n('domain_unknown')) From 80fb5f57cfacfea5fbf814558f1067903e9ade07 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:01:21 +0200 Subject: [PATCH 06/35] [mod] pep8 --- src/yunohost/domain.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index 42d23fb64..b30dc4735 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -158,8 +158,10 @@ def domain_add(auth, domain, dyndns=False): except IOError: pass except: # Force domain removal silently - try: domain_remove(auth, domain, True) - except: pass + try: + domain_remove(auth, domain, True) + except: + pass raise hook_callback('post_domain_add', args=[domain]) From 98a629ebbdf785c41630f885a687454bf4d9f7d6 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:02:56 +0200 Subject: [PATCH 07/35] [mod] use shutil instead of os.system --- src/yunohost/domain.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index b30dc4735..2bbf589f4 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -28,9 +28,11 @@ import re import json import yaml import errno -import requests +import shutil from urllib import urlopen +import requests + from moulinette.core import MoulinetteError from moulinette.utils.log import getActionLogger @@ -194,7 +196,7 @@ def domain_remove(auth, domain, force=False): m18n.n('domain_uninstall_app_first')) if auth.remove('virtualdomain=' + domain + ',ou=domains') or force: - os.system('rm -rf /etc/yunohost/certs/%s' % domain) + shutil.rmtree('rm -rf /etc/yunohost/certs/%s' % domain) else: raise MoulinetteError(errno.EIO, m18n.n('domain_deletion_failed')) From 67041e2680b1c7b177a0e9633b5fc79ed0bcfdf4 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:03:11 +0200 Subject: [PATCH 08/35] [mod] check that os.system didn't failed --- src/yunohost/domain.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index 2bbf589f4..8eeb677d1 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -156,7 +156,7 @@ def domain_add(auth, domain, dyndns=False): with open('/etc/yunohost/installed', 'r') as f: service_regen_conf(names=[ 'nginx', 'metronome', 'dnsmasq', 'rmilter']) - os.system('yunohost app ssowatconf > /dev/null 2>&1') + assert os.system('yunohost app ssowatconf > /dev/null 2>&1') == 0, "SSOwat conf regen failed" except IOError: pass except: # Force domain removal silently @@ -202,6 +202,7 @@ def domain_remove(auth, domain, force=False): service_regen_conf(names=['nginx', 'metronome', 'dnsmasq']) os.system('yunohost app ssowatconf > /dev/null 2>&1') + assert os.system('yunohost app ssowatconf > /dev/null 2>&1') == 0, "SSOwat conf regen failed" hook_callback('post_domain_remove', args=[domain]) From 04929f4c8430ad60c3b85416bb44303b1d8b3e90 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:03:39 +0200 Subject: [PATCH 09/35] [mod] update copyright years --- src/yunohost/domain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index 8eeb677d1..817263958 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -2,7 +2,7 @@ """ License - Copyright (C) 2013 YunoHost + Copyright (C) 2013-2016 YunoHost This program is free software; you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published From 99f3ef96d3d1ae6b5904cc0c4109b4ce7d886005 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:11:46 +0200 Subject: [PATCH 10/35] [mod] use requests everywhere --- src/yunohost/domain.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index 817263958..5f163aa3f 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -29,8 +29,6 @@ import json import yaml import errno import shutil -from urllib import urlopen - import requests from moulinette.core import MoulinetteError @@ -293,7 +291,7 @@ def get_public_ip(protocol=4): else: raise ValueError("invalid protocol version") try: - return urlopen(url).read().strip() + return requests.get(url).content.strip() except IOError: logger.debug('cannot retrieve public IPv%d' % protocol, exc_info=1) raise MoulinetteError(errno.ENETUNREACH, From 22fb53abc2e8ec6bc0dc425cd917d01bd74bd2ec Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:15:09 +0200 Subject: [PATCH 11/35] [mod] remove useless imports --- src/yunohost/app.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/yunohost/app.py b/src/yunohost/app.py index 582f8e065..1fae1e503 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -24,10 +24,8 @@ Manage apps """ import os -import sys import json import shutil -import stat import yaml import time import re From f0dfb3c960aaa9ce7e89310ae94310c7bcf95516 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:18:43 +0200 Subject: [PATCH 12/35] [mod] simplify code --- src/yunohost/app.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/yunohost/app.py b/src/yunohost/app.py index 1fae1e503..e5e4c6b55 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -1402,11 +1402,11 @@ def _value_for_locale(values): if not isinstance(values, dict): return values - for lang in [m18n.locale, m18n.default_locale]: - try: - return _encode_string(values[lang]) - except KeyError: - continue + if m18n.locale in values: + return _encode_string(values[m18n.locale]) + + if m18n.default_locale in values: + return _encode_string(values[m18n.default_locale]) # Fallback to first value return _encode_string(values.values()[0]) From 49155ec8c3281aa82cc2d0f21340d444b0f03a74 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:20:07 +0200 Subject: [PATCH 13/35] [mod] imports at the beginning of the file --- src/yunohost/app.py | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/src/yunohost/app.py b/src/yunohost/app.py index e5e4c6b55..8fcfa0caa 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -29,6 +29,8 @@ import shutil import yaml import time import re +import string +import random import socket import urlparse import errno @@ -40,6 +42,10 @@ from moulinette.utils.log import getActionLogger from yunohost.service import service_log from yunohost.utils import packages +from yunohost.hook import hook_add, hook_remove, hook_exec, hook_callback +from yunohost.user import user_list, user_info +from yunohost.domain import domain_list + logger = getActionLogger('yunohost.app') @@ -331,8 +337,6 @@ def app_upgrade(auth, app=[], url=None, file=None): url -- Git url to fetch for upgrade """ - from yunohost.hook import hook_add, hook_remove, hook_exec - try: app_list() except MoulinetteError: @@ -443,8 +447,6 @@ def app_install(auth, app, label=None, args=None): args -- Serialize arguments for app installation """ - from yunohost.hook import hook_add, hook_remove, hook_exec - # Fetch or extract sources try: os.listdir(install_tmp) except OSError: os.makedirs(install_tmp) @@ -586,8 +588,6 @@ def app_remove(auth, app): app -- App(s) to delete """ - from yunohost.hook import hook_exec, hook_remove - if not _is_installed(app): raise MoulinetteError(errno.EINVAL, m18n.n('app_not_installed', app=app)) @@ -629,9 +629,6 @@ def app_addaccess(auth, apps, users=[]): apps """ - from yunohost.user import user_list, user_info - from yunohost.hook import hook_callback - result = {} if not users: @@ -684,9 +681,6 @@ def app_removeaccess(auth, apps, users=[]): apps """ - from yunohost.user import user_list - from yunohost.hook import hook_callback - result = {} remove_all = False @@ -734,8 +728,6 @@ def app_clearaccess(auth, apps): apps """ - from yunohost.hook import hook_callback - if not isinstance(apps, list): apps = [apps] for app in apps: @@ -786,8 +778,6 @@ def app_makedefault(auth, app, domain=None): domain """ - from yunohost.domain import domain_list - app_settings = _get_app_settings(app) app_domain = app_settings['domain'] app_path = app_settings['path'] @@ -879,8 +869,6 @@ def app_checkurl(auth, url, app=None): app -- Write domain & path to app settings for further checks """ - from yunohost.domain import domain_list - if "https://" == url[:8]: url = url[8:] elif "http://" == url[:7]: @@ -963,9 +951,6 @@ def app_ssowatconf(auth): """ - from yunohost.domain import domain_list - from yunohost.user import user_list - with open('/etc/yunohost/current_host', 'r') as f: main_domain = f.readline().rstrip() @@ -1464,6 +1449,7 @@ def _check_manifest_requirements(manifest): pkgname=pkgname, version=version, spec=spec)) + def _parse_args_from_manifest(manifest, action, args={}, auth=None): """Parse arguments needed for an action from the manifest @@ -1478,9 +1464,6 @@ def _parse_args_from_manifest(manifest, action, args={}, auth=None): args -- A dictionnary of arguments to parse """ - from yunohost.domain import domain_list - from yunohost.user import user_info - args_list = OrderedDict() try: action_args = manifest['arguments'][action] @@ -1575,6 +1558,7 @@ def _parse_args_from_manifest(manifest, action, args={}, auth=None): args_list[arg_name] = arg_value return args_list + def _make_environment_dict(args_dict): """ Convert a dictionnary containing manifest arguments @@ -1589,6 +1573,7 @@ def _make_environment_dict(args_dict): env_dict[ "YNH_APP_ARG_%s" % arg_name.upper() ] = arg_value return env_dict + def _parse_app_instance_name(app_instance_name): """ Parse a Yunohost app instance name and extracts the original appid @@ -1616,6 +1601,7 @@ def _parse_app_instance_name(app_instance_name): app_instance_nb = int(match.groupdict().get('appinstancenb')) if match.groupdict().get('appinstancenb') is not None else 1 return (appid, app_instance_nb) + def is_true(arg): """ Convert a string into a boolean @@ -1648,7 +1634,5 @@ def random_password(length=8): length -- The string length to generate """ - import string, random - char_set = string.ascii_uppercase + string.digits + string.ascii_lowercase return ''.join(random.sample(char_set, length)) From ea4ec42b416214c6dd543d23d23319587d6605be Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:21:47 +0200 Subject: [PATCH 14/35] [mod] constant at the beginning of the file --- src/yunohost/dyndns.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/yunohost/dyndns.py b/src/yunohost/dyndns.py index 878bc577e..6d17cc43c 100644 --- a/src/yunohost/dyndns.py +++ b/src/yunohost/dyndns.py @@ -40,6 +40,10 @@ from yunohost.domain import get_public_ip logger = getActionLogger('yunohost.dyndns') +re_dyndns_private_key = re.compile( + r'.*/K(?P[^\s\+]+)\.\+157.+\.private$' +) + class IPRouteLine(object): """ Utility class to parse an ip route output line @@ -62,10 +66,6 @@ class IPRouteLine(object): for k, v in self.m.groupdict().items(): setattr(self, k, v) -re_dyndns_private_key = re.compile( - r'.*/K(?P[^\s\+]+)\.\+157.+\.private$' -) - def dyndns_subscribe(subscribe_host="dyndns.yunohost.org", domain=None, key=None): """ From f942623e164866346c7dd46f2a3a1e6bab7c2e54 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:21:56 +0200 Subject: [PATCH 15/35] [mod] remove useless imports --- src/yunohost/backup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/yunohost/backup.py b/src/yunohost/backup.py index 649f3116e..f037941f6 100644 --- a/src/yunohost/backup.py +++ b/src/yunohost/backup.py @@ -25,7 +25,6 @@ """ import os import re -import sys import json import errno import time From 69f2705b13be5d904b10567c68a795dd3ac6429b Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:22:05 +0200 Subject: [PATCH 16/35] [mod] constants in capslock --- src/yunohost/dyndns.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/yunohost/dyndns.py b/src/yunohost/dyndns.py index 6d17cc43c..55f7fd45b 100644 --- a/src/yunohost/dyndns.py +++ b/src/yunohost/dyndns.py @@ -40,7 +40,7 @@ from yunohost.domain import get_public_ip logger = getActionLogger('yunohost.dyndns') -re_dyndns_private_key = re.compile( +RE_DYNDNS_PRIVATE_KEY = re.compile( r'.*/K(?P[^\s\+]+)\.\+157.+\.private$' ) @@ -171,7 +171,7 @@ def dyndns_update(dyn_host="dyndns.yunohost.org", domain=None, key=None, if domain is None: # Retrieve the first registered domain for path in glob.iglob('/etc/yunohost/dyndns/K*.private'): - match = re_dyndns_private_key.match(path) + match = RE_DYNDNS_PRIVATE_KEY.match(path) if not match: continue _domain = match.group('domain') From fc5e37edc8c04e2a79066ce86f619dcc34b402b3 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:25:27 +0200 Subject: [PATCH 17/35] [mod] imports at the beginning of the file --- src/yunohost/firewall.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/yunohost/firewall.py b/src/yunohost/firewall.py index ef6249434..bfe18b553 100644 --- a/src/yunohost/firewall.py +++ b/src/yunohost/firewall.py @@ -36,7 +36,9 @@ except ImportError: from moulinette.core import MoulinetteError from moulinette.utils import process from moulinette.utils.log import getActionLogger -from moulinette.utils.text import prependlines +from moulinette.utils.text import prependlines, searchf + +from yunohost.hook import hook_callback firewall_file = '/etc/yunohost/firewall.yml' upnp_cron_job = '/etc/cron.d/yunohost-firewall-upnp' @@ -194,8 +196,6 @@ def firewall_reload(skip_upnp=False): skip_upnp -- Do not refresh port forwarding using UPnP """ - from yunohost.hook import hook_callback - reloaded = False errors = False @@ -434,7 +434,6 @@ def _get_ssh_port(default=22): Retrieve the SSH port from the sshd_config file or used the default one if it's not defined. """ - from moulinette.utils.text import searchf try: m = searchf(r'^Port[ \t]+([0-9]+)$', '/etc/ssh/sshd_config', count=-1) @@ -444,12 +443,14 @@ def _get_ssh_port(default=22): pass return default + def _update_firewall_file(rules): """Make a backup and write new rules to firewall file""" os.system("cp {0} {0}.old".format(firewall_file)) with open(firewall_file, 'w') as f: yaml.safe_dump(rules, f, default_flow_style=False) + def _on_rule_command_error(returncode, cmd, output): """Callback for rules commands error""" # Log error and continue commands execution From 088fc01b0636e1a5563313d77c6804714171611e Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:25:53 +0200 Subject: [PATCH 18/35] [mod] autopep8 --- src/yunohost/firewall.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/yunohost/firewall.py b/src/yunohost/firewall.py index bfe18b553..f304deb00 100644 --- a/src/yunohost/firewall.py +++ b/src/yunohost/firewall.py @@ -125,7 +125,7 @@ def firewall_disallow(protocol, port, ipv4_only=False, ipv6_only=False, ipvs = ['ipv4', 'ipv6'] upnp = True if ipv4_only and ipv6_only: - upnp = True # automatically disallow UPnP + upnp = True # automatically disallow UPnP elif ipv4_only: ipvs = ['ipv4',] upnp = upnp_only @@ -180,7 +180,7 @@ def firewall_list(raw=False, by_ip_version=False, list_forwarded=False): ports = sorted(set(ports['ipv4']) | set(ports['ipv6'])) # Format returned dict - ret = { "opened_ports": ports } + ret = {"opened_ports": ports} if list_forwarded: # Combine TCP and UDP forwarded ports ret['forwarded_ports'] = sorted( @@ -224,8 +224,8 @@ def firewall_reload(skip_upnp=False): # Iterate over ports and add rule for protocol in ['TCP', 'UDP']: for port in firewall['ipv4'][protocol]: - rules.append("iptables -A INPUT -p %s --dport %s -j ACCEPT" \ - % (protocol, process.quote(str(port)))) + rules.append("iptables -A INPUT -p %s --dport %s -j ACCEPT" + % (protocol, process.quote(str(port)))) rules += [ "iptables -A INPUT -i lo -j ACCEPT", "iptables -A INPUT -p icmp -j ACCEPT", @@ -253,8 +253,8 @@ def firewall_reload(skip_upnp=False): # Iterate over ports and add rule for protocol in ['TCP', 'UDP']: for port in firewall['ipv6'][protocol]: - rules.append("ip6tables -A INPUT -p %s --dport %s -j ACCEPT" \ - % (protocol, process.quote(str(port)))) + rules.append("ip6tables -A INPUT -p %s --dport %s -j ACCEPT" + % (protocol, process.quote(str(port)))) rules += [ "ip6tables -A INPUT -i lo -j ACCEPT", "ip6tables -A INPUT -p icmpv6 -j ACCEPT", @@ -308,13 +308,14 @@ def firewall_upnp(action='status', no_refresh=False): try: # Remove old cron job os.remove('/etc/cron.d/yunohost-firewall') - except: pass + except: + pass action = 'status' no_refresh = False if action == 'status' and no_refresh: # Only return current state - return { 'enabled': enabled } + return {'enabled': enabled} elif action == 'enable' or (enabled and action == 'status'): # Add cron job with open(upnp_cron_job, 'w+') as f: @@ -330,7 +331,8 @@ def firewall_upnp(action='status', no_refresh=False): try: # Remove cron job os.remove(upnp_cron_job) - except: pass + except: + pass enabled = False if action == 'status': no_refresh = True @@ -364,7 +366,8 @@ def firewall_upnp(action='status', no_refresh=False): if upnpc.getspecificportmapping(port, protocol): try: upnpc.deleteportmapping(port, protocol) - except: pass + except: + pass if not enabled: continue try: @@ -403,7 +406,7 @@ def firewall_upnp(action='status', no_refresh=False): if action == 'enable' and not enabled: raise MoulinetteError(errno.ENXIO, m18n.n('upnp_port_open_failed')) - return { 'enabled': enabled } + return {'enabled': enabled} def firewall_stop(): From c65f3f82081252ae2c77d35bb0d43a421fe0f5b0 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:26:38 +0200 Subject: [PATCH 19/35] [mod] remove useless imports --- src/yunohost/hook.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/yunohost/hook.py b/src/yunohost/hook.py index aafe15459..b263f3ef6 100644 --- a/src/yunohost/hook.py +++ b/src/yunohost/hook.py @@ -24,11 +24,7 @@ Manage hooks """ import os -import sys -import re -import json import errno -import subprocess from glob import iglob from moulinette.core import MoulinetteError @@ -309,7 +305,6 @@ def hook_exec(path, args=None, raise_on_error=False, no_trace=False, """ from moulinette.utils.process import call_async_output - from yunohost.app import _value_for_locale # Validate hook path if path[0] != '/': From 5fc5d3b6ca9935b788f922fd88a091806f253572 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:26:57 +0200 Subject: [PATCH 20/35] [mod] imports at the beginning of the file --- src/yunohost/hook.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/yunohost/hook.py b/src/yunohost/hook.py index b263f3ef6..60d8324ee 100644 --- a/src/yunohost/hook.py +++ b/src/yunohost/hook.py @@ -29,6 +29,7 @@ from glob import iglob from moulinette.core import MoulinetteError from moulinette.utils import log +from moulinette.utils.process import call_async_output hook_folder = '/usr/share/yunohost/hooks/' custom_hook_folder = '/etc/yunohost/hooks.d/' @@ -304,8 +305,6 @@ def hook_exec(path, args=None, raise_on_error=False, no_trace=False, env -- Dictionnary of environment variables to export """ - from moulinette.utils.process import call_async_output - # Validate hook path if path[0] != '/': path = os.path.realpath(path) From b754bdef5324f38ecda22b16effe0ff2e4c66169 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:27:26 +0200 Subject: [PATCH 21/35] [mod] autopep8 --- src/yunohost/hook.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/yunohost/hook.py b/src/yunohost/hook.py index 60d8324ee..0bf015320 100644 --- a/src/yunohost/hook.py +++ b/src/yunohost/hook.py @@ -49,14 +49,16 @@ def hook_add(app, file): path, filename = os.path.split(file) priority, action = _extract_filename_parts(filename) - try: os.listdir(custom_hook_folder + action) - except OSError: os.makedirs(custom_hook_folder + action) + try: + os.listdir(custom_hook_folder + action) + except OSError: + os.makedirs(custom_hook_folder + action) - finalpath = custom_hook_folder + action +'/'+ priority +'-'+ app + finalpath = custom_hook_folder + action + '/' + priority + '-' + app os.system('cp %s %s' % (file, finalpath)) os.system('chown -hR admin: %s' % hook_folder) - return { 'hook': finalpath } + return {'hook': finalpath} def hook_remove(app): @@ -71,8 +73,9 @@ def hook_remove(app): for action in os.listdir(custom_hook_folder): for script in os.listdir(custom_hook_folder + action): if script.endswith(app): - os.remove(custom_hook_folder + action +'/'+ script) - except OSError: pass + os.remove(custom_hook_folder + action + '/' + script) + except OSError: + pass def hook_info(action, name): @@ -133,11 +136,11 @@ def hook_list(action, list_by='name', show_info=False): def _append_hook(d, priority, name, path): # Use the priority as key and a dict of hooks names # with their info as value - value = { 'path': path } + value = {'path': path} try: d[priority][name] = value except KeyError: - d[priority] = { name: value } + d[priority] = {name: value} else: def _append_hook(d, priority, name, path): # Use the priority as key and the name as value @@ -159,11 +162,12 @@ def hook_list(action, list_by='name', show_info=False): if h['path'] != path: h['path'] = path return - l.append({ 'priority': priority, 'path': path }) + l.append({'priority': priority, 'path': path}) d[name] = l else: if list_by == 'name': result = set() + def _append_hook(d, priority, name, path): # Add only the name d.add(name) @@ -201,7 +205,7 @@ def hook_list(action, list_by='name', show_info=False): logger.debug("custom hook folder not found for action '%s' in %s", action, custom_hook_folder) - return { 'hooks': result } + return {'hooks': result} def hook_callback(action, hooks=[], args=None, no_trace=False, chdir=None, @@ -223,7 +227,7 @@ def hook_callback(action, hooks=[], args=None, no_trace=False, chdir=None, (name, priority, path, succeed) as arguments """ - result = { 'succeed': {}, 'failed': {} } + result = {'succeed': {}, 'failed': {}} hooks_dict = {} # Retrieve hooks @@ -255,7 +259,7 @@ def hook_callback(action, hooks=[], args=None, no_trace=False, chdir=None, for h in hl: # Update hooks dict d = hooks_dict.get(h['priority'], dict()) - d.update({ n: { 'path': h['path'] }}) + d.update({n: {'path': h['path']}}) hooks_dict[h['priority']] = d if not hooks_dict: return result From 43217c6fb2b0188294332fe68dda6a7413e96611 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:28:12 +0200 Subject: [PATCH 22/35] [mod] remove useless imports --- src/yunohost/monitor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/yunohost/monitor.py b/src/yunohost/monitor.py index 5142c8305..7fd5ff8e9 100644 --- a/src/yunohost/monitor.py +++ b/src/yunohost/monitor.py @@ -35,7 +35,7 @@ import errno import os import dns.resolver import cPickle as pickle -from datetime import datetime, timedelta +from datetime import datetime from moulinette.core import MoulinetteError from moulinette.utils.log import getActionLogger From 17ca60c3a533072a4eed56da50612c24d0915287 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:29:08 +0200 Subject: [PATCH 23/35] [mod] imports at the beginning of the file --- src/yunohost/monitor.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/yunohost/monitor.py b/src/yunohost/monitor.py index 7fd5ff8e9..af67d1409 100644 --- a/src/yunohost/monitor.py +++ b/src/yunohost/monitor.py @@ -41,6 +41,9 @@ from moulinette.core import MoulinetteError from moulinette.utils.log import getActionLogger from yunohost.domain import get_public_ip +from yunohost.service import (service_status, service_enable, + service_start, service_disable, service_stop) + logger = getActionLogger('yunohost.monitor') @@ -406,9 +409,6 @@ def monitor_enable(no_stats=False): no_stats -- Disable monitoring statistics """ - from yunohost.service import (service_status, service_enable, - service_start) - glances = service_status('glances') if glances['status'] != 'running': service_start('glances') @@ -433,9 +433,6 @@ def monitor_disable(): Disable server monitoring """ - from yunohost.service import (service_status, service_disable, - service_stop) - glances = service_status('glances') if glances['status'] != 'inactive': service_stop('glances') @@ -467,8 +464,6 @@ def _get_glances_api(): else: return p - from yunohost.service import service_status - if service_status('glances')['status'] != 'running': raise MoulinetteError(errno.EPERM, m18n.n('monitor_not_enabled')) raise MoulinetteError(errno.EIO, m18n.n('monitor_glances_con_failed')) From 93cb77d0a50252e53371bdaf63d03916f8bad5d8 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:30:56 +0200 Subject: [PATCH 24/35] [mod] try to improve lisibility a bit --- src/yunohost/monitor.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/yunohost/monitor.py b/src/yunohost/monitor.py index af67d1409..1ff69b9b2 100644 --- a/src/yunohost/monitor.py +++ b/src/yunohost/monitor.py @@ -717,22 +717,26 @@ def _append_to_stats(stats, monitor, statics=[]): statics = [statics] # Appending function - def _append(s, m, st): - for k, v in m.items(): - if k in st: - s[k] = v - elif isinstance(v, dict): - if k not in s: - s[k] = {} - s[k] = _append(s[k], v, st) + def _append(_stats, _monitor, _statics): + for key, value in _monitor.items(): + if key in _statics: + _stats[key] = value + + elif isinstance(value, dict): + if key not in _stats: + _stats[key] = {} + _stats[key] = _append(_stats[key], value, _statics) + else: - if k not in s: - s[k] = [] - if isinstance(v, list): - s[k].extend(v) + if key not in _stats: + _stats[key] = [] + + if isinstance(value, list): + _stats[key].extend(value) else: - s[k].append(v) - return s + _stats[key].append(value) + + return _stats stats = _append(stats, monitor, statics) return stats From a75e07a677eb783775995f1ad7ab34f76df6eb07 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:31:10 +0200 Subject: [PATCH 25/35] [mod] remove useless imports --- src/yunohost/service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/yunohost/service.py b/src/yunohost/service.py index ab26dd2bc..b1b776326 100644 --- a/src/yunohost/service.py +++ b/src/yunohost/service.py @@ -36,7 +36,7 @@ from difflib import unified_diff from moulinette.core import MoulinetteError from moulinette.utils import log, filesystem -from yunohost.hook import hook_list, hook_callback +from yunohost.hook import hook_callback base_conf_path = '/home/yunohost.conf' From 1bcf14d1d0a153da0bbb30b93c24188cff88123a Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:31:55 +0200 Subject: [PATCH 26/35] [mod] remove useless imports --- src/yunohost/tools.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/yunohost/tools.py b/src/yunohost/tools.py index f78e32363..6f4ff6262 100644 --- a/src/yunohost/tools.py +++ b/src/yunohost/tools.py @@ -24,10 +24,7 @@ Specific tools """ import os -import sys import yaml -import re -import getpass import requests import json import errno @@ -42,9 +39,9 @@ from moulinette.utils.log import getActionLogger from yunohost.app import app_fetchlist, app_info, app_upgrade, app_ssowatconf, app_list from yunohost.domain import domain_add, domain_list, get_public_ip from yunohost.dyndns import dyndns_subscribe -from yunohost.firewall import firewall_upnp, firewall_reload +from yunohost.firewall import firewall_upnp from yunohost.service import service_status, service_regen_conf, service_log -from yunohost.monitor import monitor_disk, monitor_network, monitor_system +from yunohost.monitor import monitor_disk, monitor_system from yunohost.utils.packages import ynh_packages_version apps_setting_path= '/etc/yunohost/apps/' From fde312ad412e745ba97d92ab5bebd0d4ee0af92c Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:32:42 +0200 Subject: [PATCH 27/35] [mod] remove useless condition --- src/yunohost/tools.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/yunohost/tools.py b/src/yunohost/tools.py index 6f4ff6262..7377c6c9d 100644 --- a/src/yunohost/tools.py +++ b/src/yunohost/tools.py @@ -315,29 +315,29 @@ def tools_update(ignore_apps=False, ignore_packages=False): except MoulinetteError: pass app_list = os.listdir(apps_setting_path) - if len(app_list) > 0: - for app_id in app_list: - if '__' in app_id: - original_app_id = app_id[:app_id.index('__')] - else: - original_app_id = app_id - current_app_dict = app_info(app_id, raw=True) - new_app_dict = app_info(original_app_id, raw=True) + for app_id in app_list: + if '__' in app_id: + original_app_id = app_id[:app_id.index('__')] + else: + original_app_id = app_id - # Custom app - if new_app_dict is None or 'lastUpdate' not in new_app_dict or 'git' not in new_app_dict: - continue + current_app_dict = app_info(app_id, raw=True) + new_app_dict = app_info(original_app_id, raw=True) - if (new_app_dict['lastUpdate'] > current_app_dict['lastUpdate']) \ - or ('update_time' not in current_app_dict['settings'] \ - and (new_app_dict['lastUpdate'] > current_app_dict['settings']['install_time'])) \ - or ('update_time' in current_app_dict['settings'] \ - and (new_app_dict['lastUpdate'] > current_app_dict['settings']['update_time'])): - apps.append({ - 'id': app_id, - 'label': current_app_dict['settings']['label'] - }) + # Custom app + if new_app_dict is None or 'lastUpdate' not in new_app_dict or 'git' not in new_app_dict: + continue + + if (new_app_dict['lastUpdate'] > current_app_dict['lastUpdate']) \ + or ('update_time' not in current_app_dict['settings'] \ + and (new_app_dict['lastUpdate'] > current_app_dict['settings']['install_time'])) \ + or ('update_time' in current_app_dict['settings'] \ + and (new_app_dict['lastUpdate'] > current_app_dict['settings']['update_time'])): + apps.append({ + 'id': app_id, + 'label': current_app_dict['settings']['label'] + }) if len(apps) == 0 and len(packages) == 0: logger.info(m18n.n('packages_no_upgrade')) From bb87c41bc0e13ec717d6cca53107a63733311e91 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:33:07 +0200 Subject: [PATCH 28/35] [mod] avoid to overwrite an import name --- src/yunohost/tools.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/yunohost/tools.py b/src/yunohost/tools.py index 7377c6c9d..79c42ff81 100644 --- a/src/yunohost/tools.py +++ b/src/yunohost/tools.py @@ -314,9 +314,8 @@ def tools_update(ignore_apps=False, ignore_packages=False): app_fetchlist() except MoulinetteError: pass - app_list = os.listdir(apps_setting_path) - for app_id in app_list: + for app_id in os.listdir(apps_setting_path): if '__' in app_id: original_app_id = app_id[:app_id.index('__')] else: From 8694cde7abef6883d5c7e18715a452b5dd26ed8c Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:34:31 +0200 Subject: [PATCH 29/35] [mod] autopep8 --- src/yunohost/tools.py | 51 ++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/src/yunohost/tools.py b/src/yunohost/tools.py index 79c42ff81..a2fb8e9c0 100644 --- a/src/yunohost/tools.py +++ b/src/yunohost/tools.py @@ -44,7 +44,7 @@ from yunohost.service import service_status, service_regen_conf, service_log from yunohost.monitor import monitor_disk, monitor_system from yunohost.utils.packages import ynh_packages_version -apps_setting_path= '/etc/yunohost/apps/' +apps_setting_path = '/etc/yunohost/apps/' logger = getActionLogger('yunohost.tools') @@ -59,12 +59,16 @@ def tools_ldapinit(auth): ldap_map = yaml.load(f) for rdn, attr_dict in ldap_map['parents'].items(): - try: auth.add(rdn, attr_dict) - except: pass + try: + auth.add(rdn, attr_dict) + except: + pass for rdn, attr_dict in ldap_map['children'].items(): - try: auth.add(rdn, attr_dict) - except: pass + try: + auth.add(rdn, attr_dict) + except: + pass admin_dict = { 'cn': 'admin', @@ -115,7 +119,7 @@ def tools_maindomain(auth, old_domain=None, new_domain=None, dyndns=False): old_domain = f.readline().rstrip() if not new_domain: - return { 'current_main_domain': old_domain } + return {'current_main_domain': old_domain} if not new_domain: raise MoulinetteError(errno.EINVAL, m18n.n('new_domain_required')) @@ -127,7 +131,7 @@ def tools_maindomain(auth, old_domain=None, new_domain=None, dyndns=False): command_list = [ 'ln -s /etc/yunohost/certs/%s/key.pem /etc/ssl/private/yunohost_key.pem' % new_domain, - 'ln -s /etc/yunohost/certs/%s/crt.pem /etc/ssl/certs/yunohost_crt.pem' % new_domain, + 'ln -s /etc/yunohost/certs/%s/crt.pem /etc/ssl/certs/yunohost_crt.pem' % new_domain, 'echo %s > /etc/yunohost/current_host' % new_domain, ] @@ -143,14 +147,15 @@ def tools_maindomain(auth, old_domain=None, new_domain=None, dyndns=False): pass else: dyndomains = json.loads(r.text) - dyndomain = '.'.join(new_domain.split('.')[1:]) + dyndomain = '.'.join(new_domain.split('.')[1:]) if dyndomain in dyndomains: dyndns_subscribe(domain=new_domain) try: with open('/etc/yunohost/installed', 'r') as f: service_regen_conf() - except IOError: pass + except IOError: + pass logger.success(m18n.n('maindomain_changed')) @@ -178,7 +183,7 @@ def tools_postinstall(domain, password, ignore_dyndns=False): pass else: dyndomains = json.loads(r.text) - dyndomain = '.'.join(domain.split('.')[1:]) + dyndomain = '.'.join(domain.split('.')[1:]) if dyndomain in dyndomains: if requests.get('https://dyndns.yunohost.org/test/%s' % domain).status_code == 200: dyndns = True @@ -192,7 +197,7 @@ def tools_postinstall(domain, password, ignore_dyndns=False): auth = init_authenticator(('ldap', 'default'), {'uri': "ldap://localhost:389", 'base_dn': "dc=yunohost,dc=org", - 'user_rdn': "cn=admin" }) + 'user_rdn': "cn=admin"}) auth.authenticate('yunohost') # Initialize LDAP for YunoHost @@ -209,8 +214,10 @@ def tools_postinstall(domain, password, ignore_dyndns=False): ] for folder in folders_to_create: - try: os.listdir(folder) - except OSError: os.makedirs(folder) + try: + os.listdir(folder) + except OSError: + os.makedirs(folder) # Change folders permissions os.system('chmod 755 /home/yunohost.app') @@ -229,7 +236,7 @@ def tools_postinstall(domain, password, ignore_dyndns=False): if 'redirected_urls' not in ssowat_conf: ssowat_conf['redirected_urls'] = {} - ssowat_conf['redirected_urls']['/'] = domain +'/yunohost/admin' + ssowat_conf['redirected_urls']['/'] = domain + '/yunohost/admin' with open('/etc/ssowat/conf.json.persistent', 'w+') as f: json.dump(ssowat_conf, f, sort_keys=True, indent=4) @@ -241,8 +248,8 @@ def tools_postinstall(domain, password, ignore_dyndns=False): ssl_dir = '/usr/share/yunohost/yunohost-config/ssl/yunoCA' command_list = [ 'echo "01" > %s/serial' % ssl_dir, - 'rm %s/index.txt' % ssl_dir, - 'touch %s/index.txt' % ssl_dir, + 'rm %s/index.txt' % ssl_dir, + 'touch %s/index.txt' % ssl_dir, 'cp %s/openssl.cnf %s/openssl.ca.cnf' % (ssl_dir, ssl_dir), 'sed -i "s/yunohost.org/%s/g" %s/openssl.ca.cnf ' % (domain, ssl_dir), 'openssl req -x509 -new -config %s/openssl.ca.cnf -days 3650 -out %s/ca/cacert.pem -keyout %s/ca/cakey.pem -nodes -batch' % (ssl_dir, ssl_dir, ssl_dir), @@ -341,7 +348,7 @@ def tools_update(ignore_apps=False, ignore_packages=False): if len(apps) == 0 and len(packages) == 0: logger.info(m18n.n('packages_no_upgrade')) - return { 'packages': packages, 'apps': apps } + return {'packages': packages, 'apps': apps} def tools_upgrade(auth, ignore_apps=False, ignore_packages=False): @@ -377,7 +384,7 @@ def tools_upgrade(auth, ignore_apps=False, ignore_packages=False): # ... and set a hourly cron up to upgrade critical packages if critical_upgrades: logger.info(m18n.n('packages_upgrade_critical_later', - packages=', '.join(critical_upgrades))) + packages=', '.join(critical_upgrades))) with open('/etc/cron.d/yunohost-upgrade', 'w+') as f: f.write('00 * * * * root PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin apt-get install %s -y && rm -f /etc/cron.d/yunohost-upgrade\n' % ' '.join(critical_upgrades)) @@ -410,7 +417,7 @@ def tools_upgrade(auth, ignore_apps=False, ignore_packages=False): # Return API logs if it is an API call if is_api: - return { "log": service_log('yunohost-api', number="100").values()[0] } + return {"log": service_log('yunohost-api', number="100").values()[0]} def tools_diagnosis(auth, private=False): @@ -418,7 +425,7 @@ def tools_diagnosis(auth, private=False): Return global info about current yunohost instance to help debugging """ - diagnosis = OrderedDict(); + diagnosis = OrderedDict() # Debian release try: @@ -462,8 +469,8 @@ def tools_diagnosis(auth, private=False): logger.warning(m18n.n('diagnosis_monitor_system_error', error=format(e)), exc_info=1) else: diagnosis['system']['memory'] = { - 'ram' : '%s (%s free)' % (system['memory']['ram']['total'], system['memory']['ram']['free']), - 'swap' : '%s (%s free)' % (system['memory']['swap']['total'], system['memory']['swap']['free']), + 'ram': '%s (%s free)' % (system['memory']['ram']['total'], system['memory']['ram']['free']), + 'swap': '%s (%s free)' % (system['memory']['swap']['total'], system['memory']['swap']['free']), } # Services status From 8c1c8fa981588a6cc5846cae3317d9340155317b Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:34:54 +0200 Subject: [PATCH 30/35] [mod] remove useless imports --- src/yunohost/user.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/yunohost/user.py b/src/yunohost/user.py index ec7dd539c..f5ac25ea8 100644 --- a/src/yunohost/user.py +++ b/src/yunohost/user.py @@ -30,7 +30,6 @@ import string import json import errno import subprocess -import math import re from moulinette.core import MoulinetteError From 03d83b7166703358d5e0bf91b9e924bf04dc10d2 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:35:47 +0200 Subject: [PATCH 31/35] [mod] imports at the beginning of the file --- src/yunohost/user.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/yunohost/user.py b/src/yunohost/user.py index f5ac25ea8..cdb7bbf17 100644 --- a/src/yunohost/user.py +++ b/src/yunohost/user.py @@ -31,10 +31,16 @@ import json import errno import subprocess import re +import pwd from moulinette.core import MoulinetteError from moulinette.utils.log import getActionLogger +from yunohost.domain import domain_list +from yunohost.hook import hook_callback +from yunohost.app import app_ssowatconf + + logger = getActionLogger('yunohost.user') @@ -104,11 +110,6 @@ def user_create(auth, username, firstname, lastname, mail, password, mailbox_quota -- Mailbox size quota """ - import pwd - from yunohost.domain import domain_list - from yunohost.hook import hook_callback - from yunohost.app import app_ssowatconf - # Validate uniqueness of username and mail in LDAP auth.validate_uniqueness({ 'uid' : username, @@ -222,9 +223,6 @@ def user_delete(auth, username, purge=False): purge """ - from yunohost.app import app_ssowatconf - from yunohost.hook import hook_callback - if auth.remove('uid=%s,ou=users' % username): # Invalidate passwd to take user deletion into account subprocess.call(['nscd', '-i', 'passwd']) @@ -264,9 +262,6 @@ def user_update(auth, username, firstname=None, lastname=None, mail=None, remove_mailalias -- Mail aliases to remove """ - from yunohost.domain import domain_list - from yunohost.app import app_ssowatconf - attrs_to_fetch = ['givenName', 'sn', 'mail', 'maildrop'] new_attr_dict = {} domains = domain_list(auth)['domains'] From f52b805bf43fc95bd740ba70126597cca2ee4d9b Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 04:36:49 +0200 Subject: [PATCH 32/35] [mod] autopep8 --- src/yunohost/tools.py | 4 +- src/yunohost/user.py | 125 ++++++++++++++++++++++-------------------- 2 files changed, 67 insertions(+), 62 deletions(-) diff --git a/src/yunohost/tools.py b/src/yunohost/tools.py index a2fb8e9c0..e56196760 100644 --- a/src/yunohost/tools.py +++ b/src/yunohost/tools.py @@ -328,8 +328,8 @@ def tools_update(ignore_apps=False, ignore_packages=False): else: original_app_id = app_id - current_app_dict = app_info(app_id, raw=True) - new_app_dict = app_info(original_app_id, raw=True) + current_app_dict = app_info(app_id, raw=True) + new_app_dict = app_info(original_app_id, raw=True) # Custom app if new_app_dict is None or 'lastUpdate' not in new_app_dict or 'git' not in new_app_dict: diff --git a/src/yunohost/user.py b/src/yunohost/user.py index cdb7bbf17..e6fe112e6 100644 --- a/src/yunohost/user.py +++ b/src/yunohost/user.py @@ -55,12 +55,12 @@ def user_list(auth, fields=None, filter=None, limit=None, offset=None): fields -- fields to fetch """ - user_attrs = { 'uid': 'username', - 'cn': 'fullname', - 'mail': 'mail', - 'maildrop': 'mail-forward', - 'mailuserquota': 'mailbox-quota' } - attrs = [ 'uid' ] + user_attrs = {'uid': 'username', + 'cn': 'fullname', + 'mail': 'mail', + 'maildrop': 'mail-forward', + 'mailuserquota': 'mailbox-quota'} + attrs = ['uid'] users = {} # Set default arguments values @@ -79,12 +79,12 @@ def user_list(auth, fields=None, filter=None, limit=None, offset=None): raise MoulinetteError(errno.EINVAL, m18n.n('field_invalid', attr)) else: - attrs = [ 'uid', 'cn', 'mail', 'mailuserquota' ] + attrs = ['uid', 'cn', 'mail', 'mailuserquota'] result = auth.search('ou=users,dc=yunohost,dc=org', filter, attrs) if len(result) > offset and limit > 0: - for user in result[offset:offset+limit]: + for user in result[offset:offset + limit]: entry = {} for attr, values in user.items(): try: @@ -93,7 +93,7 @@ def user_list(auth, fields=None, filter=None, limit=None, offset=None): pass uid = entry[user_attrs['uid']] users[uid] = entry - return { 'users' : users } + return {'users': users} def user_create(auth, username, firstname, lastname, mail, password, @@ -112,8 +112,8 @@ def user_create(auth, username, firstname, lastname, mail, password, """ # Validate uniqueness of username and mail in LDAP auth.validate_uniqueness({ - 'uid' : username, - 'mail' : mail + 'uid': username, + 'mail': mail }) # Validate uniqueness of username in system users @@ -125,10 +125,10 @@ def user_create(auth, username, firstname, lastname, mail, password, raise MoulinetteError(errno.EEXIST, m18n.n('system_username_exists')) # Check that the mail domain exists - if mail[mail.find('@')+1:] not in domain_list(auth)['domains']: + if mail[mail.find('@') + 1:] not in domain_list(auth)['domains']: raise MoulinetteError(errno.EINVAL, m18n.n('mail_domain_unknown', - domain=mail[mail.find('@')+1:])) + domain=mail[mail.find('@') + 1:])) # Get random UID/GID uid_check = gid_check = 0 @@ -141,7 +141,7 @@ def user_create(auth, username, firstname, lastname, mail, password, fullname = '%s %s' % (firstname, lastname) rdn = 'uid=%s,ou=users' % username char_set = string.ascii_uppercase + string.digits - salt = ''.join(random.sample(char_set,8)) + salt = ''.join(random.sample(char_set, 8)) salt = '$1$' + salt + '$' user_pwd = '{CRYPT}' + crypt.crypt(str(password), salt) attr_dict = { @@ -166,12 +166,12 @@ def user_create(auth, username, firstname, lastname, mail, password, with open('/etc/yunohost/current_host') as f: main_domain = f.readline().rstrip() aliases = [ - 'root@'+ main_domain, - 'admin@'+ main_domain, - 'webmaster@'+ main_domain, - 'postmaster@'+ main_domain, + 'root@' + main_domain, + 'admin@' + main_domain, + 'webmaster@' + main_domain, + 'postmaster@' + main_domain, ] - attr_dict['mail'] = [ attr_dict['mail'] ] + aliases + attr_dict['mail'] = [attr_dict['mail']] + aliases # If exists, remove the redirection from the SSO try: @@ -184,8 +184,8 @@ def user_create(auth, username, firstname, lastname, mail, password, with open('/etc/ssowat/conf.json.persistent', 'w+') as f: json.dump(ssowat_conf, f, sort_keys=True, indent=4) - except IOError: pass - + except IOError: + pass if auth.add(rdn, attr_dict): # Invalidate passwd to take user creation into account @@ -194,7 +194,7 @@ def user_create(auth, username, firstname, lastname, mail, password, # Update SFTP user group memberlist = auth.search(filter='cn=sftpusers', attrs=['memberUid'])[0]['memberUid'] memberlist.append(username) - if auth.update('cn=sftpusers,ou=groups', { 'memberUid': memberlist }): + if auth.update('cn=sftpusers,ou=groups', {'memberUid': memberlist}): try: # Attempt to create user home folder subprocess.check_call( @@ -204,12 +204,12 @@ def user_create(auth, username, firstname, lastname, mail, password, logger.warning(m18n.n('user_home_creation_failed'), exc_info=1) app_ssowatconf(auth) - #TODO: Send a welcome mail to user + # TODO: Send a welcome mail to user logger.success(m18n.n('user_created')) hook_callback('post_user_create', args=[username, mail, password, firstname, lastname]) - return { 'fullname' : fullname, 'username' : username, 'mail' : mail } + return {'fullname': fullname, 'username': username, 'mail': mail} raise MoulinetteError(169, m18n.n('user_creation_failed')) @@ -229,9 +229,11 @@ def user_delete(auth, username, purge=False): # Update SFTP user group memberlist = auth.search(filter='cn=sftpusers', attrs=['memberUid'])[0]['memberUid'] - try: memberlist.remove(username) - except: pass - if auth.update('cn=sftpusers,ou=groups', { 'memberUid': memberlist }): + try: + memberlist.remove(username) + except: + pass + if auth.update('cn=sftpusers,ou=groups', {'memberUid': memberlist}): if purge: subprocess.call(['rm', '-rf', '/home/{0}'.format(username)]) else: @@ -274,11 +276,11 @@ def user_update(auth, username, firstname=None, lastname=None, mail=None, # Get modifications from arguments if firstname: - new_attr_dict['givenName'] = firstname # TODO: Validate + new_attr_dict['givenName'] = firstname # TODO: Validate new_attr_dict['cn'] = new_attr_dict['displayName'] = firstname + ' ' + user['sn'][0] if lastname: - new_attr_dict['sn'] = lastname # TODO: Validate + new_attr_dict['sn'] = lastname # TODO: Validate new_attr_dict['cn'] = new_attr_dict['displayName'] = user['givenName'][0] + ' ' + lastname if lastname and firstname: @@ -286,34 +288,34 @@ def user_update(auth, username, firstname=None, lastname=None, mail=None, if change_password: char_set = string.ascii_uppercase + string.digits - salt = ''.join(random.sample(char_set,8)) + salt = ''.join(random.sample(char_set, 8)) salt = '$1$' + salt + '$' new_attr_dict['userPassword'] = '{CRYPT}' + crypt.crypt(str(change_password), salt) if mail: - auth.validate_uniqueness({ 'mail': mail }) - if mail[mail.find('@')+1:] not in domains: + auth.validate_uniqueness({'mail': mail}) + if mail[mail.find('@') + 1:] not in domains: raise MoulinetteError(errno.EINVAL, m18n.n('mail_domain_unknown', - domain=mail[mail.find('@')+1:])) + domain=mail[mail.find('@') + 1:])) del user['mail'][0] new_attr_dict['mail'] = [mail] + user['mail'] if add_mailalias: if not isinstance(add_mailalias, list): - add_mailalias = [ add_mailalias ] + add_mailalias = [add_mailalias] for mail in add_mailalias: - auth.validate_uniqueness({ 'mail': mail }) - if mail[mail.find('@')+1:] not in domains: + auth.validate_uniqueness({'mail': mail}) + if mail[mail.find('@') + 1:] not in domains: raise MoulinetteError(errno.EINVAL, m18n.n('mail_domain_unknown', - domain=mail[mail.find('@')+1:])) + domain=mail[mail.find('@') + 1:])) user['mail'].append(mail) new_attr_dict['mail'] = user['mail'] if remove_mailalias: if not isinstance(remove_mailalias, list): - remove_mailalias = [ remove_mailalias ] + remove_mailalias = [remove_mailalias] for mail in remove_mailalias: if len(user['mail']) > 1 and mail in user['mail'][1:]: user['mail'].remove(mail) @@ -324,7 +326,7 @@ def user_update(auth, username, firstname=None, lastname=None, mail=None, if add_mailforward: if not isinstance(add_mailforward, list): - add_mailforward = [ add_mailforward ] + add_mailforward = [add_mailforward] for mail in add_mailforward: if mail in user['maildrop'][1:]: continue @@ -333,7 +335,7 @@ def user_update(auth, username, firstname=None, lastname=None, mail=None, if remove_mailforward: if not isinstance(remove_mailforward, list): - remove_mailforward = [ remove_mailforward ] + remove_mailforward = [remove_mailforward] for mail in remove_mailforward: if len(user['maildrop']) > 1 and mail in user['maildrop'][1:]: user['maildrop'].remove(mail) @@ -346,11 +348,11 @@ def user_update(auth, username, firstname=None, lastname=None, mail=None, new_attr_dict['mailuserquota'] = mailbox_quota if auth.update('uid=%s,ou=users' % username, new_attr_dict): - logger.success(m18n.n('user_updated')) - app_ssowatconf(auth) - return user_info(auth, username) + logger.success(m18n.n('user_updated')) + app_ssowatconf(auth) + return user_info(auth, username) else: - raise MoulinetteError(169, m18n.n('user_update_failed')) + raise MoulinetteError(169, m18n.n('user_update_failed')) def user_info(auth, username): @@ -366,9 +368,9 @@ def user_info(auth, username): ] if len(username.split('@')) is 2: - filter = 'mail='+ username + filter = 'mail=' + username else: - filter = 'uid='+ username + filter = 'uid=' + username result = auth.search('ou=users,dc=yunohost,dc=org', filter, user_attrs) @@ -392,28 +394,31 @@ def user_info(auth, username): result_dict['mail-forward'] = user['maildrop'][1:] if 'mailuserquota' in user: - if user['mailuserquota'][0] != '0': - cmd = 'doveadm -f flow quota get -u %s' % user['uid'][0] - userquota = subprocess.check_output(cmd,stderr=subprocess.STDOUT, - shell=True) - quotavalue = re.findall(r'\d+', userquota) - result = '%s (%s%s)' % ( _convertSize(eval(quotavalue[0])), - quotavalue[2], '%') - result_dict['mailbox-quota'] = { - 'limit' : user['mailuserquota'][0], - 'use' : result - } + if user['mailuserquota'][0] != '0': + cmd = 'doveadm -f flow quota get -u %s' % user['uid'][0] + userquota = subprocess.check_output(cmd, stderr=subprocess.STDOUT, + shell=True) + quotavalue = re.findall(r'\d+', userquota) + result = '%s (%s%s)' % (_convertSize(eval(quotavalue[0])), + quotavalue[2], '%') + result_dict['mailbox-quota'] = { + 'limit': user['mailuserquota'][0], + 'use': result + } else: - result_dict['mailbox-quota'] = m18n.n('unlimit') - + result_dict['mailbox-quota'] = m18n.n('unlimit') + if result: return result_dict else: raise MoulinetteError(167, m18n.n('user_info_failed')) + def _convertSize(num, suffix=''): - for unit in ['K','M','G','T','P','E','Z']: + for unit in ['K', 'M', 'G', 'T', 'P', 'E', 'Z']: if abs(num) < 1024.0: return "%3.1f%s%s" % (num, unit, suffix) + num /= 1024.0 + return "%.1f%s%s" % (num, 'Yi', suffix) From 794bc258872f5b8e525261a252a1f5c121ccedd7 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 13:48:29 +0200 Subject: [PATCH 33/35] =?UTF-8?q?[fix]=20oops=20=E2=84=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/yunohost/domain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index 5f163aa3f..d8fbdd61c 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -194,7 +194,7 @@ def domain_remove(auth, domain, force=False): m18n.n('domain_uninstall_app_first')) if auth.remove('virtualdomain=' + domain + ',ou=domains') or force: - shutil.rmtree('rm -rf /etc/yunohost/certs/%s' % domain) + shutil.rmtree('/etc/yunohost/certs/%s' % domain) else: raise MoulinetteError(errno.EIO, m18n.n('domain_deletion_failed')) From 2ff2df12d5f13dc7bd4984bbc338330e9be3574b Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 13:49:18 +0200 Subject: [PATCH 34/35] [fix] remove duplicated line --- src/yunohost/domain.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index d8fbdd61c..d9beda2f4 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -199,7 +199,6 @@ def domain_remove(auth, domain, force=False): raise MoulinetteError(errno.EIO, m18n.n('domain_deletion_failed')) service_regen_conf(names=['nginx', 'metronome', 'dnsmasq']) - os.system('yunohost app ssowatconf > /dev/null 2>&1') assert os.system('yunohost app ssowatconf > /dev/null 2>&1') == 0, "SSOwat conf regen failed" hook_callback('post_domain_remove', args=[domain]) From 9f383970b7b1e442c67da5c7180db5918aa502eb Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 May 2016 13:51:44 +0200 Subject: [PATCH 35/35] [mod] replace os.system to a direct call to the good funciton --- src/yunohost/domain.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index d9beda2f4..b5aee0150 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -37,6 +37,7 @@ from moulinette.utils.log import getActionLogger from yunohost.service import service_regen_conf from yunohost.hook import hook_callback from yunohost.dyndns import dyndns_subscribe +from yunohost.app import app_ssowatconf logger = getActionLogger('yunohost.domain') @@ -154,7 +155,7 @@ def domain_add(auth, domain, dyndns=False): with open('/etc/yunohost/installed', 'r') as f: service_regen_conf(names=[ 'nginx', 'metronome', 'dnsmasq', 'rmilter']) - assert os.system('yunohost app ssowatconf > /dev/null 2>&1') == 0, "SSOwat conf regen failed" + app_ssowatconf(auth) except IOError: pass except: # Force domain removal silently @@ -199,7 +200,7 @@ def domain_remove(auth, domain, force=False): raise MoulinetteError(errno.EIO, m18n.n('domain_deletion_failed')) service_regen_conf(names=['nginx', 'metronome', 'dnsmasq']) - assert os.system('yunohost app ssowatconf > /dev/null 2>&1') == 0, "SSOwat conf regen failed" + app_ssowatconf(auth) hook_callback('post_domain_remove', args=[domain])