Skip to content

Commit a3d53fd

Browse files
committed
Wrap up python emulator support.
1 parent e623f71 commit a3d53fd

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
"build:publish": "tsc --build tsconfig.publish.json && npm run copyfiles",
1212
"build:watch": "npm run build && tsc --watch",
1313
"clean": "rimraf lib dev",
14-
"copyfiles": "node -e \"const fs = require('fs'); fs.mkdirSync('./lib', {recursive:true}); fs.copyFileSync('./src/dynamicImport.js', './lib/dynamicImport.js')\"",
14+
"copyfiles": "npm run copyDynamicImportModule && npm run copyEmulatorFiles",
15+
"copyDynamicImportModule": "node -e \"const fs = require('fs'); fs.mkdirSync('./lib', {recursive:true}); fs.copyFileSync('./src/dynamicImport.js', './lib/dynamicImport.js')\"",
16+
"copyEmulatorFiles": "cp ./src/emulator/functionsEmulatorRuntime.py ./lib/emulator/functionsEmulatorRuntime.py",
1517
"format": "npm run format:ts && npm run format:other",
1618
"format:other": "npm run lint:other -- --write",
1719
"format:ts": "npm run lint:ts -- --fix --quiet",

src/emulator/functionsEmulator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ export class FunctionsEmulator implements EmulatorInstance {
13051305
backend: EmulatableBackend,
13061306
envs: Record<string, string>
13071307
): Promise<FunctionsRuntimeInstance> {
1308-
const args = [path.join(__dirname, "functionsEmulatorRuntime")];
1308+
const args = [path.join(__dirname, "functionsEmulatorRuntime.py")];
13091309

13101310
if (this.args.debugPort) {
13111311
this.logger.log(
@@ -1330,6 +1330,7 @@ export class FunctionsEmulator implements EmulatorInstance {
13301330
const childProcess = runWithVirtualEnv([bin, ...args], backend.functionsDir, {
13311331
...process.env,
13321332
...envs,
1333+
HOST: "localhost",
13331334
PORT: port.toString(),
13341335
});
13351336

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2023 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""A light wrapper around the Functions Framework for the purpose of emulator support."""
15+
16+
import os
17+
from functions_framework import create_app
18+
from functions_framework._http.flask import FlaskApplication
19+
20+
def create_server(host, port):
21+
app = create_app()
22+
FlaskApplication(app, host, port, False).run()
23+
24+
25+
def main():
26+
create_server(os.environ["HOST"], os.environ["PORT"])
27+
28+
29+
if __name__ == "__main__":
30+
main()

src/functions/python.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ export function runWithVirtualEnv(
2020
const command = process.platform === "win32" ? venvActivate : "source";
2121
const args = [process.platform === "win32" ? "" : venvActivate, "&&", ...commandAndArgs];
2222
logger.debug(`Running command with virtualenv: command=${command}, args=${JSON.stringify(args)}`);
23+
logger.debug(JSON.stringify(envs));
2324

2425
return spawn(command, args, {
2526
shell: true,
2627
cwd,
2728
stdio: [/* stdin= */ "ignore", /* stdout= */ "pipe", /* stderr= */ "inherit"],
28-
...envs,
29+
// Linting disabled since internal types expect NODE_ENV which does not apply to Python runtimes.
30+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
31+
env: envs as any,
2932
});
3033
}

0 commit comments

Comments
 (0)