envtoconf.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/python
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one or more
  4. # contributor license agreements. See the NOTICE file distributed with
  5. # this work for additional information regarding copyright ownership.
  6. # The ASF licenses this file to You under the Apache License, Version 2.0
  7. # (the "License"); you may not use this file except in compliance with
  8. # the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. import os
  19. import re
  20. from shutil import copyfile
  21. import argparse
  22. import sys
  23. import transformation
  24. class Simple:
  25. def __init__(self, args):
  26. parser = argparse.ArgumentParser()
  27. parser.add_argument("--destination", help="Destination directory", required=True)
  28. self.args = parser.parse_args(args=args)
  29. # copy the default files to file.raw in desitnation directory
  30. self.known_formats = ['xml', 'properties', 'yaml', 'yml', 'env', "sh", "cfg", 'conf']
  31. self.output_dir = self.args.destination
  32. self.configurables = {}
  33. def destination_file_path(self, name, extension):
  34. return os.path.join(self.output_dir, "{}.{}".format(name, extension))
  35. def write_env_var(self, name, extension, key, value):
  36. with open(self.destination_file_path(name, extension) + ".raw", "a") as myfile:
  37. myfile.write("{}: {}\n".format(key, value))
  38. def process_envs(self):
  39. for key in os.environ.keys():
  40. p = re.compile("[_\\.]")
  41. parts = p.split(key)
  42. extension = None
  43. name = parts[0].lower()
  44. if len(parts) > 1:
  45. extension = parts[1].lower()
  46. config_key = key[len(name) + len(extension) + 2:].strip()
  47. if extension and "!" in extension:
  48. splitted = extension.split("!")
  49. extension = splitted[0]
  50. format = splitted[1]
  51. config_key = key[len(name) + len(extension) + len(format) + 3:].strip()
  52. else:
  53. format = extension
  54. if extension and extension in self.known_formats:
  55. if name not in self.configurables.keys():
  56. with open(self.destination_file_path(name, extension) + ".raw", "w") as myfile:
  57. myfile.write("")
  58. self.configurables[name] = (extension, format)
  59. self.write_env_var(name, extension, config_key, os.environ[key])
  60. else:
  61. for configurable_name in self.configurables.keys():
  62. if key.lower().startswith(configurable_name.lower()):
  63. self.write_env_var(configurable_name, self.configurables[configurable_name], key[len(configurable_name) + 1:], os.environ[key])
  64. def transform(self):
  65. for configurable_name in self.configurables.keys():
  66. name = configurable_name
  67. extension, format = self.configurables[name]
  68. destination_path = self.destination_file_path(name, extension)
  69. with open(destination_path + ".raw", "r") as myfile:
  70. content = myfile.read()
  71. transformer_func = getattr(transformation, "to_" + format)
  72. content = transformer_func(content)
  73. with open(destination_path, "w") as myfile:
  74. myfile.write(content)
  75. def main(self):
  76. # add the
  77. self.process_envs()
  78. # copy file.ext.raw to file.ext in the destination directory, and transform to the right format (eg. key: value ===> XML)
  79. self.transform()
  80. def main():
  81. Simple(sys.argv[1:]).main()
  82. if __name__ == '__main__':
  83. Simple(sys.argv[1:]).main()