Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Commit

Permalink
Create .mse file at $HOME if it doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
anoopkcn committed Mar 11, 2020
1 parent 454fa87 commit 78115d1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 89 deletions.
2 changes: 1 addition & 1 deletion src/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ export default function App() {
setAppName(arg.appName);
setAppVersion(arg.appVersion);
});
readConfig();
}, []);

const classes = useStyles();
Expand Down Expand Up @@ -163,6 +162,7 @@ export default function App() {
}

console.log(appName, appVersion);
readConfig();

let windowView;
if (isDashboard) {
Expand Down
21 changes: 10 additions & 11 deletions src/components/Nodes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const useStyles = makeStyles(theme => ({
export default function Nodes() {
const [data, setData] = useState({});
const [isLoaded, setLoaded] = useState(false);
const [isDatabase, setDatabase] = useState(true);
const isRemoteDB = false

//for split button
Expand All @@ -72,9 +71,9 @@ export default function Nodes() {
if (isActive) return "secondary";
};

const activateDatabase = () => {
setDatabase(true);
};
// const activateDatabase = () => {
// setDatabase(true);
// };

const handleMenuItemClick = (event, index) => {
setSelectedIndex(index);
Expand All @@ -101,7 +100,7 @@ export default function Nodes() {
useEffect(() => {
let didCancel = false;
async function fetchData() {
if (isDatabase) {
if (db !=null) {
db.query(queryText, (err, res) => {
if (!err && !didCancel) {
setData(res.rows);
Expand All @@ -111,7 +110,7 @@ export default function Nodes() {
}
}

if (isDatabase) {
if (db != null) {
fetchData();
if (isIntervel) {
setInterval(() => {
Expand All @@ -122,18 +121,18 @@ export default function Nodes() {
return () => {
didCancel = true;
};
}, [isDatabase, fetchInterval, isIntervel]);
}, [fetchInterval, isIntervel]);

let nodesTable;
if (isLoaded && data) {
if (isDatabase === false) {
if (db == null) {
nodesTable = (
<div>
You have to set the path to aiida config and start the postgress
server or start remote REST API connection on port 5791{" "}
</div>
);
} else if (isDatabase && !data.data) {
} else if (!data.data) {
nodesTable = <NodesTable data={data} detailsPanel={isIntervel} />;
} else {
nodesTable = (
Expand Down Expand Up @@ -171,9 +170,9 @@ export default function Nodes() {
disableRipple={true}
variant="outlined"
className={classes.button}
onClick={activateDatabase}
// onClick={activateDatabase}
>
<BallotIcon color={activeColor(isDatabase)} />
<BallotIcon color={activeColor(db)} />
</Button>
<Button
disableRipple={true}
Expand Down
105 changes: 28 additions & 77 deletions src/lib/global.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
import { channels } from "../shared/constants";
import { utils } from "./utils";

const os = window.require("os");
const fs = window.require("fs");
const net = window.require("net");
const { Pool } = window.require("pg");

const { ipcRenderer } = window;

export const HOME_DIR = os.homedir();
export const CONFIG_FILE = `${HOME_DIR}/.mse`;
export const PORT = 5791;
export const AIIDA_RESTAPI_URL = `http://localhost:${PORT}/api/v4`;
export const VERDI = readConfig("python_env")
? `${readConfig("python_env")}/bin/verdi`
: null;
export const AIIDA_CONFIG_FILE = readConfig("aiida_dir")
? `${readConfig("aiida_dir")}/config.json`
: null; //`${HOME_DIR}/.aiida/config.json`
export var db;
export var db =null;

export function readConfig() {
let fd;
var py_config
try {
fd = fs.openSync(CONFIG_FILE, 'wx+');
fs.appendFileSync(fd, `{ "python_env": "${HOME_DIR}/.virtualenvs/aiida", "aiida_config": "${HOME_DIR}/.aiida/config.json" }`, 'utf8');
} catch (err) {
//console.log('Config file already exists')
} finally {
if (fd !== undefined) fs.closeSync(fd);
}
var data = fs.readFileSync(CONFIG_FILE, "utf-8");
if (data) {
try {
py_config = JSON.parse(data);
} catch (e) {
py_config = null
alert(e);
}
}
return py_config
}

var py_config = readConfig()
export const VERDI = py_config ? `${py_config['python_env']}/bin/verdi` : null;
export const AIIDA_CONFIG_FILE = py_config ? `${py_config['aiida_config']}` : null;

var data;
if (AIIDA_CONFIG_FILE) {
Expand All @@ -28,68 +41,6 @@ if (AIIDA_CONFIG_FILE) {
}
export const db_profile = data ? JSON.parse(data) : null;

ipcRenderer.send(channels.PORT_MESSAGE, PORT);

export const configTemplate = `
{
"python_env": "",
"aiida_dir": ""
}
`;

export function writeConfig() {
fs.access(CONFIG_FILE, fs.F_OK, err => {
if (err) {
fs.writeFile(CONFIG_FILE, configTemplate);
return;
}
});
}

export function readConfig(property) {
var data = fs.readFileSync(CONFIG_FILE, "utf-8");
var config = JSON.parse(data);
if (config[property]) {
return config[property];
} else {
return null;
}
}

// ipcRenderer.on(channels.PORT_MESSAGE, (event, arg) => {
// ipcRenderer.removeAllListeners(channels.PORT_MESSAGE);
// console.log(arg)
// });

export function startRestAPI() {
// TODO:: Find if the REST API is running on port PORT if not start the API...
// ... and send PID to main else send the pid of the running API to the main process
//
//
var tester = net
.createServer()
.once("error", err => {
return `Port ${PORT} is occupied`;
})
.once("listening", () => {
tester
.once("close", () => {
if (VERDI) {
utils.spawn(`${VERDI}`, ["restapi", "-P", `${PORT}`], {
detached: true,
windowsHide: true,
stdio: "ignore"
});
return "Server Started";
} else {
return "Could not start the REST API";
}
})
.close();
})
.listen(PORT);
}

if (db_profile) {
const pool = new Pool({
user: db_profile.profiles[db_profile.default_profile].AIIDADB_USER,
Expand Down

0 comments on commit 78115d1

Please sign in to comment.