Mercurial > hgbook
changeset 721:2180358c32c4
Move some files to contrib
author | Dongsheng Song <dongsheng.song@gmail.com> |
---|---|
date | Thu, 12 Mar 2009 15:40:40 +0800 |
parents | 1ef7708b3b7f |
children | 082bb76417f1 |
files | contrib/hg-interdiff contrib/hg-replay contrib/latex-to-docbook contrib/sillybench.py examples/hg-interdiff examples/hg-replay sillybench/sillybench.py tools/latex-to-docbook |
diffstat | 8 files changed, 527 insertions(+), 527 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/contrib/hg-interdiff Thu Mar 12 15:40:40 2009 +0800 @@ -0,0 +1,45 @@ +#!/usr/bin/env python +# +# Adapter for using interdiff with mercurial's extdiff extension. +# +# Copyright 2006 Bryan O'Sullivan <bos@serpentine.com> +# +# This software may be used and distributed according to the terms of +# the GNU General Public License, incorporated herein by reference. + +import os, sys + +def walk(base): + # yield all non-directories below the base path. + for root, dirs, files in os.walk(base): + for f in files: + path = os.path.join(root, f) + yield path[len(base)+1:], path + else: + if os.path.isfile(base): + yield '', base + +# create list of unique file names under both directories. +files = dict(walk(sys.argv[1])) +files.update(walk(sys.argv[2])) +files = files.keys() +files.sort() + +def name(base, f): + if f: + path = os.path.join(base, f) + else: + path = base + # interdiff requires two files; use /dev/null if one is missing. + if os.path.exists(path): + return path + return '/dev/null' + +ret = 0 + +for f in files: + if os.system('interdiff "%s" "%s"' % (name(sys.argv[1], f), + name(sys.argv[2], f))): + ret = 1 + +sys.exit(ret)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/contrib/hg-replay Thu Mar 12 15:40:40 2009 +0800 @@ -0,0 +1,107 @@ +#!/usr/bin/env python +# +# Adapter for using interdiff with mercurial's extdiff extension. +# +# Copyright 2006 Bryan O'Sullivan <bos@serpentine.com> +# +# This software may be used and distributed according to the terms of +# the GNU General Public License, incorporated herein by reference. + +import os +import shutil +import sys +import tempfile + +if len(sys.argv) < 4: + print >> sys.stderr, ('usage: %s srcrepo destrepo cset-to-omit [...]' % + os.path.basename(sys.argv[0])) + sys.exit(1) + +srcrepo, destrepo = sys.argv[1], sys.argv[2] +omit = sys.argv[3:] + +changemap = {} +revs = [] + +parent = None + +sys.stdout.write('gathering history...') +sys.stdout.flush() + +for line in os.popen("hg --cwd %r log -r0:tip --template '{rev}:{node} {parents}\n'" % srcrepo): + changes = line.split() + cset = changes[0].split(':')[1] + rev = len(revs) + changemap[cset] = rev + if len(changes) >= 2: + p1 = int(changes[1].split(':', 1)[0]) + if len(changes) == 3: + p2 = int(changes[2].split(':', 1)[0]) + else: + p2 = None + if len(changes) == 1: + p1 = parent + revs.append((cset, p1, p2)) + parent = rev + +sys.stdout.write(' %d revs\n' % len(revs)) + +def findrev(r): + try: + i = int(r) + if str(i) == r: + rev = i + if rev < 0: + rev += len(revs) + if rev < 0 or rev > len(revs): + print >> sys.stderr, 'bad changeset: %r' % r + sys.exit(1) + cset = revs[rev][0] + except ValueError: + cset = r + matches = [changemap[c] for c in changemap if c.startswith(cset)] + if len(matches) != 1: + print >> sys.stderr, 'bad changeset: %r' % r + sys.exit(1) + rev = matches[0] + return rev + +def run(cmd): + print cmd + ret = os.system(cmd) + if ret: + print >> sys.stderr, 'failure:', cmd + sys.exit(1) + +omit = map(findrev, omit) +omit.sort() +newrevs = revs[:omit[0]] +tip = len(newrevs) - 1 +run('hg clone -q -r%s %r %r' % (tip, srcrepo, destrepo)) + +os.environ['HGMERGE'] = 'true' + +patchdir = tempfile.mkdtemp(prefix='replay.') +try: + run('hg --cwd %r export --git -o %r%s%%R %d:tip' % + (srcrepo, patchdir, os.sep, omit[0]+1)) + for rev in xrange(omit[0], len(revs)): + if rev in omit: + print 'omit', rev + newrevs.append((None, revs[rev][1], None)) + continue + _, p1, p2 = revs[rev] + np1 = newrevs[p1][1] + if tip != np1: + run('hg --cwd %r update -q -C %s' % (destrepo, np1)) + np2 = None + if p2: + np2 = newrevs[p2][1] + run('hg --cwd %r merge -q %s' % (destrepo, np2)) + print >> sys.stderr, 'XXX - cannot handle merges properly yet' + run('hg --cwd %r import -q -f %r%s%d' % (destrepo, patchdir, os.sep, rev)) + tip = len(newrevs) - 1 + newrevs.append((None, tip, np2)) +finally: + print 'cleaning up ...' + #shutil.rmtree(patchdir)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/contrib/latex-to-docbook Thu Mar 12 15:40:40 2009 +0800 @@ -0,0 +1,198 @@ +#!/usr/bin/python +# +# This is the most horrible of hacks. Pretend you're not looking.</para> + +import cStringIO as StringIO +import re, sys + +sections = { + 'chapter': 'chapter', + 'section': 'sect1', + 'subsection': 'sect2', + 'subsubsection': 'sect3', + } + +envs = { + 'codesample2': 'programlisting', + 'codesample4': 'programlisting', + 'enumerate': 'orderedlist', + 'figure': 'informalfigure', + 'itemize': 'itemizedlist', + 'note': 'note', + 'quote': 'blockquote', + } + +def process(ifp, ofp): + print >> ofp, '<!-- vim: set filetype=docbkxml shiftwidth=2 autoindent expandtab tw=77 : -->\n' + stack = [] + para = True + inlist = 0 + for line in ifp: + if line.startswith('%%% Local Variables:'): + break + line = (line.rstrip() + .replace('~', ' ') + .replace('&', '&') + .replace('---', '&emdash;') + .replace('\_', '_') + .replace('\{', '{') + .replace('\}', '}') + .replace('\$', '$') + .replace('\%', '%') + .replace('\#', '#') + .replace('<', '<') + .replace('>', '>') + .replace('``', '<quote>') + .replace("''", '</quote>') + .replace('\\', '\\')) + line = re.sub(r'\s*\\(?:centering|small)\b\s*', '', line) + line = re.sub(r'\\(?:hgrc\\|hgrc)\b', + r'<filename role="special"> /.hgrc</filename>', line) + line = re.sub(r'\\item\[(?P<key>[^]]+)\]', r'\item \g<key>:', line) + line = re.sub(r'\\bug{(?P<id>\d+)}', + r'<ulink role="hg-bug" url="http://www.selenic.com/mercurial/bts/issue\g<id>">issue \g<id></ulink>', line) + line = re.sub(r'\\cite{([^}]+)}', r'<citation>\1</citation>', line) + line = re.sub(r'\\hggopt{(?P<opt>[^}]+)}', + r'<option role="hg-opt-global">\g<opt></option>', line) + line = re.sub(r'\\hgxopt{(?P<ext>[^}]+)}{(?P<cmd>[^}]+)}{(?P<opt>[^}]+)}', + r'<option role="hg-ext-\g<ext>-cmd-\g<cmd>-opt">\g<opt></option>', line) + line = re.sub(r'\\hgxcmd{(?P<ext>[^}]+)}{(?P<cmd>[^}]+)}', + r'<command role="hg-ext-\g<ext>">\g<cmd></command>', line) + line = re.sub(r'\\hgext{(?P<ext>[^}]+)}', + r'<literal role="hg-ext">\g<ext></literal>', line) + line = re.sub(r'\\hgopt{(?P<cmd>[^}]+)}{(?P<opt>[^}]+)}', + r'<option role="hg-opt-\g<cmd>">\g<opt></option>', + line) + line = re.sub(r'\\cmdopt{(?P<cmd>[^}]+)}{(?P<opt>[^}]+)}', + r'<option role="cmd-opt-\g<cmd>">\g<opt></option>', + line) + line = re.sub(r'\\hgcmd{(?P<cmd>[^}]+)}', + r'<command role="hg-cmd">hg \g<cmd></command>', line) + line = re.sub(r'\\caption{(?P<text>[^}]+?)}', + r'<caption><para>\g<text></para></caption>', line) + line = re.sub(r'\\grafix{(?P<name>[^}]+)}', + r'<mediaobject><imageobject><imagedata fileref="\g<name>"/></imageobject><textobject><phrase>XXX add text</phrase></textobject></mediaobject>', line) + line = re.sub(r'\\envar{(?P<name>[^}]+)}', + r'<envar>\g<name></envar>', line) + line = re.sub(r'\\rcsection{(?P<sect>[^}]+)}', + r'<literal role="rc-\g<sect>">\g<sect></literal>', line) + line = re.sub(r'\\rcitem{(?P<sect>[^}]+)}{(?P<name>[^}]+)}', + r'<envar role="rc-item-\g<sect>">\g<name></envar>', line) + line = re.sub(r'\\dirname{(?P<dir>[^}]+?)}', + r'<filename class="directory">\g<dir></filename>', line) + line = re.sub(r'\\filename{(?P<file>[^}]+?)}', + r'<filename>\g<file></filename>', line) + line = re.sub(r'\\tildefile{(?P<file>[^}]+)}', + r'<filename role="home">~/\g<file></filename>', line) + line = re.sub(r'\\sfilename{(?P<file>[^}]+)}', + r'<filename role="special">\g<file></filename>', line) + line = re.sub(r'\\sdirname{(?P<dir>[^}]+)}', + r'<filename role="special" class="directory">\g<dir></filename>', line) + line = re.sub(r'\\interaction{(?P<id>[^}]+)}', + r'<!-- &interaction.\g<id>; -->', line) + line = re.sub(r'\\excode{(?P<id>[^}]+)}', + r'<!-- &example.\g<id>; -->', line) + line = re.sub(r'\\pymod{(?P<mod>[^}]+)}', + r'<literal role="py-mod">\g<mod></literal>', line) + line = re.sub(r'\\pymodclass{(?P<mod>[^}]+)}{(?P<class>[^}]+)}', + r'<literal role="py-mod-\g<mod>">\g<class></literal>', line) + line = re.sub(r'\\url{(?P<url>[^}]+)}', + r'<ulink url="\g<url>">\g<url></ulink>', line) + line = re.sub(r'\\href{(?P<url>[^}]+)}{(?P<text>[^}]+)}', + r'<ulink url="\g<url>">\g<text></ulink>', line) + line = re.sub(r'\\command{(?P<cmd>[^}]+)}', + r'<command>\g<cmd></command>', line) + line = re.sub(r'\\option{(?P<opt>[^}]+)}', + r'<option>\g<opt></option>', line) + line = re.sub(r'\\ref{(?P<id>[^}]+)}', r'<xref linkend="\g<id>"/>', line) + line = re.sub(r'\\emph{(?P<txt>[^}]+)}', + r'<emphasis>\g<txt></emphasis>', line) + line = re.sub(r'\\texttt{(?P<txt>[^}]+)}', + r'<literal>\g<txt></literal>', line) + line = re.sub(r'\\textbf{(?P<txt>[^}]+)}', + r'<emphasis role="bold">\g<txt></emphasis>', line) + line = re.sub(r'\\hook{(?P<name>[^}]+)}', + r'<literal role="hook">\g<name></literal>', line) + line = re.sub(r'\\tplfilter{(?P<name>[^}]+)}', + r'<literal role="template-filter">\g<name></literal>', line) + line = re.sub(r'\\tplkword{(?P<name>[^}]+)}', + r'<literal role="template-keyword">\g<name></literal>', line) + line = re.sub(r'\\tplkwfilt{(?P<tpl>[^}]+)}{(?P<name>[^}]+)}', + r'<literal role="template-kw-filt-\g<tpl>">\g<name></literal>', line) + line = re.sub(r'\\[vV]erb(.)(?P<txt>[^\1]+?)\1', + r'<literal>\g<txt></literal>', line) + line = re.sub(r'\\package{(?P<name>[^}]+)}', + r'<literal role="package">\g<name></literal>', line) + line = re.sub(r'\\hgcmdargs{(?P<cmd>[^}]+)}{(?P<args>[^}]+)}', + r'<command role="hg-cmd">hg \g<cmd> \g<args></command>', + line) + line = re.sub(r'\\cmdargs{(?P<cmd>[^}]+)}{(?P<args>[^}]+)}', + r'<command>\g<cmd> \g<args></command>', + line) + m = re.match(r'\\(chapter|section|subsection|subsubsection){(.*)}', line) + if m: + kind, content = m.groups() + sec = sections[kind] + while stack and stack[-1] >= sec: + close = stack.pop() + print >> ofp, '</%s>' % close + stack.append(sec) + print >> ofp, '<%s>\n<title>%s</title>' % (sec, content) + else: + m = re.match(r'\s*\\(begin|end){(?P<sect>[^}]+)}', line) + if m: + if not para: + print >> ofp, '</para>' + if inlist: + ofp.write('</listitem>') + para = True + state, env = m.groups() + env = envs[env] + if state == 'begin': + ofp.write('<') + if env in ('itemizedlist', 'orderedlist'): + inlist = 1 + else: + ofp.write('</') + if env in ('itemizedlist', 'orderedlist'): + inlist = 0 + print >> ofp, env + '>' + else: + if line.startswith('\\item '): + if inlist > 1: + print >> ofp, '</para>' + print >> ofp, '</listitem>' + else: + inlist = 2 + para = True + line = line[6:] + if line and para: + if inlist: + ofp.write('<listitem>') + ofp.write('<para>') + para = False + if not line and not para: + print >> ofp, '</para>' + if inlist: + ofp.write('</listitem>') + para = True + print >> ofp, line + while stack: + print >> ofp, '</%s>' % stack.pop() + ofp.write('\n'.join(['\n<!--', + 'local variables: ', + 'sgml-parent-document: ("00book.xml" "book" "chapter")', + 'end:', + '-->'])) + + +if __name__ == '__main__': + for name in sys.argv[1:]: + if not name.endswith('.tex'): + continue + newname = name[:-3] + 'xml' + ofp = StringIO.StringIO() + process(open(name), ofp) + s = ofp.getvalue() + s = re.sub('\n+</para>', '</para>', s, re.M) + open(newname, 'w').write(s)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/contrib/sillybench.py Thu Mar 12 15:40:40 2009 +0800 @@ -0,0 +1,177 @@ +#!/usr/bin/python +# +# Silly benchmarking program, to give a vague idea of how fast a few +# tools are on a handful of common operations. +# +# Use a fairly big and real source tarball to test with: Firefox +# 2.0.0.3 (37622 files, 5374 directories, 343MB unpacked onto +# 4KB-blocksize ext3). + +import csv +import os +import shutil +import sys +import tempfile +import time +import urllib2 + +url = 'ftp://ftp.mozilla.org/pub/mozilla.org/firefox/releases/2.0.0.3/source/firefox-2.0.0.3-source.tar.bz2' + +class CommandFailure(Exception): + pass + +class rcs(object): + def __init__(self): + self.logfp = open(self.__class__.__name__ + '.csv', 'w') + self.csv = csv.writer(self.logfp) + + def download(self): + name = url[url.rfind('/')+1:] + path = os.path.join(os.environ['HOME'], name) + if not os.path.isfile(path): + ofp = open(path + '.part', 'wb') + try: + ifp = urllib2.urlopen(url) + nbytes = ifp.info()['content-length'] + sys.stdout.write('%s: %s bytes ' % (name, nbytes)) + sys.stdout.flush() + while True: + data = ifp.read(131072) + if not data: break + sys.stdout.write('.') + sys.stdout.flush() + ofp.write(data) + del ofp + os.rename(path + '.part', path) + except: + if os.path.exists(path + '.part'): + os.unlink(path + '.part') + if os.path.exists(path): + os.unlink(path) + raise + return path + + def run(self, args, mustsucceed=True): + ret = os.spawnvp(os.P_WAIT, args[0], args) + if ret < 0: + msg = 'killed by signal %d' % (-ret) + if ret > 0: + msg = 'exited with status %d' % (ret) + if ret: + if mustsucceed: + raise CommandFailure('%s: %s' % (msg, ' '.join(args))) + print >> sys.stderr, 'WARNING: %s: %s' % (msg, ' '.join(args)) + + def time(self, *args, **kwargs): + start = time.time() + self.run(*args, **kwargs) + end = time.time() + return end - start + + def logtime(self, name, elapsed, rest=[]): + self.log('time:' + name, '%.3f' % elapsed, rest) + + def log(self, name, value, rest=[]): + item = (name, value, repr(rest)) + print ' '.join(item) + self.csv.writerow(item) + self.logfp.flush() + + def unpack(self): + tarball = self.download() + t = self.time(['tar', '-C', self.wdir, '-jxf', tarball]) + self.logtime('internal:untar', t) + for name in os.listdir(os.path.join(self.wdir, 'mozilla')): + os.rename(os.path.join(self.wdir, 'mozilla', name), + os.path.join(self.wdir, name)) + + def cleanup(self): + pass + + def add(self, paths): + pass + + def commit(self, msg, paths): + pass + + def status(self, path): + pass + + def remove(self, path): + pass + + +class subversion(rcs): + def __init__(self, root): + rcs.__init__(self) + self.repo = os.path.join(root, 'repo') + self.wdir = os.path.join(root, 'wc') + create = self.time(['svnadmin', 'create', '--fs-type=fsfs', self.repo]) + self.logtime('svn:create', create) + co = self.time(['svn', 'co', 'file://' + self.repo, self.wdir]) + self.logtime('svn:co', co) + self.logtime('init', create + co) + os.chdir(self.wdir) + + def dropmeta(self, names): + return [n for n in names if os.path.basename(n) != '.svn'] + + def add(self, paths): + t = self.time(['svn', 'add', '-q'] + paths) + self.logtime('add %r' % paths, t) + + def commit(self, msg, paths=[]): + if paths: + t = self.time(['svn', 'ci', '-q', '-m', msg] + paths) + else: + t = self.time(['svn', 'ci', '-q', '-m', msg]) + self.logtime('commit %r' % paths, t) + + +class mercurial(rcs): + def __init__(self, root): + rcs.__init__(self) + self.repo = os.path.join(root, 'repo') + self.wdir = self.repo + init = self.time(['hg', 'init', self.repo]) + self.logtime('init', init) + os.chdir(self.wdir) + + def dropmeta(self, names): + return [n for n in names if os.path.basename(n) != '.hg'] + + def add(self, paths): + t = self.time(['hg', 'add', '-q'] + paths) + self.logtime('add %r' % paths, t) + + def commit(self, msg, paths=[]): + if paths: + t = self.time(['hg', 'ci', '-q', '-m', msg] + paths) + else: + t = self.time(['hg', 'ci', '-q', '-m', msg]) + self.logtime('commit %r' % paths, t) + +def benchmark(cls): + oldcwd = os.getcwd() + root = tempfile.mkdtemp(prefix='sillybench.') + try: + print 'root', root + inst = cls(root) + inst.unpack() + names = inst.dropmeta(os.listdir('.')) + dirs = [n for n in names if os.path.isdir(n)] + nondirs = [n for n in names if not os.path.isdir(n)] + dirs.sort(key=hash) + names.sort(key=hash) + for d in dirs[:len(dirs)/2]: + inst.add([d]) + inst.commit('Add %r' % d, [d]) + inst.add(dirs[len(dirs)/2:] + names) + inst.commit('Add remaining dirs and files') + finally: + print >> sys.stderr, '[cleaning up...]' + shutil.rmtree(root) + os.chdir(oldcwd) + +benchmark(mercurial) +#benchmark(subversion)
--- a/examples/hg-interdiff Thu Mar 12 15:35:19 2009 +0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,45 +0,0 @@ -#!/usr/bin/env python -# -# Adapter for using interdiff with mercurial's extdiff extension. -# -# Copyright 2006 Bryan O'Sullivan <bos@serpentine.com> -# -# This software may be used and distributed according to the terms of -# the GNU General Public License, incorporated herein by reference. - -import os, sys - -def walk(base): - # yield all non-directories below the base path. - for root, dirs, files in os.walk(base): - for f in files: - path = os.path.join(root, f) - yield path[len(base)+1:], path - else: - if os.path.isfile(base): - yield '', base - -# create list of unique file names under both directories. -files = dict(walk(sys.argv[1])) -files.update(walk(sys.argv[2])) -files = files.keys() -files.sort() - -def name(base, f): - if f: - path = os.path.join(base, f) - else: - path = base - # interdiff requires two files; use /dev/null if one is missing. - if os.path.exists(path): - return path - return '/dev/null' - -ret = 0 - -for f in files: - if os.system('interdiff "%s" "%s"' % (name(sys.argv[1], f), - name(sys.argv[2], f))): - ret = 1 - -sys.exit(ret)
--- a/examples/hg-replay Thu Mar 12 15:35:19 2009 +0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,107 +0,0 @@ -#!/usr/bin/env python -# -# Adapter for using interdiff with mercurial's extdiff extension. -# -# Copyright 2006 Bryan O'Sullivan <bos@serpentine.com> -# -# This software may be used and distributed according to the terms of -# the GNU General Public License, incorporated herein by reference. - -import os -import shutil -import sys -import tempfile - -if len(sys.argv) < 4: - print >> sys.stderr, ('usage: %s srcrepo destrepo cset-to-omit [...]' % - os.path.basename(sys.argv[0])) - sys.exit(1) - -srcrepo, destrepo = sys.argv[1], sys.argv[2] -omit = sys.argv[3:] - -changemap = {} -revs = [] - -parent = None - -sys.stdout.write('gathering history...') -sys.stdout.flush() - -for line in os.popen("hg --cwd %r log -r0:tip --template '{rev}:{node} {parents}\n'" % srcrepo): - changes = line.split() - cset = changes[0].split(':')[1] - rev = len(revs) - changemap[cset] = rev - if len(changes) >= 2: - p1 = int(changes[1].split(':', 1)[0]) - if len(changes) == 3: - p2 = int(changes[2].split(':', 1)[0]) - else: - p2 = None - if len(changes) == 1: - p1 = parent - revs.append((cset, p1, p2)) - parent = rev - -sys.stdout.write(' %d revs\n' % len(revs)) - -def findrev(r): - try: - i = int(r) - if str(i) == r: - rev = i - if rev < 0: - rev += len(revs) - if rev < 0 or rev > len(revs): - print >> sys.stderr, 'bad changeset: %r' % r - sys.exit(1) - cset = revs[rev][0] - except ValueError: - cset = r - matches = [changemap[c] for c in changemap if c.startswith(cset)] - if len(matches) != 1: - print >> sys.stderr, 'bad changeset: %r' % r - sys.exit(1) - rev = matches[0] - return rev - -def run(cmd): - print cmd - ret = os.system(cmd) - if ret: - print >> sys.stderr, 'failure:', cmd - sys.exit(1) - -omit = map(findrev, omit) -omit.sort() -newrevs = revs[:omit[0]] -tip = len(newrevs) - 1 -run('hg clone -q -r%s %r %r' % (tip, srcrepo, destrepo)) - -os.environ['HGMERGE'] = 'true' - -patchdir = tempfile.mkdtemp(prefix='replay.') -try: - run('hg --cwd %r export --git -o %r%s%%R %d:tip' % - (srcrepo, patchdir, os.sep, omit[0]+1)) - for rev in xrange(omit[0], len(revs)): - if rev in omit: - print 'omit', rev - newrevs.append((None, revs[rev][1], None)) - continue - _, p1, p2 = revs[rev] - np1 = newrevs[p1][1] - if tip != np1: - run('hg --cwd %r update -q -C %s' % (destrepo, np1)) - np2 = None - if p2: - np2 = newrevs[p2][1] - run('hg --cwd %r merge -q %s' % (destrepo, np2)) - print >> sys.stderr, 'XXX - cannot handle merges properly yet' - run('hg --cwd %r import -q -f %r%s%d' % (destrepo, patchdir, os.sep, rev)) - tip = len(newrevs) - 1 - newrevs.append((None, tip, np2)) -finally: - print 'cleaning up ...' - #shutil.rmtree(patchdir)
--- a/sillybench/sillybench.py Thu Mar 12 15:35:19 2009 +0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,177 +0,0 @@ -#!/usr/bin/python -# -# Silly benchmarking program, to give a vague idea of how fast a few -# tools are on a handful of common operations. -# -# Use a fairly big and real source tarball to test with: Firefox -# 2.0.0.3 (37622 files, 5374 directories, 343MB unpacked onto -# 4KB-blocksize ext3). - -import csv -import os -import shutil -import sys -import tempfile -import time -import urllib2 - -url = 'ftp://ftp.mozilla.org/pub/mozilla.org/firefox/releases/2.0.0.3/source/firefox-2.0.0.3-source.tar.bz2' - -class CommandFailure(Exception): - pass - -class rcs(object): - def __init__(self): - self.logfp = open(self.__class__.__name__ + '.csv', 'w') - self.csv = csv.writer(self.logfp) - - def download(self): - name = url[url.rfind('/')+1:] - path = os.path.join(os.environ['HOME'], name) - if not os.path.isfile(path): - ofp = open(path + '.part', 'wb') - try: - ifp = urllib2.urlopen(url) - nbytes = ifp.info()['content-length'] - sys.stdout.write('%s: %s bytes ' % (name, nbytes)) - sys.stdout.flush() - while True: - data = ifp.read(131072) - if not data: break - sys.stdout.write('.') - sys.stdout.flush() - ofp.write(data) - del ofp - os.rename(path + '.part', path) - except: - if os.path.exists(path + '.part'): - os.unlink(path + '.part') - if os.path.exists(path): - os.unlink(path) - raise - return path - - def run(self, args, mustsucceed=True): - ret = os.spawnvp(os.P_WAIT, args[0], args) - if ret < 0: - msg = 'killed by signal %d' % (-ret) - if ret > 0: - msg = 'exited with status %d' % (ret) - if ret: - if mustsucceed: - raise CommandFailure('%s: %s' % (msg, ' '.join(args))) - print >> sys.stderr, 'WARNING: %s: %s' % (msg, ' '.join(args)) - - def time(self, *args, **kwargs): - start = time.time() - self.run(*args, **kwargs) - end = time.time() - return end - start - - def logtime(self, name, elapsed, rest=[]): - self.log('time:' + name, '%.3f' % elapsed, rest) - - def log(self, name, value, rest=[]): - item = (name, value, repr(rest)) - print ' '.join(item) - self.csv.writerow(item) - self.logfp.flush() - - def unpack(self): - tarball = self.download() - t = self.time(['tar', '-C', self.wdir, '-jxf', tarball]) - self.logtime('internal:untar', t) - for name in os.listdir(os.path.join(self.wdir, 'mozilla')): - os.rename(os.path.join(self.wdir, 'mozilla', name), - os.path.join(self.wdir, name)) - - def cleanup(self): - pass - - def add(self, paths): - pass - - def commit(self, msg, paths): - pass - - def status(self, path): - pass - - def remove(self, path): - pass - - -class subversion(rcs): - def __init__(self, root): - rcs.__init__(self) - self.repo = os.path.join(root, 'repo') - self.wdir = os.path.join(root, 'wc') - create = self.time(['svnadmin', 'create', '--fs-type=fsfs', self.repo]) - self.logtime('svn:create', create) - co = self.time(['svn', 'co', 'file://' + self.repo, self.wdir]) - self.logtime('svn:co', co) - self.logtime('init', create + co) - os.chdir(self.wdir) - - def dropmeta(self, names): - return [n for n in names if os.path.basename(n) != '.svn'] - - def add(self, paths): - t = self.time(['svn', 'add', '-q'] + paths) - self.logtime('add %r' % paths, t) - - def commit(self, msg, paths=[]): - if paths: - t = self.time(['svn', 'ci', '-q', '-m', msg] + paths) - else: - t = self.time(['svn', 'ci', '-q', '-m', msg]) - self.logtime('commit %r' % paths, t) - - -class mercurial(rcs): - def __init__(self, root): - rcs.__init__(self) - self.repo = os.path.join(root, 'repo') - self.wdir = self.repo - init = self.time(['hg', 'init', self.repo]) - self.logtime('init', init) - os.chdir(self.wdir) - - def dropmeta(self, names): - return [n for n in names if os.path.basename(n) != '.hg'] - - def add(self, paths): - t = self.time(['hg', 'add', '-q'] + paths) - self.logtime('add %r' % paths, t) - - def commit(self, msg, paths=[]): - if paths: - t = self.time(['hg', 'ci', '-q', '-m', msg] + paths) - else: - t = self.time(['hg', 'ci', '-q', '-m', msg]) - self.logtime('commit %r' % paths, t) - -def benchmark(cls): - oldcwd = os.getcwd() - root = tempfile.mkdtemp(prefix='sillybench.') - try: - print 'root', root - inst = cls(root) - inst.unpack() - names = inst.dropmeta(os.listdir('.')) - dirs = [n for n in names if os.path.isdir(n)] - nondirs = [n for n in names if not os.path.isdir(n)] - dirs.sort(key=hash) - names.sort(key=hash) - for d in dirs[:len(dirs)/2]: - inst.add([d]) - inst.commit('Add %r' % d, [d]) - inst.add(dirs[len(dirs)/2:] + names) - inst.commit('Add remaining dirs and files') - finally: - print >> sys.stderr, '[cleaning up...]' - shutil.rmtree(root) - os.chdir(oldcwd) - -benchmark(mercurial) -#benchmark(subversion)
--- a/tools/latex-to-docbook Thu Mar 12 15:35:19 2009 +0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,198 +0,0 @@ -#!/usr/bin/python -# -# This is the most horrible of hacks. Pretend you're not looking.</para> - -import cStringIO as StringIO -import re, sys - -sections = { - 'chapter': 'chapter', - 'section': 'sect1', - 'subsection': 'sect2', - 'subsubsection': 'sect3', - } - -envs = { - 'codesample2': 'programlisting', - 'codesample4': 'programlisting', - 'enumerate': 'orderedlist', - 'figure': 'informalfigure', - 'itemize': 'itemizedlist', - 'note': 'note', - 'quote': 'blockquote', - } - -def process(ifp, ofp): - print >> ofp, '<!-- vim: set filetype=docbkxml shiftwidth=2 autoindent expandtab tw=77 : -->\n' - stack = [] - para = True - inlist = 0 - for line in ifp: - if line.startswith('%%% Local Variables:'): - break - line = (line.rstrip() - .replace('~', ' ') - .replace('&', '&') - .replace('---', '&emdash;') - .replace('\_', '_') - .replace('\{', '{') - .replace('\}', '}') - .replace('\$', '$') - .replace('\%', '%') - .replace('\#', '#') - .replace('<', '<') - .replace('>', '>') - .replace('``', '<quote>') - .replace("''", '</quote>') - .replace('\\', '\\')) - line = re.sub(r'\s*\\(?:centering|small)\b\s*', '', line) - line = re.sub(r'\\(?:hgrc\\|hgrc)\b', - r'<filename role="special"> /.hgrc</filename>', line) - line = re.sub(r'\\item\[(?P<key>[^]]+)\]', r'\item \g<key>:', line) - line = re.sub(r'\\bug{(?P<id>\d+)}', - r'<ulink role="hg-bug" url="http://www.selenic.com/mercurial/bts/issue\g<id>">issue \g<id></ulink>', line) - line = re.sub(r'\\cite{([^}]+)}', r'<citation>\1</citation>', line) - line = re.sub(r'\\hggopt{(?P<opt>[^}]+)}', - r'<option role="hg-opt-global">\g<opt></option>', line) - line = re.sub(r'\\hgxopt{(?P<ext>[^}]+)}{(?P<cmd>[^}]+)}{(?P<opt>[^}]+)}', - r'<option role="hg-ext-\g<ext>-cmd-\g<cmd>-opt">\g<opt></option>', line) - line = re.sub(r'\\hgxcmd{(?P<ext>[^}]+)}{(?P<cmd>[^}]+)}', - r'<command role="hg-ext-\g<ext>">\g<cmd></command>', line) - line = re.sub(r'\\hgext{(?P<ext>[^}]+)}', - r'<literal role="hg-ext">\g<ext></literal>', line) - line = re.sub(r'\\hgopt{(?P<cmd>[^}]+)}{(?P<opt>[^}]+)}', - r'<option role="hg-opt-\g<cmd>">\g<opt></option>', - line) - line = re.sub(r'\\cmdopt{(?P<cmd>[^}]+)}{(?P<opt>[^}]+)}', - r'<option role="cmd-opt-\g<cmd>">\g<opt></option>', - line) - line = re.sub(r'\\hgcmd{(?P<cmd>[^}]+)}', - r'<command role="hg-cmd">hg \g<cmd></command>', line) - line = re.sub(r'\\caption{(?P<text>[^}]+?)}', - r'<caption><para>\g<text></para></caption>', line) - line = re.sub(r'\\grafix{(?P<name>[^}]+)}', - r'<mediaobject><imageobject><imagedata fileref="\g<name>"/></imageobject><textobject><phrase>XXX add text</phrase></textobject></mediaobject>', line) - line = re.sub(r'\\envar{(?P<name>[^}]+)}', - r'<envar>\g<name></envar>', line) - line = re.sub(r'\\rcsection{(?P<sect>[^}]+)}', - r'<literal role="rc-\g<sect>">\g<sect></literal>', line) - line = re.sub(r'\\rcitem{(?P<sect>[^}]+)}{(?P<name>[^}]+)}', - r'<envar role="rc-item-\g<sect>">\g<name></envar>', line) - line = re.sub(r'\\dirname{(?P<dir>[^}]+?)}', - r'<filename class="directory">\g<dir></filename>', line) - line = re.sub(r'\\filename{(?P<file>[^}]+?)}', - r'<filename>\g<file></filename>', line) - line = re.sub(r'\\tildefile{(?P<file>[^}]+)}', - r'<filename role="home">~/\g<file></filename>', line) - line = re.sub(r'\\sfilename{(?P<file>[^}]+)}', - r'<filename role="special">\g<file></filename>', line) - line = re.sub(r'\\sdirname{(?P<dir>[^}]+)}', - r'<filename role="special" class="directory">\g<dir></filename>', line) - line = re.sub(r'\\interaction{(?P<id>[^}]+)}', - r'<!-- &interaction.\g<id>; -->', line) - line = re.sub(r'\\excode{(?P<id>[^}]+)}', - r'<!-- &example.\g<id>; -->', line) - line = re.sub(r'\\pymod{(?P<mod>[^}]+)}', - r'<literal role="py-mod">\g<mod></literal>', line) - line = re.sub(r'\\pymodclass{(?P<mod>[^}]+)}{(?P<class>[^}]+)}', - r'<literal role="py-mod-\g<mod>">\g<class></literal>', line) - line = re.sub(r'\\url{(?P<url>[^}]+)}', - r'<ulink url="\g<url>">\g<url></ulink>', line) - line = re.sub(r'\\href{(?P<url>[^}]+)}{(?P<text>[^}]+)}', - r'<ulink url="\g<url>">\g<text></ulink>', line) - line = re.sub(r'\\command{(?P<cmd>[^}]+)}', - r'<command>\g<cmd></command>', line) - line = re.sub(r'\\option{(?P<opt>[^}]+)}', - r'<option>\g<opt></option>', line) - line = re.sub(r'\\ref{(?P<id>[^}]+)}', r'<xref linkend="\g<id>"/>', line) - line = re.sub(r'\\emph{(?P<txt>[^}]+)}', - r'<emphasis>\g<txt></emphasis>', line) - line = re.sub(r'\\texttt{(?P<txt>[^}]+)}', - r'<literal>\g<txt></literal>', line) - line = re.sub(r'\\textbf{(?P<txt>[^}]+)}', - r'<emphasis role="bold">\g<txt></emphasis>', line) - line = re.sub(r'\\hook{(?P<name>[^}]+)}', - r'<literal role="hook">\g<name></literal>', line) - line = re.sub(r'\\tplfilter{(?P<name>[^}]+)}', - r'<literal role="template-filter">\g<name></literal>', line) - line = re.sub(r'\\tplkword{(?P<name>[^}]+)}', - r'<literal role="template-keyword">\g<name></literal>', line) - line = re.sub(r'\\tplkwfilt{(?P<tpl>[^}]+)}{(?P<name>[^}]+)}', - r'<literal role="template-kw-filt-\g<tpl>">\g<name></literal>', line) - line = re.sub(r'\\[vV]erb(.)(?P<txt>[^\1]+?)\1', - r'<literal>\g<txt></literal>', line) - line = re.sub(r'\\package{(?P<name>[^}]+)}', - r'<literal role="package">\g<name></literal>', line) - line = re.sub(r'\\hgcmdargs{(?P<cmd>[^}]+)}{(?P<args>[^}]+)}', - r'<command role="hg-cmd">hg \g<cmd> \g<args></command>', - line) - line = re.sub(r'\\cmdargs{(?P<cmd>[^}]+)}{(?P<args>[^}]+)}', - r'<command>\g<cmd> \g<args></command>', - line) - m = re.match(r'\\(chapter|section|subsection|subsubsection){(.*)}', line) - if m: - kind, content = m.groups() - sec = sections[kind] - while stack and stack[-1] >= sec: - close = stack.pop() - print >> ofp, '</%s>' % close - stack.append(sec) - print >> ofp, '<%s>\n<title>%s</title>' % (sec, content) - else: - m = re.match(r'\s*\\(begin|end){(?P<sect>[^}]+)}', line) - if m: - if not para: - print >> ofp, '</para>' - if inlist: - ofp.write('</listitem>') - para = True - state, env = m.groups() - env = envs[env] - if state == 'begin': - ofp.write('<') - if env in ('itemizedlist', 'orderedlist'): - inlist = 1 - else: - ofp.write('</') - if env in ('itemizedlist', 'orderedlist'): - inlist = 0 - print >> ofp, env + '>' - else: - if line.startswith('\\item '): - if inlist > 1: - print >> ofp, '</para>' - print >> ofp, '</listitem>' - else: - inlist = 2 - para = True - line = line[6:] - if line and para: - if inlist: - ofp.write('<listitem>') - ofp.write('<para>') - para = False - if not line and not para: - print >> ofp, '</para>' - if inlist: - ofp.write('</listitem>') - para = True - print >> ofp, line - while stack: - print >> ofp, '</%s>' % stack.pop() - ofp.write('\n'.join(['\n<!--', - 'local variables: ', - 'sgml-parent-document: ("00book.xml" "book" "chapter")', - 'end:', - '-->'])) - - -if __name__ == '__main__': - for name in sys.argv[1:]: - if not name.endswith('.tex'): - continue - newname = name[:-3] + 'xml' - ofp = StringIO.StringIO() - process(open(name), ofp) - s = ofp.getvalue() - s = re.sub('\n+</para>', '</para>', s, re.M) - open(newname, 'w').write(s)