Mostly ok till line 534

This commit is contained in:
Phyks 2013-07-27 14:56:40 +02:00
parent 95e7413348
commit 59fc3b5d47

View File

@ -85,8 +85,8 @@ def auto_dir(path):
if not os.path.exists(directory):
os.makedirs(directory)
except IOError:
sys.exit("[ERROR] An error occured while creating "+path+" file \
and parent dirs.")
sys.exit("[ERROR] An error occured while creating "+path+" file "
"and parent dirs.")
# Replace some user specific syntax tags (to repplace smileys for example)
@ -100,17 +100,17 @@ def replace_tags(article, search_list, replace_list):
try:
opts, args = getopt.gnu_getopt(sys.argv, "hf", ["help", "force-regen"])
except getopt.GetoptError:
sys.exit("Error while parsing command line arguments. \
See pre-commit -h for more infos on how to use.")
sys.exit("Error while parsing command line arguments. "
"See pre-commit -h for more infos on how to use.")
force_regen = False
for opt, arg in opts:
if opt in ("-h", "--help"):
print("Usage :")
print("This should be called automatically as a pre-commit git hook. \
You can also launch it manually right before commiting.\n")
print("This script generates static pages ready to be served behind \
your webserver.\n")
print("This should be called automatically as a pre-commit git hook. "
"You can also launch it manually right before commiting.\n")
print("This script generates static pages ready to be served behind "
"your webserver.\n")
print("Usage :")
print("-h \t --help \t displays this help message.")
print("-f \t --force-regen \t force complete rebuild of all pages.")
@ -136,9 +136,9 @@ try:
else:
params[option.strip()] = value.strip()
except IOError:
sys.exit("[ERROR] Unable to load raw/params file which defines important \
parameters. Does such a file exist ? \
See doc for more info on this file.")
sys.exit("[ERROR] Unable to load raw/params file which defines important "
"parameters. Does such a file exist ? See doc for more info "
"on this file.")
# Fill lists for modified, deleted and added files
@ -151,13 +151,13 @@ if not force_regen:
try:
changes = subprocess.check_output(["git", "diff", "--cached", "--name-status"], universal_newlines=True)
except:
sys.exit("[ERROR] An error occured when fetching file changes \
from git.")
sys.exit("[ERROR] An error occured when fetching file changes "
"from git.")
changes = changes.strip().split("\n")
if changes == [""]:
sys.exit("[ERROR] Nothing to do... Did you had new files with \
\"git add\" before ?")
sys.exit("[ERROR] Nothing to do... Did you had new files with "
"\"git add\" before ?")
for changed_file in changes:
if changed_file[0].startswith("A"):
@ -174,15 +174,14 @@ else:
added_files = list_directory("raw")
if not added_files and not modified_files and not deleted_files:
sys.exit("[ERROR] Nothing to do... Did you had new files with \
\"git add\" before ?")
sys.exit("[ERROR] Nothing to do... Did you had new files with "
"\"git add\" before ?")
# Only keep modified raw articles files
for filename in list(added_files):
direct_copy = False
if not filename.startswith("raw/"):
# TODO : Delete files starting by gen / blog ? + same thing for modified / deleted
added_files.remove(filename)
continue
@ -193,8 +192,9 @@ for filename in list(added_files):
if ((not filename.endswith(".html") and not filename.endswith(".ignore"))
or direct_copy):
print("[INFO] (Not HTML file) Copying directly not html file \
"+filename[4:]+" to blog dir.")
# Note : this deal with CSS, images or footer file
print("[INFO] (Not HTML file) Copying directly not html file "
+ filename[4:]+" to blog dir.")
auto_dir("blog/"+filename[4:])
shutil.copy(filename, "blog/"+filename[4:])
@ -202,8 +202,8 @@ for filename in list(added_files):
continue
if filename.endswith(".ignore"):
print("[INFO] (Not published) Found not published article \
"+filename[4:-7]+".")
print("[INFO] (Not published) Found not published article "
+ filename[4:-7]+".")
added_files.remove(filename)
continue
@ -221,16 +221,16 @@ for filename in list(modified_files):
if ((not filename.endswith("html") and not filename.endswith("ignore"))
or direct_copy):
print("[INFO] (Not HTML file) Updating directly not html file \
"+filename[4:]+" to blog dir.")
print("[INFO] (Not HTML file) Updating directly not html file "
+ filename[4:]+" to blog dir.")
auto_dir("blog/"+filename[4:])
shutil.copy(filename, "blog/"+filename[4:])
modified_files.remove(filename)
continue
if filename.endswith("ignore"):
print("[INFO] (Not published) Found not published article \
"+filename[4:-7]+".")
print("[INFO] (Not published) Found not published article "
+ filename[4:-7]+".")
added_files.remove(filename)
continue
@ -248,16 +248,16 @@ for filename in list(deleted_files):
if ((not filename.endswith("html") and not filename.endswith("ignore"))
or direct_copy):
print("[INFO] (Not HTML file) Copying directly not html file \
"+filename[4:]+" to blog dir.")
print("[INFO] (Not HTML file) Copying directly not html file "
+ filename[4:]+" to blog dir.")
auto_dir("blog/"+filename[4:])
shutil.copy(filename, "blog/"+filename[4:])
deleted_files.remove(filename)
continue
if filename.endswith("ignore"):
print("[INFO] (Not published) Found not published article \
"+filename[4:-7]+".")
print("[INFO] (Not published) Found not published article "
+ filename[4:-7]+".")
added_files.remove(filename)
continue
@ -275,8 +275,8 @@ for filename in added_files:
sys.exit("[ERROR] Unable to open file "+filename+".")
if not tags:
sys.exit("[ERROR] (TAGS) In added article "+filename[4:]+" : \
No tags found !")
sys.exit("[ERROR] (TAGS) In added article "+filename[4:]+" : "
"No tags found !")
for tag in tags:
try:
auto_dir("gen/tags/"+tag+".tmp")
@ -284,11 +284,11 @@ for filename in added_files:
tag_file.seek(0)
if filename[4:] not in tag_file.read():
tag_file.write(filename[4:]+"\n")
print("[INFO] (TAGS) Found tag "+tag+" in article \
"+filename[4:])
print("[INFO] (TAGS) Found tag "+tag+" in article "
+ filename[4:])
except IOError:
sys.exit("[ERROR] (TAGS) New tag found but an error \
occured in article "+filename[4:]+": "+tag+".")
sys.exit("[ERROR] (TAGS) New tag found but an error "
"occured in article "+filename[4:]+": "+tag+".")
for filename in modified_files:
try:
@ -298,8 +298,8 @@ for filename in modified_files:
sys.exit("[ERROR] Unable to open file "+filename+".")
if not tags:
sys.exit("[ERROR] (TAGS) In modified article "+filename[4:]+" : \
No tags found !")
sys.exit("[ERROR] (TAGS) In modified article "+filename[4:]+" : "
" No tags found !")
for tag in list_directory("gen/tags/"):
try:
@ -318,19 +318,19 @@ for filename in modified_files:
print("[INFO] (TAGS) Deleted tag "+tag[:tag.index(".tmp")]+" in modified article "+filename[4:])
tags.remove(tag_file[9:])
except IOError:
sys.exit("[ERROR] (TAGS) An error occured when parsing tags \
of article "+filename[4:]+".")
sys.exit("[ERROR] (TAGS) An error occured when parsing tags "
" of article "+filename[4:]+".")
for tag in tags: # New tags created
try:
auto_dir("gen/tags/"+tag+".tmp")
with open("gen/tags/"+tag+".tmp", "a+") as tag_file: # Delete tag file here if empty after deletion
tag_file.write(filename[4:]+"\n")
print("[INFO] (TAGS) Found new tag "+tag+" for \
modified article "+filename[4:])
print("[INFO] (TAGS) Found new tag "+tag+" for "
"modified article "+filename[4:])
except IOError:
sys.exit("[ERROR] (TAGS) An error occured when parsing tags \
of article "+filename[4:]+".")
sys.exit("[ERROR] (TAGS) An error occured when parsing tags "
"of article "+filename[4:]+".")
# Delete tags for deleted files and delete all generated files
for filename in deleted_files:
@ -341,8 +341,8 @@ for filename in deleted_files:
sys.exit("[ERROR] Unable to open file "+filename+".")
if not tags:
sys.exit("[ERROR] (TAGS) In deleted article "+filename[4:]+" : \
No tags found !")
sys.exit("[ERROR] (TAGS) In deleted article "+filename[4:]+" : "
"No tags found !")
for tag in tags:
try:
@ -350,38 +350,37 @@ for filename in deleted_files:
tag_file_old_content = tag_file.read()
tag_file.truncate()
# Delete file in tag
tag_file.write(tag_file_old_content.replace(filename[4:]+"\n", ""))
tag_file_write = tag_file_old_content.replace(filename[4:]+"\n", "")
if tag_file_write:
tag_file.write(tag_file_write)
except IOError:
sys.exit("[ERROR] An error occured while deleting article \
"+filename[4:]+" from tags files.")
sys.exit("[ERROR] An error occured while deleting article" +
filename[4:]+" from tags files.")
if not tag_file_write:
try:
os.unlink(tag)
print("[INFO] (TAGS) No more article with tag "+tag[8:-4]+", deleting it.")
except FileNotFoundError:
print("[INFO] "+tag+" was found to be empty but there was an error during deletion. You should check manually.")
# Delete generated files
try:
os.unlink("gen/"+filename[4:-5]+".gen")
os.unlink("blog/"+filename[4:])
except FileNotFoundError:
print("[INFO] Article "+filename[4:]+" seems to not have already been \
generated. You should check manually.")
print("[INFO] Article "+filename[4:]+" seems to not have already been "
"generated. You should check manually.")
print("[INFO] Deleted article "+filename[4:]+" in both gen and blog \
directories")
print("[INFO] Deleted article "+filename[4:]+" in both gen and blog "
"directories")
# TODO : Delete following code
# Delete empty tags files
#for tag in list_directory("gen/tags"):
# try:
# with open(tag, 'r') as tag_file:
# content = tag_file.read().strip()
# if content == '':
# try:
# os.unlink(tag)
# os.unlink(tag.replace("gen", "blog"))
# except FileNotFoundError:
# print("[INFO] "+tag+" was found to be empty but there was an error during deletion. You should check manually.")
# print("[INFO] (TAGS) No more article with tag "+tag[8:-4]+", deleting it.")
# except IOError:
# sys.exit("[ERROR] Unable to open "+tag+".")
# Common lists that are used multiple times
last_articles = latest_articles("raw/", int(params["NB_ARTICLES_INDEX"]))
tags_full_list = list_directory("gen/tags")
# Generate html for each article
for filename in added_files+modified_files:
try:
with open(filename, 'r') as fh:
@ -409,7 +408,7 @@ for filename in added_files+modified_files:
date_readable = "Le "+date[0:2]+"/"+date[2:4]+"/"+date[4:8]+" à "+date[9:11]+":"+date[11:13]
# Write generated HTML for this article in gen /
#TODO : Replace tags
article = replace_tags(article, search_list, replace_list)
try:
auto_dir("gen/"+filename[4:-5]+".gen")
with open("gen/"+filename[4:-5]+".gen", 'w') as article_file:
@ -418,117 +417,97 @@ for filename in added_files+modified_files:
except IOError:
sys.exit("[ERROR] An error occured when writing generated HTML for article "+filename[4:]+".")
#=====================================
#Generate headers file (except title)
# Starting to generate header file (except title)
tags_header = "<ul>"
for tag in tags_full_list:
tags_header += "<li><a href=\""+params["BLOG_URL"]+tag[4:-4]+".html\">"+tag[9:-4]+"</a></li>"
tags_header += "</ul>"
try:
with open("raw/header.html", "r") as header_fh:
#Tags
tags = list_directory("gen/tags")
header = header_fh.read()
tags_header = "<ul>"
for tag in tags:
tags_header += "<li><a href=\""+params["BLOG_URL"]+tag[4:-4]+".html\">"+tag[9:-4]+"</a></li>"
tags_header += "</ul>"
header = header.replace("@categories", tags_header, 1)
header = header.replace("@blog_url", params["BLOG_URL"], 1)
#Articles
latest_articles_list = latest_articles("gen/", 5)
articles_header = "<ul>"
for article in latest_articles_list:
try:
with open(article, 'r') as fh:
line = fh.readline()
while "@title" not in line:
line = fh.readline()
line = line.strip()
title_pos = line.find("@title=")
title = line[title_pos+7:]
articles_header += "<li><a href=\""+params["BLOG_URL"]+article[4:-4]+".html\">"+title+"</a></li>"
except IOError:
sys.exit("[ERROR] Unable to open file "+article+".")
articles_header += "</ul>"
header = header.replace("@articles", articles_header, 1)
try:
auto_dir("gen/header.gen")
with open("gen/header.gen", "w") as header_gen_fh:
header_gen_fh.write(header)
print("[INFO] (HEADER) Header has been generated successfully.")
except FileNotFoundError:
sys.exit("[ERROR] (HEADER) An error occured while writing header file.")
except IOError:
sys.exit("[ERROR] Unable to open gen/header.gen for writing.")
except IOError:
sys.exit("[ERROR] Unable to open raw/header.html file.")
#Generate footer file
if not os.path.isfile("raw/footer.html"):
sys.exit("[ERROR] (FOOTER) Footer file (raw/footer.html) not found.")
try:
shutil.copy("raw/footer.html", "gen/footer.gen")
print("[INFO] (FOOTER) Footer has been generated successfully")
except IOError:
sys.exit("[ERROR] Unable to copy the footer.html file.")
header = header.replace("@tags", tags_header, 1)
header = header.replace("@blog_url", params["BLOG_URL"], 1)
articles_header = "<ul>"
articles_index = "<ul>"
#Copy CSS files
if not os.path.isdir("raw/css") or list_directory("raw/css") == []:
print("[INFO] (CSS) No CSS files in raw/css folder")
else:
# Generate header (except title) + index file
del title
for i, article in enumerate(last_articles):
try:
shutil.copytree("raw/css", "blog/css")
with open(article, "r") as fh:
for line in fh.readlines():
if "@title=" in line:
title = line[line.find("@title=")+7:].strip()
break
except IOError:
sys.exit("[ERROR] An error occured while copying css files.")
sys.exit("[ERROR] Unable to open "+article+" file.")
if not isset("title"):
sys.exit("[ERROR] No title found in article "+article[4:]+".")
if i < 5:
articles_header += "<li><a href=\""+params["BLOG_URL"]+article[4:-4]+".html\">"+title+"</a></li>"
articles_index += "<li><a href=\""+params["BLOG_URL"]+article[4:-4]+".html\">"+title+"</a></li>"
# Finishing header gen
articles_header += "</ul>"
header = header.replace("@articles", articles_header, 1)
#Regenerate index file
last_articles_index = latest_articles("gen/", int(params["NB_ARTICLES_INDEX"]))
try:
auto_dir("blog/index.html")
with open("blog/index.html", "w") as index_fh:
try:
with open("gen/header.gen", "r") as header_gen_fh:
index = header_gen_fh.read()
for article in last_articles_index:
with open(article, "r") as article_fh:
index += replace_tags(article_fh.read(), search_list, replace_list)
with open("gen/footer.gen") as footer_gen_fh:
index += footer_gen_fh.read()
auto_dir("gen/header.gen")
with open("gen/header.gen", "w") as header_gen_fh:
header_gen_fh.write(header)
print("[INFO] (HEADER) Header has been generated successfully.")
except FileNotFoundError:
sys.exit("[ERROR] (HEADER) An error occured while writing header file.")
except IOError:
sys.exit("[ERROR] Unable to open gen/header.gen for writing.")
index = index.replace("@titre", params["BLOG_TITLE"], 1)
index_fh.write(index)
print("[INFO] (INDEX) Index page has been generated successfully.")
except IOError:
sys.exit("[ERROR] Error while creating index.html file")
# Getting content from footer file
try:
with open("raw/footer.html", "r") as footer_fh:
footer = footer_fh.read()
except IOError:
sys.exit("[ERROR] An error occured while parsing footer file raw/footer.html.")
# Finishing index gen
articles_index += "</ul>"
index = header.replace("@title", params["BLOG_TITLE"], 1) + articles_index + footer
try:
with open("blog/index.html", "w") as index_fh:
index_fh.write(index)
print("[INFO] (INDEX) Index page has been generated successfully.")
except IOError:
sys.exit("[ERROR] Error while creating index.html file")
except IOError:
sys.exit("[ERROR] Unable to open index.html file for writing.")
#Regenerate tags pages
for tag in list_directory("gen/tags"):
# Regenerate tags pages
for tag in tags_full_list:
tag_content = header.replace("@title", params["BLOG_TITLE"]+" - "+tag[4:-4], 1)
with open(tag, "r") as tag_gen_fh:
for line in tag_gen_fh.readlines():
line = line.replace(".html", ".gen")
with open("gen/"+line.strip(), "r") as article_fh:
tag_content += article_fh.read()
tag_content += footer
try:
auto_dir(tag.replace("gen/", "blog/"))
with open(tag.replace("gen/", "blog/")[:-4]+".html", "w") as tag_fh:
with open(tag, "r") as tag_gen_fh:
with open("gen/header.gen", "r") as header_fh:
tag_content = header_fh.read()
tag_content = tag_content.replace("@titre", params["BLOG_TITLE"]+" - "+tag[4:-4], 1)
tag_gen_fh_lines = tag_gen_fh.readlines()
for line in tag_gen_fh_lines:
line = line.replace(".html", ".gen")
with open("gen/"+line.strip(), "r") as article_handler:
tag_content += replace_tags(article_handler.read(), search_list, replace_list)
with open("gen/footer.gen", "r") as footer_handler:
tag_content += footer_handler.read()
tag_fh.write(tag_content)
print("[INFO] (TAGS) Tag page for "+tag[9:-4]+" has been generated successfully.")
except IOError:
sys.exit("[ERROR] An error occured while generating tag page \""+tag[9:-4]+"\"")
#Finish articles pages generation
# Finish articles pages generation
for filename in added_files+modified_files:
try:
auto_dir("blog/"+filename[4:])
with open("blog/"+filename[4:], "w") as article_fh:
@ -543,7 +522,7 @@ for filename in added_files+modified_files:
title = line[title_pos+7:]
article_gen_fh.seek(0)
article = article.replace("@titre", params["BLOG_TITLE"]+" - "+title, 1)
article = article.replace("@title", params["BLOG_TITLE"]+" - "+title, 1)
article += replace_tags(article_gen_fh.read(), search_list, replace_list)
with open("gen/footer.gen", "r") as footer_gen_fh:
article += footer_gen_fh.read()
@ -552,7 +531,8 @@ for filename in added_files+modified_files:
except IOError:
sys.exit("[ERROR] An error occured while generating article "+filename[4:]+" page.")
#Generate pages for each year and month
#======================================
# Generate pages for each year and month
with open("gen/header.gen", "r") as header_gen_fh:
header_gen = header_gen_fh.read()
@ -567,8 +547,8 @@ for i in years_list:
except ValueError:
continue
#Generate page per year
page_year = header_gen.replace("@titre", params["BLOG_TITLE"]+" - "+i, 1)
# Generate page per year
page_year = header_gen.replace("@title", params["BLOG_TITLE"]+" - "+i, 1)
months_list = os.listdir("blog/"+i)
months_list.sort(reverse=True)
@ -576,8 +556,8 @@ for i in years_list:
if not os.path.isdir("blog/"+i+"/"+j):
continue
#Generate pages per month
page_month = header_gen.replace("@titre", params["BLOG_TITLE"]+" - "+i+"/"+j, 1)
# Generate pages per month
page_month = header_gen.replace("@title", params["BLOG_TITLE"]+" - "+i+"/"+j, 1)
articles_list = list_directory("gen/"+i+"/"+j)
articles_list.sort(key=lambda x: os.stat(x).st_mtime, reverse=True)
@ -605,13 +585,13 @@ for i in years_list:
sys.exit("[ERROR] Unable to write index file for "+i+".")
#Generate RSS
# Generate RSS
rss = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">"
rss += "<channel><atom:link href=\""+params["BLOG_URL"]+"rss.xml\" rel=\"self\" type=\"application/rss+xml\"/><title>"+params["BLOG_TITLE"]+"</title><link>"+params["BLOG_URL"]+"</link>"
rss += "<description>"+params["DESCRIPTION"]+"</description><language>"+params["LANGUAGE"]+"</language><copyright>"+params["COPYRIGHT"]+"</copyright>"
rss += "<webMaster>"+params["WEBMASTER"]+"</webMaster><lastBuildDate>"+strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())+"</lastBuildDate>"
for article in last_articles_index:
for article in last_articles:
del date, title
try:
with open(article, "r") as article_fh: