Skip to content

Commit

Permalink
chore(native): Use correct file name for python module loading (#6935)
Browse files Browse the repository at this point in the history
  • Loading branch information
ovr committed Jul 21, 2023
1 parent c167002 commit 79b3e1a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/cubejs-backend-native/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export interface PyConfiguration {
contextToApiScopes?: () => Promise<string[]>
}

export const pythonLoadConfig = async (content: string, options: { file: string }): Promise<PyConfiguration> => {
export const pythonLoadConfig = async (content: string, options: { fileName: string }): Promise<PyConfiguration> => {
if (isFallbackBuild()) {
throw new Error('Python is not supported in fallback build');
}
Expand Down
8 changes: 6 additions & 2 deletions packages/cubejs-backend-native/src/python/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ use pyo3::prelude::*;
use pyo3::types::{PyDict, PyFunction, PyList, PyString, PyTuple};

fn python_load_config(mut cx: FunctionContext) -> JsResult<JsPromise> {
let config_file_content = cx.argument::<JsString>(0)?.value(&mut cx);
let file_content_arg = cx.argument::<JsString>(0)?.value(&mut cx);
let options_arg = cx.argument::<JsObject>(1)?;
let options_file_name = options_arg
.get::<JsString, _, _>(&mut cx, "fileName")?
.value(&mut cx);

let (deferred, promise) = cx.promise();
let channel = cx.channel();
Expand All @@ -22,7 +26,7 @@ fn python_load_config(mut cx: FunctionContext) -> JsResult<JsPromise> {
));
PyModule::from_code(py, cube_conf_code, "__init__.py", "cube.conf")?;

let config_module = PyModule::from_code(py, &config_file_content, "config.py", "")?;
let config_module = PyModule::from_code(py, &file_content_arg, &options_file_name, "")?;
let settings_py = if config_module.hasattr("__execution_context_locals")? {
let execution_context_locals = config_module.getattr("__execution_context_locals")?;
execution_context_locals.get_item("settings")?
Expand Down
10 changes: 5 additions & 5 deletions packages/cubejs-backend-native/test/python.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ const suite = native.isFallbackBuild() ? xdescribe : describe;
// TODO(ovr): Find what is going wrong with parallel tests & python on Linux
const darwinSuite = process.platform === 'darwin' && !native.isFallbackBuild() ? describe : xdescribe;

async function loadConfigurationFile(file: string) {
const content = await fs.readFile(path.join(process.cwd(), 'test', file), 'utf8');
async function loadConfigurationFile(fileName: string) {
const content = await fs.readFile(path.join(process.cwd(), 'test', fileName), 'utf8');
console.log('content', {
content,
file
fileName
});

const config = await native.pythonLoadConfig(
content,
{
file
fileName
}
);

console.log(`loaded config ${file}`, config);
console.log(`loaded config ${fileName}`, config);

return config;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cubejs-backend-native/test/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as native from '../js';
const config = await native.pythonLoadConfig(
content,
{
file: 'config.py'
fileName: 'config.py'
}
);

Expand Down
2 changes: 1 addition & 1 deletion packages/cubejs-backend-native/test/python_async_bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as native from '../js';
const config = await native.pythonLoadConfig(
content,
{
file: 'config.py'
fileName: 'config.py'
}
);

Expand Down
12 changes: 8 additions & 4 deletions packages/cubejs-server/src/server/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,17 +339,21 @@ export class ServerContainer {
}

protected async loadConfigurationFromPythonFile(): Promise<CreateOptions> {
const file = await fsAsync.readFile(
const content = await fsAsync.readFile(
path.join(process.cwd(), 'cube.py'),
'utf-8'
);

if (this.configuration.debug) {
console.log('Loaded python configuration file', file);
console.log('Loaded python configuration file', content);
}

const config = await pythonLoadConfig(file, {
file: 'cube.py',
return this.loadConfigurationFromPythonMemory(content);
}

protected async loadConfigurationFromPythonMemory(content: string): Promise<CreateOptions> {
const config = await pythonLoadConfig(content, {
fileName: 'cube.py',
});

return config as any;
Expand Down

0 comments on commit 79b3e1a

Please sign in to comment.