Skip to content

Instantly share code, notes, and snippets.

@whitmo
Created April 8, 2010 21:44
Show Gist options
  • Select an option

  • Save whitmo/360584 to your computer and use it in GitHub Desktop.

Select an option

Save whitmo/360584 to your computer and use it in GitHub Desktop.
this_dir = os.path.dirname(__file__) or "."
options(
download_cache=path('~/.python-cache').expanduser(),
env=path(os.environ.get('VIRTUAL_ENV', '.')),
port=8081,
local_ini=path('build_state.ini'),
build_ini=path('build.ini'),
minilib=Bunch(
extra_files=[]
),
sphinx=Bunch(
docroot='docs',
builddir="_build",
sourcedir="."
),
virtualenv=Bunch(
# move these to bu
packages_to_install=['urlgrabber', 'tempita', 'setuptools-git'],
install_paver=True,
script_name='bootstrap.py',
paver_command_line="after_bootstrap",
dest_dir=this_dir
)
)
@task
def auto(options):
"""
Loads up configuration and puts `emma` on the path
"""
try:
import emma
except ImportError:
import sys
sys.path.append(path(__file__).parent.abspath())
del sys
from emma.utils import SafeConfigDict
options.config = SafeConfigDict(options.local_ini.abspath(),
options.build_ini.abspath(),
env=options.env,
port=options.port)
@task
@needs("install_sphinx")
def html(options):
__doc__ = oldhtml.__doc__
call_task("paver.doctools.html")
@task
@needs('build_modwsgi', 'build_php', 'general')
def build_server(options):
"""Build and configure the primary server"""
pass
@task
def setupapp(options):
pass
@task
def install_sphinx(options):
"""
Install sphinx if not available
"""
try:
import sphinx
except ImportError:
sh('pip install sphinx')
info("Sphinx installed. Please run your command again")
sys.exit()
@task
@cmdopts([("overwrite", 'O', 'overwrite')])
def general(options):
"""Some general options. A dependency of other tasks"""
pass
@task
@needs('urlgrabber', 'src_dir', 'general')
def build_apache(options):
"""
Install and configure a server from the config
@@ use prep_build and mark_installed
"""
pkg = 'apache'
src_dir = prep_build(pkg, options)
if src_dir is 0:
return src_dir
cmmi(options, pkg, src_dir)
mark_installed(options, pkg, src_dir)
def cmmi(options, section, src_dir, patch_makefile=None,
stockopts="standard"):
"""
cmmi::
c.onfigure
m.ake
m.ake i.nstall
Reads config values, does cmmi
"""
if stockopts == "standard":
stockopts = dict(LDFLAGS="-L%s" %(options.env / 'include'),
CPPFLAGS="-I%s" %(options.env / 'lib'))
with pushd(src_dir):
opt_list = options.config[(section, 'options')].split()
opts = ' '.join(opt.strip() for opt in opt_list if opt and not opt.startswith('#'))
stockopts = " ".join("%s=%s" %(key, val) for key, val in stockopts.items())
configure = './configure %s %s' %(stockopts.strip(), opts.strip())
sh(configure)
if patch_makefile is not None:
patch_makefile(src_dir)
sh('make')
sh('make install')
def download(options, url):
from urlgrabber.grabber import urlgrab
from urlgrabber.progress import text_progress_meter
filename = options.src_dir / url.split('/')[-1]
if not filename.exists():
filename = urlgrab(url, filename, progress_obj=text_progress_meter())
return filename
@task
def clean_modwsgi(options):
mods = options.env / path('etc') / path('httpd') / 'modules'
path(mods / 'mod_wsgi.so').remove()
location = options.config[('installed', 'modwsgi')]
with pushd(location):
sh('make distclean')
def strep(str_, term, sub):
return str_.replace(term, sub)
def add_resolve(str_):
return "%s -lresolv" %str_
@task
@needs('urlgrabber', 'src_dir', 'general', 'build_libxml', 'build_apache', 'build_pg')
def build_php(options):
"""
Builds php for the sandboxed apache
"""
pkg = 'php'
src_dir = prep_build(pkg, options)
if src_dir is 0:
return src_dir
spec = dict(EXTRA_LIBS=add_resolve,
MH_BUNDLE_FLAGS=add_resolve,
EXTRA_LDFLAGS=add_resolve,
EXTRA_LDFLAGS_PROGRAM=add_resolve)
patcher = partial(mkfile_patcher, specs=spec.items())
cmmi(options, pkg, src_dir, stockopts=dict(), patch_makefile=patcher)
phpmod = src_dir / path('libs/libphp5.so')
phpmod.copy(options.env / path('etc/httpd/modules'))
mark_installed(options, pkg, src_dir)
@task
@needs('urlgrabber', 'src_dir', 'general')
def build_iconv(options):
"""
Installs iconv w/ a prefix
"""
pkg = 'iconv'
src_dir = prep_build(pkg, options)
if src_dir is 0:
return src_dir
cmmi(options, pkg, src_dir)
mark_installed(options, pkg, src_dir)
def prep_build(pkg, options):
from emma import utils
check = path(options.env) / path(options.config[pkg, 'check'])
if check.exists() and not getattr(options.general, 'overwrite', False):
info("%s is installed" %pkg)
return 0
url = options.config.get((pkg, 'url'))
filename = download(options, url)
with pushd(options.src_dir):
src_dir = path(utils.unpack_file(filename))
return src_dir
@task
@needs('urlgrabber', 'src_dir', 'general')
def build_pg(options):
"""
Installs postgres
"""
pkg = 'postgres'
src_dir = prep_build(pkg, options)
if src_dir is 0:
return src_dir
cmmi(options, pkg, src_dir)
mark_installed(options, pkg, src_dir)
@task
def build_xml(options):
"""
Builds libxml and libxslt
"""
call_task('build_libxslt')
call_task('build_libxml')
def replace_line(lines, sig, newtext):
# put in utils
ln, line = [(i, line) for i, line in enumerate(lines) if line.startswith(sig)][0]
info("Replacing line:%s %s -->> %s w/" %(i, line, newtext))
line = line.split('=')
if callable(newtext):
line[1] = newtext(line[1])
else:
line[1] = newtext
line = '='.join(line)
info(line)
lines[ln] = line
return lines
def mkfile_patcher(src_dir, specs):
mf = src_dir / 'MakeFile'
txtlist = mf.text().split('\n')
for sig, text in specs:
txtlist = replace_line(txtlist, sig, text)
mf.write_text('\n'.join(txtlist))
@task
@needs('urlgrabber', 'src_dir', 'general', 'build_iconv')
def build_libxml(options):
"""
Builds libxml
"""
pkg = 'libxml'
src_dir = prep_build(pkg, options)
if src_dir is 0:
return src_dir
cmmi(options, pkg, src_dir)
mark_installed(options, pkg, src_dir)
@task
@needs('urlgrabber', 'src_dir', 'general', 'build_libxml')
def build_libxslt(options):
"""
Builds libxslt
"""
pkg = 'libxslt'
src_dir = prep_build(pkg, options)
if src_dir is 0:
return src_dir
cmmi(options, pkg, src_dir)
mark_installed(options, pkg, src_dir)
def mark_installed(options, pkg, src_dir):
options.config[('installed', pkg)] = src_dir.abspath()
options.config.save()
@task
@needs('build_apache', 'urlgrabber', 'src_dir', 'general')
def build_modwsgi(options):
"""
Builds modwsgi for sandboxed apache
@ parameterize python
"""
pkg = 'modwsgi'
src_dir = prep_build(pkg, options)
if src_dir is 0:
return src_dir
patcher = lambda x: x
if sys.platform == 'darwin':
#@@ parameterize python version
LDFLAGS = sh("python2.6-config --ldflags", capture=True)
def patcher(src_dir):
mf = src_dir / 'MakeFile'
txtlist = mf.text().split('\n')
ln, line = [(i, line) for i, line in enumerate(txtlist) if line.startswith("LDFLAGS")][0]
info("%s %s" %(i, line))
line = line.split('=')
line[-1] = LDFLAGS
line = ' = '.join(line)
txtlist[ln] = line
mf.write_text('\n'.join(txtlist))
cmmi(options, pkg, src_dir, patcher)
options.config[('installed', pkg)] = src_dir.abspath()
options.config.save()
@task
@cmdopts([("make_clean", 'C', 'run make clean')])
def clean_apache(options):
sh("rm -rf error cgi-bin conf icons manual modules share")
if getattr(options.clean_apache, 'make_clean', False):
srcdir = options.config[('installed', 'apache')]
with pushd(srcdir):
sh('make clean')
sh("rm bin/apachectl")
del options.config[('installed', 'apache')]
options.config.save()
@task
def src_dir(options):
src = options.env / 'src'
if not src.exists():
src.mkdir()
options.src_dir = src
try:
import paver.doctools
import paver.virtual
import paver.misctasks
ALL_TASKS_LOADED = True
except ImportError, e:
info("some tasks could not not be imported.")
debug(str(e))
ALL_TASKS_LOADED = False
vwrapper_files = """
postactivate
postdeactivate
postmkvirtualenv
postrmvirtualenv
preactivate
predeactivate
premkvirtualenv
prermvirtualenv
"""
@task
def after_bootstrap(options):
"""
Only meant to be run after the initial bootstrap. Run by hand at
your own risk.
Installs requirements and copies any
"""
if path('EmmaApp.egg-info').exists():
path('EmmaApp.egg-info').rmtree()
info('Installing app pkg')
try:
pip_install('-e ./')
except :
# diaper for egginfo mess
pass
info('Installing requirements')
pip_install('-r requirements.txt')
woh = path(os.environ.get('WORKON_HOME'))
for vfile in (x for x in vwrapper_files.split('\n') if x):
if path(woh / vfile).exists():
contents = path(woh / vfile).text()
newfile = path(options.env / 'bin') / vfile
newfile.write_text(contents)
postactivate = options.env / path('bin/postactivate')
lines = ['export DYLD_LIBRARY_PATH=%s/lib' %options.env.abspath()]
postactivate.write_lines(lines, append=True)
@task
def urlgrabber(options):
"""
Installs urlgrabber
"""
try:
import urlgrabber
except ImportError:
pip_install('urlgrabber')
def pip_install(spec):
dlc = options.download_cache
sh("%s/bin/pip install --download-cache=%s %s" %(options.virtualenv.dest_dir, dlc, spec))
if ALL_TASKS_LOADED:
@task
@needs('generate_setup', 'minilib', 'setuptools.command.sdist')
def sdist(options):
"""Overrides sdist to make sure that our setup.py is generated."""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment