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# Finds the local leanback library version based on leanback-v17/maven-metadata.xml
24def lookup_local_library_version(repo_path):
25  leanback_maven_metadata_path = repo_path + "/out/host/gradle/frameworks/support/build/support_repo/com/android/support/leanback-v17/maven-metadata.xml"
26  if not os.path.exists(leanback_maven_metadata_path):
27    print "ERROR: Missing leanback-v17 library {} in local repo".format(leanback_maven_metadata_path)
28
29  file = open(leanback_maven_metadata_path, "r")
30  for line in file:
31    matchObj = re.match(r'\s*<version>(.*)</version>', line)
32    if matchObj:
33      return matchObj.group(1).strip(' \t\n\r')
34
35# Get repo path
36current_path = os.getcwd()
37index = current_path.find("frameworks/support/samples/SupportLeanbackShowcase")
38if index < 0:
39  print "ERROR: Invalid repo {0}".format(current_path)
40  exit(0)
41
42repo_path = current_path[:index]
43support_frameworks_path = repo_path + "/frameworks/support"
44if not (os.path.isdir(repo_path) or os.path.isdir(support_frameworks_path)):
45  print 'ERROR : Repo "{0}" does not exist'.format(repo_path)
46  print 'Please run gradlew uploadArchives inside frameworks/support'
47  exit(0)
48
49# Substitute LIBRARY_VERSION/LOCAL_REPO in local.properties
50library_version = lookup_local_library_version(repo_path)
51replace(os.getcwd()+"/local.properties", r'(.*)LOCAL_REPO(.*)', 'LOCAL_REPO='+repo_path)
52replace(os.getcwd()+"/local.properties", r'(.*)LIBRARY_VERSION(.*)', 'LIBRARY_VERSION='+library_version)
53
54# Build
55print "Building SupportLeanbackShowcase app..."
56subprocess.call(["./gradlew", "assembleDebug"])
57
58#Install apk
59print "Installing SupportLeanbackShowcase..."
60subprocess.call(["adb", "install", "-r", "./app/build/outputs/apk/app-debug.apk"])
61
62print "Finished installing SupportLeanbackShowcase app."
63
64