Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IEP-999: Tools Validation for required tools #802

Merged
merged 12 commits into from
Sep 14, 2023
Merged
2 changes: 2 additions & 0 deletions bundles/com.espressif.idf.core/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<attribute name="javadoc_location" value="jar:platform:/resource/com.espressif.idf.core/lib/commons-collections4-4.4-javadoc.jar!/"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/commons-compress-1.21.jar"/>
<classpathentry kind="lib" path="lib/xz-1.9.jar"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
Expand Down
8 changes: 7 additions & 1 deletion bundles/com.espressif.idf.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ Export-Package: com.espressif.idf.core,
com.espressif.idf.core.logging,
com.espressif.idf.core.resources,
com.espressif.idf.core.toolchain,
com.espressif.idf.core.tools,
com.espressif.idf.core.tools.util,
com.espressif.idf.core.tools.vo,
com.espressif.idf.core.util,
com.espressif.idf.core.variable,
org.json.simple,
Expand All @@ -47,4 +50,7 @@ Bundle-ClassPath: .,
lib/snakeyaml-1.30.jar,
lib/opencsv-5.7.0.jar,
lib/commons-beanutils-1.9.4.jar,
lib/commons-text-1.10.0.jar
lib/commons-text-1.10.0.jar,
lib/commons-compress-1.21.jar,
lib/xz-1.9.jar
Import-Package: org.eclipse.embedcdt.core
4 changes: 3 additions & 1 deletion bundles/com.espressif.idf.core/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ bin.includes = META-INF/,\
lib/opencsv-5.7.0.jar,\
lib/commons-lang3-3.12.0.jar,\
lib/commons-beanutils-1.9.4.jar,\
lib/commons-text-1.10.0.jar
lib/commons-text-1.10.0.jar,\
lib/commons-compress-1.21.jar,\
lib/xz-1.9.jar
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

public class DefaultSystemWrapper implements SystemWrapper
{
private static final String PATH = "PATH"; //$NON-NLS-1$
private static final String PATHEXT = "PATHEXT"; //$NON-NLS-1$

@Override
public String getPathEnv()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
package com.espressif.idf.core;

import java.io.File;
import java.io.IOException;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;

import com.espressif.idf.core.logging.Logger;
import com.espressif.idf.core.util.StringUtil;

public class SystemExecutableFinder implements ExecutableFinder
Expand Down Expand Up @@ -79,9 +81,21 @@ private IPath findExecutable(IPath path)
}

}
else if (isExecutable(path))
else
{
return path;
try
{
Runtime.getRuntime().exec("/bin/chmod 755 ".concat(path.toOSString())); //$NON-NLS-1$
}
catch (IOException e)
{
Logger.log(e);
}

if (isExecutable(path))
{
return path;
}
}
return null;
}
alirana01 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -100,7 +114,7 @@ private boolean isExecutable(IPath path)
{
return false;
}

return file.canExecute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

