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.
 
 
 
 
 
 

600 line
17 KiB

  1. #!/usr/bin/env python
  2. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  3. # MIT License. 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 import build
  53. build.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. parser.add_option('--install_fixtures', default=False, action="store_true",
  114. help="(re) install install-fixtures from app/startup/install_fixtures")
  115. parser.add_option('--reinstall', default=False, action="store_true",
  116. help="install fresh db in db_name specified in conf.py")
  117. parser.add_option('--make_demo', default=False, action="store_true",
  118. help="install in database 'demo'")
  119. parser.add_option('--make_demo_fresh', default=False, action="store_true",
  120. help="install in database 'demo'")
  121. # update
  122. parser.add_option("-u", "--update",
  123. help="Pull, run latest patches and sync all",
  124. default=False, action="store_true", metavar="ORIGIN BRANCH")
  125. parser.add_option("--backup", help="Takes backup of database in backup folder",
  126. default=False, action="store_true")
  127. # build
  128. parser.add_option("-b", "--build", default=False, action="store_true",
  129. help="minify + concat js files")
  130. parser.add_option("-w", "--watch", default=False, action="store_true",
  131. help="watch and minify + concat js files, if necessary")
  132. parser.add_option("--no_cms", default=False, action="store_true",
  133. help="do not build wn-web.js and wn-css.js")
  134. parser.add_option("--docs", default=False, action="store_true",
  135. help="Build docs")
  136. parser.add_option("-d", "--db",
  137. dest="db_name",
  138. help="Apply the patches on given db")
  139. parser.add_option("--password",
  140. help="Password for given db", nargs=1)
  141. parser.add_option("--clear_web", default=False, action="store_true",
  142. help="clear web cache")
  143. parser.add_option("--clear_cache", default=False, action="store_true",
  144. help="clear cache")
  145. parser.add_option("--clear_defaults", default=False, action="store_true",
  146. help="clear cache of defaults")
  147. parser.add_option("--domain", metavar="DOMAIN",
  148. help="store domain in Website Settings", nargs=1)
  149. # git
  150. parser.add_option("--status", default=False, action="store_true",
  151. help="git status")
  152. parser.add_option("--git", nargs=1, default=False,
  153. metavar = "git options",
  154. help="run git with options in both repos")
  155. parser.add_option("--pull", nargs=2, default=False,
  156. metavar = "remote branch",
  157. help="git pull (both repos)")
  158. parser.add_option("-c", "--commit", nargs=1, default=False,
  159. metavar = "commit both repos",
  160. help="git commit -a -m [comment]")
  161. parser.add_option("-p", "--push", default=False,
  162. action="store_true",
  163. metavar = "remote branch",
  164. help="git push (both repos) [remote] [branch]")
  165. parser.add_option("--checkout", nargs=1, default=False,
  166. metavar = "branch",
  167. help="git checkout [branch]")
  168. parser.add_option("-l", "--latest",
  169. action="store_true", dest="run_latest", default=False,
  170. help="Apply the latest patches")
  171. # patch
  172. parser.add_option("--patch", nargs=1, dest="patch_list",
  173. metavar='patch_module',
  174. action="append",
  175. help="Apply patch")
  176. parser.add_option("-f", "--force",
  177. action="store_true", dest="force", default=False,
  178. help="Force Apply all patches specified using option -p or --patch")
  179. parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname",
  180. help="reload doc")
  181. parser.add_option('--export_doc', nargs=2, metavar = "doctype docname",
  182. help="export doc")
  183. # diff
  184. parser.add_option('--diff_ref_file', nargs=0, \
  185. help="Get missing database records and mismatch properties, with file as reference")
  186. parser.add_option('--diff_ref_db', nargs=0, \
  187. help="Get missing .txt files and mismatch properties, with database as reference")
  188. # scheduler
  189. parser.add_option('--run_scheduler', default=False, action="store_true",
  190. help="Trigger scheduler")
  191. parser.add_option('--run_scheduler_event', nargs=1, metavar="[all|daily|weekly|monthly]",
  192. help="Run scheduler event")
  193. # misc
  194. parser.add_option("--replace", nargs=3, default=False,
  195. metavar = "search replace_by extension",
  196. help="file search-replace")
  197. parser.add_option("--sync_all", help="Synchronize all DocTypes using txt files",
  198. nargs=0)
  199. parser.add_option("--sync", help="Synchronize given DocType using txt file",
  200. nargs=2, metavar="module doctype (use their folder names)")
  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("--build_message_files", default=False, action="store_true",
  210. help="Build message files for translation")
  211. parser.add_option('--export_messages', nargs=2, metavar="LANG FILENAME",
  212. help="""Export all messages for a language to translation in a csv file.
  213. Example, lib/wnf.py --export_messages hi hindi.csv""")
  214. parser.add_option('--import_messages', nargs=2, metavar="LANG FILENAME",
  215. help="""Import messages for a language and make language files.
  216. Example, lib/wnf.py --import_messages hi hindi.csv""")
  217. parser.add_option('--google_translate', nargs=3, metavar="LANG INFILE OUTFILE",
  218. help="""Auto translate using Google Translate API""")
  219. parser.add_option('--translate', nargs=1, metavar="LANG",
  220. help="""Rebuild translation for the given langauge and
  221. use Google Translate to tranlate untranslated messages. use "all" """)
  222. parser.add_option("--reset_perms", default=False, action="store_true",
  223. help="Reset permissions for all doctypes.")
  224. parser.add_option("--make_conf", default=False, action="store_true",
  225. help="Create new conf.py file")
  226. # bean helpers
  227. parser.add_option('--export_doclist', nargs=3, metavar="DOCTYPE NAME PATH",
  228. help="""Export doclist as json to the given path, use '-' as name for Singles.""")
  229. parser.add_option('--export_csv', nargs=2, metavar="DOCTYPE PATH",
  230. help="""Dump DocType as csv.""")
  231. parser.add_option('--import_doclist', nargs=1, metavar="PATH",
  232. help="""Import (insert/update) doclist. If the argument is a directory, all files ending with .json are imported""")
  233. return parser.parse_args()
  234. def run():
  235. sys.path.insert(0, '.')
  236. sys.path.insert(0, 'lib')
  237. sys.path.insert(0, 'app')
  238. (options, args) = setup_options()
  239. # build
  240. if options.build:
  241. from webnotes import build
  242. if options.no_cms:
  243. cms_make = False
  244. else:
  245. cms_make = True
  246. build.bundle(False, cms_make)
  247. return
  248. elif options.watch:
  249. from webnotes import build
  250. build.watch(True)
  251. return
  252. # code replace
  253. elif options.replace:
  254. print options.replace
  255. replace_code('.', options.replace[0], options.replace[1], options.replace[2], force=options.force)
  256. return
  257. # git
  258. elif options.status:
  259. os.chdir('lib')
  260. os.system('git status')
  261. os.chdir('../app')
  262. os.system('git status')
  263. return
  264. elif options.git:
  265. os.chdir('lib')
  266. os.system('git %s' % options.git)
  267. os.chdir('../app')
  268. os.system('git %s' % options.git)
  269. return
  270. import webnotes
  271. webnotes.init()
  272. try:
  273. import conf
  274. except ImportError, e:
  275. conf = webnotes._dict({})
  276. webnotes.print_messages = True
  277. # connect
  278. if options.db_name is not None:
  279. if options.password:
  280. webnotes.connect(options.db_name, options.password)
  281. else:
  282. webnotes.connect(options.db_name)
  283. elif not any([options.install, options.pull, options.install_fresh, options.reinstall, options.make_conf]):
  284. webnotes.connect(conf.db_name)
  285. if options.pull:
  286. pull(options.pull[0], options.pull[1], build=True)
  287. elif options.commit:
  288. os.chdir('lib')
  289. os.system('git commit -a -m "%s"' % (options.commit))
  290. os.chdir('../app')
  291. os.system('git commit -a -m "%s"' % (options.commit))
  292. elif options.push:
  293. if not args:
  294. args = ["origin", conf.branch]
  295. os.chdir('lib')
  296. os.system('git push %s %s' % (args[0], args[1]))
  297. os.chdir('../app')
  298. os.system('git push %s %s' % (args[0], args[1]))
  299. elif options.checkout:
  300. os.chdir('lib')
  301. os.system('git checkout %s' % options.checkout)
  302. os.chdir('../app')
  303. os.system('git checkout %s' % options.checkout)
  304. # patch
  305. elif options.patch_list:
  306. import webnotes.modules.patch_handler
  307. # clear log
  308. webnotes.modules.patch_handler.log_list = []
  309. # run individual patches
  310. for patch in options.patch_list:
  311. webnotes.modules.patch_handler.run_single(\
  312. patchmodule = patch, force = options.force)
  313. print '\n'.join(webnotes.modules.patch_handler.log_list)
  314. # reload
  315. elif options.reload_doc:
  316. import webnotes.modules.patch_handler
  317. webnotes.modules.patch_handler.reload_doc(\
  318. {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
  319. print '\n'.join(webnotes.modules.patch_handler.log_list)
  320. elif options.export_doc:
  321. from webnotes.modules import export_doc
  322. export_doc(options.export_doc[0], options.export_doc[1])
  323. # run all pending
  324. elif options.run_latest:
  325. apply_latest_patches()
  326. elif options.install:
  327. from webnotes.install_lib.install import Installer
  328. inst = Installer('root')
  329. inst.import_from_db(options.install[0], source_path=options.install[1],
  330. verbose = 1)
  331. elif options.install_fresh:
  332. from webnotes.install_lib.install import Installer
  333. inst = Installer('root')
  334. inst.import_from_db(options.install_fresh, verbose = 1)
  335. elif options.install_fixtures:
  336. from webnotes.install_lib.install import install_fixtures
  337. install_fixtures()
  338. elif options.reinstall:
  339. from webnotes.install_lib.install import Installer
  340. inst = Installer('root')
  341. import conf
  342. inst.import_from_db(conf.db_name, verbose = 1)
  343. elif options.make_demo:
  344. import utilities.demo.make_demo
  345. utilities.demo.make_demo.make()
  346. elif options.make_demo_fresh:
  347. import utilities.demo.make_demo
  348. utilities.demo.make_demo.make(reset=True)
  349. elif options.diff_ref_file is not None:
  350. import webnotes.modules.diff
  351. webnotes.modules.diff.diff_ref_file()
  352. elif options.diff_ref_db is not None:
  353. import webnotes.modules.diff
  354. webnotes.modules.diff.diff_ref_db()
  355. elif options.run_scheduler:
  356. import webnotes.utils.scheduler
  357. print webnotes.utils.scheduler.execute()
  358. elif options.run_scheduler_event is not None:
  359. import webnotes.utils.scheduler
  360. print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event)
  361. elif options.sync_all is not None:
  362. sync_all(options.force or 0)
  363. elif options.sync is not None:
  364. webnotes.reload_doc(options.sync[0], "doctype", options.sync[1])
  365. elif options.update:
  366. if not args:
  367. args = ["origin", conf.branch]
  368. update_erpnext(args[0], args[1])
  369. elif options.patch_sync_build:
  370. patch_sync_build()
  371. elif options.patch_sync:
  372. patch_sync()
  373. elif options.cleanup_data:
  374. from utilities import cleanup_data
  375. cleanup_data.run()
  376. elif options.domain:
  377. webnotes.conn.set_value('Website Settings', None, 'subdomain', options.domain)
  378. webnotes.conn.commit()
  379. print "Domain set to", options.domain
  380. elif options.clear_web:
  381. # build wn-web.js and wn-web.css
  382. from website.doctype.website_settings.make_web_include_files import make
  383. make()
  384. import webnotes.webutils
  385. webnotes.webutils.clear_cache()
  386. elif options.clear_cache:
  387. clear_cache()
  388. elif options.clear_defaults:
  389. import webnotes.defaults
  390. webnotes.defaults.clear_cache()
  391. webnotes.clear_cache()
  392. elif options.append_future_import:
  393. append_future_import()
  394. elif options.backup:
  395. from webnotes.utils.backups import scheduled_backup
  396. scheduled_backup(ignore_files = True)
  397. # print messages
  398. if webnotes.message_log:
  399. print '\n'.join(webnotes.message_log)
  400. elif options.build_message_files:
  401. import webnotes.translate
  402. webnotes.translate.build_message_files()
  403. elif options.export_messages:
  404. import webnotes.translate
  405. webnotes.translate.export_messages(*options.export_messages)
  406. elif options.import_messages:
  407. import webnotes.translate
  408. webnotes.translate.import_messages(*options.import_messages)
  409. elif options.google_translate:
  410. from webnotes.translate import google_translate
  411. google_translate(*options.google_translate)
  412. elif options.translate:
  413. from webnotes.translate import translate
  414. translate(options.translate)
  415. elif options.docs:
  416. from core.doctype.documentation_tool.documentation_tool import write_static
  417. write_static()
  418. elif options.export_doclist:
  419. from core.page.data_import_tool.data_import_tool import export_json
  420. export_json(*list(options.export_doclist))
  421. elif options.export_csv:
  422. from core.page.data_import_tool.data_import_tool import export_csv
  423. export_csv(*options.export_csv)
  424. elif options.import_doclist:
  425. import json
  426. if os.path.isdir(options.import_doclist):
  427. docs = [os.path.join(options.import_doclist, f) \
  428. for f in os.listdir(options.import_doclist)]
  429. else:
  430. docs = [options.import_doclist]
  431. for f in docs:
  432. if f.endswith(".json"):
  433. with open(f, "r") as infile:
  434. b = webnotes.bean(json.loads(infile.read())).insert_or_update()
  435. print "Imported: " + b.doc.doctype + " / " + b.doc.name
  436. webnotes.conn.commit()
  437. if f.endswith(".csv"):
  438. from core.page.data_import_tool.data_import_tool import import_file_by_path
  439. import_file_by_path(f, ignore_links=True)
  440. webnotes.conn.commit()
  441. elif options.reset_perms:
  442. for d in webnotes.conn.sql_list("""select name from `tabDocType`
  443. where ifnull(istable, 0)=0 and ifnull(custom, 0)=0"""):
  444. try:
  445. webnotes.clear_cache(doctype=d)
  446. webnotes.reset_perms(d)
  447. except:
  448. pass
  449. elif options.make_conf:
  450. if os.path.exists("conf.py"):
  451. os.system("mv conf.py conf.py.bak")
  452. with open("lib/conf/conf.py", "r") as confsrc:
  453. confstr = confsrc.read()
  454. db_name = raw_input("Database Name: ")
  455. if not db_name:
  456. print "Database Name Required"
  457. return
  458. db_password = raw_input("Database Password: ")
  459. if not db_password:
  460. print "Database Name Required"
  461. return
  462. with open("conf.py", "w") as conftar:
  463. conftar.write(confstr % {"db_name": db_name, "db_password": db_password })
  464. if __name__=='__main__':
  465. run()