<commit_before># -*- coding: utf-8 -*-
# vim:ts=4:sts=4:sw=4:expandtab
"""Satori judge implementation.
"""

from satori.objects import Object, Argument
from satori.client.common import want_import
want_import(globals(), '*')
from types import NoneType
import BaseHTTPServer
import cgi
import datetime
from multiprocessing import Process, Pipe, Manager
import os
import sys
import shutil
import signal
import subprocess
import time
import unshare
import yaml
import traceback


def loopUnmount(root):
    paths = []
    with open('/proc/mounts', 'r') as mounts:
        for mount in mounts:
            path = mount.split(' ')[1]
            if checkSubPath(os.path.join('/', root), path):
                paths.append(path)
        paths.reverse()
        for path in paths:
            subprocess.call(['umount', '-l', path])

def checkSubPath(root, path):
    root = os.path.realpath(root).split('/')
    path = os.path.realpath(path).split('/')

    if len(path) < len(root):
        return False
    for i in range(0, len(root)):
        if root[i] != path[i]:
            return False
    return True

def jailPath(root, path):
    return os.path.join(root, os.path.abspath(os.path.join('/',path))[1:])

class JailExec(Object):
    @Argument('root', type=str)
    @Argument('path', type=str)
    @Argument('args', default=[])
    @Argument('search', type=bool, default=False)
    def __init__(self, root, path, args, search):
        self.root = root
        self.path = path
        self.args = [self.path] + args
        self.search = search

    def run(self):
        os.chroot(self.root)
        os.chdir('/')
        if self.search:
            os.execvp(self.path, self.args)
        else:
            os.execv(self.path, self.args)

class JailBuilder(Object):
    scriptTimeout = 15

    @Argument('root', type=str)
    @Argument('template_path', type=str)
    @Argument('template', type=str)
    def __init__(self, root, template, template_path):
        template = os.path.split(template)[1]
        self.root = root
        self.template = os.path.join(template_path, template + '.template')

    def create(self):
        try:
            os.mkdir(self.root)
            template = yaml.load(open(self.template, 'r'))
            dirlist = []
            quota = 0
            num = 1
            if 'base' in template:
                for base in template['base']:
                    if isinstance(base, str):
                        base = { 'path' : base }
                    opts = base.get('opts', '')
                    name = base.get('name', '__' + str(num) + '__')
                    num += 1
                    path = os.path.join(self.root, os.path.basename(name))
                    if 'path' in base:
                        type = base.get('type')
                        src = os.path.realpath(base['path'])
                        if os.path.isdir(src):
                            os.mkdir(path)
                            subprocess.check_call(['mount', '-o', 'bind,ro,'+opts, src, path])
                            dirlist.append(path + '=nfsro')
                        elif os.path.isfile(src):
                            os.mkdir(path)
                            if type is None:
                                ext = os.path.splitext(src)[1]
                                if ext:
                                    type = ext[1:]
                                else:
                                    type = 'auto'
                            subprocess.check_call(['mount', '-t', type, '-o', 'loop,noatime,ro,'+opts, src, path])
                            dirlist.append(path + '=nfsro')
                        else:
                          raise Exception('Path '+base['path']+' can\'t be mounted')
            if 'quota' in template:
                quota = int(template['quota'])
            if quota > 0:
                path = os.path.join(self.root, '__rw__')
                os.mkdir(path)
                subprocess.check_call(['mount', '-t', 'tmpfs', '-o', 'noatime,rw,size=' + str(quota) + 'm', 'tmpfs', path])
                dirlist.append(path + '=rw')
            dirlist.reverse()
            subprocess.check_call(['mount', '-t', 'aufs', '-o', 'noatime,rw,dirs=' + ':'.join(dirlist), 'aufs', self.root])
            if 'insert' in template:
                for src in template['insert']:
                    src = os.path.realpath(src)
                    if os.path.isdir(src):
                        subprocess.check_call(['rsync', '-a', src, self.root])
                    elif os.path.isfile(src):
                        name = os.path.basename(src).split('.')
                        if name[-1] == 'zip':
                            subprocess.check_call(['unzip', '-d', self.root, src])
                        elif name[-1] == 'tar':<commit_msg>Change aufs to overlayfs in satori.judge<commit_after># -*- coding: utf-8 -*-
# vim:ts=4:sts=4:sw=4:expandtab
"""Satori judge implementation.
"""

