Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lambda-python-alpha): add without-urls option for poetry #27442

Merged
merged 6 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 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,20 @@ new python.PythonFunction(this, 'function', {
});
```

**Including hashes and excluding URLs**

You can include hashes and exclude URLs in `poetry` as follows:

```ts
new python.PythonFunction(this, 'function', {
entry: '/path/to/poetry-function',
runtime: Runtime.PYTHON_3_8,
bundling: {
poetryIncludeHashes: true,
poetryWithoutUrls: true,
},
});
```
msambol marked this conversation as resolved.
Show resolved Hide resolved

msambol marked this conversation as resolved.
Show resolved Hide resolved
## 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: 21 additions & 7 deletions packages/@aws-cdk/aws-lambda-python-alpha/test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ test('Bundling a function with poetry and no assetExcludes', () => {
test('Bundling a function with poetry dependencies, with hashes', () => {
const entry = path.join(__dirname, 'lambda-handler-poetry');

const assetCode = Bundling.bundle({
Bundling.bundle({
entry: path.join(entry, '.'),
runtime: Runtime.PYTHON_3_9,
architecture: Architecture.X86_64,
Expand All @@ -322,13 +322,27 @@ test('Bundling a function with poetry dependencies, with hashes', () => {
],
}),
}));
});

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');
msambol marked this conversation as resolved.
Show resolved Hide resolved
test('Bundling a function with poetry dependencies, without urls', () => {
const entry = path.join(__dirname, 'lambda-handler-poetry');

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',
],
}),
}));
});

test('Bundling a function with custom bundling image', () => {
Expand Down
Loading
Loading