release.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import os
  2. import sys
  3. import platform
  4. import shutil
  5. import json
  6. import collections
  7. def join_path(root, subdir):
  8. return os.path.normpath(os.path.join(root, subdir))
  9. OS_NAME = platform.system()
  10. PRJ_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  11. OUTPUT_DIR = join_path(PRJ_DIR, 'release')
  12. BIN_DIR = join_path(PRJ_DIR, 'bin')
  13. DEMO_NAME = 'demo.exe' if OS_NAME == 'Windows' else 'demo'
  14. def read_file(filename):
  15. content = ''
  16. if sys.version_info >= (3, 0):
  17. with open(filename, 'r', encoding='utf8') as f:
  18. content = f.read()
  19. else:
  20. with open(filename, 'r') as f:
  21. content = f.read()
  22. return content
  23. def to_file_system_coding(s):
  24. if sys.version_info >= (3, 0): return s
  25. coding = sys.getfilesystemencoding()
  26. return s.encode(coding)
  27. def init_project_config():
  28. global CONFIG
  29. json_path = join_path(PRJ_DIR, 'project.json')
  30. if not os.path.exists(json_path):
  31. return
  32. content = read_file(json_path)
  33. CONFIG = json.loads(content, object_pairs_hook=collections.OrderedDict)
  34. def get_args(args, longsopts = []) :
  35. list_opts = []
  36. for arg in args:
  37. if arg.startswith('--') :
  38. tmp_opt = ''
  39. for opt in longsopts:
  40. if arg.find(opt) > 0 :
  41. tmp_opt = opt
  42. break
  43. if tmp_opt != '' :
  44. list_opts.append(arg.split(tmp_opt)[1])
  45. continue
  46. else :
  47. print(arg + " not find command, command :")
  48. print(longsopts)
  49. sys.exit()
  50. return list_opts
  51. def release():
  52. if not os.path.exists(OUTPUT_DIR):
  53. os.makedirs(OUTPUT_DIR)
  54. init_project_config()
  55. assets = CONFIG['assets']
  56. if 'outputDir' in assets:
  57. res_root = to_file_system_coding(assets['outputDir'])
  58. assets_root = join_path(PRJ_DIR, res_root + '/assets')
  59. copyExe()
  60. copyAssets(assets_root)
  61. cleanFiles()
  62. def copyExe():
  63. output_bin_dir = join_path(OUTPUT_DIR, 'bin')
  64. common.copyFile(BIN_DIR, DEMO_NAME, output_bin_dir, DEMO_NAME)
  65. common.copySharedLib(BIN_DIR, output_bin_dir)
  66. os.chmod(join_path(output_bin_dir, DEMO_NAME), 0o755)
  67. def copyAssets(assets_root):
  68. common.copyFiles(assets_root, '', OUTPUT_DIR, 'assets/')
  69. def cleanFiles():
  70. assets = CONFIG['assets']
  71. if 'themes' not in assets:
  72. return
  73. themes = assets['themes']
  74. for theme in themes:
  75. d = join_path(OUTPUT_DIR, 'assets/' + theme + '/inc')
  76. shutil.rmtree(d, True)
  77. LONGSOPTS = ['awtk_root=', 'AWTK_ROOT=']
  78. opts = get_args(sys.argv[1:], LONGSOPTS)
  79. ARGUMENTS = dict()
  80. if len(opts) > 0 :
  81. ARGUMENTS['AWTK_ROOT'] = opts[0]
  82. else :
  83. ARGUMENTS['AWTK_ROOT'] = ''
  84. import awtk_locator as locator
  85. locator.init(ARGUMENTS)
  86. import release_common as common
  87. release()