Skip to content

Commit ef04b0c

Browse files
committed
WIP: Change to os-agnostic path string handling
1 parent 79367d7 commit ef04b0c

File tree

1 file changed

+37
-39
lines changed

1 file changed

+37
-39
lines changed

src/renderer/search_emme_pythonpath.js

+37-39
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ const listEMMEPythonPaths = () => {
7272
const paths = []
7373
commonEmmePaths.forEach(commonEmmePath => {
7474
paths.push(
75-
`\\Program Files\\${commonEmmePath}`,
76-
`\\Program Files (x86)\\${commonEmmePath}`,
77-
`\\${commonEmmePath}`,
75+
`Program Files\\${commonEmmePath}`,
76+
`Program Files (x86)\\${commonEmmePath}`,
77+
`${commonEmmePath}`,
7878
)
7979
})
8080
paths.push(`usr/bin/python${pythonVersion.major}`); // mainly for developers on Mac & Linux
@@ -83,7 +83,7 @@ const listEMMEPythonPaths = () => {
8383
const allPathCombinations = drives.reduce(
8484
(accumulator, d) => {
8585
// Combine each (d)rive to all (p)aths, and merge results via reduce
86-
return accumulator.concat(paths.map((p) => `${d}${p}`));
86+
return accumulator.concat(paths.map((p) => path.join(d,p)));
8787
}, []);
8888
allPathCombinations.forEach((pathCombination) => {
8989
const foundPythonEnv = hasPythonEnv(pathCombination);
@@ -112,50 +112,48 @@ function getVersion(semver) {
112112
}
113113

114114
function hasPythonEnv(basePath) {
115-
const pathExists = fs.existsSync(basePath) && fs.lstatSync(basePath).isDirectory();
115+
const pathExists = fs.existsSync(basePath);
116116
console.log(basePath);
117117
console.log(pathExists);
118118
let exePaths = [];
119119
if (pathExists) {
120-
const subPaths = fs.readdirSync(basePath)
121-
subPaths.forEach(path => {
122-
if(path.startsWith("Emme ")) {
123-
const majorVersionFolderPath = `${basePath}${getDirSeparator()}${path}`;
124-
const majorVersionFolderPathFiles = fs.readdirSync(majorVersionFolderPath);
125-
126-
console.log(majorVersionFolderPathFiles);
127-
128-
// Filter away every folder except possible Emme installation folders
129-
const subVersionFolders = majorVersionFolderPathFiles.filter(file => file.startsWith("Emme-"));
130-
subVersionFolders.forEach(subVersionFolder => {
131-
// Go through the subdirectory and look for a python executable, should be in Emme-x.xx.xx.xx/Python/python.exe on Windows machines.
132-
const emmeFolderFilesPath = majorVersionFolderPath.concat(getDirSeparator(), subVersionFolder);
133-
const emmeFolderFiles = fs.readdirSync(emmeFolderFilesPath);
134-
console.log(emmeFolderFilesPath);
135-
console.log(emmeFolderFiles);
136-
emmeFolderFiles.forEach(folderPath => {
137-
if(folderPath.startsWith("Python")) {
138-
const pythonFolderPath = emmeFolderFilesPath.concat(getDirSeparator(), folderPath);
139-
const pythonPathFiles = fs.readdirSync(pythonFolderPath);
140-
pythonPathFiles.forEach(fileName => {
141-
if(fileName === 'python.exe') {
142-
exePaths.push(pythonFolderPath.concat(getDirSeparator(), fileName));
143-
}
144-
})
145-
}
120+
try {
121+
const subPaths = fs.readdirSync(basePath)
122+
subPaths.forEach(subPath => {
123+
if(subPath.startsWith("Emme ")) {
124+
const majorVersionFolderPath = path.join(basePath, subPath);
125+
const majorVersionFolderPathFiles = fs.readdirSync(majorVersionFolderPath);
126+
127+
console.log(majorVersionFolderPathFiles);
128+
129+
// Filter away every folder except possible Emme installation folders
130+
const subVersionFolders = majorVersionFolderPathFiles.filter(file => file.startsWith("Emme-"));
131+
subVersionFolders.forEach(subVersionFolder => {
132+
// Go through the subdirectory and look for a python executable, should be in Emme-x.xx.xx.xx/Python/python.exe on Windows machines.
133+
const emmeFolderFilesPath = path.join(majorVersionFolderPath, subVersionFolder);
134+
const emmeFolderFiles = fs.readdirSync(emmeFolderFilesPath);
135+
emmeFolderFiles.forEach(emmeFolderPath => {
136+
if(emmeFolderPath.startsWith("Python")) {
137+
const pythonFolderPath = path.join(emmeFolderFilesPath, emmeFolderPath);
138+
const pythonPathFiles = fs.readdirSync(pythonFolderPath);
139+
pythonPathFiles.forEach(fileName => {
140+
if(fileName === 'python.exe') {
141+
exePaths.push(path.join(pythonFolderPath, fileName));
142+
}
143+
})
144+
}
145+
})
146146
})
147-
})
148-
}
149-
})
147+
}
148+
})
149+
}
150+
catch(e) {
151+
console.log(`Error traversing path ${basePath}`);
152+
}
150153
}
151154
return exePaths;
152155
}
153156

154-
function getDirSeparator() {
155-
const isRunningOnWindows = os.platform() === 'win32';
156-
return isRunningOnWindows ? '\\' : '/'
157-
}
158-
159157
module.exports = {
160158
searchEMMEPython: searchEMMEPython,
161159
listEMMEPythonPaths: listEMMEPythonPaths,

0 commit comments

Comments
 (0)