Debian Codegraph @

live_clone/cloneToMem

raw
1 #! /usr/bin/python3
2
3 import shutil, os, io, sys, time
4 from subprocess import call, run
5
6 class MyLogger:
7 """
8 Custom Logger, which can add a date information
9 Parameters for the constructor:
10
11 :param: t0 a date taken as origin of time. defaults to None
12 :type : float
13 """
14
15 def __init__(self, t0 = None):
16 self.t0 = t0
17 return
18
19 def log(self, msg, end="\n"):
20 """
21 Logging a message
22 :param: msg the message
23 :type : str
24 :param: end some string to append (default to newline)
25 :type : str
26 """
27 # eventually prepend a time indication
28 if self.t0 is not None:
29 t = f"{int(time.time() - self.t0):5d} s: "
30 if msg[0] == "\r":
31 msg = "\r" + t + msg
32 else:
33 msg = t + msg
34 print(msg, end=end)
35 sys.stdout.flush()
36
37
38 def initImg(isoFile, imgFile, size="14G"):
39 """
40 Création d'une image de clé USB complète de taille <size>,
41 à partir d'un fichier ISO de nom <isoFile> ou <isoFile>.iso,
42 vers un fichier nouveau <imgFile> ou <imgFile>-<size>.img
43
44 :param: isoFile nom de fichier ISO, terminé par .iso supposément
45 :type : str
46 :param: imgFile un nom pour l'image, terminé par .img supposément
47 :type : str
48 :param: size la taille, en Giga si le suffixe est G, ou Méga si
49 le suffice est M, sinon directement en octets
50 :type : str
51 """
52 if not isoFile.endswith(".iso"):
53 isoFile += ".iso"
54 if not imgFile.endswith(".img"):
55 imgFile = f"{imgFile}-{size}.img"
56 isize = None
57 if size[-1] == "G":
58 isize = 1E9 * int(size[:-1])
59 elif size[-1] == "M":
60 isize = 1E6 * int(size[:-1])
61 else:
62 isize = int(size)
63 ISOsize = os.path.getsize(isoFile)
64 log = MyLogger(time.time())
65 cmd = f"dd if={isoFile} of={imgFile} bs=4M oflag=dsync status=progress"
66 log.log(f"Copying the ISO file : {cmd}")
67 call(cmd, shell=True)
68 toseek = ISOsize // (4*1024*1024) + 1
69 count = int((isize - ISOsize) // (4*1024*1024))
70 cmd = f"dd if=/dev/zero of={imgFile} seek={toseek} bs=4M count={count} oflag=dsync status=progress"
71 log.log(f"padding with zeros : {cmd}")
72 call(cmd, shell=True)
73 log.log("Finished")
74 return
75
76 def usage():
77 print(f"""\
78 USAGE:
79 {sys.argv[0]} ISOfile IMGfile
80 write ISO file to IMGfile, then pads with zeros to
81 grow the file length of IMGfile up to 14G.""")
82 return
83
84 if __name__ == "__main__":
85 try:
86 initImg(sys.argv[1], sys.argv[2])
87 except Exception as e:
88 print(e)
89 usage()