1#!/usr/bin/env python 2# 3# Copyright 2016 Google Inc. 4# 5# Use of this source code is governed by a BSD-style license that can be 6# found in the LICENSE file. 7 8 9"""Create the asset.""" 10 11 12import argparse 13import os 14import platform 15import shutil 16import subprocess 17import sys 18 19 20NDK_VER = "android-ndk-r26b" 21NDK_URL = \ 22 "https://dl.google.com/android/repository/%s-darwin.dmg" % NDK_VER 23DMG = "ndk.dmg" 24MOUNTED_NAME_START = '/Volumes/Android NDK' 25 26def find_ndk(volume): 27 """Find the NDK within the mounted volume.""" 28 for f in os.listdir(volume): 29 if f.endswith('.app'): 30 return os.path.join(volume, f, 'Contents/NDK') 31 32def create_asset(target_dir): 33 """Create the asset.""" 34 if platform.system() != 'Darwin': 35 print("This script can only be run on a Mac!") 36 sys.exit(1) 37 38 subprocess.check_call(["curl", NDK_URL, "-o", DMG]) 39 output = subprocess.check_output(['hdiutil', 'attach', DMG]) 40 41 # hdiutil mounted the DMG somewhere - find where it was mounted. 42 lines = output.decode('utf-8').split('\n') 43 found = False 44 for line in lines: 45 words = line.split('\t') 46 if len(words) == 3: 47 if words[2].startswith(MOUNTED_NAME_START): 48 found = True 49 50 # copytree (in python2, and by default in python3) requires that the 51 # dst does not exist. Remove it so that is the case. 52 if os.path.isdir(target_dir): 53 os.rmdir(target_dir) 54 55 shutil.copytree(find_ndk(words[2]), target_dir) 56 57 # Unmount the volume, now that we're done with it. 58 subprocess.check_call(['hdiutil', 'detach', words[0].strip()]) 59 60 subprocess.check_call(["rm", DMG]) 61 break 62 63 if not found: 64 print("Could not find mount point! Output from hdiutil attach:") 65 for line in lines: 66 print(line) 67 sys.exit(2) 68 69def main(): 70 parser = argparse.ArgumentParser() 71 parser.add_argument('--target_dir', '-t', required=True) 72 args = parser.parse_args() 73 create_asset(args.target_dir) 74 75 76if __name__ == '__main__': 77 main() 78