1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tradefed.targetprep; 18 19 import com.android.tradefed.build.IBuildInfo; 20 import com.android.tradefed.config.Option; 21 import com.android.tradefed.config.OptionClass; 22 import com.android.tradefed.device.DeviceNotAvailableException; 23 import com.android.tradefed.device.ITestDevice; 24 import com.android.tradefed.log.LogUtil.CLog; 25 import com.android.tradefed.util.CommandResult; 26 import com.android.tradefed.util.CommandStatus; 27 import com.android.tradefed.util.FileUtil; 28 import com.android.tradefed.util.IRunUtil; 29 import com.android.tradefed.util.RunUtil; 30 31 import java.io.File; 32 import java.io.IOException; 33 import java.util.ArrayList; 34 import java.util.List; 35 36 /** 37 * Sets up a Python virtualenv on the host and installs packages. To activate it, the working 38 * directory is changed to the root of the virtualenv. 39 */ 40 @OptionClass(alias = "python-venv") 41 public class PythonVirtualenvPreparer implements ITargetPreparer { 42 43 private static final String PIP = "pip"; 44 private static final String PATH = "PATH"; 45 protected static final String PYTHONPATH = "PYTHONPATH"; 46 private static final int BASE_TIMEOUT = 1000 * 60; 47 48 @Option(name = "venv-dir", description = "path of an existing virtualenv to use") 49 private File mVenvDir = null; 50 51 @Option(name = "requirements-file", description = "pip-formatted requirements file") 52 private File mRequirementsFile = null; 53 54 @Option(name = "dep-module", description = "modules which need to be installed by pip") 55 private List<String> mDepModules = new ArrayList<>(); 56 57 @Option(name = "disable", description = "disable this preparer") 58 private boolean mDisable = false; 59 60 IRunUtil mRunUtil = new RunUtil(); 61 String mPip = PIP; 62 63 @Override 64 public void setUp(ITestDevice device, IBuildInfo buildInfo) 65 throws TargetSetupError, BuildError, DeviceNotAvailableException { 66 if (mDisable) { 67 CLog.i("Skipping PythonVirtualenvPreparer"); 68 return; 69 } 70 startVirtualenv(buildInfo, device); 71 installDeps(buildInfo, device); 72 } 73 74 protected void installDeps(IBuildInfo buildInfo, ITestDevice device) throws TargetSetupError { 75 boolean hasDependencies = false; 76 if (mRequirementsFile != null) { 77 CommandResult c = mRunUtil.runTimedCmd(BASE_TIMEOUT * 5, mPip, 78 "install", "-r", mRequirementsFile.getAbsolutePath()); 79 if (c.getStatus() != CommandStatus.SUCCESS) { 80 CLog.e("Installing dependencies from %s failed", 81 mRequirementsFile.getAbsolutePath()); 82 throw new TargetSetupError("Failed to install dependencies with pip", 83 device.getDeviceDescriptor()); 84 } 85 hasDependencies = true; 86 } 87 if (!mDepModules.isEmpty()) { 88 for (String dep : mDepModules) { 89 CLog.i("Attempting installation of %s", dep); 90 CommandResult c = mRunUtil.runTimedCmd(BASE_TIMEOUT * 5, mPip, 91 "install", dep); 92 if (c.getStatus() != CommandStatus.SUCCESS) { 93 CLog.e("Installing %s failed", dep); 94 throw new TargetSetupError("Failed to install dependencies with pip", 95 device.getDeviceDescriptor()); 96 } 97 hasDependencies = true; 98 } 99 } 100 if (!hasDependencies) { 101 CLog.i("No dependencies to install"); 102 } else { 103 // make the install directory of new packages available to other classes that 104 // receive the build 105 buildInfo.setFile(PYTHONPATH, new File(mVenvDir, 106 "local/lib/python2.7/site-packages"), 107 buildInfo.getBuildId()); 108 } 109 } 110 111 protected void startVirtualenv(IBuildInfo buildInfo, ITestDevice device) 112 throws TargetSetupError { 113 if (mVenvDir != null) { 114 CLog.i("Using existing virtualenv based at %s", mVenvDir.getAbsolutePath()); 115 activate(); 116 return; 117 } 118 try { 119 mVenvDir = FileUtil.createNamedTempDir(buildInfo.getTestTag() + "-virtualenv"); 120 mRunUtil.runTimedCmd(BASE_TIMEOUT, "virtualenv", mVenvDir.getAbsolutePath()); 121 activate(); 122 } catch (IOException e) { 123 CLog.e("Failed to create temp directory for virtualenv"); 124 throw new TargetSetupError("Error creating virtualenv", e, 125 device.getDeviceDescriptor()); 126 } 127 } 128 129 protected void addDepModule(String module) { 130 mDepModules.add(module); 131 } 132 133 protected void setRequirementsFile(File f) { 134 mRequirementsFile = f; 135 } 136 137 private void activate() { 138 File binDir = new File(mVenvDir, "bin"); 139 mRunUtil.setWorkingDir(binDir); 140 String path = System.getenv(PATH); 141 mRunUtil.setEnvVariable(PATH, binDir + ":" + path); 142 File pipFile = new File(binDir, PIP); 143 pipFile.setExecutable(true); 144 mPip = pipFile.getAbsolutePath(); 145 } 146 }