Advertisement
DeaD_EyE

CR1203 Image creator

Nov 3rd, 2022 (edited)
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.27 KB | None | 0 0
  1. """
  2. # Schritte
  3. 1. Programm starten
  4. 2. Nach dem einhängen die Dateien kopieren
  5. 3. Enter, damit das Image ausgehangen wird und danach wird die swu-Datei erstellt
  6.  
  7. ## Schriftarten
  8.  * [Noto Sans   ](https://fonts.google.com/noto/specimen/Noto+Sans)
  9.  * [Noto Sans SC](https://fonts.google.com/noto/specimen/Noto+Sans+SC)
  10.  * [Liberation  ](https://github.com/liberationfonts/liberation-fonts)
  11.  
  12.    LiberationMono-BoldItalic.ttf
  13.    LiberationMono-Bold.ttf
  14.    LiberationMono-Italic.ttf
  15.    LiberationMono-Regular.ttf
  16.    LiberationSans-BoldItalic.ttf
  17.    LiberationSans-Bold.ttf
  18.    LiberationSans-Italic.ttf
  19.    LiberationSans-Regular.ttf
  20.    LiberationSerif-BoldItalic.ttf
  21.    LiberationSerif-Bold.ttf
  22.    LiberationSerif-Italic.ttf
  23.    LiberationSerif-Regular.ttf
  24.    NotoSans-BlackItalic.ttf
  25.    NotoSans-Black.ttf
  26.    NotoSans-BoldItalic.ttf
  27.    NotoSans-Bold.ttf
  28.    NotoSans-ExtraBoldItalic.ttf
  29.    NotoSans-ExtraBold.ttf
  30.    NotoSans-ExtraLightItalic.ttf
  31.    NotoSans-ExtraLight.ttf
  32.    NotoSans-Italic.ttf
  33.    NotoSans-LightItalic.ttf
  34.    NotoSans-Light.ttf
  35.    NotoSans-MediumItalic.ttf
  36.    NotoSans-Medium.ttf
  37.    NotoSans-Regular.ttf
  38.    NotoSansSC-Black.otf
  39.    NotoSansSC-Bold.otf
  40.    NotoSansSC-Light.otf
  41.    NotoSansSC-Medium.otf
  42.    NotoSansSC-Regular.otf
  43.    NotoSansSC-Thin.otf
  44.    NotoSans-SemiBoldItalic.ttf
  45.    NotoSans-SemiBold.ttf
  46.    NotoSans-ThinItalic.ttf
  47.    NotoSans-Thin.ttf
  48.  
  49. ## Zu kopierende Dateien
  50.  
  51.  
  52.    /opt/CODESYSControl/CODESYSControl.cfg
  53.    /home/etc
  54.    /home/cds-apps
  55. """
  56.  
  57.  
  58. import subprocess
  59. import zipfile
  60. import gzip
  61. import shutil
  62. from argparse import ArgumentParser
  63. from pathlib import Path
  64.  
  65.  
  66. WORKING_DIR = Path("work")
  67. FILES = WORKING_DIR / "files.txt"
  68. ROOTFS = WORKING_DIR / "rootfs.ext4"
  69. ROOTFS_MOUNT = WORKING_DIR / "rootfs"
  70. NEW_SWU = Path("../cr1203_recovery.swu")
  71.  
  72.  
  73. def clean():
  74.     """
  75.    Remove the working directory
  76.    """
  77.     shutil.rmtree("work", ignore_errors=True)
  78.  
  79.  
  80. def extract_zip(file: str | Path) -> Path:
  81.     """
  82.    Extract the recovery zip file for os-installation of CR1203
  83.    The path to the .swu file is returned
  84.    """
  85.     with zipfile.ZipFile(file, "r") as zip_fd:
  86.         for zf in zip_fd.filelist:
  87.             if (swu := Path(zf.filename)).suffix == ".swu":
  88.                 zip_fd.extract(zf)
  89.                 swu.rename(swu.name)
  90.                 swu.parent.rmdir()
  91.                 break
  92.         else:
  93.             raise RuntimeError("Could not find swu file in archive")
  94.  
  95.     return Path(swu.name)
  96.  
  97.  
  98. def extract_swu(swu: str | Path) -> Path:
  99.     """
  100.    Extract .swu file to WORKING_DIR and
  101.    returns the pathobject pointing to the compressed
  102.    ext4 root file system
  103.    """
  104.     WORKING_DIR.mkdir(exist_ok=True)
  105.     swu.rename(WORKING_DIR / swu.name)
  106.     proc = subprocess.run(
  107.         ["cpio", "-iv", "--file", swu],
  108.         cwd=WORKING_DIR,
  109.         capture_output=True,
  110.         encoding="utf8",
  111.     )
  112.     files = proc.stderr.splitlines()[:-1]
  113.     with FILES.open("w") as fd_txt:
  114.         fd_txt.writelines("\n".join(files))
  115.         fd_txt.write("\n")
  116.  
  117.     for file in WORKING_DIR.glob("*.ext4.gz"):
  118.         return file
  119.     raise RuntimeError("Could not find the root filesystem.")
  120.  
  121.  
  122. def extract_rootfs(root_gz: str | Path) -> None:
  123.     with gzip.open(root_gz, "rb") as fd_in:
  124.         with ROOTFS.open("wb") as fd_out:
  125.             buffer = bytearray(4 * 1024**2)
  126.             view = memoryview(buffer)
  127.             while chunk_size := fd_in.readinto(view):
  128.                 fd_out.write(bytes(view[:chunk_size]))
  129.  
  130.  
  131. def mount() -> None:
  132.     ROOTFS_MOUNT.mkdir(exist_ok=True)
  133.     subprocess.run(["sudo", "mount", "-t", "ext4", ROOTFS, ROOTFS_MOUNT])
  134.  
  135.  
  136. def umount() -> None:
  137.     subprocess.run(["sudo", "umount", ROOTFS_MOUNT])
  138.  
  139.  
  140. def pack_rootfs(root_gz: str | Path) -> None:
  141.     """
  142.    Pack rootfs to root_gz
  143.    """
  144.     with ROOTFS.open("rb") as fd_in:
  145.         with gzip.open(root_gz, "wb") as fd_out:
  146.             buffer = bytearray(4 * 1024**2)
  147.             view = memoryview(buffer)
  148.             while chunk_size := fd_in.readinto(view):
  149.                 fd_out.write(view[:chunk_size])
  150.  
  151.     Path("work/rootfs.ext4").unlink()
  152.  
  153.  
  154. def pack_swu():
  155.     with open("work/files.txt") as fd:
  156.         subprocess.run(
  157.             ["cpio", "-o", "-H", "crc", "--file", NEW_SWU], stdin=fd, cwd="work"
  158.         )
  159.  
  160.     shutil.rmtree("work")
  161.  
  162.  
  163. def get_args():
  164.     parser = ArgumentParser()
  165.     parser.add_argument(
  166.         "package",
  167.         nargs="?",
  168.         help="Path to package",
  169.         default=Path.home().joinpath("Downloads/ifm_ecomatDisplay_Package_FW22012.zip"),
  170.     )
  171.     return parser.parse_args()
  172.  
  173.  
  174. def main():
  175.     args = get_args()
  176.     clean()
  177.  
  178.     print("Entpacke zip-Datei")
  179.     swu_file = extract_zip(args.package)
  180.  
  181.     print("Entpacke SWU-CPIO-Archiv")
  182.     rootfs_file = extract_swu(swu_file)
  183.  
  184.     print("Dekomprimiere root-Dateisystem")
  185.     extract_rootfs(rootfs_file)
  186.  
  187.     print("Root-Dateisystem wird eingehangen")
  188.     mount()
  189.  
  190.     input("Mit Eingabe fortfahren -> ")
  191.     print("Root-Dateisystem wird ausgehangen")
  192.     umount()
  193.  
  194.     print("Root-Dateisystem wird gepackt")
  195.     pack_rootfs(rootfs_file)
  196.  
  197.     print("SWU-Datei wird erstellt")
  198.     pack_swu()
  199.  
  200.     print("Fertig")
  201.  
  202.  
  203. if __name__ == "__main__":
  204.     main()
  205.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement