2
2
3
3
const debugLog = require ( './debugLog' ) ;
4
4
5
- module . exports = {
6
- getFunctionOptions ( fun , funName , servicePath ) {
5
+ function runPythonHandler ( funOptions , options ) {
6
+ var spawn = require ( "child_process" ) . spawn ;
7
+ return function ( event , context ) {
8
+ var process = spawn ( 'python' , [ "run.py" ] ,
9
+ { stdio : [ 'pipe' , 'pipe' , 'pipe' ] , shell : true , cwd :funOptions . servicePath } ) ;
10
+ process . stdin . write ( JSON . stringify ( { event, context, options, funOptions} ) + "\n" ) ;
11
+ process . stdin . end ( ) ;
12
+ let results = ''
13
+ process . stdout . on ( 'data' , ( data ) => {
14
+ console . log ( `handler out: ${ data } ` ) ;
15
+ results = results + data ;
16
+ console . log ( results )
17
+ } ) ;
18
+ process . stderr . on ( 'data' , ( data ) => {
19
+ context . fail ( data ) ;
20
+ } ) ;
21
+ process . on ( 'close' , ( code ) => {
22
+ if ( code == 0 ) {
23
+ context . succeed ( JSON . parse ( results ) ) ;
24
+ } else {
25
+ context . succeed ( code , results ) ;
26
+ }
27
+
28
+ } ) ;
29
+ }
30
+ }
7
31
32
+ module . exports = {
33
+ getFunctionOptions ( fun , funName , servicePath , serviceRuntime ) {
34
+ console . log ( fun , funName , servicePath )
8
35
// Split handler into method name and path i.e. handler.run
9
- const handlerPath = fun . handler . split ( '.' ) [ 0 ] ;
36
+ const handlerFile = fun . handler . split ( '.' ) [ 0 ] ;
10
37
const handlerName = fun . handler . split ( '/' ) . pop ( ) . split ( '.' ) [ 1 ] ;
11
38
12
39
return {
13
40
funName,
14
41
handlerName, // i.e. run
15
- handlerPath : `${ servicePath } /${ handlerPath } ` ,
42
+ handlerFile,
43
+ handlerPath : `${ servicePath } /${ handlerFile } ` ,
44
+ servicePath,
16
45
funTimeout : ( fun . timeout || 30 ) * 1000 ,
17
46
babelOptions : ( ( fun . custom || { } ) . runtime || { } ) . babel ,
47
+ serviceRuntime,
18
48
} ;
19
49
} ,
20
50
51
+
21
52
// Create a function handler
22
53
// The function handler is used to simulate Lambda functions
23
54
createHandler ( funOptions , options ) {
@@ -30,10 +61,14 @@ module.exports = {
30
61
if ( ! key . match ( 'node_modules' ) ) delete require . cache [ key ] ;
31
62
}
32
63
}
33
-
34
- debugLog ( `Loading handler... (${ funOptions . handlerPath } )` ) ;
35
- const handler = require ( funOptions . handlerPath ) [ funOptions . handlerName ] ;
36
-
64
+ let user_python = true
65
+ let handler = null ;
66
+ if ( funOptions [ 'serviceRuntime' ] == 'python2.7' ) {
67
+ handler = runPythonHandler ( funOptions , options )
68
+ } else {
69
+ debugLog ( `Loading handler... (${ funOptions . handlerPath } )` ) ;
70
+ handler = require ( funOptions . handlerPath ) [ funOptions . handlerName ] ;
71
+ }
37
72
if ( typeof handler !== 'function' ) {
38
73
throw new Error ( `Serverless-offline: handler for '${ funOptions . funName } ' is not a function` ) ;
39
74
}
0 commit comments