pakhandler.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import re
  5. import piksemel
  6. import subprocess
  7. raise_install = []
  8. class BuildModuleError(Exception):
  9. pass
  10. def generate_initrd(kver):
  11. subprocess.call(["/usr/bin/mkinitcpio","-k","%s"% kver ,"-g","/boot/initramfs-%s-fallback.img"% kver,"-S","autodetect"])
  12. subprocess.call(["/usr/bin/mkinitcpio","-k","%s"% kver ,"-c","/etc/mkinitcpio.conf","-g","/boot/initramfs-%s.img"% kver])
  13. def get_kver():
  14. with open("/etc/kernel/kernel") as f:
  15. return f.readline().strip()
  16. def run_dkms(action, name, version, kver, arch):
  17. actions = {"build": ["build", "install"],
  18. "remove": ["uninstall", "remove"]}
  19. for action in actions[action]:
  20. os.system("PATH='/usr/sbin:/usr/bin:/sbin:/bin' dkms %s -m %s -v %s -k %s -a %s" % (action, name, version, kver, arch))
  21. if action == "install" and os.path.exists("/var/lib/dkms/%s/%s/build/make.log" % (name, version)):
  22. raise_install.append("check /var/lib/dkms/%s/%s/build/make.log" % (name, version))
  23. def check_dkms(metapath, filepath, action):
  24. if piksemel.parse(metapath).getTag("Package").getTagData("Name") == "kernel-module-headers":
  25. kver = get_kver()
  26. arch = piksemel.parse(metapath).getTag("Package").getTagData("Architecture").replace("_", "-")
  27. # rebuild if /usr/src/*/dkms.conf exists
  28. for d in os.walk("/usr/src").next()[1]:
  29. if os.path.isfile("/usr/src/%s/dkms.conf" % d):
  30. m = re.match(r"(?P<name>[^\/]+)-(?P<version>[^-]+)", d).groupdict()
  31. run_dkms(action, m["name"], m["version"], kver, arch)
  32. generate_initrd(kver)
  33. if raise_install:
  34. raise BuildModuleError("\n\t".join(raise_install))
  35. return
  36. parse = piksemel.parse(filepath)
  37. for fp in parse.tags("File"):
  38. path = fp.getTagData("Path")
  39. # build if package has /usr/src/*/dkms.conf
  40. if path.endswith("/dkms.conf") and path.startswith("usr/src/"):
  41. m = re.match(r".*\/(?P<name>[^\/]+)-(?P<version>[^-]+)\/[^\/]+", path).groupdict()
  42. kver = get_kver()
  43. arch = piksemel.parse(metapath).getTag("Package").getTagData("Architecture").replace("_", "-")
  44. run_dkms(action, m["name"], m["version"], kver, arch)
  45. if raise_install:
  46. raise BuildModuleError(raise_install.pop())
  47. generate_initrd(kver)
  48. return
  49. def setupPackage(metapath, filepath):
  50. check_dkms(metapath, filepath, action="build")
  51. def cleanupPackage(metapath, filepath):
  52. check_dkms(metapath, filepath, action="remove")
  53. def postCleanupPackage(metapath, filepath):
  54. pass