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