22
33const debugLog = require ( './debugLog' ) ;
44
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+ }
731
32+ module . exports = {
33+ getFunctionOptions ( fun , funName , servicePath , serviceRuntime ) {
34+ console . log ( fun , funName , servicePath )
835 // 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 ] ;
1037 const handlerName = fun . handler . split ( '/' ) . pop ( ) . split ( '.' ) [ 1 ] ;
1138
1239 return {
1340 funName,
1441 handlerName, // i.e. run
15- handlerPath : `${ servicePath } /${ handlerPath } ` ,
42+ handlerFile,
43+ handlerPath : `${ servicePath } /${ handlerFile } ` ,
44+ servicePath,
1645 funTimeout : ( fun . timeout || 30 ) * 1000 ,
1746 babelOptions : ( ( fun . custom || { } ) . runtime || { } ) . babel ,
47+ serviceRuntime,
1848 } ;
1949 } ,
2050
51+
2152 // Create a function handler
2253 // The function handler is used to simulate Lambda functions
2354 createHandler ( funOptions , options ) {
@@ -30,10 +61,14 @@ module.exports = {
3061 if ( ! key . match ( 'node_modules' ) ) delete require . cache [ key ] ;
3162 }
3263 }
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+ }
3772 if ( typeof handler !== 'function' ) {
3873 throw new Error ( `Serverless-offline: handler for '${ funOptions . funName } ' is not a function` ) ;
3974 }
0 commit comments