package_SBC.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. # This script is used to build the uos app package
  2. # Usage:
  3. # Just modify the app_name, app_version, app_description, app_author, app_email etc.
  4. # Rename this file to package_your_app.py (optional)
  5. # Open command and input 'python3 package_your_app.py'
  6. # Then the deb file will create in ./bin directory
  7. # Warning:
  8. # Your computer should installed 'dh_make', 'python3' and 'linuxdeployqt'
  9. # If your app_path have directory lib, it won't exec linuxdeployqt~
  10. # Datetime: 2024/10/17
  11. import os
  12. import json
  13. import shutil
  14. import glob
  15. # define your app information
  16. class App(object):
  17. app_name = 'SafeBroadClient'
  18. app_chn_name = 'EQM安播客户端'
  19. app_id = 'com.hzlh.cas-sbc' # not support upper,just lowwer and digit!
  20. app_version = '1.0.1.0'
  21. app_description = '联汇国产化智慧播控系统(EQM)'
  22. app_author = 'wangfang'
  23. app_email = 'wangfang@hzlh.com'
  24. icon_size = '200x200' # support 16/24/32/48/128/256/512
  25. icon_name = 'logo.ico' # should put it in 'app_path/bin'
  26. app_path = '/SRC/' # your application path should be 'app_path/bin'!!!
  27. home_page = 'https://www.hzlh.com'
  28. depend_lib_path = '' # if needed, it'll add to Environment Path
  29. # global var
  30. ROOT_DIR = os.getcwd()
  31. CURRENT_USER = os.getenv("USER") or os.getenv("LOGNAME")
  32. #HOME_PATH = os.path.expanduser('~')
  33. def add_depend_libs_path():
  34. if App.depend_lib_path != '':
  35. os.system('export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:' + App.depend_lib_path)
  36. # switch to directory
  37. def switch_to_dir(dir_name):
  38. if not os.path.exists(dir_name):
  39. os.makedirs(dir_name)
  40. os.chdir(dir_name)
  41. # create target directory
  42. def create_target_dir(dir_name):
  43. target_dir = dir_name
  44. if not os.path.exists(target_dir):
  45. os.makedirs(target_dir)
  46. return target_dir
  47. # create a file
  48. def create_file(file_name):
  49. with open(file_name, 'w') as f:
  50. f.write('')
  51. # create your_app_id-6.0.0 directory
  52. # it always like ./bin/your_app_id-6.0.0
  53. def create_app_dir():
  54. app_dir = os.path.join(ROOT_DIR, 'bin', App.app_id + '-' + App.app_version)
  55. return create_target_dir(app_dir)
  56. # create a file nemed info
  57. def create_info_file():
  58. data = {
  59. "appid": App.app_id,
  60. "name": App.app_name,
  61. "version": App.app_version,
  62. "arch": ["amd64"],
  63. "permissions": {
  64. "autostart": False,
  65. "notification": False,
  66. "trayicon": False,
  67. "clipboard": True,
  68. "account": False,
  69. "bluetooth": False,
  70. "camera": False,
  71. "audio_record": False,
  72. "installed_apps": False
  73. }
  74. }
  75. with open('info', 'w') as json_file:
  76. json.dump(data, json_file, indent=4)
  77. # create a file named your_app_id.desktop
  78. # Icon=/opt/apps/{App.app_id}/files/bin/{App.app_name}.png
  79. def create_desktop_entry_file(file_name):
  80. content = f"""[Desktop Entry]
  81. Name={App.app_name}
  82. Name[zh_CN]={App.app_chn_name}
  83. Comment={App.app_description}
  84. Exec=/opt/apps/{App.app_id}/files/AppRun
  85. Icon=/opt/apps/{App.app_id}/files/bin/{App.icon_name}
  86. Terminal=false
  87. Categories=Utility
  88. Type=Application
  89. """
  90. with open(file_name, 'w') as file:
  91. file.write(content)
  92. def create_apprun_file():
  93. content = f"""#!/bin/bash
  94. SELF=$(readlink -f "$0")
  95. HERE=${{SELF%/*}}
  96. ARCH="x86_64-linux-gnu"
  97. #ARCH="aarch64-linux-gnu"
  98. export PATH="${{HERE}}/bin:${{PATH:+:$PATH}}:$PATH"
  99. export LD_LIBRARY_PATH="${{HERE}}/lib"
  100. export XDG_DATA_DIRS="${{HERE}}/share:/usr/share"
  101. exec {App.app_name} $*
  102. """
  103. with open('AppRun', 'w') as file:
  104. file.write(content)
  105. os.system('chmod 755 AppRun')
  106. def get_desktop_path():
  107. user_dirs_path = os.path.expanduser('~/.config/user-dirs.dirs')
  108. desktop_path = os.path.expanduser('~/Desktop') # default
  109. if os.path.exists(user_dirs_path):
  110. with open(user_dirs_path) as f:
  111. for line in f:
  112. if "XDG_DESKTOP_DIR" in line:
  113. desktop_path = line.split("=")[1].strip().strip('"')
  114. desktop_path = desktop_path.replace('$HOME', os.path.expanduser('~'))
  115. break
  116. return desktop_path
  117. def create_install_file():
  118. desktop_path = get_desktop_path()
  119. print('This user home path is ' + desktop_path)
  120. with open('install', 'w') as f:
  121. f.write(f'{App.app_id}/ /opt/apps\n')
  122. f.write(f'{App.app_id}/entries/applications/{App.app_id}.desktop /usr/share/applications\n')
  123. #f.write(f'{App.app_id}/entries/applications/{App.app_id}.desktop {desktop_path}\n')
  124. #f.write(f'{App.app_id}/files/lib/* /usr/lib\n')
  125. def modify_rules_file():
  126. # append "xxx" to the rules file
  127. with open('rules', 'a') as f:
  128. f.write("""
  129. override_dh_auto_build:
  130. override_dh_shlibdeps:
  131. --dpkg-shlibdeps-params=--ignore-missing-info
  132. override_dh_strip:""")
  133. def modify_control_file():
  134. with open('control', 'r+') as f:
  135. data = f.read()
  136. start_pos = data.find('Description:')
  137. if start_pos != -1:
  138. insert_pos = start_pos + len('Description:')
  139. new_text = data[:insert_pos] + App.app_description + '\n'
  140. else:
  141. new_text = data
  142. line_datas = new_text.split('\n')
  143. new_datas = []
  144. for line in line_datas:
  145. if line.startswith('Maintainer:'):
  146. new_datas.append(f'Maintainer:{App.app_author} {App.app_email}')
  147. elif line.startswith('Homepage:'):
  148. new_datas.append(f'Homepage:{App.home_page}')
  149. else:
  150. new_datas.append(line)
  151. f.seek(0)
  152. f.truncate()
  153. f.write('\n'.join(new_datas))
  154. def create_postinst_file():
  155. with open('postinst', 'w') as f:
  156. f.write(f'''
  157. #!/bin/bash
  158. set -e
  159. #chown -R {CURRENT_USER}:{CURRENT_USER} /opt/apps/{App.app_id}/files/bin || true
  160. chmod -R 777 /opt/apps/{App.app_id}/files/bin
  161. exit 0''')
  162. os.system('chmod 755 postinst')
  163. def is_bin_exists():
  164. if not os.path.exists('bin'):
  165. return False
  166. return True
  167. # copy ./bin/your_app to ./your_app
  168. def copy_file_to_target_dir(src_path, target_path):
  169. shutil.copy(src_path, target_path)
  170. def circulate_copy_to_target_dir(src_path, target_path):
  171. for file in glob.glob(src_path):
  172. des = target_path + '/' + file.split('/')[-1]
  173. shutil.copy(file, des)
  174. # remove all files below this directory
  175. def remove_files_in_current_dir(current_dir):
  176. for item in os.listdir(current_dir): # 遍历当前目录下的所有文件和目录
  177. item_path = os.path.join(current_dir, item) # 获取完整路径
  178. if os.path.isfile(item_path): # 如果是文件
  179. os.remove(item_path) # 删除文件
  180. def is_packaged():
  181. if os.path.exists('lib'):
  182. return True
  183. else:
  184. return False
  185. def package_app():
  186. command = f"linuxdeployqt ./bin/{App.app_name} -appimage"
  187. os.system(command)
  188. if __name__ == '__main__':
  189. print('Start to build uos app package...')
  190. add_depend_libs_path()
  191. app_bin_dir = ROOT_DIR + App.app_path
  192. switch_to_dir(app_bin_dir)
  193. if not is_bin_exists():
  194. print('The bin directory is not exists!')
  195. exit(1)
  196. if not is_packaged():
  197. package_app()
  198. switch_to_dir(ROOT_DIR)
  199. app_dir = create_app_dir()
  200. switch_to_dir(app_dir)
  201. create_target_dir(App.app_id)
  202. data_dir = app_dir + '/' + App.app_id
  203. switch_to_dir(data_dir)
  204. # create entries and files directory
  205. create_target_dir('entries')
  206. create_target_dir('files')
  207. # create a file named info
  208. create_info_file()
  209. entries_dir = data_dir + '/entries'
  210. switch_to_dir(entries_dir)
  211. create_target_dir('applications')
  212. app_desktop_file = 'applications/' + App.app_id + '.desktop'
  213. create_file(app_desktop_file)
  214. create_desktop_entry_file(app_desktop_file)
  215. # create icons directory
  216. icon_path = 'icons/hicolor/' + App.icon_size + '/apps'
  217. create_target_dir(icon_path)
  218. # copy the icon file to the icons directory
  219. app_path = ROOT_DIR + App.app_path
  220. icon_file = app_path + 'bin/' + App.icon_name
  221. os.system(f'cp {icon_file} {icon_path}/{App.app_id}.png')
  222. files_dir = data_dir + '/files'
  223. # copy the app directory to the files directory
  224. os.system(f'cp -r {app_path}/* {files_dir}')
  225. switch_to_dir(files_dir)
  226. create_apprun_file()
  227. switch_to_dir(app_dir)
  228. # make debian directory,just exec 'dh_make --createorig -s' or 'apt install dh-make' first
  229. # if command'dh_make' not found
  230. os.system('dh_make --createorig -s -y')
  231. # if command dh_make not found, you can exec 'apt install dh-make' to install it
  232. # create install file, just like 'touch install':
  233. # install file format
  234. # com.hzlh.cas10m/ /opt/apps
  235. # com.hzlh.cas10m/entries/applications/cas10m.desktop /usr/share/applications
  236. # //com.hzlh.cas10m/files/lib/ /usr/lib
  237. debian_dir = app_dir + '/debian'
  238. switch_to_dir(debian_dir)
  239. create_install_file()
  240. # just for log and config file
  241. create_postinst_file()
  242. # modify the control file, like depends, description, etc.
  243. modify_control_file()
  244. # modify changelog file, read all data then replace 'app_version-1' to 'app_version'
  245. with open('changelog', 'r+') as f:
  246. data = f.read()
  247. data = data.replace(App.app_version + '-1', App.app_version)
  248. f.seek(0)
  249. f.truncate()
  250. f.write(data)
  251. # remove ex or EX files, just exec 'rm *.ex *.EX'
  252. os.system('rm *.ex *.EX')
  253. # modify the rules file
  254. modify_rules_file()
  255. switch_to_dir(app_dir)
  256. # make deb package, just exec 'dpkg-buildpackage -tc -uc -us -b -d'
  257. os.system('dpkg-buildpackage -tc -uc -us -b -d')