PageRenderTime 29ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/silverlining/commands/init.py

https://bitbucket.org/ianb/silverlining/
Python | 163 lines | 156 code | 3 blank | 4 comment | 11 complexity | 321b29f0c69c98c61cc21140fd835a85 MD5 | raw file
Possible License(s): GPL-2.0
  1. """Set up a new silver/virtualenv environment"""
  2. import os
  3. import sys
  4. import virtualenv
  5. from silversupport.shell import run
  6. def command_init(config):
  7. dir = config.args.dir
  8. app_name = os.path.basename(os.path.abspath(dir))
  9. vars = dict(
  10. app_name=app_name,
  11. main=config.args.main,
  12. config=config.args.config)
  13. vars['force'] = config.args.force
  14. if sys.version[:3] == '2.6':
  15. virtualenv.logger = config.logger
  16. virtualenv.create_environment(
  17. dir,
  18. # This should be true to pick up global binaries like psycopg:
  19. site_packages=True,
  20. unzip_setuptools=True,
  21. use_distribute=config.args.distribute)
  22. else:
  23. # This is the crude way we need to do this when we're not in Python 2.6
  24. config.logger.warn('Creating virtualenv environment in subprocess')
  25. virtualenv_file = virtualenv.__file__
  26. if virtualenv_file.endswith('.pyc'):
  27. virtualenv_file = virtualenv_file[:-1]
  28. if config.args.distribute:
  29. cmd = [
  30. '/usr/bin/python2.6', virtualenv_file,
  31. '--unzip-setuptools', '--distribute',
  32. dir]
  33. else:
  34. cmd = [
  35. '/usr/bin/python2.6', virtualenv_file,
  36. '--unzip-setuptools', dir]
  37. run(cmd)
  38. noforce_vars = vars.copy()
  39. noforce_vars['force'] = False
  40. init_copy('README.txt', os.path.join(dir, 'README.txt'), config.logger, noforce_vars)
  41. init_copy('app.ini', os.path.join(dir, 'app.ini'), config.logger, noforce_vars)
  42. if config.args.config:
  43. init_copy('config.ini', os.path.join(dir, 'config.ini'), config.logger, vars)
  44. if config.args.main:
  45. init_copy('main.py', os.path.join(dir, 'main.py'), config.logger, vars)
  46. src = os.path.join(dir, 'src')
  47. if not os.path.exists(src):
  48. os.mkdir(src)
  49. static = os.path.join(dir, 'static')
  50. if not os.path.exists(static):
  51. os.mkdir(static)
  52. lib_python = os.path.join(dir, 'lib', 'python')
  53. if not os.path.exists(lib_python):
  54. os.makedirs(lib_python)
  55. #XXX this is a hack around http://bitbucket.org/ianb/silverlining/issue/1/bug-on-running-silver-init-for-the-second
  56. distutils_dir = os.path.join(dir, 'lib', 'python2.6', 'distutils')
  57. if os.path.islink(distutils_dir):
  58. distutils_init = os.path.join(dir, 'lib', 'python2.6', 'distutils', '__init__.py')
  59. _distutils_init_content = open(distutils_init).read()
  60. os.unlink(distutils_dir)
  61. os.makedirs(distutils_dir)
  62. init_copystring(
  63. _distutils_init_content,
  64. distutils_init,
  65. config.logger, vars, append=True)
  66. init_copy(
  67. 'sitecustomize.py',
  68. os.path.join(dir, 'lib', 'python2.6', 'sitecustomize.py'),
  69. config.logger, vars)
  70. init_copystring(
  71. _distutils_cfg,
  72. os.path.join(dir, 'lib', 'python2.6', 'distutils', 'distutils.cfg'),
  73. config.logger, vars, append=True)
  74. init_copystring(
  75. _distutils_init,
  76. os.path.join(dir, 'lib', 'python2.6', 'distutils', '__init__.py'),
  77. config.logger, vars, append=True)
  78. init_copystring(
  79. _activate_this,
  80. os.path.join(dir, 'bin', 'activate_this.py'),
  81. config.logger, vars, append=True)
  82. _distutils_cfg = """\
  83. # This is what makes things install into lib/python instead of lib/python2.6:
  84. [install]
  85. home = <sys.prefix>
  86. """
  87. _distutils_init = """\
  88. # Patch by silverlining:
  89. old_parse_config_files = dist.Distribution.parse_config_files
  90. def parse_config_files(self, filenames=None):
  91. old_parse_config_files(self, filenames)
  92. opt_dict = self.get_option_dict('install')
  93. if 'home' in opt_dict:
  94. location, value = opt_dict['home']
  95. if value.lower().strip() == 'default':
  96. del opt_dict['home']
  97. else:
  98. opt_dict['home'] = (location, value.replace('<sys.prefix>', sys.prefix))
  99. dist.Distribution.parse_config_files = parse_config_files
  100. """
  101. _activate_this = """\
  102. # Added by silverlining:
  103. # This is some extra code to make activate_this.py run sitecustomize:
  104. sitecustomize = os.path.abspath(os.path.join(__file__, '../../lib/python%s/sitecustomize.py' % sys.version[:3]))
  105. if os.path.exists(sitecustomize):
  106. execfile(sitecustomize, dict(__file__=sitecustomize, __name__='sitecustomize'))
  107. """
  108. def init_copy(source, dest, logger, vars, append=False):
  109. import tempita
  110. source = os.path.join(
  111. os.path.dirname(os.path.dirname(__file__)),
  112. 'init-files',
  113. source)
  114. if os.path.exists(source+'.tmpl'):
  115. source = source+'.tmpl'
  116. template = tempita.Template.from_filename(source)
  117. source_content = template.substitute(vars)
  118. else:
  119. fp = open(source, 'rb')
  120. source_content = fp.read()
  121. fp.close()
  122. init_copystring(source_content, dest, logger, vars,
  123. append=append)
  124. def init_copystring(source_content, dest, logger, vars,
  125. append=False):
  126. if os.path.exists(dest):
  127. fp = open(dest, 'rb')
  128. content = fp.read()
  129. fp.close()
  130. if append:
  131. if content in source_content:
  132. logger.info(
  133. 'Not adding to %s (already has content)' % dest)
  134. return
  135. else:
  136. if content == source_content:
  137. logger.info(
  138. 'Not overwriting %s (same content)' % dest)
  139. return
  140. elif vars['force']:
  141. logger.notify(
  142. 'Overwriting %s' % dest)
  143. else:
  144. logger.notify(
  145. 'Not overwriting %s (content differs)' % dest)
  146. return
  147. if append:
  148. fp = open(dest, 'ab')
  149. else:
  150. fp = open(dest, 'wb')
  151. fp.write(source_content)
  152. fp.close()