def check_input_dir_bootcamp(directory: str): """ Check the bootcamp directory file organization Args: directory (str): bootcamp day directory """ # check directory is in the format dayXX while directory[-1] == '/': directory = directory[:-1] dir = directory.split('/')[-1] day_regex = re.compile(r'^day[0-9]{2}$') module_regex = re.compile(r'^module[0-9]{2}$') if not day_regex.search(dir) and not module_regex.search(dir): error("'{}' invalid day/module directory (dayXX/moduleXX allowed)". format(directory)) # check if it is a directory if not os.path.isdir(directory): error("'{}' is not a directory!".format(directory)) # check directory has a dayXX.md ls_day = sub_run("ls {}/day*.md".format(directory)) ls_module = sub_run("ls {}/module*.md".format(directory)) if ls_day.stderr and ls_module.stderr: error("markdown for day/module missing") # check directory has exXX.md files ls_ex = sub_run("ls {}/ex*/ex*.md".format(directory)) if ls_ex.stderr: error("markdown for exercises missing")
def file_add_clearpage(file_name): """ Add clearpages at the end of exercises. Args: file_name (undefined): file name """ if platform.system() == "Darwin": sub_run("echo '\n\n\\\\clearpage' >> tmp/{}".format( file_name.split('/')[-1])) else: sub_run("echo '\n\n\\\\clearpage' >> tmp/{}".format( file_name.split('/')[-1]))
def bootcamp(output_path, input_directory, bootcamp_title, day_title, logo_file, template_file, debug): try: check_bootcamp_title(bootcamp_title) check_day_title(day_title) check_input_dir_bootcamp(input_directory) bootcamp_files_cpy(input_directory, template_file, logo_file) except Exception as e: print(e) sys.exit(-1) # FILES FORMATTING ##################### try: insert_project_title(bootcamp_title) insert_day_title(day_title) files = sub_run("ls tmp/*.md").stdout.strip() for f in files.decode().split('\n'): content = None file_add_clearpage(f) with open(f, 'r') as infile: content = infile.read() content = change_img_format(f, content) content = change_header_format(f, content) content = change_list_format(f, content) content = change_empty_code_block_style(f, content) content = change_equations_format(f, content) content = set_url_color(f, content) with open(f + ".tmp", 'w') as outfile: outfile.write(content) sub_run("mv {0}.tmp {0}".format(f)) if debug: run_pandoc(f) # GENERATING PDF OUTPUT run_pandoc_all(output_path.split('/')[-1], debug) print("Successfully built pdf!") except Exception as e: print(e) sys.exit(-1)
def check_input_dir_simple(directory: str): """ Check the bootcamp directory file organization Args: directory (str): bootcamp day directory """ # check directory is in the format dayXX while directory[-1] == '/': directory = directory[:-1] # check if it is a directory if not os.path.isdir(directory): error("'{}' is not a directory!".format(directory)) # check directory has a projectXX.md ls_project = sub_run("ls {}/project*.md".format(directory)) if ls_project.stderr: error("markdown for project missing") # check directory has exXX.md files ls_ex = sub_run("ls {}/ex*/ex*.md".format(directory)) if ls_ex.stderr: error("markdown for exercises missing")
def project_files_cpy(input_dir, template_file, logo_file): """ Copy project files and create a temporary directory. Args: input_dir (undefined): input day directory template_file (undefined): template file """ # retrieve file lists project_f = sub_run("ls {0}/project*.md".format(input_dir)).stdout.strip() ex_list = sub_run("ls {}/**/ex*.md".format(input_dir)).stdout imgs_dir = sub_run("ls -d {}/assets".format(input_dir)) # create a tmp dir with a copy of all files sub_run("rm -rf tmp") sub_run("mkdir -p tmp") if not imgs_dir.stderr: sub_run("cp -rp {} tmp/".format(imgs_dir.stdout.decode().strip())) sub_run("cp {} tmp/day.md".format(project_f.decode())) cpy_template_file_to(logo_file, 'tmp/logo-42-ai.png') cpy_template_file_to(template_file, 'tmp/template.latex') for f in ex_list.split(): pattern = re.compile(r'(ex[0-9]{2}\.md)$') for e in pattern.findall(f.decode()): sub_run("cp {} tmp/{}".format(f.decode(), e[:-3] + "_master.md")) pattern = re.compile(r'(ex[0-9]{2}_interlude.*)$') for e in pattern.findall(f.decode()): sub_run("cp {} tmp/".format(f.decode()))