You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wnf.py 9.8 KiB

13 years ago
13 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 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 pull(remote, branch):
  63. os.system('git pull %s %s' % (remote, branch))
  64. os.system('cd lib && git pull %s %s' % (remote, branch))
  65. def apply_latest_patches():
  66. import webnotes.modules.patch_handler
  67. webnotes.modules.patch_handler.run_all()
  68. print '\n'.join(webnotes.modules.patch_handler.log_list)
  69. def sync_all(force=0):
  70. import webnotes.model.sync
  71. webnotes.model.sync.sync_all(force)
  72. def update_erpnext(remote='origin', branch='master'):
  73. # do a pull
  74. pull(remote, branch)
  75. # apply latest patches
  76. apply_latest_patches()
  77. import webnotes.modules.patch_handler
  78. for l in webnotes.modules.patch_handler.log_list:
  79. if "failed: STOPPED" in l:
  80. return
  81. # sync all
  82. sync_all()
  83. def setup_options():
  84. from optparse import OptionParser
  85. parser = OptionParser()
  86. parser.add_option("-d", "--db",
  87. dest="db_name",
  88. help="Apply the patches on given db")
  89. parser.add_option("--password",
  90. help="Password for given db", nargs=1)
  91. # build
  92. parser.add_option("-b", "--build", default=False, action="store_true",
  93. help="minify + concat js files")
  94. parser.add_option("--build_web_cache", default=False, action="store_true",
  95. help="build web cache")
  96. parser.add_option("--domain", metavar="DOMAIN",
  97. help="store domain in Website Settings", nargs=1)
  98. # git
  99. parser.add_option("--status", default=False, action="store_true",
  100. help="git status")
  101. parser.add_option("--pull", nargs=2, default=False,
  102. metavar = "remote branch",
  103. help="git pull (both repos)")
  104. parser.add_option("--push", nargs=3, default=False,
  105. metavar = "remote branch comment",
  106. help="git commit + push (both repos) [remote] [branch] [comment]")
  107. parser.add_option("--checkout", nargs=1, default=False,
  108. metavar = "branch",
  109. help="git checkout [branch]")
  110. parser.add_option("-l", "--latest",
  111. action="store_true", dest="run_latest", default=False,
  112. help="Apply the latest patches")
  113. # patch
  114. parser.add_option("-p", "--patch", nargs=1, dest="patch_list", metavar='patch_module',
  115. action="append",
  116. help="Apply patch")
  117. parser.add_option("-f", "--force",
  118. action="store_true", dest="force", default=False,
  119. help="Force Apply all patches specified using option -p or --patch")
  120. parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname",
  121. help="reload doc")
  122. parser.add_option('--export_doc', nargs=2, metavar = "doctype docname",
  123. help="export doc")
  124. # install
  125. parser.add_option('--install', nargs=2, metavar = "dbname source",
  126. help="install fresh db")
  127. # diff
  128. parser.add_option('--diff_ref_file', nargs=0, \
  129. help="Get missing database records and mismatch properties, with file as reference")
  130. parser.add_option('--diff_ref_db', nargs=0, \
  131. help="Get missing .txt files and mismatch properties, with database as reference")
  132. # scheduler
  133. parser.add_option('--run_scheduler', default=False, action="store_true",
  134. help="Trigger scheduler")
  135. parser.add_option('--run_scheduler_event', nargs=1, metavar="[all|daily|weekly|monthly]",
  136. help="Run scheduler event")
  137. # misc
  138. parser.add_option("--replace", nargs=3, default=False,
  139. metavar = "search replace_by extension",
  140. help="file search-replace")
  141. parser.add_option("--sync_all", help="Synchronize all DocTypes using txt files",
  142. nargs=0)
  143. parser.add_option("--sync", help="Synchronize given DocType using txt file",
  144. nargs=2, metavar="module doctype (use their folder names)")
  145. parser.add_option("--update", help="Pull, run latest patches and sync all",
  146. nargs=2, metavar="ORIGIN BRANCH")
  147. parser.add_option("--cleanup_data", help="Cleanup test data", default=False,
  148. action="store_true")
  149. parser.add_option("--backup", help="Takes backup of database in backup folder",
  150. default=False, action="store_true")
  151. return parser.parse_args()
  152. def run():
  153. sys.path.append('.')
  154. sys.path.append('lib/py')
  155. import webnotes
  156. import conf
  157. sys.path.append(conf.modules_path)
  158. (options, args) = setup_options()
  159. from webnotes.db import Database
  160. import webnotes.modules.patch_handler
  161. # connect
  162. if options.db_name is not None:
  163. if options.password:
  164. webnotes.connect(options.db_name, options.password)
  165. else:
  166. webnotes.connect(options.db_name)
  167. elif not any([options.install, options.pull]):
  168. webnotes.connect(conf.db_name)
  169. # build
  170. if options.build:
  171. import build.project
  172. build.project.build()
  173. # code replace
  174. elif options.replace:
  175. print options.replace
  176. replace_code('.', options.replace[0], options.replace[1], options.replace[2])
  177. # git
  178. elif options.status:
  179. os.system('git status')
  180. os.chdir('lib')
  181. os.system('git status')
  182. elif options.pull:
  183. pull(options.pull[0], options.pull[1])
  184. elif options.push:
  185. os.system('git commit -a -m "%s"' % options.push[2])
  186. os.system('git push %s %s' % (options.push[0], options.push[1]))
  187. os.chdir('lib')
  188. os.system('git commit -a -m "%s"' % options.push[2])
  189. os.system('git push %s %s' % (options.push[0], options.push[1]))
  190. elif options.checkout:
  191. os.system('git checkout %s' % options.checkout)
  192. os.chdir('lib')
  193. os.system('git checkout %s' % options.checkout)
  194. # patch
  195. elif options.patch_list:
  196. # clear log
  197. webnotes.modules.patch_handler.log_list = []
  198. # run individual patches
  199. for patch in options.patch_list:
  200. webnotes.modules.patch_handler.run_single(\
  201. patchmodule = patch, force = options.force)
  202. print '\n'.join(webnotes.modules.patch_handler.log_list)
  203. # reload
  204. elif options.reload_doc:
  205. webnotes.modules.patch_handler.reload_doc(\
  206. {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
  207. print '\n'.join(webnotes.modules.patch_handler.log_list)
  208. elif options.export_doc:
  209. from webnotes.modules import export_doc
  210. export_doc(options.export_doc[0], options.export_doc[1])
  211. # run all pending
  212. elif options.run_latest:
  213. apply_latest_patches()
  214. elif options.install:
  215. from webnotes.install_lib.install import Installer
  216. inst = Installer('root')
  217. inst.import_from_db(options.install[0], source_path=options.install[1], \
  218. password='admin', verbose = 1)
  219. elif options.diff_ref_file is not None:
  220. import webnotes.modules.diff
  221. webnotes.modules.diff.diff_ref_file()
  222. elif options.diff_ref_db is not None:
  223. import webnotes.modules.diff
  224. webnotes.modules.diff.diff_ref_db()
  225. elif options.run_scheduler:
  226. import webnotes.utils.scheduler
  227. print webnotes.utils.scheduler.execute()
  228. elif options.run_scheduler_event is not None:
  229. import webnotes.utils.scheduler
  230. print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event)
  231. elif options.sync_all is not None:
  232. sync_all(options.force or 0)
  233. elif options.sync is not None:
  234. import webnotes.model.sync
  235. webnotes.model.sync.sync(options.sync[0], options.sync[1], options.force or 0)
  236. elif options.update:
  237. update_erpnext(options.update[0], options.update[1])
  238. elif options.cleanup_data:
  239. from utilities import cleanup_data
  240. cleanup_data.run()
  241. elif options.domain:
  242. webnotes.conn.set_value('Website Settings', None, 'subdomain', options.domain)
  243. webnotes.conn.commit()
  244. print "Domain set to", options.domain
  245. elif options.build_web_cache:
  246. import website.web_cache
  247. website.web_cache.refresh_cache(True)
  248. elif options.backup:
  249. from webnotes.utils.backups import scheduled_backup
  250. scheduled_backup()
  251. # print messages
  252. if webnotes.message_log:
  253. print '\n'.join(webnotes.message_log)
  254. if __name__=='__main__':
  255. run()