Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 9edccd0

Browse files
committed
Add additional fields to update_library
1 parent 20d2cfc commit 9edccd0

File tree

1 file changed

+64
-11
lines changed

1 file changed

+64
-11
lines changed

runestone/server/utils.py

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# Standard library
1414
# ----------------
1515

16+
import datetime
1617
import os
1718
import subprocess
1819
import sys
@@ -95,6 +96,8 @@ def _build_runestone_book(course, click=click):
9596
click.echo("Deploy failed, check the log to see what went wrong.")
9697
return False
9798

99+
update_library(config, "", course, click, build_system="Runestone")
100+
98101

99102
# Build a PreTeXt Book
100103
# --------------------
@@ -107,6 +110,7 @@ def _build_ptx_book(config, gen, manifest, course, click=click):
107110
course: the name of the course to build.
108111
click: default is the click module otherwise an object that has an echo method
109112
"""
113+
110114
if not os.path.exists("project.ptx"):
111115
click.echo("PreTeXt books need a project.ptx file")
112116
return False
@@ -152,7 +156,8 @@ def _build_ptx_book(config, gen, manifest, course, click=click):
152156
populate_static(config, mpath, course)
153157
# update the library page
154158
click.echo("updating library...")
155-
update_library(config, mpath, course)
159+
main_page = find_real_url(cname)
160+
update_library(config, mpath, course, main_page=main_page, build_system="PTX")
156161
return True
157162

158163

@@ -225,7 +230,9 @@ def extract_docinfo(tree, string, attr=None, click=click):
225230
return ""
226231

227232

228-
def update_library(config, mpath, course, click=click):
233+
def update_library(
234+
config, mpath, course, click=click, build_system="", main_page="index.html"
235+
):
229236
"""
230237
Parameters:
231238
config : This originated as a config object from click -- a mock config will be provided by the AuthorServer
@@ -236,39 +243,85 @@ def update_library(config, mpath, course, click=click):
236243
237244
Returns: Nothing
238245
"""
239-
tree = ET.parse(mpath)
240-
docinfo = tree.find("./library-metadata")
241246
eng = create_engine(config.dburl)
242-
title = extract_docinfo(docinfo, "title")
243-
subtitle = extract_docinfo(docinfo, "subtitle")
244-
description = extract_docinfo(docinfo, "blurb")
245-
shelf = extract_docinfo(docinfo, "shelf")
247+
if build_system == "PTX":
248+
tree = ET.parse(mpath)
249+
docinfo = tree.find("./library-metadata")
250+
title = extract_docinfo(docinfo, "title")
251+
subtitle = extract_docinfo(docinfo, "subtitle")
252+
description = extract_docinfo(docinfo, "blurb")
253+
shelf = extract_docinfo(docinfo, "shelf")
254+
else:
255+
try:
256+
config_vars = {}
257+
exec(open("config.py").read(), config_vars)
258+
except Exception as e:
259+
print(f"Error adding book {book} to library list: {e}")
260+
return
261+
subtitle = ""
262+
if "navbar_title" in config_vars:
263+
title = config_vars["navbar_title"]
264+
elif "html_title" in config_vars:
265+
title = config_vars["html_title"]
266+
elif "html_short_title" in config_vars:
267+
title = config_vars["html_short_title"]
268+
else:
269+
title = "Runestone Book"
270+
# update course description if found in the book's conf.py
271+
if "course_description" in config_vars:
272+
description = config_vars["course_description)"]
273+
# update course key_words if found in book's conf.py
274+
if "key_words" in config_vars:
275+
key_words = config_vars["key_words)"]
276+
277+
if "shelf_section" in config_vars:
278+
shelf = config_vars["shelf_section"]
279+
else:
280+
shelf = "Computer Science"
281+
246282
click.echo(f"{title} : {subtitle}")
283+
247284
try:
248285
res = eng.execute(f"select * from library where basecourse = '{course}'")
249286
except:
250287
click.echo("Missing library table? You may need to run an alembic migration.")
251288
return False
252289

290+
build_time = str(datetime.datetime.utcnow())
253291
if res.rowcount == 0:
254292
eng.execute(
255293
f"""insert into library
256-
(title, subtitle, description, shelf_section, basecourse )
257-
values('{title}', '{subtitle}', '{description}', '{shelf}', '{course}') """
294+
(title, subtitle, description, shelf_section, basecourse,
295+
build_system, main_page, last_build )
296+
values('{title}', '{subtitle}', '{description}', '{shelf}', '{course}',
297+
'{build_system}', '{main_page}', '{build_time}') """
258298
)
259299
else:
260300
eng.execute(
261301
f"""update library set
262302
title = '{title}',
263303
subtitle = '{subtitle}',
264304
description = '{description}',
265-
shelf_section = '{shelf}'
305+
shelf_section = '{shelf}',
306+
build_system = '{build_system}',
307+
main_page = '{main_page}',
308+
build_time = '{build_time}'
266309
where basecourse = '{course}'
267310
"""
268311
)
269312
return True
270313

271314

315+
def find_real_url(book):
316+
idx = pathlib.Path("published", book, "index.html")
317+
if idx.exists():
318+
with open(idx, "r") as idxf:
319+
for line in idxf:
320+
if g := re.search(r"refresh.*URL='(.*?)'", line):
321+
return g.group(1)
322+
return "index.html"
323+
324+
272325
def populate_static(config, mpath: Path, course: str, click=click):
273326
"""
274327
Copy the apropriate Javascript to the _static folder for PreTeXt books. This may

0 commit comments

Comments
 (0)