您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

13 年前
13 年前
12 年前
12 年前
12 年前
12 年前
12 年前
12 年前
12 年前
12 年前
12 年前
12 年前
12 年前
12 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. #!/usr/bin/env python
  2. # Copyright 2013 Web Notes Technologies Pvt Ltd
  3. # License: MIT. See license.txt
  4. from __future__ import unicode_literals
  5. import os, sys
  6. def replace_code(start, txt1, txt2, extn, search=None, force=False):
  7. """replace all txt1 by txt2 in files with extension (extn)"""
  8. import webnotes.utils
  9. import os, re
  10. esc = webnotes.utils.make_esc('[]')
  11. if not search: search = esc(txt1)
  12. for wt in os.walk(start, followlinks=1):
  13. for fn in wt[2]:
  14. if fn.split('.')[-1]==extn:
  15. fpath = os.path.join(wt[0], fn)
  16. with open(fpath, 'r') as f:
  17. content = f.read()
  18. if re.search(search, content):
  19. res = search_replace_with_prompt(fpath, txt1, txt2, force)
  20. if res == 'skip':
  21. return 'skip'
  22. def search_replace_with_prompt(fpath, txt1, txt2, force=False):
  23. """ Search and replace all txt1 by txt2 in the file with confirmation"""
  24. from termcolor import colored
  25. with open(fpath, 'r') as f:
  26. content = f.readlines()
  27. tmp = []
  28. for c in content:
  29. if c.find(txt1) != -1:
  30. print fpath
  31. print colored(txt1, 'red').join(c[:-1].split(txt1))
  32. a = ''
  33. if force:
  34. c = c.replace(txt1, txt2)
  35. else:
  36. while a.lower() not in ['y', 'n', 'skip']:
  37. a = raw_input('Do you want to Change [y/n/skip]?')
  38. if a.lower() == 'y':
  39. c = c.replace(txt1, txt2)
  40. elif a.lower() == 'skip':
  41. return 'skip'
  42. tmp.append(c)
  43. with open(fpath, 'w') as f:
  44. f.write(''.join(tmp))
  45. print colored('Updated', 'green')
  46. def pull(remote, branch, build=False):
  47. os.system('cd lib && git pull %s %s' % (remote, branch))
  48. os.system('cd app && git pull %s %s' % (remote, branch))
  49. if build: rebuild()
  50. def rebuild():
  51. # build js / css
  52. from webnotes.utils import bundlejs
  53. bundlejs.bundle(False)
  54. def apply_latest_patches():
  55. import webnotes.modules.patch_handler
  56. webnotes.modules.patch_handler.run_all()
  57. print '\n'.join(webnotes.modules.patch_handler.log_list)
  58. def sync_all(force=0):
  59. import webnotes.model.sync
  60. webnotes.model.sync.sync_all(force)
  61. def update_erpnext(remote='origin', branch='master'):
  62. pull(remote, branch)
  63. from webnotes.utils import execute_in_shell
  64. execute_in_shell("lib/wnf.py --patch_sync_build", verbose=1)
  65. def patch_sync_build():
  66. patch_sync()
  67. rebuild()
  68. def patch_sync():
  69. apply_latest_patches()
  70. import webnotes.modules.patch_handler
  71. for l in webnotes.modules.patch_handler.log_list:
  72. if "failed: STOPPED" in l:
  73. return
  74. sync_all()
  75. clear_cache()
  76. def clear_cache():
  77. import webnotes.sessions
  78. webnotes.sessions.clear_cache()
  79. def append_future_import():
  80. """appends from __future__ import unicode_literals to py files if necessary"""
  81. import os
  82. import conf
  83. conf_path = os.path.abspath(conf.__file__)
  84. if conf_path.endswith("pyc"):
  85. conf_path = conf_path[:-1]
  86. base_path = os.path.dirname(conf_path)
  87. for path, folders, files in os.walk(base_path):
  88. for f in files:
  89. if f.endswith('.py'):
  90. file_path = os.path.join(path, f)
  91. with open(file_path, 'r') as pyfile:
  92. content = pyfile.read()
  93. future_import = 'from __future__ import unicode_literals'
  94. if future_import in content: continue
  95. content = content.split('\n')
  96. idx = -1
  97. for c in content:
  98. idx += 1
  99. if c and not c.startswith('#'):
  100. break
  101. content.insert(idx, future_import)
  102. content = "\n".join(content)
  103. with open(file_path, 'w') as pyfile:
  104. pyfile.write(content)
  105. def setup_options():
  106. from optparse import OptionParser
  107. parser = OptionParser()
  108. # install
  109. parser.add_option('--install', nargs=2, metavar = "NEW_DB_NAME SOURCE_PATH",
  110. help="install db")
  111. parser.add_option('--install_fresh', nargs=1, metavar = "NEW_DB_NAME",
  112. help="install fresh db")
  113. # update
  114. parser.add_option("--update", help="Pull, run latest patches and sync all",
  115. nargs=2, metavar="ORIGIN BRANCH")
  116. parser.add_option("--backup", help="Takes backup of database in backup folder",
  117. default=False, action="store_true")
  118. # build
  119. parser.add_option("-b", "--build", default=False, action="store_true",
  120. help="minify + concat js files")
  121. parser.add_option("-w", "--watch", default=False, action="store_true",
  122. help="watch and minify + concat js files, if necessary")
  123. parser.add_option("--no_cms", default=False, action="store_true",
  124. help="do not build wn-web.js and wn-css.js")
  125. parser.add_option("-d", "--db",
  126. dest="db_name",
  127. help="Apply the patches on given db")
  128. parser.add_option("--password",
  129. help="Password for given db", nargs=1)
  130. parser.add_option("--clear_web", default=False, action="store_true",
  131. help="clear web cache")
  132. parser.add_option("--clear_cache", default=False, action="store_true",
  133. help="clear cache")
  134. parser.add_option("--domain", metavar="DOMAIN",
  135. help="store domain in Website Settings", nargs=1)
  136. # git
  137. parser.add_option("--status", default=False, action="store_true",
  138. help="git status")
  139. parser.add_option("--git", nargs=1, default=False,
  140. metavar = "git options",
  141. help="run git with options in both repos")
  142. parser.add_option("--pull", nargs=2, default=False,
  143. metavar = "remote branch",
  144. help="git pull (both repos)")
  145. parser.add_option("--commit", nargs=1, default=False,
  146. metavar = "commit both repos",
  147. help="git commit -a -m [comment]")
  148. parser.add_option("--push", nargs=2, default=False,
  149. metavar = "remote branch",
  150. help="git push (both repos) [remote] [branch]")
  151. parser.add_option("--checkout", nargs=1, default=False,
  152. metavar = "branch",
  153. help="git checkout [branch]")
  154. parser.add_option("-l", "--latest",
  155. action="store_true", dest="run_latest", default=False,
  156. help="Apply the latest patches")
  157. # patch
  158. parser.add_option("-p", "--patch", nargs=1, dest="patch_list", metavar='patch_module',
  159. action="append",
  160. help="Apply patch")
  161. parser.add_option("-f", "--force",
  162. action="store_true", dest="force", default=False,
  163. help="Force Apply all patches specified using option -p or --patch")
  164. parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname",
  165. help="reload doc")
  166. parser.add_option('--export_doc', nargs=2, metavar = "doctype docname",
  167. help="export doc")
  168. # diff
  169. parser.add_option('--diff_ref_file', nargs=0, \
  170. help="Get missing database records and mismatch properties, with file as reference")
  171. parser.add_option('--diff_ref_db', nargs=0, \
  172. help="Get missing .txt files and mismatch properties, with database as reference")
  173. # scheduler
  174. parser.add_option('--run_scheduler', default=False, action="store_true",
  175. help="Trigger scheduler")
  176. parser.add_option('--run_scheduler_event', nargs=1, metavar="[all|daily|weekly|monthly]",
  177. help="Run scheduler event")
  178. # misc
  179. parser.add_option("--replace", nargs=3, default=False,
  180. metavar = "search replace_by extension",
  181. help="file search-replace")
  182. parser.add_option("--sync_all", help="Synchronize all DocTypes using txt files",
  183. nargs=0)
  184. parser.add_option("--sync", help="Synchronize given DocType using txt file",
  185. nargs=2, metavar="module doctype (use their folder names)")
  186. parser.add_option("--patch_sync_build", action="store_true", default=False,
  187. help="run latest patches, sync all and rebuild js css")
  188. parser.add_option("--patch_sync", action="store_true", default=False,
  189. help="run latest patches, sync all")
  190. parser.add_option("--cleanup_data", help="Cleanup test data", default=False,
  191. action="store_true")
  192. parser.add_option("--append_future_import", default=False, action="store_true",
  193. help="append from __future__ import unicode literals to py files")
  194. parser.add_option("--test", help="Run test", metavar="MODULE",
  195. nargs=1)
  196. parser.add_option("--build_message_files", default=False, action="store_true",
  197. help="Build message files for translation")
  198. parser.add_option('--export_messages', nargs=2, metavar="LANG FILENAME",
  199. help="""Export all messages for a language to translation in a csv file.
  200. Example, lib/wnf.py --export_messages hi hindi.csv""")
  201. parser.add_option('--import_messages', nargs=2, metavar="LANG FILENAME",
  202. help="""Import messages for a language and make language files.
  203. Example, lib/wnf.py --import_messages hi hindi.csv""")
  204. parser.add_option('--google_translate', nargs=3, metavar="LANG INFILE OUTFILE",
  205. help="""Auto translate using Google Translate API""")
  206. parser.add_option('--translate', nargs=1, metavar="LANG",
  207. help="""Rebuild translation for the given langauge and
  208. use Google Translate to tranlate untranslated messages. use "all" """)
  209. return parser.parse_args()
  210. def run():
  211. sys.path.append('.')
  212. sys.path.append('lib')
  213. sys.path.append('app')
  214. (options, args) = setup_options()
  215. # build
  216. if options.build:
  217. from webnotes.utils import bundlejs
  218. if options.no_cms:
  219. cms_make = False
  220. else:
  221. cms_make = True
  222. bundlejs.bundle(False, cms_make)
  223. return
  224. elif options.watch:
  225. from webnotes.utils import bundlejs
  226. bundlejs.watch(True)
  227. return
  228. # code replace
  229. elif options.replace:
  230. print options.replace
  231. replace_code('.', options.replace[0], options.replace[1], options.replace[2], force=options.force)
  232. return
  233. # git
  234. elif options.status:
  235. os.chdir('lib')
  236. os.system('git status')
  237. os.chdir('../app')
  238. os.system('git status')
  239. return
  240. elif options.git:
  241. os.chdir('lib')
  242. os.system('git %s' % options.git)
  243. os.chdir('../app')
  244. os.system('git %s' % options.git)
  245. return
  246. import webnotes
  247. import conf
  248. from webnotes.db import Database
  249. import webnotes.modules.patch_handler
  250. # connect
  251. if options.db_name is not None:
  252. if options.password:
  253. webnotes.connect(options.db_name, options.password)
  254. else:
  255. webnotes.connect(options.db_name)
  256. elif not any([options.install, options.pull, options.install_fresh]):
  257. webnotes.connect(conf.db_name)
  258. if options.pull:
  259. pull(options.pull[0], options.pull[1], build=True)
  260. elif options.commit:
  261. os.chdir('lib')
  262. os.system('git commit -a -m "%s"' % (options.commit))
  263. os.chdir('../app')
  264. os.system('git commit -a -m "%s"' % (options.commit))
  265. elif options.push:
  266. os.chdir('lib')
  267. os.system('git push %s %s' % (options.push[0], options.push[1]))
  268. os.chdir('../app')
  269. os.system('git push %s %s' % (options.push[0], options.push[1]))
  270. elif options.checkout:
  271. os.chdir('lib')
  272. os.system('git checkout %s' % options.checkout)
  273. os.chdir('../app')
  274. os.system('git checkout %s' % options.checkout)
  275. # patch
  276. elif options.patch_list:
  277. # clear log
  278. webnotes.modules.patch_handler.log_list = []
  279. # run individual patches
  280. for patch in options.patch_list:
  281. webnotes.modules.patch_handler.run_single(\
  282. patchmodule = patch, force = options.force)
  283. print '\n'.join(webnotes.modules.patch_handler.log_list)
  284. # reload
  285. elif options.reload_doc:
  286. webnotes.modules.patch_handler.reload_doc(\
  287. {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
  288. print '\n'.join(webnotes.modules.patch_handler.log_list)
  289. elif options.export_doc:
  290. from webnotes.modules import export_doc
  291. export_doc(options.export_doc[0], options.export_doc[1])
  292. # run all pending
  293. elif options.run_latest:
  294. apply_latest_patches()
  295. elif options.install:
  296. from webnotes.install_lib.install import Installer
  297. inst = Installer('root')
  298. inst.import_from_db(options.install[0], source_path=options.install[1],
  299. verbose = 1)
  300. elif options.install_fresh:
  301. from webnotes.install_lib.install import Installer
  302. inst = Installer('root')
  303. inst.import_from_db(options.install_fresh, source_path="lib/conf/Framework.sql",
  304. verbose = 1)
  305. elif options.diff_ref_file is not None:
  306. import webnotes.modules.diff
  307. webnotes.modules.diff.diff_ref_file()
  308. elif options.diff_ref_db is not None:
  309. import webnotes.modules.diff
  310. webnotes.modules.diff.diff_ref_db()
  311. elif options.run_scheduler:
  312. import webnotes.utils.scheduler
  313. print webnotes.utils.scheduler.execute()
  314. elif options.run_scheduler_event is not None:
  315. import webnotes.utils.scheduler
  316. print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event)
  317. elif options.sync_all is not None:
  318. sync_all(options.force or 0)
  319. elif options.sync is not None:
  320. webnotes.reload_doc(options.sync[0], "doctype", options.sync[1])
  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(ignore_files = True)
  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. elif options.build_message_files:
  358. import webnotes.translate
  359. webnotes.translate.build_message_files()
  360. elif options.export_messages:
  361. import webnotes.translate
  362. webnotes.translate.export_messages(*options.export_messages)
  363. elif options.import_messages:
  364. import webnotes.translate
  365. webnotes.translate.import_messages(*options.import_messages)
  366. elif options.google_translate:
  367. from webnotes.translate import google_translate
  368. google_translate(*options.google_translate)
  369. elif options.translate:
  370. from webnotes.translate import build_message_files, \
  371. export_messages, google_translate, import_messages
  372. languages = [options.translate]
  373. if options.translate=="all":
  374. from startup import lang_list
  375. languages = lang_list
  376. print "Extracting messages..."
  377. build_message_files()
  378. for lang in languages:
  379. if lang != "en":
  380. filename = 'app/translations/'+lang+'.csv'
  381. print "For " + lang + ":"
  382. print "Compiling messages in one file..."
  383. export_messages(lang, '_lang_tmp.csv')
  384. print "Translating via Google Translate..."
  385. google_translate(lang, '_lang_tmp.csv', filename)
  386. print "Updating language files..."
  387. import_messages(lang, filename)
  388. print "Deleting temp files..."
  389. os.remove('_lang_tmp.csv')
  390. if __name__=='__main__':
  391. run()