#! /usr/bin/python3 import shutil, os, io, sys, time from subprocess import call, run class MyLogger: """ Custom Logger, which can add a date information Parameters for the constructor: :param: t0 a date taken as origin of time. defaults to None :type : float """ def __init__(self, t0 = None): self.t0 = t0 return def log(self, msg, end="\n"): """ Logging a message :param: msg the message :type : str :param: end some string to append (default to newline) :type : str """ # eventually prepend a time indication if self.t0 is not None: t = f"{int(time.time() - self.t0):5d} s: " if msg[0] == "\r": msg = "\r" + t + msg else: msg = t + msg print(msg, end=end) sys.stdout.flush() def initImg(isoFile, imgFile, size="14G"): """ Création d'une image de clé USB complète de taille , à partir d'un fichier ISO de nom ou .iso, vers un fichier nouveau ou -.img :param: isoFile nom de fichier ISO, terminé par .iso supposément :type : str :param: imgFile un nom pour l'image, terminé par .img supposément :type : str :param: size la taille, en Giga si le suffixe est G, ou Méga si le suffice est M, sinon directement en octets :type : str """ if not isoFile.endswith(".iso"): isoFile += ".iso" if not imgFile.endswith(".img"): imgFile = f"{imgFile}-{size}.img" isize = None if size[-1] == "G": isize = 1E9 * int(size[:-1]) elif size[-1] == "M": isize = 1E6 * int(size[:-1]) else: isize = int(size) ISOsize = os.path.getsize(isoFile) log = MyLogger(time.time()) cmd = f"dd if={isoFile} of={imgFile} bs=4M oflag=dsync status=progress" log.log(f"Copying the ISO file : {cmd}") call(cmd, shell=True) toseek = ISOsize // (4*1024*1024) + 1 count = int((isize - ISOsize) // (4*1024*1024)) cmd = f"dd if=/dev/zero of={imgFile} seek={toseek} bs=4M count={count} oflag=dsync status=progress" log.log(f"padding with zeros : {cmd}") call(cmd, shell=True) log.log("Finished") return def usage(): print(f"""\ USAGE: {sys.argv[0]} ISOfile IMGfile write ISO file to IMGfile, then pads with zeros to grow the file length of IMGfile up to 14G.""") return if __name__ == "__main__": try: initImg(sys.argv[1], sys.argv[2]) except Exception as e: print(e) usage()