foxpkg.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/python
  2. import os, argparse
  3. from datetime import datetime
  4. SPEC_DIR = "/home/luna/.foxpkg/catalyst/"
  5. ROOTS = "/home/luna/Documents/projects/xenia/"
  6. def init(developer):
  7. os.system(f"git clone https://gitlab.com/xenia-group/catalyst {SPEC_DIR}")
  8. input("Go to ~/.foxpkg/catalyst now and switch branch if needed, press enter when done: ")
  9. os.system(f"cd {SPEC_DIR} && git config user.name 'foxpkg' && git config user.email 'foxpkg@example.com'")
  10. os.system("sudo mkdir -p /var/tmp/catalyst/builds/default/")
  11. input("Download a stage3 tarball to /var/tmp/catalyst/builds/default/stage3-amd64-openrc-latest.tar.xz, and press enter: ")
  12. os.system(f"sudo ln -sf {SPEC_DIR}config /var/tmp/catalyst/")
  13. os.system(f"cd {SPEC_DIR} && git branch local && git checkout local")
  14. with open(os.path.expanduser(f"{SPEC_DIR}stage4-openrc.spec")) as specfile:
  15. contents = specfile.read()
  16. with open(os.path.expanduser(f"{SPEC_DIR}stage4-openrc.spec"), "w") as specfile:
  17. specfile.write(contents.replace("[CATALYST_DIR]", os.path.expanduser(SPEC_DIR)))
  18. os.system(f"cd {SPEC_DIR} && git add * && git commit -m 'foxpkg: Changed catalyst dir.'")
  19. update(False, developer)
  20. upgrade(developer=developer)
  21. def install_root(developer):
  22. os.system(f"sudo mount -o remount,rw {ROOTS}") if not developer else None
  23. os.system(f"sudo rm {ROOTS}root_temp.img")
  24. os.system(f"sudo tar2sqfs {ROOTS}root_temp.img < /var/tmp/catalyst/builds/default/stage4-amd64-openrc-\@TIMESTAMP\@.tar.xz")
  25. def update(instant, developer):
  26. specfile = open(os.path.expanduser(f"{SPEC_DIR}stage1-openrc.spec")).readlines()
  27. snapshot_date = [line for line in specfile if line.startswith("snapshot:")][0]
  28. old_snapshot = snapshot_date.split("snapshot: ")[1].strip()
  29. os.system("sudo emerge-webrsync")
  30. date = datetime.today().strftime("%Y.%m.%d")
  31. os.system(f"sudo catalyst -s {date}")
  32. for specfilename in [os.path.expanduser(f"{SPEC_DIR}{name}") for name in os.listdir(os.path.expanduser(SPEC_DIR)) if name.endswith(".spec")]:
  33. with open(specfilename, "r") as specfile:
  34. contents = specfile.read()
  35. with open(specfilename, "w") as specfile:
  36. specfile.write(contents.replace(old_snapshot, date))
  37. os.system(f"cd {SPEC_DIR} && git add * && git commit -m 'foxpkg: Changed timestamp in specfile.'")
  38. upgrade(developer=developer) if instant else None
  39. def upgrade_kernel():
  40. os.system("sudo mkdir /mnt/root")
  41. os.system("sudo mount /boot")
  42. os.system(f"sudo mount -o ro,loop -t squashfs {ROOTS}root_temp.img /mnt/root")
  43. os.system("sudo cp -r /mnt/root/boot/* /boot/")
  44. os.system("umount /mnt/root")
  45. os.system(f"sudo rm {ROOTS}root_b.img")
  46. os.system(f"sudo mv {ROOTS}root.img {ROOTS}root_b.img")
  47. os.system(f"sudo mv {ROOTS}root_temp.img {ROOTS}root.img")
  48. def upgrade(full=False, stage4=True, developer=False):
  49. if full:
  50. os.system(f"sudo catalyst -a -f {SPEC_DIR}stage1-openrc.spec")
  51. os.system(f"sudo catalyst -a -f {SPEC_DIR}stage3-openrc.spec")
  52. if stage4:
  53. os.system(f"sudo catalyst -a -f {SPEC_DIR}stage4-openrc.spec")
  54. input("Waiting: ")
  55. install_root(developer)
  56. upgrade_kernel() if not developer else None
  57. def edit(name, instant, developer):
  58. os.system(f"nano {SPEC_DIR}config/stages/{name}")
  59. os.system(f"cd {SPEC_DIR} && git add * && git commit -m 'foxpkg: Edited {name}.'")
  60. upgrade(developer=developer) if instant else None
  61. def revert(commit=None, instant=False, developer=False):
  62. os.system(f"cd {SPEC_DIR} && " + ("git reset --hard HEAD~1" if commit == None else f"git reset --hard {commit}"))
  63. upgrade(developer=developer) if instant else None
  64. def install(package, instant, developer):
  65. specfile = open(os.path.expanduser(f"{SPEC_DIR}stage4-openrc.spec")).readlines()
  66. start = specfile[:specfile.index("stage4/packages:\n")]
  67. packages = specfile[specfile.index("stage4/packages:\n"):specfile.index("stage4/rcadd:\n")]
  68. end = specfile[specfile.index("stage4/rcadd:\n"):]
  69. if "/" not in package:
  70. print("Error: Package not formatted properly.")
  71. else:
  72. if package in [pkg.strip("\n").strip("\t") for pkg in packages]:
  73. print("already in list")
  74. else:
  75. packages.append(f"\t{package}\n")
  76. new = start + packages + end
  77. with open(os.path.expanduser(f"{SPEC_DIR}stage4-openrc.spec"), "w") as specfile:
  78. for line in new:
  79. specfile.write(line)
  80. os.system(f"cd {SPEC_DIR} && git add * && git commit -m 'foxpkg: Added package {package}'")
  81. upgrade(developer=developer) if instant else None
  82. def parse():
  83. parser = argparse.ArgumentParser(
  84. prog = "foxpkg",
  85. description = "foxpkg enables the installation of packages and updates for Xenia Linux",
  86. epilog = "Thank you for being apart of Xenia Linux!"
  87. )
  88. parser.add_argument("-d", "--developer", action="store_true", help="Enables developer mode: Stores root images in ~/.foxpkg/output and doesn't install kernel")
  89. subparsers = parser.add_subparsers(dest="action", required=True)
  90. init = subparsers.add_parser("init", help="Initialises the catalyst directory and builds stage1 through stage3")
  91. update = subparsers.add_parser("update", help="Updates the system repositories and updates catalyst repositories")
  92. update.add_argument("-i", "--instant", action="store_true", help="Upgrades the system after this action.")
  93. upgrade = subparsers.add_parser("upgrade", help="Upgrades the stage4 to include any package changes or use flag changes.")
  94. upgrade.add_argument("-f", "--full", action="store_true", help="Also upgrades/rebuilds stage1 and stage3")
  95. install = subparsers.add_parser("install", help="Adds the specified package to the catalyst spec file.")
  96. install.add_argument("package")
  97. install.add_argument("-i", "--instant", action="store_true", help="Upgrades the system after this action.")
  98. edit = subparsers.add_parser("edit", help="Edits a specific file in the config directory. For example, foxpkg edit package.use/stage4.")
  99. edit.add_argument("file")
  100. edit.add_argument("-i", "--instant", action="store_true", help="Upgrades the system after this action.")
  101. revert = subparsers.add_parser("revert", help="Reverts the last commit, or the commit given after it. Note: This uses git reset, enter the commit you want to go back to.")
  102. revert.add_argument("commit", nargs="?", default=None)
  103. revert.add_argument("-i", "--instant", action="store_true", help="Upgrades the system after this action.")
  104. args = parser.parse_args()
  105. return args
  106. def main():
  107. global ROOTS
  108. arguments = parse()
  109. if arguments.developer:
  110. os.system("mkdir /home/luna/.foxpkg/output")
  111. ROOTS = "/home/luna/.foxpkg/output/"
  112. match arguments.action:
  113. case "init":
  114. init(arguments.developer)
  115. case "update":
  116. update(arguments.instant, arguments.developer)
  117. case "upgrade":
  118. upgrade(arguments.full, developer=arguments.developer)
  119. case "install":
  120. install(arguments.package, arguments.instant, arguments.developer)
  121. case "edit":
  122. edit(arguments.file, arguments.instant, arguments.developer)
  123. case "revert":
  124. revert(arguments.commit, arguments.instant, arguments.developer)
  125. if __name__ == "__main__":
  126. main()