make_release.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import os
  2. import shutil
  3. import zipfile
  4. class ReleaseMaker:
  5. def __init__(self):
  6. self.script_dir = os.path.dirname(os.path.abspath(__file__))
  7. self.base_dir = os.path.join(self.script_dir, "client", "release")
  8. self.__ensure_directory_exists(self.base_dir)
  9. self.version = self.__find_latest_version()
  10. self.release_dir = None
  11. def __find_latest_version(self):
  12. versions = [dir_name for dir_name in os.listdir(self.base_dir) if
  13. os.path.isdir(os.path.join(self.base_dir, dir_name))]
  14. return max(versions, key=lambda v: list(map(int, v.split('.'))))
  15. @staticmethod
  16. def __ensure_directory_exists(dir_path):
  17. if not os.path.exists(dir_path):
  18. os.makedirs(dir_path)
  19. def __unzip_files(self):
  20. zip_dir = os.path.join(self.base_dir, self.version)
  21. self.__ensure_directory_exists(zip_dir)
  22. for item in os.listdir(zip_dir):
  23. if item.endswith('.zip'):
  24. with zipfile.ZipFile(os.path.join(zip_dir, item), 'r') as zip_ref:
  25. zip_ref.extractall(self.release_dir)
  26. def __copy_files(self):
  27. # Copy ./server/dist/server to ./release/PLC_SIM v{version}/server
  28. server_source = os.path.join(self.script_dir, "server", "dist", "server")
  29. server_dest = os.path.join(self.release_dir, "server")
  30. self.__ensure_directory_exists(server_dest)
  31. shutil.copytree(server_source, server_dest, dirs_exist_ok=True)
  32. # Copy ./source to ./release/PLC_SIM v{version}/server/source
  33. source_source = os.path.join(self.script_dir, "source")
  34. source_dest = os.path.join(self.release_dir, "server", "source")
  35. self.__ensure_directory_exists(source_dest)
  36. shutil.copytree(source_source, source_dest, dirs_exist_ok=True)
  37. # Copy README.md
  38. shutil.copy2(os.path.join(self.script_dir, "README.md"), self.release_dir)
  39. # Copy ./instrument_setting.ini
  40. shutil.copy2(os.path.join(self.script_dir, "instrument_setting.ini"), self.release_dir)
  41. # Copy everything from ./dist/launcher/ to ./release/PLC_SIM v{version}/
  42. launcher_source = os.path.join(self.script_dir, "dist", "launcher")
  43. for item in os.listdir(launcher_source):
  44. item_path = os.path.join(launcher_source, item)
  45. if os.path.isdir(item_path):
  46. shutil.copytree(item_path, os.path.join(self.release_dir, item), dirs_exist_ok=True)
  47. else:
  48. shutil.copy2(item_path, self.release_dir)
  49. def make_release(self):
  50. self.release_dir = os.path.join(self.script_dir, f"release/PLC_SIM v{self.version}")
  51. self.__ensure_directory_exists(self.release_dir)
  52. self.__unzip_files()
  53. self.__copy_files()
  54. # Create export directory
  55. export_dir = os.path.join(self.release_dir, "server", "export")
  56. self.__ensure_directory_exists(export_dir)
  57. print(f"Release for PLC_SIM v{self.version} has been successfully created!")
  58. if __name__ == "__main__":
  59. # Create an instance and run the release process
  60. maker = ReleaseMaker()
  61. maker.make_release()