def make_pdf(self, tex_file, force=False): """ Assumes the tex file has already been made. If not use `self.make`. """ pdf_file = splitext(tex_file)[0] + '.pdf' if not force and not ask_before_overwrite(pdf_file): return cwd = dirname(tex_file) if cwd == '': cwd = '.' for i in range(self.nb_compile_times): subprocess.call([ 'pdflatex', '--file-line-error', '--shell-escape', '--synctex=1', os.path.basename(tex_file)], cwd=cwd) # cleaning for ext in 'sol qsl synctex.gz log aux'.split(): path = '%s.%s' % (tex_file[:-4], ext) if exists(path): os.remove(path) for ext in r'func\d?.gnuplot func\d?.table'.split(): regex = re.compile('^%s.%s$' % (tex_file[:-4], ext)) for f in os.listdir(cwd): if regex.match(f): os.remove(f)
def batch_assignment(make_assignment, students, output, make_args=None): """ make_assignment has to be a function that takes a student name (lastname, firstname) tuple (or None) and that returns two assignment instances, one with the questions, and one with de solutions students is the list of (lastname, firstname) tuples output is the output dir or .zip file or .gz file We will create an intermediary folder with name `output`. """ output, ext = splitext(output) sol_output = join(dirname(output), 'sol_' + basename(output)) if make_args is None: make_args = {} for folder in [output, sol_output]: if exists(folder) and ask_before_overwrite(folder): shutil.rmtree(folder) os.mkdir(folder) for s in students: name = '' filename_suffix = '' if s is not None: name = ', '.join(s) filename_suffix += '_' + '_'.join(s) ass, sol = make_assignment(name) for folder, homework in zip([output, sol_output], [ass, sol]): filename = basename(folder) + filename_suffix homework.make(join(folder, filename), **make_args) if ext != '': for o in [output, sol_output]: print 'zipping', o shutil.make_archive(o, ext.replace('.', ''), o)
def make_tex(self, tex_file, force=False): if not force and not ask_before_overwrite(tex_file): sys.exit() with open(tex_file, 'w') as f: f.write(self.header) f.write(str(self))