half working

downloader-work
BrOakes 2020-01-04 21:47:03 -05:00
parent 8eeed800ad
commit 37ad03d9d5
2 changed files with 159 additions and 103 deletions

View File

@ -1,6 +1,6 @@
module.exports = {
download
};
// module.exports = {
// download
// };
const request = require('request');
const fs = require('fs');
@ -8,104 +8,90 @@ const os = require('os');
const {execSync} = require('child_process');
const path = require('path');
const copydir = require('copy-dir');
const pyversion = require('./getpythonversions.js');
async function download() {
async function download(ver) {
let url = await getPythonDownloadLink();
let filename = path.parse(url).base;
try {
fs.mkdirSync('Python-Installer');
} catch(e) {'';}
try {
fs.mkdirSync('Python');
} catch(e) {'';}
try {
fs.mkdirSync('Temp-Python');
} catch(e) {'';}
try {
fs.writeFileSync('.installerLocation', filename);
} catch(e) {'';}
// let installSettings = fs.readFileSync('unattend.xml');
let url = "";
let version = "";
let out_file = `Python-Installer/${filename}`
await new Promise(resolve => {
request({
uri: url,
encoding: null
}, function (error, response, body) {
fs.writeFileSync(out_file, body, {});
resolve();
});
});
console.log('Download Complete!');
switch (os.platform())
{
case 'win32': {
try {
let installerPath = path.join(__dirname, '..', 'Python-Installer/', filename);
let tempPath = path.join(__dirname, '..', 'Temp-Python/');
let targetPath = path.join(__dirname, '..', 'Python/');
execSync(`${installerPath} /passive DefaultJustForMeTargetDir=${tempPath}`, {
stdio: "inherit"
});
console.log("Creating Portable Python Directory");
copydir.sync(tempPath, targetPath);
console.log("Portable Directory Created!");
console.log("Uninstalling Unnecessary Python Files");
execSync(`${installerPath} /passive /uninstall`, {
stdio: 'inherit'
});
fs.rmdirSync(tempPath);
console.log("PordaPy Installed!");
} catch (err) {
console.log(err);
}
break;
}
if (ver === undefined) {
version = await pyversion.current;
console.log("Python: " + version.toString() + " Selected");
} else if (await pyversion.validateVersion(ver)) {
version = ver;
console.log("Python: " + version.toString() + " Selected");
} else {
console.log("Version of python not available");
}
}
function getPythonDownloadLink() {
return new Promise(resolve => {
request({
uri: "https://www.python.org/downloads/",
}, function (error, response, body) {
let current_os = '';
switch (os.platform())
{
case 'darwin':
current_os = "download-os-mac-osx";
break;
case 'linux':
current_os = "download-os-source";
break;
case 'win32':
current_os = "download-os-windows";
break;
default:
current_os = "download-os-source";
break;
}
let current_download_start = body.indexOf("href=",body.indexOf(current_os)) + 6;
let current_download_end = body.indexOf('">', current_download_start);
let current_download = body.substring(current_download_start, current_download_end);
let url = current_download;
if (version !== "") {
console.log(await pyversion.getDownloadLink(version));
}
resolve(url);
});
});
console.log(url);
// let url = await getPythonDownloadLink();
// let filename = path.parse(url).base;
// try {
// fs.mkdirSync('Python-Installer');
// } catch(e) {'';}
// try {
// fs.mkdirSync('Python');
// } catch(e) {'';}
// try {
// fs.mkdirSync('Temp-Python');
// } catch(e) {'';}
// try {
// fs.writeFileSync('.installerLocation', filename);
// } catch(e) {'';}
// // let installSettings = fs.readFileSync('unattend.xml');
}
// let out_file = `Python-Installer/${filename}`
// await new Promise(resolve => {
// request({
// uri: url,
// encoding: null
// }, function (error, response, body) {
// fs.writeFileSync(out_file, body, {});
// resolve();
// });
// });
// console.log('Download Complete!');
// switch (os.platform())
// {
// case 'win32': {
// try {
// let installerPath = path.join(__dirname, '..', 'Python-Installer/', filename);
// let tempPath = path.join(__dirname, '..', 'Temp-Python/');
// let targetPath = path.join(__dirname, '..', 'Python/');
// execSync(`${installerPath} /passive DefaultJustForMeTargetDir=${tempPath}`, {
// stdio: "inherit"
// });
// console.log("Creating Portable Python Directory");
// copydir.sync(tempPath, targetPath);
// console.log("Portable Directory Created!");
// console.log("Uninstalling Unnecessary Python Files");
// execSync(`${installerPath} /passive /uninstall`, {
// stdio: 'inherit'
// });
// fs.rmdirSync(tempPath);
// console.log("PordaPy Installed!");
// } catch (err) {
// console.log(err);
// }
// break;
// }
// }
}
download('2.7.14');

View File

@ -1,13 +1,31 @@
module.exports = {
versions
}
// module.exports = { THIS IS AT THE BOTTOM SO NO ERROREROS WHILE TESTING
// versions
// }
const request = require('request');
const os = require('os');
// let matches = [];
let versions = function() {
new Promise(resolve => {
async function validateVersion(v) {
let availVer = await versions;
return availVer.includes(v);
}
async function getDownloadLink(v) {
switch (os.platform())
{
case 'darwin':
break;
case 'linux':
break;
case 'win32':
return 'https://www.python.org/ftp/python/' + v.toString() + '/python-' + v.toString() + '.exe';
default:
break;
}
}
let versions = new Promise(resolve => {
request({
uri: "https://www.python.org/ftp/python/",
}, function (error, response, body) {
@ -16,7 +34,59 @@ let versions = function() {
matches = matches.map((value) => {
return value.slice(1, -2);
});
resolve(matches);
});
})
}
})
let current = new Promise(resolve => {
request({
uri: "https://www.python.org/downloads/",
}, function (error, response, body) {
let current_os = '';
switch (os.platform())
{
case 'darwin':
current_os = "download-os-mac-osx";
break;
case 'linux':
current_os = "download-os-source";
break;
case 'win32':
current_os = "download-os-windows";
break;
default:
current_os = "download-os-source";
break;
}
let current_download_start = body.indexOf("Download Python ",body.indexOf(current_os)) + 16;
let current_download_end = body.indexOf('</a>', current_download_start);
let current_download = body.substring(current_download_start, current_download_end);
let ver = current_download;
resolve(ver);
});
});
current.then(value => {
console.log(value);
})
// versions.then(value => {
// let i = 0;
// for (i = 0; i < value.length; i++){
// console.log(value[i]);
// }
// })
module.exports = {
versions,
current,
validateVersion,
getDownloadLink
}