ogclone-engine/client/shared/scripts/initCache.py

95 lines
2.7 KiB
Python

import sys
import time
import subprocess
import SystemLib
import FileSystemLib
import CacheLib
import DiskLib
#!/usr/bin/env python3
def main():
TIME1 = time.time()
PROG = sys.argv[0]
EXECFORMAT = f"{PROG} [int_ndisk [int_npart]] {{-1 | 0 | int_size}} [NOMOUNT]"
args = sys.argv[1:]
if args and args[-1].upper() == "NOMOUNT":
MOUNT = 0
args = args[:-1]
else:
MOUNT = 1
PARAMS = len(args)
if PARAMS == 1:
NDISK = 1
NPART = 4
SIZE = int(args[0])
elif PARAMS == 2:
NDISK = int(args[0])
NPART = 4
SIZE = int(args[1])
elif PARAMS == 3:
NDISK = int(args[0])
NPART = int(args[1])
SIZE = int(args[2])
else:
SystemLib.ogRaiseError("OG_ERR_FORMAT", f"{EXECFORMAT}")
if NDISK < 1 or NPART < 1:
SystemLib.ogRaiseError("OG_ERR_FORMAT", f"{EXECFORMAT}")
if SIZE < -1:
SystemLib.ogRaiseError("OG_ERR_FORMAT", f"{EXECFORMAT}")
if SIZE == 0:
print("No modificar la caché local.")
return
if SIZE == -1:
print("[10] Trabajar sin caché local.")
CacheLib.ogUnmountCache()
CacheLib.ogDeleteCache()
else:
current_cache = CacheLib.ogFindCache()
if current_cache and f"{NDISK} {NPART}" != current_cache:
print("[10] Detectada otra caché, eliminarla")
CacheLib.ogUnmountCache()
CacheLib.ogDeleteCache()
try:
OLDSIZE = CacheLib.ogGetCacheSize()
except ValueError:
OLDSIZE = 0
if SIZE <= 0:
SystemLib.ogRaiseError("OG_ERR_FORMAT", f"!({SIZE}>0)")
if SIZE != OLDSIZE:
print("[10] Crear partición de caché local.")
CacheLib.ogUnmountCache()
CacheLib.ogCreateCache(NDISK, NPART, SIZE)
DiskLib.ogUpdatePartitionTable(NDISK)
cache = CacheLib.ogFindCache()
if not FileSystemLib.ogIsFormated(cache) or SIZE != OLDSIZE:
print("[50] Formatear caché local.")
CacheLib.ogFormatCache()
print("[70] Comprobar montaje de caché local.")
if CacheLib.ogMountCache() != 0:
print("[80] Comprobar consistencia y volver a montar caché local.")
FileSystem.ogCheckFs(cache)
if CacheLib.ogMountCache() != 0:
sys.exit(1)
if MOUNT == 0:
print("[90] Dejar desmontada la caché local.")
CacheLib.ogUnmountCache()
TIME = time.time() - TIME1
print(f"[100] Duración de la operación {int(TIME // 60)}m {int(TIME % 60)}s")
if __name__ == "__main__":
main()