def check_input_dir(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] regex = re.compile(r'^day[0-9]{2}$') if not regex.search(dir): error("'{}' invalid day directory (dayXX 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)) if ls_day.stderr: error("markdown for day missing") # check directory has exXX.md files ls_ex = sub_run("ls {}/ex*/ex*.md".format(directory)) if ls_ex.stderr: error("markdown for exercices missing")
def main(): args = get_arguments() # CHECK PARAMETERS AND FILES ############################### try: params_check.check_bootcamp_title(args.bootcamp_title) params_check.check_day_title(args.day_title) if args.input_dir: # check and copy the day directory params_check.check_input_dir(args.input_dir) files_formatting.day_files_cpy(args.input_dir, args.template_file) else: # check and copy the documentation file params_check.check_input_file(args.input_file) files_formatting.input_file_cpy(args.input_file, args.template_file) except Exception as e: print(e) sys.exit(-1) # FILES FORMATTING ##################### try: files_formatting.insert_bootcamp_title(args) files_formatting.insert_day_title(args) files = sub_run("ls tmp/*.md").stdout.strip() for f in files.decode().split('\n'): content = None files_formatting.files_format(f) with open(f, 'r') as infile: content = infile.read() content = files_formatting.change_img_format(f, content) content = files_formatting.change_header_format(f, content) content = files_formatting.change_list_format(f, content) content = files_formatting.change_empty_code_block_style( f, content) content = files_formatting.change_equations_format(f, content) content = files_formatting.set_url_color(f, content) with open(f + ".tmp", 'w') as outfile: outfile.write(content) sub_run("mv {0}.tmp {0}".format(f)) if args.debug: files_formatting.run_pandoc(f) # GENERATING PDF OUTPUT files_formatting.run_pandoc_all( args.output_file.split('/')[-1], args.debug) print("Successfully built pdf !") except Exception as e: print(e) sys.exit(-1)
def run_pandoc_all(outfile, debug): """ Build a pdf with all markdown files. Args: outfile (undefined): output file name debug (undefined): debug option """ res = sub_run("pandoc tmp/*.md --to=pdf --pdf-engine=pdflatex --highlight-style=breezedark\ -t latex -o {} --template=tmp/template.latex".format(outfile)) if res.stderr: error(res.stderr.decode().strip()) if not debug: sub_run("rm -rf tmp")
def main(): repos = { "bootcamp_python": "master", "bootcamp_machine-learning": "master", "bootcamp_data-engineering": "master" } for _dir, _branch in repos.items(): clone_branch(_dir, _branch) for _dir in repos: days = {} res = sub_run("ls -d {}/day*/".format(_dir)).stdout.decode().strip() for d in res.split('\n'): days[d.split('/')[1]] = d for k, v in days.items(): dd = "{}.{}.pdf".format(_dir, k) res = sub_run("python3 pdf_builder -b \"{}\" -d {} -t \ \"Day00 - ddd\" -o {}".format(_dir.replace('_', ' ') .replace('-', ' '), v, dd)) pattern = re.compile(r'(Successfully.*?!)') if pattern.findall(res.stderr.decode().strip()): print("{}SUCCESS{} !".format(OKGREEN, ENDC)) else: print("{}ERROR{} !".format(FAIL, ENDC)) print(res.stdout.decode().strip()) res = sub_run("python3 pdf_builder -b \"Data Engineering\" \ -f bootcamp_data-engineering/day00/ex03/psycopg2_basics.md \ -t \"Day00 - ddd\" -o \ bootcamp_data-engineering.psycopg2_doc.pdf") pattern = re.compile(r'(Successfully.*?!)') if pattern.findall(res.stderr.decode().strip()): print("{}SUCCESS{} !".format(OKGREEN, ENDC)) else: print("{}ERROR{} !".format(FAIL, ENDC)) print(res.stderr.decode()) pattern = re.compile(r'Detected !.*?$', re.DOTALL) print("\n".join(pattern.findall(res.stderr.decode() .strip())[0].split('\n')[1:]))
def run_pandoc(file_name): """ Build pdf file for each markdown. Args: file_name (undefined): """ res = sub_run("pandoc {} --to=pdf --pdf-engine=pdflatex --highlight-style=breezedark\ -t latex -o {} --template=tmp/template.latex" .format(file_name, file_name + ".pdf")) if res.stderr: print(file_name) error(res.stderr.decode().strip(), file_name)
def files_format(file_name): """ Add clearpages at the end of exercises. Args: file_name (undefined): file name """ def in_docker(): with open('/proc/self/cgroup', 'r') as procfile: for line in procfile: fields = line.strip().split('/') if 'docker' in fields: return True return False if in_docker(): sub_run( "echo '\n\\clearpage' >> tmp/{}" .format(file_name.split('/')[-1])) else: sub_run( "echo '\n\\\\clearpage' >> tmp/{}" .format(file_name.split('/')[-1]))
def clone_branch(repo, branch): sub_run("git clone -b {} https://github.com/42-AI/{}.git".format( branch, repo))
def input_file_cpy(input_file, template_file): """ Copy input file and create tmp directory. Args: input_file (undefined): input file template_file (undefined): template file """ # create a tmp dir with a copy of all files sub_run("rm -rf tmp") sub_run("mkdir -p tmp") # copy input_file into tmp sub_run("cp {} tmp/".format(input_file)) sub_run("cp {} tmp/".format(template_file)) imgs_dir = sub_run( "ls -d {}/assets".format("/".join(input_file.split('/')[:-1]))) if not imgs_dir.stderr: sub_run("cp -rp {} tmp/".format(imgs_dir.stdout.decode().strip()))
def day_files_cpy(input_dir, template_file): """ Copy files of a day and create a temporary directory. Args: input_dir (undefined): input day directory template_file (undefined): template file """ # retrieve file lists day_f = sub_run("ls {}/day*.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/".format(day_f.decode())) sub_run("cp {} tmp/".format(day_f.decode())) sub_run("cp {} tmp/".format(template_file)) 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()))