dever.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. dever-manage tools
  5. name:dever.py
  6. author:rabin
  7. """
  8. from core import *
  9. import json
  10. class Dever(object):
  11. git = 'http://git.dever.cc:3000/'
  12. ssh = 'ssh://git@git.dever.cc:10022/'
  13. lib = Core.path + 'container/share/lib/php/'
  14. dev = Core.path + 'container/web/'
  15. framework = 'dever/framework.git'
  16. demo = 'dever/demo.git'
  17. package = 'dever-package/'
  18. product = 'dever-product/'
  19. package_list = []
  20. @classmethod
  21. def init(self):
  22. method = Core.getMethod(Dever_Action, Args.action)
  23. method()
  24. @classmethod
  25. def rely(self, action, path):
  26. package = path + 'package.json'
  27. if Args.param:
  28. Dever_Create.package(path, Args.param)
  29. Args.param = ''
  30. if File.exists(package):
  31. data = File.read(package, '')
  32. data = json.loads(data)
  33. if 'rely' in data:
  34. rely = data['rely'].split(',')
  35. for v in rely:
  36. package = action.package(v)
  37. self.package_list.append({'path':package, 'name':v})
  38. @classmethod
  39. def create(self, path):
  40. name = os.path.basename(path)
  41. Dever_Create.boot(path + '/', name)
  42. print "create finished!"
  43. @staticmethod
  44. def cur(up = True):
  45. name = Args.name
  46. path = File.cur()
  47. if name:
  48. git = ''
  49. if '/' in name:
  50. git = name
  51. name = os.path.basename(git).replace('.git', '')
  52. path = path + '/' + name
  53. if git and up:
  54. Dever_Create.git(git, path)
  55. return path
  56. class Dever_Create(object):
  57. @staticmethod
  58. def package_boot(lib):
  59. file = lib + 'boot.php'
  60. if not File.exists(file):
  61. File.write(file, "<?php \r\nif (!defined('DEVER_PROJECT')) {\r\ndefine('DEVER_PROJECT', 'default');\r\ndefine('DEVER_PROJECT_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);\r\n}\r\ninclude(dirname(__FILE__) . DIRECTORY_SEPARATOR . '../dever/boot.php');")
  62. @classmethod
  63. def boot(self, path, name):
  64. file = path + 'boot.php'
  65. if not File.exists(file):
  66. File.write(file, "<?php \r\ndefine('DEVER_PROJECT', '"+name+"');\r\ndefine('DEVER_PROJECT_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);\r\nif (defined('DEVER_PACKAGE')) {\r\n include('dever_package/'.DEVER_PACKAGE.'/index.php');\r\n} else {\r\n include('dever/boot.php');\r\n}")
  67. self.dm(path)
  68. self.gitignore(path)
  69. self.data(path)
  70. self.config(path, name)
  71. @classmethod
  72. def index(self, path, name, package):
  73. path = path + name + '/'
  74. if File.exists(package + 'index.php') and not File.exists(path):
  75. File.mkdir(path)
  76. file = path + 'index.php'
  77. if not File.exists(file):
  78. File.write(file, "<?php \r\ndefine('DEVER_PACKAGE', '"+name+"');\r\ndefine('DEVER_APP_SETUP', dirname(__FILE__) . DIRECTORY_SEPARATOR);\r\ninclude(dirname(__FILE__) . DIRECTORY_SEPARATOR . '../boot.php');")
  79. if File.exists(package + 'daemon'):
  80. path = path + 'daemon/'
  81. if not File.exists(path):
  82. Core.popen('cp -R ' + package + 'daemon/ ' + path)
  83. @staticmethod
  84. def gitignore(path):
  85. file = path + '.gitignore'
  86. if not File.exists(file):
  87. File.write(file, "dm\r\n.DS_Store\r\n*.pyc\r\ndata/upload/*\r\ndata/database/*\r\ndata/project/*\r\ndata/logs/*\r\n\r\ndata/signature/*\r\ndata/avatar/*")
  88. @staticmethod
  89. def dm(path, check = False):
  90. file = path + 'dm'
  91. state = File.exists(file)
  92. if check:
  93. return state
  94. if not state:
  95. File.write(file, "dm created")
  96. @staticmethod
  97. def package(path, rely):
  98. file = path + 'package.json'
  99. if not File.exists(file):
  100. File.write(file, '{\r\n"rely": "'+rely+'"\r\n}')
  101. @staticmethod
  102. def git(addr, path):
  103. file = path + '/' + '.git/'
  104. if not File.exists(file):
  105. Git.update(addr, path)
  106. else:
  107. Git.set(addr, path)
  108. @staticmethod
  109. def data(path):
  110. path = path + 'data/'
  111. if not File.exists(path):
  112. File.mkdir(path)
  113. Core.popen('chmod -R 777 ' + path)
  114. file = path + 'readme'
  115. if not File.exists(file):
  116. File.write(file, "dever create")
  117. @staticmethod
  118. def config(path, name):
  119. path = path + 'config/'
  120. if not File.exists(path):
  121. File.mkdir(path)
  122. file = path + 'base.php'
  123. if not File.exists(file):
  124. File.write(file, "<?php\r\n$config['base'] = array\r\n(\r\n'name' => '"+name+"',\r\n'version' => '1.0.0 Beta'\r\n);\r\nreturn $config;")
  125. file = path + 'route.php'
  126. if not File.exists(file):
  127. File.write(file, "<?php\r\nreturn array\r\n(\r\n'home' => 'home',\r\n);")
  128. path = path + 'env/localhost/'
  129. if not File.mkdirs(path):
  130. File.mkdir(path)
  131. file = path + 'default.php'
  132. if not File.exists(file):
  133. File.write(file, "<?php\r\n$config['database'] = array\r\n(\r\n'default' => array\r\n(\r\n'type' => 'pdo',\r\n'host' => array\r\n(\r\n'read' => 'web-mysql:3306',\r\n'update' => 'web-mysql:3306',\r\n),\r\n'database' => '"+name+"',\r\n'username' => 'root',\r\n'password' => '123456',\r\n'charset' => 'utf8',\r\n),\r\n);\r\nreturn $config;")
  134. class Dever_Action(object):
  135. @staticmethod
  136. def init():
  137. Git.update(Dever.git + Dever.framework, Dever.lib + 'dever')
  138. @classmethod
  139. def package(self, name = False):
  140. if not name:
  141. name = Args.name
  142. return self.update(Dever.git + Dever.package + name, name)
  143. @classmethod
  144. def put(self):
  145. Env.dever(Args.name)
  146. @classmethod
  147. def get(self):
  148. git = Args.param
  149. if not git:
  150. git = Env.dever()
  151. if not git or 'http' not in git:
  152. git = Dever.git + Dever.package
  153. return self.update(git + Args.name, Args.name)
  154. @classmethod
  155. def update(self, store, name = False):
  156. if not name:
  157. name = Args.name
  158. lib = Dever.lib + 'dever_package/'
  159. path = lib + name + '/'
  160. Git.update(store, path)
  161. Dever.rely(self, path)
  162. Dever_Create.package_boot(lib)
  163. project = Dever.cur(False) + '/'
  164. if Dever_Create.dm(project, True):
  165. Dever_Create.index(project, name, path)
  166. return path
  167. @classmethod
  168. def all(self):
  169. path = Dever.lib + 'dever_package/'
  170. files = File.getFiles(path)
  171. if files:
  172. for i in files:
  173. if '.' not in i:
  174. self.update(False, i)
  175. @classmethod
  176. def pull(self):
  177. path = Dever.cur()
  178. Dever.rely(self, path + '/')
  179. Git.update('', path + '/')
  180. return path
  181. @classmethod
  182. def push(self):
  183. path = File.cur()
  184. Git.push(path + '/', Args.name, Args.param)
  185. @classmethod
  186. def push_package(self):
  187. path = Dever.lib + 'dever_package/' + Args.name
  188. Git.push(path + '/', False, Args.param)
  189. @classmethod
  190. def dev(self):
  191. path = File.cur()
  192. Git.push(path + '/', Args.name, Args.param)
  193. @classmethod
  194. def product(self):
  195. path = Dever.dev + Args.name
  196. Git.update(Dever.git + Dever.product + Args.name, path)
  197. Dever.rely(self, path)
  198. @staticmethod
  199. def demo():
  200. Git.update(Dever.git + Dever.demo, Dever.dev + 'demo')
  201. @classmethod
  202. def create(self):
  203. path = self.pull()
  204. print "creating..."
  205. for v in Dever.package_list:
  206. Dever_Create.index(path + '/', v['name'], v['path'])
  207. Dever.create(path)