Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

311 rindas
9.5 KiB

  1. #!/usr/bin/python
  2. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  3. #
  4. # MIT License (MIT)
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a
  7. # copy of this software and associated documentation files (the "Software"),
  8. # to deal in the Software without restriction, including without limitation
  9. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. # and/or sell copies of the Software, and to permit persons to whom the
  11. # Software is furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  17. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  18. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  20. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  21. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. #
  23. import os, sys
  24. def replace_code(start, txt1, txt2, extn, search=None):
  25. """replace all txt1 by txt2 in files with extension (extn)"""
  26. import webnotes.utils
  27. import os, re
  28. esc = webnotes.utils.make_esc('[]')
  29. if not search: search = esc(txt1)
  30. for wt in os.walk(start, followlinks=1):
  31. for fn in wt[2]:
  32. if fn.split('.')[-1]==extn:
  33. fpath = os.path.join(wt[0], fn)
  34. if fpath != '/var/www/erpnext/erpnext/patches/jan_mar_2012/rename_dt.py': # temporary
  35. with open(fpath, 'r') as f:
  36. content = f.read()
  37. if re.search(search, content):
  38. res = search_replace_with_prompt(fpath, txt1, txt2)
  39. if res == 'skip':
  40. return 'skip'
  41. def search_replace_with_prompt(fpath, txt1, txt2):
  42. """ Search and replace all txt1 by txt2 in the file with confirmation"""
  43. from termcolor import colored
  44. with open(fpath, 'r') as f:
  45. content = f.readlines()
  46. tmp = []
  47. for c in content:
  48. if c.find(txt1) != -1:
  49. print '\n', fpath
  50. print colored(txt1, 'red').join(c[:-1].split(txt1))
  51. a = ''
  52. while a.lower() not in ['y', 'n', 'skip']:
  53. a = raw_input('Do you want to Change [y/n/skip]?')
  54. if a.lower() == 'y':
  55. c = c.replace(txt1, txt2)
  56. elif a.lower() == 'skip':
  57. return 'skip'
  58. tmp.append(c)
  59. with open(fpath, 'w') as f:
  60. f.write(''.join(tmp))
  61. print colored('Updated', 'green')
  62. def create_cms_files():
  63. from webnotes.model.code import get_obj
  64. os.system('rm public/login-page.html')
  65. # rewrite pages
  66. ws = get_obj('Website Settings')
  67. ws.rewrite_pages()
  68. ss = get_obj('Style Settings')
  69. ss.validate()
  70. ss.doc.save()
  71. ss.on_update()
  72. # create login-page.html if it doesnt exist by copying index.html
  73. if not os.path.exists('public/login-page.html') and os.path.exists('public/index.html'):
  74. os.system('cp public/index.html public/login-page.html')
  75. # change owner of files
  76. os.system('chown -R apache:apache *')
  77. def pull(remote, branch):
  78. os.system('git pull %s %s' % (remote, branch))
  79. os.system('cd lib && git pull %s %s' % (remote, branch))
  80. def apply_latest_patches():
  81. import webnotes.modules.patch_handler
  82. webnotes.modules.patch_handler.run_all()
  83. print '\n'.join(webnotes.modules.patch_handler.log_list)
  84. def sync_all(force=0):
  85. import webnotes.model.sync
  86. webnotes.model.sync.sync_all(force)
  87. def update_erpnext(remote='origin', branch='master'):
  88. # do a pull
  89. pull(remote, branch)
  90. # apply latest patches
  91. apply_latest_patches()
  92. # sync all
  93. sync_all()
  94. def setup_options():
  95. from optparse import OptionParser
  96. parser = OptionParser()
  97. parser.add_option("-d", "--db",
  98. dest="db_name",
  99. help="Apply the patches on given db")
  100. parser.add_option("--password",
  101. help="Password for given db", nargs=1)
  102. # build
  103. parser.add_option("-b", "--build", default=False, action="store_true",
  104. help="minify + concat js files")
  105. parser.add_option("--cms", default=False, action="store_true",
  106. help="take a dump of website pages, js and css")
  107. # git
  108. parser.add_option("--status", default=False, action="store_true",
  109. help="git status")
  110. parser.add_option("--pull", nargs=2, default=False,
  111. metavar = "remote branch",
  112. help="git pull (both repos)")
  113. parser.add_option("--push", nargs=3, default=False,
  114. metavar = "remote branch comment",
  115. help="git commit + push (both repos) [remote] [branch] [comment]")
  116. parser.add_option("--checkout", nargs=1, default=False,
  117. metavar = "branch",
  118. help="git checkout [branch]")
  119. parser.add_option("-l", "--latest",
  120. action="store_true", dest="run_latest", default=False,
  121. help="Apply the latest patches")
  122. # patch
  123. parser.add_option("-p", "--patch", nargs=1, dest="patch_list", metavar='patch_module',
  124. action="append",
  125. help="Apply patch")
  126. parser.add_option("-f", "--force",
  127. action="store_true", dest="force", default=False,
  128. help="Force Apply all patches specified using option -p or --patch")
  129. parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname",
  130. help="reload doc")
  131. parser.add_option('--export_doc', nargs=2, metavar = "doctype docname",
  132. help="export doc")
  133. # install
  134. parser.add_option('--install', nargs=3, metavar = "rootpassword dbname source",
  135. help="install fresh db")
  136. # diff
  137. parser.add_option('--diff_ref_file', nargs=0, \
  138. help="Get missing database records and mismatch properties, with file as reference")
  139. parser.add_option('--diff_ref_db', nargs=0, \
  140. help="Get missing .txt files and mismatch properties, with database as reference")
  141. # scheduler
  142. parser.add_option('--run_scheduler', default=False, action="store_true",
  143. help="Trigger scheduler")
  144. parser.add_option('--run_scheduler_event', nargs=1, metavar="[all|daily|weekly|monthly]",
  145. help="Run scheduler event")
  146. # misc
  147. parser.add_option("--replace", nargs=3, default=False,
  148. metavar = "search replace_by extension",
  149. help="file search-replace")
  150. parser.add_option("--sync_all", help="Synchronize all DocTypes using txt files",
  151. nargs=0)
  152. parser.add_option("--sync", help="Synchronize given DocType using txt file",
  153. nargs=2, metavar="module doctype (use their folder names)")
  154. parser.add_option("--update", help="Pull, run latest patches and sync all",
  155. nargs=2, metavar="ORIGIN BRANCH")
  156. return parser.parse_args()
  157. def run():
  158. sys.path.append('.')
  159. sys.path.append('lib/py')
  160. import webnotes
  161. import conf
  162. sys.path.append(conf.modules_path)
  163. (options, args) = setup_options()
  164. from webnotes.db import Database
  165. import webnotes.modules.patch_handler
  166. # connect
  167. if options.db_name is not None:
  168. if options.password:
  169. webnotes.connect(options.db_name, options.password)
  170. else:
  171. webnotes.connect(options.db_name)
  172. elif not any([options.install, options.pull]):
  173. webnotes.connect(conf.db_name)
  174. # build
  175. if options.build:
  176. import build.project
  177. build.project.build()
  178. # code replace
  179. elif options.replace:
  180. replace_code('.', options.replace[0], options.replace[1], options.replace[2])
  181. # git
  182. elif options.status:
  183. os.system('git status')
  184. os.chdir('lib')
  185. os.system('git status')
  186. elif options.pull:
  187. pull(options.pull[0], options.pull[1])
  188. elif options.push:
  189. os.system('git commit -a -m "%s"' % options.push[2])
  190. os.system('git push %s %s' % (options.push[0], options.push[1]))
  191. os.chdir('lib')
  192. os.system('git commit -a -m "%s"' % options.push[2])
  193. os.system('git push %s %s' % (options.push[0], options.push[1]))
  194. elif options.checkout:
  195. os.system('git checkout %s' % options.checkout)
  196. os.chdir('lib')
  197. os.system('git checkout %s' % options.checkout)
  198. # patch
  199. elif options.patch_list:
  200. # clear log
  201. webnotes.modules.patch_handler.log_list = []
  202. # run individual patches
  203. for patch in options.patch_list:
  204. webnotes.modules.patch_handler.run_single(\
  205. patchmodule = patch, force = options.force)
  206. print '\n'.join(webnotes.modules.patch_handler.log_list)
  207. # reload
  208. elif options.reload_doc:
  209. webnotes.modules.patch_handler.reload_doc(\
  210. {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
  211. print '\n'.join(webnotes.modules.patch_handler.log_list)
  212. elif options.export_doc:
  213. from webnotes.modules import export_doc
  214. export_doc(options.export_doc[0], options.export_doc[1])
  215. # run all pending
  216. elif options.run_latest:
  217. apply_latest_patches()
  218. elif options.install:
  219. from webnotes.install_lib.install import Installer
  220. inst = Installer('root', options.install[0])
  221. inst.import_from_db(options.install[1], source_path=options.install[2], \
  222. password='admin', verbose = 1)
  223. elif options.diff_ref_file is not None:
  224. import webnotes.modules.diff
  225. webnotes.modules.diff.diff_ref_file()
  226. elif options.diff_ref_db is not None:
  227. import webnotes.modules.diff
  228. webnotes.modules.diff.diff_ref_db()
  229. elif options.run_scheduler:
  230. import webnotes.utils.scheduler
  231. print webnotes.utils.scheduler.execute()
  232. elif options.run_scheduler_event is not None:
  233. import webnotes.utils.scheduler
  234. print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event)
  235. elif options.sync_all is not None:
  236. sync_all(options.force or 0)
  237. elif options.sync is not None:
  238. import webnotes.model.sync
  239. webnotes.model.sync.sync(options.sync[0], options.sync[1], options.force or 0)
  240. elif options.update:
  241. update_erpnext(options.update[0], options.update[1])
  242. if options.cms: create_cms_files()
  243. elif options.cms:
  244. create_cms_files()
  245. # print messages
  246. if webnotes.message_log:
  247. print '\n'.join(webnotes.message_log)
  248. if __name__=='__main__':
  249. run()