Skip to content

Commit 8505bca

Browse files
authored
Merge pull request #1768 from rosahbruno/1852217-support-python-cli-arg
2 parents 0f79435 + e78ee61 commit 8505bca

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[Full changelog](https://github.com/mozilla/glean.js/compare/v2.0.1...main)
44

5+
* [#1768](https://github.com/mozilla/glean.js/pull/1768): Add support for `GLEAN_PYTHON` and `GLEAN_PIP` environment variables.
56
* [#1755](https://github.com/mozilla/glean.js/pull/1755): Add sync check to `set` function for the URL metric.
67
* [#1766](https://github.com/mozilla/glean.js/pull/1766): Update default `maxEvents` count to 1. This means an events ping will be sent after each recorded event unless the `maxEvents` count is explicitly set to a larger number.
78

bin/python-env-vars-test.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This Source Code Form is subject to the terms of the Mozilla Public
4+
# License, v. 2.0. If a copy of the MPL was not distributed with this
5+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
7+
set -eo pipefail
8+
9+
run() {
10+
[ "${VERB:-0}" != 0 ] && echo "+ $*"
11+
"$@"
12+
}
13+
14+
# All sed commands below work with either
15+
# GNU sed (standard on Linux distrubtions) or BSD sed (standard on macOS)
16+
SED="sed"
17+
18+
WORKSPACE_ROOT="$( cd "$(dirname "$0")/.." ; pwd -P )"
19+
20+
# Set custom values for Python environment variables.
21+
if [ -x "$(command -v python)" ]; then
22+
export GLEAN_PYTHON=python
23+
fi
24+
25+
if [ -x "$(command -v pip)" ]; then
26+
export GLEAN_PIP=pip
27+
fi
28+
29+
# Delete the .venv so a new one gets created for testing.
30+
rm -rf .venv
31+
32+
# Generate files using the Glean.js CLI tool (which just runs glean_parser)
33+
npm run cli -- \
34+
translate tests/integration/schema/metrics.yaml tests/integration/schema/pings.yaml \
35+
-f typescript \
36+
-o tests/integration/schema/generated
37+
38+
# Update metrics import path
39+
FILE=glean/tests/integration/schema/generated/forTesting.ts
40+
run $SED -i.bak -E \
41+
-e 's#@mozilla/glean/private/metrics#../../../../src/core/metrics/types#g' \
42+
"${WORKSPACE_ROOT}/${FILE}"
43+
run rm "${WORKSPACE_ROOT}/${FILE}.bak"
44+
45+
# Update ping import path
46+
FILE=glean/tests/integration/schema/generated/pings.ts
47+
run $SED -i.bak -E \
48+
-e 's#@mozilla/glean/private/ping#../../../../src/core/pings/ping_type.js#g' \
49+
"${WORKSPACE_ROOT}/${FILE}"
50+
run rm "${WORKSPACE_ROOT}/${FILE}.bak"
51+
52+
unset GLEAN_PYTHON
53+
unset GLEAN_PIP
54+
55+
# Delete the custom .venv so next run uses the default Python binaries.
56+
rm -rf .venv

glean/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"scripts": {
5959
"test": "run-s test:unit test:integration",
6060
"test:integration": "npm run test:base -- \"tests/integration/**/*.spec.ts\" --recursive",
61-
"pretest:integration": "../bin/parser-for-schema-testing.sh",
61+
"pretest:integration": "../bin/parser-for-schema-testing.sh && ../bin/python-env-vars-test.sh",
6262
"test:unit": "run-s test:unit:core test:unit:platform test:unit:plugins",
6363
"test:unit:core": "npm run test:base -- \"tests/unit/core/**/*.spec.ts\" --recursive",
6464
"test:unit:plugins": "npm run test:base -- \"tests/unit/plugins/**/*.spec.ts\" --recursive",

glean/src/cli.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ function getSystemPythonBinName(): string {
109109
return (platform === "win32") ? "python.exe" : "python3";
110110
}
111111

112+
/**
113+
* Gets the name of the Python pip binary, based on the host OS.
114+
*
115+
* @returns the name of the Python pip binary.
116+
*/
117+
function getPipBinName(): string {
118+
return (platform === "win32") ? "pip3.exe" : "pip3";
119+
}
120+
112121
/**
113122
* Gets the full path to the directory containing the python
114123
* binaries in the virtual environment.
@@ -137,8 +146,8 @@ function getPythonVenvBinariesPath(venvRoot: string): string {
137146
async function checkPythonVenvExists(venvPath: string): Promise<boolean> {
138147
log(LOG_TAG, `Checking for a virtual environment at ${venvPath}`);
139148

140-
const venvPython =
141-
path.join(getPythonVenvBinariesPath(venvPath), getSystemPythonBinName());
149+
const pythonBinary = process.env.GLEAN_PYTHON ?? getSystemPythonBinName();
150+
const venvPython = path.join(getPythonVenvBinariesPath(venvPath), pythonBinary);
142151

143152
const access = promisify(fs.access);
144153

@@ -161,12 +170,14 @@ async function checkPythonVenvExists(venvPath: string): Promise<boolean> {
161170
async function createPythonVenv(venvPath: string): Promise<boolean> {
162171
log(LOG_TAG, `Creating a virtual environment at ${venvPath}`);
163172

164-
const pipFilename = (platform === "win32") ? "pip3.exe" : "pip3";
173+
const pipFilename = process.env.GLEAN_PIP ?? getPipBinName();
165174
const venvPip =
166175
path.join(getPythonVenvBinariesPath(VIRTUAL_ENVIRONMENT_DIR), pipFilename);
167176

168177
const pipCmd = `${venvPip} install wheel`;
169-
const venvCmd = `${getSystemPythonBinName()} -m venv ${VIRTUAL_ENVIRONMENT_DIR}`;
178+
179+
const pythonBinary = process.env.GLEAN_PYTHON ?? getSystemPythonBinName();
180+
const venvCmd = `${pythonBinary} -m venv ${VIRTUAL_ENVIRONMENT_DIR}`;
170181

171182
for (const cmd of [venvCmd, pipCmd]) {
172183
const spinner = getStartedSpinner();
@@ -207,7 +218,10 @@ async function setup() {
207218
*/
208219
async function runGlean(parserArgs: string[]) {
209220
const spinner = getStartedSpinner();
210-
const pythonBin = path.join(getPythonVenvBinariesPath(VIRTUAL_ENVIRONMENT_DIR), getSystemPythonBinName());
221+
222+
const pythonBinary = process.env.GLEAN_PYTHON ?? getSystemPythonBinName();
223+
const pythonBin = path.join(getPythonVenvBinariesPath(VIRTUAL_ENVIRONMENT_DIR), pythonBinary);
224+
211225
const isOnlineArg = process.env.OFFLINE ? "offline" : "online";
212226

213227
// Trying to pass PYTHON_SCRIPT as a string in the command line arguments

0 commit comments

Comments
 (0)