Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 13 roky
před 13 roky
před 13 roky
před 13 roky
před 12 roky
před 13 roky
před 13 roky
před 12 roky
před 13 roky
před 13 roky
před 13 roky
před 13 roky
před 13 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. if fpath != '/var/www/erpnext/erpnext/patches/jan_mar_2012/rename_dt.py': # temporary
  36. with open(fpath, 'r') as f:
  37. content = f.read()
  38. if re.search(search, content):
  39. res = search_replace_with_prompt(fpath, txt1, txt2, force)
  40. if res == 'skip':
  41. return 'skip'
  42. def search_replace_with_prompt(fpath, txt1, txt2, force=False):
  43. """ Search and replace all txt1 by txt2 in the file with confirmation"""
  44. from termcolor import colored
  45. with open(fpath, 'r') as f:
  46. content = f.readlines()
  47. tmp = []
  48. for c in content:
  49. if c.find(txt1) != -1:
  50. print fpath
  51. print colored(txt1, 'red').join(c[:-1].split(txt1))
  52. a = ''
  53. if force:
  54. c = c.replace(txt1, txt2)
  55. else:
  56. while a.lower() not in ['y', 'n', 'skip']:
  57. a = raw_input('Do you want to Change [y/n/skip]?')
  58. if a.lower() == 'y':
  59. c = c.replace(txt1, txt2)
  60. elif a.lower() == 'skip':
  61. return 'skip'
  62. tmp.append(c)
  63. with open(fpath, 'w') as f:
  64. f.write(''.join(tmp))
  65. print colored('Updated', 'green')
  66. def pull(remote, branch, build=False):
  67. os.system('cd lib && git pull %s %s' % (remote, branch))
  68. os.system('cd app && git pull %s %s' % (remote, branch))
  69. if build: rebuild()
  70. def rebuild():
  71. # build js / css
  72. from webnotes.utils import bundlejs
  73. bundlejs.bundle(False)
  74. # build cache
  75. import website.web_cache
  76. website.web_cache.refresh_cache(['Blog'])
  77. def apply_latest_patches():
  78. import webnotes.modules.patch_handler
  79. webnotes.modules.patch_handler.run_all()
  80. print '\n'.join(webnotes.modules.patch_handler.log_list)
  81. def sync_all(force=0):
  82. import webnotes.model.sync
  83. webnotes.model.sync.sync_all(force)
  84. def update_erpnext(remote='origin', branch='master'):
  85. pull(remote, branch)
  86. patch_sync_build()
  87. def patch_sync_build():
  88. patch_sync()
  89. rebuild()
  90. def patch_sync():
  91. apply_latest_patches()
  92. import webnotes.modules.patch_handler
  93. for l in webnotes.modules.patch_handler.log_list:
  94. if "failed: STOPPED" in l:
  95. return
  96. sync_all()
  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("--build_web_cache", default=False, action="store_true",
  141. help="build web cache")
  142. parser.add_option("--domain", metavar="DOMAIN",
  143. help="store domain in Website Settings", nargs=1)
  144. # git
  145. parser.add_option("--status", default=False, action="store_true",
  146. help="git status")
  147. parser.add_option("--git", nargs=1, default=False,
  148. metavar = "git options",
  149. help="run git with options in both repos")
  150. parser.add_option("--pull", nargs=2, default=False,
  151. metavar = "remote branch",
  152. help="git pull (both repos)")
  153. parser.add_option("--push", nargs=3, default=False,
  154. metavar = "remote branch comment",
  155. help="git commit + push (both repos) [remote] [branch] [comment]")
  156. parser.add_option("--checkout", nargs=1, default=False,
  157. metavar = "branch",
  158. help="git checkout [branch]")
  159. parser.add_option("-l", "--latest",
  160. action="store_true", dest="run_latest", default=False,
  161. help="Apply the latest patches")
  162. # patch
  163. parser.add_option("-p", "--patch", nargs=1, dest="patch_list", metavar='patch_module',
  164. action="append",
  165. help="Apply patch")
  166. parser.add_option("-f", "--force",
  167. action="store_true", dest="force", default=False,
  168. help="Force Apply all patches specified using option -p or --patch")
  169. parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname",
  170. help="reload doc")
  171. parser.add_option('--export_doc', nargs=2, metavar = "doctype docname",
  172. help="export doc")
  173. # install
  174. parser.add_option('--install', nargs=2, metavar = "dbname source",
  175. help="install fresh db")
  176. # diff
  177. parser.add_option('--diff_ref_file', nargs=0, \
  178. help="Get missing database records and mismatch properties, with file as reference")
  179. parser.add_option('--diff_ref_db', nargs=0, \
  180. help="Get missing .txt files and mismatch properties, with database as reference")
  181. # scheduler
  182. parser.add_option('--run_scheduler', default=False, action="store_true",
  183. help="Trigger scheduler")
  184. parser.add_option('--run_scheduler_event', nargs=1, metavar="[all|daily|weekly|monthly]",
  185. help="Run scheduler event")
  186. # misc
  187. parser.add_option("--replace", nargs=3, default=False,
  188. metavar = "search replace_by extension",
  189. help="file search-replace")
  190. parser.add_option("--sync_all", help="Synchronize all DocTypes using txt files",
  191. nargs=0)
  192. parser.add_option("--sync", help="Synchronize given DocType using txt file",
  193. nargs=2, metavar="module doctype (use their folder names)")
  194. parser.add_option("--update", help="Pull, run latest patches and sync all",
  195. nargs=2, metavar="ORIGIN BRANCH")
  196. parser.add_option("--patch_sync_build", action="store_true", default=False,
  197. help="run latest patches, sync all and rebuild js css")
  198. parser.add_option("--patch_sync", action="store_true", default=False,
  199. help="run latest patches, sync all")
  200. parser.add_option("--cleanup_data", help="Cleanup test data", default=False,
  201. action="store_true")
  202. parser.add_option("--append_future_import", default=False, action="store_true",
  203. help="append from __future__ import unicode literals to py files")
  204. parser.add_option("--backup", help="Takes backup of database in backup folder",
  205. default=False, action="store_true")
  206. parser.add_option("--test", help="Run test", metavar="MODULE",
  207. nargs=1)
  208. return parser.parse_args()
  209. def run():
  210. sys.path.append('.')
  211. sys.path.append('lib')
  212. sys.path.append('app')
  213. (options, args) = setup_options()
  214. # build
  215. if options.build:
  216. from webnotes.utils import bundlejs
  217. if options.no_cms:
  218. cms_make = False
  219. else:
  220. cms_make = True
  221. bundlejs.bundle(options.no_compress, cms_make)
  222. return
  223. elif options.watch:
  224. from webnotes.utils import bundlejs
  225. bundlejs.watch(True)
  226. return
  227. # code replace
  228. elif options.replace:
  229. print options.replace
  230. replace_code('.', options.replace[0], options.replace[1], options.replace[2], force=options.force)
  231. return
  232. # git
  233. elif options.status:
  234. os.chdir('lib')
  235. os.system('git status')
  236. os.chdir('../app')
  237. os.system('git status')
  238. return
  239. elif options.git:
  240. os.chdir('lib')
  241. os.system('git %s' % options.git)
  242. os.chdir('../app')
  243. os.system('git %s' % options.git)
  244. return
  245. import webnotes
  246. import conf
  247. from webnotes.db import Database
  248. import webnotes.modules.patch_handler
  249. # connect
  250. if options.db_name is not None:
  251. if options.password:
  252. webnotes.connect(options.db_name, options.password)
  253. else:
  254. webnotes.connect(options.db_name)
  255. elif not any([options.install, options.pull]):
  256. webnotes.connect(conf.db_name)
  257. if options.pull:
  258. pull(options.pull[0], options.pull[1], build=True)
  259. elif options.push:
  260. os.chdir('lib')
  261. os.system('git commit -a -m "%s"' % options.push[2])
  262. os.system('git push %s %s' % (options.push[0], options.push[1]))
  263. os.chdir('../app')
  264. os.system('git commit -a -m "%s"' % options.push[2])
  265. os.system('git push %s %s' % (options.push[0], options.push[1]))
  266. elif options.checkout:
  267. os.chdir('lib')
  268. os.system('git checkout %s' % options.checkout)
  269. os.chdir('../app')
  270. os.system('git checkout %s' % options.checkout)
  271. # patch
  272. elif options.patch_list:
  273. # clear log
  274. webnotes.modules.patch_handler.log_list = []
  275. # run individual patches
  276. for patch in options.patch_list:
  277. webnotes.modules.patch_handler.run_single(\
  278. patchmodule = patch, force = options.force)
  279. print '\n'.join(webnotes.modules.patch_handler.log_list)
  280. # reload
  281. elif options.reload_doc:
  282. webnotes.modules.patch_handler.reload_doc(\
  283. {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
  284. print '\n'.join(webnotes.modules.patch_handler.log_list)
  285. elif options.export_doc:
  286. from webnotes.modules import export_doc
  287. export_doc(options.export_doc[0], options.export_doc[1])
  288. # run all pending
  289. elif options.run_latest:
  290. apply_latest_patches()
  291. elif options.install:
  292. from webnotes.install_lib.install import Installer
  293. inst = Installer('root')
  294. inst.import_from_db(options.install[0], source_path=options.install[1], \
  295. password='admin', verbose = 1)
  296. elif options.diff_ref_file is not None:
  297. import webnotes.modules.diff
  298. webnotes.modules.diff.diff_ref_file()
  299. elif options.diff_ref_db is not None:
  300. import webnotes.modules.diff
  301. webnotes.modules.diff.diff_ref_db()
  302. elif options.run_scheduler:
  303. import webnotes.utils.scheduler
  304. print webnotes.utils.scheduler.execute()
  305. elif options.run_scheduler_event is not None:
  306. import webnotes.utils.scheduler
  307. print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event)
  308. elif options.sync_all is not None:
  309. sync_all(options.force or 0)
  310. elif options.sync is not None:
  311. import webnotes.model.sync
  312. webnotes.model.sync.sync(options.sync[0], options.sync[1], options.force or 0)
  313. elif options.update:
  314. update_erpnext(options.update[0], options.update[1])
  315. elif options.patch_sync_build:
  316. patch_sync_build()
  317. elif options.patch_sync:
  318. patch_sync()
  319. elif options.cleanup_data:
  320. from utilities import cleanup_data
  321. cleanup_data.run()
  322. elif options.domain:
  323. webnotes.conn.set_value('Website Settings', None, 'subdomain', options.domain)
  324. webnotes.conn.commit()
  325. print "Domain set to", options.domain
  326. elif options.build_web_cache:
  327. # build wn-web.js and wn-web.css
  328. import webnotes.cms.make
  329. webnotes.cms.make.make()
  330. import website.web_cache
  331. website.web_cache.refresh_cache(['Blog'])
  332. elif options.append_future_import:
  333. append_future_import()
  334. elif options.backup:
  335. from webnotes.utils.backups import scheduled_backup
  336. scheduled_backup()
  337. # print messages
  338. if webnotes.message_log:
  339. print '\n'.join(webnotes.message_log)
  340. if options.test is not None:
  341. module_name = options.test
  342. import unittest
  343. del sys.argv[1:]
  344. # is there a better way?
  345. exec ('from %s import *' % module_name) in globals()
  346. unittest.main()
  347. if __name__=='__main__':
  348. run()