def _clear_directory(path, skip_list=()): """ Clear out the contents of the given directory. :param path: path of the directory to clear out :type path: str :param skip_list: list of files or directories to not remove :type skip_list: list or tuple """ misc.clear_directory(path, skip_list)
def process_main(self, item=None): """ Publish a directory from the repo to a target directory. """ # Use the timestamp as the name of the current master repository # directory. This allows us to identify when these were created as well # as having more than one side-by-side during the publishing process. timestamp_master_dir = os.path.join(self.master_publish_dir, self.parent.timestamp) # Given that it is timestamped for this publish/repo we could skip the copytree # for items where http & https are published to a separate directory _logger.debug('Copying tree from %s to %s' % (self.source_dir, timestamp_master_dir)) shutil.copytree(self.source_dir, timestamp_master_dir, symlinks=True) for source_relative_location, publish_location in self.publish_locations: if source_relative_location.startswith('/'): source_relative_location = source_relative_location[1::] timestamp_master_location = os.path.join(timestamp_master_dir, source_relative_location) timestamp_master_location = timestamp_master_location.rstrip('/') # Without the trailing '/' publish_location = publish_location.rstrip('/') # Create the parent directory of the published repository tree, if needed publish_dir_parent = os.path.dirname(publish_location) if not os.path.exists(publish_dir_parent): os.makedirs(publish_dir_parent, 0750) if not self.only_publish_directory_contents: # Create a temporary symlink in the parent of the published directory tree tmp_link_name = os.path.join(publish_dir_parent, self.parent.timestamp) os.symlink(timestamp_master_location, tmp_link_name) # Rename the symlink to the official published location name. # This has two desirable effects: # 1. it will overwrite an existing link, if it's there # 2. the operation is atomic, instantly changing the published directory # NOTE: it's not easy (possible?) to directly edit the target of a symlink os.rename(tmp_link_name, publish_location) else: if not os.path.exists(publish_location): os.makedirs(publish_location, 0750) for file_name in os.listdir(timestamp_master_location): tmp_link_name = os.path.join(publish_location, self.parent.timestamp) master_source_file = os.path.join(timestamp_master_location, file_name) os.symlink(master_source_file, tmp_link_name) final_name = os.path.join(publish_location, file_name) os.rename(tmp_link_name, final_name) # Clear out any previously published masters misc.clear_directory(self.master_publish_dir, skip_list=[self.parent.timestamp])
def test_clear_directory(self): for file_name in ('one', 'two', 'three'): touch(os.path.join(self.working_dir, file_name)) os.makedirs(os.path.join(self.working_dir, 'four')) self.assertEqual(len(os.listdir(self.working_dir)), 4) misc.clear_directory(self.working_dir, ['two']) self.assertEqual(len(os.listdir(self.working_dir)), 1)
def test_clear_directory(self): for file_name in ("one", "two", "three"): touch(os.path.join(self.working_dir, file_name)) os.makedirs(os.path.join(self.working_dir, "four")) self.assertEqual(len(os.listdir(self.working_dir)), 4) misc.clear_directory(self.working_dir, ["two"]) self.assertEqual(len(os.listdir(self.working_dir)), 1)
def test_clear_directory_that_does_not_exist(self): # If this doesn't throw we are ok misc.clear_directory(os.path.join(self.working_dir, 'imaginary'))
def process_main(self, item=None): """ Publish a directory from the repo to a target directory. """ # Use the timestamp as the name of the current master repository # directory. This allows us to identify when these were created as well # as having more than one side-by-side during the publishing process. timestamp_master_dir = os.path.join(self.master_publish_dir, self.parent.timestamp) # Given that it is timestamped for this publish/repo we could skip the copytree # for items where http & https are published to a separate directory _logger.debug('Copying tree from %s to %s' % (self.source_dir, timestamp_master_dir)) shutil.copytree(self.source_dir, timestamp_master_dir, symlinks=True) for source_relative_location, publish_location in self.publish_locations: if source_relative_location.startswith('/'): source_relative_location = source_relative_location[1::] timestamp_master_location = os.path.join(timestamp_master_dir, source_relative_location) timestamp_master_location = timestamp_master_location.rstrip('/') # Without the trailing '/' publish_location = publish_location.rstrip('/') # Create the parent directory of the published repository tree, if needed publish_dir_parent = os.path.dirname(publish_location) if not os.path.exists(publish_dir_parent): os.makedirs(publish_dir_parent, 0750) if not self.only_publish_directory_contents: # Create a temporary symlink in the parent of the published directory tree tmp_link_name = os.path.join(publish_dir_parent, self.parent.timestamp) os.symlink(timestamp_master_location, tmp_link_name) # Rename the symlink to the official published location name. # This has two desirable effects: # 1. it will overwrite an existing link, if it's there # 2. the operation is atomic, instantly changing the published directory # NOTE: it's not easy (possible?) to directly edit the target of a symlink os.rename(tmp_link_name, publish_location) else: if not os.path.exists(publish_location): os.makedirs(publish_location, 0750) for file_name in os.listdir(timestamp_master_location): tmp_link_name = os.path.join(publish_location, self.parent.timestamp) master_source_file = os.path.join( timestamp_master_location, file_name) os.symlink(master_source_file, tmp_link_name) final_name = os.path.join(publish_location, file_name) os.rename(tmp_link_name, final_name) # Clear out any previously published masters misc.clear_directory(self.master_publish_dir, skip_list=[self.parent.timestamp])
def test_clear_directory_that_does_not_exist(self): # If this doesn't throw we are ok misc.clear_directory(os.path.join(self.working_dir, "imaginary"))