download.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. from urllib.request import urlretrieve
  5. from zipfile import ZipFile
  6. def unzip(z, src, dst, binary=False):
  7. dst = os.path.join(dst, os.path.basename(src))
  8. with open(dst, 'wb') as f:
  9. f.write(z.read(src))
  10. if binary and sys.platform != 'win32':
  11. os.chmod(dst, 0o755)
  12. def resolveExecutableName(path):
  13. return path if sys.platform != 'win32' else path + '.exe'
  14. def resolvePath(path):
  15. return os.path.join(os.path.dirname(__file__), path)
  16. def resolvePlatformPath():
  17. path = resolvePath('../dist')
  18. if sys.platform == 'win32':
  19. return os.path.join(path, 'windows/tools')
  20. elif sys.platform == 'linux':
  21. return os.path.join(path, 'linux/bin')
  22. elif sys.platform == 'darwin':
  23. return os.path.join(path, 'macos/app/Contents/MacOS')
  24. def progress(blocknum, blocksize, totalsize):
  25. bytesdone = min(blocknum * blocksize, totalsize)
  26. percent = bytesdone * 1e2 / totalsize
  27. s = '\r%5.1f%% %*d / %d KB' % (percent, len(str(totalsize)), bytesdone / 1024, totalsize / 1024)
  28. sys.stderr.write(s)
  29. if bytesdone >= totalsize:
  30. sys.stderr.write('\n')
  31. # Prepare
  32. os.makedirs(resolvePath('../dist/all/tools'), exist_ok=True)
  33. os.makedirs(resolvePlatformPath(), exist_ok=True)
  34. # Download Apktool
  35. print('(1/3) Downloading Apktool...')
  36. urlretrieve('https://bitbucket.org/iBotPeaches/apktool/downloads/apktool_2.9.1.jar',
  37. resolvePath('../dist/all/tools/apktool.jar'), progress)
  38. # Download and unpack Android Build Tools
  39. print('(2/3) Downloading Android Build Tools...')
  40. buildToolsUrl = 'https://dl.google.com/android/repository/build-tools_r33.0.1-{}.zip'
  41. if sys.platform == 'win32':
  42. buildToolsUrl = buildToolsUrl.format('windows')
  43. elif sys.platform == 'linux':
  44. buildToolsUrl = buildToolsUrl.format('linux')
  45. elif sys.platform == 'darwin':
  46. buildToolsUrl = buildToolsUrl.format('macosx')
  47. urlretrieve(buildToolsUrl, 'build-tools.zip', progress)
  48. with ZipFile('build-tools.zip') as z:
  49. unzip(z, 'android-13/lib/apksigner.jar', resolvePath('../dist/all/tools/'))
  50. unzip(z, resolveExecutableName('android-13/zipalign'), resolvePlatformPath(), True)
  51. if sys.platform == 'win32':
  52. unzip(z, 'android-13/libwinpthread-1.dll', resolvePlatformPath())
  53. os.remove('build-tools.zip')
  54. # Download and unpack Android SDK Platform Tools
  55. print('(3/3) Downloading Android SDK Platform Tools...')
  56. platformToolsUrl = 'https://dl.google.com/android/repository/platform-tools-latest-{}.zip'
  57. if sys.platform == 'win32':
  58. platformToolsUrl = platformToolsUrl.format('windows')
  59. elif sys.platform == 'linux':
  60. platformToolsUrl = platformToolsUrl.format('linux')
  61. elif sys.platform == 'darwin':
  62. platformToolsUrl = platformToolsUrl.format('darwin')
  63. urlretrieve(platformToolsUrl, 'platform-tools.zip', progress)
  64. with ZipFile('platform-tools.zip') as z:
  65. unzip(z, resolveExecutableName('platform-tools/adb'), resolvePlatformPath(), True)
  66. if sys.platform == 'win32':
  67. unzip(z, 'platform-tools/AdbWinApi.dll', resolvePlatformPath())
  68. unzip(z, 'platform-tools/AdbWinUsbApi.dll', resolvePlatformPath())
  69. os.remove('platform-tools.zip')