Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Use “conda” (if exists) to create virtual environment // Resolve #152 R…
Browse files Browse the repository at this point in the history
…esolve #174 Resolve #176 Resolve #209 Resolve #218
  • Loading branch information
ivankravets committed Feb 21, 2017
1 parent e7332c3 commit cecd555
Showing 1 changed file with 98 additions and 39 deletions.
137 changes: 98 additions & 39 deletions lib/installer/stages/platformio-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import tmp from 'tmp';

export default class PlatformIOCoreStage extends BaseStage {

static VIRTUALENV_UNKNOWN_TYPE = 0;
static VIRTUALENV_USER_TYPE = 1;
static VIRTUALENV_CONDA_TYPE = 2;

static vitrualenvUrl = 'https://pypi.python.org/packages/source/v/virtualenv/virtualenv-14.0.6.tar.gz';
static virtualenvArhivePath = path.join(getCacheDir(), 'virtualenv.tar.gz');

Expand Down Expand Up @@ -118,58 +122,95 @@ export default class PlatformIOCoreStage extends BaseStage {
return null;
}

createVirtualenv(pythonExecutable) {
async getVirtualenvType() {
let found = false;
// conda
found = await new Promise(resolve => {
utils.runCommand('conda', ['--version'], code => resolve(code === 0));
});
if (found) {
return PlatformIOCoreStage.VIRTUALENV_CONDA_TYPE;
}
// user's
found = await new Promise(resolve => {
utils.runCommand('virtualenv', ['--version'], code => resolve(code === 0));
});
if (found) {
return PlatformIOCoreStage.VIRTUALENV_USER_TYPE;
}
return PlatformIOCoreStage.VIRTUALENV_UNKNOWN_TYPE;
}

createVirtualenvWithConda() {
return new Promise((resolve, reject) => {
utils.runCommand(
'conda',
['create', '--yes', '--quiet', 'python=2', '--prefix', config.ENV_DIR],
(code, stdout, stderr) => {
if (code === 0) {
return resolve(stdout);
} else {
return reject(`Conda Virtualenv: ${stderr}`);
}
}
);
});
}

createVirtualenvWithUser(pythonExecutable) {
return new Promise((resolve, reject) => {
if (utils.isDir(config.ENV_DIR)) {
fs.removeSync(config.ENV_DIR);
}
// try to create with built-in virtualenv (if it is installed)
utils.runCommand(
config.IS_WINDOWS ? 'virtualenv.exe' : 'virtualenv',
'virtualenv',
['-p', pythonExecutable, config.ENV_DIR],
(code, stdout) => {
(code, stdout, stderr) => {
if (code === 0) {
return resolve(stdout);
} else {
// virtualenv is not installed or cmd failed
// download own version of virtualenv
this.downloadVirtualenv().then(archivePath => {
const tmpDir = tmp.dirSync({
unsafeCleanup: true
});
extractTarGz(archivePath, tmpDir.name).then(dstDir => {
const virtualenvScript = this.findFileByName('virtualenv.py', dstDir);
if (!virtualenvScript) {
return reject('Can not find virtualenv.py script');
}
utils.runCommand(
pythonExecutable,
[virtualenvScript, config.ENV_DIR],
(code, stdout, stderr) => {
if (code === 0) {
return resolve(stdout);
} else {
return reject(`Virtualenv: ${stderr}`);
}
}
);
});
}).catch(err => reject(`Virtualenv: ${err}`));
return reject(`User's Virtualenv: ${stderr}`);
}
}
);
});
}

createVirtualenvWithDownload(pythonExecutable) {
return new Promise((resolve, reject) => {
this.downloadVirtualenv().then(archivePath => {
const tmpDir = tmp.dirSync({
unsafeCleanup: true
});
extractTarGz(archivePath, tmpDir.name).then(dstDir => {
const virtualenvScript = this.findFileByName('virtualenv.py', dstDir);
if (!virtualenvScript) {
return reject('Can not find virtualenv.py script');
}
utils.runCommand(
pythonExecutable,
[virtualenvScript, config.ENV_DIR],
(code, stdout, stderr) => {
if (code === 0) {
return resolve(stdout);
} else {
return reject(`Virtualenv Download: ${stderr}`);
}
}
);
});
}).catch(err => reject(`Virtualenv Download: ${err}`));
});
}

installPIOCore() {
return new Promise((resolve, reject) => {
let cmd = 'pip';
const args = ['install', '--no-cache-dir', '-U'];
if (atom.config.get('platformio-ide.useDevelopmentPIOCore') || semver.prerelease(utils.getIDEVersion())) {
cmd = path.join(config.ENV_BIN_DIR, 'pip');
args.push('https://github.com/platformio/platformio/archive/develop.zip');
} else {
args.push('platformio');
}
utils.runCommand('pip', args, (code, stdout, stderr) => {
utils.runCommand(cmd, args, (code, stdout, stderr) => {
if (code === 0) {
resolve(stdout);
} else {
Expand Down Expand Up @@ -199,17 +240,35 @@ export default class PlatformIOCoreStage extends BaseStage {
if (this.status === BaseStage.STATUS_SUCCESSED) {
return true;
}
if (!atom.config.get('platformio-ide.useBuiltinPIOCore')) {
this.status = BaseStage.STATUS_SUCCESSED;
return true;
}
this.status = BaseStage.STATUS_INSTALLING;
const pythonExecutable = await this.whereIsPython();
if (!pythonExecutable) {
this.status = BaseStage.STATUS_FAILED;
throw new Error('Can not find Python Interpreter');

// remove existing virtual environment
if (utils.isDir(config.ENV_DIR)) {
fs.removeSync(config.ENV_DIR);
}

if (atom.config.get('platformio-ide.useBuiltinPIOCore')) {
await this.createVirtualenv(pythonExecutable);
await this.installPIOCore();
const venvType = await this.getVirtualenvType();
if (venvType === PlatformIOCoreStage.VIRTUALENV_CONDA_TYPE) {
await this.createVirtualenvWithConda();
} else {
const pythonExecutable = await this.whereIsPython();
if (!pythonExecutable) {
this.status = BaseStage.STATUS_FAILED;
throw new Error('Can not find Python Interpreter');
}
if (venvType === PlatformIOCoreStage.VIRTUALENV_USER_TYPE) {
await this.createVirtualenvWithUser(pythonExecutable);
} else {
await this.createVirtualenvWithDownload(pythonExecutable);
}
}

await this.installPIOCore();

this.status = BaseStage.STATUS_SUCCESSED;
return true;
}
Expand Down

0 comments on commit cecd555

Please sign in to comment.