Skip to content

Commit a6b9009

Browse files
committed
Remove sys.path change requirement! closes #1
1 parent 841dacf commit a6b9009

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![npm](https://nodei.co/npm/serverless-python-requirements.png?downloads=true&downloadRank=true)](https://www.npmjs.com/package/serverless-python-requirements)
66

77
A Serverless v1.0 plugin to automatically bundle dependencies from
8-
`requirements.txt`.
8+
`requirements.txt` and make them available in your `PYTHONPATH`.
99

1010

1111
## Install
@@ -24,6 +24,21 @@ plugins:
2424
2525
## Adding the dependencies to `sys.path`
2626

27+
### Automatic
28+
The default behavior of this plugin is to link libraries into the working tree
29+
during deployment so that they are in your handler's `PYTHONPATH` when running
30+
on lambda.
31+
32+
### Manual
33+
This method is required when using [ZipImport](#zipimport) support and can be
34+
enabled manually by adding the following option to your config:
35+
36+
```yaml
37+
custom:
38+
pythonRequirements:
39+
link: false
40+
```
41+
2742
`serverless-python-requirements` adds a module called `requirements` to your
2843
puck. To easily make the bundled dependencies available, simply import it. Eg.
2944
add this to the top of any file using dependencies specified in your

index.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ServerlessPythonRequirements {
2424
return BbPromise.resolve();
2525
}
2626

27-
this.serverless.cli.log('Packaging required Python packages...');
27+
this.serverless.cli.log('Installing required Python packages...');
2828

2929
return new BbPromise((resolve, reject) => {
3030
let cmd = 'pip';
@@ -56,6 +56,7 @@ class ServerlessPythonRequirements {
5656
return this.installRequirements().then(() => {
5757
return new BbPromise((resolve, reject) => {
5858
if (this.custom.zipImport) {
59+
this.serverless.cli.log('Zipping required Python packages...');
5960
const zip = new Zip();
6061
zip.addLocalFolder('.requirements', '');
6162
zip.writeZip('.requirements.zip');
@@ -65,6 +66,21 @@ class ServerlessPythonRequirements {
6566
});
6667
}
6768

69+
linkRequirements() {
70+
if (!this.custom.zipImport && this.custom.link) {
71+
this.serverless.cli.log('Linking required Python packages...');
72+
fse.readdirSync('.requirements').map(file =>
73+
fse.symlinkSync(`.requirements/${file}`, `./${file}`));
74+
}
75+
}
76+
77+
unlinkRequirements() {
78+
if (!this.custom.zipImport && this.custom.link) {
79+
this.serverless.cli.log('Unlinking required Python packages...');
80+
fse.readdirSync('.requirements').map(file => fse.unlinkSync(file));
81+
}
82+
}
83+
6884
cleanup() {
6985
const artifacts = ['requirements.py'];
7086
if (this.custom.zipImport)
@@ -79,7 +95,11 @@ class ServerlessPythonRequirements {
7995
constructor(serverless, options) {
8096
this.serverless = serverless;
8197
this.options = options;
82-
this.custom = this.serverless.service.custom && this.serverless.service.custom.pythonRequirements || {};
98+
this.custom = Object.assign({
99+
zipImport: false,
100+
link: true,
101+
}, this.serverless.service.custom &&
102+
this.serverless.service.custom.pythonRequirements || {});
83103

84104
this.commands = {
85105
'requirements': {
@@ -103,14 +123,19 @@ class ServerlessPythonRequirements {
103123
this.hooks = {
104124
'before:deploy:createDeploymentArtifacts': () => BbPromise.bind(this)
105125
.then(this.packVendorHelper)
106-
.then(this.packRequirements),
126+
.then(this.packRequirements)
127+
.then(this.linkRequirements),
128+
129+
'after:deploy:createDeploymentArtifacts': () => BbPromise.bind(this)
130+
.then(this.unlinkRequirements),
107131

108132
'requirements:install:install': () => BbPromise.bind(this)
109133
.then(this.packVendorHelper)
110134
.then(this.packRequirements),
111135

112136
'requirements:clean:clean': () => BbPromise.bind(this)
113137
.then(this.cleanup)
138+
.then(this.unlinkRequirements),
114139
};
115140
}
116141
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-python-requirements",
3-
"version": "1.2.0",
3+
"version": "2.0.0-beta",
44
"engines": {
55
"node": ">=4.0"
66
},

0 commit comments

Comments
 (0)