Blame view

ios/cocos2d/download-deps.py 14.5 KB
520389e3   xiaoyu   接入cocos源码,编译未通过,继续修改
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
  #!/usr/bin/env python
  #coding=utf-8
  #
  # ./download-deps.py
  #
  # Downloads Cocos2D-x 3rd party dependencies from github:
  # https://github.com/cocos2d/cocos2d-x-3rd-party-libs-bin) and extracts the zip
  # file
  #
  # Having the dependencies outside the official cocos2d-x repo helps prevent
  # bloating the repo.
  #
  
  """****************************************************************************
  Copyright (c) 2014 cocos2d-x.org
  Copyright (c) 2014-2017 Chukong Technologies Inc.
  
  http://www.cocos2d-x.org
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
  ****************************************************************************"""
  
  import os.path
  import zipfile
  import shutil
  import sys
  import traceback
  import distutils
  import fileinput
  import json
  
  from optparse import OptionParser
  from time import time
  from time import sleep
  from sys import stdout
  from distutils.errors import DistutilsError
  from distutils.dir_util import copy_tree, remove_tree
  
  
  def delete_folder_except(folder_path, excepts):
      """
      Delete a folder excepts some files/subfolders, `excepts` doesn't recursively which means it can not include
      `subfoler/file1`. `excepts` is an array.
      """
      for file in os.listdir(folder_path):
          if (file in excepts):
              continue
  
          full_path = os.path.join(folder_path, file)
          if os.path.isdir(full_path):
              shutil.rmtree(full_path)
          else:
              os.remove(full_path)
  
  
  class UnrecognizedFormat(Exception):
      def __init__(self, prompt):
          self._prompt = prompt
  
      def __str__(self):
          return self._prompt
  
  
  class CocosZipInstaller(object):
      def __init__(self, workpath, config_path, version_path, remote_version_key=None):
          self._workpath = workpath
          self._config_path = config_path
          self._version_path = version_path
  
          data = self.load_json_file(config_path)
  
          self._current_version = data["version"]
          self._repo_name = data["repo_name"]
          try:
              self._move_dirs = data["move_dirs"]
          except:
              self._move_dirs = None
          self._filename = self._current_version + '.zip'
          self._url = data["repo_parent"] + self._repo_name + '/archive/' + self._filename
          self._zip_file_size = int(data["zip_file_size"])
          # 'v' letter was swallowed by github, so we need to substring it from the 2nd letter
          if self._current_version[0] == 'v':
              self._extracted_folder_name = os.path.join(self._workpath, self._repo_name + '-' + self._current_version[1:])
          else:
              self._extracted_folder_name = os.path.join(self._workpath, self._repo_name + '-' + self._current_version)
  
          try:
              data = self.load_json_file(version_path)
              if remote_version_key is None:
                  self._remote_version = data["version"]
              else:
                  self._remote_version = data[remote_version_key]
          except:
              print("==> version file doesn't exist")
  
      def get_input_value(self, prompt):
          ret = raw_input(prompt)
          ret.rstrip(" \t")
          return ret
  
      def download_file(self):
          # remove file for retry
          try:
              os.remove(self._filename)
          except OSError:
              pass
          print("==> Ready to download '%s' from '%s'" % (self._filename, self._url))
          import urllib2
          try:
              u = urllib2.urlopen(self._url)
          except urllib2.HTTPError as e:
              if e.code == 404:
                  print("==> Error: Could not find the file from url: '%s'" % (self._url))
              print("==> Http request failed, error code: " + str(e.code) + ", reason: " + e.read())
              sys.exit(1)
  
          f = open(self._filename, 'wb')
          meta = u.info()
          content_len = meta.getheaders("Content-Length")
          file_size = 0
          if content_len and len(content_len) > 0:
              file_size = int(content_len[0])
          else:
              # github server may not reponse a header information which contains `Content-Length`,
              # therefore, the size needs to be written hardcode here. While server doesn't return
              # `Content-Length`, use it instead
              print("==> WARNING: Couldn't grab the file size from remote, use 'zip_file_size' section in '%s'" % self._config_path)
              file_size = self._zip_file_size
  
          print("==> Start to download, please wait ...")
  
          file_size_dl = 0
          block_sz = 8192
          block_size_per_second = 0
          old_time = time()
  
          status = ""
          while True:
              buffer = u.read(block_sz)
              if not buffer:
                  print("%s%s" % (" " * len(status), "\r")),
                  break
  
              file_size_dl += len(buffer)
              block_size_per_second += len(buffer)
              f.write(buffer)
              new_time = time()
              if (new_time - old_time) > 1:
                  speed = block_size_per_second / (new_time - old_time) / 1000.0
                  if file_size != 0:
                      percent = file_size_dl * 100. / file_size
                      status = r"Downloaded: %6dK / Total: %dK, Percent: %3.2f%%, Speed: %6.2f KB/S " % (file_size_dl / 1000, file_size / 1000, percent, speed)
                  else:
                      status = r"Downloaded: %6dK, Speed: %6.2f KB/S " % (file_size_dl / 1000, speed)
                  print(status),
                  sys.stdout.flush()
                  print("\r"),
                  block_size_per_second = 0
                  old_time = new_time
  
          print("==> Downloading finished!")
          f.close()
  
      def ensure_directory(self, target):
          if not os.path.exists(target):
              os.mkdir(target)
  
      def unpack_zipfile(self, extract_dir):
          """Unpack zip `filename` to `extract_dir`
  
          Raises ``UnrecognizedFormat`` if `filename` is not a zipfile (as determined
          by ``zipfile.is_zipfile()``).
          """
  
          if not zipfile.is_zipfile(self._filename):
              raise UnrecognizedFormat("%s is not a zip file" % (self._filename))
  
          print("==> Extracting files, please wait ...")
          z = zipfile.ZipFile(self._filename)
          try:
              for info in z.infolist():
                  name = info.filename
  
                  # don't extract absolute paths or ones with .. in them
                  if name.startswith('/') or '..' in name:
                      continue
  
                  target = os.path.join(extract_dir, *name.split('/'))
                  if not target:
                      continue
                  if name.endswith('/'):
                      # directory
                      self.ensure_directory(target)
                  else:
                      # file
                      data = z.read(info.filename)
                      f = open(target, 'wb')
                      try:
                          f.write(data)
                      finally:
                          f.close()
                          del data
                  unix_attributes = info.external_attr >> 16
                  if unix_attributes:
                      os.chmod(target, unix_attributes)
          finally:
              z.close()
              print("==> Extraction done!")
  
      def ask_to_delete_downloaded_zip_file(self):
          ret = self.get_input_value("==> Would you like to save '%s'? So you don't have to download it later. [Yes/no]: " % self._filename)
          ret = ret.strip()
          if ret != 'yes' and ret != 'y' and ret != 'no' and ret != 'n':
              print("==> Saving the dependency libraries by default")
              return False
          else:
              return True if ret == 'no' or ret == 'n' else False
  
      def download_zip_file(self):
          if not os.path.isfile(self._filename):
              self.download_file_with_retry(5, 3)
          try:
              if not zipfile.is_zipfile(self._filename):
                  raise UnrecognizedFormat("%s is not a zip file" % (self._filename))
          except UnrecognizedFormat as e:
              print("==> Unrecognized zip format from your local '%s' file!" % (self._filename))
              if os.path.isfile(self._filename):
                  os.remove(self._filename)
              print("==> Download it from internet again, please wait...")
              self.download_zip_file()
  
      def download_file_with_retry(self, times, delay):
          import urllib2
          times_count = 0
          while(times_count < times):
              times_count += 1
              try:
                  if(times_count > 1):
                      print("==> Download file retry " + str(times_count))
                  self.download_file()
                  return
              except Exception as err:
                  if(times_count >= times):
                      raise err
                  sleep(delay)
  
      def need_to_update(self):
          if not os.path.isfile(self._version_path):
              return True
  
          with open(self._version_path) as data_file:
              data = json.load(data_file)
  
          if self._remote_version == self._current_version:
              return False
          return True
  
      def load_json_file(self, file_path):
          if not os.path.isfile(file_path):
              raise Exception("Could not find (%s)" % (file_path))
  
          with open(file_path) as data_file:
              data = json.load(data_file)
          return data
  
      def clean_external_folder(self, external_folder):
          print('==> Cleaning cocos2d-x/external folder ...')
          # remove external except 'config.json'
          delete_folder_except(external_folder, ['config.json'])
  
      # rebuild link on linux
      def fix_fmod_link(self, extract_dir):
          import os
          import platform
          if platform.system() != "Linux":
              return
          print("==> Fix fmod link ... ")                        
          fmod_path = os.path.join(extract_dir, "linux-specific/fmod/prebuilt/64-bit")
          if os.path.exists(fmod_path):
              os.unlink(os.path.join(fmod_path, "libfmod.so.6"))
              os.unlink(os.path.join(fmod_path, "libfmodL.so.6"))
              os.symlink("libfmod.so", os.path.join(fmod_path, "libfmod.so.6"))
              os.symlink("libfmodL.so", os.path.join(fmod_path, "libfmodL.so.6"))
          else:
              print("==> fmod directory not found `%s`, failed to fix fmod link!"%fmod_path)
  
      def run(self, workpath, folder_for_extracting, remove_downloaded, force_update, download_only):
          if not force_update and not self.need_to_update():
              print("==> Not need to update!")
              return
  
          if os.path.exists(self._extracted_folder_name):
              shutil.rmtree(self._extracted_folder_name)
  
          self.download_zip_file()
  
          if not download_only:
              self.unpack_zipfile(self._workpath)
  
              if not os.path.exists(folder_for_extracting):
                  os.mkdir(folder_for_extracting)
  
              self.clean_external_folder(folder_for_extracting)
              print("==> Copying files...")
              distutils.dir_util.copy_tree(self._extracted_folder_name, folder_for_extracting)
              if self._move_dirs is not None:
                  for srcDir in self._move_dirs.keys():
                      distDir = os.path.join( os.path.join(workpath, self._move_dirs[srcDir]), srcDir)
                      if os.path.exists(distDir):
                          shutil.rmtree(distDir)
                      shutil.move( os.path.join(folder_for_extracting, srcDir), distDir)
              self.fix_fmod_link(folder_for_extracting)
              print("==> Cleaning...")
              if os.path.exists(self._extracted_folder_name):
                  shutil.rmtree(self._extracted_folder_name)
              if os.path.isfile(self._filename):
                  if remove_downloaded is not None:
                      if remove_downloaded == 'yes':
                          os.remove(self._filename)
                  elif self.ask_to_delete_downloaded_zip_file():
                      os.remove(self._filename)
          else:
              print("==> Download (%s) finish!" % self._filename)
  
  
  def _check_python_version():
      major_ver = sys.version_info[0]
      if major_ver > 2:
          print ("The python version is %d.%d. But python 2.x is required. (Version 2.7 is well tested)\n"
                 "Download it here: https://www.python.org/" % (major_ver, sys.version_info[1]))
          return False
  
      return True
  
  
  def main():
      workpath = os.path.dirname(os.path.realpath(__file__))
  
      if not _check_python_version():
          exit()
  
      parser = OptionParser()
      parser.add_option('-r', '--remove-download',
                        action="store", type="string", dest='remove_downloaded', default=None,
                        help="Whether to remove downloaded zip file, 'yes' or 'no'")
  
      parser.add_option("-f", "--force-update",
                        action="store_true", dest="force_update", default=False,
                        help="Whether to force update the third party libraries")
  
      parser.add_option("-d", "--download-only",
                        action="store_true", dest="download_only", default=False,
                        help="Only download zip file of the third party libraries, will not extract it")
  
      (opts, args) = parser.parse_args()
  
      print("=======================================================")
      print("==> Prepare to download external libraries!")
      external_path = os.path.join(workpath, 'external')
      installer = CocosZipInstaller(workpath, os.path.join(workpath, 'external', 'config.json'), os.path.join(workpath, 'external', 'version.json'), "prebuilt_libs_version")
      installer.run(workpath, external_path, opts.remove_downloaded, opts.force_update, opts.download_only)
  
  # -------------- main --------------
  if __name__ == '__main__':
      try:
          main()
      except Exception as e:
          traceback.print_exc()
          sys.exit(1)