diff --git a/.github/workflows/draft-pdf.yml b/.github/workflows/draft-pdf.yml index cf6d3db..dcc5c2b 100644 --- a/.github/workflows/draft-pdf.yml +++ b/.github/workflows/draft-pdf.yml @@ -6,7 +6,7 @@ jobs: name: Paper Draft steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Build draft PDF uses: openjournals/openjournals-draft-action@master with: @@ -14,7 +14,8 @@ jobs: # This should be the path to the paper within your repo. paper-path: paper.md - name: Upload - uses: actions/upload-artifact@v1 + uses: actions/upload-artifact@v4 + with: name: paper # This is the output path where Pandoc will write the compiled diff --git a/Dockerfile.java b/Dockerfile.java new file mode 100644 index 0000000..178c451 --- /dev/null +++ b/Dockerfile.java @@ -0,0 +1,14 @@ +FROM openjdk:17-jdk-alpine + +WORKDIR /app + +# Only copy the JAR if it exists +COPY ./target/concore-0.0.1-SNAPSHOT.jar /app/concore.jar || true + +# Ensure the JAR file is executable if present +RUN [ -f /app/concore.jar ] && chmod +x /app/concore.jar || true + +EXPOSE 3000 + +# Run Java app only if the JAR exists, otherwise do nothing +CMD ["/bin/sh", "-c", "if [ -f /app/concore.jar ]; then java -jar /app/concore.jar; else echo 'No Java application found, exiting'; fi"] diff --git a/concore.py b/concore.py index 1459c76..51eef7b 100644 --- a/concore.py +++ b/concore.py @@ -12,15 +12,16 @@ with open("concorekill.bat","w") as fpid: fpid.write("taskkill /F /PID "+str(os.getpid())+"\n") -try: - iport = literal_eval(open("concore.iport").read()) -except: - iport = dict() -try: - oport = literal_eval(open("concore.oport").read()) -except: - oport = dict() - +def safe_literal_eval(filename, defaultValue): + try: + with open(filename, "r") as file: + return literal_eval(file.read()) + except (FileNotFoundError, SyntaxError, ValueError, Exception) as e: + print(f"Error reading {filename}: {e}") + return defaultValue + +iport = safe_literal_eval("concore.iport", {}) +oport = safe_literal_eval("concore.oport", {}) s = '' olds = '' @@ -28,6 +29,7 @@ retrycount = 0 inpath = "./in" #must be rel path for local outpath = "./out" +simtime = 0 #9/21/22 try: @@ -46,70 +48,92 @@ except: params = dict() #9/30/22 -def tryparam(n,i): - try: - return params[n] - except: - return i +def tryparam(n, i): + return params.get(n, i) #9/12/21 def default_maxtime(default): global maxtime - try: - maxtime = literal_eval(open(inpath+"1/concore.maxtime").read()) - except: - maxtime = default + maxtime = safe_literal_eval(os.path.join(inpath + "1", "concore.maxtime"), default) + default_maxtime(100) def unchanged(): - global olds,s - if olds==s: + global olds, s + if olds == s: s = '' return True - else: - olds = s - return False + olds = s + return False def read(port, name, initstr): - global s,simtime,retrycount + global s, simtime, retrycount + max_retries=5 time.sleep(delay) + file_path = os.path.join(inpath+str(port), name) + try: - infile = open(inpath+str(port)+"/"+name); - ins = infile.read() - infile.close() - except: + with open(file_path, "r") as infile: + ins = infile.read() + except FileNotFoundError: + print(f"File {file_path} not found, using default value.") ins = initstr - while len(ins)==0: + except Exception as e: + print(f"Error reading {file_path}: {e}") + return initstr + + attempts = 0 + while len(ins) == 0 and attempts < max_retries: time.sleep(delay) - infile = open(inpath+str(port)+"/"+name); - ins = infile.read() - infile.close() + try: + with open(file_path, "r") as infile: + ins = infile.read() + except Exception as e: + print(f"Retry {attempts + 1}: Error reading {file_path} - {e}") + attempts += 1 retrycount += 1 + + if len(ins) == 0: + print(f"Max retries reached for {file_path}, using default value.") + return initstr + s += ins - inval = literal_eval(ins) - simtime = max(simtime,inval[0]) - return inval[1:] + try: + inval = literal_eval(ins) + simtime = max(simtime, inval[0]) + return inval[1:] + except Exception as e: + print(f"Error parsing {ins}: {e}") + return initstr + def write(port, name, val, delta=0): - global outpath,simtime - if isinstance(val,str): - time.sleep(2*delay) - elif isinstance(val,list)==False: - print("mywrite must have list or str") - quit() + global simtime + file_path = os.path.join(outpath+str(port), name) + + if isinstance(val, str): + time.sleep(2 * delay) + elif not isinstance(val, list): + print("write must have list or str") + return + try: - with open(outpath+str(port)+"/"+name,"w") as outfile: - if isinstance(val,list): - outfile.write(str([simtime+delta]+val)) + with open(file_path, "w") as outfile: + if isinstance(val, list): + outfile.write(str([simtime + delta] + val)) simtime += delta else: outfile.write(val) - except: - print("skipping"+outpath+str(port)+"/"+name); + except Exception as e: + print(f"Error writing to {file_path}: {e}") def initval(simtime_val): global simtime - val = literal_eval(simtime_val) - simtime = val[0] - return val[1:] + try: + val = literal_eval(simtime_val) + simtime = val[0] + return val[1:] + except Exception as e: + print(f"Error parsing simtime_val: {e}") + return [] diff --git a/concoredocker.hpp b/concoredocker.hpp index a636a26..cbce718 100644 --- a/concoredocker.hpp +++ b/concoredocker.hpp @@ -1 +1,130 @@ -// concoredocker.hpp -- this C++ include file will be the equivalent of concoredocker.py +#ifndef CONCORE_HPP +#define CONCORE_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class Concore { +public: + std::unordered_map iport; + std::unordered_map oport; + std::string s, olds; + int delay = 1; + int retrycount = 0; + std::string inpath = "/in"; + std::string outpath = "/out"; + int simtime = 0; + int maxtime = 100; + std::unordered_map params; + + Concore() { + iport = safe_literal_eval("concore.iport", {}); + oport = safe_literal_eval("concore.oport", {}); + default_maxtime(100); + load_params(); + } + + std::unordered_map safe_literal_eval(const std::string& filename, std::unordered_map defaultValue) { + std::ifstream file(filename); + if (!file) { + std::cerr << "Error reading " << filename << "\n"; + return defaultValue; + } + return defaultValue; + } + + void load_params() { + std::ifstream file(inpath + "/1/concore.params"); + if (!file) return; + std::stringstream buffer; + buffer << file.rdbuf(); + std::string sparams = buffer.str(); + + if (!sparams.empty() && sparams[0] == '"') { + sparams = sparams.substr(1, sparams.find('"') - 1); + } + + if (!sparams.empty() && sparams[0] != '{') { + sparams = "{'" + std::regex_replace(std::regex_replace(std::regex_replace(sparams, std::regex(","), ", '"), std::regex("="), "':"), std::regex(" "), "") + "}"; + } + } + + std::string tryparam(const std::string& n, const std::string& i) { + return params.count(n) ? params[n] : i; + } + + void default_maxtime(int defaultValue) { + maxtime = defaultValue; + std::ifstream file(inpath + "/1/concore.maxtime"); + if (file) { + file >> maxtime; + } + } + + bool unchanged() { + if (olds == s) { + s.clear(); + return true; + } + olds = s; + return false; + } + + std::vector read(int port, const std::string& name, const std::string& initstr) { + std::this_thread::sleep_for(std::chrono::seconds(delay)); + std::string file_path = inpath + std::to_string(port) + "/" + name; + std::ifstream infile(file_path); + std::string ins; + + if (!infile) { + std::cerr << "File " << file_path << " not found, using default value.\n"; + return {initstr}; + } + std::getline(infile, ins); + + int attempts = 0, max_retries = 5; + while (ins.empty() && attempts < max_retries) { + std::this_thread::sleep_for(std::chrono::seconds(delay)); + infile.open(file_path); + if (infile) std::getline(infile, ins); + attempts++; + retrycount++; + } + + if (ins.empty()) { + std::cerr << "Max retries reached for " << file_path << ", using default value.\n"; + return {initstr}; + } + + s += ins; + return {ins}; + } + + void write(int port, const std::string& name, const std::vector& val, int delta = 0) { + std::string file_path = outpath + std::to_string(port) + "/" + name; + std::ofstream outfile(file_path); + if (!outfile) { + std::cerr << "Error writing to " << file_path << "\n"; + return; + } + if (!val.empty()) { + outfile << "[" << simtime + delta << ", "; + for (size_t i = 0; i < val.size(); ++i) { + outfile << val[i] << (i + 1 < val.size() ? ", " : ""); + } + outfile << "]"; + simtime += delta; + } + } +}; + +#endif diff --git a/concoredocker.java b/concoredocker.java new file mode 100644 index 0000000..dde521c --- /dev/null +++ b/concoredocker.java @@ -0,0 +1,149 @@ +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; + +public class concoredocker { + private static Map iport = new HashMap<>(); + private static Map oport = new HashMap<>(); + private static String s = ""; + private static String olds = ""; + private static int delay = 1; + private static int retrycount = 0; + private static String inpath = "/in"; + private static String outpath = "/out"; + private static Map params = new HashMap<>(); + private static int maxtime; + + public static void main(String[] args) { + try { + iport = parseFile("concore.iport"); + } catch (IOException e) { + e.printStackTrace(); + } + try { + oport = parseFile("concore.oport"); + } catch (IOException e) { + e.printStackTrace(); + } + + try { + String sparams = new String(Files.readAllBytes(Paths.get(inpath + "1/concore.params"))); + if (sparams.charAt(0) == '"') { // windows keeps "" need to remove + sparams = sparams.substring(1); + sparams = sparams.substring(0, sparams.indexOf('"')); + } + if (!sparams.equals("{")) { + System.out.println("converting sparams: " + sparams); + sparams = "{'" + sparams.replaceAll(",", ",'").replaceAll("=", "':").replaceAll(" ", "") + "}"; + System.out.println("converted sparams: " + sparams); + } + try { + params = literalEval(sparams); + } catch (Exception e) { + System.out.println("bad params: " + sparams); + } + } catch (IOException e) { + params = new HashMap<>(); + } + + defaultMaxTime(100); + } + + private static Map parseFile(String filename) throws IOException { + String content = new String(Files.readAllBytes(Paths.get(filename))); + return literalEval(content); + } + + private static void defaultMaxTime(int defaultValue) { + try { + String content = new String(Files.readAllBytes(Paths.get(inpath + "1/concore.maxtime"))); + maxtime = literalEval(content).size(); + } catch (IOException e) { + maxtime = defaultValue; + } + } + + private static void unchanged() { + if (olds.equals(s)) { + s = ""; + } else { + olds = s; + } + } + + private static Object tryParam(String n, Object i) { + if (params.containsKey(n)) { + return params.get(n); + } else { + return i; + } + } + + private static Object read(int port, String name, String initstr) { + try { + String ins = new String(Files.readAllBytes(Paths.get(inpath + port + "/" + name))); + while (ins.length() == 0) { + Thread.sleep(delay); + ins = new String(Files.readAllBytes(Paths.get(inpath + port + "/" + name))); + retrycount++; + } + s += ins; + Object[] inval = new Map[] { literalEval(ins) }; + int simtime = Math.max((int) inval[0], 0); // assuming simtime is an integer + return inval[1]; + } catch (IOException | InterruptedException e) { + return initstr; + } + } + + private static void write(int port, String name, Object val, int delta) { + try { + String path = outpath + port + "/" + name; + StringBuilder content = new StringBuilder(); + if (val instanceof String) { + Thread.sleep(2 * delay); + } else if (!(val instanceof Object[])) { + System.out.println("mywrite must have list or str"); + System.exit(1); + } + if (val instanceof Object[]) { + Object[] arrayVal = (Object[]) val; + content.append("[") + .append(maxtime + delta) + .append(",") + .append(arrayVal[0]); + for (int i = 1; i < arrayVal.length; i++) { + content.append(",") + .append(arrayVal[i]); + } + content.append("]"); + } else { + content.append(val); + } + Files.write(Paths.get(path), content.toString().getBytes()); + } catch (IOException | InterruptedException e) { + System.out.println("skipping" + outpath + port + "/" + name); + } + } + + private static Object[] initVal(String simtimeVal) { + int simtime = 0; + Object[] val = new Object[] {}; + try { + Object[] arrayVal = new Map[] { literalEval(simtimeVal) }; + simtime = (int) arrayVal[0]; // assuming simtime is an integer + val = new Object[arrayVal.length - 1]; + System.arraycopy(arrayVal, 1, val, 0, val.length); + } catch (Exception e) { + e.printStackTrace(); + } + return val; + } + + private static Map literalEval(String s) { + + return new HashMap<>(); + } +} diff --git a/concoredocker.py b/concoredocker.py index e590d9d..5c5689d 100644 --- a/concoredocker.py +++ b/concoredocker.py @@ -1,23 +1,26 @@ import time from ast import literal_eval import re +import os -try: - iport = literal_eval(open("concore.iport").read()) -except: - iport = dict() -try: - oport = literal_eval(open("concore.oport").read()) -except: - oport = dict() - +def safe_literal_eval(filename, defaultValue): + try: + with open(filename, "r") as file: + return literal_eval(file.read()) + except (FileNotFoundError, SyntaxError, ValueError, Exception) as e: + print(f"Error reading {filename}: {e}") + return defaultValue + +iport = safe_literal_eval("concore.iport", {}) +oport = safe_literal_eval("concore.oport", {}) s = '' olds = '' delay = 1 retrycount = 0 -inpath = "/in" #must be abs path for docker -outpath = "/out" +inpath = os.path.abspath("/in") +outpath = os.path.abspath("/out") +simtime = 0 #9/21/22 try: @@ -35,68 +38,94 @@ print("bad params: "+sparams) except: params = dict() + #9/30/22 -def tryparam(n,i): - try: - return params[n] - except: - return i +def tryparam(n, i): + return params.get(n, i) #9/12/21 def default_maxtime(default): global maxtime - try: - maxtime = literal_eval(open(inpath+"1/concore.maxtime").read()) - except: - maxtime = default + maxtime = safe_literal_eval(os.path.join(inpath, "1", "concore.maxtime"), default) + default_maxtime(100) def unchanged(): - global olds,s - if olds==s: + global olds, s + if olds == s: s = '' return True - else: - olds = s - return False + olds = s + return False def read(port, name, initstr): - global s,simtime,retrycount + global s, simtime, retrycount + max_retries=5 time.sleep(delay) + file_path = os.path.join(inpath+str(port), name) + try: - infile = open(inpath+str(port)+"/"+name); - ins = infile.read() - except: + with open(file_path, "r") as infile: + ins = infile.read() + except FileNotFoundError: + print(f"File {file_path} not found, using default value.") ins = initstr - while len(ins)==0: + except Exception as e: + print(f"Error reading {file_path}: {e}") + return initstr + + attempts = 0 + while len(ins) == 0 and attempts < max_retries: time.sleep(delay) - ins = infile.read() + try: + with open(file_path, "r") as infile: + ins = infile.read() + except Exception as e: + print(f"Retry {attempts + 1}: Error reading {file_path} - {e}") + attempts += 1 retrycount += 1 + + if len(ins) == 0: + print(f"Max retries reached for {file_path}, using default value.") + return initstr + s += ins - inval = literal_eval(ins) - simtime = max(simtime,inval[0]) - return inval[1:] + try: + inval = literal_eval(ins) + simtime = max(simtime, inval[0]) + return inval[1:] + except Exception as e: + print(f"Error parsing {ins}: {e}") + return initstr + def write(port, name, val, delta=0): - global outpath,simtime - if isinstance(val,str): - time.sleep(2*delay) - elif isinstance(val,list)==False: - print("mywrite must have list or str") - quit() + global simtime + file_path = os.path.join(outpath+str(port), name) + + if isinstance(val, str): + time.sleep(2 * delay) + elif not isinstance(val, list): + print("write must have list or str") + return + try: - with open(outpath+str(port)+"/"+name,"w") as outfile: - if isinstance(val,list): - outfile.write(str([simtime+delta]+val)) + with open(file_path, "w") as outfile: + if isinstance(val, list): + outfile.write(str([simtime + delta] + val)) simtime += delta else: outfile.write(val) - except: - print("skipping"+outpath+str(port)+"/"+name); + except Exception as e: + print(f"Error writing to {file_path}: {e}") def initval(simtime_val): global simtime - val = literal_eval(simtime_val) - simtime = val[0] - return val[1:] + try: + val = literal_eval(simtime_val) + simtime = val[0] + return val[1:] + except Exception as e: + print(f"Error parsing simtime_val: {e}") + return [] diff --git a/contribute.py b/contribute.py index 194b692..43972f2 100644 --- a/contribute.py +++ b/contribute.py @@ -17,45 +17,36 @@ # Defining Functions def checkInputValidity(): - if AUTHOR_NAME=="" or STUDY_NAME=="" or STUDY_NAME=="": + if not AUTHOR_NAME or not STUDY_NAME or not STUDY_NAME_PATH: print("Please Provide necessary Inputs") - exit(0) + exit(1) if not os.path.isdir(STUDY_NAME_PATH): print("Directory doesnot Exists.Invalid Path") - exit(0) - - -def getPRs(upstream_repo): - try: - return upstream_repo.get_pulls(head=f'{BOT_ACCOUNT}:{BRANCH_NAME}') - except Exception as e: - print("Not able to fetch Status of your example.Please try after some time.") - exit(0) + exit(1) def printPR(pr): print(f'Check your example here https://github.com/{UPSTREAM_ACCOUNT}/{REPO_NAME}/pulls/{pr.number}',end="") def anyOpenPR(upstream_repo): - pr = getPRs(upstream_repo) - openPr=None - for i in pr: - if i.state=="open": - openPr=i - break - return openPr + try: + prs = upstream_repo.get_pulls(state='open', head=f'{BOT_ACCOUNT}:{BRANCH_NAME}') + return prs[0] if prs.totalCount > 0 else None + except Exception: + print("Unable to fetch PR status. Try again later.") + exit(1) def commitAndUpdateRef(repo,tree_content,commit,branch): try: new_tree = repo.create_git_tree(tree=tree_content,base_tree=commit.commit.tree) - new_commit = repo.create_git_commit("commit study",new_tree,[commit.commit]) + new_commit = repo.create_git_commit(f"Committing Study Named {STUDY_NAME}",new_tree,[commit.commit]) if len(repo.compare(base=commit.commit.sha,head=new_commit.sha).files) == 0: print("Your don't have any new changes.May be your example is already accepted.If this is not the case try with different fields.") - exit(0) + exit(1) ref = repo.get_git_ref("heads/"+branch.name) ref.edit(new_commit.sha,True) except Exception as e: print("failed to Upload your example.Please try after some time.",end="") - exit(0) + exit(1) def appendBlobInTree(repo,content,file_path,tree_content): @@ -65,31 +56,33 @@ def appendBlobInTree(repo,content,file_path,tree_content): def runWorkflow(repo,upstream_repo): openPR = anyOpenPR(upstream_repo) - if openPR==None: - workflow_runned = repo.get_workflow(id_or_name="pull_request.yml").create_dispatch(ref=BRANCH_NAME,inputs={'title':f"[BOT]: {PR_TITLE}",'body':PR_BODY,'upstreamRepo':UPSTREAM_ACCOUNT,'botRepo':BOT_ACCOUNT,'repo':REPO_NAME}) - if not workflow_runned: - print("Some error occured. Please try after some time") - exit(0) - else: + if not openPR: + try: + repo.get_workflow("pull_request.yml").create_dispatch( + ref=BRANCH_NAME, + inputs={'title': f"[BOT]: {PR_TITLE}", 'body': PR_BODY, 'upstreamRepo': UPSTREAM_ACCOUNT, 'botRepo': BOT_ACCOUNT, 'repo': REPO_NAME} + ) printPRStatus(upstream_repo) + except Exception as e: + print(f"Error triggering workflow. Try again later.\n ERROR: {e}") + exit(1) else: - print("Successfully uploaded all files, your example is in waiting.Please wait for us to accept it.",end="") - printPR(openPR) + print(f"Successfully uploaded. Waiting for approval: https://github.com/{UPSTREAM_ACCOUNT}/{REPO_NAME}/pull/{openPR.number}") def printPRStatus(upstream_repo): - try: - issues = upstream_repo.get_issues() - pulls = upstream_repo.get_pulls(state='all') - max_num = -1 - for i in issues: - max_num = max(max_num,i.number) - for i in pulls: - max_num = max(max_num,i.number) - time.sleep(4) - print(f'Check your example here https://github.com/{UPSTREAM_ACCOUNT}/{REPO_NAME}/pulls/{max_num+1}',end="") - except Exception as e: - print("Your example successfully uploaded but unable to fetch status. Please try again") - + attempts = 5 + delay = 2 + for i in range(attempts): + print(f"Attempt: {i}") + try: + latest_pr = upstream_repo.get_pulls(state='open', sort='created', direction='desc')[0] + print(f"Check your example here: https://github.com/{UPSTREAM_ACCOUNT}/{REPO_NAME}/pull/{latest_pr.number}") + return + except Exception: + time.sleep(delay) + delay *= 2 + print("Uploaded successfully, but unable to fetch status.") + def isImageFile(filename): image_extensions = ['.jpeg', '.jpg', '.png','.gif'] @@ -115,37 +108,34 @@ def decode_token(encoded_token): # Authenticating Github with Access token try: - if BRANCH_NAME=="#": - BRANCH_NAME=AUTHOR_NAME+"_"+STUDY_NAME - if PR_TITLE=="#": - PR_TITLE=f"Contributing Study {STUDY_NAME} by {AUTHOR_NAME}" - if PR_BODY=="#": - PR_BODY=f"Study Name: {STUDY_NAME} \n Author Name: {AUTHOR_NAME}" - AUTHOR_NAME = AUTHOR_NAME.replace(" ","_") + BRANCH_NAME = AUTHOR_NAME.replace(" ", "_") + "_" + STUDY_NAME if BRANCH_NAME == "#" else BRANCH_NAME.replace(" ", "_") + PR_TITLE = f"Contributing Study {STUDY_NAME} by {AUTHOR_NAME}" if PR_TITLE == "#" else PR_TITLE + PR_BODY = f"Study Name: {STUDY_NAME}\nAuthor Name: {AUTHOR_NAME}" if PR_BODY == "#" else PR_BODY DIR_PATH = STUDY_NAME + DIR_PATH = DIR_PATH.replace(" ","_") g = Github(decode_token(BOT_TOKEN)) repo = g.get_user(BOT_ACCOUNT).get_repo(REPO_NAME) upstream_repo = g.get_repo(f'{UPSTREAM_ACCOUNT}/{REPO_NAME}') #controlcore-Project/concore-studies base_ref = upstream_repo.get_branch(repo.default_branch) - branches = repo.get_branches() - BRANCH_NAME = BRANCH_NAME.replace(" ","_") - DIR_PATH = DIR_PATH.replace(" ","_") - is_present = any(branch.name == BRANCH_NAME for branch in branches) + + try: + repo.get_branch(BRANCH_NAME) + is_present = True + except github.GithubException: + print(f"No Branch is available with the name {BRANCH_NAME}") + is_present = False except Exception as e: print("Authentication failed", end="") - exit(0) + exit(1) try: - # If creating PR First Time - # Create New Branch for that exmaple if not is_present: - repo.create_git_ref(f'refs/heads/{BRANCH_NAME}', base_ref.commit.sha) - # Get current branch - branch = repo.get_branch(branch=BRANCH_NAME) -except Exception as e: - print("Not able to create study for you. Please try again after some time", end="") - exit(0) + repo.create_git_ref(f"refs/heads/{BRANCH_NAME}", base_ref.commit.sha) + branch = repo.get_branch(BRANCH_NAME) +except Exception: + print("Unable to create study. Try again later.") + exit(1) tree_content = [] @@ -170,4 +160,4 @@ def decode_token(encoded_token): except Exception as e: print(e) print("Some error Occured.Please try again after some time.",end="") - exit(0) \ No newline at end of file + exit(1) \ No newline at end of file diff --git a/fri/DOCKER-README.md b/fri/DOCKER-README.md index 459d362..036b571 100644 --- a/fri/DOCKER-README.md +++ b/fri/DOCKER-README.md @@ -2,78 +2,99 @@ Open Terminal and run the following commands:- -```` +``` $ sudo apt-get update $ sudo apt-get install curl $ curl -fsSL https://get.docker.com/ | sh -```` -Optional command:- To run docker commands without sudo -```` +``` + +Optional command:- To run docker commands without sudo + +``` $ sudo usermod -aG docker -```` -The above command add the system username in the docker group, Restart the system to complete the process. +``` -After restart run +The above command adds the system username to the docker group. Restart the system to complete the process. -```` -$ sudo service docker start -```` +After restart, run + +``` +$ sudo service docker start +``` # Building FRI Container Now, we elaborate on building FRI as a container, together with the Kong API Gateway. Connect to the Server VM, assuming x.x.x.x to be the IP address of your server. -```` + +``` $ ssh -i "controlcore.pem" ubuntu@x.x.x.x -```` +``` + Perform Git clone if this is the first time you are configuring the Server -```` + +``` $ git clone git@github.com/ControlCore-Project/concore.git -```` +``` -First build the Docker Container of the FRI. -```` +First build & run the Docker Container of the FRI. + +``` $ git pull +$ cd fri + $ docker build -t fri . -```` + +$ docker run -d --name fri -p 8090:8080 fri +``` # Running Control-Core FRI with Kong as containers -If you are already running FRI, make sure to stop and clear existing FRI container as it is likely conflict with the port. If there is Kong gateway running in default ports, stop and clear it too. -```` +If you are already running FRI, make sure to stop and clear existing FRI containers as they will likely conflict with the port. If there is a Kong gateway running in default ports, stop and clear it too. + +``` $ docker stop fri $ docker rm fri $ docker stop kong $ docker rm kong -```` +$ docker stop kong-database +$ docker rm kong-database +``` -Start and configure Cassandra container for Kong API. -```` +Start and configure PostgreSQL container for Kong API. + +``` $ docker run -d --name kong-database \ - -p 9042:9042 \ - cassandra:3 + -p 5432:5432 \ + -e POSTGRES_USER=kong \ + -e POSTGRES_DB=kong \ + -e POSTGRES_PASSWORD=kong \ + postgres:13 +``` +Run the Kong migrations for PostgreSQL. +``` $ docker run --rm \ --link kong-database:kong-database \ - -e "KONG_DATABASE=cassandra" \ + -e "KONG_DATABASE=postgres" \ -e "KONG_PG_HOST=kong-database" \ -e "KONG_PG_USER=kong" \ -e "KONG_PG_PASSWORD=kong" \ - -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \ - kong kong migrations bootstrap -```` + kong/kong-gateway:latest kong migrations bootstrap +``` Start Kong -```` + +``` $ docker run -d --name kong \ --link kong-database:kong-database \ - -e "KONG_DATABASE=cassandra" \ + -e "KONG_DATABASE=postgres" \ -e "KONG_PG_HOST=kong-database" \ + -e "KONG_PG_USER=kong" \ -e "KONG_PG_PASSWORD=kong" \ - -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \ -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \ -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \ -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \ @@ -83,62 +104,62 @@ $ docker run -d --name kong \ -p 8443:8443 \ -p 8001:8001 \ -p 8444:8444 \ - kong -```` + kong/kong-gateway:latest +``` Start FRI container -```` -$ nohup sudo docker run --name fri -p 8090:8080 fri > controlcore.out & -```` - -Delete if there is a previously configured Kong service. If not, skip this step. First you need to find the ID-VALUE for the route with a GET command before deleting the route and service. -```` -$ curl -X GET "http://localhost:8001/services/fri/routes" -```` -Use the ID output from above to issue the delete command as below (issue this only if you have a previous conflicting service definiton in kong. Otherwise, skip this step): -```` -$ curl -X DELETE "http://localhost:8001/services/fri/routes/ID-VALUE" -$ curl -X DELETE "http://localhost:8001/services/fri/" -```` +``` +$ nohup sudo docker run --name fri -p 8090:8080 fri > controlcore.out & +``` Define Kong Service and Route. -First Configure a Kong service, replacing the variable "private-ip" with the private IP address of your server below. -```` +First, configure a Kong service, replacing the variable "private-ip" with the private IP address of your server below. + +``` $ curl -i -X POST --url http://localhost:8001/services/ --data 'name=fri' --data 'url=http://private-ip:8090' -```` -Then configure route to the service -```` +``` + +Then configure a route to the service + +``` $ curl -i -X POST --url http://localhost:8001/services/fri/routes --data 'paths=/' -```` +``` Now, controlcore.org is routed through the Kong APIs. - # Troubleshooting the FRI Connect to the Server VM -```` + +``` $ ssh -i "controlcore.pem" ubuntu@x.x.x.x -```` +``` + Check the Server logs. -```` + +``` $ tail -f controlcore.out -```` +``` + or -```` + +``` $ docker logs fri -f -```` +``` + Find the FRI docker container -```` + +``` $ docker ps -```` -CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -dfdd3b3d3308 fri "python main.py" 38 minutes ago Up 38 minutes 0.0.0.0:80->80/tcp fri +``` + +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dfdd3b3d3308 fri "python main.py" 38 minutes ago Up 38 minutes 0.0.0.0:80->80/tcp fri Access the container -```` + +``` $ docker exec -it dfdd /bin/bash -```` +``` diff --git a/fri/Dockerfile b/fri/Dockerfile index ad37113..450274f 100644 --- a/fri/Dockerfile +++ b/fri/Dockerfile @@ -1,9 +1,18 @@ -FROM centos/python-36-centos7 +FROM python:3.6-slim + +RUN apt-get update && apt-get install -y ca-certificates \ + && update-ca-certificates \ + && python3 -m ensurepip --default-pip \ + && pip install --upgrade pip setuptools wheel + COPY requirements.txt /tmp/requirements.txt -RUN pip install -r /tmp/requirements.txt +RUN pip install -r /tmp/requirements.txt + WORKDIR /fri -COPY server /fri +COPY server /fri + USER root -RUN useradd fri && chown -R fri /fri +RUN useradd fri && chown -R fri /fri + USER fri CMD ["gunicorn", "--timeout=180", "--workers=4", "--bind=0.0.0.0:8080", "--access-logfile=-", "main:app"] \ No newline at end of file diff --git a/fri/docker-compose.yml b/fri/docker-compose.yml index 542536f..71bd5a2 100644 --- a/fri/docker-compose.yml +++ b/fri/docker-compose.yml @@ -9,20 +9,39 @@ services: - 8090:8080 command: tail -f /dev/null - kong-database: - image: cassandra:3 + postgres: + image: postgres:13 + container_name: kong-database + restart: always + environment: + POSTGRES_USER: kong + POSTGRES_PASSWORD: kong + POSTGRES_DB: kong ports: - - 9042:9042 + - 5432:5432 + volumes: + - postgres_data:/var/lib/postgresql/data + + kong-migrations: + image: kong + depends_on: + - postgres + environment: + KONG_DATABASE: postgres + KONG_PG_HOST: kong-database + KONG_PG_USER: kong + KONG_PG_PASSWORD: kong + command: kong migrations bootstrap kong: image: kong depends_on: - - kong-database + - kong-migrations environment: - KONG_DATABASE: cassandra + KONG_DATABASE: postgres KONG_PG_HOST: kong-database + KONG_PG_USER: kong KONG_PG_PASSWORD: kong - KONG_CASSANDRA_CONTACT_POINTS: kong-database KONG_PROXY_ACCESS_LOG: /dev/stdout KONG_ADMIN_ACCESS_LOG: /dev/stdout KONG_PROXY_ERROR_LOG: /dev/stderr @@ -34,4 +53,5 @@ services: - 8001:8001 - 8444:8444 - \ No newline at end of file +volumes: + postgres_data: \ No newline at end of file diff --git a/fri/server/main.py b/fri/server/main.py index 764993f..f94bc66 100644 --- a/fri/server/main.py +++ b/fri/server/main.py @@ -149,7 +149,7 @@ def build(dir): if(out_dir == None or out_dir == ""): if(docker == 'true'): try: - output_bytes = subprocess.check_output(["./makedocker", makestudy_dir], cwd=concore_path) + output_bytes = subprocess.check_output([r"./makedocker", makestudy_dir], cwd=concore_path) output_str = output_bytes.decode("utf-8") proc = 0 except subprocess.CalledProcessError as e: @@ -157,7 +157,7 @@ def build(dir): proc = 1 else: try: - output_bytes = subprocess.check_output(["./makestudy", makestudy_dir], cwd=concore_path) + output_bytes = subprocess.check_output([r"./makestudy", makestudy_dir], cwd=concore_path) output_str = output_bytes.decode("utf-8") proc = 0 except subprocess.CalledProcessError as e: @@ -166,7 +166,7 @@ def build(dir): else: if(docker == 'true'): try: - output_bytes = subprocess.check_output(["./makedocker", makestudy_dir, out_dir], cwd=concore_path) + output_bytes = subprocess.check_output([r"./makedocker", makestudy_dir, out_dir], cwd=concore_path) output_str = output_bytes.decode("utf-8") proc = 0 except subprocess.CalledProcessError as e: @@ -174,7 +174,7 @@ def build(dir): proc = 1 else: try: - output_bytes = subprocess.check_output(["./makestudy", makestudy_dir, out_dir], cwd=concore_path) + output_bytes = subprocess.check_output([r"./makestudy", makestudy_dir, out_dir], cwd=concore_path) output_str = output_bytes.decode("utf-8") proc = 0 except subprocess.CalledProcessError as e: @@ -201,7 +201,7 @@ def build(dir): proc=call(["params", params],shell=True, cwd=dir_path) else: try: - output_bytes = subprocess.check_output("./build", cwd=dir_path) + output_bytes = subprocess.check_output(r"./build", cwd=dir_path) output_str = output_str + output_bytes.decode("utf-8") resp = jsonify({'message': 'Directory successfully created', 'output': output_str}) except subprocess.CalledProcessError as e: @@ -209,9 +209,9 @@ def build(dir): resp = jsonify({'message': 'Build Failed', 'output': output_str}) resp.status_code = 500 if(maxtime != None and maxtime != ''): - proc=call(["./maxtime", maxtime], cwd=dir_path) + proc=call([r"./maxtime", maxtime], cwd=dir_path) if(params != None and params != ''): - proc=call(["./params", params], cwd=dir_path) + proc=call([r"./params", params], cwd=dir_path) return resp @app.route('/debug/', methods=['POST']) @@ -221,7 +221,7 @@ def debug(dir): if(platform.uname()[0]=='Windows'): proc = call(["debug"],shell=True, cwd=dir_path) else: - proc = call(["./debug"], cwd=dir_path) + proc = call([r"./debug"], cwd=dir_path) if(proc == 0): resp = jsonify({'message': 'Close the pop window after obtaining result'}) resp.status_code = 201 @@ -238,7 +238,7 @@ def run(dir): if(platform.uname()[0]=='Windows'): proc = call(["run"],shell=True, cwd=dir_path) else: - proc = call(["./run"], cwd=dir_path) + proc = call([r"./run"], cwd=dir_path) if(proc == 0): resp = jsonify({'message': 'result prepared'}) resp.status_code = 201 @@ -255,7 +255,7 @@ def stop(dir): if(platform.uname()[0]=='Windows'): proc = call(["stop"],shell=True, cwd=dir_path) else: - proc = call(["./stop"], cwd=dir_path) + proc = call([r"./stop"], cwd=dir_path) if(proc == 0): resp = jsonify({'message': 'resources cleaned'}) resp.status_code = 201 @@ -280,11 +280,11 @@ def clear(dir): if(params != None and params != ''): proc = call(["params", params],shell=True, cwd=dir_path) else: - proc = call(["./clear"], cwd=dir_path) + proc = call([r"./clear"], cwd=dir_path) if(maxtime != None and maxtime != ''): - proc = call(["./maxtime", maxtime], cwd=dir_path) + proc = call([r"./maxtime", maxtime], cwd=dir_path) if(params != None and params != ''): - proc = call(["./params", params], cwd=dir_path) + proc = call([r"./params", params], cwd=dir_path) if(proc == 0): resp = jsonify({'message': 'result deleted'}) resp.status_code = 201 @@ -308,9 +308,9 @@ def contribute(): proc=check_output(["contribute",STUDY_NAME,STUDY_NAME_PATH,AUTHOR_NAME,BRANCH_NAME,PR_TITLE,PR_BODY],cwd=concore_path,shell=True) else: if len(BRANCH_NAME)==0: - proc = check_output(["./contribute",STUDY_NAME,STUDY_NAME_PATH,AUTHOR_NAME],cwd=concore_path) + proc = check_output([r"./contribute",STUDY_NAME,STUDY_NAME_PATH,AUTHOR_NAME],cwd=concore_path) else: - proc = check_output(["./contribute",STUDY_NAME,STUDY_NAME_PATH,AUTHOR_NAME,BRANCH_NAME,PR_TITLE,PR_BODY],cwd=concore_path) + proc = check_output([r"./contribute",STUDY_NAME,STUDY_NAME_PATH,AUTHOR_NAME,BRANCH_NAME,PR_TITLE,PR_BODY],cwd=concore_path) output_string = proc.decode() status=200 if output_string.find("/pulls/")!=-1: @@ -349,7 +349,7 @@ def destroy(dir): if(platform.uname()[0]=='Windows'): proc = call(["destroy", dir],shell=True, cwd=concore_path) else: - proc = call(["./destroy", dir], cwd=concore_path) + proc = call([r"./destroy", dir], cwd=concore_path) if(proc == 0): resp = jsonify({'message': 'Successfuly deleted Dirctory'}) resp.status_code = 201 @@ -367,11 +367,11 @@ def library(dir): library_path = request.args.get('path') proc = 0 if (library_path == None or library_path == ''): - library_path = "../tools" + library_path = r"../tools" if(platform.uname()[0]=='Windows'): - proc = subprocess.check_output(["..\library", library_path, filename],shell=True, cwd=dir_path) + proc = subprocess.check_output([r"..\library", library_path, filename],shell=True, cwd=dir_path) else: - proc = subprocess.check_output(["../library", library_path, filename], cwd=dir_path) + proc = subprocess.check_output([r"../library", library_path, filename], cwd=dir_path) if(proc != 0): resp = jsonify({'message': proc.decode("utf-8")}) resp.status_code = 201 diff --git a/fri/test.py b/fri/test.py index fe7e396..eabcf4c 100644 --- a/fri/test.py +++ b/fri/test.py @@ -1,11 +1,9 @@ -from cgi import test import requests import os import urllib.request import time # function to test upload() method. - def upload(files): url = "http://127.0.0.1:5000/upload/test?apikey=xyz" payload={} @@ -62,8 +60,13 @@ def openJupyter(): # function to test download() method. def download(dir, subDir, fileName , apikey ): - url = "http://127.0.0.1:5000/download/"+dir+"?"+"fetchDir="+subDir+"&"+"fetch="+ fileName+"&"+"apikey="+apikey - urllib.request.urlretrieve(url, fileName) + url = "http://127.0.0.1:5000/download/" + dir + "?" + "fetchDir=" + subDir + "&" + "fetch=" + fileName + "&" + "apikey=" + apikey + response = requests.get(url) + if response.status_code == 200: + with open(fileName, 'wb') as f: + f.write(response.content) + else: + print(f"Failed to download: {response.status_code}, {response.text}") # file list to be uploaded cur_path = os.path.dirname(os.path.abspath(__file__)) diff --git a/makestudy b/makestudy index 310de02..382cb6f 100755 --- a/makestudy +++ b/makestudy @@ -1,7 +1,7 @@ -#!/bin/bash -if [ $# == 0 ] -then - echo "Make a concore study " +#!/usr/bin/env bash + +if [ "$#" -eq 0 ]; then + echo "Make a concore study" echo " ./makestudy path/name.graphml" echo " ./makestudy path/name.graphml study" echo "In the first case, the name of the study is the same as the name of the .graphml" @@ -9,38 +9,37 @@ then echo "The equivalent mkconcore.py is displayed" exit fi -graphml=$1 -if [ -e $graphml ] -then - sourcedir=$(dirname "$graphml") + +graphml="$1" + +if [ -e "$graphml" ]; then + sourcedir="$(dirname "$graphml")" else - graphml=$graphml'.graphml' - if [ -e $graphml ] - then - sourcedir=$(dirname "$graphml") + graphml="${graphml}.graphml" + if [ -e "$graphml" ]; then + sourcedir="$(dirname "$graphml")" else echo "$graphml does not exist" exit fi fi -if [ $# = 1 ] -then - studydir=`basename ${graphml%\.*}` + +if [ "$#" -eq 1 ]; then + studydir="$(basename "${graphml%.*}")" else - studydir=$2 + studydir="$2" fi -if [ -e $studydir ] -then - echo "cannot make $studydir because one already exists with that name" - echo "either do ./destroy $studydir, or choose a unique name as 2nd arg" + +if [ -e "$studydir" ]; then + echo "Cannot make $studydir because one already exists with that name" + echo "Either do ./destroy $studydir, or choose a unique name as 2nd arg" + exit else - which osascript >/dev/null - if [ $? == 0 ] - then - echo "python3 mkconcore.py $graphml $sourcedir $studydir macos" - python3 mkconcore.py $graphml $sourcedir $studydir macos + if command -v osascript >/dev/null; then + echo "python3 mkconcore.py \"$graphml\" \"$sourcedir\" \"$studydir\" macos" + python3 mkconcore.py "$graphml" "$sourcedir" "$studydir" macos else - echo "python3 mkconcore.py $graphml $sourcedir $studydir ubuntu" - python3 mkconcore.py $graphml $sourcedir $studydir ubuntu + echo "python3 mkconcore.py \"$graphml\" \"$sourcedir\" \"$studydir\" ubuntu" + python3 mkconcore.py "$graphml" "$sourcedir" "$studydir" ubuntu fi -fi +fi \ No newline at end of file diff --git a/makestudy.bat b/makestudy.bat index 118b3dc..26bf995 100644 --- a/makestudy.bat +++ b/makestudy.bat @@ -1,18 +1,34 @@ @echo off -if dummy%~n2 == dummy ( - if dummy%~x1 == dummy.graphml ( - echo python mkconcore.py %~d1%~p1%~n1%~x1 %~d1%~p1 %~n1 windows - python mkconcore.py %~d1%~p1%~n1%~x1 %~d1%~p1 %~n1 windows - ) else ( - echo python mkconcore.py %~d1%~p1%~n1.graphml %~d1%~p1 %~n1 windows - python mkconcore.py %~d1%~p1%~n1.graphml %~d1%~p1 %~n1 windows - ) +setlocal EnableDelayedExpansion + +:: Extracting parameters safely +set "file1=%~f1" +set "file2=%~2" +set "dir1=%~dp1" +set "name1=%~n1" +set "ext1=%~x1" + +:: Handling spaces in paths by ensuring the format remains intact +set "file1=!file1:\=\\!" +set "dir1=!dir1:\=\\!" + +:: If the second argument (file2) is not provided +if not defined file2 ( + if /I "%ext1%"==".graphml" ( + echo python mkconcore.py "!file1!" "!dir1!" "!name1!" windows + python mkconcore.py "!file1!" "!dir1!" "!name1!" windows + ) else ( + echo python mkconcore.py "!dir1!!name1!.graphml" "!dir1!" "!name1!" windows + python mkconcore.py "!dir1!!name1!.graphml" "!dir1!" "!name1!" windows + ) ) else ( - if dummy%~x1 == dummy.graphml ( - echo python mkconcore.py %~d1%~p1%~n1%~x1 %~d1%~p1 %~n2 windows - python mkconcore.py %~d1%~p1%~n1%~x1 %~d1%~p1 %~n2 windows - ) else ( - echo python mkconcore.py %~d1%~p1%~n1.graphml %~d1%~p1 %~n2 windows - python mkconcore.py %~d1%~p1%~n1.graphml %~d1%~p1 %~n2 windows - ) -) \ No newline at end of file + if /I "%ext1%"==".graphml" ( + echo python mkconcore.py "!file1!" "!dir1!" "!file2!" windows + python mkconcore.py "!file1!" "!dir1!" "!file2!" windows + ) else ( + echo python mkconcore.py "!dir1!!name1!.graphml" "!dir1!" "!file2!" windows + python mkconcore.py "!dir1!!name1!.graphml" "!dir1!" "!file2!" windows + ) +) + +endlocal diff --git a/mkconcore.py b/mkconcore.py index b5330a3..a608ffb 100644 --- a/mkconcore.py +++ b/mkconcore.py @@ -15,7 +15,6 @@ CPPEXE = "g++" #Ubuntu/macOS C++ 6/22/21 VWIN = "iverilog" #Windows verilog 6/25/21 VEXE = "iverilog" #Ubuntu/macOS verilog 6/25/21 -CPPEXE = "g++" #Ubuntu/macOS C++ 6/22/21 PYTHONEXE = "python3" #Ubuntu/macOS python 3 PYTHONWIN = "python" #Windows python 3 MATLABEXE = "matlab" #Ubuntu/macOS matlab @@ -29,6 +28,11 @@ INDIRNAME = ":/in" OUTDIRNAME = ":/out" +logging.basicConfig( + level=logging.INFO, + format='%(message)s' if TRIMMED_LOGS else '%(asctime)s %(levelname)-8s %(message)s', + datefmt='%Y-%m-%d %H:%M:%S' +) if os.path.exists(CONCOREPATH+"/concore.octave"): M_IS_OCTAVE = True #treat .m as octave 9/27/21 @@ -47,12 +51,12 @@ sourcedir = sys.argv[2] outdir = sys.argv[3] if not os.path.isdir(sourcedir): - print(sourcedir+" does not exist") + logging.error(f"{sourcedir} does not exist") quit() if len(sys.argv) < 4: - print("usage: py mkconcore.py file.graphml sourcedir outdir [type]") - print(" type must be posix (macos or ubuntu), windows, or docker") + logging.error("usage: py mkconcore.py file.graphml sourcedir outdir [type]") + logging.error(" type must be posix (macos or ubuntu), windows, or docker") quit() elif len(sys.argv) == 4: prefixedgenode = outdir+"_" #nodes and edges prefixed with outdir_ only in case no type specified 3/24/21 @@ -60,7 +64,7 @@ else: concoretype = sys.argv[4] if not (concoretype in ["posix","windows","docker","macos","ubuntu"]): - print(" type must be posix (macos or ubuntu), windows, or docker") + logging.error(" type must be posix (macos or ubuntu), windows, or docker") quit() ubuntu = False #6/24/21 if concoretype == "ubuntu": @@ -70,11 +74,10 @@ concoretype = "posix" if os.path.exists(outdir): - print(outdir+" already exists") - print("if intended, remove or rename "+outdir+" first") + logging.error(f"{outdir} already exists") + logging.error(f"if intended, Remove/Rename {outdir} first") quit() -currentdir = os.getcwd() os.mkdir(outdir) os.chdir(outdir) if concoretype == "windows": @@ -99,22 +102,16 @@ os.mkdir("src") os.chdir("..") - -print("mkconcore "+MKCONCORE_VER) -print("concore path: "+CONCOREPATH) -print("graphml input: "+GRAPHML_FILE) -print("source directory: "+sourcedir) -print("output directory: "+outdir) -print("control core type: "+concoretype) -print("treat .m as octave:"+str(M_IS_OCTAVE)) -print("MCR path: "+MCRPATH) -print("Docker repository: "+DOCKEREPO) - -# Output in a preferred format. -if TRIMMED_LOGS: - logging.basicConfig(level=logging.INFO, format='%(message)s') -else: - logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S') + +logging.info(f"mkconcore {MKCONCORE_VER}") +logging.info(f"Concore path: {CONCOREPATH}") +logging.info(f"graphml input: {GRAPHML_FILE}") +logging.info(f"source directory: {sourcedir}") +logging.info(f"output directory: {outdir}") +logging.info(f"control core type: {concoretype}") +logging.info(f"treat .m as octave: {str(M_IS_OCTAVE)}") +logging.info(f"MCR path: {MCRPATH}") +logging.info(f"Docker repository: {DOCKEREPO}") f = open(GRAPHML_FILE, "r") text_str = f.read() @@ -183,7 +180,7 @@ ms += mp if (ms == 0).any(): - print("not all nodes reachable") + logging.warning("Unreachable nodes detected") #not right for PM2_1_1 and PM2_1_2 volswr = len(nodes_dict)*[''] @@ -213,7 +210,7 @@ try: fsource = open(sourcedir+"/"+sourcecode) except: - print(sourcecode+" does not exist in "+sourcedir) + logging.error(f"{sourcecode} not found in {sourcedir}") quit() with open(outdir+"/src/"+sourcecode,"w") as fcopy: fcopy.write(fsource.read()) @@ -223,9 +220,9 @@ fsource = open(sourcedir+"/Dockerfile."+dockername) with open(outdir+"/src/Dockerfile."+dockername,"w") as fcopy: fcopy.write(fsource.read()) - print(" Using custom Dockerfile for "+dockername) + logging.info(f"Using custom Dockerfile for {dockername}") except: - print(" Using default Dockerfile for "+dockername) + logging.info(f"Using default Dockerfile for {dockername}") fsource.close() if os.path.isdir(sourcedir+"/"+dockername+".dir"): shutil.copytree(sourcedir+"/"+dockername+".dir",outdir+"/src/"+dockername+".dir") @@ -237,7 +234,7 @@ else: fsource = open(CONCOREPATH+"/concore.py") except: - print(CONCOREPATH+" is not correct path to concore") + logging.error(f"{CONCOREPATH} is not correct path to concore") quit() with open(outdir+"/src/concore.py","w") as fcopy: fcopy.write(fsource.read()) @@ -250,7 +247,7 @@ else: fsource = open(CONCOREPATH+"/concore.hpp") except: - print(CONCOREPATH+" is not correct path to concore") + logging.error(f"{CONCOREPATH} is not correct path to concore") quit() with open(outdir+"/src/concore.hpp","w") as fcopy: fcopy.write(fsource.read()) @@ -263,7 +260,7 @@ else: fsource = open(CONCOREPATH+"/concore.v") except: - print(CONCOREPATH+" is not correct path to concore") + logging.error(f"{CONCOREPATH} is not correct path to concore") quit() with open(outdir+"/src/concore.v","w") as fcopy: fcopy.write(fsource.read()) @@ -273,7 +270,7 @@ try: fsource = open(CONCOREPATH+"/mkcompile") except: - print(CONCOREPATH+" is not correct path to concore") + logging.error(f"{CONCOREPATH} is not correct path to concore") quit() with open(outdir+"/src/mkcompile","w") as fcopy: fcopy.write(fsource.read()) @@ -284,7 +281,7 @@ try: #maxtime in matlab 11/22/21 fsource = open(CONCOREPATH+"/concore_default_maxtime.m") except: - print(CONCOREPATH+" is not correct path to concore") + logging.error(f"{CONCOREPATH} is not correct path to concore") quit() with open(outdir+"/src/concore_default_maxtime.m","w") as fcopy: fcopy.write(fsource.read()) @@ -292,7 +289,7 @@ try: fsource = open(CONCOREPATH+"/concore_unchanged.m") except: - print(CONCOREPATH+" is not correct path to concore") + logging.error(f"{CONCOREPATH} is not correct path to concore") quit() with open(outdir+"/src/concore_unchanged.m","w") as fcopy: fcopy.write(fsource.read()) @@ -300,7 +297,7 @@ try: fsource = open(CONCOREPATH+"/concore_read.m") except: - print(CONCOREPATH+" is not correct path to concore") + logging.error(f"{CONCOREPATH} is not correct path to concore") quit() with open(outdir+"/src/concore_read.m","w") as fcopy: fcopy.write(fsource.read()) @@ -308,7 +305,7 @@ try: fsource = open(CONCOREPATH+"/concore_write.m") except: - print(CONCOREPATH+" is not correct path to concore") + logging.error(f"{CONCOREPATH} is not correct path to concore") quit() with open(outdir+"/src/concore_write.m","w") as fcopy: fcopy.write(fsource.read()) @@ -316,7 +313,7 @@ try: #4/9/21 fsource = open(CONCOREPATH+"/concore_initval.m") except: - print(CONCOREPATH+" is not correct path to concore") + logging.error(f"{CONCOREPATH} is not correct path to concore") quit() with open(outdir+"/src/concore_initval.m","w") as fcopy: fcopy.write(fsource.read()) @@ -324,7 +321,7 @@ try: #11/19/21 fsource = open(CONCOREPATH+"/concore_iport.m") except: - print(CONCOREPATH+" is not correct path to concore") + logging.error(f"{CONCOREPATH} is not correct path to concore") quit() with open(outdir+"/src/concore_iport.m","w") as fcopy: fcopy.write(fsource.read()) @@ -332,7 +329,7 @@ try: #11/19/21 fsource = open(CONCOREPATH+"/concore_oport.m") except: - print(CONCOREPATH+" is not correct path to concore") + logging.error(f"{CONCOREPATH} is not correct path to concore") quit() with open(outdir+"/src/concore_oport.m","w") as fcopy: fcopy.write(fsource.read()) @@ -343,7 +340,7 @@ else: fsource = open(CONCOREPATH+"/import_concore.m") except: - print(CONCOREPATH+" is not correct path to concore") + logging.error(f"{CONCOREPATH} is not correct path to concore") quit() with open(outdir+"/src/import_concore.m","w") as fcopy: fcopy.write(fsource.read()) @@ -361,7 +358,7 @@ if len(sourcecode)!=0 and sourcecode.find(".")!=-1: #3/28/21 dockername,langext = sourcecode.split(".") if os.path.exists(outdir+"/src/"+dockername+".iport"): - print("warning: "+dockername+" has multiple instantiations; iport/oport may be invalid") + logging.warning(f"{dockername} has multiple instantiations ; iport/oport may be invalid") with open(outdir+"/src/"+dockername+".iport", "w") as fport: if prefixedgenode == "": # 5/18/21 fport.write(str(iportmap_dict)) @@ -402,21 +399,21 @@ try: if langext=="py": fsource = open(CONCOREPATH+"/Dockerfile.py") - print("assuming .py extension for Dockerfile") + logging.info("assuming .py extension for Dockerfile") elif langext == "cpp": # 6/22/21 fsource = open(CONCOREPATH+"/Dockerfile.cpp") - print("assuming .cpp extension for Dockerfile") + logging.info("assuming .cpp extension for Dockerfile") elif langext == "v": # 6/26/21 fsource = open(CONCOREPATH+"/Dockerfile.v") - print("assuming .v extension for Dockerfile") + logging.info("assuming .v extension for Dockerfile") elif langext == "sh": # 5/19/21 fsource = open(CONCOREPATH+"/Dockerfile.sh") - print("assuming .sh extension for Dockerfile") + logging.info("assuming .sh extension for Dockerfile") else: - print("assuming .m extension for Dockerfile") fsource = open(CONCOREPATH+"/Dockerfile.m") + logging.info("assuming .m extension for Dockerfile") except: - print(CONCOREPATH+" is not correct path to concore") + logging.error(f"{CONCOREPATH} is not correct path to concore") quit() with open(outdir+"/src/Dockerfile."+dockername,"w") as fcopy: fcopy.write(fsource.read()) @@ -469,11 +466,11 @@ containername,sourcecode = nodes_dict[node].split(':') if len(sourcecode)!=0: if sourcecode.find(".")==-1: - print(DOCKEREXE+' run --name='+containername+volswr[i]+volsro[i]+" "+DOCKEREPO+"/docker-"+sourcecode) + logging.debug(f"Generating Docker run command: {DOCKEREXE} run --name={containername+volswr[i]+volsro[i]} {DOCKEREPO}/docker- {sourcecode}") frun.write(DOCKEREXE+' run --name='+containername+volswr[i]+volsro[i]+" "+DOCKEREPO+"/docker-"+sourcecode+"&\n") else: dockername,langext = sourcecode.split(".") - print(DOCKEREXE+' run --name='+containername+volswr[i]+volsro[i]+" docker-"+dockername) + logging.debug(f"Generating Docker run command for {dockername}: {DOCKEREXE} run --name={containername+volswr[i]+volsro[i]} docker-{dockername}") frun.write(DOCKEREXE+' run --name='+containername+volswr[i]+volsro[i]+" docker-"+dockername+"&\n") #if langext != "m": #3/27/21 # print(DOCKEREXE+' run --name='+containername+volswr[i]+volsro[i]+" docker-"+dockername) @@ -653,7 +650,7 @@ containername,sourcecode = nodes_dict[node].split(':') if len(sourcecode)!=0: if sourcecode.find(".")==-1: - print("cannot pull container "+sourcecode+" with control core type "+concoretype) #3/28/21 + logging.error("cannot pull container "+sourcecode+" with control core type "+concoretype) #3/28/21 quit() dockername,langext = sourcecode.split(".") fbuild.write('mkdir '+containername+"\n") @@ -698,7 +695,6 @@ #make directories equivalent to volumes for edges in edges_dict: - #print("mkdir "+edges) fbuild.write("mkdir "+edges+"\n") #make links for out directories @@ -743,7 +739,7 @@ if len(sourcecode)!=0: dockername,langext = sourcecode.split(".") if not (langext in ["py","m","sh","cpp","v"]): # 6/22/21 - print("."+langext+" not supported (Yet)") + logging.error(f"Extension .{langext} is unsupported") quit() if concoretype=="windows": if langext=="py": @@ -778,57 +774,59 @@ frun.write('start /B /D '+containername+" "+MATLABWIN+' -batch "run('+"'"+sourcecode+"'"+')"'+" >"+containername+"\\concoreout.txt\n") fdebug.write('start /D '+containername+" cmd /K " +MATLABWIN+' -batch "run('+"'"+sourcecode+"'"+')"'+"\n") else: - if langext=="py": - frun.write('(cd '+containername+";"+PYTHONEXE+" "+sourcecode+" >concoreout.txt&echo $!>concorepid)&\n") - if ubuntu: - fdebug.write('concorewd=`pwd`\n') - fdebug.write('xterm -e bash -c "cd $concorewd/'+containername+";"+PYTHONEXE+" "+sourcecode+';bash"&\n') - else: - fdebug.write('concorewd=`pwd`\n') - fdebug.write('osascript -e "tell application \\"Terminal\\" to do script \\"cd $concorewd/'+containername+";"+PYTHONEXE+" "+sourcecode+'\\""\n') - elif langext=="cpp": # 6/22/21 - frun.write('(cd '+containername+";"+CPPEXE+" "+sourcecode+";./a.out >concoreout.txt&echo $!>concorepid)&\n") - if ubuntu: - fdebug.write('concorewd=`pwd`\n') - fdebug.write('xterm -e bash -c "cd $concorewd/'+containername+";"+CPPEXE+" "+sourcecode+';./a.out;bash"&\n') - else: - fdebug.write('concorewd=`pwd`\n') - fdebug.write('osascript -e "tell application \\"Terminal\\" to do script \\"cd $concorewd/'+containername+";"+CPPEXE+" "+sourcecode+';./a.out\\""\n') - elif langext=="v": # 6/25/21 - frun.write('(cd '+containername+";"+VEXE+" "+sourcecode+";./a.out >concoreout.txt&echo $!>concorepid)&\n") - if ubuntu: - fdebug.write('concorewd=`pwd`\n') - fdebug.write('xterm -e bash -c "cd $concorewd/'+containername+";"+VEXE+" "+sourcecode+';./a.out;bash"&\n') - else: - fdebug.write('concorewd=`pwd`\n') - fdebug.write('osascript -e "tell application \\"Terminal\\" to do script \\"cd $concorewd/'+containername+";"+VEXE+" "+sourcecode+';vvp a.out\\""\n') - elif langext=="sh": # 5/19/21 - frun.write('(cd '+containername+";./"+sourcecode+" "+ MCRPATH + " >concoreout.txt&echo $!>concorepid)&\n") - if ubuntu: - fdebug.write('concorewd=`pwd`\n') - fdebug.write('xterm -e bash -c "cd $concorewd/'+containername+";./"+sourcecode+' '+MCRPATH+';bash"&\n') - else: # 11/23/21 MCRPATH - fdebug.write('concorewd=`pwd`\n') - fdebug.write('osascript -e "tell application \\"Terminal\\" to do script \\"cd $concorewd/'+containername+";./"+sourcecode+' '+MCRPATH+'\\""\n') - elif langext=="m": #3/23/21 - if M_IS_OCTAVE: - frun.write('(cd '+containername+";"+ OCTAVEEXE+' -qf --eval run\\(\\'+"'"+sourcecode+"\\'"+'\\)'+" >concoreout.txt&echo $!>concorepid)&\n") - if ubuntu: - fdebug.write('concorewd=`pwd`\n') - fdebug.write('xterm -e bash -c "cd $concorewd/'+containername+";"+OCTAVEEXE+' -qf --eval run\\(\\'+"'"+sourcecode+"\\'"+'\\);bash"&'+"\n") - else: - fdebug.write('concorewd=`pwd`\n') - fdebug.write('osascript -e "tell application \\"Terminal\\" to do script \\"cd $concorewd/'+containername+";"+OCTAVEEXE+' -qf --eval run\\\\\\(\\\\\\'+"'"+sourcecode+"\\\\\\'"+'\\\\\\)\\""'+"\n") - else: # 4/2/21 - frun.write('(cd ' -+containername+";"+ MATLABEXE+' -batch run\\(\\'+"'"+sourcecode+"\\'"+'\\)'+" >concoreout.txt&echo $!>concorepid)&\n") - if ubuntu: - fdebug.write('concorewd=`pwd`\n') - fdebug.write('xterm -e bash -c "cd $concorewd/' +containername+";"+ MATLABEXE+' -batch run\\(\\'+"'"+sourcecode+"\\'"+'\\);bash"&\n' ) - else: - fdebug.write('concorewd=`pwd`\n') - fdebug.write('osascript -e "tell application \\"Terminal\\" to do script \\"cd $concorewd/' +containername+";"+ MATLABEXE+' -batch run\\\\\\(\\\\\\'+"'"+sourcecode+"\\\\\\'"+'\\\\\\)\\""\n' ) - + if langext == "py": + frun.write('(cd "' + containername + '"; ' + PYTHONEXE + ' ' + sourcecode + ' >concoreout.txt & echo $! >concorepid) &\n') + if ubuntu: + fdebug.write('concorewd="$(pwd)"\n') + fdebug.write('xterm -e bash -c "cd \\"$concorewd/' + containername + '\\"; ' + PYTHONEXE + ' ' + sourcecode + '; bash" &\n') + else: + fdebug.write('concorewd="$(pwd)"\n') + fdebug.write('osascript -e "tell application \\"Terminal\\" to do script \\"cd \\\\\\"$concorewd/' + containername + '\\\\\\"; ' + PYTHONEXE + ' ' + sourcecode + '\\"" \n') + + elif langext == "cpp": # 6/22/21 + frun.write('(cd "' + containername + '"; ' + CPPEXE + ' ' + sourcecode + '; ./a.out >concoreout.txt & echo $! >concorepid) &\n') + if ubuntu: + fdebug.write('concorewd="$(pwd)"\n') + fdebug.write('xterm -e bash -c "cd \\"$concorewd/' + containername + '\\"; ' + CPPEXE + ' ' + sourcecode + '; ./a.out; bash" &\n') + else: + fdebug.write('concorewd="$(pwd)"\n') + fdebug.write('osascript -e "tell application \\"Terminal\\" to do script \\"cd \\\\\\"$concorewd/' + containername + '\\\\\\"; ' + CPPEXE + ' ' + sourcecode + '; ./a.out\\"" \n') + + elif langext == "v": # 6/25/21 + frun.write('(cd "' + containername + '"; ' + VEXE + ' ' + sourcecode + '; ./a.out >concoreout.txt & echo $! >concorepid) &\n') + if ubuntu: + fdebug.write('concorewd="$(pwd)"\n') + fdebug.write('xterm -e bash -c "cd \\"$concorewd/' + containername + '\\"; ' + VEXE + ' ' + sourcecode + '; ./a.out; bash" &\n') + else: + fdebug.write('concorewd="$(pwd)"\n') + fdebug.write('osascript -e "tell application \\"Terminal\\" to do script \\"cd \\\\\\"$concorewd/' + containername + '\\\\\\"; ' + VEXE + ' ' + sourcecode + '; vvp a.out\\"" \n') + + elif langext == "sh": # 5/19/21 + frun.write('(cd "' + containername + '"; ./' + sourcecode + ' ' + MCRPATH + ' >concoreout.txt & echo $! >concorepid) &\n') + if ubuntu: + fdebug.write('concorewd="$(pwd)"\n') + fdebug.write('xterm -e bash -c "cd \\"$concorewd/' + containername + '\\"; ./' + sourcecode + ' ' + MCRPATH + '; bash" &\n') + else: + fdebug.write('concorewd="$(pwd)"\n') + fdebug.write('osascript -e "tell application \\"Terminal\\" to do script \\"cd \\\\\\"$concorewd/' + containername + '\\\\\\"; ./' + sourcecode + ' ' + MCRPATH + '\\"" \n') + + elif langext == "m": #3/23/21 + if M_IS_OCTAVE: + frun.write('(cd "' + containername + '"; ' + OCTAVEEXE + ' -qf --eval run(\\\'' + sourcecode + '\\\') >concoreout.txt & echo $! >concorepid) &\n') + if ubuntu: + fdebug.write('concorewd="$(pwd)"\n') + fdebug.write('xterm -e bash -c "cd \\"$concorewd/' + containername + '\\"; ' + OCTAVEEXE + ' -qf --eval run(\\\'' + sourcecode + '\\\'); bash" &\n') + else: + fdebug.write('concorewd="$(pwd)"\n') + fdebug.write('osascript -e "tell application \\"Terminal\\" to do script \\"cd \\\\\\"$concorewd/' + containername + '\\\\\\"; ' + OCTAVEEXE + ' -qf --eval run(\\\\\\\'' + sourcecode + '\\\\\\\')\\"" \n') + else: + frun.write('(cd "' + containername + '"; ' + MATLABEXE + ' -batch run(\\\'' + sourcecode + '\\\') >concoreout.txt & echo $! >concorepid) &\n') + if ubuntu: + fdebug.write('concorewd="$(pwd)"\n') + fdebug.write('xterm -e bash -c "cd \\"$concorewd/' + containername + '\\"; ' + MATLABEXE + ' -batch run(\\\'' + sourcecode + '\\\'); bash" &\n') + else: + fdebug.write('concorewd="$(pwd)"\n') + fdebug.write('osascript -e "tell application \\"Terminal\\" to do script \\"cd \\\\\\"$concorewd/' + containername + '\\\\\\"; ' + MATLABEXE + ' -batch run(\\\\\\\'' + sourcecode + '\\\\\\\')\\"" \n') if concoretype=="posix": fstop.write('#!/bin/bash' + "\n") i=0 # 3/30/21 @@ -906,7 +904,7 @@ writeedges = volswr[i] while writeedges.find(":") != -1: if concoretype=="windows": - funlock.write('copy %HOMEDRIVE%%HOMEPATH%\concore.apikey' + writeedges.split(":")[0].split("-v")[1]+ "\\concore.apikey\n") + funlock.write('copy %HOMEDRIVE%%HOMEPATH%\\concore.apikey' + writeedges.split(":")[0].split("-v")[1]+ "\\concore.apikey\n") else: funlock.write('cp ~/concore.apikey ' + writeedges.split(":")[0].split("-v")[1]+ "/concore.apikey\n") writeedges = writeedges[writeedges.find(":")+1:]