ModuleObj.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import os, sys
  4. import time
  5. import re
  6. import string
  7. from utility import version
  8. from utility.util import log
  9. from utility.util import LogLevel
  10. class ModuleObj:
  11. _chip_id = ''
  12. _gen_path = ''
  13. def __init__(self, name1, name2):
  14. self.__hName = name1
  15. self.__dtsiName = name2
  16. self.__data = {}
  17. def get_hFileName(self):
  18. return self.__hName
  19. def get_dtsiFileName(self):
  20. return self.__dtsiName
  21. def get_cfgInfo(self, section):
  22. pass
  23. def set_data(self, key, value):
  24. self.__data[key] = value
  25. def get_data(self):
  26. return self.__data
  27. def gen_files(self):
  28. self.gen_hFile()
  29. self.gen_dtsiFile()
  30. def gen_hFile(self):
  31. fp = open(os.path.join(ModuleObj.get_genPath(), ModuleObj.get_hFileName(self)), 'w')
  32. gen_str = ''
  33. gen_str += ModuleObj.writeComment()
  34. gen_str += ModuleObj.writeHeader(ModuleObj.get_hFileName(self))
  35. gen_str += self.fill_hFile()
  36. gen_str += ModuleObj.writeTail(ModuleObj.get_hFileName(self))
  37. fp.write(gen_str)
  38. fp.close()
  39. def gen_dtsiFile(self):
  40. fp = open(os.path.join(ModuleObj.get_genPath(), ModuleObj.get_dtsiFileName(self)), 'w')
  41. gen_str = ''
  42. gen_str = ModuleObj.writeComment()
  43. gen_str += ModuleObj.writeHeader(ModuleObj.get_dtsiFileName(self))
  44. gen_str += self.fill_dtsiFile()
  45. fp.write(gen_str)
  46. fp.close()
  47. def gen_spec(self, para):
  48. if re.match(r'.*_h$', para):
  49. self.gen_hFile()
  50. elif re.match(r'.*_dtsi', para):
  51. self.gen_dtsiFile()
  52. elif re.match(r'.*_c', para):
  53. self.gen_cFile()
  54. @staticmethod
  55. def get_figPath():
  56. figPath = os.path.join(sys.path[0], 'config', ModuleObj.get_chipId() + '.fig')
  57. if not os.path.exists(figPath) or not os.path.isfile(figPath):
  58. log(LogLevel.error, 'Can not find %s.fig file!' %(ModuleObj.get_chipId()))
  59. sys.exit(-1)
  60. return figPath
  61. @staticmethod
  62. def get_cmpPath():
  63. cmpPath = os.path.join(sys.path[0], 'config', 'YuSu.cmp')
  64. if not os.path.exists(cmpPath) or not os.path.isfile(cmpPath):
  65. log(LogLevel.error, 'Can not find YuSu.cmp file!')
  66. sys.exit(-1)
  67. return cmpPath
  68. @staticmethod
  69. def get_chipId():
  70. return ModuleObj._chip_id
  71. @staticmethod
  72. def set_chipId(id):
  73. ModuleObj._chip_id = id
  74. @staticmethod
  75. def set_genPath(path):
  76. ModuleObj._gen_path = path
  77. @staticmethod
  78. def get_genPath():
  79. return ModuleObj._gen_path
  80. @staticmethod
  81. def writeComment():
  82. stamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  83. ver_info = version.VER_MAIN + '.' + version.VER_SUB + '.' + version.BUILD_SN
  84. gen_str = '''/*\n * Generated by MTK SP DrvGen Version: '''
  85. gen_str += ver_info
  86. gen_str += ''' for '''
  87. gen_str += ModuleObj.get_chipId()
  88. gen_str += '''.\n'''
  89. gen_str += ''' * '''
  90. gen_str += stamp
  91. gen_str += '''\n * Do Not Modify The File.\n'''
  92. gen_str += ''' * Copyright Mediatek Inc. (c) 2016.\n*/\n\n'''
  93. return gen_str
  94. @staticmethod
  95. def writeHeader(name):
  96. str = ''
  97. if re.match(r'.*\.h$', name):
  98. name = string.replace(name, '.', '_')
  99. name = string.replace(name, '-', '_')
  100. str += '''#ifndef __%s\n''' %(name.upper())
  101. str += '''#define __%s\n''' %(name.upper())
  102. str += '''\n'''
  103. elif re.match(r'.*\.dtsi$', name):
  104. str += '''/*************************\n'''
  105. str += ''' * %s File\n''' %(name.replace('cust_', '').replace('.', ' ').upper())
  106. str += '''*************************/\n\n'''
  107. return str
  108. @staticmethod
  109. def writeTail(name):
  110. if re.match(r'.*\.h$', name):
  111. gen_str = '''\n\n#endif /* %s */\n''' %('__' + string.replace(name, '.', '_').upper())
  112. return gen_str