manage.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import os
  2. import glob
  3. import zipfile
  4. import urllib
  5. import tempfile
  6. import shutil
  7. def copytree(src, dst):
  8. names = os.listdir(src)
  9. ignored_names = set()
  10. errors = []
  11. if not os.path.exists(dst):
  12. os.makedirs(dst)
  13. for name in names:
  14. srcname = os.path.join(src, name)
  15. dstname = os.path.join(dst, name)
  16. if os.path.isdir(srcname):
  17. copytree(srcname, dstname)
  18. else:
  19. shutil.copy2(srcname, dstname)
  20. class W2PInstance(object):
  21. SOURCES = {'stable':'http://web2py.com/examples/static/web2py_src.zip',
  22. 'nightly':'http://web2py.com/examples/static/nightly/web2py_src.zip',
  23. 'trunk':'https://github.com/web2py/web2py/archive/master.zip'}
  24. def __init__(self,path):
  25. self.path = path
  26. def warn(self,message="system going down soon"):
  27. apps = glob.glob(os.path.join(self.path,'applications','*'))
  28. for app in apps:
  29. if os.path.isdir(app):
  30. open(os.path.join(app,'notifications.txt'),'w').write(message)
  31. def install(self,source='stable'):
  32. if not os.path.exists(self.path):
  33. os.mkdir(self.path)
  34. tmpdir = tempfile.mkdtemp()
  35. link = self.SOURCES[source]
  36. srcfile = os.path.join(tmpdir,'web2py_src.zip')
  37. print 'downloading...'
  38. open(srcfile,'wb').write(urllib.urlopen(link).read())
  39. print 'extracing...'
  40. zipfile.ZipFile(srcfile,'r').extractall(tmpdir)
  41. print 'copying...'
  42. copytree(os.path.join(tmpdir,'web2py'),self.path)
  43. def upgrade(self,source='stable'):
  44. self.install(source)
  45. def upgrade_tmp(self,source,common=False):
  46. tmpdir = tempfile.mkdtemp()
  47. link = self.SOURCES[source]
  48. srcfile = os.path.join(tmpdir,'web2py_src.zip')
  49. print 'copying production...'
  50. copytree(self.path,os.path.join(tmpdir,'web2py'))
  51. tmpdir_web2py = os.path.join(tmpdir,'web2py')
  52. tmp_web2py = W2PInstance(tempdir_web2py)
  53. tmp_web2py.clear_sessions()
  54. tmp_web2py.clear_cache()
  55. tmp_web2py.clear_error()
  56. print 'downloading...'
  57. open(srcfile,'wb').write(urllib.urlopen(link).read())
  58. print 'extracing...'
  59. zipfile.ZipFile(srcfile,'r').extractall(tmpdir)
  60. print 'running tests...'
  61. try:
  62. olddir = os.getcwd()
  63. os.chdir(tempdir_web2py)
  64. ret = os.system("PYTHONPATH=. python -m unittest -v gluon.tests")
  65. # eventually start web2py and run functional tests
  66. finally:
  67. os.chrid(olddir)
  68. if ret:
  69. sys.exit(ret and 1)
  70. copytree(os.path.join(tmpdir,'web2py'),self.path)
  71. def clear_sessions(self):
  72. files = glob.glob(os.path.join(self.path,'applications','*','sessions','*'))
  73. for file in files:
  74. try:
  75. os.unlink(file)
  76. except:
  77. pass
  78. def clear_cache(self):
  79. files = glob.glob(os.path.join(self.path,'applications','*','cache','*'))
  80. for file in files:
  81. try:
  82. os.unlink(file)
  83. except:
  84. pass
  85. def clear_errors(self):
  86. files = glob.glob(os.path.join(self.path,'applications','*','errors','*'))
  87. for file in files:
  88. try:
  89. os.unlink(file)
  90. except:
  91. pass
  92. web2py = W2PInstance('/Users/massimodipierro/Downloads/web2py')
  93. #web2py.install()
  94. web2py.clear_sessions()
  95. """
  96. {{
  97. import os
  98. _notifications = os.path.join(request.folder,'notifications.txt')
  99. if os.path.exixts(_notifications):
  100. response.flash = response.flash or open(_notifications).read()
  101. pass
  102. }}
  103. """