-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathtest-papers
executable file
·126 lines (93 loc) · 3.53 KB
/
test-papers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
# -*- elpy-mode: nil; auto-complete-mode: nil; company-mode: nil -*-
from __future__ import print_function
import yaml
import requests
from subprocess import check_call, CalledProcessError
from pipes import quote
from fnmatch import fnmatch
import glob
import sys
import os.path
import datetime
hid_dirs = sys.argv[1:]
class Verifier(object):
def __init__(self, path):
self._path = path
@property
def path(self):
return self._path
@property
def readme(self):
return os.path.join(self.path, 'README.rst')
################################################################
def _call(self, cmd, **kwargs):
"""Wrapper around subprocess.check_call
:param cmd: a list of strings that will be passed to check_call
"""
pretty = ' '.join(map(quote, cmd))
print('Running:', pretty)
check_call(cmd, **kwargs)
def _parse_readme(self, path):
lines = []
with open(path) as fd:
for line in fd:
if ':' in line:
lines.append(line.strip())
return yaml.load('\n'.join(lines))
def make(self):
"""Checks that the 'make' command works for a submission"""
print('Building', path)
self._call(['make'], cwd=path)
def readme_exists(self):
"""Checks that the README.rst file exists for a submission"""
print('Checking for', self.readme)
if not os.path.exists(self.readme):
raise ValueError('NOT FOUND: %s' % self.readme)
def readme_values(self, params=None):
params = params or dict(
submitted = 'ENTER DATE SUBMITTED',
author = 'ENTER*NAME*',
hid = 'ENTER*HID*',
github = '*GITHUB*',
)
submission = self._parse_readme(self.readme)
# in case the submission date was parsed as a date/datetime
# convert it back to a string for fnmatch to work below
if 'submitted' in submission and (
type(submission['submitted']) == datetime.datetime or
type(submission['submitted']) == datetime.date
):
submission['submitted'] = datetime.datetime.strftime(submission['submitted'], '%Y-%m-%d')
for property, pattern in params.iteritems():
if not property in submission:
raise KeyError('README DOES NOT HAVE: %s' % property)
submitted = submission[property]
if fnmatch(submitted, pattern):
raise ValueError('PATTERN %s NOT SET IN README. CURRENT: %s' % (property, submitted))
if 'github' in params:
username = submission['github']
user_url = 'http://github.com/{}'.format(username)
req = requests.get(user_url)
if not req.status_code == 200:
raise ValueError('ERROR: Github user {} does not exist at {}'.format(username, user_url))
if 'report' in params:
report = os.path.join(self.path, submission['report'])
if not os.path.exists(report):
raise ValueError('REPORT NOT FOUND: {}'.format(report))
for path in hid_dirs:
if not os.path.exists(path):
print('Skipping %s. Path does not exist.' % path)
continue
elif not os.path.isdir(path):
print('Skipping %s. Path is not a directory.' % path)
continue
verify = Verifier(path)
try:
# verify.make()
verify.readme_exists()
verify.readme_values()
print('OK')
except CalledProcessError, e:
print('FAIL')
raise