Skip to content

Commit 998aa9b

Browse files
author
adrianj
committed
add python
1 parent a53ef4e commit 998aa9b

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

src/functionHelper.js

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,53 @@
22

33
const 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
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ class Offline {
303303
const apiKeys = this.service.provider.apiKeys;
304304
const protectedRoutes = [];
305305

306-
if (['nodejs', 'nodejs4.3', 'nodejs6.10', 'babel'].indexOf(serviceRuntime) === -1) {
306+
if (['nodejs', 'nodejs4.3', 'nodejs6.10', 'babel','python2.7'].indexOf(serviceRuntime) === -1) {
307307
this.printBlankLine();
308308
this.serverlessLog(`Warning: found unsupported runtime '${serviceRuntime}'`);
309309

0 commit comments

Comments
 (0)