Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CFG backend merge request #5

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
695 changes: 674 additions & 21 deletions LICENSE

Large diffs are not rendered by default.

68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,71 @@
Backend for PSoft Tools

Front end repo: https://github.com/aandrepingu/PSoft-Tools

# CFG

If installing on a server you need to aquire the docker for metrinome: using either "docker pull harveymudd/metrinome" OR through https://hub.docker.com/r/harveymudd/metrinome.


Ensure you run "make build" for docker to reduce possible errors


Then you need to modify the docker to run properly.

At the end of the docker file in metrinome/src/docker add:

```

RUN /bin/bash ./script.sh

```
This is so that it will run the ./script.sh on start up and run commands we want then close.

And in that same direrctory add a script.sh file with the following commands
```bash
python3 ./main.py
exit
```

you will also need to modify main.py in ./metrinome/src

```Python3
270 #prompt.cmdloop(f"{Colors.TITLE.value}" + r"""
271#
272# _ _
273 # /\/\ ___| |_ _ __(_)_ __ ___ _ __ ___ ___
274 # / \ / _ \ __| '__| | '_ \ / _ \| '_ ` _ \ / _ \
275 #/ /\/\ \ __/ |_| | | | | | | (_) | | | | | | __/
276 #\/ \/\___|\__|_| |_|_| |_|\___/|_| |_| |_|\___|
277 #
278 #""" + f"{Colors.ENDC.value}\nStarting the REPL...")
279 prompt.onecmd("convert tempfile.c")
280 prompt.onecmd("export graph tempfile_.main.dot")
281
```

modify the file so that the end of the main() function looks like this


Currently only accepting .c files in the form of
```C++
int main(){

/*
code
here
for
CFG
*/

}

```
The function must be called main


Current Issues: the generateCFG call in index.ts creates the image but doesn't return a useable image

https://stackoverflow.com/questions/57829529/how-to-fetch-and-display-an-image-from-an-express-backend-server-to-a-react-js-f

This will provide a look into making a proper index.ts fetch where the backend will host the image correctly. The frontend will also need to change to correctly call the right source for displaying the image.
59 changes: 59 additions & 0 deletions src/CFG/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
To set up CFG functionality you need to set up the docker to run.

You need to add a script.sh file in the metrinome/src dir that contains
```bash
python3 ./main.py
exit
```
and then modify the docker file in the same directory by adding this at the end of the file
```
RUN /bin/bash ./script.sh
```

then run "make build"


It may take a while to build

you may need to pip install graphviz for the .jpeg renderer of a .dot file

Within the metrinome these are important commands for later building and testing
```
to build, go to main metrinome directory and run:
sudo docker build -t apcmetrinome/dockerapc -f replication/Dockerfile .

to push to dockerhub:
sudo docker login
sudo docker push apcmetrinome/dockerapc

to run:
sudo docker run -it apcmetrinome/dockerapc

to run experiments inside docker:
./Testing.sh

```

you will also need to modify main.py in ./metrinome/src

```Python3
270 #prompt.cmdloop(f"{Colors.TITLE.value}" + r"""
271#
272# _ _
273 # /\/\ ___| |_ _ __(_)_ __ ___ _ __ ___ ___
274 # / \ / _ \ __| '__| | '_ \ / _ \| '_ ` _ \ / _ \
275 #/ /\/\ \ __/ |_| | | | | | | (_) | | | | | | __/
276 #\/ \/\___|\__|_| |_|_| |_|\___/|_| |_| |_|\___|
277 #
278 #""" + f"{Colors.ENDC.value}\nStarting the REPL...")
279 prompt.onecmd("convert tempfile.c")
280 prompt.onecmd("export graph tempfile_.main.dot")
281
```

modify the file so that the end of the main() function looks like this

On server set up, docker daemon needs to be running. Run the following command to start the docker daemon.
```
sudo service docker restart
```
1 change: 1 addition & 0 deletions src/CFG/metrinome
Submodule metrinome added at 17d148
15 changes: 15 additions & 0 deletions src/bashscp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

if ! test -f "/usr/bin/graphviz"; then
echo graphviz not installed, installing now...
sudo apt-get install graphviz
fi

cd CFG/metrinome/src
make run
#THINGS IN COMMENTS WILL BE RUN IN DOCKER ON RUN. MAKE SURE IF YOU ARE SETTING THIS UP ON A SERVER TO ADD THE DOCKER AND SET THIS UP!
#python3 ./main.py
#quit
#exit
cd exports
dot -Tpng tempfile__main_dot_export.dot > CFG.jpeg
cp CFG.jpeg ../../../
20 changes: 20 additions & 0 deletions src/generateCFG.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { exec } from "child_process";
import { writeFileSync } from "fs";
export async function generateImage(Code: string): Promise<string> {
const projectRoot = "./"; // Root directory of your project
writeFileSync(
__dirname + "/CFG/metrinome/src/tempfile.c",
Code.replace(/\r\n/g, "\n")
);
//this writes the code into the right file.
return new Promise<string>((resolve, reject) => {
exec(
'bash bashscp.sh'
);

}
);
//this runs the bash script

}

9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { verifyDafny, runDafny } from "./runDafny";
import { writeFileSync } from "fs";
import {exec} from "child_process";
import { Console } from "console";
import { generateImage } from "./generateCFG";
var cors = require("cors");
var bodyParser = require("body-parser");

Expand Down Expand Up @@ -46,6 +47,14 @@ app.post("/hoare", (request, response) => {
});
});

app.post('/fetchImage/CFG.jpeg', (req, res) => {
const endCode: string = req.body.toString();
generateImage(endCode);
//nlet file = req.params.file; //should be CFG.jpeg
//let fileLocation = path.join('./CFG/', file);

//res.sendFile(`${fileLocation}`)
});

// app.get("/", (req, res) => {
// //res.send("Hello World!");
Expand Down
7 changes: 7 additions & 0 deletions src/tempfile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
int main(){
int c = 0;
while( c != 10 ){
c++;
}
return c;
}