We now use ksdata to store data and execute() methods of kickstart commands to set the installed system up. In order to not place all the code to kickstart.py, timezone.py module gathering all timezone related code was created. --- anaconda | 2 +- pyanaconda/__init__.py | 11 -- pyanaconda/install.py | 1 + pyanaconda/kickstart.py | 7 +- pyanaconda/localization.py | 38 ------ pyanaconda/packages.py | 6 +- pyanaconda/timezone.py | 200 ++++++++++++++++++++-------- pyanaconda/ui/gui/spokes/datetime_spoke.py | 4 +- pyanaconda/ui/gui/spokes/welcome.py | 4 +- 9 files changed, 156 insertions(+), 117 deletions(-) diff --git a/anaconda b/anaconda index bb847c9..d525412 100755 --- a/anaconda +++ b/anaconda @@ -824,7 +824,7 @@ if __name__ == "__main__": anaconda.instLanguage.instLang = opts.lang anaconda.instLanguage.buildLocale() anaconda.instLanguage.systemLang = opts.lang - anaconda.timezone.setTimezoneInfo(anaconda.instLanguage.getDefaultTimeZone()) + anaconda.ksdata.timezone.timezone = anaconda.instLanguage.getDefaultTimeZone() from pyanaconda.storage import storageInitialize from pyanaconda.packaging import payloadInitialize diff --git a/pyanaconda/__init__.py b/pyanaconda/__init__.py index fd1e676..d3dc39b 100644 --- a/pyanaconda/__init__.py +++ b/pyanaconda/__init__.py @@ -80,7 +80,6 @@ class Anaconda(object): self.simpleFilter = not iutil.isS390() self.stage2 = None self._storage = None - self._timezone = None self.updateSrc = None self.upgrade = flags.cmdline.has_key("preupgrade") self.upgradeRoot = None @@ -210,15 +209,6 @@ class Anaconda(object): return self._storage - @property - def timezone(self): - if not self._timezone: - import timezone - self._timezone = timezone.Timezone() - self._timezone.setTimezoneInfo(self.instLanguage.getDefaultTimeZone()) - - return self._timezone - def dumpState(self): from meh.dump import ReverseExceptionDump from inspect import stack as _stack @@ -283,7 +273,6 @@ class Anaconda(object): self.writeXdriver() self.instLanguage.write() - self.timezone.write() if not self.ksdata: self.instClass.setNetworkOnbootDefault(self.network) self.network.write() diff --git a/pyanaconda/install.py b/pyanaconda/install.py index d377c40..92fc268 100644 --- a/pyanaconda/install.py +++ b/pyanaconda/install.py @@ -81,6 +81,7 @@ def doInstall(storage, payload, ksdata, instClass): # to be present first. ksdata.services.execute(storage, ksdata, instClass) ksdata.keyboard.execute(storage, ksdata, instClass) + ksdata.timezone.execute(storage, ksdata, instClass) runPostScripts(ksdata.scripts) diff --git a/pyanaconda/kickstart.py b/pyanaconda/kickstart.py index d8abaac..b25c815 100644 --- a/pyanaconda/kickstart.py +++ b/pyanaconda/kickstart.py @@ -46,7 +46,7 @@ import pykickstart.commands as commands from storage.devices import * from pyanaconda import keyboard from pyanaconda import ntp -from pyanaconda import localization +from pyanaconda import timezone from pykickstart.base import KickstartCommand from pykickstart.constants import * @@ -1007,10 +1007,11 @@ class Services(commands.services.FC6_Services): root=ROOT_PATH) class Timezone(commands.timezone.FC6_Timezone): - def execute(self): - if not localization.is_valid_timezone(self.timezone): + def execute(self, *args): + if not timezone.is_valid_timezone(self.timezone): log.warning("Timezone %s set in kickstart is not valid." % (self.timezone,)) + timezone.write_timezone_config(self, ROOT_PATH) chronyd_conf_path = os.path.normpath(ROOT_PATH + ntp.NTP_CONFIG_FILE) ntp.save_servers_to_config(self.ntp_servers, conf_file_path=chronyd_conf_path) diff --git a/pyanaconda/localization.py b/pyanaconda/localization.py index 1c90b64..74e68b9 100644 --- a/pyanaconda/localization.py +++ b/pyanaconda/localization.py @@ -26,7 +26,6 @@ import os import re import babel -import pytz LOCALE_PREFERENCES = {} @@ -267,40 +266,3 @@ class Language(object): def set_system_lang(self, langcode): self.system_lang = langcode - - -def get_all_territory_timezones(territory): - if isinstance(territory, LocaleInfo): - territory = territory.territory - - try: - timezones = pytz.country_timezones(territory) - except KeyError: - timezones = list() - - return timezones - - -def get_preferred_timezone(territory): - try: - timezone = get_all_territory_timezones(territory)[0] - except IndexError: - timezone = None - - return timezone - -def get_all_regions_and_timezones(): - result = OrderedDict() - - for tz in pytz.common_timezones: - parts = tz.split("/", 1) - - if len(parts) > 1: - if parts[0] not in result: - result[parts[0]] = set() - result[parts[0]].add(parts[1]) - - return result - -def is_valid_timezone(timezone): - return timezone in pytz.common_timezones diff --git a/pyanaconda/packages.py b/pyanaconda/packages.py index dc503e5..a499f42 100644 --- a/pyanaconda/packages.py +++ b/pyanaconda/packages.py @@ -55,8 +55,8 @@ def setupTimezone(anaconda): if anaconda.upgrade or flags.imageInstall or anaconda.dir == DISPATCH_BACK: return - os.environ["TZ"] = anaconda.timezone.tz - tzfile = "/usr/share/zoneinfo/" + anaconda.timezone.tz + os.environ["TZ"] = anaconda.ksdata.timezone.timezone + tzfile = "/usr/share/zoneinfo/" + anaconda.ksdata.timezone.timezone tzlocalfile = "/etc/localtime" if not os.access(tzfile, os.R_OK): log.error("unable to set timezone") @@ -73,7 +73,7 @@ def setupTimezone(anaconda): if iutil.isS390(): return args = [ "--hctosys" ] - if anaconda.timezone.utc: + if anaconda.ksdata.timezone.isUtc: args.append("-u") try: diff --git a/pyanaconda/timezone.py b/pyanaconda/timezone.py index 145a805..2c03029 100644 --- a/pyanaconda/timezone.py +++ b/pyanaconda/timezone.py @@ -1,72 +1,158 @@ # -# timezone.py - timezone install data +# Copyright (C) 2012 Red Hat, Inc. # -# Copyright (C) 2001 Red Hat, Inc. All rights reserved. +# This copyrighted material is made available to anyone wishing to use, +# modify, copy, or redistribute it subject to the terms and conditions of +# the GNU General Public License v.2, or (at your option) any later version. +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY expressed or implied, including the implied warranties of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General +# Public License for more details. You should have received a copy of the +# GNU General Public License along with this program; if not, write to the +# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the +# source code or documentation are not subject to the GNU General Public +# License and may only be used or replicated with the express permission of +# Red Hat, Inc. # -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. +# Red Hat Author(s): Vratislav Podzimek <vpodzime@xxxxxxxxxx> # -import shutil -import iutil +""" +Module providing functions for getting the list of timezones, writing timezone +configuration, valid timezones recognition etc. + +""" + import os -from flags import flags -from pyanaconda.constants import ROOT_PATH +import pytz +from collections import OrderedDict + +from pyanaconda import localization import logging log = logging.getLogger("anaconda") -class Timezone: - def write(self): - fromFile = ROOT_PATH + "/usr/share/zoneinfo/" + self.tz +class TimezoneConfigError(Exception): + """Exception class for timezone configuration related problems""" + pass - if not os.access(fromFile, os.R_OK): - log.error("Timezone to be copied (%s) doesn't exist" % fromFile) - else: - try: - shutil.copyfile(fromFile, ROOT_PATH + "/etc/localtime") - except EnvironmentError as e: - log.error("Error copying timezone (from %s): %s" % (fromFile, e.strerror)) +def write_timezone_config(timezone, root): + """ + Write timezone configuration for the system specified by root. - f = open(ROOT_PATH + "/etc/sysconfig/clock", "w") + @param timezone: ksdata.timezone object + @param root: path to the root + @raise: TimezoneConfigError - f.write('ZONE="%s"\n' % self.tz) - f.close() + """ + #we want to create a relative symlink + tz_file = "/usr/share/zoneinfo/" + timezone.timezone + rooted_tz_file = os.path.normpath(root + tz_file) + relative_path = os.path.normpath("../" + tz_file) + link_path = os.path.normpath(root + "/etc/localtime") + + if not os.access(rooted_tz_file, os.R_OK): + log.error("Timezone to be linked (%s) doesn't exist" % rooted_tz_file) + else: try: - f = open(ROOT_PATH + "/etc/adjtime", "r") - lines = f.readlines() - f.close() - except: - lines = [ "0.0 0 0.0\n", "0\n" ] - - f = open(ROOT_PATH + "/etc/adjtime", "w") - f.write(lines[0]) - f.write(lines[1]) - if self.utc: - f.write("UTC\n") - else: - f.write("LOCAL\n") - f.close() - - def getTimezoneInfo(self): - return (self.tz, self.utc) - - def setTimezoneInfo(self, timezone, asUtc = 0): - log.info("set timezone: %s, utc: %d" % (timezone, asUtc)) - self.tz = timezone - self.utc = asUtc - - def __init__(self): - self.tz = "America/New_York" - self.utc = 0 + os.symlink(relative_path, link_path) + except OSError as oserr: + log.error("Error when symlinking timezone (from %s): %s" % \ + (rooted_tz_file, oserr.strerror)) + + try: + with open(os.path.normpath(root + "/etc/sysconfig/clock"), "w") as file_: + file_.write('ZONE="%s"\n' % timezone.timezone) + except IOError as ioerr: + msg = "Error while writing /etc/sysconfig/clock file: %s" % \ + ioerr.strerror + raise TimezoneConfigError(msg) + + try: + file_ = open(os.path.normpath(root + "/etc/adjtime"), "r") + lines = file_.readlines() + file_.close() + except IOError: + lines = [ "0.0 0 0.0\n", "0\n" ] + + try: + with open(os.path.normpath(root + "/etc/adjtime"), "w") as file_: + file_.write(lines[0]) + file_.write(lines[1]) + if timezone.isUtc: + file_.write("UTC\n") + else: + file_.write("LOCAL\n") + except IOError as ioerr: + msg = "Error while writing /etc/adjtime file: %s" % ioerr.strerror + raise TimezoneConfigError(msg) + +def get_all_territory_timezones(territory): + """ + Return the list of timezones for a given territory. + + @param territory: either localization.LocaleInfo or territory + + """ + + if isinstance(territory, localization.LocaleInfo): + territory = territory.territory + + try: + timezones = pytz.country_timezones(territory) + except KeyError: + timezones = list() + + return timezones + + +def get_preferred_timezone(territory): + """ + Get the preferred timezone for a given territory. Note that this function + simply returns the first timezone in the list of timezones for a given + territory. + + @param territory: either localization.LocaleInfo or territory + + """ + + try: + timezone = get_all_territory_timezones(territory)[0] + except IndexError: + timezone = None + + return timezone + +def get_all_regions_and_timezones(): + """ + Get a dictionary mapping the regions to the list of their timezones. + + @rtype: dict + + """ + + result = OrderedDict() + + for tz in pytz.common_timezones: + parts = tz.split("/", 1) + + if len(parts) > 1: + if parts[0] not in result: + result[parts[0]] = set() + result[parts[0]].add(parts[1]) + + return result + +def is_valid_timezone(timezone): + """ + Check if a given string is an existing timezone. + + @type timezone: str + @rtype: bool + + """ + + return timezone in pytz.common_timezones + diff --git a/pyanaconda/ui/gui/spokes/datetime_spoke.py b/pyanaconda/ui/gui/spokes/datetime_spoke.py index a1d3730..e784fda 100644 --- a/pyanaconda/ui/gui/spokes/datetime_spoke.py +++ b/pyanaconda/ui/gui/spokes/datetime_spoke.py @@ -30,7 +30,7 @@ from pyanaconda.ui.gui.spokes import NormalSpoke from pyanaconda.ui.gui.categories.localization import LocalizationCategory from pyanaconda.ui.gui.utils import enlightbox -from pyanaconda import localization, iutil, network, ntp +from pyanaconda import timezone, iutil, network, ntp from pyanaconda.threads import threadMgr, AnacondaThread import datetime, os, threading @@ -263,7 +263,7 @@ class DatetimeSpoke(NormalSpoke): self._citiesStore = self.builder.get_object("cities") self._tzmap = self.builder.get_object("tzmap") - self._regions_zones = localization.get_all_regions_and_timezones() + self._regions_zones = timezone.get_all_regions_and_timezones() for day in xrange(1, 32): self.add_to_store(self._daysStore, day) diff --git a/pyanaconda/ui/gui/spokes/welcome.py b/pyanaconda/ui/gui/spokes/welcome.py index f1b6963..30b8c18 100644 --- a/pyanaconda/ui/gui/spokes/welcome.py +++ b/pyanaconda/ui/gui/spokes/welcome.py @@ -34,7 +34,7 @@ from pyanaconda.ui.gui.categories.localization import LocalizationCategory from pyanaconda.localization import Language, LOCALE_PREFERENCES from pyanaconda.product import productName, productVersion from pyanaconda import keyboard -from pyanaconda import localization +from pyanaconda import timezone __all__ = ["WelcomeLanguageSpoke", "LanguageSpoke"] @@ -59,7 +59,7 @@ class LanguageMixIn(object): #TODO: better use GeoIP data once it is available if self.language.territory and not self.data.timezone.timezone: - lang_timezone = localization.get_preferred_timezone(self.language.territory) + lang_timezone = timezone.get_preferred_timezone(self.language.territory) if lang_timezone: self.data.timezone.timezone = lang_timezone -- 1.7.4.4 _______________________________________________ Anaconda-devel-list mailing list Anaconda-devel-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/anaconda-devel-list