make.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #! /usr/bin/env python3
  2. """Сборка всей программы
  3. """
  4. import os
  5. import sys
  6. import datetime
  7. import subprocess
  8. import shutil
  9. def help() -> None:
  10. """Получить справку по сборке
  11. """
  12. h: str = """
  13. Справка по сборке:
  14. ./make.py help - Справка по сборке
  15. ./make.py build - Сборка всей программы"""
  16. print(h)
  17. class Build:
  18. """Класс сборки
  19. """
  20. def __init__(self) -> None:
  21. self.date_build: str = str(datetime.datetime.now())
  22. """Дата сборки"""
  23. self.date_build = self.date_build.replace(" ", "_")
  24. cmd: list[str] = ['git', 'describe', '--tags', '--abbrev=0']
  25. output: bytes = subprocess.Popen(
  26. cmd, stdout=subprocess.PIPE).communicate()[0]
  27. tag: str = output.decode("utf-8")
  28. tag = tag[:-1]
  29. self.tag: str = tag
  30. """Тег из гита"""
  31. cmd = ['go', 'version']
  32. output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
  33. go_version: str = output.decode("utf-8")
  34. go_version = go_version[11:][:-1].replace(" ", "_")
  35. out: str = f"date={self.date_build}\t\t" +\
  36. f"tag={self.tag}\t\t" +\
  37. f"go_version={go_version}"
  38. print(out)
  39. self.go_version: str = go_version
  40. """Версия golang"""
  41. def run(self) -> None:
  42. """Запуск сборки"""
  43. print("cleaning")
  44. try:
  45. shutil.rmtree('./bin')
  46. except OSError:
  47. pass
  48. try:
  49. os.mkdir("bin")
  50. except FileExistsError:
  51. pass
  52. try:
  53. os.mkdir("bin/web")
  54. except FileExistsError:
  55. pass
  56. try:
  57. os.mkdir("bin/web/static")
  58. except FileExistsError:
  59. pass
  60. try:
  61. os.mkdir("bin/web/tmpl")
  62. except FileExistsError:
  63. pass
  64. print("copying")
  65. os.system('cp -r ../app_serv/web ./bin')
  66. os.chdir("../app_serv")
  67. print("format")
  68. os.system("go fmt ./...")
  69. print("build")
  70. cmd: str = """go build -ldflags "-w -s -X """ +\
  71. f"""main.GoVersion={self.go_version} -X """ +\
  72. """main.Version=$TAG -X """ +\
  73. """main.Date=$BUILD_DATE" -o """ +\
  74. '../app_work/bin/server.exe ./cmd/server/main.go'
  75. print(cmd)
  76. os.system(cmd)
  77. os.chdir("../app_work")
  78. print('stripping')
  79. os.system('strip -s ./bin/server.exe')
  80. print('packing exe')
  81. os.system('upx -f ./bin/server.exe')
  82. exeSize: int = os.path.getsize("./bin/server.exe")
  83. print(f'size exe={exeSize/1000**2:.2f} MB')
  84. class Dev:
  85. """Класс разработки
  86. """
  87. def __init__(self) -> None:
  88. pass
  89. def run(self) -> None:
  90. print("cleaning")
  91. try:
  92. shutil.rmtree('./bin')
  93. except OSError:
  94. pass
  95. try:
  96. os.mkdir("bin")
  97. except FileExistsError:
  98. pass
  99. try:
  100. os.mkdir("bin/web")
  101. except FileExistsError:
  102. pass
  103. try:
  104. os.mkdir("bin/web/static")
  105. except FileExistsError:
  106. pass
  107. try:
  108. os.mkdir("bin/web/tmpl")
  109. except FileExistsError:
  110. pass
  111. print("copying")
  112. os.system('cp -r ../app_serv/web ./bin')
  113. os.chdir("../app_serv")
  114. print("format")
  115. os.system("go fmt ./...")
  116. print("build")
  117. cmd: str = """go build -race """ +\
  118. '-o ../app_work/bin/server.exe ./cmd/server/main.go'
  119. print(cmd)
  120. os.system(cmd)
  121. os.chdir("../app_work/bin")
  122. os.unsetenv('STAGE')
  123. os.putenv('STAGE', 'local')
  124. os.system('./server.exe')
  125. if __name__ == '__main__':
  126. os.system('clear')
  127. args: list[str] = sys.argv
  128. if len(args) < 2:
  129. help()
  130. sys.exit(0)
  131. strCmd: str = args[1]
  132. match strCmd:
  133. case "help": # Получить справку по сборке
  134. help()
  135. case"build": # Сборка всей программы
  136. b: Build = Build()
  137. b.run()
  138. case "dev": # Разработка
  139. d: Dev = Dev()
  140. d.run()
  141. case "":
  142. help()
  143. case _: pass