def extract_image_tarball(tarball, target_dir, temp_location=None): """Extract image from simplestreams tarball into `target_dir`. This copies the kernel, initrd, and .img file from the tarball into the target directory. The exact names of these files in the tarball may vary, but they will always be installed as `linux`, `initrd.gz`, and `disk.img`. Finally, this will run uec2roottar on the `disk.image` file, in order to create a `dist-root.tar.gz` tarball. :param tarball: Path to a tar file containing the image. :param target_dir: Directory where the image files should be installed. :param temp_location: Optional location where the function may create a temporary working directory for extracting the tarball. """ with tempdir(location=temp_location) as tmp: # Unpack tarball. The -S flag is for sparse files; the disk image # may have holes. call_and_check(["tar", "-Sxzf", tarball, "-C", tmp]) move_file_by_glob(tmp, '*-vmlinuz*', target_dir, 'linux') move_file_by_glob(tmp, '*-initrd*', target_dir, 'initrd.gz') image = move_file_by_glob(tmp, '*img', target_dir, 'disk.img') call_uec2roottar(image, os.path.join(target_dir, 'dist-root.tar.gz'))
def test_accepts_unicode_from_mkdtemp(self): fake_dir = os.path.join(self.make_dir(), factory.make_name('tempdir')) self.assertIsInstance(fake_dir, unicode) self.patch(tempfile, 'mkdtemp').return_value = fake_dir with tempdir() as directory: pass self.assertEqual(fake_dir, directory) self.assertIsInstance(directory, unicode)
def test_creates_real_fresh_directory(self): stored_text = factory.getRandomString() filename = factory.make_name('test-file') with tempdir() as directory: self.assertThat(directory, DirExists()) write_text_file(os.path.join(directory, filename), stored_text) retrieved_text = read_text_file(os.path.join(directory, filename)) files = os.listdir(directory) self.assertEqual(stored_text, retrieved_text) self.assertEqual([filename], files)
def test_uses_location(self): temp_location = self.make_dir() with tempdir(location=temp_location) as directory: self.assertThat(directory, DirExists()) location_listing = os.listdir(temp_location) self.assertNotEqual(temp_location, directory) self.assertThat(directory, StartsWith(temp_location + os.path.sep)) self.assertIn(os.path.basename(directory), location_listing) self.assertThat(temp_location, DirExists()) self.assertThat(directory, Not(DirExists()))
def test_cleans_up_on_exception_exit(self): class DeliberateFailure(Exception): pass with ExpectedException(DeliberateFailure): with tempdir() as directory: file_path = factory.make_file(directory) raise DeliberateFailure("Exiting context by exception") self.assertThat(directory, Not(DirExists())) self.assertThat(file_path, Not(FileExists()))
def test_decodes_bytes_from_mkdtemp(self): encoding = 'utf-16' self.patch(sys, 'getfilesystemencoding').return_value = encoding fake_dir = os.path.join(self.make_dir(), factory.make_name('tempdir')) self.patch(tempfile, 'mkdtemp').return_value = fake_dir.encode( encoding) with tempdir() as directory: pass self.assertEqual(fake_dir, directory) self.assertIsInstance(directory, unicode)
def generate_omapi_key(): """Generate a HMAC-MD5 key by calling out to the dnssec-keygen tool. :return: The shared key suitable for OMAPI access. :type: string """ # dnssec-keygen writes out files to a specified directory, so we # need to make a temp directory for that. # This relies on the temporary directory being accessible only to its # owner. temp_prefix = "%s." % os.path.basename(__file__) with tempdir(prefix=temp_prefix) as tmpdir: key = run_repeated_keygen(tmpdir) return key
def generate_omapi_key(): """Generate a HMAC-MD5 key by calling out to the dnssec-keygen tool. :return: The shared key suitable for OMAPI access. :type: string """ # dnssec-keygen writes out files to a specified directory, so we # need to make a temp directory for that. # This relies on the temporary directory being accessible only to its # owner. temp_prefix = "%s." % os.path.basename(__file__) with tempdir(prefix=temp_prefix) as tmpdir: key = run_repeated_keygen(tmpdir) return key
def test_yields_unicode(self): with tempdir() as directory: pass self.assertIsInstance(directory, unicode)
def test_tolerates_disappearing_dir(self): with tempdir() as directory: rmtree(directory) self.assertThat(directory, Not(DirExists()))
def test_cleans_up_on_successful_exit(self): with tempdir() as directory: file_path = factory.make_file(directory) self.assertThat(directory, Not(DirExists())) self.assertThat(file_path, Not(FileExists()))
def test_creates_unique_directory(self): with tempdir() as dir1, tempdir() as dir2: pass self.assertNotEqual(dir1, dir2)
def test_restricts_access(self): with tempdir() as directory: mode = os.stat(directory).st_mode self.assertEqual( stat.S_IMODE(mode), stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
def test_uses_suffix(self): suffix = factory.getRandomString(3) with tempdir(suffix=suffix) as directory: pass self.assertThat(os.path.basename(directory), EndsWith(suffix))
def test_uses_prefix(self): prefix = factory.getRandomString(3) with tempdir(prefix=prefix) as directory: pass self.assertThat(os.path.basename(directory), StartsWith(prefix))