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.
 
 
 
 
 
 

315 rivejä
9.6 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 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=3, metavar = "rootpassword 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. return parser.parse_args()
  150. def run():
  151. sys.path.append('.')
  152. sys.path.append('lib/py')
  153. import webnotes
  154. import conf
  155. sys.path.append(conf.modules_path)
  156. (options, args) = setup_options()
  157. from webnotes.db import Database
  158. import webnotes.modules.patch_handler
  159. # connect
  160. if options.db_name is not None:
  161. if options.password:
  162. webnotes.connect(options.db_name, options.password)
  163. else:
  164. webnotes.connect(options.db_name)
  165. elif not any([options.install, options.pull]):
  166. webnotes.connect(conf.db_name)
  167. # build
  168. if options.build:
  169. import build.project
  170. build.project.build()
  171. # code replace
  172. elif options.replace:
  173. print options.replace
  174. replace_code('.', options.replace[0], options.replace[1], options.replace[2])
  175. # git
  176. elif options.status:
  177. os.system('git status')
  178. os.chdir('lib')
  179. os.system('git status')
  180. elif options.pull:
  181. pull(options.pull[0], options.pull[1])
  182. elif options.push:
  183. os.system('git commit -a -m "%s"' % options.push[2])
  184. os.system('git push %s %s' % (options.push[0], options.push[1]))
  185. os.chdir('lib')
  186. os.system('git commit -a -m "%s"' % options.push[2])
  187. os.system('git push %s %s' % (options.push[0], options.push[1]))
  188. elif options.checkout:
  189. os.system('git checkout %s' % options.checkout)
  190. os.chdir('lib')
  191. os.system('git checkout %s' % options.checkout)
  192. # patch
  193. elif options.patch_list:
  194. # clear log
  195. webnotes.modules.patch_handler.log_list = []
  196. # run individual patches
  197. for patch in options.patch_list:
  198. webnotes.modules.patch_handler.run_single(\
  199. patchmodule = patch, force = options.force)
  200. print '\n'.join(webnotes.modules.patch_handler.log_list)
  201. # reload
  202. elif options.reload_doc:
  203. webnotes.modules.patch_handler.reload_doc(\
  204. {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
  205. print '\n'.join(webnotes.modules.patch_handler.log_list)
  206. elif options.export_doc:
  207. from webnotes.modules import export_doc
  208. export_doc(options.export_doc[0], options.export_doc[1])
  209. # run all pending
  210. elif options.run_latest:
  211. apply_latest_patches()
  212. elif options.install:
  213. from webnotes.install_lib.install import Installer
  214. inst = Installer('root', options.install[0])
  215. inst.import_from_db(options.install[1], source_path=options.install[2], \
  216. password='admin', verbose = 1)
  217. elif options.diff_ref_file is not None:
  218. import webnotes.modules.diff
  219. webnotes.modules.diff.diff_ref_file()
  220. elif options.diff_ref_db is not None:
  221. import webnotes.modules.diff
  222. webnotes.modules.diff.diff_ref_db()
  223. elif options.run_scheduler:
  224. import webnotes.utils.scheduler
  225. print webnotes.utils.scheduler.execute()
  226. elif options.run_scheduler_event is not None:
  227. import webnotes.utils.scheduler
  228. print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event)
  229. elif options.sync_all is not None:
  230. sync_all(options.force or 0)
  231. elif options.sync is not None:
  232. import webnotes.model.sync
  233. webnotes.model.sync.sync(options.sync[0], options.sync[1], options.force or 0)
  234. elif options.update:
  235. update_erpnext(options.update[0], options.update[1])
  236. elif options.cleanup_data:
  237. from utilities import cleanup_data
  238. cleanup_data.run()
  239. elif options.domain:
  240. webnotes.conn.set_value('Website Settings', None, 'subdomain', options.domain)
  241. webnotes.conn.commit()
  242. print "Domain set to", options.domain
  243. elif options.build_web_cache:
  244. import website.web_cache
  245. website.web_cache.refresh_cache(True)
  246. # print messages
  247. if webnotes.message_log:
  248. print '\n'.join(webnotes.message_log)
  249. if __name__=='__main__':
  250. run()