summaryrefslogtreecommitdiffstats
path: root/pyanaconda
diff options
context:
space:
mode:
authorVratislav Podzimek <vpodzime@redhat.com>2012-06-15 13:44:14 +0200
committerVratislav Podzimek <vpodzime@redhat.com>2012-08-22 19:59:32 +0200
commit73db213960514beb254eb9298955d41831ede3bc (patch)
treedef59bb8e10457c3503f47820cfde3fa2a498586 /pyanaconda
parent0c00d5f19579f283a510881b0988d40d16ffe955 (diff)
downloadanaconda-73db213960514beb254eb9298955d41831ede3bc.tar.gz
anaconda-73db213960514beb254eb9298955d41831ede3bc.tar.xz
anaconda-73db213960514beb254eb9298955d41831ede3bc.zip
Use ksdata.timezone and timezone module instead of anaconda.timezone
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.
Diffstat (limited to 'pyanaconda')
-rw-r--r--pyanaconda/__init__.py11
-rw-r--r--pyanaconda/install.py1
-rw-r--r--pyanaconda/kickstart.py8
-rw-r--r--pyanaconda/localization.py38
-rw-r--r--pyanaconda/packages.py6
-rw-r--r--pyanaconda/timezone.py200
-rw-r--r--pyanaconda/ui/gui/spokes/datetime_spoke.py4
-rw-r--r--pyanaconda/ui/gui/spokes/welcome.py4
-rw-r--r--pyanaconda/ui/tui/spokes/time.py4
9 files changed, 158 insertions, 118 deletions
diff --git a/pyanaconda/__init__.py b/pyanaconda/__init__.py
index 71629286c..5f2ccbe1f 100644
--- a/pyanaconda/__init__.py
+++ b/pyanaconda/__init__.py
@@ -76,7 +76,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
@@ -188,15 +187,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
@@ -267,7 +257,6 @@ class Anaconda(object):
self.writeXdriver()
self.instLanguage.write()
- self.timezone.write()
network.write_sysconfig_network()
network.disableIPV6()
network.copyConfigToPath(ROOT_PATH)
diff --git a/pyanaconda/install.py b/pyanaconda/install.py
index a2f54c33d..b5a61523d 100644
--- a/pyanaconda/install.py
+++ b/pyanaconda/install.py
@@ -84,6 +84,7 @@ def doInstall(storage, payload, ksdata, instClass):
ksdata.firstboot.execute(storage, ksdata, instClass)
ksdata.services.execute(storage, ksdata, instClass)
ksdata.keyboard.execute(storage, ksdata, instClass)
+ ksdata.timezone.execute(storage, ksdata, instClass)
# Creating users and groups requires some pre-configuration.
u = Users()
diff --git a/pyanaconda/kickstart.py b/pyanaconda/kickstart.py
index 73d435d82..ffa137887 100644
--- a/pyanaconda/kickstart.py
+++ b/pyanaconda/kickstart.py
@@ -49,7 +49,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 pyanaconda.simpleconfig import SimpleConfigFile
from pykickstart.base import KickstartCommand
@@ -1252,9 +1252,11 @@ class Services(commands.services.FC6_Services):
root=ROOT_PATH)
class Timezone(commands.timezone.F18_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.ntpservers,
conf_file_path=chronyd_conf_path)
diff --git a/pyanaconda/localization.py b/pyanaconda/localization.py
index 1c90b645d..74e68b987 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 334ee4a51..360451ab9 100644
--- a/pyanaconda/packages.py
+++ b/pyanaconda/packages.py
@@ -44,8 +44,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")
@@ -62,7 +62,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 145a80515..b7bc559fb 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@redhat.com>
#
-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 fobj:
+ fobj.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:
+ fobj = open(os.path.normpath(root + "/etc/adjtime"), "r")
+ lines = fobj.readlines()
+ fobj.close()
+ except IOError:
+ lines = [ "0.0 0 0.0\n", "0\n" ]
+
+ try:
+ with open(os.path.normpath(root + "/etc/adjtime"), "w") as fobj:
+ fobj.write(lines[0])
+ fobj.write(lines[1])
+ if timezone.isUtc:
+ fobj.write("UTC\n")
+ else:
+ fobj.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 5b0a48251..4d345df78 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 7982e2361..8a23d5b86 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 isFinal, 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
diff --git a/pyanaconda/ui/tui/spokes/time.py b/pyanaconda/ui/tui/spokes/time.py
index 5f401c216..bac1d0f6e 100644
--- a/pyanaconda/ui/tui/spokes/time.py
+++ b/pyanaconda/ui/tui/spokes/time.py
@@ -21,7 +21,7 @@
from pyanaconda.ui.tui.spokes import NormalTUISpoke
from pyanaconda.ui.tui.simpleline import TextWidget, ColumnWidget
-from pyanaconda import localization
+from pyanaconda import timezone
import gettext
_ = lambda x: gettext.ldgettext("anaconda", x)
@@ -34,7 +34,7 @@ class TimeZoneSpoke(NormalTUISpoke):
NormalTUISpoke.__init__(self, app, data, storage, payload, instclass)
def initialize(self):
- self._timezones = dict([(k, sorted(v)) for k,v in localization.get_all_regions_and_timezones().iteritems()])
+ self._timezones = dict([(k, sorted(v)) for k,v in timezone.get_all_regions_and_timezones().iteritems()])
self._regions = [r for r in self._timezones]
self._lower_regions = [r.lower() for r in self._timezones]