def write(self): if self.entries: with AtomicFile(self.path) as checksums: for entry_name in sorted(self.entries): print("%s *%s" % (self.entries[entry_name], entry_name), file=checksums) if self.sign: sign_cdimage(self.config, self.path) else: try: os.unlink(self.path) except OSError: pass
def write(self): if not self.changed: return if self.entries: with AtomicFile(self.path) as checksums: for entry_name in sorted(self.entries): print("%s *%s" % (self.entries[entry_name], entry_name), file=checksums) if self.sign: sign_cdimage(self.config, self.path) else: try: os.unlink(self.path) except OSError: pass
def test_sign_cdimage_configured(self, mock_check_call): config = Config(read=False) config["GNUPG_DIR"] = self.use_temp_dir() config["SIGNING_KEYID"] = "01234567" gpgdir, gpgconf, secring, privkeydir, pubring, trustdb = \ _gnupg_files(config) sign_path = os.path.join(self.temp_dir, "to-sign") for path in gpgconf, secring, pubring, trustdb, sign_path: touch(path) self.capture_logging() self.assertTrue(sign_cdimage(config, sign_path)) self.assertLogEqual([]) expected_command = [ "gpg", "--options", gpgconf, "--homedir", config["GNUPG_DIR"], "--no-options", "--batch", "--no-tty", "--armour", "--detach-sign", "--digest-algo", "SHA512", "-u", "01234567", ] mock_check_call.assert_called_once_with( expected_command, stdin=mock.ANY, stdout=mock.ANY) call = mock_check_call.call_args self.assertEqual(sign_path, call[1]["stdin"].name) self.assertEqual("%s.gpg" % sign_path, call[1]["stdout"].name)
def test_sign_cdimage_missing_gnupg_files(self): config = Config(read=False) config["GNUPG_DIR"] = self.use_temp_dir() config["SIGNING_KEYID"] = "01234567" self.capture_logging() self.assertFalse(sign_cdimage(config, "dummy")) self.assertLogEqual(["No keys found; not signing images."])
def test_sign_cdimage_subprocess_error(self, mock_check_call): mock_check_call.side_effect = subprocess.CalledProcessError(1, "") config = Config(read=False) config["GNUPG_DIR"] = self.use_temp_dir() config["SIGNING_KEYID"] = "01234567" gpgdir, gpgconf, secring, privkeydir, pubring, trustdb = \ _gnupg_files(config) sign_path = os.path.join(self.temp_dir, "to-sign") for path in gpgconf, secring, pubring, trustdb, sign_path: touch(path) touch("%s.gpg" % sign_path) self.capture_logging() with self.assertRaises(subprocess.CalledProcessError): sign_cdimage(config, sign_path) mock_check_call.assert_called() self.assertLogEqual([]) self.assertFalse(os.path.exists("%s.gpg" % sign_path))
def test_sign_cdimage_missing_gnupg_files(self): config = Config(read=False) self.use_temp_dir() config["GNUPG_DIR"] = self.temp_dir config["SIGNING_KEYID"] = "01234567" self.capture_logging() self.assertFalse(sign_cdimage(config, "dummy")) self.assertLogEqual(["No keys found; not signing images."])
def test_sign_cdimage_missing_signing_keyid(self): config = Config(read=False) self.use_temp_dir() for tail in "secring.gpg", "pubring.gpg", "trustdb.gpg": touch(os.path.join(self.temp_dir, tail)) config["GNUPG_DIR"] = self.temp_dir self.capture_logging() self.assertFalse(sign_cdimage(config, "dummy")) self.assertLogEqual(["No keys found; not signing images."])
def download_live_items(config, arch, item): output_dir = live_output_directory(config) found = False urls = list(live_item_paths(config, arch, item)) if not urls: return False if item in ( "kernel", "initrd", "bootimg" ): for url in urls: flavour = re.sub( r"^.*?\..*?\..*?-", "", unquote(os.path.basename(url))) target = os.path.join( output_dir, "%s.%s-%s" % (arch, item, flavour)) try: osextras.fetch(config, url, target) found = True except osextras.FetchError: pass elif item in ( "boot-%s+%s.img" % (target.ubuntu_arch, target.subarch) for target in Touch.list_targets_by_ubuntu_arch(arch) ): for url in urls: target = os.path.join(output_dir, item) try: osextras.fetch(config, url, target) found = True except osextras.FetchError: pass elif item in ( "modules.squashfs", ): for url in urls: base = unquote(os.path.basename(url)) base = "%s.%s" % (arch, base.split('.', 2)[2]) target = os.path.join(output_dir, base) try: osextras.fetch(config, url, target) found = True except osextras.FetchError: pass elif item in ( "recovery-%s+%s.img" % (target.android_arch, target.subarch) for target in Touch.list_targets_by_ubuntu_arch(arch) ): for url in urls: target = os.path.join(output_dir, item) try: osextras.fetch(config, url, target) found = True except osextras.FetchError: pass elif item in ( "system-%s+%s.img" % (target.android_arch, target.subarch) for target in Touch.list_targets_by_ubuntu_arch(arch) ): for url in urls: target = os.path.join(output_dir, item) try: osextras.fetch(config, url, target) found = True except osextras.FetchError: pass elif item == "kernel-efi-signed": for url in urls: base = unquote(os.path.basename(url)) if base.endswith(".efi.signed"): base = base[:-len(".efi.signed")] flavour = re.sub(r"^.*?\..*?\..*?-", "", base) target = os.path.join( output_dir, "%s.kernel-%s.efi.signed" % (arch, flavour)) try: osextras.fetch(config, url, target) found = True except osextras.FetchError: pass elif item in ("wubi", "usb-creator"): target = os.path.join(output_dir, "%s.%s.exe" % (arch, item)) try: osextras.fetch(config, urls[0], target) found = True except osextras.FetchError: pass else: for url in urls: # strip livecd.<PROJECT> and replace by arch filename = unquote(os.path.basename(url)).split('.', 2)[-1] target = os.path.join(output_dir, "%s.%s" % (arch, filename)) try: osextras.fetch(config, url, target) if target.endswith("squashfs"): sign.sign_cdimage(config, target) found = True except osextras.FetchError: pass return found
def download_live_items(config, arch, item): output_dir = live_output_directory(config) found = False if item == "server-squashfs": original_project = config.project try: config["PROJECT"] = "ubuntu-server" urls = list(live_item_paths(config, arch, "squashfs")) finally: config["PROJECT"] = original_project else: urls = list(live_item_paths(config, arch, item)) if not urls: return False if item in ("kernel", "initrd", "bootimg"): for url in urls: flavour = re.sub(r"^.*?\..*?\..*?-", "", unquote(os.path.basename(url))) target = os.path.join(output_dir, "%s.%s-%s" % (arch, item, flavour)) try: osextras.fetch(config, url, target) found = True except osextras.FetchError: pass elif item in ("boot-%s+%s.img" % (target.ubuntu_arch, target.subarch) for target in Touch.list_targets_by_ubuntu_arch(arch)): for url in urls: target = os.path.join(output_dir, item) try: osextras.fetch(config, url, target) found = True except osextras.FetchError: pass elif item in ("recovery-%s+%s.img" % (target.android_arch, target.subarch) for target in Touch.list_targets_by_ubuntu_arch(arch)): for url in urls: target = os.path.join(output_dir, item) try: osextras.fetch(config, url, target) found = True except osextras.FetchError: pass elif item in ("system-%s+%s.img" % (target.android_arch, target.subarch) for target in Touch.list_targets_by_ubuntu_arch(arch)): for url in urls: target = os.path.join(output_dir, item) try: osextras.fetch(config, url, target) found = True except osextras.FetchError: pass elif item == "kernel-efi-signed": for url in urls: base = unquote(os.path.basename(url)) if base.endswith(".efi.signed"): base = base[:-len(".efi.signed")] flavour = re.sub(r"^.*?\..*?\..*?-", "", base) target = os.path.join(output_dir, "%s.kernel-%s.efi.signed" % (arch, flavour)) try: osextras.fetch(config, url, target) found = True except osextras.FetchError: pass elif item in ("wubi", "umenu", "usb-creator"): target = os.path.join(output_dir, "%s.%s.exe" % (arch, item)) try: osextras.fetch(config, urls[0], target) found = True except osextras.FetchError: pass elif item == "winfoss": target = os.path.join(output_dir, "%s.%s.tgz" % (arch, item)) try: osextras.fetch(config, urls[0], target) found = True except osextras.FetchError: pass else: target = os.path.join(output_dir, "%s.%s" % (arch, item)) try: osextras.fetch(config, urls[0], target) if item in ["squashfs", "server-squashfs"]: sign.sign_cdimage(config, target) found = True except osextras.FetchError: pass return found