Watch for system-image branch lmp-emu-dev

Change-Id: Ida787ee022723dc193c656b4e326567e9451f028
diff --git a/build/masters/master.client.adt/builders.pyl b/build/masters/master.client.adt/builders.pyl
index 8d4adda..dc00818 100644
--- a/build/masters/master.client.adt/builders.pyl
+++ b/build/masters/master.client.adt/builders.pyl
@@ -13,19 +13,19 @@
                    "builds/git_mnc-emu-dev-linux-sdk_google_phone_x86-sdk_addon/",
                    "builds/git_mnc-emu-dev-linux-sdk_phone_x86_64-sdk/",
                    "builds/git_mnc-emu-dev-linux-sdk_phone_x86-sdk/"],
-       "pollInterval": 600,
+       "pollInterval": 675,
        "type": "GSMultiPoller",
        "project": "git_mnc-emu-dev",
        "branch": "all",
        "name_identifier": "system-images",
      },
-     "sys_image_lmp_poller": {
+     "sys_image_lmp_mr1_poller": {
        "gs_bucket": "android-build-emu-sysimage",
        "gs_path": ["builds/git_lmp-mr1-emu-dev-linux-sdk_google_phone_x86_64-sdk_addon/",
                    "builds/git_lmp-mr1-emu-dev-linux-sdk_google_phone_x86-sdk_addon/",
                    "builds/git_lmp-mr1-emu-dev-linux-sdk_phone_x86_64-sdk/",
                    "builds/git_lmp-mr1-emu-dev-linux-sdk_phone_x86-sdk/"],
-       "pollInterval": 900,
+       "pollInterval": 596,
        "type": "GSMultiPoller",
        "project": "git_lmp-mr1-emu-dev",
        "branch": "all",
@@ -38,12 +38,31 @@
                    "builds/git_nyc-release-linux-sdk_google_phone_x86_64-sdk_addon/",
                    "builds/git_nyc-release-linux-sdk_phone_x86-sdk/",
                    "builds/git_nyc-release-linux-sdk_phone_x86_64-sdk/"],
-       "pollInterval": 1007,
+       "pollInterval": 977,
        "type": "GSMultiPoller",
        "project": "git_nyc-release",
        "branch": "all",
        "name_identifier": "system-images",
     },