public interface SystemWrapper
{
String PATH = "PATH"; //$NON-NLS-1$
String PATHEXT = "PATHEXT"; //$NON-NLS-1$

public String getPathEnv();
public String getEnvExecutables();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright 2022 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.ui.tools.wizard;
package com.espressif.idf.core.tools;

/**
* Interface to store the constants related to tools management wizard
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.espressif.idf.ui.tools;
package com.espressif.idf.core.tools;

/**
* Keys in the tools.json file mentioned here for the better usage
Expand All @@ -22,6 +22,8 @@ public interface IToolsJsonKeys

String VERSION_CMD_KEY = "version_cmd"; //$NON-NLS-1$

String VERSION_REGEX = "version_regex"; //$NON-NLS-1$

String SUPPORTED_TARGETS_KEY = "supported_targets"; //$NON-NLS-1$

String NAME_KEY = "name"; //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.espressif.idf.ui.tools;
package com.espressif.idf.core.tools;

import static java.lang.annotation.ElementType.FIELD;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
* Copyright 2021 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.ui.tools;
package com.espressif.idf.core.tools;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.Platform;

import com.espressif.idf.core.tools.vo.ToolsVO;
import com.espressif.idf.core.tools.vo.VersionDetailsVO;
import com.espressif.idf.core.tools.vo.VersionsVO;
import com.espressif.idf.core.util.IDFUtil;
import com.espressif.idf.ui.tools.vo.ToolsVO;
import com.espressif.idf.ui.tools.vo.VersionDetailsVO;
import com.espressif.idf.ui.tools.vo.VersionsVO;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
Expand All @@ -37,11 +38,14 @@ public class ToolsJsonParser
{
private Gson gson;
private List<ToolsVO> toolsList;
private List<ToolsVO> requiredToolsList;
private static final String[] REQUIRED_TOOLS = new String[] {"cmake", "dfu-util", "ninja"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
alirana01 marked this conversation as resolved.
Show resolved Hide resolved

public ToolsJsonParser()
{
gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
toolsList = new ArrayList<>();
requiredToolsList = new ArrayList<>();
}

public void loadJson() throws Exception
Expand All @@ -50,6 +54,7 @@ public void loadJson() throws Exception
JsonReader jsonReader = new JsonReader(new FileReader(IDFUtil.getIDFToolsJsonFileForInstallation()));
JsonObject jsonObject = gson.fromJson(jsonReader, JsonObject.class);
JsonArray jsonArray = jsonObject.get(IToolsJsonKeys.TOOLS_KEY).getAsJsonArray();
List<String> reqToolsNamesList = Arrays.asList(REQUIRED_TOOLS);
for (int i = 0; i < jsonArray.size(); i++)
{
JsonObject toolsJsonObject = jsonArray.get(i).getAsJsonObject();
Expand All @@ -72,6 +77,7 @@ public void loadJson() throws Exception
getStringsListFromJsonArray(toolsJsonObject.get(IToolsJsonKeys.SUPPORTED_TARGETS_KEY).getAsJsonArray()));
}
toolsVO.setVersionCmd(getStringsListFromJsonArray(toolsJsonObject.get(IToolsJsonKeys.VERSION_CMD_KEY).getAsJsonArray()));
toolsVO.setVersionRegex(toolsJsonObject.get(IToolsJsonKeys.VERSION_REGEX).getAsString());
toolsVO.setVersionVO(getVersions(toolsJsonObject.get(IToolsJsonKeys.VERSIONS_VO_KEY).getAsJsonArray()));
toolsVO.setVersion(jsonObject.get(IToolsJsonKeys.VERSION_KEY).getAsString());
JsonElement jsonElement = toolsJsonObject.get(IToolsJsonKeys.PLATFORM_OVERRIDES_KEY);
Expand All @@ -81,6 +87,10 @@ public void loadJson() throws Exception
}

toolsList.add(toolsVO);
if (reqToolsNamesList.contains(toolsVO.getName()))
{
requiredToolsList.add(toolsVO);
}
}
}

Expand Down Expand Up @@ -219,4 +229,10 @@ public List<ToolsVO> getToolsList()
{
return toolsList;
}

public List<ToolsVO> getRequiredToolsList()
{
return requiredToolsList;

}
alirana01 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright 2022 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.ui.tools.wizard.pages;
package com.espressif.idf.core.tools;

import java.util.Arrays;

Expand All @@ -20,15 +20,15 @@ public enum ToolsPlatformMapping
{
// @formatter:off

WIN32("win32", Platform.OS_WIN32,Platform.ARCH_X86),
WIN64("win64", Platform.OS_WIN32, Platform.ARCH_X86_64),
MACOS("macos", Platform.OS_MACOSX, Platform.ARCH_X86_64),
MACOSARM64("macos-arm64", Platform.OS_MACOSX, Platform.ARCH_AARCH64),
LINUXAMD64("linux-amd64", Platform.OS_LINUX, Platform.ARCH_X86_64),
LINUXARM64("linux-arm64", Platform.OS_LINUX, "arm64"),
LINUXARMEL("linux-armel", Platform.OS_LINUX, "armel"),
LINUXARMHF("linux-armhf", Platform.OS_LINUX, "armhf"),
LINUXI686("linux-i686", Platform.OS_LINUX, "i686");
WIN32("win32", Platform.OS_WIN32,Platform.ARCH_X86), //$NON-NLS-1$
WIN64("win64", Platform.OS_WIN32, Platform.ARCH_X86_64), //$NON-NLS-1$
MACOS("macos", Platform.OS_MACOSX, Platform.ARCH_X86_64), //$NON-NLS-1$
MACOSARM64("macos-arm64", Platform.OS_MACOSX, Platform.ARCH_AARCH64), //$NON-NLS-1$
LINUXAMD64("linux-amd64", Platform.OS_LINUX, Platform.ARCH_X86_64), //$NON-NLS-1$
LINUXARM64("linux-arm64", Platform.OS_LINUX, "arm64"), //$NON-NLS-1$ //$NON-NLS-2$
LINUXARMEL("linux-armel", Platform.OS_LINUX, "armel"), //$NON-NLS-1$ //$NON-NLS-2$
LINUXARMHF("linux-armhf", Platform.OS_LINUX, "armhf"), //$NON-NLS-1$ //$NON-NLS-2$
LINUXI686("linux-i686", Platform.OS_LINUX, "i686"); //$NON-NLS-1$ //$NON-NLS-2$

// @formatter:on

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*******************************************************************************
* Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.core.tools;

import com.espressif.idf.core.SystemWrapper;

/**
* Tools System wrapper to make sure to avoid the
* system path when verifying for validation after tools installation
* @author Ali Azam Rana
*
*/
public class ToolsSystemWrapper implements SystemWrapper
{
private String path;

public ToolsSystemWrapper(String path)
{
this.path = path;
}

@Override
public String getPathEnv()
{
return path;
}

@Override
public String getEnvExecutables()
{
return System.getenv(PATHEXT);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright 2021 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.ui.tools;
package com.espressif.idf.core.tools.util;

import java.io.BufferedInputStream;
import java.io.File;
Expand All @@ -27,13 +27,17 @@
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.tukaani.xz.XZInputStream;

import com.espressif.idf.core.IDFEnvironmentVariables;
import com.espressif.idf.core.SystemExecutableFinder;
import com.espressif.idf.core.logging.Logger;
import com.espressif.idf.core.tools.ToolsSystemWrapper;
import com.espressif.idf.core.tools.vo.ToolsVO;
import com.espressif.idf.core.util.FileUtil;
import com.espressif.idf.ui.tools.vo.ToolsVO;
import com.espressif.idf.core.util.StringUtil;

/**
* Utility class for Tools Management operations
Expand Down Expand Up @@ -159,7 +163,7 @@ public static void extractTarGz(String tarFile, String outputDir)
{
Files.createDirectories(pathEntryOutput.getParent());
Files.copy(tararchiveinputstream, pathEntryOutput, StandardCopyOption.REPLACE_EXISTING);
Runtime.getRuntime().exec("/bin/chmod 755 ".concat(pathEntryOutput.toString()));
Runtime.getRuntime().exec("/bin/chmod 755 ".concat(pathEntryOutput.toString())); //$NON-NLS-1$
}

}
alirana01 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -206,9 +210,9 @@ else if (archiveentry.isDirectory())
}
else
{
System.out.println(pathEntryOutput.toString() + " " + archiveentry.getSize());
System.out.println(pathEntryOutput.toString() + " " + archiveentry.getSize()); //$NON-NLS-1$
Files.copy(tararchiveinputstream, pathEntryOutput, StandardCopyOption.REPLACE_EXISTING);
Runtime.getRuntime().exec("/bin/chmod 755 ".concat(pathEntryOutput.toString()));
Runtime.getRuntime().exec("/bin/chmod 755 ".concat(pathEntryOutput.toString())); //$NON-NLS-1$
}
}
tararchiveinputstream.close();
alirana01 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -223,7 +227,6 @@ else if (archiveentry.isDirectory())
}
}


private static void createHardLinks(Path link, Path target)
{
try
Expand Down Expand Up @@ -304,4 +307,25 @@ public static String getFileChecksum(MessageDigest digest, File file) throws IOE
}
return sb.toString();
}

/**
* Gets the absolute path for the tool from the given path
* @param toolName tool to find absolute path
* @param path the path to variable to look into, if null System.getenv() will be used
* @return absolute path to the tool
*/
public static IPath findAbsoluteToolPath(String toolName, String path)
{
if (StringUtil.isEmpty(path))
{
Map<String, String> env = System.getenv();
if (env.containsKey(IDFEnvironmentVariables.PATH))
path = env.get(IDFEnvironmentVariables.PATH);
else
path = env.get("Path"); //$NON-NLS-1$
}

SystemExecutableFinder systemExecutableFinder = new SystemExecutableFinder(new ToolsSystemWrapper(path));
return systemExecutableFinder.find(toolName);
}
}
Loading
Loading