Skip to content

Ability to have separate local / live builds #8

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
45 changes: 42 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ class ServerlessPythonRequirements {
return new BbPromise((resolve, reject) => {
let cmd = 'pip';
let options = [
'install',
'--isolated', 'install',
'-t', '.requirements',
'-r', 'requirements.txt',
'-U', '--upgrade-strategy=only-if-needed'
];
if (this.serverless.service.custom &&
this.serverless.service.custom.dockerizePip) {
Expand All @@ -52,8 +53,34 @@ class ServerlessPythonRequirements {
});
};

packLocalRequirements() {
if (!fse.existsSync(path.join(this.serverless.config.servicePath, 'requirements.txt'))) {
return BbPromise.resolve();
}

this.serverless.cli.log('Packaging required Python packages locally...');

return new BbPromise((resolve, reject) => {
let cmd = 'pip';
let options = [
'--isolated', 'install',
'-t', '.local_requirements',
'-r', 'requirements.txt',
'-U', '--upgrade-strategy=only-if-needed'
];
const res = child_process.spawnSync(cmd, options);
if (res.error) {
return reject(res.error);
}
if (res.status != 0) {
return reject(res.stderr);
}
resolve();
});
};

cleanup() {
const artifacts = ['requirements.py', '.requirements'];
const artifacts = ['requirements.py', '.requirements', '.local_requirements'];

return BbPromise.all(_.map(artifacts, (artifact) =>
fse.removeAsync(path.join(this.serverless.config.servicePath, artifact))));;
Expand Down Expand Up @@ -87,10 +114,18 @@ class ServerlessPythonRequirements {
],
},
'install': {
usage: 'install requirements manually',
usage: 'Install requirements manually',
lifecycleEvents: [
'install',
],
commands: {
'local': {
usage: 'Install requirements using host machine (not docker) into .local_requirements',
lifecycleEvents: [
'installLocal'
]
}
},
},
},
},
Expand All @@ -105,6 +140,10 @@ class ServerlessPythonRequirements {
.then(this.packVendorHelper)
.then(this.packRequirements),

'requirements:install:local:installLocal': () => BbPromise.bind(this)
.then(this.packVendorHelper)
.then(this.packLocalRequirements),

'requirements:clean:clean': () => BbPromise.bind(this)
.then(this.cleanup)
};
Expand Down
10 changes: 8 additions & 2 deletions requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@
'.requirements',
)

if requirements not in sys.path:
sys.path.append(requirements)
local_requirements = os.path.join(
os.path.split(__file__)[0],
'.local_requirements',
)

for path in [requirements, local_requirements]:
if os.path.isdir(path) and path not in sys.path:
sys.path.insert(1, path)