-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathquickinstall.py
More file actions
executable file
·978 lines (782 loc) · 31.6 KB
/
quickinstall.py
File metadata and controls
executable file
·978 lines (782 loc) · 31.6 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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
#!/usr/bin/python3
# Copyright: 2013 MoinMoin:BastianBlank
# Copyright: 2013-2018 MoinMoin:RogerHaase
# Copyright: 2023-2024 MoinMoin:UlrichB
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
"""
Create a virtual environment and install Moin 2 and all requirements in development mode.
Usage for installation:
<python> quickinstall.py (where <python> is any Python 3.10+ executable)
Requires: Python 3.10+, pip
The first run of quickinstall.py creates these files or symlinks in the repo root:
- Unix: m, activate, wikiconfig.py, intermap.txt
- Windows: m.bat, activate.bat, deactivate.bat, wikiconfig.py, intermap.txt
After initial installation, a menu of frequently used commands is provided for
Moin 2 developers and desktop wiki users.
- wraps a few commonly used moin commands, do "moin --help" for infrequently used commands
- adds default file names for selected moin commands (backup, restore, ...)
- creates log files for functions with large output, extracts success/failure messages
- displays error messages if the user tries to run commands out of sequence
In normal usage, after the initial installation the user will activate the newly created
virtual environment before executing commands.
Usage (to display a menu of commands):
- Unix: ./m
- Windows: m
"""
from __future__ import annotations
from typing import Any, ClassVar
import argparse
import fnmatch
import glob
import os
import os.path
import platform
import subprocess
import sys
import shutil
import timeit
import venv
from abc import ABC, abstractmethod
from collections import Counter
MIN_PYTHON_VERSION = (3, 10)
WIN_INFO = "m.bat, activate.bat, and deactivate.bat are created by quickinstall.py"
NIX_INFO = "the m bash script and the activate symlink are created by quickinstall.py"
# text files created by commands with high volume output
QUICKINSTALL = "m-quickinstall.txt"
TOX = "m-tox.txt"
CODING_STD = "m-coding-std.txt"
DOCS = "m-docs.txt"
NEWWIKI = "m-new-wiki.txt"
DELWIKI = "m-delete-wiki.txt"
BACKUPWIKI = "m-backup-wiki.txt"
DUMPHTML = "m-dump-html.txt"
EXTRAS = "m-extras.txt"
DIST = "m-create-dist.txt"
# default files used for backup and restore
BACKUP_FILENAME = os.path.normpath("wiki/backup.moin")
JUST_IN_CASE_BACKUP = os.path.normpath("wiki/deleted-backup.moin")
if sys.version_info < MIN_PYTHON_VERSION:
sys.exit(
f"Error: MoinMoin requires Python { '.'.join(map(str, MIN_PYTHON_VERSION)) }+, "
f"current version is { platform.python_version() }"
)
if os.name == "nt":
M = "m" # customize help to local OS
SEP = " & "
WINDOWS_OS = True
ACTIVATE = "activate"
else:
M = "./m"
SEP = ";"
WINDOWS_OS = False
ACTIVATE = ". activate"
# commands that create log files
CMD_LOGS = {
"quickinstall": QUICKINSTALL,
"tests": TOX,
# 'coding-std': CODING_STD, # not logged due to small output
"docs": DOCS,
"new-wiki": NEWWIKI,
"del-wiki": DELWIKI,
"backup": BACKUPWIKI,
"dump-html": DUMPHTML,
"extras": EXTRAS,
"dist": DIST,
}
# code below and path_locations copied from virtualenv.py v16.7.10 because path_locations dropped in v20.0.0 rewrite.
PY_VERSION = f"python{sys.version_info[0]}.{sys.version_info[1]}"
IS_PYPY = hasattr(sys, "pypy_version_info")
IS_WIN = sys.platform == "win32"
ABI_FLAGS = getattr(sys, "abiflags", "")
join = os.path.join
help = """
Usage: "{} <target>" where <target> is:
quickinstall update virtual environment with required packages
extras install packages required for docs and moin development
docs create Moin HTML documentation (requires extras)
interwiki refresh intermap.txt
log <target> view detailed log generated by <target>, omit to see list
new-wiki create empty wiki
restore * create wiki and restore wiki/backup.moin *option, specify file
backup * roll 3 prior backups and create new backup *option, specify file
dump-html * create a static HTML image of wiki *options, see docs
css run sass to update basic theme CSS files
tests * run tests, log output (-v -k my_test)
coding-std correct scripts that taint the repository with trailing spaces..
del-all same as running the 4 del-* commands below
del-orig delete all files matching *.orig
del-pyc delete all files matching *.pyc
del-rej delete all files matching *.rej
del-wiki create a backup, then delete all wiki data
Please refer to 'moin help' to learn more about the CLI for wiki administrators.
""".format(M)
def mkdir(at_path: str) -> None:
if not os.path.exists(at_path):
os.makedirs(at_path)
def path_locations(home_dir: str, dry_run: bool = False) -> tuple[str, str, str, str]:
"""
Return the path locations for the environment (where libraries are, where scripts go, etc).
"""
home_dir = os.path.abspath(home_dir)
lib_dir, inc_dir, bin_dir = None, None, None
# XXX: We'd use distutils.sysconfig.get_python_inc/lib but its
# prefix arg is broken: http://bugs.python.org/issue3386
if IS_WIN:
# Windows has lots of problems with executables with spaces in
# the name; this function will remove them (using the ~1
# format):
if not dry_run:
mkdir(home_dir)
if " " in home_dir:
import ctypes
get_short_path_name = ctypes.windll.kernel32.GetShortPathNameW
size = max(len(home_dir) + 1, 256)
buf = ctypes.create_unicode_buffer(size)
try:
# noinspection PyUnresolvedReferences
u = unicode
except NameError:
u = str
ret = get_short_path_name(u(home_dir), buf, size)
if not ret:
print(f'Error: the path "{home_dir}" has a space in it')
print("We could not determine the short pathname for it.")
print("Exiting.")
sys.exit(3)
home_dir = str(buf.value)
lib_dir = join(home_dir, "Lib")
inc_dir = join(home_dir, "Include")
bin_dir = join(home_dir, "Scripts")
if IS_PYPY:
lib_dir = home_dir
inc_dir = join(home_dir, "include")
bin_dir = join(home_dir, "bin")
elif not IS_WIN:
lib_dir = join(home_dir, "lib", PY_VERSION)
inc_dir = join(home_dir, "include", PY_VERSION + ABI_FLAGS)
bin_dir = join(home_dir, "bin")
return home_dir, lib_dir, inc_dir, bin_dir
def search_for_phrase(filename: str) -> None:
"""
Search a text file for key phrases and print the lines of interest or print a count by phrase.
"""
files = {
# filename: (list of phrases)
# Note: phrases must be lower-case
QUICKINSTALL: (
"could not find",
"error",
"fail",
"timeout",
"traceback",
"success",
"cache location",
"must be deactivated",
"no such option",
),
NEWWIKI: ("error", "fail", "timeout", "traceback", "success"),
BACKUPWIKI: ("error", "fail", "timeout", "traceback", "success"),
DUMPHTML: ("fail", "timeout", "traceback", "success", "cannot", "denied", "error"),
# use of 'error ' below is to avoid matching .../Modules/errors.o....
EXTRAS: (
"error ",
"error:",
"error.",
"error,",
"fail",
"timeout",
"traceback",
"active version",
"successfully installed",
"finished",
),
# ': e' matches lines similar to:
# src/moin/converters\_tests\test_moinwiki_in_out.py:294:5: E303 too many blank lines (3)
TOX: ("seconds =", "internalerror", "error:", "traceback", ": e", ": f", " passed, "),
CODING_STD: ("remove trailing blanks", "dos line endings", "unix line endings", "remove empty lines"),
DIST: ("creating", "copying", "adding", "hard linking"),
DOCS: (
"build finished",
"build succeeded",
"traceback",
"failed",
"error",
"usage",
"importerror",
"exception occurred",
),
}
ignore_phrases = {TOX: ("interpreternotfound",)}
# For these file names, display a count of occurrences rather than each found line
print_counts = (CODING_STD, DIST)
with open(filename) as f:
lines = f.readlines()
name = os.path.split(filename)[1]
phrases = files[name]
ignore_phrase = ignore_phrases[name] if name in ignore_phrases else ()
counts = Counter()
for idx, line in enumerate(lines):
for phrase in phrases:
if phrase in line.lower():
if filename in print_counts:
counts[phrase] += 1
else:
skip = False
for ignore in ignore_phrase:
if ignore in line.lower():
skip = True
break
if not skip:
print(idx + 1, line.rstrip())
break
for key in counts:
print(f'The phrase "{key}" was found {counts[key]} times.')
def wiki_exists() -> bool:
"""
Return True if a wiki exists.
"""
return bool(glob.glob("wiki/index/_all_revs_*.toc"))
def copy_config_files() -> None:
if not os.path.isfile("wikiconfig.py"):
shutil.copy("src/moin/config/wikiconfig.py", "wikiconfig.py")
if not os.path.isfile("intermap.txt"):
shutil.copy("src/moin/contrib/intermap.txt", "intermap.txt")
if not os.path.isdir("wiki_local"):
os.mkdir("wiki_local")
def make_wiki(command: str, mode: str = "w", msg: str = "\nSuccess: a new wiki has been created.") -> None:
"""
Process command to create a new wiki.
"""
if wiki_exists() and mode == "w":
print("Error: a wiki exists, delete it and try again.")
else:
copy_config_files()
print(f"Output messages redirected to {NEWWIKI}.")
with open(NEWWIKI, mode) as messages:
result = subprocess.call(command, shell=True, stderr=messages, stdout=messages)
if result == 0:
print(msg)
else:
print(f"Important messages from {NEWWIKI} are shown below:")
search_for_phrase(NEWWIKI)
print(f'\nError: attempt to create wiki failed. Do "{M} log new-wiki" to see complete log.')
def delete_files(pattern) -> None:
"""
Recursively delete all files matching pattern.
"""
matches = 0
for root, dirnames, filenames in os.walk(os.path.abspath(os.path.dirname(__file__))):
for filename in fnmatch.filter(filenames, pattern):
os.remove(os.path.join(root, filename))
matches += 1
print(f'Deleted {matches} files matching "{pattern}".')
def create_m() -> None:
"""
Create an 'm.bat or 'm' bash script that will run quickinstall.py using this Python.
"""
quickinstall = sys.argv[0]
if WINDOWS_OS:
with open("m.bat", "w") as f:
f.write(f":: {WIN_INFO}\n\n@{sys.executable} {quickinstall} %*\n")
else:
with open("m", "w") as f:
f.write(f"#!/bin/sh\n# {NIX_INFO}\n\n{sys.executable} {quickinstall} $*\n")
os.fchmod(f.fileno(), 0o775)
class Command(ABC):
key: ClassVar[str]
def __init__(self, args: argparse.Namespace = argparse.Namespace(), additional: list[str] = []) -> None:
self.args = args
self.additional = additional
self.tic = timeit.default_timer()
def run_time(self) -> None:
command = self.__class__.__name__
seconds = int(timeit.default_timer() - self.tic)
t_min, t_sec = divmod(seconds, 60)
t_hour, t_min = divmod(t_min, 60)
print(f"{command} run time (h:mm:ss) {t_hour}:{t_min:0>2}:{t_sec:0>2}")
@abstractmethod
def execute(self) -> None: ...
class QuickInstall(Command):
"""
Perform quick installation steps required for moin wiki development.
"""
key = "quickinstall"
def run_shell_command(self, command: str, append: bool = False) -> None:
with open(QUICKINSTALL, "a" if append else "w") as messages:
# we run ourself as a subprocess so output can be captured in a log file
subprocess.run(command, shell=True, stderr=messages, stdout=messages)
def execute(self) -> None:
source = os.path.dirname(os.path.realpath(sys.argv[0]))
command = f"{sys.executable} {sys.argv[0]} createvenv --source {source}"
if self.args.venv:
command += f" --name {self.args.venv}"
print(f"Running {sys.argv[0]}... output messages redirected to {QUICKINSTALL}")
self.run_shell_command(command)
if os.path.isdir(".tox"):
# keep tox test virtualenvs in sync with moin-env-python
command = "tox --recreate --notest"
print(f"Running tox recreate virtualenvs... output messages redirected to {QUICKINSTALL}")
self.run_shell_command(command, append=True)
copy_config_files()
print(
f"\nSearching {QUICKINSTALL}, important messages are shown below... "
f'Do "{M} log quickinstall" to see complete log.\n'
)
search_for_phrase(QUICKINSTALL)
self.run_time()
class CreateVirtualEnv(Command):
"""
Create or update a virtual environment with the required packages.
"""
key = "createvenv"
def __init__(self, args: argparse.Namespace, additional: list[str]) -> None:
super().__init__(args, additional)
self.dir_source = args.source
venv_path = self._get_venv_path(args.source, args.name)
venv_home, venv_lib, venv_inc, venv_bin = path_locations(venv_path)
self.dir_venv = venv_home
self.dir_venv_bin = venv_bin
def _get_venv_path(self, source, name: str | None = None) -> str:
if not name:
base, source_name = os.path.split(source)
executable = os.path.basename(sys.executable).split(".exe")[0]
venv_path = os.path.join(base, f"{source_name}-venv-{executable}")
else:
venv_path = os.path.join(source, name)
return os.path.abspath(venv_path)
def execute(self) -> None:
self.do_venv()
self.do_helpers()
self.do_install()
self.do_catalog()
sys.stdout.write(f"\n\nSuccessfully created or updated venv at {self.dir_venv}\n")
def do_venv(self) -> None:
venv.create(self.dir_venv, system_site_packages=False, clear=False, symlinks=False, with_pip=True, prompt=None)
def do_install(self) -> None:
args = [
os.path.join(self.dir_venv_bin, "pip"),
"install",
"--upgrade",
"--upgrade-strategy=eager",
"--editable",
self.dir_source,
]
subprocess.check_call(args)
def do_catalog(self) -> None:
subprocess.check_call(
(
os.path.join(self.dir_venv_bin, "pybabel"),
"compile",
"--statistics",
# needed in case user runs quickinstall.py with a cwd other than the repo root
"--directory",
os.path.join(os.path.dirname(__file__), "src", "moin", "translations"),
)
)
def create_wrapper(self, filename, target) -> None:
"""
Create files in the repo root that wrap files in `<path-to-virtual-env>/Scripts`.
"""
target = os.path.join(self.dir_venv_bin, target)
with open(filename, "w") as f:
f.write(f":: {WIN_INFO}\n\n@call {target} %*\n")
def do_helpers(self) -> None:
"""
Create small helper scripts or symlinks in repo root, avoid keying the long path to virtual env.
"""
create_m()
if WINDOWS_OS:
# windows commands are: activate | deactivate
self.create_wrapper("activate.bat", "activate.bat")
self.create_wrapper("deactivate.bat", "deactivate.bat")
else:
# linux commands are: source activate | deactivate
if os.path.exists("activate"):
os.unlink("activate")
os.symlink(os.path.join(self.dir_venv_bin, "activate"), "activate") # no need to define deactivate on unix
class GenerateDocs(Command):
"""
Create local Sphinx html documentation.
"""
key = "docs"
def execute(self) -> None:
command = "sphinx-apidoc -f -o docs/devel/api src/moin {0}cd docs{0} make html".format(SEP)
print(f"Creating HTML docs... output messages written to {DOCS}.")
with open(DOCS, "w") as messages:
result = subprocess.call(command, shell=True, stderr=messages, stdout=messages)
print(f"\nSearching {DOCS}, important messages are shown below...\n")
search_for_phrase(DOCS)
if result == 0:
print("HTML docs successfully created in {}.".format(os.path.normpath("docs/_build/html")))
else:
print(
'Error: creation of HTML docs failed with return code "{}".'
' Do "{} log docs" to see complete log.'.format(result, M)
)
self.run_time()
class InstallExtras(Command):
"""
Install optional packages from `requirements.d`.
"""
key = "extras"
def execute(self) -> None:
reqs = ["requirements.d/extras.txt", "requirements.d/development.txt", "docs/requirements.txt"]
if not WINDOWS_OS:
reqs.append("requirements.d/ldap.txt")
reqs_installer = "pip install --upgrade -r "
command = SEP.join(list(reqs_installer + x for x in reqs))
print("Installing {}.".format(", ".join(reqs)))
print(f"Output messages written to {EXTRAS}.")
with open(EXTRAS, "w") as messages:
subprocess.call(command, shell=True, stderr=messages, stdout=messages)
print('\nImportant messages from {} are shown below. Do "{} log extras" to see complete log.'.format(EXTRAS, M))
search_for_phrase(EXTRAS)
self.run_time()
class RefreshInterwiki(Command):
"""
Refresh the content of file `contrib/interwiki/intermap.txt`.
"""
key = "interwiki"
def execute(self) -> None:
print("Refreshing {}...".format(os.path.normpath("contrib/interwiki/intermap.txt")))
command = "{} scripts/wget.py http://master19.moinmo.in/InterWikiMap?action=raw intermap.txt".format(
sys.executable
)
subprocess.call(command, shell=True)
class ViewLogFile(Command):
"""
View a log file with the default text editor.
"""
key = "log"
def execute(self) -> None:
logs = set(CMD_LOGS.keys())
if self.args.target and self.args.target in logs and os.path.isfile(CMD_LOGS[self.args.target]):
if WINDOWS_OS:
command = f"start {CMD_LOGS[self.args.target]}"
else:
# .format requires {{ and }} to escape { and }
command = f"${{VISUAL:-${{FCEDIT:-${{EDITOR:-less}}}}}} {CMD_LOGS[self.args.target]}"
subprocess.call(command, shell=True)
else:
self.print_help()
@staticmethod
def print_help():
"""
Print a list of log files available for viewing.
"""
print(f"usage: {M} log <target> where <target> is:\n")
choices = "{0: <16}- {1}"
logs = set(CMD_LOGS.keys())
for log in sorted(logs):
if os.path.isfile(CMD_LOGS[log]):
print(choices.format(log, CMD_LOGS[log]))
else:
print(choices.format(log, "* file does not exist"))
class CreateNewWiki(Command):
"""
Create an empty wiki.
"""
key = "new-wiki"
def execute(self) -> None:
print("Creating a new empty wiki...")
command = "moin index-create"
make_wiki(command) # share code with restore command
class Import19(Command):
"""
Import a moin 1.9 wiki.
"""
key = "import19"
def execute(self) -> None:
print("Error: Subcommand import19 not supported. Please use 'moin import19'.")
class Backup(Command):
"""
Roll 3 prior backups and create new wiki/backup.moin or backup to user specified file.
"""
key = "backup"
def execute(self) -> None:
if wiki_exists():
filename = BACKUP_FILENAME
if self.args.filename:
filename = self.args.filename
print(f"Creating a wiki backup to {filename}...")
else:
print(f"Creating a wiki backup to {filename} after rolling 3 prior backups...")
b3 = BACKUP_FILENAME.replace(".", "3.")
b2 = BACKUP_FILENAME.replace(".", "2.")
b1 = BACKUP_FILENAME.replace(".", "1.")
if os.path.exists(b3):
os.remove(b3)
for src, dst in ((b2, b3), (b1, b2), (BACKUP_FILENAME, b1)):
if os.path.exists(src):
os.rename(src, dst)
command = f"moin save --all-backends --file {filename}"
with open(BACKUPWIKI, "w") as messages:
result = subprocess.call(command, shell=True, stderr=messages, stdout=messages)
if result == 0:
print(f"Success: wiki was backed up to {filename}")
else:
print(
'Important messages from {} are shown below. Do "{} log backup" to see complete log.'.format(
BACKUPWIKI, M
)
)
search_for_phrase(BACKUPWIKI)
print("\nError: attempt to backup wiki failed.")
else:
print("Error: cannot backup wiki because it has not been created.")
self.run_time()
class Restore(Command):
"""
Create wiki and load data from wiki/backup.moin or user specified path.
"""
key = "restore"
def execute(self) -> None:
command = "moin index-create{0} moin load --file %s{0} moin index-build".format(SEP)
filename = BACKUP_FILENAME
if self.args.filename:
filename = self.args.filename
if os.path.isfile(filename):
command = command % filename
print(f"Creating a new wiki and loading it with data from {filename}...")
make_wiki(command)
else:
print(f"Error: cannot create wiki because {filename} does not exist.")
self.run_time()
class HtmlDump(Command):
"""
Create a static html dump of this wiki.
"""
key = "dump-html"
def execute(self) -> None:
if wiki_exists():
print("Creating static HTML image of wiki...")
command = "moin dump-html {}".format(" ".join(self.additional))
with open(DUMPHTML, "w") as messages:
result = subprocess.call(command, shell=True, stderr=messages, stdout=messages)
if result == 0:
print("Success: wiki was dumped to html files")
else:
print("\nError: attempt to dump wiki to html files failed.")
# always show errors because individual items may fail
print(
'Important messages from {} are shown below. Do "{} log dump-html" to see complete log.'.format(
DUMPHTML, M
)
)
search_for_phrase(DUMPHTML)
else:
print("Error: cannot dump wiki because it has not been created.")
self.run_time()
class RunSass(Command):
"""
Run sass to update basic theme CSS files.
"""
key = "css"
def execute(self) -> None:
print("Running sass to update Basic theme CSS files. This requires Node.js and NPM to be installed locally.")
build_css_dir = os.path.join("contrib", "css-build")
command = f"cd {build_css_dir}{SEP}npm install{SEP}npm run build"
print(f"running command: {command}")
result = subprocess.call(command, shell=True)
if result == 0:
print("Success: Basic theme CSS files updated.")
else:
print("Error: Basic theme CSS files update failed, see error messages above.")
class RunTests(Command):
"""
Run tests, output goes to `m-tox.txt`.
"""
key = "tests"
def execute(self) -> None:
print(f"Running tests... output written to {TOX}.")
command = "tox -- {1} > {0} 2>&1".format(TOX, " ".join(self.additional))
print(f"Test command line: {command}")
subprocess.call(command, shell=True)
print(f'Important messages from {TOX} are shown below. Do "{M} log tests" to see complete log.')
search_for_phrase(TOX)
self.run_time()
class CheckCodingStandard(Command):
"""
Correct scripts that taint the repository and clutter subsequent code reviews.
"""
key = "coding-std"
def execute(self) -> None:
print("Checking for trailing blanks, DOS line endings, Unix line endings, empty lines at eof...")
command = f"{sys.executable} scripts/coding_std.py"
subprocess.call(command, shell=True)
# not on menu, rarely used, similar code was in moin 1.9
class CreateDist(Command):
"""
Create a distribution archive in subfolder `dist/`.
"""
key = "dist"
def execute(self) -> None:
print("Deleting wiki data, then creating distribution archive in /dist, output written to {}.".format(DIST))
DeleteWiki().execute()
command = "pip install build ; python -m build"
with open(DIST, "w") as messages:
result = subprocess.call(command, shell=True, stderr=messages, stdout=messages)
print(f"Summary message from {DIST} is shown below:")
search_for_phrase(DIST)
if result == 0:
print("Success: a distribution archive was created in {}.".format(os.path.normpath("/dist")))
else:
print(
'Error: create dist failed with return code = {}. Do "{} log dist" to see complete log.'.format(
result, M
)
)
class DeleteOrigFiles(Command):
"""
Delete all files matching `*.orig`.
"""
key = "del-orig"
def execute(self) -> None:
delete_files("*.orig")
class DeletePycFiles(Command):
"""
Delete all files matching `*.pyc`.
"""
key = "del-pyc"
def execute(self) -> None:
delete_files("*.pyc")
class DeleteRejectedFiles(Command):
"""
Delete all files matching `*.rej`.
"""
key = "del-rej"
def execute(self) -> None:
delete_files("*.rej")
class DeleteWiki(Command):
"""
Create a just-in-case backup, then delete all wiki data.
"""
key = "del-wiki"
def execute(self) -> None:
command = f"moin save --all-backends --file {JUST_IN_CASE_BACKUP}"
if wiki_exists():
print(f"Creating a backup named {JUST_IN_CASE_BACKUP}; then deleting all wiki data and indexes...")
with open(DELWIKI, "w") as messages:
result = subprocess.call(command, shell=True, stderr=messages, stdout=messages)
if result != 0:
print(f"Error: backup failed with return code = {result}. Complete log is in {DELWIKI}.")
# destroy wiki even if backup fails
if os.path.isdir("wiki/data") or os.path.isdir("wiki/index"):
shutil.rmtree("wiki/data")
shutil.rmtree("wiki/index")
if os.path.isdir("wiki/preview"):
shutil.rmtree("wiki/preview")
if os.path.isdir("wiki/sql"):
shutil.rmtree("wiki/sql")
print("Wiki data successfully deleted.")
else:
print("Wiki data not deleted because it does not exist.")
self.run_time()
class DeleteAll(Command):
"""
Same as running the 4 del-* commands below.
"""
key = "del-all"
def execute(self) -> None:
DeleteOrigFiles().execute()
DeletePycFiles().execute()
DeleteRejectedFiles().execute()
DeleteWiki().execute()
COMMANDS: list[type[Command]] = [
QuickInstall,
CreateVirtualEnv,
GenerateDocs,
InstallExtras,
RefreshInterwiki,
ViewLogFile,
CreateNewWiki,
Import19,
Backup,
Restore,
HtmlDump,
RunSass,
RunTests,
CheckCodingStandard,
CreateDist,
DeleteAll,
DeleteOrigFiles,
DeletePycFiles,
DeleteRejectedFiles,
DeleteWiki,
]
"""
Each cmd_ method processes a choice on the menu.
"""
class CommandArgumentParser(argparse.ArgumentParser):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self.moin_command = None
def print_usage(self, file=None) -> None:
if self.moin_command and hasattr(self.moin_command, "print_help"):
self.moin_command.print_help()
else:
super().print_usage(file)
class CustomArgumentParser(argparse.ArgumentParser):
def __init__(self, *args, **kwargs):
self.default_subparser = kwargs.pop("default_subparser", None)
super().__init__(*args, **kwargs)
def parse_known_args(self, args=None, namespace=None):
if args is None:
args = sys.argv[1:]
if (not args or args[0].startswith("-")) and self.default_subparser:
args = [self.default_subparser] + args
return super().parse_known_args(args, namespace)
def print_usage(self, file=None):
if file is None:
file = sys.stdout
file.write(help)
def already_installed() -> bool:
marker_file = "m.bat" if IS_WIN else "m"
return os.path.isfile(marker_file)
def create_args_parser() -> argparse.ArgumentParser:
# default to perform a moin quick installation if moin wasn't already installed
toplevel_parser = CustomArgumentParser(default_subparser=None if already_installed() else QuickInstall.key)
subparsers = toplevel_parser.add_subparsers(dest="command", parser_class=CommandArgumentParser)
parser = subparsers.add_parser(QuickInstall.key)
parser.add_argument("--venv", required=False, default=None)
parser = subparsers.add_parser(CreateVirtualEnv.key)
parser.add_argument("--source", required=True)
parser.add_argument("--name", required=False, default=".venv")
parser = subparsers.add_parser(InstallExtras.key)
parser = subparsers.add_parser(GenerateDocs.key)
parser = subparsers.add_parser(RefreshInterwiki.key)
parser = subparsers.add_parser(ViewLogFile.key)
parser.add_argument("target", choices=CMD_LOGS.keys())
parser = subparsers.add_parser(CreateNewWiki.key)
parser = subparsers.add_parser(Restore.key)
parser.add_argument("--filename", required=False)
parser = subparsers.add_parser(Backup.key)
parser.add_argument("--filename", required=False)
parser = subparsers.add_parser(HtmlDump.key)
parser = subparsers.add_parser(RunSass.key)
parser = subparsers.add_parser(RunTests.key)
parser = subparsers.add_parser(CheckCodingStandard.key)
parser = subparsers.add_parser(DeleteAll.key)
parser = subparsers.add_parser(DeleteOrigFiles.key)
parser = subparsers.add_parser(DeletePycFiles.key)
parser = subparsers.add_parser(DeleteRejectedFiles.key)
parser = subparsers.add_parser(DeleteWiki.key)
return toplevel_parser
def usage() -> None:
print(help)
def main() -> None:
parser = create_args_parser()
args, remainder = parser.parse_known_args()
# no command given => show usage information
if not args.command:
usage()
sys.exit(1)
command = next((cmd for cmd in COMMANDS if cmd.key == args.command), None)
assert command
print(f"command = {command} args = {args!r}")
# Some commands expect a virtual environment to be present when executing a command
# in a child process.
# Make sure to run ". activate" in your command shell.
command(args, remainder).execute()
if __name__ == "__main__":
main()