##############################################################################
# ThanCad 0.0.9 "DoesSomething": 2dimensional CAD with raster support for engineers.
#
# Copyright (c) 2001-2009 Thanasis Stamos, August 23, 2009
# URL: http://thancad.sourceforge.net
# e-mail: cyberthanasis@excite.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details (www.gnu.org/licenses/gpl.html).
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
"""\
ThanCad 0.0.9 "DoesSomething": 2dimensional CAD with raster support for engineers.
This module contains a script which updates the short description,
version, author etc. of ThanCad, which exist as comments in the
beginning of every source file.
"""
descMod = __doc__
import sys
from p_ggen import path,inpNo
import thanvers
SENTCOM = "#" * 78
def thanMain():
"Main function."
print descMod
sourcedir = path("~/temp/tcadtree.34").expand()
exclude_dirs = "var thanpackages"
print "source path:", sourcedir
print "excluded directories:", exclude_dirs
print "--------------------------------------------"
a = inpNo("Proceed with update (enter=yes)?", True)
if not a: sys.exit(0)
exclude_dirs = [sourcedir/p for p in exclude_dirs.split()]
thanCadAbout = [SENTCOM + "\n"]
for dl in thanvers.thanCadAbout.splitlines(1):
thanCadAbout.append("# " + dl)
thanCadAbout.append(SENTCOM + "\n")
thanUpdateSources(sourcedir, exclude_dirs, thanCadAbout)
print "--------------------------------------------"
print "Sources have been updated as .new files."
a = inpNo("Proceed with replace (enter=no)?", False)
if a:
rotateSources(sourcedir, exclude_dirs)
print "done"
print "--------------------------------------------"
lineCount(sourcedir)
#==========================================================================
def thanUpdateSources(sourcedir, exclude_dirs, thanCadAbout):
"Updates the version number, description and license in the sources."
for fp in path(sourcedir).walkfiles("*.py"):
if inex(exclude_dirs, fp): continue
print fp
fInp = file(fp, "r")
iterInp = iter(fInp)
fOut = file(fp+".new", "w")
__skipOldDoc(iterInp, fOut)
__writeNewDoc(fOut, thanCadAbout)
__writeRest(iterInp, fOut)
fInp.close()
fOut.close()
#==========================================================================
def rotateSources(sourcedir, exclude_dirs):
"Renames the previously created *.py.new files to *.py files."
for f in path(sourcedir).walkfiles("*.py"):
if inex(exclude_dirs, f): continue
# if f.basename() == "__init__.py": continue
filbak = __getBackupName(f)
f.rename(filbak)
fnew = f + ".new"
fnew.rename(f)
def inex(exclude_dirs, p):
for ex in exclude_dirs:
if p.startswith(ex): return True
return False
#==========================================================================
def lineCount(sourcedir):
"Counts all the lines in .py files recursively."
n = 0
for f in path(sourcedir).walkfiles("*.py"):
n += len(f.lines())
print "Total line count=", n
#==========================================================================
def __getBackupName(fp):
"Finds a backup name for the file fp, which does not already exist."
filnam = fp+".bak"
if not filnam.exists(): return filnam
for i in xrange(10):
filnam = path("%s.bk%d" % (fp, i))
if not filnam.exists(): return filnam
raise IOError, fp+": Can not create backup file."
#==========================================================================
def __skipOldDoc (iterInp, fOut):
"Skips the doc string which must be long format string and at the first line."
for dl in iterInp:
if dl.rstrip() == SENTCOM: break
fOut.write(dl.rstrip()+'\n')
else:
print " Sentinel comment not found at the beginning of file!"
sys.exit()
for dl in iterInp:
if dl.rstrip() == SENTCOM: break
else:
print " Sentinel comment not found before end of file!"
sys.exit(1)
for dl in iterInp:
if dl.rstrip() == '"""\\': break
else:
print " Doc string not found before end of file!"
sys.exit(1)
for dl in iterInp:
break
else:
print " Doc string does not exist!"
sys.exit(1)
#==========================================================================
def __writeNewDoc (fOut, thanCadAbout):
"Writes description of the project and the new doc string."
for dl in thanCadAbout: fOut.write(dl)
fOut.write('\n"""\\\n')
fOut.write(thanvers.thanCadShortDesc+"\n")
#==========================================================================
def __writeRest (iterInp, fOut):
"Copies the rest of the source file."
for dl in iterInp:
fOut.write(dl.rstrip()+'\n')
#==========================================================================
if __name__ == "__main__": thanMain()
|