Skip to content

Commit

Permalink
Simple heuristics to determine python executable name across platforms (
Browse files Browse the repository at this point in the history
#2247)

* Simple heuristics to determine python executable name across platforms.  Fixes #2181.

* Remove hardcoded reference to python3 in output to user.  Fix indentation problem.  Clarify some things in README files.

* Fixing linter issues
  • Loading branch information
garnaat authored Apr 11, 2019
1 parent f216f96 commit ad7425f
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,17 @@ This is a blank project for Python development with CDK.

The `cdk.json` file tells the CDK Toolkit how to execute your app.

This project is set up like a standard Python project. The initialization process also creates
a virtualenv within this project, stored under the .env directory. To create the virtualenv
it assumes that there is a `python3` executable in your path with access to the `venv` package.
If for any reason the automatic creation of the virtualenv fails, you can create the virtualenv
manually once the init process completes.
This project is set up like a standard Python project. The initialization
process also creates a virtualenv within this project, stored under the .env
directory. To create the virtualenv it assumes that there is a `python3`
(or `python` for Windows) executable in your path with access to the `venv`
package. If for any reason the automatic creation of the virtualenv fails,
you can create the virtualenv manually.

To manually create a virtualenv on MacOS and Linux:

```
$ python3 -m venv .env
```

For Windows, use:

```
% python -m venv .env
$ %python-executable% -m venv .env
```

After the init process completes and the virtualenv is created, you can use the following
Expand Down Expand Up @@ -48,8 +43,8 @@ At this point you can now synthesize the CloudFormation template for this code.
$ cdk synth
```

To add additional dependencies, for example other CDK libraries, just add to
your requirements.txt file and rerun the `pip install -r requirements.txt`
To add additional dependencies, for example other CDK libraries, just add
them to your `setup.py` file and rerun the `pip install -r requirements.txt`
command.

# Useful commands
Expand Down
3 changes: 0 additions & 3 deletions packages/aws-cdk/lib/init-templates/app/python/cdk.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "%python-executable% app.py"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@ manually once the init process completes.
To manually create a virtualenv on MacOS and Linux:

```
$ python3 -m venv .env
```

For Windows, use:

```
% python -m venv .env
$ %python-executable% -m venv .env
```

After the init process completes and the virtualenv is created, you can use the following
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "%python-executable% app.py"
}
30 changes: 19 additions & 11 deletions packages/aws-cdk/lib/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ export async function cliInit(type?: string, language?: string, canUseNetwork?:
await initializeProject(template, language, canUseNetwork !== undefined ? canUseNetwork : true);
}

/**
* Returns the name of the Python executable for this OS
*/
function pythonExecutable() {
let python = 'python3';
if (process.platform === 'win32') {
python = 'python';
}
return python;
}
const INFO_DOT_JSON = 'info.json';

export class InitTemplate {
Expand Down Expand Up @@ -143,7 +153,8 @@ export class InitTemplate {
.replace(/%name\.PascalCased%/g, camelCase(project.name, { pascalCase: true }))
.replace(/%cdk-version%/g, cdkVersion)
.replace(/%cdk-home%/g, CDK_HOME)
.replace(/%name\.PythonModule%/g, project.name.replace(/-/g, '_'));
.replace(/%name\.PythonModule%/g, project.name.replace(/-/g, '_'))
.replace(/%python-executable%/g, pythonExecutable());
}
}

Expand Down Expand Up @@ -233,7 +244,7 @@ async function postInstall(language: string, canUseNetwork: boolean) {
case 'java':
return await postInstallJava(canUseNetwork);
case 'python':
return await postInstallPython(canUseNetwork);
return await postInstallPython();
}
}

Expand Down Expand Up @@ -263,17 +274,14 @@ async function postInstallJava(canUseNetwork: boolean) {
await execute('mvn', 'package');
}

async function postInstallPython(canUseNetwork: boolean) {
if (!canUseNetwork) {
print(`Please run ${colors.green('python -m venv .env')}!`);
return;
}

print(`Executing ${colors.green('python -m venv .env')}`);
async function postInstallPython() {
const python = pythonExecutable();
print(`Executing ${colors.green('Creating virtualenv...')}`);
try {
await execute('python3', '-m venv', '.env');
await execute(python, '-m venv', '.env');
} catch (e) {
throw new Error(`${colors.green('python3 -m venv .env')} failed: ` + e.message);
print('Unable to create virtualenv automatically');
print(`Please run ${colors.green(python + ' -m venv .env')}!`);
}
}

Expand Down

0 comments on commit ad7425f

Please sign in to comment.