Skip to content

Commit

Permalink
prettier write
Browse files Browse the repository at this point in the history
  • Loading branch information
kaganrua committed Aug 21, 2024
1 parent c4ce412 commit 78167c9
Show file tree
Hide file tree
Showing 18 changed files with 459 additions and 430 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,3 @@ export async function computeAgent(

return { response: code, model: modelVersion };
}


Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,3 @@ export async function computeViewAgent(

return { response: code, model: modelVersion };
}

1 change: 0 additions & 1 deletion frontend/macPacker.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

exports.default = async function (context) {
console.log(`\n- [INFO] building for ${context.electronPlatformName} \n`);
if (context.electronPlatformName !== "darwin") {
Expand Down
241 changes: 116 additions & 125 deletions frontend/resources/compileComputation.mjs
Original file line number Diff line number Diff line change
@@ -1,153 +1,144 @@
import {parser} from '@lezer/python';
import { TreeCursor } from '@lezer/common';
import { readFileSync } from 'fs';
import {PythonShell} from 'python-shell'

import { parser } from "@lezer/python";
import { TreeCursor } from "@lezer/common";

Check failure on line 2 in frontend/resources/compileComputation.mjs

View workflow job for this annotation

GitHub Actions / lint (20.x)

'TreeCursor' is defined but never used
import { readFileSync } from "fs";

Check failure on line 3 in frontend/resources/compileComputation.mjs

View workflow job for this annotation

GitHub Actions / lint (20.x)

'readFileSync' is defined but never used
import { PythonShell } from "python-shell";

async function validateSource(source) {
await PythonShell.checkSyntax(source)
await PythonShell.checkSyntax(source);
}

export async function compileComputationFunction(source) {
const result = await extractIO(source)
const docstring = getDocstring(source)
if(docstring) {
result['description'] = docstring
}
return result
const result = await extractIO(source);
const docstring = getDocstring(source);
if (docstring) {
result["description"] = docstring;
}
return result;
}



async function extractIO(source) {
await validateSource(source)
const tree = parser.parse(source);
const cursor = tree.cursor()
await validateSource(source);
const tree = parser.parse(source);
const cursor = tree.cursor();

const functionInfo = {};
while (cursor.next()) {
if (cursor.node.name === "FunctionDefinition") {
cursor.firstChild();
cursor.nextSibling();
const functionName = source.slice(cursor.node.from, cursor.node.to);
if (functionName === "compute") {
cursor.nextSibling();
const inputs = {};
cursor.firstChild();
cursor.nextSibling();
while (cursor.node.name !== ")") {
const param = source.slice(cursor.node.from, cursor.node.to);
inputs[param] = {
type: "Any", // Type inference is complex in Python
connections: [],
relays: [],
};
cursor.nextSibling();
if (cursor.node.name === ",") cursor.nextSibling();
}

const functionInfo = {}
while(cursor.next()) {
if(cursor.node.name === 'FunctionDefinition') {
cursor.firstChild()
cursor.nextSibling()
const functionName = source.slice(cursor.node.from, cursor.node.to)
if(functionName === 'compute') {
functionInfo["inputs"] = inputs;
cursor.parent();

cursor.nextSibling();
const inputs = {};
const outputs = {};
while (cursor.nextSibling()) {
if (cursor.node.name === "Body") {
cursor.firstChild();
while (cursor.nextSibling()) {
if (cursor.node.name === "ReturnStatement") {
cursor.firstChild();
cursor.nextSibling()
while(cursor.node.name !== ')') {
const param = source.slice(cursor.node.from, cursor.node.to)
inputs[param] = {
'type': 'Any', // Type inference is complex in Python
'connections': [],
'relays': []
};
cursor.nextSibling()
if(cursor.node.name === ',') cursor.nextSibling()
}


functionInfo['inputs'] = inputs
cursor.parent()

const outputs = {}
while(cursor.nextSibling()){
if(cursor.node.name === 'Body'){
cursor.firstChild()
while(cursor.nextSibling()){


if(cursor.node.name === 'ReturnStatement'){
cursor.firstChild()
cursor.nextSibling()
if(cursor.node.name === 'DictionaryExpression') {
cursor.firstChild()
cursor.nextSibling()
while(cursor.node.name !== '}'){
cursor.nextSibling()
if(cursor.node.name === 'VariableName'){
const outputName = source.slice(cursor.node.from, cursor.node.to)
outputs[outputName] = {
'type': 'Any',
'connections': [],
'relays': []
}
}
}


}
}

}

cursor.nextSibling();
if (cursor.node.name === "DictionaryExpression") {
cursor.firstChild();
cursor.nextSibling();
while (cursor.node.name !== "}") {
cursor.nextSibling();
if (cursor.node.name === "VariableName") {
const outputName = source.slice(
cursor.node.from,
cursor.node.to,
);
outputs[outputName] = {
type: "Any",
connections: [],
relays: [],
};
}
}
}
functionInfo['outputs'] = outputs
break

}
}
}
}
functionInfo["outputs"] = outputs;
break;
}
}
return functionInfo
}
return functionInfo;
}


function getDocstring(source) {
const tree = parser.parse(source);
const cursor = tree.cursor();

const docstrings = {};
const tree = parser.parse(source);
const cursor = tree.cursor();

while (cursor.next()) {
if (cursor.node.name === 'FunctionDefinition') {
cursor.firstChild(); // Move to the 'def' keyword
cursor.nextSibling(); // Move to the function name
const docstrings = {};

const functionName = source.slice(cursor.node.from, cursor.node.to);

if(functionName === 'compute') {
cursor.firstChild()
while(true) {
if(cursor.node.name === 'Body') {
break
}
cursor.nextSibling()
}
cursor.firstChild()
while(cursor.nextSibling()) {
if(cursor.node.name === 'ExpressionStatement') {

const docstring = source.slice(cursor.node.from+3, cursor.node.to-3)
return docstring
}
}
}
while (cursor.next()) {
if (cursor.node.name === "FunctionDefinition") {
cursor.firstChild(); // Move to the 'def' keyword
cursor.nextSibling(); // Move to the function name

cursor.nextSibling(); // Move to the parameters
cursor.nextSibling(); // Move to the colon or the body
const functionName = source.slice(cursor.node.from, cursor.node.to);

while (cursor.nextSibling()) {
if (cursor.node.name === 'Body') {
cursor.firstChild(); // Enter the body
console.log("HERE")
// Check if the first statement is a docstring
if (cursor.node.name === 'ExpressionStatement') {
cursor.firstChild(); // Move to the string
if (cursor.node.name === 'String') {
let docstring = source.slice(cursor.node.from, cursor.node.to);
docstring = docstring.slice(1, -1); // Remove the quotes from the string
docstrings[functionName] = docstring;
}
cursor.parent(); // Move back to the ExpressionStatement
}
break;
}
if (functionName === "compute") {
cursor.firstChild();
while (true) {

Check failure on line 102 in frontend/resources/compileComputation.mjs

View workflow job for this annotation

GitHub Actions / lint (20.x)

Unexpected constant condition
if (cursor.node.name === "Body") {
break;
}
cursor.nextSibling();
}
cursor.firstChild();
while (cursor.nextSibling()) {
if (cursor.node.name === "ExpressionStatement") {
const docstring = source.slice(
cursor.node.from + 3,
cursor.node.to - 3,
);
return docstring;
}
}
}

cursor.nextSibling(); // Move to the parameters
cursor.nextSibling(); // Move to the colon or the body

while (cursor.nextSibling()) {
if (cursor.node.name === "Body") {
cursor.firstChild(); // Enter the body
console.log("HERE");
// Check if the first statement is a docstring
if (cursor.node.name === "ExpressionStatement") {
cursor.firstChild(); // Move to the string
if (cursor.node.name === "String") {
let docstring = source.slice(cursor.node.from, cursor.node.to);
docstring = docstring.slice(1, -1); // Remove the quotes from the string
docstrings[functionName] = docstring;
}
cursor.parent(); // Move back to the ExpressionStatement
}
break;
}
}
}
}

return docstrings;
return docstrings;
}

64 changes: 40 additions & 24 deletions frontend/resources/runTest.mjs
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import { spawn } from 'child_process';
import fs from 'fs'
import path from 'path'
import { spawn } from "child_process";
import fs from "fs";
import path from "path";

function runCommand(command, args, logFile) {
return new Promise((resolve, reject) => {
const process = spawn(command, args);

const logStream = fs.createWriteStream(logFile, { flags: 'a' });
const logStream = fs.createWriteStream(logFile, { flags: "a" });

process.stdout.on('data', (data) => {
const output = data.toString('utf-8');
process.stdout.on("data", (data) => {
const output = data.toString("utf-8");
process.stdout.write(output);
logStream.write(data);
});

process.stderr.on('data', (data) => {
const output = data.toString('utf-8');
process.stderr.on("data", (data) => {
const output = data.toString("utf-8");
process.stderr.write(output);
logStream.write(data);
});

process.on('close', (code) => {
process.on("close", (code) => {
logStream.end();
if (code !== 0) {
reject(new Error(`Command '${command} ${args.join(' ')}' failed with error code ${code}`));
reject(
new Error(
`Command '${command} ${args.join(" ")}' failed with error code ${code}`,
),
);
} else {
resolve();
}
Expand All @@ -32,28 +36,40 @@ function runCommand(command, args, logFile) {
}

export async function runTestContainer(blockDir, blockKey) {
const containerDir = '/app'; // The directory inside the container where you want to mount
const containerDir = "/app"; // The directory inside the container where you want to mount
const imageName = blockKey;
const containerName = blockKey;

const commands = [
['docker', ['rm', containerName]],
['docker', ['rmi', imageName]],
['docker', ['build', '-t', imageName, blockDir]],
['docker', ['run', '--name', containerName, '-v', `${blockDir}:${containerDir}`, imageName, 'python', '-B', '-c', 'from computations import test; test()']]
["docker", ["rm", containerName]],
["docker", ["rmi", imageName]],
["docker", ["build", "-t", imageName, blockDir]],
[
"docker",
[
"run",
"--name",
containerName,
"-v",
`${blockDir}:${containerDir}`,
imageName,
"python",
"-B",
"-c",
"from computations import test; test()",
],
],
];

const logFile = path.join(blockDir, 'logs.txt');
fs.writeFileSync(logFile, ''); // Clear the log file
const logFile = path.join(blockDir, "logs.txt");
fs.writeFileSync(logFile, ""); // Clear the log file

for (const [command, args] of commands) {
console.log(`Executing: ${command} ${args.join(' ')}`);
try{
await runCommand(command, args, logFile);
} catch(error) {
console.log(`Command '${command}' failed with error`)

console.log(`Executing: ${command} ${args.join(" ")}`);
try {
await runCommand(command, args, logFile);
} catch (error) {
console.log(`Command '${command}' failed with error`);
}
}
}

Loading

0 comments on commit 78167c9

Please sign in to comment.