Uh oh!
There was an error while loading. Please reload this page.
forked from frappe/frappe
- Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathwnf.py
More file actions
Latest commit
executable file
·804 lines (676 loc) · 25.4 KB
/
Copy pathwnf.py
File metadata and controls
executable file
·804 lines (676 loc) · 25.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
#!/usr/bin/env python2.7
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
from __future__ importunicode_literals
importsys
if__name__=="__main__":
sys.path= [".", "lib", "app"] +sys.path
importwebnotes
defmain():
parsed_args=webnotes._dict(vars(setup_parser()))
fn=get_function(parsed_args)
ifparsed_args.get("site")=="all":
forsiteinget_sites():
args=parsed_args.copy()
args["site"] =site
run(fn, args)
else:
run(fn, parsed_args)
defcmd(fn):
defnew_fn(*args, **kwargs):
importinspect
fnargs, varargs, varkw, defaults=inspect.getargspec(fn)
new_kwargs= {}
forainfnargs:
ifainkwargs:
new_kwargs[a] =kwargs.get(a)
returnfn(*args, **new_kwargs)
returnnew_fn
defrun(fn, args):
ifisinstance(args.get(fn), (list, tuple)):
out=globals().get(fn)(*args.get(fn), **args)
else:
out=globals().get(fn)(**args)
returnout
defget_function(args):
forfn, valinargs.items():
if (valorisinstance(val, list)) andglobals().get(fn):
returnfn
defget_sites():
importos
importconf
return [siteforsiteinos.listdir(conf.sites_dir)
ifnotos.path.islink(os.path.join(conf.sites_dir, site))
andos.path.isdir(os.path.join(conf.sites_dir, site))]
defsetup_parser():
importargparse
parser=argparse.ArgumentParser(description="Run webnotes utility functions")
setup_install(parser)
setup_utilities(parser)
setup_translation(parser)
setup_git(parser)
# common
parser.add_argument("-f", "--force", default=False, action="store_true",
help="Force execution where applicable (look for [-f] in help)")
parser.add_argument("--quiet", default=True, action="store_false", dest="verbose",
help="Don't show verbose output where applicable")
parser.add_argument("--site", nargs="?", metavar="SITE-NAME or all",
help="Run for a particular site")
parser.add_argument("--plugin", nargs="?", metavar="PLUGIN-NAME",
help="Run for a particular plugin")
returnparser.parse_args()
defsetup_install(parser):
parser.add_argument("--install", metavar="DB-NAME", nargs=1,
help="Install a new app")
parser.add_argument("--root-password", nargs=1,
help="Root password for new app")
parser.add_argument("--reinstall", default=False, action="store_true",
help="Install a fresh app in db_name specified in conf.py")
parser.add_argument("--restore", metavar=("DB-NAME", "SQL-FILE"), nargs=2,
help="Restore from an sql file")
parser.add_argument("--install_fixtures", default=False, action="store_true",
help="(Re)Install install-fixtures from app/startup/install_fixtures")
parser.add_argument("--make_demo", default=False, action="store_true",
help="Install demo in demo_db_name specified in conf.py")
parser.add_argument("--make_demo_fresh", default=False, action="store_true",
help="(Re)Install demo in demo_db_name specified in conf.py")
parser.add_argument("--add_system_manager", nargs="+",
metavar=("EMAIL", "[FIRST-NAME] [LAST-NAME]"), help="Add a user with all roles")
defsetup_utilities(parser):
# update
parser.add_argument("-u", "--update", nargs="*", metavar=("REMOTE", "BRANCH"),
help="Perform git pull, run patches, sync schema and rebuild files/translations")
parser.add_argument("--reload_gunicorn", default=False, action="store_true", help="reload gunicorn on update")
parser.add_argument("--patch", nargs=1, metavar="PATCH-MODULE",
help="Run a particular patch [-f]")
parser.add_argument("-l", "--latest", default=False, action="store_true",
help="Run patches, sync schema and rebuild files/translations")
parser.add_argument("--sync_all", default=False, action="store_true",
help="Reload all doctypes, pages, etc. using txt files [-f]")
parser.add_argument("--update_all_sites", nargs="*", metavar=("REMOTE", "BRANCH"),
help="Perform git pull, run patches, sync schema and rebuild files/translations")
parser.add_argument("--reload_doc", nargs=3,
metavar=('"MODULE"', '"DOCTYPE"', '"DOCNAME"'))
# build
parser.add_argument("-b", "--build", default=False, action="store_true",
help="Minify + concatenate JS and CSS files, build translations")
parser.add_argument("-w", "--watch", default=False, action="store_true",
help="Watch and concatenate JS and CSS files as and when they change")
# misc
parser.add_argument("--backup", default=False, action="store_true",
help="Take backup of database in backup folder [--with_files]")
parser.add_argument("--delete_older_than", default=6, type=int, help="delete backup older than")
parser.add_argument("--move", default=False, action="store_true",
help="Move site to different directory defined by --dest_dir")
parser.add_argument("--dest_dir", nargs=1, metavar="DEST-DIR",
help="Move site to different directory")
parser.add_argument("--with_files", default=False, action="store_true",
help="Also take backup of files")
parser.add_argument("--domain", nargs="*",
help="Get or set domain in Website Settings")
parser.add_argument("--make_conf", nargs="*", metavar=("DB-NAME", "DB-PASSWORD"),
help="Create new conf.py file")
parser.add_argument("--make_custom_server_script", nargs=1, metavar="DOCTYPE",
help="Create new conf.py file")
parser.add_argument("--set_admin_password", metavar='ADMIN-PASSWORD', nargs=1,
help="Set administrator password")
parser.add_argument("--mysql", action="store_true", help="get mysql shell for a site")
parser.add_argument("--serve", action="store_true", help="Run development server")
parser.add_argument("--profile", action="store_true", help="enable profiling in development server")
parser.add_argument("--smtp", action="store_true", help="Run smtp debug server",
dest="smtp_debug_server")
parser.add_argument("--python", action="store_true", help="get python shell for a site")
parser.add_argument("--ipython", action="store_true", help="get ipython shell for a site")
parser.add_argument("--get_site_status", action="store_true", help="Get site details")
parser.add_argument("--update_site_config", nargs=1,
metavar="SITE-CONFIG-JSON",
help="Update site_config.json for a given --site")
parser.add_argument("--port", default=8000, type=int, help="port for development server")
# clear
parser.add_argument("--clear_web", default=False, action="store_true",
help="Clear website cache")
parser.add_argument("--build_sitemap", default=False, action="store_true",
help="Build Website Sitemap")
parser.add_argument("--rebuild_sitemap", default=False, action="store_true",
help="Rebuild Website Sitemap")
parser.add_argument("--clear_cache", default=False, action="store_true",
help="Clear cache, doctype cache and defaults")
parser.add_argument("--reset_perms", default=False, action="store_true",
help="Reset permissions for all doctypes")
# scheduler
parser.add_argument("--run_scheduler", default=False, action="store_true",
help="Trigger scheduler")
parser.add_argument("--run_scheduler_event", nargs=1,
metavar="all | daily | weekly | monthly",
help="Run a scheduler event")
# replace
parser.add_argument("--replace", nargs=3,
metavar=("SEARCH-REGEX", "REPLACE-BY", "FILE-EXTN"),
help="Multi-file search-replace [-f]")
# import/export
parser.add_argument("--export_doc", nargs=2, metavar=('"DOCTYPE"', '"DOCNAME"'))
parser.add_argument("--export_doclist", nargs=3, metavar=("DOCTYPE", "NAME", "PATH"),
help="""Export doclist as json to the given path, use '-' as name for Singles.""")
parser.add_argument("--export_csv", nargs=2, metavar=("DOCTYPE", "PATH"),
help="""Dump DocType as csv""")
parser.add_argument("--import_doclist", nargs=1, metavar="PATH",
help="""Import (insert/update) doclist. If the argument is a directory, all files ending with .json are imported""")
defsetup_git(parser):
parser.add_argument("--pull", nargs="*", metavar=("REMOTE", "BRANCH"),
help="Run git pull for both repositories")
parser.add_argument("-p", "--push", nargs="*", metavar=("REMOTE", "BRANCH"),
help="Run git push for both repositories")
parser.add_argument("--status", default=False, action="store_true",
help="Run git status for both repositories")
parser.add_argument("--commit", nargs=1, metavar="COMMIT-MSG",
help="Run git commit COMMIT-MSG for both repositories")
parser.add_argument("--checkout", nargs=1, metavar="BRANCH",
help="Run git checkout BRANCH for both repositories")
parser.add_argument("--git", nargs="*", metavar="OPTIONS",
help="Run git command for both repositories")
parser.add_argument("--bump", metavar=("REPO", "VERSION-TYPE"), nargs=2,
help="Bump project version")
defsetup_translation(parser):
parser.add_argument("--build_message_files", default=False, action="store_true",
help="Build message files for translation")
parser.add_argument("--export_messages", nargs=2, metavar=("LANG-CODE", "FILENAME"),
help="""Export all messages for a language to translation in a csv file.
Example, lib/wnf.py --export_messages hi hindi.csv""")
parser.add_argument("--import_messages", nargs=2, metavar=("LANG-CODE", "FILENAME"),
help="""Import messages for a language and make language files.
Example, lib/wnf.py --import_messages hi hindi.csv""")
parser.add_argument("--google_translate", nargs=3,
metavar=("LANG-CODE", "INFILE", "OUTFILE"),
help="Auto translate using Google Translate API")
parser.add_argument("--translate", nargs=1, metavar="LANG-CODE",
help="""Rebuild translation for the given langauge and
use Google Translate to tranlate untranslated messages. use "all" """)
# methods
# install
@cmd
definstall(db_name, source_sql=None, site=None, verbose=True, force=False, root_password=None, site_config=None, admin_password='admin'):
fromwebnotes.install_lib.installimportInstaller
inst=Installer('root', db_name=db_name, site=site, root_password=root_password, site_config=site_config)
inst.install(db_name, source_sql=source_sql, verbose=verbose, force=force, admin_password=admin_password)
webnotes.destroy()
@cmd
defreinstall(site=None, verbose=True):
webnotes.init(site=site)
install(webnotes.conf.db_name, site=site, verbose=verbose, force=True)
@cmd
defrestore(db_name, source_sql, site=None, verbose=True, force=False):
install(db_name, source_sql, site=site, verbose=verbose, force=force)
@cmd
definstall_fixtures(site=None):
webnotes.init(site=site)
fromwebnotes.install_lib.installimportinstall_fixtures
install_fixtures()
webnotes.destroy()
@cmd
defadd_system_manager(email, first_name=None, last_name=None, site=None):
webnotes.connect(site=site)
webnotes.profile.add_system_manager(email, first_name, last_name)
webnotes.conn.commit()
webnotes.destroy()
@cmd
defmake_demo(site=None):
importutilities.demo.make_demo
webnotes.init(site=site)
utilities.demo.make_demo.make()
webnotes.destroy()
@cmd
defmake_demo_fresh(site=None):
importutilities.demo.make_demo
webnotes.init(site=site)
utilities.demo.make_demo.make(reset=True)
webnotes.destroy()
# utilities
@cmd
defupdate(remote=None, branch=None, site=None, reload_gunicorn=False):
pull(remote=remote, branch=branch, site=site)
# maybe there are new framework changes, any consequences?
reload(webnotes)
ifnotsite: build()
latest(site=site)
ifreload_gunicorn:
importsubprocess
subprocess.check_output("killall -HUP gunicorn".split())
@cmd
deflatest(site=None, verbose=True):
importwebnotes.modules.patch_handler
importwebnotes.model.sync
importwebnotes.plugins
fromwebsite.doctype.website_sitemap_config.website_sitemap_configimportbuild_website_sitemap_config
webnotes.connect(site=site)
try:
# run patches
webnotes.local.patch_log_list= []
webnotes.modules.patch_handler.run_all()
ifverbose:
print"\n".join(webnotes.local.patch_log_list)
# sync
webnotes.model.sync.sync_all()
# remove __init__.py from plugins
webnotes.plugins.remove_init_files()
# build website config if any changes in templates etc.
build_website_sitemap_config()
exceptwebnotes.modules.patch_handler.PatchError, e:
print"\n".join(webnotes.local.patch_log_list)
raise
finally:
webnotes.destroy()
@cmd
defsync_all(site=None, force=False):
importwebnotes.model.sync
webnotes.connect(site=site)
webnotes.model.sync.sync_all(force=force)
webnotes.destroy()
@cmd
defpatch(patch_module, site=None, force=False):
importwebnotes.modules.patch_handler
webnotes.connect(site=site)
webnotes.local.patch_log_list= []
webnotes.modules.patch_handler.run_single(patch_module, force=force)
print"\n".join(webnotes.local.patch_log_list)
webnotes.destroy()
@cmd
defupdate_all_sites(remote=None, branch=None, verbose=True):
pull(remote, branch)
# maybe there are new framework changes, any consequences?
reload(webnotes)
build()
forsiteinget_sites():
latest(site=site, verbose=verbose)
@cmd
defreload_doc(module, doctype, docname, plugin=None, site=None, force=False):
webnotes.connect(site=site)
webnotes.reload_doc(module, doctype, docname, plugin=plugin, force=force)
webnotes.conn.commit()
webnotes.destroy()
@cmd
defbuild():
importwebnotes.build
webnotes.build.bundle(False)
@cmd
defwatch():
importwebnotes.build
webnotes.build.watch(True)
@cmd
defbackup(site=None, with_files=False, verbose=True, backup_path_db=None, backup_path_files=None, delete_older_than=6):
fromwebnotes.utils.backupsimportscheduled_backup
webnotes.connect(site=site)
odb=scheduled_backup(delete_older_than=delete_older_than, ignore_files=notwith_files, backup_path_db=backup_path_db, backup_path_files=backup_path_files)
ifverbose:
fromwebnotes.utilsimportnow
print"database backup taken -", odb.backup_path_db, "- on", now()
ifwith_files:
print"files backup taken -", odb.backup_path_files, "- on", now()
webnotes.destroy()
returnodb
@cmd
defmove(site=None, dest_dir=None):
importos
ifnotdest_dir:
raiseException, "--dest_dir is required for --move"
ifnotos.path.isdir(dest_dir):
raiseException, "destination is not a directory or does not exist"
webnotes.init(site=site)
old_path=webnotes.utils.get_site_path()
new_path=os.path.join(dest_dir, site)
# check if site dump of same name already exists
site_dump_exists=True
count=0
whilesite_dump_exists:
final_new_path=new_path+ (countandstr(count) or"")
site_dump_exists=os.path.exists(final_new_path)
count=int(countor0) +1
os.rename(old_path, final_new_path)
webnotes.destroy()
returnos.path.basename(final_new_path)
@cmd
defdomain(host_url=None, site=None):
webnotes.connect(site=site)
ifhost_url:
webnotes.conn.set_value("Website Settings", None, "subdomain", host_url)
webnotes.conn.commit()
else:
printwebnotes.conn.get_value("Website Settings", None, "subdomain")
webnotes.destroy()
@cmd
defmake_conf(db_name=None, db_password=None, site=None, site_config=None):
fromwebnotes.install_lib.installimportmake_conf
make_conf(db_name=db_name, db_password=db_password, site=site, site_config=site_config)
@cmd
defmake_custom_server_script(doctype, site=None):
fromcore.doctype.custom_script.custom_scriptimportmake_custom_server_script_file
webnotes.connect(site=site)
make_custom_server_script_file(doctype)
webnotes.destroy()
# clear
@cmd
defclear_cache(site=None):
importwebnotes.sessions
webnotes.connect(site=site)
webnotes.sessions.clear_cache()
webnotes.destroy()
@cmd
defclear_web(site=None):
importwebnotes.webutils
webnotes.connect(site=site)
fromwebsite.doctype.website_sitemap_config.website_sitemap_configimportbuild_website_sitemap_config
build_website_sitemap_config()
webnotes.webutils.clear_cache()
webnotes.destroy()
@cmd
defbuild_sitemap(site=None):
fromwebsite.doctype.website_sitemap_config.website_sitemap_configimportbuild_website_sitemap_config
webnotes.connect(site=site)
build_website_sitemap_config()
webnotes.destroy()
@cmd
defrebuild_sitemap(site=None):
fromwebsite.doctype.website_sitemap_config.website_sitemap_configimportrebuild_website_sitemap_config
webnotes.connect(site=site)
rebuild_website_sitemap_config()
webnotes.destroy()
@cmd
defreset_perms(site=None):
webnotes.connect(site=site)
fordinwebnotes.conn.sql_list("""select name from `tabDocType`
where ifnull(istable, 0)=0 and ifnull(custom, 0)=0"""):
webnotes.clear_cache(doctype=d)
webnotes.reset_perms(d)
webnotes.destroy()
# scheduler
@cmd
defrun_scheduler(site=None):
fromwebnotes.utils.file_lockimportcreate_lock, delete_lock
importwebnotes.utils.scheduler
webnotes.init(site=site)
ifcreate_lock('scheduler'):
webnotes.connect(site=site)
printwebnotes.utils.scheduler.execute()
delete_lock('scheduler')
webnotes.destroy()
@cmd
defrun_scheduler_event(event, site=None):
importwebnotes.utils.scheduler
webnotes.connect(site=site)
printwebnotes.utils.scheduler.trigger("execute_"+event)
webnotes.destroy()
# replace
@cmd
defreplace(search_regex, replacement, extn, force=False):
printsearch_regex, replacement, extn
replace_code('.', search_regex, replacement, extn, force=force)
# import/export
@cmd
defexport_doc(doctype, docname, site=None):
importwebnotes.modules
webnotes.connect(site=site)
webnotes.modules.export_doc(doctype, docname)
webnotes.destroy()
@cmd
defexport_doclist(doctype, name, path, site=None):
fromcore.page.data_import_toolimportdata_import_tool
webnotes.connect(site=site)
data_import_tool.export_json(doctype, name, path)
webnotes.destroy()
@cmd
defexport_csv(doctype, path, site=None):
fromcore.page.data_import_toolimportdata_import_tool
webnotes.connect(site=site)
data_import_tool.export_csv(doctype, path)
webnotes.destroy()
@cmd
defimport_doclist(path, site=None, force=False):
fromcore.page.data_import_toolimportdata_import_tool
webnotes.connect(site=site)
data_import_tool.import_doclist(path, overwrite=force)
webnotes.destroy()
# translation
@cmd
defbuild_message_files(site=None):
importwebnotes.translate
webnotes.connect(site=site)
webnotes.translate.build_message_files()
webnotes.destroy()
@cmd
defexport_messages(lang, outfile, site=None):
importwebnotes.translate
webnotes.connect(site=site)
webnotes.translate.export_messages(lang, outfile)
webnotes.destroy()
@cmd
defimport_messages(lang, infile, site=None):
importwebnotes.translate
webnotes.connect(site=site)
webnotes.translate.import_messages(lang, infile)
webnotes.destroy()
@cmd
defgoogle_translate(lang, infile, outfile, site=None):
importwebnotes.translate
webnotes.connect(site=site)
webnotes.translate.google_translate(lang, infile, outfile)
webnotes.destroy()
@cmd
deftranslate(lang, site=None):
importwebnotes.translate
webnotes.connect(site=site)
webnotes.translate.translate(lang)
webnotes.destroy()
# git
@cmd
defgit(param):
ifisinstance(param, (list, tuple)):
param=" ".join(param)
importos
os.system("""cd lib && git %s"""%param)
os.system("""cd app && git %s"""%param)
defget_remote_and_branch(remote=None, branch=None):
ifnot (remoteandbranch):
webnotes.init()
ifnotwebnotes.conf.branch:
raiseException("Please specify remote and branch")
remote=remoteor"origin"
branch=branchorwebnotes.conf.branch
webnotes.destroy()
returnremote, branch
@cmd
defpull(remote=None, branch=None):
remote, branch=get_remote_and_branch(remote, branch)
git(("pull", remote, branch))
@cmd
defpush(remote=None, branch=None):
remote, branch=get_remote_and_branch(remote, branch)
git(("push", remote, branch))
@cmd
defstatus():
git("status")
@cmd
defcommit(message):
git("""commit -a -m "%s" """%message.replace('"', '\"'))
@cmd
defcheckout(branch):
git(("checkout", branch))
@cmd
defset_admin_password(admin_password, site=None):
importwebnotes
webnotes.connect(site=site)
webnotes.conn.sql("""update __Auth set `password`=password(%s)
where user='Administrator'""", (admin_password,))
webnotes.conn.commit()
webnotes.destroy()
@cmd
defmysql(site=None):
importwebnotes
importcommands, os
msq=commands.getoutput('which mysql')
webnotes.init(site=site)
os.execv(msq, [msq, '-u', webnotes.conf.db_name, '-p'+webnotes.conf.db_password, webnotes.conf.db_name, '-h', webnotes.conf.db_hostor"localhost", "-A"])
webnotes.destroy()
@cmd
defpython(site=None):
importwebnotes
importcommands, os
python=commands.getoutput('which python')
webnotes.init(site=site)
ifsite:
os.environ["site"] =site
os.environ["PYTHONSTARTUP"] =os.path.join(os.path.dirname(__file__), "pythonrc.py")
os.execv(python, [python])
webnotes.destroy()
@cmd
defipython(site=None):
importwebnotes
webnotes.connect(site=site)
importIPython
IPython.embed()
@cmd
defsmtp_debug_server():
importcommands, os
python=commands.getoutput('which python')
os.execv(python, [python, '-m', "smtpd", "-n", "-c", "DebuggingServer", "localhost:25"])
@cmd
defserve(port=8000, profile=False):
importwebnotes.app
webnotes.app.serve(port=port, profile=profile)
defreplace_code(start, txt1, txt2, extn, search=None, force=False):
"""replace all txt1 by txt2 in files with extension (extn)"""
importwebnotes.utils
importos, re
esc=webnotes.utils.make_esc('[]')
ifnotsearch: search=esc(txt1)
forwtinos.walk(start, followlinks=1):
forfninwt[2]:
iffn.split('.')[-1]==extn:
fpath=os.path.join(wt[0], fn)
withopen(fpath, 'r') asf:
content=f.read()
ifre.search(search, content):
res=search_replace_with_prompt(fpath, txt1, txt2, force)
ifres=='skip':
return'skip'
defsearch_replace_with_prompt(fpath, txt1, txt2, force=False):
""" Search and replace all txt1 by txt2 in the file with confirmation"""
fromtermcolorimportcolored
withopen(fpath, 'r') asf:
content=f.readlines()
tmp= []
forcincontent:
ifc.find(txt1) !=-1:
printfpath
printcolored(txt1, 'red').join(c[:-1].split(txt1))
a=''
ifforce:
c=c.replace(txt1, txt2)
else:
whilea.lower() notin ['y', 'n', 'skip']:
a=raw_input('Do you want to Change [y/n/skip]?')
ifa.lower() =='y':
c=c.replace(txt1, txt2)
elifa.lower() =='skip':
return'skip'
tmp.append(c)
withopen(fpath, 'w') asf:
f.write(''.join(tmp))
printcolored('Updated', 'green')
@cmd
defget_site_status(site=None, verbose=False):
importwebnotes
importwebnotes.utils
fromwebnotes.profileimportget_system_managers
fromcore.doctype.profile.profileimportget_total_users, get_active_users, \
get_website_users, get_active_website_users
importjson
webnotes.connect(site=site)
ret= {
'last_backup_on': webnotes.local.conf.last_backup_on,
'active_users': get_active_users(),
'total_users': get_total_users(),
'active_website_users': get_active_website_users(),
'website_users': get_website_users(),
'system_managers': "\n".join(get_system_managers()),
'default_company': webnotes.conn.get_default("company"),
'disk_usage': webnotes.utils.get_disk_usage(),
'working_directory': webnotes.utils.get_base_path()
}
# country, timezone, industry
control_panel_details=webnotes.conn.get_value("Control Panel", "Control Panel",
["country", "time_zone", "industry"], as_dict=True)
ifcontrol_panel_details:
ret.update(control_panel_details)
# basic usage/progress analytics
fordoctypein ("Company", "Customer", "Item", "Quotation", "Sales Invoice",
"Journal Voucher", "Stock Ledger Entry"):
key=doctype.lower().replace(" ", "_") +"_exists"
ret[key] =1ifwebnotes.conn.count(doctype) else0
webnotes.destroy()
ifverbose:
printjson.dumps(ret, indent=1, sort_keys=True)
returnret
@cmd
defupdate_site_config(site_config, site, verbose=False):
importjson
ifisinstance(site_config, basestring):
site_config=json.loads(site_config)
webnotes.init(site=site)
webnotes.conf.site_config.update(site_config)
site_config_path=webnotes.get_conf_path(webnotes.conf.sites_dir, site)
withopen(site_config_path, "w") asf:
json.dump(webnotes.conf.site_config, f, indent=1, sort_keys=True)
webnotes.destroy()
@cmd
defbump(repo, bump_type):
importjson
assertrepoin ['lib', 'app']
assertbump_typein ['minor', 'major', 'patch']
defvalidate(repo_path):
importgit
repo=git.Repo(repo_path)
ifrepo.active_branch!='master':
raiseException, "Current branch not master in {}".format(repo_path)
defbump_version(version, version_type):
importsemantic_version
v=semantic_version.Version(version)
ifversion_type=='minor':
v.minor+=1
elifversion_type=='major':
v.major+=1
elifversion_type=='patch':
v.patch+=1
returnunicode(v)
defadd_tag(repo_path, version):
importgit
repo=git.Repo(repo_path)
repo.index.add(['config.json'])
repo.index.commit('bumped to version {}'.format(version))
repo.create_tag('v'+version, repo.head)
defupdate_framework_requirement(version):
withopen('app/config.json') asf:
config=json.load(f)
config['requires_framework_version'] ='=='+version
withopen('app/config.json', 'w') asf:
json.dump(config, f, indent=1, sort_keys=True)
validate('lib/')
validate('app/')
ifrepo=='app':
withopen('app/config.json') asf:
config=json.load(f)
new_version=bump_version(config['app_version'], bump_type)
config['app_version'] =new_version
withopen('app/config.json', 'w') asf:
json.dump(config, f, indent=1, sort_keys=True)
add_tag('app/', new_version)
elifrepo=='lib':
withopen('lib/config.json') asf:
config=json.load(f)
new_version=bump_version(config['framework_version'], bump_type)
config['framework_version'] =new_version
withopen('lib/config.json', 'w') asf:
json.dump(config, f, indent=1, sort_keys=True)
add_tag('lib/', new_version)
update_framework_requirement(new_version)
bump('app', bump_type)
if__name__=="__main__":
main()