SyncUtilJs.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import argparse
  4. import re
  5. import os
  6. import sys
  7. CUR_FILE_DIR = os.path.abspath(os.path.dirname(__file__));
  8. def open_file(filepath, mode, encoding):
  9. if sys.version_info.major == 3:
  10. return open(filepath, mode, encoding=encoding)
  11. else:
  12. import codecs
  13. return codecs.open(filepath, mode, encoding)
  14. class FileInsertHandler(object):
  15. def __init__(self, filepath):
  16. self.lines = []
  17. self.thrd_libs = {}
  18. self.mark_prefix = '//---SyncByPyScript---'
  19. self.has_changed = False
  20. with open_file(filepath, 'r', 'utf-8') as fp:
  21. self.lines = fp.readlines()
  22. fp.close()
  23. expression = self.mark_prefix + "([\w+]+)-(start|end)"
  24. for i in range(0,len(self.lines)):
  25. line = self.lines[i]
  26. m_ret = re.match(expression, line)
  27. if m_ret:
  28. name = m_ret.groups()[0]
  29. tail = m_ret.groups()[1]
  30. if name not in self.thrd_libs:
  31. self.thrd_libs[name] = {
  32. "name" : name,
  33. "start": -1,
  34. "end": -1,
  35. }
  36. if tail == 'start':
  37. self.thrd_libs[name]['start'] = i
  38. elif tail == 'end':
  39. self.thrd_libs[name]['end'] = i
  40. self.output_path = filepath
  41. def update(self, name, filepath):
  42. if name not in self.thrd_libs:
  43. return
  44. start_num = self.thrd_libs[name]['start']
  45. end_num = self.thrd_libs[name]['end']
  46. print('update lib [%s] between line num %d and % d' % (name, start_num, end_num))
  47. if end_num > start_num + 1:
  48. with open_file(filepath, 'r', 'utf-8') as fp:
  49. content = fp.read()
  50. fp.close()
  51. if content[-1] != '\n':
  52. content += '\n'
  53. self.lines[start_num+1] = content
  54. for num in range(start_num+2, end_num):
  55. self.lines[num] = ""
  56. self.has_changed = True
  57. def save_data_to(self, data, output_path):
  58. fp = open_file(output_path, 'a+', 'utf-8')
  59. fp.seek(0)
  60. fp.truncate()
  61. fp.write(data)
  62. fp.flush()
  63. fp.close()
  64. def save(self):
  65. if not self.has_changed:
  66. print('no lib update,no change')
  67. return
  68. data = ''.join(self.lines)
  69. self.save_data_to(data, self.output_path)
  70. def getJsLibs():
  71. dirpath = os.path.join(CUR_FILE_DIR, "util")
  72. retnames = []
  73. for path, dirnames, filenames in os.walk(dirpath):
  74. for filename in filenames:
  75. if filename.endswith('min.js'):
  76. name = filename.split('.')[0]
  77. retnames.append(name)
  78. return retnames
  79. def syncToAll(dirpath):
  80. handler_list = []
  81. for path, dirnames, filenames in os.walk(dirpath):
  82. if path.endswith('util'):
  83. continue
  84. for filename in filenames:
  85. if filename.endswith('.js'):
  86. filepath = os.path.join(path, filename)
  87. handler = FileInsertHandler(filepath)
  88. handler_list.append(handler)
  89. jslibs = getJsLibs()
  90. # print(jslibs)
  91. print('###############start sync###################')
  92. for handler in handler_list:
  93. print('filename:' + handler.output_path)
  94. for jsname in jslibs:
  95. filename = os.path.join(CUR_FILE_DIR, "util/" + jsname + ".min.js")
  96. handler.update(jsname, filename)
  97. handler.save()
  98. print('\n')
  99. def main():
  100. parser = argparse.ArgumentParser()
  101. parser.add_argument('-d', '--dir', help='javascript dir',
  102. required=False, default=CUR_FILE_DIR)
  103. args = parser.parse_args()
  104. syncToAll(args.dir)
  105. if __name__ == '__main__':
  106. main()