Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

273 Zeilen
8.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 setup_options():
  63. from optparse import OptionParser
  64. parser = OptionParser()
  65. parser.add_option("-d", "--db",
  66. dest="db_name",
  67. help="Apply the patches on given db")
  68. # build
  69. parser.add_option("-b", "--build", default=False, action="store_true",
  70. help="minify + concat js files")
  71. parser.add_option("-c", "--clear", default=False, action="store_true",
  72. help="reset version")
  73. # git
  74. parser.add_option("--status", default=False, action="store_true",
  75. help="git status")
  76. parser.add_option("--pull", nargs=2, default=False,
  77. metavar = "remote branch",
  78. help="git pull (both repos)")
  79. parser.add_option("--push", nargs=3, default=False,
  80. metavar = "remote branch comment",
  81. help="git commit + push (both repos) [remote] [branch] [comment]")
  82. parser.add_option("--checkout", nargs=1, default=False,
  83. metavar = "branch",
  84. help="git checkout [branch]")
  85. parser.add_option("-l", "--latest",
  86. action="store_true", dest="run_latest", default=False,
  87. help="Apply the latest patches")
  88. # patch
  89. parser.add_option("-p", "--patch", nargs=1, dest="patch_list", metavar='patch_module',
  90. action="append",
  91. help="Apply patch")
  92. parser.add_option("-f", "--force",
  93. action="store_true", dest="force", default=False,
  94. help="Force Apply all patches specified using option -p or --patch")
  95. parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname",
  96. help="reload doc")
  97. parser.add_option('--export_doc', nargs=2, metavar = "doctype docname",
  98. help="export doc")
  99. # install
  100. parser.add_option('--install', nargs=3, metavar = "rootpassword dbname source",
  101. help="install fresh db")
  102. # diff
  103. parser.add_option('--diff_ref_file', nargs=0, \
  104. help="Get missing database records and mismatch properties, with file as reference")
  105. parser.add_option('--diff_ref_db', nargs=0, \
  106. help="Get missing .txt files and mismatch properties, with database as reference")
  107. # scheduler
  108. parser.add_option('--run_scheduler', default=False, action="store_true",
  109. help="Trigger scheduler")
  110. parser.add_option('--run_scheduler_event', nargs=1, metavar="[all|daily|weekly|monthly]",
  111. help="Run scheduler event")
  112. # misc
  113. parser.add_option("--replace", nargs=3, default=False,
  114. metavar = "search replace_by extension",
  115. help="file search-replace")
  116. parser.add_option("--sync_all", help="Synchronize all DocTypes using txt files",
  117. nargs=0)
  118. parser.add_option("--sync", help="Synchronize given DocType using txt file",
  119. nargs=2, metavar="module doctype (use their folder names)")
  120. return parser.parse_args()
  121. def run():
  122. sys.path.append('.')
  123. sys.path.append('lib/py')
  124. import webnotes
  125. import conf
  126. sys.path.append(conf.modules_path)
  127. (options, args) = setup_options()
  128. from webnotes.db import Database
  129. import webnotes.modules.patch_handler
  130. # connect
  131. if options.db_name is not None:
  132. webnotes.connect(options.db_name)
  133. elif not options.install:
  134. webnotes.connect(conf.db_name)
  135. # build
  136. if options.build:
  137. import build.project
  138. build.project.build()
  139. elif options.clear:
  140. from build.project import update_version
  141. print "Version:" + str(update_version())
  142. import webnotes.utils.cache
  143. webnotes.conn.begin()
  144. webnotes.utils.cache.clear()
  145. webnotes.conn.commit()
  146. # code replace
  147. elif options.replace:
  148. replace_code('.', options.replace[0], options.replace[1], options.replace[2])
  149. # git
  150. elif options.status:
  151. os.system('git status')
  152. os.chdir('lib')
  153. os.system('git status')
  154. elif options.pull:
  155. from build.project import update_version
  156. os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
  157. os.chdir('lib')
  158. os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
  159. # update js code version (clear to localStorage)
  160. update_version()
  161. elif options.push:
  162. os.system('git commit -a -m "%s"' % options.push[2])
  163. os.system('git push %s %s' % (options.push[0], options.push[1]))
  164. os.chdir('lib')
  165. os.system('git commit -a -m "%s"' % options.push[2])
  166. os.system('git push %s %s' % (options.push[0], options.push[1]))
  167. elif options.checkout:
  168. os.system('git checkout %s' % options.checkout)
  169. os.chdir('lib')
  170. os.system('git checkout %s' % options.checkout)
  171. # patch
  172. elif options.patch_list:
  173. # clear log
  174. webnotes.modules.patch_handler.log_list = []
  175. # run individual patches
  176. for patch in options.patch_list:
  177. webnotes.modules.patch_handler.run_single(\
  178. patchmodule = patch, force = options.force)
  179. print '\n'.join(webnotes.modules.patch_handler.log_list)
  180. # reload
  181. elif options.reload_doc:
  182. webnotes.modules.patch_handler.reload_doc(\
  183. {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
  184. print '\n'.join(webnotes.modules.patch_handler.log_list)
  185. elif options.export_doc:
  186. from webnotes.modules import export_doc
  187. export_doc(options.export_doc[0], options.export_doc[1])
  188. # run all pending
  189. elif options.run_latest:
  190. webnotes.modules.patch_handler.run_all()
  191. print '\n'.join(webnotes.modules.patch_handler.log_list)
  192. elif options.install:
  193. from webnotes.install_lib.install import Installer
  194. inst = Installer('root', options.install[0])
  195. inst.import_from_db(options.install[1], source_path=options.install[2], \
  196. password='admin', verbose = 1)
  197. elif options.diff_ref_file is not None:
  198. import webnotes.modules.diff
  199. webnotes.modules.diff.diff_ref_file()
  200. elif options.diff_ref_db is not None:
  201. import webnotes.modules.diff
  202. webnotes.modules.diff.diff_ref_db()
  203. elif options.run_scheduler:
  204. import webnotes.utils.scheduler
  205. print webnotes.utils.scheduler.execute()
  206. elif options.run_scheduler_event is not None:
  207. import webnotes.utils.scheduler
  208. print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event)
  209. elif options.sync_all is not None:
  210. import webnotes.model.sync
  211. webnotes.model.sync.sync_all(options.force or 0)
  212. elif options.sync is not None:
  213. import webnotes.model.sync
  214. webnotes.model.sync.sync(options.sync[0], options.sync[1], options.force or 0)
  215. # print messages
  216. if webnotes.message_log:
  217. print '\n'.join(webnotes.message_log)
  218. if __name__=='__main__':
  219. run()