1 | import subprocess |
---|
2 | import os |
---|
3 | import shutil |
---|
4 | |
---|
5 | def ogCalculateChecksum(*args): |
---|
6 | # Check if help is requested |
---|
7 | if "help" in args: |
---|
8 | print("ogCalculateChecksum [ str_repo | int_ndisk int_npartition ] path_filepath") |
---|
9 | print("ogCalculateChecksum REPO ubuntu.img ==> ef899299caf8b517ce36f1157a93d8bf") |
---|
10 | return |
---|
11 | |
---|
12 | # Get the file path |
---|
13 | file_path = ogGetPath(*args) |
---|
14 | if not file_path: |
---|
15 | ogRaiseError(OG_ERR_NOTFOUND, *args) |
---|
16 | return |
---|
17 | |
---|
18 | # Calculate the checksum |
---|
19 | result = subprocess.run(["tail", "-c1M", file_path], capture_output=True) |
---|
20 | checksum = result.stdout.decode().split()[0] |
---|
21 | |
---|
22 | return checksum |
---|
23 | |
---|
24 | def ogCompareChecksumFiles(*args): |
---|
25 | # Variables locales. |
---|
26 | ARGS = args |
---|
27 | if "help" in args: |
---|
28 | ogHelp("$FUNCNAME", "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath", "if $FUNCNAME REPO ubuntu.img CACHE ubuntu.img; then ...; fi") |
---|
29 | return |
---|
30 | |
---|
31 | ARGS = args |
---|
32 | if args[0].startswith("/"): |
---|
33 | # Camino completo. */ (Comentario Doxygen) |
---|
34 | SOURCE = ogGetPath(*args[:1]) |
---|
35 | args = args[1:] |
---|
36 | elif args[0].isdigit(): |
---|
37 | # ndisco npartición. |
---|
38 | SOURCE = ogGetPath(*args[:3]) |
---|
39 | args = args[3:] |
---|
40 | else: |
---|
41 | # Otros: repo, cache, cdrom (no se permiten caminos relativos). |
---|
42 | SOURCE = ogGetPath(*args[:2]) |
---|
43 | args = args[2:] |
---|
44 | |
---|
45 | TARGET = ogGetPath(*args) |
---|
46 | |
---|
47 | # Comparar los ficheros de checksum. |
---|
48 | return "$(cat "$SOURCE.sum" 2>/dev/null)" == "$(cat "$TARGET.sum" 2>/dev/null)" |
---|
49 | |
---|
50 | def ogCalculateFullChecksum(*args): |
---|
51 | # Variables locales. |
---|
52 | FILE = None |
---|
53 | if "help" in args: |
---|
54 | ogHelp("$FUNCNAME", "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath", "$FUNCNAME REPO ubuntu.img ==> ef899299caf8b517ce36f1157a93d8bf") |
---|
55 | return |
---|
56 | |
---|
57 | # Comprobar que existe el fichero y devolver sus datos. |
---|
58 | FILE = ogGetPath(*args) |
---|
59 | if not FILE: |
---|
60 | ogRaiseError(OG_ERR_NOTFOUND, *args) |
---|
61 | return |
---|
62 | |
---|
63 | # Calculate the checksum |
---|
64 | result = subprocess.run(["md5sum", FILE, "-b"], capture_output=True) |
---|
65 | checksum = result.stdout.decode().split()[0] |
---|
66 | |
---|
67 | return checksum |
---|
68 | |
---|
69 | def ogCopyFile(*args): |
---|
70 | # Variables locales. |
---|
71 | ARGS = args |
---|
72 | if "help" in args: |
---|
73 | ogHelp("$FUNCNAME", "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_source [ str_repo | int_ndisk int_npartition ] path_target", "$FUNCNAME REPO newfile.txt 1 2 /tmp/newfile.txt") |
---|
74 | return |
---|
75 | |
---|
76 | ARGS = args |
---|
77 | if args[0].startswith("/"): |
---|
78 | # Camino completo. */ (Comentrio Doxygen) |
---|
79 | SOURCE = ogGetPath(*args[:1]) |
---|
80 | args = args[1:] |
---|
81 | elif args[0].isdigit(): |
---|
82 | # ndisco npartición. |
---|
83 | SOURCE = ogGetPath(*args[:3]) |
---|
84 | args = args[3:] |
---|
85 | else: |
---|
86 | # Otros: repo, cache, cdrom (no se permiten caminos relativos). |
---|
87 | SOURCE = ogGetPath(*args[:2]) |
---|
88 | args = args[2:] |
---|
89 | |
---|
90 | TARGET = ogGetPath(*args) |
---|
91 | |
---|
92 | # Comprobar fichero origen y directorio destino. |
---|
93 | if not SOURCE: |
---|
94 | ogRaiseError(OG_ERR_NOTFOUND, *args[:-1]) |
---|
95 | return |
---|
96 | if not TARGET: |
---|
97 | ogRaiseError(OG_ERR_NOTFOUND, *args) |
---|
98 | return |
---|
99 | |
---|
100 | # Copiar fichero (para evitar problemas de comunicaciones las copias se hacen con rsync en vez de cp). |
---|
101 | result = subprocess.run(["rsync", "--progress", "--inplace", "-avh", SOURCE, TARGET]) |
---|
102 | return result.returncode |
---|
103 | |
---|
104 | def ogDeleteFile(*args): |
---|
105 | # Variables locales. |
---|
106 | FILE = None |
---|
107 | if "help" in args: |
---|
108 | ogHelp("$FUNCNAME", "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_file", "$FUNCNAME 1 2 /tmp/newfile.txt") |
---|
109 | return |
---|
110 | |
---|
111 | # Comprobar que existe el fichero y borrarlo. |
---|
112 | FILE = ogGetPath(*args) |
---|
113 | if not FILE: |
---|
114 | ogRaiseError(OG_ERR_NOTFOUND, *args) |
---|
115 | return |
---|
116 | try: |
---|
117 | os.remove(FILE) |
---|
118 | except OSError as e: |
---|
119 | ogRaiseError(OG_ERR_NOTFOUND, *args) |
---|
120 | return |
---|
121 | |
---|
122 | def ogDeleteTree(*args): |
---|
123 | # Variables locales. |
---|
124 | DIR = None |
---|
125 | if "help" in args: |
---|
126 | ogHelp("$FUNCNAME", "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_dir", "$FUNCNAME 1 2 /tmp/newdir") |
---|
127 | return |
---|
128 | |
---|
129 | # Comprobar que existe el directorio y borrarlo con su contenido. |
---|
130 | DIR = ogGetPath(*args) |
---|
131 | if not DIR: |
---|
132 | ogRaiseError(OG_ERR_NOTFOUND, *args) |
---|
133 | return |
---|
134 | try: |
---|
135 | shutil.rmtree(DIR) |
---|
136 | except OSError as e: |
---|
137 | ogRaiseError(OG_ERR_NOTFOUND, *args) |
---|
138 | return |
---|
139 | |
---|
140 | def ogGetPath(*args): |
---|
141 | # Variables locales. |
---|
142 | MNTDIR = None |
---|
143 | FILE = None |
---|
144 | PREVFILE = None |
---|
145 | FILEPATH = None |
---|
146 | CURRENTDIR = None |
---|
147 | |
---|
148 | # Si se solicita, mostrar ayuda. |
---|
149 | if "help" in args: |
---|
150 | ogHelp("$FUNCNAME", "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath", "$FUNCNAME \"/mnt/sda1/windows/system32\" ==> /mnt/sda1/WINDOWS/System32", "$FUNCNAME REPO /etc/fstab ==> /opt/opengnsys/images/etc/fstab", "$FUNCNAME 1 1 \"/windows/system32\" ==> /mnt/sda1/WINDOWS/System32") |
---|
151 | return |
---|
152 | |
---|
153 | # Procesar camino según el número de parámetros. |
---|
154 | if len(args) == 1: |
---|
155 | FILE = args[0] |
---|
156 | elif len(args) == 2: |
---|
157 | if args[0].upper() == "REPO": |
---|
158 | FILE = os.path.join(OGIMG, args[1]) |
---|
159 | elif args[0].upper() == "CACHE": |
---|
160 | MNTDIR = ogMountCache() |
---|
161 | if not MNTDIR: |
---|
162 | return |
---|
163 | FILE = os.path.join(MNTDIR, OGIMG, args[1]) |
---|
164 | elif args[0].upper() == "CDROM": |
---|
165 | MNTDIR = ogMountCdrom() |
---|
166 | if not MNTDIR: |
---|
167 | return |
---|
168 | FILE = os.path.join(MNTDIR, args[1]) |
---|
169 | else: |
---|
170 | ogRaiseError(OG_ERR_FORMAT) |
---|
171 | return |
---|
172 | elif len(args) == 3: |
---|
173 | MNTDIR = ogMount(args[0], args[1]) |
---|
174 | if not MNTDIR: |
---|
175 | return |
---|
176 | FILE = os.path.join(MNTDIR, args[2]) |
---|
177 | else: |
---|
178 | ogRaiseError(OG_ERR_FORMAT) |
---|
179 | return |
---|
180 | |
---|
181 | # Eliminar caracteres \c / duplicados y finales. |
---|
182 | FILE = os.path.normpath(FILE) |
---|
183 | |
---|
184 | # Comprobar si existe el fichero para reducir tiempos. |
---|
185 | if os.path.exists(FILE): |
---|
186 | FILEPATH = FILE |
---|
187 | else: |
---|
188 | # Buscar el nombre correcto en cada subdirectorio del camino. |
---|
189 | FILEPATH = "/" |
---|
190 | while FILE != PREVFILE: |
---|
191 | FILEPATH = os.path.join(FILEPATH.rstrip("/"), FILE.split("/")[0]) |
---|
192 | PREVFILE = FILE |
---|
193 | FILE = "/".join(FILE.split("/")[1:]) |
---|
194 | |
---|
195 | if FILEPATH: |
---|
196 | return FILEPATH |
---|
197 | else: |
---|
198 | return None |
---|
199 | |
---|
200 | def ogGetParentPath(*args): |
---|
201 | # Variables locales. |
---|
202 | PARENT = None |
---|
203 | if "help" in args: |
---|
204 | ogHelp("$FUNCNAME", "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath", "$FUNCNAME \"/mnt/sda1/windows/system32\" ==> /mnt/sda1/WINDOWS", "$FUNCNAME REPO /etc/fstab ==> /opt/opengnsys/images/etc", "$FUNCNAME 1 1 \"/windows/system32\" ==> /mnt/sda1/WINDOWS") |
---|
205 | return |
---|
206 | |
---|
207 | # Procesar camino según el número de parámetros. |
---|
208 | if len(args) == 1: |
---|
209 | PARENT = os.path.dirname(args[0]) |
---|
210 | elif len(args) == 2: |
---|
211 | PARENT = f"{args[0]} {os.path.dirname(f'/{args[1]}')}" |
---|
212 | elif len(args) == 3: |
---|
213 | PARENT = f"{args[0]} {args[1]} {os.path.dirname(f'/{args[2]}')}" |
---|
214 | else: |
---|
215 | ogRaiseError(OG_ERR_FORMAT) |
---|
216 | return |
---|
217 | |
---|
218 | return ogGetPath(PARENT) |
---|
219 | |
---|
220 | def ogIsNewerFile(*args): |
---|
221 | # Variables locales. |
---|
222 | ARGS = args |
---|
223 | if "help" in args: |
---|
224 | ogHelp("$FUNCNAME", "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_source [ str_repo | int_ndisk int_npartition ] path_target", "if $FUNCNAME REPO ubuntu.img CACHE ubuntu.img; then ... fi") |
---|
225 | return |
---|
226 | |
---|
227 | ARGS = args |
---|
228 | if args[0].startswith("/"): |
---|
229 | # Camino completo. */ (Comentrio Doxygen) |
---|
230 | SOURCE = ogGetPath(*args[:1]) |
---|
231 | args = args[1:] |
---|
232 | elif args[0].isdigit(): |
---|
233 | # ndisco npartición. |
---|
234 | SOURCE = ogGetPath(*args[:3]) |
---|
235 | args = args[3:] |
---|
236 | else: |
---|
237 | # Otros: repo, cache, cdrom (no se permiten caminos relativos). |
---|
238 | SOURCE = ogGetPath(*args[:2]) |
---|
239 | args = args[2:] |
---|
240 | |
---|
241 | TARGET = ogGetPath(*args) |
---|
242 | |
---|
243 | # Comprobar que existen los ficheros origen y destino. |
---|
244 | if not SOURCE: |
---|
245 | ogRaiseError(OG_ERR_NOTFOUND, *args[:-1]) |
---|
246 | return |
---|
247 | if not TARGET: |
---|
248 | ogRaiseError(OG_ERR_NOTFOUND, *args) |
---|
249 | return |
---|
250 | |
---|
251 | # Devolver si el primer fichero se ha modificado después que el segundo. |
---|
252 | return os.path.getmtime(SOURCE) > os.path.getmtime(TARGET) |
---|
253 | |
---|
254 | def ogMakeChecksumFile(*args): |
---|
255 | # Variables locales. |
---|
256 | FILE = None |
---|
257 | if "help" in args: |
---|
258 | ogHelp("$FUNCNAME", "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_filepath", "$FUNCNAME REPO ubuntu.img") |
---|
259 | return |
---|
260 | |
---|
261 | # Comprobar que existe el fichero y guardar su checksum. |
---|
262 | FILE = ogGetPath(*args) |
---|
263 | if not FILE: |
---|
264 | ogRaiseError(OG_ERR_NOTFOUND, *args) |
---|
265 | return |
---|
266 | checksum = ogCalculateChecksum(FILE) |
---|
267 | with open(f"{FILE}.sum", "w") as f: |
---|
268 | f.write(checksum) |
---|
269 | |
---|
270 | def ogMakeDir(*args): |
---|
271 | # Variables locales. |
---|
272 | PARENT = None |
---|
273 | DIR = None |
---|
274 | if "help" in args: |
---|
275 | ogHelp("$FUNCNAME", "$FUNCNAME [ str_repo | int_ndisk int_npartition ] path_dir", "$FUNCNAME 1 2 /tmp/newdir") |
---|
276 | return |
---|
277 | |
---|
278 | PARENT = ogGetParentPath(*args) |
---|
279 | if not PARENT: |
---|
280 | ogRaiseError(OG_ERR_NOTFOUND, *args) |
---|
281 | return |
---|
282 | |
---|
283 | DIR = os.path.basename(args[-1]) |
---|
284 | os.makedirs(os.path.join(PARENT, DIR), exist_ok=True) |
---|