Example #1
0
    def open(self):
        """Open the TFRecords file.
		"""
        parent_path = os.path.dirname(self.path)
        mkdir(parent_path)

        # open the TFRecords file
        options = None
        if self.compression_type is not None:
            options = tf.python_io.TFRecordOptions(self.compression_type)
        self.writer = tf.python_io.TFRecordWriter(self.path, options=options)
Example #2
0
    def open(self):
        """Open the run and initialize all paths and configurations.

		Returns:
			Flag whether the run was successfully opened.
		"""
        # create a new run id
        if self.id is None:
            while True:
                id = "run_{}_{}".format(
                    datetime.now().strftime("%Y-%m-%d-%H-%M"), uuid.uuid4())
                path = get_full_path("training", id)
                if not os.path.isdir(path):
                    break
            self.id = id
            self.base_path = path

        # check whether the run with the initialized id exists
        else:
            base_path = get_full_path("training", self.id)
            if not os.path.isdir(base_path):
                base_path = get_full_path(self.id)
                if not os.path.isdir(base_path):
                    self.__open = False
                    return False
                base_path = base_path.rstrip("/")
                self.id = os.path.basename(base_path)
                base_path = get_full_path("training", self.id)
            self.id = self.id.rstrip("/")
            self.base_path = base_path

        # create paths
        self.config_file_path = os.path.join(self.base_path, "config.json")
        self.checkpoints_path = os.path.join(self.base_path, "checkpoints")
        self.checkpoints_file_path = os.path.join(self.checkpoints_path,
                                                  "checkpoints")

        mkdir(self.base_path)
        mkdir(self.checkpoints_path)

        # load or initialize configuration
        if os.path.isfile(self.config_file_path):
            with open(self.config_file_path, "r") as file:
                self.__config = json.load(file)
        else:
            self.__config = {}

        self.__open = True
        return True
Example #3
0
def extract_archive(archive_path, destination_path):
    """Extract an archive to a given destination directory.

	Arguments:
		archive_path: Path to the archive.
		destination_path: Destination directory where to extract the archive.
	"""
    archive_filename = os.path.basename(archive_path)

    # create temporary directory for extracting archive
    temp_filename = "{}_{}".format(archive_filename, str(uuid.uuid4()))
    temp_path = os.path.join(tempfile.gettempdir(), temp_filename)
    mkdir(temp_path)

    # extract archive
    if archive_filename.endswith(".zip"):
        archive_name = archive_filename[:-4]
        with zipfile.ZipFile(archive_path, "r") as file:
            file.extractall(temp_path)
    elif archive_filename.endswith(".tar"):
        archive_name = archive_filename[:-4]
        with tarfile.open(archive_path, "r:") as file:
            file.extractall(temp_path)
    elif archive_filename.endswith(".tar.bz2"):
        archive_name = archive_filename[:-8]
        with tarfile.open(archive_path, "r:bz2") as file:
            file.extractall(temp_path)
    elif archive_filename.endswith(".tar.gz"):
        archive_name = archive_filename[:-7]
        with tarfile.open(archive_path, "r:gz") as file:
            file.extractall(temp_path)
    else:
        raise NotImplementedError(
            "The type of the archive '{}' is currently not supported.".format(
                archive_filename))

    # build target path
    target_path = os.path.join(destination_path, archive_name)

    # move extracted files to destination
    extracted_items = os.listdir(temp_path)
    if len(extracted_items) == 1 and extracted_items[0] == archive_name:
        single_path = os.path.join(temp_path, archive_name)
        if os.path.isdir(single_path):
            shutil.move(single_path, target_path)
            return

    shutil.move(temp_path, target_path)