Skip to content

Commit 5893b3f

Browse files
authored
feat(lambda-python-alpha): add without-urls option for poetry (#27442)
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*
1 parent b898d3b commit 5893b3f

File tree

17 files changed

+40796
-2410
lines changed

17 files changed

+40796
-2410
lines changed

packages/@aws-cdk/aws-lambda-python-alpha/README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Packaging is executed using the `Packaging` class, which:
111111

112112
**Excluding source files**
113113

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

116116
```ts
117117
new python.PythonFunction(this, 'function', {
@@ -124,6 +124,33 @@ new python.PythonFunction(this, 'function', {
124124
});
125125
```
126126

127+
**Including hashes**
128+
129+
You can include hashes in `poetry` using the optional boolean parameter `poetryIncludeHashes`:
130+
131+
```ts
132+
new python.PythonFunction(this, 'function', {
133+
entry: '/path/to/poetry-function',
134+
runtime: Runtime.PYTHON_3_8,
135+
bundling: {
136+
poetryIncludeHashes: true,
137+
},
138+
});
139+
```
140+
141+
**Excluding URLs**
142+
143+
You can exclude URLs in `poetry` using the optional boolean parameter `poetryWithoutUrls`:
144+
145+
```ts
146+
new python.PythonFunction(this, 'function', {
147+
entry: '/path/to/poetry-function',
148+
runtime: Runtime.PYTHON_3_8,
149+
bundling: {
150+
poetryWithoutUrls: true,
151+
},
152+
});
153+
```
127154

128155
## Custom Bundling
129156

packages/@aws-cdk/aws-lambda-python-alpha/lib/bundling.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export class Bundling implements CdkBundlingOptions {
8282
outputPathSuffix = '',
8383
image,
8484
poetryIncludeHashes,
85+
poetryWithoutUrls,
8586
commandHooks,
8687
assetExcludes = [],
8788
} = props;
@@ -93,6 +94,7 @@ export class Bundling implements CdkBundlingOptions {
9394
inputDir: AssetStaging.BUNDLING_INPUT_DIR,
9495
outputDir: outputPath,
9596
poetryIncludeHashes,
97+
poetryWithoutUrls,
9698
commandHooks,
9799
assetExcludes,
98100
});
@@ -117,7 +119,7 @@ export class Bundling implements CdkBundlingOptions {
117119
}
118120

