@@ -11,12 +11,21 @@ const {zipDirectory} = require('./zipService');
11
11
BbPromise . promisifyAll ( fse ) ;
12
12
13
13
class ServerlessPythonRequirements {
14
- packVendorHelper ( ) {
15
- this . serverless . cli . log ( 'Installing Python requirements helper...' ) ;
14
+ addVendorHelper ( ) {
15
+ if ( this . custom . zip ) {
16
+ this . serverless . cli . log ( 'Removing Python requirements helper...' ) ;
16
17
17
- return fse . copyAsync (
18
- path . resolve ( __dirname , 'requirements.py' ) ,
19
- path . join ( this . serverless . config . servicePath , 'requirements.py' ) ) ;
18
+ return fse . copyAsync (
19
+ path . resolve ( __dirname , 'unzip_requirements.py' ) ,
20
+ path . join ( this . serverless . config . servicePath , 'unzip_requirements.py' ) ) ;
21
+ }
22
+ } ;
23
+
24
+ removeVendorHelper ( ) {
25
+ if ( this . custom . zip ) {
26
+ this . serverless . cli . log ( 'Adding Python requirements helper...' ) ;
27
+ return fse . removeAsync ( 'unzip_requirements.py' ) ;
28
+ }
20
29
} ;
21
30
22
31
installRequirements ( ) {
@@ -60,32 +69,34 @@ class ServerlessPythonRequirements {
60
69
61
70
packRequirements ( ) {
62
71
return this . installRequirements ( ) . then ( ( ) => {
63
- if ( this . custom . zipImport )
72
+ if ( this . custom . zip ) {
73
+ this . serverless . cli . log ( 'Zipping required Python packages...' ) ;
64
74
return zipDirectory ( '.requirements' , '.requirements.zip' ) ;
75
+ }
65
76
} ) ;
66
77
}
67
78
68
79
linkRequirements ( ) {
69
- if ( ! this . custom . zipImport && this . custom . link ) {
80
+ if ( ! this . custom . zip ) {
70
81
this . serverless . cli . log ( 'Linking required Python packages...' ) ;
71
82
fse . readdirSync ( '.requirements' ) . map ( file =>
72
83
fse . symlinkSync ( `.requirements/${ file } ` , `./${ file } ` ) ) ;
73
84
}
74
85
}
75
86
76
87
unlinkRequirements ( ) {
77
- if ( ! this . custom . zipImport && this . custom . link ) {
88
+ if ( ! this . custom . zip ) {
78
89
this . serverless . cli . log ( 'Unlinking required Python packages...' ) ;
79
90
fse . readdirSync ( '.requirements' ) . map ( file => fse . unlinkSync ( file ) ) ;
80
91
}
81
92
}
82
93
83
94
cleanup ( ) {
84
- const artifacts = [ 'requirements.py ' ] ;
85
- if ( this . custom . zipImport )
86
- artifacts . push ( '.requirements.zip' )
87
- else
88
- artifacts . push ( '.requirements' )
95
+ const artifacts = [ '.requirements ' ] ;
96
+ if ( this . custom . zip ) {
97
+ artifacts . push ( '.requirements.zip' ) ;
98
+ artifacts . push ( 'unzip_requirements.py' ) ;
99
+ }
89
100
90
101
return BbPromise . all ( _ . map ( artifacts , ( artifact ) =>
91
102
fse . removeAsync ( path . join ( this . serverless . config . servicePath , artifact ) ) ) ) ; ;
@@ -95,16 +106,19 @@ class ServerlessPythonRequirements {
95
106
this . serverless = serverless ;
96
107
this . options = options ;
97
108
this . custom = Object . assign ( {
98
- zipImport : false ,
99
- link : true ,
109
+ zip : false ,
100
110
} , this . serverless . service . custom &&
101
111
this . serverless . service . custom . pythonRequirements || { } ) ;
102
112
113
+ if ( ! _ . has ( this . serverless . service , [ 'package' , 'exclude' ] ) )
114
+ _ . set ( this . serverless . service , [ 'package' , 'exclude' ] , [ ] ) ;
115
+ this . serverless . service . package . exclude . push ( '.requirements/**' ) ;
116
+
103
117
this . commands = {
104
118
'requirements' : {
105
119
commands : {
106
120
'clean' : {
107
- usage : 'Remove .requirements and requirements.py ' ,
121
+ usage : 'Remove .requirements and requirements.zip ' ,
108
122
lifecycleEvents : [
109
123
'clean' ,
110
124
] ,
@@ -121,15 +135,16 @@ class ServerlessPythonRequirements {
121
135
122
136
this . hooks = {
123
137
'before:deploy:createDeploymentArtifacts' : ( ) => BbPromise . bind ( this )
124
- . then ( this . packVendorHelper )
138
+ . then ( this . addVendorHelper )
125
139
. then ( this . packRequirements )
126
140
. then ( this . linkRequirements ) ,
127
141
128
142
'after:deploy:createDeploymentArtifacts' : ( ) => BbPromise . bind ( this )
143
+ . then ( this . removeVendorHelper )
129
144
. then ( this . unlinkRequirements ) ,
130
145
131
146
'requirements:install:install' : ( ) => BbPromise . bind ( this )
132
- . then ( this . packVendorHelper )
147
+ . then ( this . addVendorHelper )
133
148
. then ( this . packRequirements ) ,
134
149
135
150
'requirements:clean:clean' : ( ) => BbPromise . bind ( this )
0 commit comments