Skip to content
Open
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
48 changes: 43 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const BbPromise = require('bluebird');
const _ = require('lodash');
const Fse = require('fs-extra');
const minimatch = require('minimatch')
const Path = require('path');
const ChildProcess = require('child_process');
const zipper = require('zip-local');
Expand All @@ -28,10 +29,19 @@ class PkgPyFuncs {
config.buildDir ? this.buildDir = config.buildDir : this.error("No buildDir configuration specified")
this.globalRequirements = config.globalRequirements || []
this.globalIncludes = config.globalIncludes || []
this.globalExcludes = config.globalExcludes || []
config.cleanup === undefined ? this.cleanup = true : this.cleanup = config.cleanup
this.useDocker = config.useDocker || false
this.dockerImage = config.dockerImage || `lambci/lambda:build-${this.serverless.service.provider.runtime}`
this.containerName = config.containerName || 'serverless-package-python-functions'
this.pipArgs = config.pipCmdExtraArgs || []

this.defaultExcludes = [
"**/.serverless",
"**/node_modules",
"**/package.json",
"**/package-lock.json"
]
}

clean(){
Expand All @@ -51,7 +61,7 @@ class PkgPyFuncs {
}

selectAll() {
const functions = this.serverless.service.functions
const functions = _.pickBy(this.serverless.service.functions, (value, key) => !this.options.function || key === this.options.function )
const info = _.map(functions, (target) => {
return {
name: target.name,
Expand All @@ -75,7 +85,16 @@ class PkgPyFuncs {
}

let cmd = 'pip'
let args = ['install','--upgrade','-t', upath.normalize(buildPath), '-r', upath.normalize(requirementsPath)]
const out = this.runProcess(cmd, ['-V'])
const pipVersion = out.split(" ")[1]

let args = ['install','--disable-pip-version-check','--upgrade','-t', upath.normalize(buildPath), '-r', upath.normalize(requirementsPath)]
if (pipVersion.split('.')[0] >= 10) {
args.push('--no-warn-conflicts')
}
if (this.pipArgs){
args = _.concat(args, this.pipArgs)
}
if ( this.useDocker === true ){
cmd = 'docker'
args = ['exec',this.containerName, 'pip', ...args]
Expand Down Expand Up @@ -150,12 +169,26 @@ class PkgPyFuncs {
if (this.globalIncludes){
includes = _.concat(includes, this.globalIncludes)
}
_.forEach(includes, (item) => { Fse.copySync(item, buildPath) } )
let excludes = target.excludes || []
excludes = _.concat(excludes, this.defaultExcludes)
if (this.globalExcludes){
excludes = _.concat(excludes, this.globalExcludes)
}

let filter = (src, dest) => {
for(var i = 0; i < excludes.length; i++) {
if (minimatch(src, excludes[i])){
return false
}
}
return true
}
_.forEach(includes, (item) => { Fse.copySync(item, buildPath, filter) } )

// Install requirements
let requirements = [requirementsPath]
if (this.globalRequirements){
requirements = _.concat(requirements, this.globalRequirements)
requirements = _.concat(this.globalRequirements, requirements)
}
_.forEach(requirements, (req) => { this.installRequirements(buildPath,req)})
zipper.sync.zip(buildPath).compress().save(`${buildPath}.zip`)
Expand All @@ -173,7 +206,12 @@ class PkgPyFuncs {
.then(this.setupDocker)
.then(this.selectAll)
.map(this.makePackage),

'deploy:function:packageFunction': () => BbPromise.bind(this)
.then(this.fetchConfig)
.then( () => { Fse.ensureDirAsync(this.buildDir) })
.then(this.setupDocker)
.then(this.selectAll)
.map(this.makePackage),
'after:deploy:deploy': () => BbPromise.bind(this)
.then(this.clean)
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"bluebird": "^3.5.0",
"fs-extra": "^3.0.0",
"lodash": "^4.17.4",
"minimatch": "^3.0.4",
"upath": "^1.0.0",
"zip-local": "^0.3.4"
},
Expand Down