119121
private createBundlingCommand(options: BundlingCommandOptions): string[] {
120-
const packaging = Packaging.fromEntry(options.entry, options.poetryIncludeHashes);
122+
const packaging = Packaging.fromEntry(options.entry, options.poetryIncludeHashes, options.poetryWithoutUrls);
121123
let bundlingCommands: string[] = [];
122124
bundlingCommands.push(...options.commandHooks?.beforeBundling(options.inputDir, options.outputDir) ?? []);
123125
const exclusionStr = options.assetExcludes?.map(item => `--exclude='${item}'`).join(' ');
@@ -140,6 +142,7 @@ interface BundlingCommandOptions {
140142
readonly outputDir: string;
141143
readonly assetExcludes?: string[];
142144
readonly poetryIncludeHashes?: boolean;
145+
readonly poetryWithoutUrls?: boolean;
143146
readonly commandHooks?: ICommandHooks;
144147
}
145148

packages/@aws-cdk/aws-lambda-python-alpha/lib/packaging.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,16 @@ export interface PoetryPackagingProps {
2727
* export with a hash.
2828
*
2929
* @see https://github.com/aws/aws-cdk/issues/19232
30-
* @default Hashes are NOT included in the exported `requirements.txt` file
30+
* @default Hashes are NOT included in the exported `requirements.txt` file.
3131
*/
3232
readonly poetryIncludeHashes?: boolean;
33+
34+
/**
35+
* Whether to export Poetry dependencies with source repository urls.
36+
*
37+
* @default URLs are included in the exported `requirements.txt` file.
38+
*/
39+
readonly poetryWithoutUrls?: boolean;
3340
}
3441

3542
export class Packaging {
@@ -65,6 +72,7 @@ export class Packaging {
6572
exportCommand: [
6673
'poetry', 'export',
6774
...props?.poetryIncludeHashes ? [] : ['--without-hashes'],
75+
...props?.poetryWithoutUrls ? ['--without-urls'] : [],
6876
'--with-credentials',
6977
'--format', DependenciesFile.PIP,
7078
'--output', DependenciesFile.PIP,
@@ -79,11 +87,11 @@ export class Packaging {
7987
return new Packaging({ dependenciesFile: DependenciesFile.NONE });
8088
}
8189

82-
public static fromEntry(entry: string, poetryIncludeHashes?: boolean): Packaging {
90+
public static fromEntry(entry: string, poetryIncludeHashes?: boolean, poetryWithoutUrls?: boolean): Packaging {
8391
if (fs.existsSync(path.join(entry, DependenciesFile.PIPENV))) {
8492
return this.withPipenv();
8593
} if (fs.existsSync(path.join(entry, DependenciesFile.POETRY))) {
86-
return this.withPoetry({ poetryIncludeHashes });
94+
return this.withPoetry({ poetryIncludeHashes, poetryWithoutUrls });
8795
} else if (fs.existsSync(path.join(entry, DependenciesFile.PIP))) {
8896
return this.withPip();
8997
} else {
@@ -97,4 +105,4 @@ export class Packaging {
97105
this.dependenciesFile = props.dependenciesFile;
98106
this.exportCommand = props.exportCommand;
99107
}
100-
}
108+
}

packages/@aws-cdk/aws-lambda-python-alpha/lib/types.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@ export interface BundlingOptions extends DockerRunOptions {
1515
readonly poetryIncludeHashes?: boolean;
1616

1717
/**
18-
* List of file patterns to exclude when copying assets from source for bundling.
19-
*
20-
* @default - Empty list
21-
*/
18+
* Whether to export Poetry dependencies with source repository urls.
19+
*
20+
* @default URLs are included in the exported `requirements.txt` file.
21+
*/
22+
readonly poetryWithoutUrls?: boolean;
23+
24+
/**
25+
* List of file patterns to exclude when copying assets from source for bundling.
26+
*
27+
* @default - Empty list
28+
*/
2229
readonly assetExcludes?: string[];
2330

2431
/**

packages/@aws-cdk/aws-lambda-python-alpha/test/bundling.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,34 @@ test('Bundling a function with poetry dependencies, with hashes', () => {
331331
expect(files).toContain('.ignorefile');
332332
});
333333

334+
test('Bundling a function with poetry dependencies, without urls', () => {
335+
const entry = path.join(__dirname, 'lambda-handler-poetry');
336+
337+
const assetCode = Bundling.bundle({
338+
entry: path.join(entry, '.'),
339+
runtime: Runtime.PYTHON_3_9,
340+
architecture: Architecture.X86_64,
341+
outputPathSuffix: 'python',
342+
poetryWithoutUrls: true,
343+
});
344+
345+
expect(Code.fromAsset).toHaveBeenCalledWith(entry, expect.objectContaining({
346+
bundling: expect.objectContaining({
347+
command: [
348+
'bash', '-c',
349+
'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',
350+
],
351+
}),
352+
}));
353+
354+
const files = fs.readdirSync(assetCode.path);
355+
expect(files).toContain('index.py');
356+
expect(files).toContain('pyproject.toml');
357+
expect(files).toContain('poetry.lock');
358+
// Contains hidden files.
359+
expect(files).toContain('.ignorefile');
360+
});
361+
334362
test('Bundling a function with custom bundling image', () => {
335363
const entry = path.join(__dirname, 'lambda-handler-custom-build');
336364
const image = DockerImage.fromBuild(path.join(entry));

0 commit comments

Comments
 (0)