def _ProfileWithExtraFiles(profile_dir, profile_files_to_copy): """Yields a temporary directory populated with input files. Args: profile_dir: A directory whose contents will be copied to the output directory. profile_files_to_copy: A list of (source, dest) tuples to be copied to the output directory. Yields: A path to a temporary directory, named "_default_profile". This directory will be cleaned up when this context exits. """ with tempfile_ext.NamedTemporaryDirectory() as tempdir: # TODO(csharrison): "_default_profile" was chosen because this directory # will be pushed to the device's sdcard. We don't want to choose a # random name due to the extra failure mode of filling up the sdcard # in the case of unclean test teardown. We should consider changing # PushProfile to avoid writing to this intermediate location. host_profile = os.path.join(tempdir, '_default_profile') if profile_dir: shutil.copytree(profile_dir, host_profile) else: os.mkdir(host_profile) # Add files from |profile_files_to_copy| into the host profile # directory. Don't copy files if they already exist. for source, dest in profile_files_to_copy: host_path = os.path.join(host_profile, dest) if not os.path.exists(host_path): file_util.CopyFileWithIntermediateDirectories( source, host_path) yield host_profile
def SetUpEnvironment(self, browser_options): super(PossibleDesktopBrowser, self).SetUpEnvironment(browser_options) if self._browser_options.dont_override_profile: return # If given, this directory's contents will be used to seed the profile. source_profile = self._browser_options.profile_dir if source_profile and self._is_content_shell: raise RuntimeError('Profiles cannot be used with content shell') self._profile_directory = tempfile.mkdtemp() if source_profile: logging.info('Seeding profile directory from: %s', source_profile) # copytree requires the directory to not exist, so just delete the empty # directory and re-create it. os.rmdir(self._profile_directory) shutil.copytree(source_profile, self._profile_directory) # When using an existing profile directory, we need to make sure to # delete the file containing the active DevTools port number. devtools_file_path = os.path.join( self._profile_directory, desktop_browser_backend.DEVTOOLS_ACTIVE_PORT_FILE) if os.path.isfile(devtools_file_path): os.remove(devtools_file_path) # Copy data into the profile if it hasn't already been added via # |source_profile|. for source, dest in self._browser_options.profile_files_to_copy: full_dest_path = os.path.join(self._profile_directory, dest) if not os.path.exists(full_dest_path): file_util.CopyFileWithIntermediateDirectories(source, full_dest_path)
def testRaisesError(self): source_path = os.path.join(self._tempdir, 'source') with open(source_path, 'w') as f: f.write('data') dest_path = "" with self.assertRaises(OSError) as cm: file_util.CopyFileWithIntermediateDirectories( source_path, dest_path) self.assertEqual(errno.ENOENT, cm.exception.error_code)
def testCopyOverwrites(self): source_path = os.path.join(self._tempdir, 'source') with open(source_path, 'w') as f: f.write('source_data') dest_path = os.path.join(self._tempdir, 'dest') with open(dest_path, 'w') as f: f.write('existing_data') file_util.CopyFileWithIntermediateDirectories(source_path, dest_path) self.assertEqual('source_data', open(dest_path, 'r').read())
def testCopyMakeDirectories(self): source_path = os.path.join(self._tempdir, 'source') with open(source_path, 'w') as f: f.write('data') dest_path = os.path.join(self._tempdir, 'path', 'to', 'dest') self.assertFalse(os.path.exists(dest_path)) file_util.CopyFileWithIntermediateDirectories(source_path, dest_path) self.assertTrue(os.path.exists(dest_path)) self.assertEqual('data', open(dest_path, 'r').read())