+    "sys_image_klp_poller": {
+       "gs_bucket": "android-build-emu-sysimage",
+       "gs_path": ["builds/git_klp-emu-dev-linux-google_sdk_x86-sdk_addon/"],
+       "pollInterval": 7870000,
+       "type": "GSMultiPoller",
+       "project": "git_klp-emu-dev",
+       "branch": "all",
+       "name_identifier": "linux-addon",
+    },
+    "sys_image_lmp_poller": {
+       "gs_bucket": "android-build-emu-sysimage",
+       "gs_path": ["builds/git_lmp-emu-dev-linux-sdk_google_phone_x86-sdk_addon/",
+                   "builds/git_lmp-emu-dev-linux-sdk_google_phone_x86_64-sdk_addon/"],
+       "pollInterval": 853,
+       "type": "GSMultiPoller",
+       "project": "git_lmp-emu-dev",
+       "branch": "all",
+       "name_identifier": "system-images",
+    },
     "emulator_linux_poller": {
        "gs_bucket": "android-build-emu",
        "gs_path": ["builds/aosp-emu-master-dev-linux-sdk_tools_linux/"],
@@ -142,17 +161,17 @@
 
   "schedulers": {
     "emu_linux_scheduler": {
-      "project": ["emu-master-dev", "git_lmp-mr1-emu-dev", "git_mnc-emu-dev", "git_nyc-release"],
+      "project": ["emu-master-dev", "git_lmp-mr1-emu-dev", "git_mnc-emu-dev", "git_nyc-release", "git_lmp-emu-dev"],
       "branch": ["linux", "all"],
       "type": "gs_poller",
     },
     "emu_mac_scheduler": {
-      "project": ["emu-master-dev", "git_lmp-mr1-emu-dev", "git_mnc-emu-dev", "git_nyc-release"],
+      "project": ["emu-master-dev", "git_lmp-mr1-emu-dev", "git_mnc-emu-dev", "git_nyc-release", "git_lmp-emu-dev"],
       "branch": ["mac", "all"],
       "type": "gs_poller",
     },
     "emu_windows_scheduler": {
-      "project": ["emu-master-dev", "git_lmp-mr1-emu-dev", "git_mnc-emu-dev", "git_nyc-release"],
+      "project": ["emu-master-dev", "git_lmp-mr1-emu-dev", "git_mnc-emu-dev", "git_nyc-release", "git_lmp-emu-dev"],
       "branch": ["windows", "all"],
       "type": "gs_poller",
     },
diff --git a/build/scripts/master/emu_gs_scheduler.py b/build/scripts/master/emu_gs_scheduler.py
index eaef37e..4d1c2d5 100644
--- a/build/scripts/master/emu_gs_scheduler.py
+++ b/build/scripts/master/emu_gs_scheduler.py
@@ -35,13 +35,13 @@
         log.msg("%s: Error - emulator cache file not available, cancel build" % self.name)
         cancel_build = True
     try:
-        with open('sys_image_lmp_poller.cache', 'r') as f:
+        with open('sys_image_lmp_mr1_poller.cache', 'r') as f:
             content = f.read().splitlines()
-            lmp_revision = content[0]
-            lmp_file = ','.join(content[1:])
+            lmp_mr1_revision = content[0]
+            lmp_mr1_file = ','.join(content[1:])
     except:
-        lmp_revision = 'None'
-        lmp_file = ''
+        lmp_mr1_revision = 'None'
+        lmp_mr1_file = ''
     try:
         with open('sys_image_mnc_poller.cache', 'r') as f:
             content = f.read().splitlines()
@@ -58,16 +58,35 @@
     except:
         nyc_revision = 'None'
         nyc_file = ''
-
+    try:
+        with open('sys_image_lmp_poller.cache', 'r') as f:
+            content = f.read().splitlines()
+            lmp_revision = content[0]
+            lmp_file = ','.join(content[1:])
+    except:
+        lmp_revision = 'None'
+        lmp_file = ''
+    try:
+        with open('sys_image_klp_poller.cache', 'r') as f:
+            content = f.read().splitlines()
+            klp_revision = content[0]
+            klp_file = ','.join(content[1:])
+    except:
+        klp_revision = 'None'
+        klp_file = ''
     self.properties.setProperty('mnc_revision', mnc_revision, 'Scheduler')
     self.properties.setProperty('mnc_system_image', mnc_file, 'Scheduler')
-    self.properties.setProperty('lmp_revision', lmp_revision, 'Scheduler')
-    self.properties.setProperty('lmp_system_image', lmp_file, 'Scheduler')
+    self.properties.setProperty('lmp_mr1_revision', lmp_mr1_revision, 'Scheduler')
+    self.properties.setProperty('lmp_mr1_system_image', lmp_mr1_file, 'Scheduler')
     self.properties.setProperty('nyc_revision', nyc_revision, 'Scheduler')
     self.properties.setProperty('nyc_system_image', nyc_file, 'Scheduler')
+    self.properties.setProperty('klp_revision', klp_revision, 'Scheduler')
+    self.properties.setProperty('klp_system_image', klp_file, 'Scheduler')
+    self.properties.setProperty('lmp_revision', lmp_revision, 'Scheduler')
+    self.properties.setProperty('lmp_system_image', lmp_file, 'Scheduler')
     self.properties.setProperty('emu_revision', emu_revision, 'Scheduler')
     self.properties.setProperty('emulator_image', emu_file, 'Scheduler')
-    self.properties.setProperty('got_revision', '%s-%s-%s-%s' % (emu_revision, mnc_revision, lmp_revision, nyc_revision), 'Scheduler')
+    self.properties.setProperty('got_revision', '%s-%s-%s-%s-%s' % (emu_revision, mnc_revision, lmp_mr1_revision, nyc_revision.split('-')[0], lmp_revision), 'Scheduler')
     self.properties.setProperty('logs_dir', os.path.join(os.getcwd(), 'slave_logs', ''), 'Scheduler')
 
     rv = yield SingleBranchScheduler.addBuildsetForChanges(
diff --git a/build/scripts/slave/recipes/adt/adt.py b/build/scripts/slave/recipes/adt/adt.py
index 68366e1..d4356d0 100644
--- a/build/scripts/slave/recipes/adt/adt.py
+++ b/build/scripts/slave/recipes/adt/adt.py
@@ -28,9 +28,13 @@
   if project == 'git_mnc-emu-dev':
     remote_files_list.append(api.properties['mnc_system_image'])
   elif project == 'git_lmp-mr1-emu-dev':
-    remote_files_list.append(api.properties['lmp_system_image'])
+    remote_files_list.append(api.properties['lmp_mr1_system_image'])
   elif project == 'git_nyc-release':
     remote_files_list.append(api.properties['nyc_system_image'])
+  elif project == 'git_lmp-emu-dev':
+    remote_files_list.append(api.properties['lmp_system_image'])
+  elif project == 'git_klp-emu-dev':
+    remote_files_list.append(api.properties['klp_system_image'])
 
   download_path = api.path['slave_build'].join('')
   emulator_path = download_path.join('tools', 'emulator')
@@ -127,9 +131,9 @@
                      'boot_cfg.csv',
                      '{"api": "<=21"}')
     # At least one of the system images are available
-    if str(api.properties['lmp_revision']) != 'None' and project in ['git_lmp-mr1-emu-dev', 'emu-master-dev']:
-      PythonTestStep('Boot Test - LMP System Image',
-                     api.path.join(log_dir, 'boot_test_LMP_sysimage'),
+    if str(api.properties['lmp_mr1_revision']) != 'None' and project in ['git_lmp-mr1-emu-dev', 'emu-master-dev']:
+      PythonTestStep('Boot Test - LMP MR1 System Image',
+                     api.path.join(log_dir, 'boot_test_LMP_MR1_sysimage'),
                      'test_boot.*',
                      'boot_cfg.csv',
                      '{"api": "22"}')
@@ -145,6 +149,18 @@
                      'test_boot.*',
                      'boot_cfg.csv',
                      '{"api": "24"}')
+    if str(api.properties['lmp_revision']) != 'None' and project in ['git_lmp-emu-dev', 'emu-master-dev']:
+      PythonTestStep('Boot Test - LMP System Image',
+                     api.path.join(log_dir, 'boot_test_LMP_sysimage'),
+                     'test_boot.*',
+                     'boot_cfg.csv',
+                     '{"api": "21", "tag": "google_apis"}')
+    if str(api.properties['klp_revision']) != 'None' and project in ['git_klp-emu-dev', 'emu-master-dev']:
+      PythonTestStep('Boot Test - KLP System Image',
+                     api.path.join(log_dir, 'boot_test_KLP_sysimage'),
+                     'test_boot.*',
+                     'boot_cfg.csv',
+                     '{"api": "19", "tag": "default"}')
     PythonTestStep('Run Emulator CTS Test',
                    api.path.join(log_dir, 'CTS_test'),
                    'test_cts.*',
diff --git a/emu_test/config/boot_cfg.csv b/emu_test/config/boot_cfg.csv
index 312670a..89180bd 100644
--- a/emu_test/config/boot_cfg.csv
+++ b/emu_test/config/boot_cfg.csv
@@ -32,10 +32,10 @@
 ,,x86,,,,,,P,,,,,,,,,,
 ,default,armeabi-v7a,Nexus 5,2048,yes,P,P,P,P,P,,P,P,P,P,P,P,P
 ,,x86,Nexus 5,2048,yes,P,P,P,P,P,P,P,P,P,P,P,P,P
-,,x86_64,Nexus 5,2048,yes,P,P,P,P,S,,P,P,P,X,X,X,X
+,,x86_64,Nexus 5,2048,yes,P,P,P,P,S,,P,P,P,P,P,P,P
 ,google_apis,armeabi-v7a,,,,,,P,,,,,,,,,,
-,,x86,,,,,,P,,,,,,,,,,
-,,x86_64,,,,,,P,,,,,,,,,,
+,,x86,,,,P,P,P,P,P,P,P,P,P,P,P,P,P
+,,x86_64,,,,P,P,P,P,P,P,P,P,P,P,P,P,P
 API 20,android-wear,armeabi-v7a,,,,,,P,,,,,,,,,,
 ,,x86,,,,,,P,,,,,,,,,,
 API 19,default,armeabi-v7a,,,,,,P,,,,,,,,,,
@@ -53,4 +53,4 @@
 API 15,default,armeabi-v7a,,,no,,,P,,,,,,,,,,
 ,,mips,,,no,,,P,,,,,,,,,,
 ,,x86,,,no,,,P,,,,,,,,,,
-API 10,default,x86,,,no,P,P,P,P,,,P,P,P,P,P,P,P
+API 10,default,x86,,,no,P,P,P,P,,,P,P,P,P,P,P,P
\ No newline at end of file
diff --git a/emu_test/test_boot/test_boot.py b/emu_test/test_boot/test_boot.py
index 41c64b6..3066c1a 100644
--- a/emu_test/test_boot/test_boot.py
+++ b/emu_test/test_boot/test_boot.py
@@ -35,6 +35,7 @@
         try:
             if result:
                 self.start_proc.wait()
+            time.sleep(1)
             self.kill_proc_by_name(["crash-service"])
             psutil.Popen(["adb", "kill-server"])
             os.remove(os.path.join(avd_dir, '%s.ini' % self.avd_config.name()))
diff --git a/emu_test/utils/download_unzip_image.py b/emu_test/utils/download_unzip_image.py
index 230e06a..29db8f8 100644
--- a/emu_test/utils/download_unzip_image.py
+++ b/emu_test/utils/download_unzip_image.py
@@ -2,6 +2,8 @@
 import argparse
 import subprocess
 import psutil
+import zipfile
+import shutil
 
 parser = argparse.ArgumentParser(description='Download and unzip a list of files separated by comma')
 parser.add_argument('--file', dest='remote_file_list', action='store',
@@ -18,18 +20,22 @@
 
 def get_dst_dir(remote_path):
   file_name = os.path.basename(remote_path)
-  if file_name.startswith('sdk-repo-linux-system-images'):
+  if file_name.startswith('sdk-repo-linux-system-images') or file_name.startswith('sdk-repo-linux-addon'):
     branch_name = remote_path.split('/')[-2]
     if 'google_phone' in branch_name:
       tag = 'google_apis'
     else:
       tag = 'default'
-    if 'lmp' in branch_name:
+    if 'lmp_mr1' in branch_name:
       api = '22'
     elif 'mnc' in branch_name:
       api = '23'
     elif 'nyc' in branch_name:
       api = 'N'
+    elif 'lmp' in branch_name:
+      api = '21'
+    elif 'klp' in branch_name:
+      api = '19'
     else:
       raise ValueError("unsupported image %s", branch_name)
     return os.path.join(os.environ['ANDROID_SDK_ROOT'],
@@ -49,15 +55,32 @@
     except:
       pass
 
+def verbose_call(cmd):
+  print "Run command %s" % ' '.join(cmd)
+  subprocess.check_call(cmd)
+
+def unzip_addon_dir(file_name, dst_dir):
+  print file_name, dst_dir
+  with open(file_name, 'rb') as fh:
+    z = zipfile.ZipFile(fh)
+    for name in z.namelist():
+      if ("images/") in name and not name.endswith("images/"):
+        base_name = os.path.basename(name)
+        if not base_name:
+          abi = os.path.basename(os.path.normpath(name))
+          verbose_call(["mkdir", "-p", os.path.join(dst_dir,abi)])
+          print "Found abi %s" % abi
+          continue
+        dst_path = os.path.join(dst_dir, abi, base_name)
+        with z.open(name) as src, file(dst_path, "wb") as dst:
+          print "unzip from %s to %s" % (name, dst_path)
+          shutil.copyfileobj(src, dst)
+
 def download_and_unzip():
   clean_emu_proc()
   file_list = args.remote_file_list.split(',')
   dst_dir = get_dst_dir(file_list[0])
 
-  def verbose_call(cmd):
-    print "Run command %s" % ' '.join(cmd)
-    subprocess.check_call(cmd)
-
   for file_path in file_list:
     file_path = file_path.strip('\n')
     if file_path == '':
@@ -66,7 +89,8 @@
     remote_path = '%s@%s:%s' % (args.remote_user, args.remote_ip, file_path)
     file_name = os.path.basename(remote_path)
     try:
-      verbose_call(['scp', remote_path, '.'])
+      #verbose_call(['scp', remote_path, '.'])
+      verbose_call(['scp', file_path, '.'])
       if dst_dir is not None:
         verbose_call(['mkdir', '-p', dst_dir])
         if 'x86_64' in file_path:
@@ -75,7 +99,10 @@
           verbose_call(['rm', '-rf', os.path.join(dst_dir,'x86')])
         elif 'armv7' in file_path:
           verbose_call(['rm', '-rf', os.path.join(dst_dir,'armeabi-v7a')])
-        verbose_call(['unzip', '-o', file_name, '-d', dst_dir])
+        if 'system-images' in file_path:
+          verbose_call(['unzip', '-o', file_name, '-d', dst_dir])
+        else:
+          unzip_addon_dir(file_name, dst_dir)
       else:
         verbose_call(['unzip', '-o', file_name])
     except Exception as e: