Пример #1
0
def test_make_directories(mount_dir, sub_done, sub_open):
    with pytest.raises(FileExistsError):
        mkdir(sub_open, 'dir')
    assert isdir(sub_open, 'dir')

    with pytest.raises(PermissionError):
        rm_rf(sub_open, 'dir')

    assert isdir(sub_open, 'dir')
    assert isfile(sub_open, 'dir', 'single_file_work')
    assert isfile(sub_open, 'dir', 'single_file_work_copy')

    with pytest.raises(IsADirectoryError):
        rm(sub_done, 'dir')
    with pytest.raises(OSError):
        rmdir(sub_done, 'dir')
    assert isdir(sub_done, 'dir')

    rm_rf(sub_done, 'dir')

    assert 'dir' not in ls(sub_done)

    mkdir(sub_done, 'dir')
    assert isdir(sub_done, 'dir')

    rmdir(sub_done, 'dir')
    assert 'dir' not in ls(sub_done)
Пример #2
0
def test_compiling_program(sub_done, mount, fixed):
    url = 'https://attach.libremail.nl/__test_codegra.fs__.tar.gz'
    fname = join(sub_done, '42.tar.gz')
    fdir = join(sub_done, '42sh/')
    urllib.request.urlretrieve(url, fname)
    tar = tarfile.open(fname, "r:gz")
    tar.extractall(sub_done)
    tar.close()
    print(subprocess.check_output(['make', '-C', fdir]))
    assert isdir(fdir)
    assert isfile(fname)
    assert subprocess.check_output(
        [join(fdir, '42sh'), '-c', 'echo hello from 42']
    ).decode('utf-8') == 'hello from 42\n'

    mount()

    if fixed:
        assert not isdir(fdir)
        assert not isfile(fname)
        return

    assert isfile(fname)
    assert isdir(fdir)
    assert subprocess.check_output(
        [join(fdir, '42sh'), '-c', 'echo hello from 42']
    ).decode('utf-8') == 'hello from 42\n'

    rm_rf(fdir)
    assert not isdir(fdir)

    mount()

    assert isfile(fname)
    assert not isdir(fdir)
Пример #3
0
def test_list_assignments(mount_dir):
    course_dir = join(mount_dir, 'Programmeertalen')

    assert isdir(mount_dir)
    assert isdir(course_dir)

    for assig in ['Haskell', 'Go', 'Python', 'Shell']:
        assert isdir(course_dir, assig)
Пример #4
0
def test_delete_invalid_file(mount_dir):
    with pytest.raises(PermissionError):
        top = [l for l in ls(mount_dir) if isdir(mount_dir, l)][0]
        rm_rf(mount_dir, top)

    with pytest.raises(PermissionError):
        top = [l for l in ls(mount_dir) if isdir(mount_dir, l)][0]
        f = [l for l in ls(mount_dir, top) if isdir(mount_dir, top, l)][0]
        rm_rf(mount_dir, top, f)
Пример #5
0
def test_create_symlink(sub_done):
    with pytest.raises(PermissionError):
        symlink([sub_done, 'dir'], [sub_done, 'wowsers'])

    assert isdir(sub_done, 'dir')
    assert not isdir(sub_done, 'wowsers')

    mkdir(sub_done, 'wowsers')
    assert isdir(sub_done, 'wowsers')
Пример #6
0
def test_deleting_directory_in_fixed(sub_open, mount):
    fdir = join(sub_open, 'new_test_file')
    fdir2 = join(sub_open, 'new_test_file2')
    fdir3 = join(sub_open, 'new_test_file3')

    # Make sure we cannot delete existing files in fixed mode
    assert not isdir(fdir)
    mkdir(fdir)
    assert isdir(fdir)

    mount(fixed=True)

    assert isdir(fdir)
    with pytest.raises(PermissionError):
        rmdir(fdir)
    assert isdir(fdir)

    del fdir

    assert not isdir(fdir2)
    mkdir(fdir2)
    assert isdir(fdir2)
    rename([fdir2], [fdir3])

    assert not isdir(fdir2)
    assert isdir(fdir3)

    mount(fixed=True)
    assert not isdir(fdir3)
Пример #7
0
def test_list_assignments(mount_dir):
    assert isdir(mount_dir)

    for assig in ['Security assignment', 'Shell']:
        assert isdir(mount_dir, 'Besturingssystemen', assig)

    for assig in ['Final deadline']:
        assert isdir(mount_dir, 'Project Software Engineering', assig)

    for assig in ['Haskell', 'Go', 'Python', 'Shell']:
        assert isdir(mount_dir, 'Programmeertalen', assig)
Пример #8
0
def test_illegal_rename(mount_dir, sub_done, sub_open):
    assert isdir(sub_done, 'dir')
    assert not isdir(sub_done, 'dir33')
    mkdir(sub_done, 'dir33')
    assert isdir(sub_done, 'dir33')

    with pytest.raises(FileNotFoundError):
        rename([sub_done, 'dir33'], [sub_done, 'dir', 'non_existing', 'dir33'])

    with pytest.raises(FileExistsError):
        rename([sub_done, 'dir33'], [sub_done, 'dir'])
Пример #9
0
def test_list_courses(mount_dir):
    assert isdir(mount_dir)
    for course in [
            'Besturingssystemen', 'Programmeertalen',
            'Project Software Engineering'
    ]:
        assert isdir(mount_dir, course)

    assert set(ls(mount_dir)) == set([
        'Besturingssystemen', 'Programmeertalen',
        'Project Software Engineering', '.api.socket', '.cg-mode'
    ])
Пример #10
0
def test_list_submissions(mount_dir, mount):
    course_dir = join(mount_dir, 'Programmeertalen')
    assert isdir(mount_dir)
    assert isdir(course_dir)

    for assig in ls(course_dir):
        for sub in ls(course_dir, assig):
            assert 'Student1' in sub or sub[0] == '.'

    mount(assigned_to_me=True)

    for assig in ls(course_dir):
        for sub in ls(course_dir, assig):
            assert 'Student1' in sub or sub[0] == '.'
Пример #11
0
def test_rename(mount_dir, sub_done, sub_open):
    assert isdir(sub_done, 'dir')
    assert not isdir(sub_done, 'dir33')

    assert isdir(sub_done, 'dir')
    assert not isdir(sub_done, 'dir33')
    mkdir(sub_done, 'dir33')
    mkdir(sub_done, 'dir', 'sub_dir')
    assert isdir(sub_done, 'dir33')
    with open(join(sub_done, 'dir33', 'new_file'), 'w') as f:
        f.write('bye\n')

    rename([sub_done, 'dir33'], [sub_done, 'dir', 'sub_dir', 'dir33'])
    with open(join(sub_done, 'dir', 'sub_dir', 'dir33', 'new_file'), 'r') as f:
        assert f.read() == 'bye\n'
Пример #12
0
 def __init__(self, path, state, lang, output, resume=False):
     try:
         self.reader = getattr(getattr(getattr(rolls, state), lang),
                               '%sPDF' % state.capitalize())
     except AttributeError:
         raise ValueError('Unsupported state with name: %s' % state)
     self.pdfs = scanpdfs(path) if isdir(path) else [path]
     self.tracker = Tracker(path,
                            output) if isdir(path) and resume else None
     self.writer = CsvWriter(
         path=self.tracker.output if self.tracker else output,
         header=OutputRow._fields)
     self.logger = LogFacility(self.__class__.__name__)
     self.__parse()
     if self.tracker is not None:
         self.tracker.cleanup()
Пример #13
0
def test_list_courses(mount_dir):
    assert isdir(mount_dir)
    for course in [
            'Besturingssystemen', 'Programmeertalen',
            'Project Software Engineering'
    ]:
        assert isdir(mount_dir, course)

    ls_res = [
        x for x in ls(mount_dir)
        if not x.startswith('ASCII_') and not x.startswith('New_Assig-')
    ]
    assert set(ls_res) == set([
        'Besturingssystemen', 'Programmeertalen',
        'Project Software Engineering', '.api.socket', '.cg-mode'
    ])
Пример #14
0
 def __init__(self, path, output):
     if not isdir(path):
         raise NotADirectoryError('Not a directory or not found: \'%s\'' %
                                  path)
     self.data = None
     self.dir = path
     self.file = pathjoin(path, TRACK_FILE_NAME)
     self._newout = output
     self._oldout = None
     self.__load()
Пример #15
0
def test_create_invalid_file(mount_dir):
    with pytest.raises(PermissionError):
        with open(join(mount_dir, 'file'), 'w+') as f:
            f.write('hello\n')

    with pytest.raises(PermissionError):
        with open(
            join(
                mount_dir, [l for l in ls(mount_dir)
                            if isdir(mount_dir, l)][0], 'file'
            ), 'w+'
        ) as f:
            f.write('hello\n')
Пример #16
0
def test_write_to_directory(sub_done):
    fdir = join(sub_done, 'new_dir')
    fname = join(fdir, 'new_file')

    mkdir(fdir)
    assert isdir(fdir)
    assert not isfile(fdir)

    with pytest.raises(IsADirectoryError):
        with open(fdir, 'w') as f:
            f.write('hello\n')

    open(fname, 'w').close()
    with pytest.raises(NotADirectoryError):
        ls(fname)
    with pytest.raises(FileExistsError):
        mkdir(fname)
Пример #17
0
def test_create_file_outside_submissions(mount_dir, mount):
    with open(join(mount_dir, 'file'), 'w+') as f:
        f.write('hello\n')
    with open(join(mount_dir, 'file'), 'r') as f:
        assert f.read() == 'hello\n'

    with open(
        join(
            mount_dir, [l for l in ls(mount_dir) if isdir(mount_dir, l)][0],
            'file'
        ), 'w+'
    ) as f:
        f.write('hello\n')

    mkdir(mount_dir, 'dir')
    mkdir(mount_dir, 'dir', 'hello')
    mkdir(mount_dir, 'dir', 'hello', 'codegrade')
    with open(
        join(mount_dir, 'dir', 'hello', 'codegrade', 'nice_to_meet'), 'w'
    ) as f:
        f.write('hello')
    with pytest.raises(FileExistsError):
        mkdir(mount_dir, 'dir', 'hello', 'codegrade', 'nice_to_meet')
    with open(
        join(mount_dir, 'dir', 'hello', 'codegrade', 'nice_to_meet'), 'r'
    ) as f:
        assert f.read() == 'hello'

    with pytest.raises(FileNotFoundError):
        with open(join(mount_dir, 'dir', 'bye', 'cg'), 'w') as f:
            pass
    with pytest.raises(FileNotFoundError):
        mkdir(mount_dir, 'dir', 'bye', 'cg2')

    assert 'dir' in ls(mount_dir)
    assert 'file' in ls(mount_dir)
    mount()
    assert 'dir' not in ls(mount_dir)
    assert 'file' not in ls(mount_dir)
Пример #18
0
def get_args():
    parser = ArgumentParser(description=APP_DESC, prog=APP_CMD)

    parser.add_argument('-f',
                        '--file',
                        metavar='FILE',
                        help='path to the PDF file that is to be parsed')

    parser.add_argument(
        '-d',
        '--dir',
        metavar='DIR',
        help=
        'path to the directory containing the PDF files that are to be parsed')

    parser.add_argument('-s',
                        '--state',
                        metavar='STATE',
                        help='state whose PDF rolls(s) are to be parsed')

    parser.add_argument('-l',
                        '--lang',
                        metavar='LANG',
                        default='english',
                        help='specify language used in parsing \
documents (default is English if not being specified)')

    parser.add_argument('-o',
                        '--out',
                        metavar='FILE',
                        default=OUTPUT_FILE.format(timestamp(OUTPUT_FILE_TS)),
                        help='\
specify output file for storing parsed result (must be \'.csv\' file). The default output file is \
\'Parsed-{timestamp}.csv\' and stored in the \'output\' directory')

    parser.add_argument('--resume',
                        action='store_true',
                        default=False,
                        help='allows the parsing to be \
resumed later if the program is stopped unexpectedly or intentionally. Only takes effect if it is applied to a directory'
                        )

    parser.add_argument('--version',
                        action='version',
                        version=APP_NAME_WITH_VERSION)

    parser.add_argument('--all-states',
                        action='store_true',
                        default=False,
                        help='show all the states that are supported and exit')

    args = parser.parse_args()

    if args.all_states:
        out = []
        chars = max(len(x) for x in states.keys())
        for state, langs in states.items():
            out.append('%-{}s : %s'.format(chars) % (state, ', '.join(langs)))
        parser.exit(message='States supported:\n%s\n' % '\n'.join(out))

    if not args.file and not args.dir or not args.state:
        parser.error(
            'the following arguments are required: -f/--file or -d/--dir, -s/--state'
        )

    if args.file and args.dir:
        parser.error('only accepts one of --file or --dir at a time')

    elif args.file:
        if not isfile(args.file):
            parser.error('file not found: %s' % args.file)
        if not ispdf(args.file):
            parser.error('file must be in PDF format')
        if args.resume:
            parser.error('parsing file does not allow --resume')

    elif args.dir:
        if not isdir(args.dir):
            parser.error('folder not found: %s' % args.dir)

    if args.state.lower() not in states:
        parser.error('unsupported state \'%s\'' % args.state)

    if args.lang and args.lang.lower() not in states[args.state.lower()]:
        parser.error('state \'%s\' does not have \'%s\' language' %
                     (args.state, args.lang))

    if dirname(args.out) and not isdir(dirname(args.out)):
        parser.error('output folder does not exist: %s' % dirname(args.out))

    if filext(args.out).lower() != 'csv':
        parser.error('output file must be \'.csv\'')

    args.path = args.file or args.dir
    args.state = args.state.lower()
    args.lang = args.lang.lower()

    return args
Пример #19
0
def test_list_courses(mount_dir):
    assert isdir(mount_dir)
    for course in ['Inleiding Programmeren', 'Programmeertalen']:
        assert isdir(mount_dir, course)
Пример #20
0
def main():
    """Main program"""

    # Check Python
    if not minpy(MINIMUM_PYTHON_REQUIRED):
        print('Failed! Minimum Python required is %s' %
              MINIMUM_PYTHON_REQUIRED)
        sys.exit(1)

    # Read arguments
    try:
        args = readargs(**COMMAND_LINE_SETTINGS)
    except SyntaxError as msg:
        print(msg)
        sys.exit(2)
    except SystemExit:
        sys.exit()

    # Load settings
    wdir = args.get('dir', DEFAULT_WORKING_DIR)
    xml = joinroot(wdir, args.get('xmlfile', DEFAULT_XML_FILE))
    out = joinroot(wdir, args.get('outfile', DEFAULT_OUTPUT_FILE))
    lang = args.get('lang', None)
    anyext = True if 'anyext' in args else False

    # Check XML file existence
    if not isfile(xml):
        print('Failed! XML file is not existed:', xml)
        sys.exit(1)

    # Check output file existence
    if isfile(out):
        print('Output file is existed:', out)
        confirm = input(
            'Do you want to overwrite? (type Y to confirm, any key to exit): ')
        if confirm != 'Y' and confirm != 'y':
            sys.exit()

    # Make sure output file is not a dir
    if isdir(out):
        print('Failed! Ouput file path is a directory:', out)
        sys.exit(1)

    # Check output file parent dir
    if not dirfound(out, path_is_file=True):
        print('Failed! Output file\'s parent dir is not found:', out)
        sys.exit(1)

    # Do generating
    try:
        MDGen(infile=xml,
              outfile=out,
              workdir=wdir,
              language=lang,
              any_ext=anyext)
    except Exception as msg:
        print('Failed! Error found: %s' % msg)
        sys.exit(2)

    # Done
    print('MD file successfully generated:', out)