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.

13 jaren geleden
12 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
13 jaren geleden
12 jaren geleden
13 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
12 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. from __future__ import unicode_literals
  24. import os, sys
  25. def replace_code(start, txt1, txt2, extn, search=None, force=False):
  26. """replace all txt1 by txt2 in files with extension (extn)"""
  27. import webnotes.utils
  28. import os, re
  29. esc = webnotes.utils.make_esc('[]')
  30. if not search: search = esc(txt1)
  31. for wt in os.walk(start, followlinks=1):
  32. for fn in wt[2]:
  33. if fn.split('.')[-1]==extn:
  34. fpath = os.path.join(wt[0], fn)
  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, force)
  39. if res == 'skip':
  40. return 'skip'
  41. def search_replace_with_prompt(fpath, txt1, txt2, force=False):
  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. if force:
  53. c = c.replace(txt1, txt2)
  54. else:
  55. while a.lower() not in ['y', 'n', 'skip']:
  56. a = raw_input('Do you want to Change [y/n/skip]?')
  57. if a.lower() == 'y':
  58. c = c.replace(txt1, txt2)
  59. elif a.lower() == 'skip':
  60. return 'skip'
  61. tmp.append(c)
  62. with open(fpath, 'w') as f:
  63. f.write(''.join(tmp))
  64. print colored('Updated', 'green')
  65. def pull(remote, branch, build=False):
  66. os.system('cd lib && git pull %s %s' % (remote, branch))
  67. os.system('cd app && git pull %s %s' % (remote, branch))
  68. if build: rebuild()
  69. def rebuild():
  70. # build js / css
  71. from webnotes.utils import bundlejs
  72. bundlejs.bundle(False)
  73. def apply_latest_patches():
  74. import webnotes.modules.patch_handler
  75. webnotes.modules.patch_handler.run_all()
  76. print '\n'.join(webnotes.modules.patch_handler.log_list)
  77. def sync_all(force=0):
  78. import webnotes.model.sync
  79. webnotes.model.sync.sync_all(force)
  80. def update_erpnext(remote='origin', branch='master'):
  81. pull(remote, branch)
  82. patch_sync_build()
  83. def patch_sync_build():
  84. patch_sync()
  85. rebuild()
  86. def patch_sync():
  87. apply_latest_patches()
  88. import webnotes.modules.patch_handler
  89. for l in webnotes.modules.patch_handler.log_list:
  90. if "failed: STOPPED" in l:
  91. return
  92. sync_all()
  93. clear_cache()
  94. def clear_cache():
  95. import webnotes.sessions
  96. webnotes.sessions.clear_cache()
  97. def append_future_import():
  98. """appends from __future__ import unicode_literals to py files if necessary"""
  99. import os
  100. import conf
  101. conf_path = os.path.abspath(conf.__file__)
  102. if conf_path.endswith("pyc"):
  103. conf_path = conf_path[:-1]
  104. base_path = os.path.dirname(conf_path)
  105. for path, folders, files in os.walk(base_path):
  106. for f in files:
  107. if f.endswith('.py'):
  108. file_path = os.path.join(path, f)
  109. with open(file_path, 'r') as pyfile:
  110. content = pyfile.read()
  111. future_import = 'from __future__ import unicode_literals'
  112. if future_import in content: continue
  113. content = content.split('\n')
  114. idx = -1
  115. for c in content:
  116. idx += 1
  117. if c and not c.startswith('#'):
  118. break
  119. content.insert(idx, future_import)
  120. content = "\n".join(content)
  121. with open(file_path, 'w') as pyfile:
  122. pyfile.write(content)
  123. def setup_options():
  124. from optparse import OptionParser
  125. parser = OptionParser()
  126. parser.add_option("-d", "--db",
  127. dest="db_name",
  128. help="Apply the patches on given db")
  129. parser.add_option("--password",
  130. help="Password for given db", nargs=1)
  131. # build
  132. parser.add_option("-b", "--build", default=False, action="store_true",
  133. help="minify + concat js files")
  134. parser.add_option("-w", "--watch", default=False, action="store_true",
  135. help="watch and minify + concat js files, if necessary")
  136. parser.add_option("--no_compress", default=False, action="store_true",
  137. help="do not compress when building js bundle")
  138. parser.add_option("--no_cms", default=False, action="store_true",
  139. help="do not build wn-web.js and wn-css.js")
  140. parser.add_option("--clear_web", default=False, action="store_true",
  141. help="clear web cache")
  142. parser.add_option("--clear_cache", default=False, action="store_true",
  143. help="clear cache")
  144. parser.add_option("--domain", metavar="DOMAIN",
  145. help="store domain in Website Settings", nargs=1)
  146. # git
  147. parser.add_option("--status", default=False, action="store_true",
  148. help="git status")
  149. parser.add_option("--git", nargs=1, default=False,
  150. metavar = "git options",
  151. help="run git with options in both repos")
  152. parser.add_option("--pull", nargs=2, default=False,
  153. metavar = "remote branch",
  154. help="git pull (both repos)")
  155. parser.add_option("--commit", nargs=1, default=False,
  156. metavar = "commit both repos",
  157. help="git commit -a -m [comment]")
  158. parser.add_option("--push", nargs=2, default=False,
  159. metavar = "remote branch",
  160. help="git push (both repos) [remote] [branch]")
  161. parser.add_option("--checkout", nargs=1, default=False,
  162. metavar = "branch",
  163. help="git checkout [branch]")
  164. parser.add_option("-l", "--latest",
  165. action="store_true", dest="run_latest", default=False,
  166. help="Apply the latest patches")
  167. # patch
  168. parser.add_option("-p", "--patch", nargs=1, dest="patch_list", metavar='patch_module',
  169. action="append",
  170. help="Apply patch")
  171. parser.add_option("-f", "--force",
  172. action="store_true", dest="force", default=False,
  173. help="Force Apply all patches specified using option -p or --patch")
  174. parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname",
  175. help="reload doc")
  176. parser.add_option('--export_doc', nargs=2, metavar = "doctype docname",
  177. help="export doc")
  178. # install
  179. parser.add_option('--install', nargs=2, metavar = "dbname source",
  180. help="install fresh db")
  181. # diff
  182. parser.add_option('--diff_ref_file', nargs=0, \
  183. help="Get missing database records and mismatch properties, with file as reference")
  184. parser.add_option('--diff_ref_db', nargs=0, \
  185. help="Get missing .txt files and mismatch properties, with database as reference")
  186. # scheduler
  187. parser.add_option('--run_scheduler', default=False, action="store_true",
  188. help="Trigger scheduler")
  189. parser.add_option('--run_scheduler_event', nargs=1, metavar="[all|daily|weekly|monthly]",
  190. help="Run scheduler event")
  191. # misc
  192. parser.add_option("--replace", nargs=3, default=False,
  193. metavar = "search replace_by extension",
  194. help="file search-replace")
  195. parser.add_option("--sync_all", help="Synchronize all DocTypes using txt files",
  196. nargs=0)
  197. parser.add_option("--sync", help="Synchronize given DocType using txt file",
  198. nargs=2, metavar="module doctype (use their folder names)")
  199. parser.add_option("--update", help="Pull, run latest patches and sync all",
  200. nargs=2, metavar="ORIGIN BRANCH")
  201. parser.add_option("--patch_sync_build", action="store_true", default=False,
  202. help="run latest patches, sync all and rebuild js css")
  203. parser.add_option("--patch_sync", action="store_true", default=False,
  204. help="run latest patches, sync all")
  205. parser.add_option("--cleanup_data", help="Cleanup test data", default=False,
  206. action="store_true")
  207. parser.add_option("--append_future_import", default=False, action="store_true",
  208. help="append from __future__ import unicode literals to py files")
  209. parser.add_option("--backup", help="Takes backup of database in backup folder",
  210. default=False, action="store_true")
  211. parser.add_option("--test", help="Run test", metavar="MODULE",
  212. nargs=1)
  213. return parser.parse_args()
  214. def run():
  215. sys.path.append('.')
  216. sys.path.append('lib')
  217. sys.path.append('app')
  218. (options, args) = setup_options()
  219. # build
  220. if options.build:
  221. from webnotes.utils import bundlejs
  222. if options.no_cms:
  223. cms_make = False
  224. else:
  225. cms_make = True
  226. bundlejs.bundle(options.no_compress, cms_make)
  227. return
  228. elif options.watch:
  229. from webnotes.utils import bundlejs
  230. bundlejs.watch(True)
  231. return
  232. # code replace
  233. elif options.replace:
  234. print options.replace
  235. replace_code('.', options.replace[0], options.replace[1], options.replace[2], force=options.force)
  236. return
  237. # git
  238. elif options.status:
  239. os.chdir('lib')
  240. os.system('git status')
  241. os.chdir('../app')
  242. os.system('git status')
  243. return
  244. elif options.git:
  245. os.chdir('lib')
  246. os.system('git %s' % options.git)
  247. os.chdir('../app')
  248. os.system('git %s' % options.git)
  249. return
  250. import webnotes
  251. import conf
  252. from webnotes.db import Database
  253. import webnotes.modules.patch_handler
  254. # connect
  255. if options.db_name is not None:
  256. if options.password:
  257. webnotes.connect(options.db_name, options.password)
  258. else:
  259. webnotes.connect(options.db_name)
  260. elif not any([options.install, options.pull]):
  261. webnotes.connect(conf.db_name)
  262. if options.pull:
  263. pull(options.pull[0], options.pull[1], build=True)
  264. elif options.commit:
  265. os.chdir('lib')
  266. os.system('git commit -a -m "%s"' % (options.commit))
  267. os.chdir('../app')
  268. os.system('git commit -a -m "%s"' % (options.commit))
  269. elif options.push:
  270. os.chdir('lib')
  271. os.system('git push %s %s' % (options.push[0], options.push[1]))
  272. os.chdir('../app')
  273. os.system('git push %s %s' % (options.push[0], options.push[1]))
  274. elif options.checkout:
  275. os.chdir('lib')
  276. os.system('git checkout %s' % options.checkout)
  277. os.chdir('../app')
  278. os.system('git checkout %s' % options.checkout)
  279. # patch
  280. elif options.patch_list:
  281. # clear log
  282. webnotes.modules.patch_handler.log_list = []
  283. # run individual patches
  284. for patch in options.patch_list:
  285. webnotes.modules.patch_handler.run_single(\
  286. patchmodule = patch, force = options.force)
  287. print '\n'.join(webnotes.modules.patch_handler.log_list)
  288. # reload
  289. elif options.reload_doc:
  290. webnotes.modules.patch_handler.reload_doc(\
  291. {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
  292. print '\n'.join(webnotes.modules.patch_handler.log_list)
  293. elif options.export_doc:
  294. from webnotes.modules import export_doc
  295. export_doc(options.export_doc[0], options.export_doc[1])
  296. # run all pending
  297. elif options.run_latest:
  298. apply_latest_patches()
  299. elif options.install:
  300. from webnotes.install_lib.install import Installer
  301. inst = Installer('root')
  302. inst.import_from_db(options.install[0], source_path=options.install[1], \
  303. password='admin', verbose = 1)
  304. elif options.diff_ref_file is not None:
  305. import webnotes.modules.diff
  306. webnotes.modules.diff.diff_ref_file()
  307. elif options.diff_ref_db is not None:
  308. import webnotes.modules.diff
  309. webnotes.modules.diff.diff_ref_db()
  310. elif options.run_scheduler:
  311. import webnotes.utils.scheduler
  312. print webnotes.utils.scheduler.execute()
  313. elif options.run_scheduler_event is not None:
  314. import webnotes.utils.scheduler
  315. print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event)
  316. elif options.sync_all is not None:
  317. sync_all(options.force or 0)
  318. elif options.sync is not None:
  319. import webnotes.model.sync
  320. webnotes.model.sync.sync(options.sync[0], options.sync[1], options.force or 0)
  321. elif options.update:
  322. update_erpnext(options.update[0], options.update[1])
  323. elif options.patch_sync_build:
  324. patch_sync_build()
  325. elif options.patch_sync:
  326. patch_sync()
  327. elif options.cleanup_data:
  328. from utilities import cleanup_data
  329. cleanup_data.run()
  330. elif options.domain:
  331. webnotes.conn.set_value('Website Settings', None, 'subdomain', options.domain)
  332. webnotes.conn.commit()
  333. print "Domain set to", options.domain
  334. elif options.clear_web:
  335. # build wn-web.js and wn-web.css
  336. from website.helpers.make_web_include_files import make
  337. make()
  338. import website.utils
  339. website.utils.clear_cache()
  340. elif options.clear_cache:
  341. clear_cache()
  342. elif options.append_future_import:
  343. append_future_import()
  344. elif options.backup:
  345. from webnotes.utils.backups import scheduled_backup
  346. scheduled_backup()
  347. # print messages
  348. if webnotes.message_log:
  349. print '\n'.join(webnotes.message_log)
  350. if options.test is not None:
  351. module_name = options.test
  352. import unittest
  353. del sys.argv[1:]
  354. # is there a better way?
  355. exec ('from %s import *' % module_name) in globals()
  356. unittest.main()
  357. if __name__=='__main__':
  358. run()