|
| 1 | +import os |
| 2 | +import pathlib |
| 3 | +from sqlalchemy import create_engine |
| 4 | +import importlib |
| 5 | +import pdb |
| 6 | +import sys |
| 7 | +import re |
| 8 | + |
| 9 | +# This is a one-off script to populate the new library table. Once that is populated |
| 10 | +# this can be removed from the repo - or kept for posterity. |
| 11 | + |
| 12 | +sys.path.insert(0, ".") |
| 13 | +if os.environ["HOME"] == "/root": |
| 14 | + cwd = pathlib.Path("/srv/web2py/applications/runestone/books") |
| 15 | +else: |
| 16 | + cwd = pathlib.Path(os.environ.get("HOME"), "Runestone", "books") |
| 17 | + |
| 18 | + |
| 19 | +def _find_real_url(libdir, book): |
| 20 | + idx = pathlib.Path(libdir, book, "published", book, "index.html") |
| 21 | + if idx.exists(): |
| 22 | + with open(idx, "r") as idxf: |
| 23 | + for line in idxf: |
| 24 | + if g := re.search(r"refresh.*URL='(.*?)'", line): |
| 25 | + return g.group(1) |
| 26 | + return "index.html" |
| 27 | + |
| 28 | + |
| 29 | +print("CWD = ", cwd) |
| 30 | + |
| 31 | + |
| 32 | +def update_library(book): |
| 33 | + """ |
| 34 | + Parameters: |
| 35 | + config : This originated as a config object from click -- a mock config will be provided by the AuthorServer |
| 36 | + mpath: Path to the runestone-manifest file which containes the library metadata |
| 37 | + course: the name of the course we are building |
| 38 | +
|
| 39 | + Update the library table using meta data from the book |
| 40 | +
|
| 41 | + Returns: Nothing |
| 42 | + """ |
| 43 | + eng = create_engine(os.environ["DEV_DBURL"]) |
| 44 | + print(f"BOOK = {book}") |
| 45 | + |
| 46 | + try: |
| 47 | + config = importlib.import_module(f"{book}.conf") |
| 48 | + except Exception as e: |
| 49 | + print(f"Error adding book {book} to library list: {e}") |
| 50 | + return |
| 51 | + |
| 52 | + book_info = {} |
| 53 | + book_info.update(description="") |
| 54 | + book_info.update(key_words="") |
| 55 | + book_info.update(basecourse=book) |
| 56 | + book_info.update(is_visible="T") |
| 57 | + book_info.update(subtitle="") |
| 58 | + if hasattr(config, "navbar_title"): |
| 59 | + book_info["title"] = config.navbar_title |
| 60 | + elif hasattr(config, "html_title"): |
| 61 | + book_info["title"] = config.html_title |
| 62 | + elif hasattr(config, "html_short_title"): |
| 63 | + book_info["title"] = config.html_short_title |
| 64 | + else: |
| 65 | + book_info["title"] = "Runestone Book" |
| 66 | + # update course description if found in the book's conf.py |
| 67 | + if hasattr(config, "course_description"): |
| 68 | + book_info.update(description=config.course_description) |
| 69 | + # update course key_words if found in book's conf.py |
| 70 | + if hasattr(config, "key_words"): |
| 71 | + book_info.update(key_words=config.key_words) |
| 72 | + if hasattr(config, "publisher") and config.publisher == "PTX": |
| 73 | + bks = "/ns" |
| 74 | + book_info["build_system"] = "PTX" |
| 75 | + else: |
| 76 | + bks = "/ns" |
| 77 | + book_info["build_system"] = "Runestone" |
| 78 | + |
| 79 | + if hasattr(config, "shelf_section"): |
| 80 | + book_info["shelf_section"] = config.shelf_section |
| 81 | + else: |
| 82 | + book_info["shelf_section"] = "Computer Science" |
| 83 | + |
| 84 | + book_info["main_page"] = _find_real_url(cwd, book) |
| 85 | + |
| 86 | + res = eng.execute(f"select * from library where basecourse = '{book}'") |
| 87 | + |
| 88 | + if res.rowcount == 0: |
| 89 | + eng.execute( |
| 90 | + """insert into library |
| 91 | + (title, subtitle, description, shelf_section, basecourse, is_visible, main_page ) |
| 92 | + values('{title}', '{subtitle}', '{description}', '{shelf_section}', |
| 93 | + '{basecourse}', '{is_visible}', '{main_page}') """.format( |
| 94 | + **book_info |
| 95 | + ) |
| 96 | + ) |
| 97 | + else: |
| 98 | + eng.execute( |
| 99 | + """update library set |
| 100 | + title = '{title}', |
| 101 | + subtitle = '{subtitle}', |
| 102 | + description = '{description}', |
| 103 | + shelf_section = '{shelf_section}', |
| 104 | + main_page = '{main_page}' |
| 105 | + where basecourse = '{basecourse}' |
| 106 | + """.format( |
| 107 | + **book_info |
| 108 | + ) |
| 109 | + ) |
| 110 | + return True |
| 111 | + |
| 112 | + |
| 113 | +os.chdir(cwd) |
| 114 | +for path in cwd.iterdir(): |
| 115 | + if path.is_dir(): |
| 116 | + update_library(path.name) |
0 commit comments