From 59fc3b5d476637bafa132915959e99eba343ace1 Mon Sep 17 00:00:00 2001 From: Phyks Date: Sat, 27 Jul 2013 14:56:40 +0200 Subject: [PATCH] Mostly ok till line 534 --- pre-commit.py | 306 +++++++++++++++++++++++--------------------------- 1 file changed, 143 insertions(+), 163 deletions(-) diff --git a/pre-commit.py b/pre-commit.py index 0176323..1400846 100755 --- a/pre-commit.py +++ b/pre-commit.py @@ -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 = "" try: with open("raw/header.html", "r") as header_fh: - #Tags - tags = list_directory("gen/tags") header = header_fh.read() - tags_header = "" - 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 = "" - 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 = "" +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 = "" rss += ""+params["BLOG_TITLE"]+""+params["BLOG_URL"]+"" rss += ""+params["DESCRIPTION"]+""+params["LANGUAGE"]+""+params["COPYRIGHT"]+"" rss += ""+params["WEBMASTER"]+""+strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())+"" -for article in last_articles_index: +for article in last_articles: del date, title try: with open(article, "r") as article_fh: