build.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # -*-coding:utf-8 -*-
  2. import shutil, os
  3. import time
  4. inspurPath = os.path.abspath('.') + '\inspur-web'
  5. inspurInnerPath = os.path.abspath('.') + '\inspur-web\inspur-web'
  6. inspurZipPath = os.path.abspath('.') + '\inspur-web.zip'
  7. distPath = os.path.abspath('.') + '\dist'
  8. zipPath = 'D:\Project\Zip'
  9. def deleteFile(isDelete):
  10. if os.path.exists(distPath):
  11. shutil.rmtree(distPath)
  12. if os.path.exists(inspurPath):
  13. shutil.rmtree(inspurPath)
  14. if isDelete:
  15. if os.path.exists(inspurZipPath):
  16. os.remove(inspurZipPath)
  17. def buildModel():
  18. rootPath = os.path.abspath('.')
  19. print('########### run build ############')
  20. # 打包命令
  21. cmd = 'npm run build:prod'
  22. # 切换到需要项目目录
  23. os.chdir(rootPath)
  24. # 当前项目目录下执行打包命令
  25. if os.system(cmd) == 0:
  26. # 打包完成
  27. print('########### build complete ############')
  28. # 判断文件是否存在
  29. def isExitFile(pathString):
  30. if os.path.exists(pathString):
  31. return True;
  32. else:
  33. return False;
  34. # 创建文件夹
  35. def isCreateFile(pathStrring):
  36. if isExitFile(pathStrring) == False:
  37. os.makedirs(pathStrring)
  38. def moveFile():
  39. if os.path.exists(distPath):
  40. isCreateFile(inspurInnerPath)
  41. if isExitFile(distPath):
  42. for filename in os.listdir(distPath):
  43. if filename.endswith('static'):
  44. shutil.copytree(os.path.join(distPath, filename), os.path.join(inspurPath, filename))
  45. elif filename == 'html':
  46. shutil.copytree(os.path.join(distPath, filename), os.path.join(inspurInnerPath, filename))
  47. else:
  48. shutil.copy(os.path.join(distPath, filename), os.path.join(inspurInnerPath, filename))
  49. print('*************zip*************')
  50. zipName = time.strftime("%Y-%m-%d-%H%M%S")
  51. shutil.make_archive(zipPath + '\inspur-web'+ zipName, 'zip', inspurPath);
  52. deleteFile(True)
  53. buildModel()
  54. moveFile()
  55. deleteFile(False)
  56. print('*************finished*************')