From f4f4d245a5c4923d43a4f0ad00609316ec328590 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 30 Jun 2017 16:39:13 +0200 Subject: [PATCH] Trying some changes because of Python2 --- pp_lib/quota_check.py | 50 ++++++++++++++++++++++++++++++++++++++++--- quota-check | 13 +++++++++-- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/pp_lib/quota_check.py b/pp_lib/quota_check.py index 8417257..402ddd8 100644 --- a/pp_lib/quota_check.py +++ b/pp_lib/quota_check.py @@ -44,7 +44,24 @@ from .cfg_app import PpCfgAppError, PpConfigApplication __version__ = '0.5.4' LOG = logging.getLogger(__name__) -UTC = datetime.timezone.utc +ZERO = datetime.timedelta(0) + +# A Utc class. + +class Utc(datetime.tzinfo): + """Utc""" + + def utcoffset(self, dt): + return ZERO + + def tzname(self, dt): + return "UTC" + + def dst(self, dt): + return ZERO + +UTC = Utc() +# UTC = datetime.timezone.utc # ============================================================================= @@ -379,6 +396,7 @@ class PpQuotaCheckApp(PpConfigApplication): LOG.info("Writing status data from {!r} ...".format(self.statusfile)) if self.verbose > 2: + # LOG.debug("Status to write:\n{!r}".format(self.status_data)) LOG.debug("Status to write:\n{}".format(pp(self.status_data))) open_args = {} @@ -389,9 +407,13 @@ class PpQuotaCheckApp(PpConfigApplication): if not os.path.exists(self.status_dir): LOG.info("Creating {!r} ...".format(self.status_dir)) mode = stat.S_IRWXU | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IXOTH - os.makedirs(self.status_dir, mode, exist_ok=True) + try: + os.makedirs(self.status_dir, mode) + except os.error as e: + LOG.error("Could not create {!r}: {}".format(self.status_dir, e)) + sys.exit(9) elif not os.path.isdir(self.status_dir): - msg = "Status directory {!r} exists, but is ot a directory.".format(self.status_dir) + msg = "Status directory {!r} exists, but is not a directory.".format(self.status_dir) LOG.error(msg) return @@ -468,6 +490,25 @@ class PpQuotaCheckApp(PpConfigApplication): LOG.info("Checking utilization of home directories ...") + # Senseless opening of all user home directories to activate automounter + for user_name in self.passwd_data: + entry = self.passwd_data[user_name] + pwd_home_dir = entry.pw_dir + if not pwd_home_dir: + continue + if not pwd_home_dir.startswith(self.home_root_abs): + if self.verbose > 2: + LOG.debug("Home dir {!r} is not below {!r}.".format(pwd_home_dir, self.home_root_abs)) + continue + abs_home_dir = os.path.join(self.chroot_homedir, os.path.relpath(pwd_home_dir, os.sep)) + LOG.debug("Trying to open {!r} ...".format(abs_home_dir)) + try: + os.listdir(abs_home_dir) + if self.verbose > 2: + LOG.debug("Found home directory {!r} ...".format(abs_home_dir)) + except OSError as e: + LOG.warn("Directory {!r} does not exists.".format(abs_home_dir)) + glob_pattern = os.path.join(self.home_root_real, '*') all_home_entries = glob.glob(glob_pattern) @@ -492,6 +533,9 @@ class PpQuotaCheckApp(PpConfigApplication): i = 0 for path in all_home_entries: + + LOG.debug("Searching for {!r} ...".format(path)) + if not os.path.isdir(path): continue number_dirs += 1 diff --git a/quota-check b/quota-check index c168f0a..af46130 100755 --- a/quota-check +++ b/quota-check @@ -1,4 +1,7 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from __future__ import print_function # Standard modules import sys @@ -26,7 +29,13 @@ __copyright__ = '(C) 2017 by Frank Brehm, Pixelpark GmbH, Berlin' appname = os.path.basename(sys.argv[0]) -locale.setlocale(locale.LC_ALL, 'de_DE.utf8') +for loc in ('de_DE.utf8', 'en_US.UTF-8'): + try: + locale.setlocale(locale.LC_ALL, loc) + break + except locale.Error as e: + sys.stderr.write("Locale %r not supported: %s\n" % (loc, e)) + continue app = PpQuotaCheckApp(appname=appname) app.initialized = True -- 2.39.5