Forráskód Böngészése

caide-docker 0.1.0

Marcelo Fornet 5 éve
commit
042b6018e7
7 módosított fájl, 150 hozzáadás és 0 törlés
  1. 10 0
      Dockerfile
  2. 28 0
      Readme.md
  3. BIN
      caide
  4. 79 0
      caide.py
  5. 16 0
      example/library/number_theory.hpp
  6. 13 0
      example/solution.cpp
  7. 4 0
      run.sh

+ 10 - 0
Dockerfile

@@ -0,0 +1,10 @@
+FROM ubuntu
+COPY caide /home/caide
+COPY run.sh /home/run.sh
+WORKDIR /home
+RUN ./caide init
+RUN ./caide problem basic
+RUN mkdir io
+RUN chmod +x run.sh
+RUN echo "" > basic/basic.cpp
+ENTRYPOINT ./run.sh

+ 28 - 0
Readme.md

@@ -0,0 +1,28 @@
+# Caide on docker
+
+This tool is a wrapper of [caide](https://github.com/slycelote/caide) specifically to build solutions that uses multiple includes.
+
+## Install
+
+1. Install [docker](https://docs.docker.com/install/)
+
+2. Build the docker image from the Dockerfile:
+
+    ```bash
+    cd /path/to/caide-docker
+    docker build . -t caide-docker
+    ```
+
+3. Subsequently you will need to invoke the python script `caide.py`. Optionally you can add it to your path.
+
+    ```bash
+    sudo ln -s (pwd)/caide.py /usr/local/bin/caide-docker
+    ```
+
+## Create submission
+
+In the example folder try:
+
+```bash
+caide-docker solution.cpp -l library -o main.cpp
+```


+ 79 - 0
caide.py

@@ -0,0 +1,79 @@
+#!/usr/bin/python3
+import argparse
+import sys
+from os import makedirs
+from os.path import abspath, expanduser, join
+from pathlib import Path
+from shutil import copy2, rmtree
+from subprocess import PIPE, Popen
+from tempfile import TemporaryDirectory
+
+
+def check_exists(docker_image):
+    proc = Popen(['docker', 'images'], stdout=PIPE)
+    proc.wait()
+
+    if proc.returncode != 0:
+        exit(proc.returncode)
+
+    if docker_image not in proc.stdout.read().decode():
+        print(f"Image {docker_image} not found.")
+        exit(1)
+
+
+def main():
+    check_exists('caide-docker')
+    parser = argparse.ArgumentParser(
+        "Caide-Docker", description="Inline c++ code from libraries in a single file.")
+
+    parser.add_argument(
+        'main_file', help="Path to the .cpp that contains the main function.")
+    parser.add_argument('-l', '--libraries', default='',
+                        help="List of libraries to be used other than the std. Use comma (,) to separate several libraries.")
+    parser.add_argument('-o', '--output', default='',
+                        help="Name of file to store final submission. stdout is used by default.")
+
+    args = parser.parse_args()
+
+    main_file = abspath(args.main_file)
+    libraries = [abspath(lib) for lib in args.libraries.split(',') if lib]
+
+    target_folder = Path(abspath(expanduser('~/.caide-docker/tmp')))
+    rmtree(target_folder, ignore_errors=True)
+    makedirs(target_folder, exist_ok=True)
+
+    copy2(main_file, join(target_folder, 'main.cpp'))
+
+    command = ["docker",
+               "run",
+               f"--volume={target_folder}:/home/io"] +\
+        [f"--volume={lib}:/home/cpplib" for lib in libraries] +\
+        ["caide-docker"]
+
+    proc = Popen(command)
+    proc.wait()
+
+    err = target_folder / 'output.err'
+    out = target_folder / 'submission.cpp'
+
+    if err.exists():
+        with open(err) as f:
+            print(f.read(), file=sys.stderr)
+
+    if out.exists():
+        file = sys.stdout
+        should_close = False
+
+        if args.output:
+            file = open(args.output, 'w')
+            should_close = True
+
+        with open(out) as f:
+            print(f.read(), file=file)
+
+        if should_close:
+            file.close()
+
+
+if __name__ == '__main__':
+    main()

+ 16 - 0
example/library/number_theory.hpp

@@ -0,0 +1,16 @@
+#pragma once
+
+namespace nt
+{
+const int mod = 1000000007;
+
+long long fib(int n)
+{
+    return n <= 1 ? n : fib(n - 1) + fib(n - 2);
+}
+
+long long factorial(int n)
+{
+    return n <= 1 ? 1 : n * factorial(n - 1);
+}
+} // namespace nt

+ 13 - 0
example/solution.cpp

@@ -0,0 +1,13 @@
+#include <iostream>
+#include "number_theory.hpp"
+
+int main()
+{
+    int total;
+    std::cin >> total;
+
+    for (int i = 0; i < total; ++i)
+    {
+        std::cout << i << " : " << nt::fib(i) % nt::mod << std::endl;
+    }
+}

+ 4 - 0
run.sh

@@ -0,0 +1,4 @@
+#!/bin/bash
+cp io/main.cpp basic/main.cpp
+./caide make 2> io/output.err
+cp submission.cpp io