Esempio n. 1
0
def parse_device_sizeinfo(line: str) -> Dict:
    if string_is_empty(line):
        return None
    p = line.find("bytes")
    c = find_char_backwards(line, ",", len(line) - p)
    fsize = int(strip(line[(p - c):p]))
    return {"size_b": fsize, "size_h": bytes_to_unit(fsize, True)}
Esempio n. 2
0
	def _download_file(
			self,
			indentationlevel: int,
			sftp: paramiko.SFTPClient,
			remote_root: Path,
			localfile: Path,
			remote_filenode: Path,
			entry: BackupEntry
	):
		options = entry.get_options()
		has_options = is_sequence_with_any_elements(options)
		is_simulation = False

		if has_options is True:
			is_simulation = options["simulate"] if "simulate" in options and options["simulate"] is True else False

		indentation = repeat("\t", indentationlevel+1)

		if not is_simulation:
			stat_remote = sftp.lstat(str(remote_filenode))

		do_transfer = True

		self.info("{}Processing file: '{}'".format(indentation, remote_filenode))

		if has_options and not is_simulation:
			do_transfer = self._check_file_with_options(
				options,
				remote_root,
				localfile,
				remote_filenode,
				stat_remote,
				indentation
			)

		if do_transfer is not None:
			self.info("{}Excluding '{}' due to json-file-option {}".format(
				indentation, remote_filenode, do_transfer
			))
		else:
			if is_simulation:
				self.info("{}Simulating download of file '{}'".format(indentation, remote_filenode))
			else:
				self.info("{}Downloading file (Total: {})".format(
					indentation, bytes_to_unit(stat_remote.st_size, 1, True, False))
				)

				self._current_progress_divider = get_filesize_progress_divider(stat_remote.st_size)

				sftp.get(str(remote_filenode), str(localfile))

				if self._copystats:
					self.info("{}Copying file modification dates".format(indentation))
					utime(str(localfile), (stat_remote.st_atime, stat_remote.st_mtime))
Esempio n. 3
0
def parse_partition_line(line: str, isboottype: bool) -> Dict:

    if string_is_empty(line):
        return None

    columns = {}
    columncount = 7 if isboottype else 6
    startedgap = False
    lastcolumn = False
    columncollect = ""
    collect = False
    cc = 0

    for c in line:
        if c == ' ' and lastcolumn is False:
            if startedgap is False:
                collect = False
                if isboottype:
                    if cc == 0 or cc == 4 or cc == 6:
                        collect = True
                else:
                    if cc == 0 or cc == 4 or cc == 5:
                        collect = True
                if collect is True:
                    if cc == 0:
                        columns["dev"] = columncollect
                    elif cc == 4:
                        partbytes = fdisk_size_to_bytesize(columncollect)
                        columns["size_b"] = partbytes
                        columns["size_h"] = bytes_to_unit(partbytes, True)

                startedgap = True
                cc += 1
                columncollect = ""
        else:
            if cc == columncount - 1:
                lastcolumn = True
            if startedgap is True:
                startedgap = False
            columncollect += c
    columns["type"] = columncollect
    return columns
 def get_target_image_sizehuman(self) -> str:
     return bytes_to_unit(self.get_target_image_sizebytes(), True, True,
                          False)
    def start_dd(self,
                 interactive: bool = False,
                 ddbatchsize: str = None,
                 finished_handler: Callable = None) -> int:
        retcode = None
        devpath = str(self._devicepath.absolute())
        imagepath = str(self._imagepath.absolute())

        param_if = "if={}".format(devpath.replace(" ", "\\ "))
        param_of = "of={}".format(imagepath.replace(" ", "\\ "))
        param_status = "status=progress"

        # todo: make configurable batch size
        if ddbatchsize is not None:
            param_bs = "bs={}".format(ddbatchsize)
        else:
            param_bs = "bs=1M"

        if interactive is True:
            yn = "y"
        else:
            print(
                ConsoleColor.colorline(
                    "Will execute \"{}\"".format("sudo dd {} {} {} {}".format(
                        param_if, param_of, param_status, param_bs)),
                    ConsoleColors.OKGREEN))

            yn = singlecharinput(
                "Confirm the creation of image '{}' from device '{}' (y/n)!".
                format(imagepath, devpath), ConsoleColors.OKGREEN)

        if yn == "n":
            if interactive is True:
                print(
                    ConsoleColor.colorline(
                        "Cancelled image creation on your wish",
                        ConsoleColors.OKBLUE))
            return -1
        elif yn == "y":
            sudo = local["sudo"]
            dd = local["dd"]

            starttime = datetime.now()

            p = sudo[dd[param_if, param_of, param_status,
                        param_bs]].popen(stderr=PIPE)
            line = ''
            # retcode = 0

            # Intention is not to get the first "\r" from dd-output
            # sleep(1)
            if interactive is True:
                print()

            target_image_sizehuman = self.get_target_image_sizehuman()
            target_image_sizebytes = self.get_target_image_sizebytes()

            if interactive is False:
                p._proc.communicate()
            else:
                while True:
                    retcode = p.poll()
                    if retcode is None:
                        out = p.stderr.read(1).decode("utf-8",
                                                      errors="replace")
                        if out == '':
                            break
                        else:
                            if out == '\r':
                                line = strip(line)
                                if not string_is_empty(line):
                                    dd_info = parse_dd_info(line)
                                    currenttime = datetime.now()
                                    deltatime = currenttime - starttime

                                    print(
                                        "{0:10}{1:4}{2:9}{3:3}{4:10}{5:4}{6:8}{7:3}{8}"
                                        .format(
                                            bytes_to_unit(
                                                dd_info["size_b"], True, True,
                                                False), "of",
                                            target_image_sizehuman, "|",
                                            dd_info["time_h"], "of",
                                            humantime(
                                                predict_job_time(
                                                    dd_info["time"],
                                                    target_image_sizebytes,
                                                    dd_info["size_b"])), "|",
                                            "Total time: {}".format(
                                                humantime(deltatime.
                                                          total_seconds()))),
                                        end="\r")

                                line = ''
                            else:
                                line = line + out
                    else:
                        break

            retcode = retcode if retcode is not None else p._proc.returncode

            if interactive is True:
                print()

                currenttime = datetime.now()
                deltatime = currenttime - starttime
                print(
                    ConsoleColor.colorline(
                        "Total time: {}".format(
                            humantime(deltatime.total_seconds())),
                        ConsoleColors.OKGREEN))

                st = stat(imagepath)
                print(
                    ConsoleColor.colorline(
                        "Final image size: {}".format(
                            bytes_to_unit(st.st_size, True, True, False)),
                        ConsoleColors.OKGREEN))
                if retcode == 0:
                    print(
                        ConsoleColor.colorline("Successfully created image!",
                                               ConsoleColors.OKGREEN))
                else:
                    print(
                        ConsoleColor.colorline(
                            "No Result from dd (image might be ok)!",
                            ConsoleColors.WARNING))
            finished_handler(retcode, imagepath)
            return retcode