You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
8.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3. """Youtube-dlg setup file.
  4. Examples:
  5. Windows::
  6. python setup.py py2exe
  7. Linux::
  8. python setup.py install
  9. Build source distribution::
  10. python setup.py sdist
  11. Build platform distribution::
  12. python setup.py bdist
  13. Build the translations::
  14. python setup.py build_trans
  15. Build with updates disabled::
  16. python setup.py build --no-updates
  17. Requirements:
  18. * GNU gettext utilities
  19. Notes:
  20. If you get 'TypeError: decoding Unicode is not supported' when you run
  21. py2exe then apply the following patch::
  22. http://sourceforge.net/p/py2exe/patches/28/
  23. Basic steps of the setup::
  24. * Run pre-build tasks
  25. * Call setup handler based on OS & options
  26. * Set up hicolor icons (if supported by platform)
  27. * Set up fallback pixmaps icon (if supported by platform)
  28. * Set up package level pixmaps icons (*.png)
  29. * Set up package level i18n files (*.mo)
  30. * Set up scripts (executables) (if supported by platform)
  31. * Run setup
  32. """
  33. from distutils import cmd, log
  34. from distutils.core import setup
  35. from distutils.command.build import build
  36. import os
  37. import sys
  38. import glob
  39. from shutil import copyfile
  40. from subprocess import call
  41. PY2EXE = len(sys.argv) >= 2 and sys.argv[1] == "py2exe"
  42. if PY2EXE:
  43. try:
  44. import py2exe
  45. except ImportError as error:
  46. print(error)
  47. sys.exit(1)
  48. from youtube_dl_gui import (
  49. __author__,
  50. __appname__,
  51. __contact__,
  52. __version__,
  53. __license__,
  54. __projecturl__,
  55. __description__,
  56. __packagename__,
  57. __descriptionfull__
  58. )
  59. # Setup can not handle unicode
  60. __packagename__ = str(__packagename__)
  61. def on_windows():
  62. """Returns True if OS is Windows."""
  63. return os.name == "nt"
  64. class BuildBin(cmd.Command):
  65. description = "build the youtube-dl-gui binary file"
  66. user_options = []
  67. def initialize_options(self):
  68. self.scripts_dir = None
  69. def finalize_options(self):
  70. self.scripts_dir = os.path.join("build", "_scripts")
  71. def run(self):
  72. if not os.path.exists(self.scripts_dir):
  73. os.makedirs(self.scripts_dir)
  74. copyfile(os.path.join(__packagename__, "__main__.py"),
  75. os.path.join(self.scripts_dir, "youtube-dl-gui"))
  76. class BuildTranslations(cmd.Command):
  77. description = "build the translation files"
  78. user_options = []
  79. def initialize_options(self):
  80. self.exec_name = None
  81. self.search_pattern = None
  82. def finalize_options(self):
  83. if on_windows():
  84. self.exec_name = "msgfmt.exe"
  85. else:
  86. self.exec_name = "msgfmt"
  87. self.search_pattern = os.path.join(__packagename__, "locale", "*", "LC_MESSAGES", "youtube_dl_gui.po")
  88. def run(self):
  89. for po_file in glob.glob(self.search_pattern):
  90. mo_file = po_file.replace(".po", ".mo")
  91. try:
  92. log.info("building MO file for '{}'".format(po_file))
  93. call([self.exec_name, "-o", mo_file, po_file])
  94. except OSError:
  95. log.error("could not locate file '{}', exiting...".format(self.exec_name))
  96. sys.exit(1)
  97. class Build(build):
  98. """Overwrite the default 'build' behaviour."""
  99. sub_commands = [
  100. ("build_bin", None),
  101. ("build_trans", None)
  102. ] + build.sub_commands
  103. build.user_options.append(("no-updates", None, "build with updates disabled"))
  104. def initialize_options(self):
  105. build.initialize_options(self)
  106. self.no_updates = None
  107. def run(self):
  108. build.run(self)
  109. if self.no_updates:
  110. self.__disable_updates()
  111. def __disable_updates(self):
  112. lib_dir = os.path.join(self.build_lib, __packagename__)
  113. target_file = "optionsmanager.py"
  114. # Options file should be available from previous build commands
  115. optionsfile = os.path.join(lib_dir, target_file)
  116. data = None
  117. with open(optionsfile, "r") as input_file:
  118. data = input_file.readlines()
  119. if data is None:
  120. log.error("building with updates disabled failed!")
  121. sys.exit(1)
  122. for index, line in enumerate(data):
  123. if "'disable_update': False" in line:
  124. log.info("disabling updates")
  125. data[index] = line.replace("False", "True")
  126. break
  127. with open(optionsfile, "w") as output_file:
  128. output_file.writelines(data)
  129. # Overwrite cmds
  130. cmdclass = {
  131. "build": Build,
  132. "build_bin": BuildBin,
  133. "build_trans": BuildTranslations
  134. }
  135. def linux_setup():
  136. scripts = []
  137. data_files = []
  138. package_data = {}
  139. # Add hicolor icons
  140. for path in glob.glob("youtube_dl_gui/data/icons/hicolor/*x*"):
  141. size = os.path.basename(path)
  142. dst = "share/icons/hicolor/{size}/apps".format(size=size)
  143. src = "{icon_path}/apps/youtube-dl-gui.png".format(icon_path=path)
  144. data_files.append((dst, [src]))
  145. # Add fallback icon, see issue #14
  146. data_files.append(
  147. ("share/pixmaps", ["youtube_dl_gui/data/pixmaps/youtube-dl-gui.png"])
  148. )
  149. # Add man page
  150. data_files.append(
  151. ("share/man/man1", ["youtube-dl-gui.1"])
  152. )
  153. # Add pixmaps icons (*.png) & i18n files
  154. package_data[__packagename__] = [
  155. "data/pixmaps/*.png",
  156. "locale/*/LC_MESSAGES/*.mo"
  157. ]
  158. # Add scripts
  159. scripts.append("build/_scripts/youtube-dl-gui")
  160. setup_params = {
  161. "scripts": scripts,
  162. "data_files": data_files,
  163. "package_data": package_data
  164. }
  165. return setup_params
  166. def windows_setup():
  167. def normal_setup():
  168. package_data = {}
  169. # Add pixmaps icons (*.png) & i18n files
  170. package_data[__packagename__] = [
  171. "data\\pixmaps\\*.png",
  172. "locale\\*\\LC_MESSAGES\\*.mo"
  173. ]
  174. setup_params = {
  175. "package_data": package_data
  176. }
  177. return setup_params
  178. def py2exe_setup():
  179. windows = []
  180. data_files = []
  181. # py2exe dependencies & options
  182. # TODO change directory for ffmpeg.exe & ffprobe.exe
  183. dependencies = [
  184. "C:\\Windows\\System32\\ffmpeg.exe",
  185. "C:\\Windows\\System32\\ffprobe.exe",
  186. "C:\\python27\\DLLs\\MSVCP90.dll"
  187. ]
  188. options = {
  189. "includes": ["wx.lib.pubsub.*",
  190. "wx.lib.pubsub.core.*",
  191. "wx.lib.pubsub.core.arg1.*"]
  192. }
  193. #############################################
  194. # Add py2exe deps & pixmaps icons (*.png)
  195. data_files.extend([
  196. ("", dependencies),
  197. ("data\\pixmaps", glob.glob("youtube_dl_gui\\data\\pixmaps\\*.png")),
  198. ])
  199. # We have to manually add the translation files since py2exe cant do it
  200. for lang in os.listdir("youtube_dl_gui\\locale"):
  201. dst = os.path.join("locale", lang, "LC_MESSAGES")
  202. src = os.path.join("youtube_dl_gui", dst, "youtube_dl_gui.mo")
  203. data_files.append((dst, [src]))
  204. # Add GUI executable details
  205. windows.append({
  206. "script": "build\\_scripts\\youtube-dl-gui",
  207. "icon_resources": [(0, "youtube_dl_gui\\data\\pixmaps\\youtube-dl-gui.ico")]
  208. })
  209. setup_params = {
  210. "windows": windows,
  211. "data_files": data_files,
  212. "options": {"py2exe": options}
  213. }
  214. return setup_params
  215. if PY2EXE:
  216. return py2exe_setup()
  217. return normal_setup()
  218. if on_windows():
  219. params = windows_setup()
  220. else:
  221. params = linux_setup()
  222. setup(
  223. author = __author__,
  224. name = __appname__,
  225. version = __version__,
  226. license = __license__,
  227. author_email = __contact__,
  228. url = __projecturl__,
  229. description = __description__,
  230. long_description = __descriptionfull__,
  231. packages = [__packagename__],
  232. cmdclass = cmdclass,
  233. **params
  234. )