def online_dasd(dev): """ Given a device number, switch the device to be online. :param str dev: a DASD device number Raises a ValueError if a device with that number does not exist, is already online, or can not be put online. """ online = "/sys/bus/ccw/drivers/dasd-eckd/%s/online" % (dev) if not os.path.exists(online): log.info("Freeing DASD device %s", dev) util.run_program(["dasd_cio_free", "-d", dev]) if not os.path.exists(online): raise ValueError(_("DASD device %s not found, not even in device ignore list.") % dev) try: with open(online, "r") as f: devonline = f.readline().strip() if devonline == "1": raise ValueError(_("Device %s is already online.") % dev) else: with open(online, "w") as f: log.debug("echo %s > %s", "1", online) f.write("%s\n" % ("1")) except IOError as e: raise ValueError(_("Could not set DASD device %(dev)s online (%(e)s).") \ % {'dev': dev, 'e': e})
def testHumanReadableTranslation(self): s = Size("56.19 MiB") size_str = s.humanReadable() for lang in self.TEST_LANGS: os.environ['LANG'] = lang locale.setlocale(locale.LC_ALL, '') self.assertTrue(s.humanReadable().endswith("%s%s" % (_("Mi"), _("B")))) self.assertEqual(s.humanReadable(xlate=False), size_str)
def sanitize_dasd_dev_input(dev): """ Synthesizes a complete DASD number from a possibly partial one. :param str dev: a possibly partial DASD device number :returns: a synthesized DASD device number :rtype: str :raises: ValueError if dev is None or empty *) Assumes that the rightmost '.' if any, separates the bus number from the device number. *) Pads the device number on the left with 0s to a length of four characters. *) If no bus number extracted from dev, uses bus number default 0.0. A DASD number has the format n.n.hhhh, where n is any decimal digit and h any hexadecimal digit, e.g., 0.0.abcd, 0.0.002A. A properly formatted number can be synthesized from a partial number if the partial number is missing hexadecimal digits, e.g., 0.0.b, or missing a bus number, e.g., 0012. The minimal partial number contains a single digit. For example a will be extended to 0.0.000a. Wildly improper partial numbers, e.g., qu.er.ty will yield a wildly improper result. """ if dev is None or dev == "": raise ValueError(_("You have not specified a device number or the number is invalid")) dev = dev.lower() (bus, _sep, dev) = dev.rpartition('.') padding = "0" * (4 - len(dev)) bus = bus or '0.0' return bus + '.' + padding + dev
def testMakeSpec(self): """ Tests for _makeSpecs(). """ for lang in self.TEST_LANGS: os.environ['LANG'] = lang locale.setlocale(locale.LC_ALL, '') # untranslated specs self.assertEqual(size._makeSpec("", "BYTES", False), "bytes") self.assertEqual(size._makeSpec("Mi", "b", False), "mib") # un-lower-cased specs self.assertEqual(size._makeSpec("", "BYTES", False, False), "BYTES") self.assertEqual(size._makeSpec("Mi", "b", False, False), "Mib") self.assertEqual(size._makeSpec("Mi", "B", False, False), "MiB") # translated specs res = size._makeSpec("", "bytes", True) # Note that exp != _("bytes").lower() as one might expect exp = (_("") + _("bytes")).lower() self.assertEqual(res, exp)
def run(self): """Run the task. :raise: UnusableStorageError if the model is not usable """ try: self._reload_modules() self._reset_storage(self._storage) except UnusableConfigurationError as e: log.error("Failed to scan devices: %s", e) message = "\n\n".join([str(e), _(e.suggestion)]) raise UnusableStorageError(message) from None
def run(self): """Run the task. :raise: UnusableStorageError if the model is not usable """ try: self._devicetree.populate() self._devicetree.teardown_all() except UnusableConfigurationError as e: log.error("Failed to find devices: %s", e) message = "\n\n".join([str(e), _(e.suggestion)]) raise UnusableStorageError(message) from None
def testParseSpec(self): """ Tests for parseSpec(). """ for lang in self.TEST_LANGS: os.environ['LANG'] = lang locale.setlocale(locale.LC_ALL, '') # Test parsing English spec in foreign locales self.assertEqual(size.parseSpec("1 kibibytes"), Decimal(1024)) self.assertEqual(size.parseSpec("2 kibibyte"), Decimal(2048)) self.assertEqual(size.parseSpec("2 kilobyte"), Decimal(2000)) self.assertEqual(size.parseSpec("2 kilobytes"), Decimal(2000)) self.assertEqual(size.parseSpec("2 KB"), Decimal(2000)) self.assertEqual(size.parseSpec("2 K"), Decimal(2048)) self.assertEqual(size.parseSpec("2 k"), Decimal(2048)) self.assertEqual(size.parseSpec("2 Ki"), Decimal(2048)) self.assertEqual(size.parseSpec("2 g"), Decimal(2 * 1024 ** 3)) self.assertEqual(size.parseSpec("2 G"), Decimal(2 * 1024 ** 3)) # Test parsing foreign spec self.assertEqual(size.parseSpec("1 %s%s" % (_("kibi"), _("bytes"))), Decimal(1024)) # Can't parse a valueless number with self.assertRaises(ValueError): size.parseSpec("Ki") self.assertEqual(size.parseSpec("2 %s" % _("K")), Decimal(2048)) self.assertEqual(size.parseSpec("2 %s" % _("Ki")), Decimal(2048)) self.assertEqual(size.parseSpec("2 %s" % _("g")), Decimal(2 * 1024 ** 3)) self.assertEqual(size.parseSpec("2 %s" % _("G")), Decimal(2 * 1024 ** 3))
def testTranslated(self): s = Size("56.19 MiB") for lang in self.TEST_LANGS: os.environ['LANG'] = lang locale.setlocale(locale.LC_ALL, '') # Check English parsing self.assertEqual(s, Size("56.19 MiB")) # Check native parsing self.assertEqual(s, Size("56.19 %s%s" % (_("Mi"), _("B")))) # Check native parsing, all lowercase self.assertEqual(s, Size(("56.19 %s%s" % (_("Mi"), _("B"))).lower())) # Check native parsing, all uppercase self.assertEqual(s, Size(("56.19 %s%s" % (_("Mi"), _("B"))).upper())) # If the radix separator is not a period, repeat the tests with the # native separator radix = locale.nl_langinfo(locale.RADIXCHAR) if radix != '.': self.assertEqual(s, Size("56%s19 MiB" % radix)) self.assertEqual(s, Size("56%s19 %s%s" % (radix, _("Mi"), _("B")))) self.assertEqual(s, Size(("56%s19 %s%s" % (radix, _("Mi"), _("B"))).lower())) self.assertEqual(s, Size(("56%s19 %s%s" % (radix, _("Mi"), _("B"))).upper()))
def testTranslated(self): import locale import os from blivet.i18n import _ saved_lang = os.environ.get('LANG', None) # es_ES uses latin-characters but a comma as the radix separator # kk_KZ uses non-latin characters and is case-sensitive # te_IN uses a lot of non-letter modifier characters # fa_IR uses non-ascii digits, or would if python supported that, but # you know, just in case test_langs = ["es_ES.UTF-8", "kk_KZ.UTF-8", "ml_IN.UTF-8", "fa_IR.UTF-8"] s = Size("56.19 MiB") for lang in test_langs: os.environ['LANG'] = lang locale.setlocale(locale.LC_ALL, '') # Check English parsing self.assertEquals(s, Size("56.19 MiB")) # Check native parsing self.assertEquals(s, Size("56.19 %s%s" % (_("Mi"), _("B")))) # Check native parsing, all lowercase self.assertEquals(s, Size(("56.19 %s%s" % (_("Mi"), _("B"))).lower())) # Check native parsing, all uppercase self.assertEquals(s, Size(("56.19 %s%s" % (_("Mi"), _("B"))).upper())) # If the radix separator is not a period, repeat the tests with the # native separator radix = locale.nl_langinfo(locale.RADIXCHAR) if radix != '.': self.assertEquals(s, Size("56%s19 MiB" % radix)) self.assertEquals(s, Size("56%s19 %s%s" % (radix, _("Mi"), _("B")))) self.assertEquals(s, Size(("56%s19 %s%s" % (radix, _("Mi"), _("B"))).lower())) self.assertEquals(s, Size(("56%s19 %s%s" % (radix, _("Mi"), _("B"))).upper())) os.environ['LANG'] = saved_lang locale.setlocale(locale.LC_ALL, '')