/fabfile.py

https://github.com/yyuu/fabfile-kyototycoon
Python | 119 lines | 97 code | 12 blank | 10 comment | 6 complexity | 01e5db8aa709055964bc26d262b50bfd MD5 | raw file
  1. #!/usr/bin/env python
  2. from __future__ import with_statement
  3. from fabric.api import *
  4. from fabric.decorators import *
  5. from fabric.contrib import project
  6. import glob
  7. import logging
  8. import os
  9. import re
  10. import sys
  11. class LazyAttributeDictionary(dict):
  12. def __getattr__(self, key):
  13. if self.has_key(key):
  14. return self.__getitem__(key)
  15. else:
  16. raise AttributeError(key)
  17. def __setattr__(self, key, value):
  18. self.__setitem__(key, value)
  19. def __getitem__(self, key):
  20. val = super(LazyAttributeDictionary, self).__getitem__(key)
  21. if hasattr(val, '__call__'):
  22. val = val()
  23. self.__setitem__(key, val)
  24. return val
  25. if env.user is None:
  26. env.user = os.getenv('USER')
  27. opt = LazyAttributeDictionary(env)
  28. opt.prefix = (lambda: os.path.join('/u/apps/kyototycoon', re.findall('(?:[0-9]+\.)*[0-9]+', opt.archive)[0]))
  29. opt.target = os.path.realpath('./target')
  30. ## kyotocabinet
  31. opt.kc_archive = (lambda: glob.glob('kyotocabinet-*.tar.gz')[-1])
  32. opt.kc_extracted = (lambda: os.path.splitext(os.path.splitext(opt.kc_archive)[0])[0])
  33. opt.kc_patches = (lambda: opt.kc_extracted + '.patches')
  34. ## kyototycon
  35. opt.archive = (lambda: glob.glob('kyototycoon-*.tar.gz')[-1])
  36. opt.extracted = (lambda: os.path.splitext(os.path.splitext(opt.archive)[0])[0])
  37. opt.patches = (lambda: opt.extracted + '.patches')
  38. @task
  39. def setup():
  40. clean()
  41. build()
  42. upload()
  43. symlink()
  44. @task
  45. @runs_once
  46. def clean():
  47. local("""
  48. rm -rf %(kc_extracted)s %(extracted)s %(target)s
  49. """ % opt)
  50. @task
  51. @runs_once
  52. def build():
  53. build_kyotocabinet()
  54. build_kyototycoon()
  55. @task
  56. def build_kyotocabinet():
  57. local("""
  58. test -f %(kc_archive)s && ( rm -rf %(kc_extracted)s; tar zxf %(kc_archive)s )
  59. """ % opt)
  60. with lcd(opt.kc_extracted):
  61. ## patch
  62. local("""
  63. if test -d %(kc_patches)s; then
  64. umask 022; env QUILT_PATCHES=%(kc_patches)s quilt push -a
  65. fi
  66. """ % opt)
  67. ## configure
  68. local("""
  69. umask 022; ./configure --prefix=%(prefix)s
  70. """ % opt)
  71. ## build/test/install
  72. local("""
  73. umask 022; make -j4 && make -j4 check && make -j4 DESTDIR=%(target)s install
  74. """ % opt)
  75. @task
  76. def build_kyototycoon():
  77. local("""
  78. test -f %(archive)s && ( rm -rf %(extracted)s; tar zxf %(archive)s )
  79. """ % opt)
  80. with lcd(opt.extracted):
  81. ## patch
  82. local("""
  83. if test -d %(patches)s; then
  84. umask 022; env QUILT_PATCHES=%(patches)s quilt push -a
  85. fi
  86. """ % opt)
  87. ## configure
  88. local("""
  89. umask 022; ./configure --prefix=%(prefix)s --with-kc=%(kc)s
  90. """ % dict(prefix=opt.prefix, kc=os.path.realpath(opt.target + os.path.sep + opt.prefix)))
  91. ## build/test/install
  92. local("""
  93. umask 022; make -j4 && make -j4 check && make -j4 DESTDIR=%(target)s install
  94. """ % opt)
  95. @task
  96. def upload():
  97. run("""
  98. test -d %(prefix)s || mkdir -p %(prefix)s
  99. """ % opt)
  100. local_dir = os.path.realpath(opt.target + os.path.sep + opt.prefix) + os.path.sep
  101. project.rsync_project(local_dir=local_dir, remote_dir=opt.prefix, delete=True, extra_opts='--chmod=Du+rwx,Dgo+rx,Fu+rw,Fgo+r --links')
  102. @task
  103. def symlink():
  104. run("""
  105. current=`dirname %(prefix)s`/current; rm -f $current; ln -s %(prefix)s $current
  106. """ % opt)
  107. # vim:set ft=python :