hg site extension
 
(Arne Babenhauserheide)
2012-08-11: refactoring: use absolute calls for os.path.*

refactoring: use absolute calls for os.path.*

diff --git a/staticsite.py b/staticsite.py
--- a/staticsite.py
+++ b/staticsite.py
@@ -18,7 +18,6 @@ GNU General Public License version 2 or 
 """
 
 import os
-from os.path import join, isdir, isfile, basename, dirname
 import shutil
 import re
 import mercurial
@@ -126,8 +125,8 @@ def writeoverview(ui, repo, target, name
     readme = name
     for f in os.listdir(repo.root):
         if f.lower().startswith("readme"):
-            readme = parsereadme(join(repo.root, f))
-            readme_intro = parsereadme(join(repo.root, f), truncated=True)
+            readme = parsereadme(os.path.join(repo.root, f))
+            readme_intro = parsereadme(os.path.join(repo.root, f), truncated=True)
             overview += "<div id='intro'>"
             overview += readme_intro
             overview += "</div>"
@@ -207,21 +206,21 @@ def writeoverview(ui, repo, target, name
 
     # finish the overview
     overview += templates["foot"]
-    indexfile = join(target, "index.html")
+    indexfile = os.path.join(target, "index.html")
     if not contentequals(indexfile, overview): 
         with open(indexfile, "w") as f: 
             f.write(overview)
 
 def writelog(ui, repo, target, name):
     """Write the full changelog, in steps of 100."""
-    commits = join(target, "commits")
+    commits = os.path.join(target, "commits")
 
     # create the folders
-    if not isdir(commits):
+    if not os.path.isdir(commits):
         os.makedirs(commits)
     for i in range(len(repo.changelog)/100):
         d = commits+"-"+str(i+1)+"00"
-        if not isdir(d):
+        if not os.path.isdir(d):
             os.makedirs(d)
 
     # create the log files
@@ -240,10 +239,10 @@ def writelog(ui, repo, target, name):
                 logs[-1][-1] += "<p><a href=\"../commits-"+str(ck-2)+"00"+"\">later</a></p>"
             elif ck>1:
                 logs[-1][-1] += "<p><a href=\"../commits\">later</a></p>"
-            logs.append([join(d, "index.html"), ""])
+            logs.append([os.path.join(d, "index.html"), ""])
         else:
             d = commits
-            logs.append([join(d, "index.html"), ""])
+            logs.append([os.path.join(d, "index.html"), ""])
 
         logs[-1][-1] += templates["head"].replace("{reponame}", "<a href='../'>"+name+"</a>").replace("{title}", name)
         for c in range(ck*100+1, min(len(repo.changelog)+1, (ck+1)*100)):
@@ -360,7 +359,7 @@ def getforkdata(ui, repo, target, name, 
     return html
 
 def getforkdir(target, forkname):
-    return join("forks", forkname)
+    return os.path.join("forks", forkname)
 
 def writeforks(ui, repo, target, name):
     """Write an info-page for each fork, defined in hg paths.
@@ -373,27 +372,27 @@ def writeforks(ui, repo, target, name):
         if os.path.abspath(forkuri) == os.path.abspath(target):
             continue
         forkdir = getforkdir(target, forkname)
-        if not isdir(join(target, forkdir)):
-            os.makedirs(join(target, forkdir))
-        with open(join(target, forkdir, "index.html"), "w") as f:
+        if not os.path.isdir(os.path.join(target, forkdir)):
+            os.makedirs(os.path.join(target, forkdir))
+        with open(os.path.join(target, forkdir, "index.html"), "w") as f:
             f.write(
                 getforkdata(ui, repo, target, name, forkname, forkuri))
 
 
 def writecommits(ui, repo, target, name, force=False):
     """Write all not yet existing commit files."""
-    commit = join(target, "commit")
+    commit = os.path.join(target, "commit")
 
     # create the folders
-    if not isdir(commit):
+    if not os.path.isdir(commit):
         os.makedirs(commit)
 
     t = cmdutil.changeset_templater(ui, repo, patch=False, diffopts=None, mapfile=None, buffered=False)
     t.use_template(templates["commitlog"].replace("{relativepath}", "../"))
     for c in range(len(repo.changelog)):
         ctx = repo.changectx(str(c))
-        cpath = join(commit, ctx.hex() + ".html")
-        if not force and isfile(cpath):
+        cpath = os.path.join(commit, ctx.hex() + ".html")
+        if not force and os.path.isfile(cpath):
             continue
         with open(cpath, "w") as cf:
             cf.write(templates["head"].replace("{reponame}", "<a href='../'>"+name+"</a>").replace("{title}", name))
@@ -419,11 +418,11 @@ def parsesrcdata(data):
 
 def srcpath(target, ctx, filename):
     """Get the relative path to the static sourcefile for an already escaped filename."""
-    return join(target,"src",ctx.hex(),filename+".html")
+    return os.path.join(target,"src",ctx.hex(),filename+".html")
 
 def rawpath(target, ctx, filename):
     """Get the relative path to the static sourcefile for an already escaped filename."""
-    return join(target,"raw",ctx.hex(),filename)
+    return os.path.join(target,"raw",ctx.hex(),filename)
 
 def ctxdiffstat(ui, repo, ctx):
     """Get the diffstat of a change context."""
@@ -452,7 +451,7 @@ def createindex(ui, repo, target, ctx):
     for filename in ctx:
         filectx = ctx[filename]
         lasteditctx = filectx.filectx(filectx.filerev())
-        index += "<li><a href='../../"+ join("src",lasteditctx.hex(), escapename(filename)+".html") + "'>" + filename + "</a>"# (<a href='../../" + join("raw",lasteditctx.hex(), filename) + "'>raw</a>)</li>"
+        index += "<li><a href='../../"+ os.path.join("src",lasteditctx.hex(), escapename(filename)+".html") + "'>" + filename + "</a>"# (<a href='../../" + os.path.join("raw",lasteditctx.hex(), filename) + "'>raw</a>)</li>"
     index += "</ul>"
     return index
 
@@ -474,20 +473,20 @@ def writesourcetree(ui, repo, target, na
                 # first write the raw data
                 filepath = rawpath(target,ctx,filectx.path())
                 # skip already existing files
-                if not force and isfile(filepath):
+                if not force and os.path.isfile(filepath):
                     continue
                 try:
-                    os.makedirs(dirname(filepath))
+                    os.makedirs(os.path.dirname(filepath))
                 except OSError: pass # exists
                 with open(filepath, "w") as f:
                     f.write(filectx.data())
             # then write it as html
             _filenameescaped = escapename(filectx.path())
             filepath = srcpath(target,ctx,_filenameescaped)
-            if not force and isfile(filepath):
+            if not force and os.path.isfile(filepath):
                 continue
             try:
-                os.makedirs(dirname(filepath))
+                os.makedirs(os.path.dirname(filepath))
             except OSError: pass # exists
             with open(filepath, "w") as f:
                 f.write(templates["srchead"].replace("{filetitle}", name+": " + filename))
@@ -496,12 +495,12 @@ def writesourcetree(ui, repo, target, na
     # then write manifests for all commits
     for c in range(len(repo.changelog)):
         ctx = repo.changectx(str(c))
-        filepath = join(target,"src",ctx.hex(),"index.html")
+        filepath = os.path.join(target,"src",ctx.hex(),"index.html")
         # skip already existing files
-        if not force and isfile(filepath):
+        if not force and os.path.isfile(filepath):
             continue
         try:
-            os.makedirs(dirname(filepath))
+            os.makedirs(os.path.dirname(filepath))
         except OSError: pass # exists
         with open(filepath, "w") as f:
             f.write(templates["head"].replace("{reponame}", "<a href='../../'>"+name+"</a>").replace("{title}", name))
@@ -510,12 +509,12 @@ def writesourcetree(ui, repo, target, na
 
 def parsesite(ui, repo, target, **opts):
     """Create the static folder."""
-    idfile = join(target, _staticidentifier)
-    if not isdir(target):
+    idfile = os.path.join(target, _staticidentifier)
+    if not os.path.isdir(target):
         # make sure the target exists
         os.makedirs(target)
     else: # make sure it is a staticrepo
-        if not isfile(idfile):
+        if not os.path.isfile(idfile):
             if not ui.prompt("The target folder " + target + " has not yet been used as static repo. Really use it? (y/N)", default="n").lower() in ["y", "yes"]:
                 return
             with open(idfile, "w") as i:
@@ -524,18 +523,18 @@ def parsesite(ui, repo, target, **opts):
     if opts["name"]:
         name = opts["name"]
     elif target != "static": name = target
-    else: name = basename(repo.root)
+    else: name = os.path.basename(repo.root)
 
     # first the stylesheets
     screenstyle = opts["screenstyle"]
-    screenfile = join(target, "style.css")
+    screenfile = os.path.join(target, "style.css")
     if screenstyle and not samefilecontent(screenstyle, screenfile):
         shutil.copyfile(screenstyle, screenfile)
     elif not contentequals(screenfile,templates["screenstyle"]):
         with open(screenfile, "w") as f:
             f.write(templates["screenstyle"])
     printstyle = opts["printstyle"]
-    printfile = join(target, "print.css")
+    printfile = os.path.join(target, "print.css")
     if printstyle and not samefilecontent(printstyle, printfile):
         shutil.copyfile(printstyle, printfile)
     elif not contentequals(printfile, templates["printstyle"]):
@@ -595,7 +594,7 @@ def upload(ui, repo, target, ftpstring, 
     ui.status(ftp.getwelcome(), "\n")
 
     # create the target dir.
-    serverdir = dirname(ftppath)
+    serverdir = os.path.dirname(ftppath)
     serverdirparts = ftppath.split("/")
     sd = serverdirparts[0]
     if not sd in ftp.nlst():
@@ -620,9 +619,9 @@ def upload(ui, repo, target, ftpstring, 
 
     for d, dirnames, filenames in os.walk(target):
         for filename in filenames:
-            localfile = join(d, filename)
+            localfile = os.path.join(d, filename)
             serverfile = localfile[len(target)+1:]
-            serverdir = dirname(serverfile)
+            serverdir = os.path.dirname(serverfile)
             serverdirparts = serverdir.split("/")
 #            print serverdirparts, serverfile
             with open(localfile, "rb") as f:
@@ -640,7 +639,7 @@ def upload(ui, repo, target, ftpstring, 
 
                 for sdp in serverdirparts[1:]:
                     sdold = sd
-                    sd = join(sd, sdp)
+                    sd = os.path.join(sd, sdp)
                     #print sd, sdp
                     #print ftp.nlst(sdold)
                     if sd and not sd in _ftplistcache: # should happen only once per superdir