update-environment.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # A script to update the environment
  5. # Copyright (C) 2017, Suleyman POYRAZ (AquilaNipalensis)
  6. #
  7. # This program is free software; you can redistribute it and/or modify it
  8. # under the terms of the GNU General Public License as published by the
  9. # Free Software Foundation; either version 2 of the License, or (at your
  10. # option) any later version. Please read the COPYING file.
  11. #
  12. import os
  13. import sys
  14. import getopt
  15. header = "### This file is automatically generated by update-environment"
  16. header_note = """
  17. #
  18. # Please do not edit this file directly. If you want to change
  19. # anything, please take a look at the files in /etc/env.d
  20. # and read the Pardus initialization system documentation.
  21. #
  22. # Bu otomatik olarak oluşturulmuş bir dosyadır.
  23. # Lütfen bu dosyayı elle değiştirmeyin. Değiştirmek istediğiniz
  24. # şeyler varsa, /etc/env.d dizinindeki dosyalara ve Pardus
  25. # açılış sistemi belgesine bakın.
  26. #
  27. """
  28. specials = (
  29. "KDEDIRS",
  30. "PATH",
  31. "CLASSPATH",
  32. "MANPATH",
  33. "INFOPATH",
  34. "ROOTPATH",
  35. "CONFIG_PROTECT",
  36. "CONFIG_PROTECT_MASK",
  37. "PRELINK_PATH",
  38. "PRELINK_PATH_MASK",
  39. "PYTHONPATH",
  40. "ADA_INCLUDE_PATH",
  41. "ADA_OBJECTS_PATH",
  42. "PKG_CONFIG_PATH"
  43. )
  44. def read_env_d(envdir):
  45. d = {}
  46. paths = []
  47. for name in os.listdir(envdir):
  48. path = os.path.join(envdir, name)
  49. # skip dirs (.svn, .cvs, etc)
  50. if os.path.isdir(path):
  51. continue
  52. # skip backup and version control files
  53. if name.endswith("~") or name.endswith(".bak") or name.endswith(",v"):
  54. continue
  55. # skip pisi's config file backups
  56. # .oldconfig is obsolete, but checked anyway cause it may still exist at old systems
  57. if name.endswith(".oldconfig") or name.endswith(".newconfig"):
  58. continue
  59. paths.append(path)
  60. paths.sort()
  61. for path in paths:
  62. for line in file(path):
  63. if line == "" or line.startswith("#"):
  64. continue
  65. line = line.rstrip("\n")
  66. if "=" in line:
  67. key, value = line.split("=", 1)
  68. key = key.strip()
  69. value = value.strip()
  70. if value.startswith('"') or value.startswith("'"):
  71. value = value[1:-1]
  72. # Merge for special variables, override for others
  73. if key in specials:
  74. if key in d:
  75. d[key].extend(value.split(":"))
  76. else:
  77. d[key] = value.split(":")
  78. else:
  79. d[key] = value
  80. return d
  81. def generate_profile_env(envdict, format='export %s="%s"\n'):
  82. profile = ""
  83. keys = list(envdict.keys())
  84. keys.sort()
  85. for key in keys:
  86. tmp = envdict[key]
  87. if isinstance(tmp, list):
  88. tmp = ":".join(tmp)
  89. profile += format % (key, tmp)
  90. return header + header_note + profile
  91. def update_file(path, content):
  92. f = open(path, "w")
  93. f.write(content)
  94. f.close()
  95. def update_environment(prefix):
  96. join = os.path.join
  97. env = read_env_d(join(prefix, "etc/env.d"))
  98. update_file(join(prefix, "etc/profile.env"), generate_profile_env(env))
  99. update_file(join(prefix, "etc/csh.env"), generate_profile_env(env, 'setenv %s %s\n'))
  100. #
  101. # Command line driver
  102. #
  103. def usage():
  104. print("update-environment [--destdir <prefix>]")
  105. def main(argv):
  106. prefix = "/"
  107. try:
  108. opts, args = getopt.gnu_getopt(argv, "h", [ "help", "destdir=", "live" ])
  109. except getopt.GetoptError:
  110. usage()
  111. for o, a in opts:
  112. if o in ("-h", "--help"):
  113. usage()
  114. sys.exit(0)
  115. if o in ("--destdir"):
  116. prefix = a
  117. update_environment(prefix)
  118. if __name__ == "__main__":
  119. main(sys.argv[1:])