Skip to content

Commit

Permalink
feat(lambda-python-alpha): add without-urls option for poetry (#27442)
Browse files Browse the repository at this point in the history
Added `without-urls` option for poetry. Also added Python versions for the integration tests.

Closes #27103.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
msambol authored Oct 10, 2023
1 parent b898d3b commit 5893b3f
Show file tree
Hide file tree
Showing 17 changed files with 40,796 additions and 2,410 deletions.
29 changes: 28 additions & 1 deletion packages/@aws-cdk/aws-lambda-python-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Packaging is executed using the `Packaging` class, which:

**Excluding source files**

You can exclude files from being copied using the optional bundling string array parameter `assetExcludes`
You can exclude files from being copied using the optional bundling string array parameter `assetExcludes`:

```ts
new python.PythonFunction(this, 'function', {
Expand All @@ -124,6 +124,33 @@ new python.PythonFunction(this, 'function', {
});
```

**Including hashes**

You can include hashes in `poetry` using the optional boolean parameter `poetryIncludeHashes`:

```ts
new python.PythonFunction(this, 'function', {
entry: '/path/to/poetry-function',
runtime: Runtime.PYTHON_3_8,
bundling: {
poetryIncludeHashes: true,
},
});
```

**Excluding URLs**

You can exclude URLs in `poetry` using the optional boolean parameter `poetryWithoutUrls`:

```ts
new python.PythonFunction(this, 'function', {
entry: '/path/to/poetry-function',
runtime: Runtime.PYTHON_3_8,
bundling: {
poetryWithoutUrls: true,
},
});
```

## Custom Bundling

Expand Down
5 changes: 4 additions & 1 deletion packages/@aws-cdk/aws-lambda-python-alpha/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class Bundling implements CdkBundlingOptions {
outputPathSuffix = '',
image,
poetryIncludeHashes,
poetryWithoutUrls,
commandHooks,
assetExcludes = [],
} = props;
Expand All @@ -93,6 +94,7 @@ export class Bundling implements CdkBundlingOptions {
inputDir: AssetStaging.BUNDLING_INPUT_DIR,
outputDir: outputPath,
poetryIncludeHashes,
poetryWithoutUrls,
commandHooks,
assetExcludes,
});
Expand All @@ -117,7 +119,7 @@ export class Bundling implements CdkBundlingOptions {
}

private createBundlingCommand(options: BundlingCommandOptions): string[] {
const packaging = Packaging.fromEntry(options.entry, options.poetryIncludeHashes);
const packaging = Packaging.fromEntry(options.entry, options.poetryIncludeHashes, options.poetryWithoutUrls);
let bundlingCommands: string[] = [];
bundlingCommands.push(...options.commandHooks?.beforeBundling(options.inputDir, options.outputDir) ?? []);
const exclusionStr = options.assetExcludes?.map(item => `--exclude='${item}'`).join(' ');
Expand All @@ -140,6 +142,7 @@ interface BundlingCommandOptions {
readonly outputDir: string;
readonly assetExcludes?: string[];
readonly poetryIncludeHashes?: boolean;
readonly poetryWithoutUrls?: boolean;
readonly commandHooks?: ICommandHooks;
}

Expand Down
16 changes: 12 additions & 4 deletions packages/@aws-cdk/aws-lambda-python-alpha/lib/packaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ export interface PoetryPackagingProps {
* export with a hash.
*
* @see https://github.com/aws/aws-cdk/issues/19232
* @default Hashes are NOT included in the exported `requirements.txt` file
* @default Hashes are NOT included in the exported `requirements.txt` file.
*/
readonly poetryIncludeHashes?: boolean;

/**
* Whether to export Poetry dependencies with source repository urls.
*
* @default URLs are included in the exported `requirements.txt` file.
*/
readonly poetryWithoutUrls?: boolean;
}

export class Packaging {
Expand Down Expand Up @@ -65,6 +72,7 @@ export class Packaging {
exportCommand: [
'poetry', 'export',
...props?.poetryIncludeHashes ? [] : ['--without-hashes'],
...props?.poetryWithoutUrls ? ['--without-urls'] : [],
'--with-credentials',
'--format', DependenciesFile.PIP,
'--output', DependenciesFile.PIP,
Expand All @@ -79,11 +87,11 @@ export class Packaging {
return new Packaging({ dependenciesFile: DependenciesFile.NONE });
}

public static fromEntry(entry: string, poetryIncludeHashes?: boolean): Packaging {
public static fromEntry(entry: string, poetryIncludeHashes?: boolean, poetryWithoutUrls?: boolean): Packaging {
if (fs.existsSync(path.join(entry, DependenciesFile.PIPENV))) {
return this.withPipenv();
} if (fs.existsSync(path.join(entry, DependenciesFile.POETRY))) {
return this.withPoetry({ poetryIncludeHashes });
return this.withPoetry({ poetryIncludeHashes, poetryWithoutUrls });
} else if (fs.existsSync(path.join(entry, DependenciesFile.PIP))) {
return this.withPip();
} else {
Expand All @@ -97,4 +105,4 @@ export class Packaging {
this.dependenciesFile = props.dependenciesFile;
this.exportCommand = props.exportCommand;
}
}
}
15 changes: 11 additions & 4 deletions packages/@aws-cdk/aws-lambda-python-alpha/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ export interface BundlingOptions extends DockerRunOptions {
readonly poetryIncludeHashes?: boolean;

/**
* List of file patterns to exclude when copying assets from source for bundling.
*
* @default - Empty list
*/
* Whether to export Poetry dependencies with source repository urls.
*
* @default URLs are included in the exported `requirements.txt` file.
*/
readonly poetryWithoutUrls?: boolean;

/**
* List of file patterns to exclude when copying assets from source for bundling.
*
* @default - Empty list
*/
readonly assetExcludes?: string[];

/**
Expand Down
28 changes: 28 additions & 0 deletions packages/@aws-cdk/aws-lambda-python-alpha/test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,34 @@ test('Bundling a function with poetry dependencies, with hashes', () => {
expect(files).toContain('.ignorefile');
});

test('Bundling a function with poetry dependencies, without urls', () => {
const entry = path.join(__dirname, 'lambda-handler-poetry');

const assetCode = Bundling.bundle({
entry: path.join(entry, '.'),
runtime: Runtime.PYTHON_3_9,
architecture: Architecture.X86_64,
outputPathSuffix: 'python',
poetryWithoutUrls: true,
});

expect(Code.fromAsset).toHaveBeenCalledWith(entry, expect.objectContaining({
bundling: expect.objectContaining({
command: [
'bash', '-c',
'rsync -rLv /asset-input/ /asset-output/python && cd /asset-output/python && poetry export --without-hashes --without-urls --with-credentials --format requirements.txt --output requirements.txt && python -m pip install -r requirements.txt -t /asset-output/python',
],
}),
}));

const files = fs.readdirSync(assetCode.path);
expect(files).toContain('index.py');
expect(files).toContain('pyproject.toml');
expect(files).toContain('poetry.lock');
// Contains hidden files.
expect(files).toContain('.ignorefile');
});

test('Bundling a function with custom bundling image', () => {
const entry = path.join(__dirname, 'lambda-handler-custom-build');
const image = DockerImage.fromBuild(path.join(entry));
Expand Down
Loading

0 comments on commit 5893b3f

Please sign in to comment.