Basic options

Basic options to :
- display a help message
- force complete regeneration of the pages
This commit is contained in:
Phyks 2013-07-23 20:48:17 +02:00
parent 71f618a27a
commit dd39ba0c47
2 changed files with 47 additions and 18 deletions

3
TODO
View File

@ -1,7 +1,8 @@
Search ? Search ?
What happen when a file is moved with git ? What happen when a file is moved with git ?
Flake8 the whole thing ! :) Flake8 the whole thing ! :)
pass parameters via command line to force regeneration of files basepath ?
tags / smileys / ...
Known bugs: Known bugs:
========== ==========

View File

@ -3,6 +3,7 @@
#TODO : gitignore #TODO : gitignore
import sys import sys
import getopt
import shutil import shutil
import os import os
import datetime import datetime
@ -73,12 +74,24 @@ def auto_dir(path):
sys.exit("[ERROR] An error occured while creating "+path+" file \ sys.exit("[ERROR] An error occured while creating "+path+" file \
and parent dirs.") and parent dirs.")
#Find the changes to be committed
try: try:
#TODO : Check this command opts, args = getopt.gnu_getopt(sys.argv, "hf", ["help", "force-regen"])
changes = subprocess.check_output(["git", "diff", "--cached", "--name-status"], universal_newlines=True) except getopt.GetoptError:
except: sys.exit("Error while parsing command line arguments. See pre-commit -h for more infos on how to use.")
sys.exit("[ERROR] An error occured when running git diff.")
for opt, arg in opts:
if opt in ("-h", "--help"):
print("Usage :")
print("This should be called automatically as a git hook when commiting. You can also launch it manually.\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 the regeneration of all the pages.")
sys.exit(0)
elif opt in ("-f", "--force-regen"):
force_regen = True
else:
force_regen = False
#Set parameters #Set parameters
with open("raw/params", "r") as params_fh: with open("raw/params", "r") as params_fh:
@ -94,19 +107,34 @@ modified_files = []
deleted_files = [] deleted_files = []
added_files = [] added_files = []
changes = changes.strip().split("\n") if not force_regen:
if changes == [""]: #Find the changes to be committed
sys.exit("[ERROR] Nothing to do...") try:
#TODO : Check this command
changes = subprocess.check_output(["git", "diff", "--cached", "--name-status"], universal_newlines=True)
except:
sys.exit("[ERROR] An error occured when running git diff.")
for changed_file in changes: changes = changes.strip().split("\n")
if changed_file[0] == "A": if changes == [""]:
added_files.append(changed_file[changed_file.index("\t")+1:]) sys.exit("[ERROR] Nothing to do...")
elif changed_file[0] == "M":
modified_files.append(changed_file[changed_file.index("\t")+1:]) for changed_file in changes:
elif changed_file[0] == "D": if changed_file[0] == "A":
deleted_files.append(changed_file[changed_file.index("\t")+1:]) added_files.append(changed_file[changed_file.index("\t")+1:])
else: elif changed_file[0] == "M":
sys.exit("[ERROR] An error occured when running git diff.") modified_files.append(changed_file[changed_file.index("\t")+1:])
elif changed_file[0] == "D":
deleted_files.append(changed_file[changed_file.index("\t")+1:])
else:
sys.exit("[ERROR] An error occured when running git diff.")
else:
shutil.rmtree("blog/")
shutil.rmtree("gen/")
added_files = list_directory("raw")
if len(added_files) == 0 and len(modified_files) == 0 and len(deleted_files) == 0:
sys.exit("[ERROR] Nothing to do...")
#Only keep modified raw articles files #Only keep modified raw articles files
for filename in list(added_files): for filename in list(added_files):