Bug correction, now using git ls built-in function instead of directory listing to avoir problems with not added files. TODO : Error while generating articles html and month pages

This commit is contained in:
Phyks 2013-07-27 21:59:48 +02:00
parent fc5c8c3b1f
commit 43f7621f7f

View File

@ -4,7 +4,6 @@
# TODO : Test the whole thing # TODO : Test the whole thing
# TODO : What happens when I run it as a hook ? # TODO : What happens when I run it as a hook ?
# TODO : What happens when I commit with -a option ? # TODO : What happens when I commit with -a option ?
# TODO : git ls-files
import sys import sys
import getopt import getopt
@ -22,6 +21,14 @@ def isset(variable):
return variable in locals() or variable in globals() return variable in locals() or variable in globals()
def isint(variable):
try:
int(variable)
return True
except ValueError:
return False
# List all files in path directory # List all files in path directory
# Works recursively # Works recursively
# Return files list with path relative to current dir # Return files list with path relative to current dir
@ -50,30 +57,19 @@ def get_tags(fh):
# Return the number latest articles in dir directory # Return the number latest articles in dir directory
def latest_articles(directory, number): def latest_articles(directory, number):
now = datetime.datetime.now() try:
counter = 0 latest_articles = subprocess.check_output(["git",
latest_articles = [] "ls-files",
directory],
for i in range(int(now.strftime('%Y')), 0, -1): universal_newlines=True)
if counter >= number: except:
break sys.exit("[ERROR] An error occurred when fetching file changes "
"from git.")
if os.path.isdir(directory+"/"+str(i)): latest_articles = latest_articles.strip().split("\n")
for j in range(12, 0, -1): latest_articles = [x for x in latest_articles if isint(x[4:8])]
if j < 10: latest_articles.sort(key=lambda x: (x[4:11], os.stat(x).st_mtime),
j = "0"+str(j) reverse=True)
return latest_articles[:number]
if os.path.isdir(directory+"/"+str(i)+"/"+str(j)):
articles_list = list_directory(directory+str(i)+"/"+str(j))
# Sort by date the articles
articles_list.sort(key=lambda x: os.stat(x).st_mtime)
latest_articles += articles_list[:number-counter]
if len(latest_articles) < number-counter:
counter += len(articles_list)
else:
counter = number
return latest_articles
# Auto create necessary directories to write a file # Auto create necessary directories to write a file
@ -537,7 +533,7 @@ for i, article in enumerate(["gen/"+x[4:-5]+".gen" for x in last_articles]):
if i < 5: if i < 5:
articles_header += "<li>" articles_header += "<li>"
articles_header += ("<a href=\""+params["BLOG_URL"] + articles_header += ("<a href=\""+params["BLOG_URL"] +
article[4:-5]+".html\">"+title+"</a>") article[4:-4]+".html\">"+title+"</a>")
articles_header += "</li>" articles_header += "</li>"
articles_index += content articles_index += content