1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
# make_dist.py
# Subversion Details
# $LastChangedDate: 2005-06-24 16:01:45 +0100 (Fri, 24 Jun 2005) $
# $LastChangedBy: fuzzyman $
# $HeadURL: https://svn.rest2web.python-hosting.com/trunk/rest2web.py $
# $LastChangedRevision: 39 $
# Distribution building tool for rest2web
# http://www.voidspace.org.uk/python/rest2web/
# Copyright Michael Foord, 2004 & 2005.
# Released subject to the BSD License
# Please see http://www.voidspace.org.uk/python/license.shtml
# For information about bugfixes, updates, and support, please join the
# Pythonutils mailing list.
# http://groups.google.com/group/pythonutils/
# Comments, suggestions, and bug reports, welcome.
# Scripts maintained at http://www.voidspace.org.uk/python/index.shtml
# E-mail fuzzyman@voidspace.org.uk
import os
import sys
import shutil
import stat
import time
from StringIO import StringIO
from traceback import print_exc
from rest2web.pythonutils.standout import StandOut
from rest2web.pythonutils.pathutils import walkdirs, walkfiles, walkemptydirs, onerror
from os.path import split, splitext
"""
This script makes a new distribution folder from the Working Copy.
It removes all the 'svn' stuff from the distribution.
It also removes 'pyc' files.
It also removes any generated docs/thumbnails/etc that aren't needed in the
final distribution.
This script is also useful for creating a 'clean distribution' for running the
tests on.
"""
# directory to start in (current directory here)
source = '.'
# location to put the built distribution
dest = 'rest2web-dist'
# file extensions to delete
bad_ext = ['.html', '.pyc', '.bak']
# filenames to delete
bad_files = ['thumbs.db', 'log.txt',
'gallery1.ini', 'gallery2.ini',
'gallery3.ini', 'make_dist.py', '__dist__']
# whole directories to remove
bad_dirs = ['thumbnails', '.svn']
# exceptions to the 'bad_ext' rules
good_files = ['entry.html', 'page.html','gallery.html']
# path to log the process to
log_file = 'log.txt'
# pause for input after completion ?
pause = True
if sys.argv[1:] and sys.argv[1] == 'nopause':
pause = False
def copytree(src, dst):
"""
Modified version of copytree from shutil.
DOesn't copy the files we don't want.
"""
names = os.listdir(src)
os.mkdir(dst)
errors = []
for name in names:
if (name in bad_dirs) or (name in bad_files):
continue
ext = splitext(name)[1]
if (ext in bad_ext) and not (name in good_files):
continue
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if os.path.isdir(srcname):
copytree(srcname, dstname)
else:
shutil.copy2(srcname, dstname)
except (IOError, os.error), why:
errors.append((srcname, dstname, why))
if errors:
raise Error, errors
def main():
# delete the distribution directory if it exists
if os.path.isdir(dest):
print 'Deleting the current target directory.'
shutil.rmtree(dest, onerror=onerror)
#
# copy the current copy to dest
print 'Copying the working directory.'
copytree(source, dest)
#
# remove empty directories
# (mainly from the docs_html folder)
print 'Trimming empty directories.'
thelist = []
while True:
lastlist = thelist
thelist = list(walkemptydirs(dest))
if not thelist or lastlist == thelist:
break
for aDir in thelist:
print 'Trimming "%s".' % aDir
os.rmdir(aDir)
#
print '\nDone.'
if __name__ == '__main__':
stand = None
if log_file:
stand = StandOut(logfile=log_file)
time1 = time.time()
try:
# do the business
main()
except KeyboardInterrupt:
sys.stderr.write('Exited by Keyboard Interrupt.\n')
except Exception, e:
# print any error without bombing out
# (so we can display it, then close our files nicely)
f = StringIO()
print_exc(file=f)
# write error to sys.stderr rather than printing to sys.stdout
sys.stderr.write(f.getvalue() + '\n')
else:
print
print ('Time taken to make distribution was %.3f seconds.' %
(time.time() - time1))
#
if stand:
stand.close()
#
if pause:
raw_input('Hit return to continue >>> ')
"""
TODO
=====
* Auto-create zip and tarball
* Build executable
* MD5 hash
* replace with a monolithic setup.py ?
* What about accepting command line arguments or reading a config file for the
details ?
Move onerror into pathutils.
ISSUES
======
None yet.
CHANGELOG
=========
2006/07/27
----------
Distribution directory is now 'rest2web-dist'.
2005/11/12
----------
``make_dist`` will now work from SVN without pythonutils installed.
Added logging of build process.
2005/11/11
----------
``onerror`` handler for ``rmtree`` - can delete read only directories.
2005/08/08
----------
Implemented to cope with the gallery files.
2005/08/01
----------
Moved into SVN
Amended for new location and for gallery files.
"""
|