#!/usr/bin/env python3

# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This Swarming bot test script calls the gapit validate_gpu_profiling command.

import argparse
import botutil
import json
import os
import sys


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('agi_dir', help='Path to AGI build')
    parser.add_argument('out_dir', help='Path to output directory')
    args = parser.parse_args()

    #### Early checks and sanitization
    assert os.path.isdir(args.agi_dir)
    agi_dir = os.path.normpath(args.agi_dir)
    assert os.path.isdir(args.out_dir)
    out_dir = os.path.normpath(args.out_dir)

    #### Load test parameters
    test_params = {}
    # Do not require 'apk' and 'package' params, as some drivers are just
    # obtained by system update.
    botutil.load_params(test_params)

    #### Install APK
    if 'apk' in test_params.keys():
        if not 'package' in test_params.keys():
            botutil.log('Error: have "apk" but no "package" in params.json')
            return 1
        botutil.install_apk(test_params)

    #### Call gapit command
    gapit = os.path.join(agi_dir, 'gapit')
    cmd = [
        gapit, 'validate_gpu_profiling',
        '-os', 'android'
    ]
    p = botutil.runcmd(cmd)
    return p.returncode


if __name__ == '__main__':
    sys.exit(main())

@@ -791,16 +791,80 @@
 arser()%0A
+    parser.add_argument('adb_path', help='Path to adb command')%0A
     pars
@@ -1055,16 +1055,103 @@
 ization%0A
+    assert os.path.isfile(args.adb_path)%0A    adb_path = os.path.abspath(args.adb_path)%0A
     asse
@@ -1195,36 +1195,35 @@
 i_dir = os.path.
-norm
+abs
 path(args.agi_di
@@ -1290,29 +1290,204 @@
 ath.
-normpath(args.out_dir
+abspath(args.out_dir)%0A    gapit_path = os.path.join(agi_dir, 'gapit')%0A%0A    #### Create BotUtil with relevant adb and gapit paths%0A    bu = botutil.BotUtil(adb_path)%0A    bu.set_gapit_path(gapit_path
 )%0A%0A 
@@ -1701,16 +1701,27 @@
 tall APK
+ if need be
 %0A    if 
@@ -1904,22 +1904,17 @@
        b
-otutil
+u
 .install
@@ -1973,69 +1973,52 @@
 apit
- = os.path.join(agi_dir, 'gapit')%0A    cmd = %5B%0A        
+_args = %5B'-os', 'android'%5D%0A    p = bu.
 gapit
-, 
+(
 'val
@@ -2042,66 +2042,19 @@
 ng',
-%0A        '-os', 'android'%0A    %5D%0A    p = botutil.runcmd(cmd
+ gapit_args
 )%0A  

Fix swarming gpu profiling validation (#757)