from satori.objects import Object, Argument
from satori.client.common import want_import
want_import(globals(), '*')
from types import NoneType
import BaseHTTPServer
import cgi
import datetime
from multiprocessing import Process, Pipe, Manager
import os
import sys
import shutil
import signal
import subprocess
import time
import unshare
import yaml
import traceback


def loopUnmount(root):
    paths = []
    with open('/proc/mounts', 'r') as mounts:
        for mount in mounts:
            path = mount.split(' ')[1]
            if checkSubPath(os.path.join('/', root), path):
                paths.append(path)
        paths.reverse()
        for path in paths:
            subprocess.call(['umount', '-l', path])

def checkSubPath(root, path):
    root = os.path.realpath(root).split('/')
    path = os.path.realpath(path).split('/')

    if len(path) < len(root):
        return False
    for i in range(0, len(root)):
        if root[i] != path[i]:
            return False
    return True

def jailPath(root, path):
    return os.path.join(root, os.path.abspath(os.path.join('/',path))[1:])

class JailExec(Object):
    @Argument('root', type=str)
    @Argument('path', type=str)
    @Argument('args', default=[])
    @Argument('search', type=bool, default=False)
    def __init__(self, root, path, args, search):
        self.root = root
        self.path = path
        self.args = [self.path] + args
        self.search = search

    def run(self):
        os.chroot(self.root)
        os.chdir('/')
        if self.search:
            os.execvp(self.path, self.args)
        else:
            os.execv(self.path, self.args)

class JailBuilder(Object):
    scriptTimeout = 15

    @Argument('root', type=str)
    @Argument('template_path', type=str)
    @Argument('template', type=str)
    def __init__(self, root, template, template_path):
        template = os.path.split(template)[1]
        self.root = root
        self.template = os.path.join(template_path, template + '.template')

    def create(self):
        try:
            os.mkdir(self.root)
            template = yaml.load(open(self.template, 'r'))
            dirlist = []
            quota = 0
            num = 1
            if 'base' in template:
                for base in template['base']:
                    if isinstance(base, str):
                        base = { 'path' : base }
                    opts = base.get('opts', '')
                    name = base.get('name', '__' + str(num) + '__')
                    num += 1
                    path = os.path.join(self.root, os.path.basename(name))
                    if 'path' in base:
                        type = base.get('type')
                        src = os.path.realpath(base['path'])
                        if os.path.isdir(src):
                            os.mkdir(path)
                            subprocess.check_call(['mount', '-o', 'bind,ro,'+opts, src, path])
                            dirlist.append(path)
                        elif os.path.isfile(src):
                            os.mkdir(path)
                            if type is None:
                                ext = os.path.splitext(src)[1]
                                if ext:
                                    type = ext[1:]
                                else:
                                    type = 'auto'
                            subprocess.check_call(['mount', '-t', type, '-o', 'loop,noatime,ro,'+opts, src, path])
                            dirlist.append(path)
                        else:
                          raise Exception('Path '+base['path']+' can\'t be mounted')
            if 'quota' in template:
                quota = int(template['quota'])
            if quota > 0:
                path = os.path.join(self.root, '__rw__')
                os.mkdir(path)
                subprocess.check_call(['mount', '-t', 'tmpfs', '-o', 'noatime,rw,size=' + str(quota) + 'm', 'tmpfs', path])
                dirlist.append(path)
            if len(dirlist) == 1:
                subprocess.check_call(['mount', '-o', 'bind', dirlist[0], self.root])
            else
                subprocess.check_call(['mount', '-t', 'overlayfs', '-o', 'lowerdir='+dirlist[0]+',upperdir='+dirlist[1], dirlist[1], self.root])
                for d in dirlist[2:]:
                    subprocess.check_call(['mount', '-t', 'overlayfs', '-o', 'lowerdir='+self.root+',upperdir='+d, d, self.root])
            if 'insert' in template:
                for src in template['insert']:
                    src = os.path.realpath(src)
                    if os.path.isdir(src):
                        subprocess.check_call(['rsync', '-a', src, self.root])
                    elif os.path.isfile(src):
                        name = os.path.basename(src).split('.')
                        if name[-1] == 'zip':
                            subprocess.check_call(['unzip', '-d', self.root, src])
                        elif name[-1] == 'tar':
{'fraction_non_alphanumeric': 0.14208253840747062, 'fraction_numerical': 0.0042172908926599055, 'mean_word_length': 1.5703225806451613, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2212744', 'n_tokens_mistral': 2933, 'n_tokens_neox': 2839, 'n_words': 655}