1# BUILDING SupportLeanbackShowcase app using local library.
2import sys
3import subprocess
4import os
5import fileinput
6import re
7
8# Does an inplace substitution of the pattern with newVal in inputFile
9def replace(inputFile, pattern, newVal, ):
10  print 'About to replace repo path to {0} in {1}'.format(newVal, inputFile)
11  replaced = False
12  if os.path.exists(inputFile):
13    for line in fileinput.input(inputFile, inplace = 1):
14      if re.match(pattern, line, re.I|re.M):
15        line = re.sub(pattern, newVal, line)
16        replaced = True
17      print line,
18
19  if not replaced:
20    file = open(inputFile, "a")
21    file.write(newVal + "\n")
22
23# Substitute LIBRARY_VERSION/LOCAL_REPO in local.properties
24# It will use default values in build.gradle
25replace(os.getcwd()+"/local.properties", r'(.*)LOCAL_REPO(.*)', 'LOCAL_REPO=')
26replace(os.getcwd()+"/local.properties", r'(.*)LIBRARY_VERSION(.*)', 'LIBRARY_VERSION=')
27
28# Build
29print "Building SupportLeanbackShowcase app..."
30subprocess.call(["./gradlew", "assembleDebug"])
31
32#Install apk
33print "Installing SupportLeanbackShowcase..."
34subprocess.call(["adb", "install", "-r", "./app/build/outputs/apk/app-debug.apk"])
35
36print "Finished installing SupportLeanbackShowcase app."
37
38