Skip to content

Commit 3d030a5

Browse files
authored
Merge branch 'master' into package_individually_like_base_sls
2 parents 35b0ce2 + eabbd89 commit 3d030a5

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ It is important that the host of your private repositories has already been adde
7272
`$HOME/.ssh/known_hosts` file, as the install process will fail otherwise due to host authenticity
7373
failure.
7474

75+
You can also pass environment variables to docker by specifying them in `dockerEnv`
76+
option:
77+
```yaml
78+
custom:
79+
pythonRequirements:
80+
dockerEnv:
81+
- https_proxy
82+
```
83+
7584
[:checkered_flag: Windows notes](#checkered_flag-windows-dockerizepip-notes)
7685

7786
## Pipenv support :sparkles::cake::sparkles:

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class ServerlessPythonRequirements {
4242
dockerSsh: false,
4343
dockerImage: null,
4444
dockerFile: null,
45+
dockerEnv: false,
4546
useStaticCache: false,
4647
useDownloadCache: false,
4748
cacheLocation: false,

lib/pip.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ function installRequirements(targetFolder, serverless, options) {
196196
pipCmd.push('--cache-dir', quote_single(dockerDownloadCacheDir));
197197
}
198198

199+
if (options.dockerEnv) {
200+
// Add environment variables to docker run cmd
201+
options.dockerEnv.forEach(function(item) {
202+
cmdOptions.push('-e', item);
203+
});
204+
}
205+
199206
if (process.platform === 'linux') {
200207
// Use same user so requirements folder is not root and so --cache-dir works
201208
var commands = [];

test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const mkCommand = cmd => (args, options = {}) => {
3636
const sls = mkCommand('sls');
3737
const git = mkCommand('git');
3838
const npm = mkCommand('npm');
39+
const perl = mkCommand('perl');
3940

4041
const setup = () => {
4142
removeSync(getUserCachePath());
@@ -81,6 +82,12 @@ const getPythonBin = (version = 3) => {
8182

8283
const listZipFiles = filename =>
8384
Object.keys(deasync(new JSZip().loadAsync(readFileSync(filename))).files);
85+
const listRequirementsZipFiles = filename => {
86+
const zip = deasync(new JSZip().loadAsync(readFileSync(filename)));
87+
const reqsBuffer = deasync(zip.file('.requirements.zip').async('nodebuffer'));
88+
const reqsZip = deasync(new JSZip().loadAsync(reqsBuffer));
89+
return Object.keys(reqsZip.files)
90+
};
8491

8592
test('default pythonBin can package flask with default options', t => {
8693
process.chdir('tests/base');
@@ -135,6 +142,7 @@ test('py3.6 can package flask with slim option', t => {
135142
t.end();
136143
});
137144

145+
138146
test('can package individually without moving modules to root of zip-File', t => {
139147
process.chdir('tests/base');
140148
const path = npm(['pack', '../..']);
@@ -150,3 +158,29 @@ test('can package individually without moving modules to root of zip-File', t =>
150158
t.true(zipfiles.includes(`flask${sep}__init__.py`), 'flask is packaged');
151159
t.end();
152160
});
161+
162+
/*
163+
* News tests not in test.bats
164+
*/
165+
166+
test("py3.6 doesn't package bottle with zip option", t => {
167+
process.chdir('tests/base');
168+
const path = npm(['pack', '../..']);
169+
npm(['i', path]);
170+
perl(['-p', "-i'.bak'", '-e', 's/(pythonRequirements:$)/\\1\\n noDeploy: [bottle]/', 'serverless.yml'])
171+
sls([`--pythonBin=${getPythonBin(3)}`, '--zip=true', 'package']);
172+
const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip');
173+
const zippedReqs = listRequirementsZipFiles('.serverless/sls-py-req-test.zip');
174+
t.true(
175+
zipfiles.includes('.requirements.zip'),
176+
'zipped requirements are packaged'
177+
);
178+
t.true(zipfiles.includes(`unzip_requirements.py`), 'unzip util is packaged');
179+
t.false(
180+
zipfiles.includes(`flask${sep}__init__.py`),
181+
"flask isn't packaged on its own"
182+
);
183+
t.true(zippedReqs.includes(`flask/__init__.py`), 'flask is packaged in the .requirements.zip file');
184+
t.false(zippedReqs.includes(`bottle.py`), 'bottle is not packaged in the .requirements.zip file');
185+
t.end();
186+
});

0 commit comments

Comments
 (0)