-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
setup.py
1465 lines (1234 loc) · 51.2 KB
/
setup.py
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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""\
Python script used to provide development support functions.
"""
# Copyright (C) 2020-2023 Bob Swift
# Copyright (C) 2021 Philipp Wolfer
# pylint: disable=too-many-lines
# pylint: disable=wrong-import-order
import argparse
import glob
import os
import re
import shutil
import subprocess
from subprocess import SubprocessError
import sys
import time
import zipfile
import conf
import tag_mapping
import restructuredtext_lint
SCRIPT_NAME = 'Picard Docs Builder'
SCRIPT_VERS = '0.18'
SCRIPT_COPYRIGHT = '2021-2023'
SCRIPT_AUTHOR = 'Bob Swift'
PACKAGE_NAME = 'picard-docs'
PACKAGE_TITLE = 'Picard Docs'
OUTPUT_DIR = 'docs'
BASE_FILE_NAME = conf.base_filename if hasattr(conf, 'base_filename') and conf.base_filename else 'musicbrainzpicard'
FILE_NAME_ROOT = 'MusicBrainz_Picard'
TAG_MAP_NAME = FILE_NAME_ROOT + '_Tag_Map'
CURRENT_VERSION = conf.version
######################
# Linter Options #
######################
IGNORE_INFO_MESSAGES = False
FAIL_ON_WARNINGS = True
PYTHON_FILES_TO_CHECK = [
'setup.py',
'conf.py',
'tag_mapping.py',
'_extensions/*.py',
'gitstage.py',
]
#################################################################
# Sphinx Directives and Roles to ignore while lint checking #
#################################################################
IGNORE_DIRECTIVES = [
# Table of Contents
'toctree',
# Paragraph Level Markup
'note', 'warning', 'versionadded', 'versionchanged', 'deprecated',
'seealso', 'rubic', 'centered', 'hlist',
# Showing Code Examples
'highlight', 'code-block', 'literalinclude',
# Glossary
'glossary',
# Meta-information Markup
'sectionauthor', 'codeauthor',
# Index-generating Markup
'index',
# Including content based on tags
'only',
# Tables
'tabularcolumns',
# Math
'math',
# Grammar production displays
'productionlist',
# Command line parameter docs
'option', 'describe',
# Specialty directives
'youtube',
]
IGNORE_ROLES = [
# Cross-referencing
'any', 'ref', 'doc', 'download', 'numref', 'envar', 'token',
'keyword', 'option', 'term', 'index',
# Math
'math', 'eq',
# Semantic Markup
'abbr', 'command', 'dfn', 'file', 'guilabel', 'kbd', 'mailheader',
'makevar', 'manpage', 'menuselection', 'mimetype', 'newsgroup',
'program', 'regexp', 'samp', 'pep', 'rfc',
]
IGNORE_LEXERS = [
'taggerscript',
]
################################################
# RE Tests for Sphinx Roles and Directives #
################################################
RE_TEST_DIRECTIVE_1 = re.compile(r'^No directive entry for "([^"]+)')
RE_TEST_DIRECTIVE_2 = re.compile(r'^.*directive type "([^"]+)".*$')
RE_TEST_DIRECTIVE_3 = re.compile(r'^.*No Pygments lexer found for "([^"]+)".*$')
RE_TEST_ROLE_1 = re.compile(r'^No role entry for "([^"]+)')
RE_TEST_ROLE_2 = re.compile(r'^.*role "([^"]+)".*$')
RE_TEST_LANGUAGE = re.compile(r'^[a-z]{2}(_[A-Z]([A-Z]{1}|[a-z]{3}){1})?$')
##################################################
# Text to display when the script is started #
##################################################
COPYRIGHT_TEXT = f"""\
{SCRIPT_NAME} (v{SCRIPT_VERS}) Copyright (C) {SCRIPT_COPYRIGHT}, {SCRIPT_AUTHOR}
"""
ABOUT_TEXT = f"""\
{COPYRIGHT_TEXT}
This program provides some utility functions to aid in the development
of the {PACKAGE_TITLE} package.
For usage instructions, please use the '--help' option.
This program comes with ABSOLUTELY NO WARRANTY; for details use command
'info warranty'. This is free software, and you are welcome to redistribute
it under certain conditions. Please see the GPLv3 license for details.
"""
WARRANTY_TEXT = f"""\
{COPYRIGHT_TEXT}
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT
WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF
THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR
CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES
SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE
WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
If the disclaimer of warranty and limitation of liability provided above
cannot be given local legal effect according to their terms, reviewing
courts shall apply local law that most closely approximates an absolute
waiver of all civil liability in connection with the Program, unless a
warranty or assumption of liability accompanies a copy of the Program in
return for a fee.
"""
DESCRIPTION = f"{SCRIPT_NAME} (v{SCRIPT_VERS})"
HELP = f"""\
Usage: {os.path.basename(os.path.realpath(__file__))} [optional arguments] command [command args]
Commands:
clean Clean the specified target directory
build Build the specified target files
test Run the specified tests
info about Information about the script
info warranty Warranty information about the script
info languages Display list of supported languages
Optional Arguments:
-l LANGUAGE Specify language for processing
-h, --help Show this help message and exit
"""
########################################
# Documentation Languages to Build #
########################################
LANGUAGE_NAMES = {
'bg': 'Bulgarian',
'cs': 'Czech',
'da': 'Danish',
'de': 'German',
'el': 'Greek',
'en': 'English',
'es': 'Spanish',
'et': 'Estonian',
'fi': 'Finnish',
'fr': 'French',
'hr': 'Croatian',
'hu': 'Hungarian',
'it': 'Italian',
'ja': 'Japanese',
'lt': 'Lithuanian',
'lv': 'Latvian',
'nb': 'Norwegian (Bokmål)',
'nl': 'Dutch',
'no': 'Norwegian',
'pl': 'Polish',
'pt': 'Portuguese',
'ro': 'Romanian',
'ru': 'Russian',
'sk': 'Slovak',
'sl': 'Slovenian',
'sv': 'Swedish',
'tr': 'Turkish',
'zh': 'Chinese',
}
LANGUAGE_LIST = {
'en': 'English',
}
if conf.supported_languages:
for code, title in conf.supported_languages:
LANGUAGE_LIST[code] = LANGUAGE_NAMES[code[:2]] if code[:2] in LANGUAGE_NAMES else title
DEFAULT_LANGUAGE = conf.default_language if conf.default_language else 'en'
LANGUAGES = list(LANGUAGE_LIST.keys())
################################################
# Custom exceptions used within the script #
################################################
class CustomError(Exception):
"""Custom base exception for the script.
"""
def __init__(self, message='Setup Script Error'):
self.message = message
super().__init__(self.message)
class CreateDirectoryError(CustomError):
"""Exception when creating a directory.
"""
def __init__(self, message='Create Directory Error'):
self.message = message
super().__init__(self.message)
class CleanDirectoryError(CustomError):
"""Exception when cleaning a directory.
"""
def __init__(self, message='Clean Directory Error'):
self.message = message
super().__init__(self.message)
class RemoveDirectoryError(CustomError):
"""Exception when removing a directory.
"""
def __init__(self, message='Remove Directory Error'):
self.message = message
super().__init__(self.message)
class RemoveFileError(CustomError):
"""Exception when removing a file.
"""
def __init__(self, message='Remove File Error'):
self.message = message
super().__init__(self.message)
class ErrorLintRST(): # pylint: disable=too-few-public-methods
"""Special LintRST error for unhandled exceptions.
"""
def __init__(self, err_type='INFO', err_line=0, err_message=''):
self.type = err_type
self.line = err_line
self.message = err_message
class SPHINX_(): # pylint: disable=too-few-public-methods
"""Sphinx constants used when building the documentation.
"""
OPTS = ''
BUILD = 'sphinx-build'
INTL = 'sphinx-intl'
BUILD_DIR = '_build'
SOURCE_DIR = '.'
LOCALE_DIR = conf.locale_dirs[0] if conf.locale_dirs else '_locale'
GETTEXT_DIR = os.path.join(LOCALE_DIR, 'gettext')
BUILD_TIMEOUT = 300
BUILD_TARGETS = {
'html': {'dir': 'html', 'cmd': 'html', 'extra': ''},
'epub': {'dir': 'epub', 'cmd': 'epub', 'extra': '-D master_doc=epub'},
'pdf': {'dir': 'latex', 'cmd': 'latex', 'extra': ''},
}
def exit_with_code(exit_code=0):
"""Print and exit with the specified exit code.
Keyword Arguments:
exit_code {int} -- Exit code to use (default: 0)
"""
print(f'\nExit Code: {exit_code}\n')
sys.exit(exit_code)
def python_files_to_check():
"""Provide expanded list of python files to check.
Yields:
str: Path and name of file.
"""
for filepath in PYTHON_FILES_TO_CHECK:
for filename in glob.glob(filepath, recursive=True):
yield filename
class LintRST():
"""Lint the restructured text (RST) files.
"""
def __init__(self):
"""Provides an instance of the "restructuredtext-lint" linter.
"""
self.checked_count = 0
self.warning_count = 0
self.error_count = 0
self.info_count = 0
self.untested = 0
self.linter = restructuredtext_lint
def process_error(self, err):
"""Print the error information and increment the appropriate error counter.
Args:
err (restructuredtext_lint error): Restructured text linter error object.
"""
print(f'\n [{err.type}] Line {err.line}: {err.message}', end='', flush=True)
if err.type == 'WARNING':
self.warning_count += 1
elif err.type == 'INFO':
self.info_count += 1
elif err.type == 'UNTESTED':
self.untested += 1
else:
# Includes 'ERROR' and 'SEVERE'
self.error_count += 1
def check_file(self, file_name, ignore_info=True): # pylint: disable=too-many-branches
"""Lint check the specified file, printing the findings to the console.
Arguments:
file_name {str} -- Path and name of the file to check
Keyword Arguments:
ignore_info {bool} -- Determines whether INFO notices should be ignored (default: {True})
"""
print(f"{' ' * 79}\r{file_name}", end='', flush=True)
self.checked_count += 1
if not os.path.isfile(file_name):
self.process_error(ErrorLintRST('ERROR', 0, 'File not found'))
return
try:
err_processed = False
errs = self.linter.lint_file(file_name)
if not errs:
errs = []
for err in errs:
err_process = True
if err.type == 'INFO':
if ignore_info:
# Ignore this error message and continue testing
continue
m = RE_TEST_DIRECTIVE_1.match(err.message)
if m and m.group(1) in IGNORE_DIRECTIVES:
continue
m = RE_TEST_ROLE_1.match(err.message)
if m and m.group(1) in IGNORE_ROLES:
continue
elif err.type == 'WARNING':
m = RE_TEST_DIRECTIVE_3.match(err.message)
if m and m.group(1) in IGNORE_LEXERS:
continue
elif err.type in {'ERROR', 'SEVERE'}:
m = RE_TEST_DIRECTIVE_2.match(err.message)
if m and m.group(1) in IGNORE_DIRECTIVES:
continue
m = RE_TEST_ROLE_2.match(err.message)
if m and m.group(1) in IGNORE_ROLES:
continue
if err_process:
err_processed = True
self.process_error(err)
print('\n\n' if err_processed else f"\r{' ' * 79}\r", end='', flush=True)
except UnicodeDecodeError:
err = ErrorLintRST('UNTESTED', 0, 'Unable to check because of unmapped unicode characters.\n')
self.process_error(err)
except IOError as ex:
self.process_error(ErrorLintRST('ERROR', 0, f'Error reading file. ({ex})'))
def check(self, root_dir, ignore_info=False, fail_on_warnings=False):
"""Check all files in the specified directory, including files in subdirectories.
Arguments:
root_dir {str} -- Path to the root directory to check
Keyword Arguments:
ignore_info {bool} -- Determines whether INFO notices should be ignored (default: {False})
fail_on_warnings {bool} -- Determines whether warnings will cause the check to return a failed status (default: {False})
Returns:
{int} -- Error code 1 if check failed, otherwise 0.
"""
for dir_name, subdir_list, file_list in os.walk(root_dir): # pylint: disable=unused-variable
for fname in file_list:
if str(fname).lower().endswith('.rst'):
self.check_file(os.path.join(dir_name, fname), ignore_info)
text = (
f'Checked {self.checked_count:,} files. '
f'Errors: {self.error_count:,} '
f'Warnings: {self.warning_count:,} '
f'Untested: {self.untested:,} '
) + ('' if ignore_info else f'Info: {self.info_count:,}')
print(f"{text.strip()}")
err = self.error_count + (self.warning_count if fail_on_warnings else 0)
return 1 if err > 0 else 0
##############################################################################
class POCheck():
"""Check for restructured text errors in the translation *.po files.
"""
def __init__(self):
"""Provides an instance of the "POCheck" class.
"""
self.warning_count = 0
self.file_count = 0
self.line_count = 0
self.bad_files = []
self.filename = ''
self.filename_written = False
def get_lines(self, file_lines):
"""Assemble the content into full lines for testing.
Args:
file_lines (list): List of the lines read from the *.po file
Returns:
dict: Dictionary of assembled lines by starting line number in the file.
"""
tx_lines = {}
line_number = 0
build_line = False
temp_line_text = ''
temp_line_number = 0
for file_line in file_lines:
line_number += 1
if build_line:
if re.match(r'^".*"$', file_line):
temp_line_text += file_line[1:-2]
else:
tx_lines[temp_line_number] = temp_line_text
build_line = False
if file_line and not build_line:
if re.match(r'^(msgid|msgstr)', file_line):
temp_line_number = line_number
temp_line_text = ''
build_line = True
if build_line: # Catch case where line ends on the last line of a msgstr entry
tx_lines[temp_line_number] = temp_line_text
self.line_count += len(tx_lines)
return tx_lines
def write_warning(self, message):
"""Add message to file processing output.
Args:
message (str): Message to add
"""
if not self.filename_written:
self.bad_files.append('')
self.bad_files.append(f'File: {self.filename}')
self.filename_written = True
self.warning_count += 1
self.bad_files.append(' ' + message)
def check_line(self, line_number, content): # pylint: disable=too-many-branches
"""Check the line for restructured-text errors.
Args:
filename (str): Full path of the file being checked
line_number (int): Line number of the start of the assembled line
content (str): The assembled line to check
"""
if re.search(r"`[^`_]+`\s+_", content) or re.search(r"`[^`']*'\s*_", content):
self.write_warning(f'[L{line_number}]: Link')
for item in IGNORE_DIRECTIVES:
if re.search(r"\.\.\s+" + item + r"\s+::", content):
self.write_warning(f'[L{line_number}]: Directive "{item}"')
for item in IGNORE_ROLES:
if re.search(r":\s+" + item + r":", content) \
or re.search(r":" + item + r"\s+:", content) \
or re.search(r":" + item + r":\s+`", content) \
or re.search(r"[^\:]+" + item + r":`", content):
self.write_warning(f'[L{line_number}]: Role "{item}"')
links = re.findall(r"`([^`]*)`_", content)
for item in ['doc', 'download', 'numref', 'ref']:
links.extend(re.findall(r":" + item + r":`([^`]*)`", content))
for item in links:
if (
item.count('<') != item.count('>')
or '<>' in item
or re.search(r"\S<", ' ' + item)
or re.search(r"<\S*\s+.*>", item)
):
self.write_warning(f'[L{line_number}]: Link "{item}"')
indices = re.findall(r":index:`[^<]*<([^>]*)>", content)
for item in indices:
if re.search(r"\s+(,|;)", item):
self.write_warning(f'[L{line_number}]: Index "{item}"')
backticks = content.count('`') % 2
if backticks:
self.write_warning(f'[L{line_number}]: Backtick Mismatch')
return
backticks_open = False
lastchar = ''
nextchar = ''
ccount = 0
for char in content:
ccount += 1
if char == '`':
backticks_open = not backticks_open
nextchar = content[ccount] if len(content) > ccount else ''
if backticks_open:
# if lastchar and lastchar not in ' :"\'' and nextchar and nextchar != '`':
if lastchar and nextchar and nextchar != '`' and re.search(r"[a-zA-Z0-9]+", lastchar):
self.write_warning(f'[L{line_number},C{ccount}]: Backtick Open "{lastchar + char + nextchar}"')
break
else:
if lastchar != '`' and nextchar and re.search(r"[a-zA-Z0-9]+", nextchar):
self.write_warning(f' [L{line_number},C{ccount}]: Backtick Close "{lastchar + char + nextchar}"')
break
lastchar = char
def check(self, locale_dir, filetype='po'):
"""Check all translation *.po files in the specified directory and subdirectories.
"""
if not (os.path.exists(locale_dir) and os.path.isdir(locale_dir)):
return
print(f"\nTesting restructured-text in *.po files.\nStarting root directory: {locale_dir}\n")
if os.path.isdir(locale_dir):
for dir_name, _subdir_list, file_list in os.walk(locale_dir):
for file_name in file_list:
if re.match(r'.*\.' + filetype + '$', file_name, re.IGNORECASE):
self.file_count += 1
filename = os.path.join(dir_name, file_name)
self.filename_written = False
print(f"{(filename + ' ' * 80)[:79]}\r", end='', flush=True)
self.filename = filename
content = {}
with open(filename, 'r', encoding='utf8') as f:
content = self.get_lines(f.readlines())
for key in sorted(content):
self.check_line(key, content[key])
print(' ' * 79 + '\r', end='', flush=True)
print(f"Checked {self.line_count:,} lines in {self.file_count:,} files. Found {self.warning_count:,} issues to check.")
if self.bad_files:
print("\nCheck the following for errors:")
for item in self.bad_files:
print(item)
# print()
if self.warning_count:
exit_with_code(1 if self.warning_count else 0)
def show_help():
"""Print the help screen.
"""
print(f"\n{HELP}")
def parse_command_line():
"""Parse the command line arguments.
"""
arg_parser = argparse.ArgumentParser(description=DESCRIPTION)
arg_parser.add_argument(
'-l', '--language',
action='store',
nargs='?',
default='en',
const='en',
metavar='LANGUAGE',
dest='language',
help="specify language for processing"
)
subparsers = arg_parser.add_subparsers()
parser01 = subparsers.add_parser(
'test',
help='Test the files'
)
parser01.add_argument(
'test_targets',
action='store',
nargs='+',
type=str,
choices=['rst', 'sphinx', 'po', 'flake8', 'pylint', 'isort', 'python'],
help="rst = lint check the rst files, "
"sphinx = test build of the *.rst files "
"po = rudimentary test of RST in *.po files "
"flake8 = test python files with flake8, "
"pylint = test python files with pylint, "
"isort = check python files import sorting, "
"python = all python tests (isort, flake8 and pylint)"
)
parser02 = subparsers.add_parser(
'build',
help='Build the files'
)
parser02.add_argument(
'build_targets',
action='store',
nargs='+',
choices=['map', 'pot', 'po', 'html', 'pdf', 'epub', 'all'],
help="map = build tag map files, "
"pot = build translation template files, "
"po = build translation files, "
"html = build html files, "
"pdf = build pdf file, "
"epub = build epub file, "
"all = build all files"
)
parser03 = subparsers.add_parser(
'clean',
help='Reset the build directories'
)
parser03.add_argument(
'clean_targets',
action='store',
nargs='+',
choices=['mo', 'html', 'pdf', 'epub', 'all'],
help="mo = remove all compiled MO files, "
"html = clean html build directory, "
"pdf = clean pdf build directory, "
"epub = clean epub build directory, "
"all = clean all build targets"
)
parser04 = subparsers.add_parser(
'info',
help='Display project information'
)
parser04.add_argument(
'info_type',
action='store',
choices=['about', 'languages', 'warranty'],
help="about = info about the script, "
"languages = list of supported languages, "
"warranty = warranty of the script"
)
args = arg_parser.parse_args()
return args
def run_sphinx_test(language=''):
"""Perform a trial build using Sphinx with all warnings enabled.
"""
print('\nSphinx test build.\n')
if not (language and language in LANGUAGES):
language = DEFAULT_LANGUAGE
if language == DEFAULT_LANGUAGE:
language_option = ''
else:
language_option = '-D language=' + language
# update_po_files_for_language(language)
junk_dir = '_junk'
command = f"{SPHINX_.BUILD} -b dummy -W --keep-going {SPHINX_.SOURCE_DIR} {junk_dir} {language_option}".strip().replace(' ', ' ')
exit_code = subprocess.run(command, shell=True, check=False, capture_output=False, timeout=SPHINX_.BUILD_TIMEOUT).returncode
remove_dir(junk_dir)
if exit_code:
exit_with_code(exit_code)
print()
def run_lint(root_dir, ignore_info=False, fail_on_warnings=False):
"""Check the RST files in the specified directory and subdirectories.
Arguments:
root_dir {str} -- Path to the root directory to check
Keyword Arguments:
ignore_info {bool} -- Determines whether INFO notices should be ignored (default: {False})
fail_on_warnings {bool} -- Determines whether warnings will cause the check to return a failed status (default: {False})
"""
# # ---------------------------------------------------------------------
# # Example code from https://pypi.org/project/restructuredtext-lint/
# # Load in our dependencies
# from docutils.parsers.rst.directives import register_directive
# from sphinx.directives.code import Highlight
# import restructuredtext_lint
#
# # Load our new directive
# register_directive('highlight', Highlight)
#
# # Lint our README
# errors = restructuredtext_lint.lint_file('docs/sphinx/README.rst')
# print errors[0].message # Error in "highlight" directive: no content permitted.
# # ---------------------------------------------------------------------
print(f'\nLint checking all RST files starting in the "{root_dir}" directory.\n')
linter = LintRST()
err = linter.check(root_dir, ignore_info, fail_on_warnings)
exit_code = 1 if err > 0 else 0
if exit_code:
exit_with_code(exit_code)
def run_pylint():
"""Check the Python code using pylint.
"""
print("\nChecking the python files with pylint.\n")
exit_code = 0
for filename in python_files_to_check():
print(f'pylint: {filename}', end='', flush=True)
command = f'pylint {filename}'
result = subprocess.run(command, shell=True, check=False, capture_output=True, timeout=SPHINX_.BUILD_TIMEOUT)
exit_code = max(exit_code, result.returncode)
text = ['[Error]']
text.extend(result.stdout.decode('utf-8').splitlines()[:-4])
text = ('\n'.join(text) + '\n') if result.returncode else '[Okay]'
print(f" {text}")
if exit_code:
exit_with_code(exit_code)
def run_flake8():
"""Check the Python code using flake8.
"""
print("\nChecking the python files with flake8.\n")
exit_code = 0
for filename in python_files_to_check():
print(f'flake8: {filename}', end='', flush=True)
command = f'flake8 {filename}'
result = subprocess.run(command, shell=True, check=False, capture_output=True, timeout=SPHINX_.BUILD_TIMEOUT)
exit_code = max(exit_code, result.returncode)
text = ['[Error]']
text.extend(result.stdout.decode('utf-8').splitlines()[:-1])
text = ('\n'.join(text) + '\n') if result.returncode else '[Okay]'
print(f" {text}")
if exit_code:
exit_with_code(exit_code)
def run_isort():
"""Check the Python code using isort.
"""
print("\nChecking the python files with isort.\n")
exit_code = 0
for filename in python_files_to_check():
print(f'isort: {filename}', end='', flush=True)
command = f'isort -c {filename}'
result = subprocess.run(command, shell=True, check=False, capture_output=True, timeout=SPHINX_.BUILD_TIMEOUT)
print(('\n' + result.stderr.decode('utf-8').strip() + '\n') if result.returncode else ' [Okay]')
exit_code = max(exit_code, result.returncode)
if exit_code:
exit_with_code(exit_code)
def get_major_minor(version):
"""Extract the major.minor portion of the supplied version string.
Args:
version (str): Version string to process
Returns:
str: Major.minor portion of the version string, or '' if invalid version
"""
if re.match(r'^(v?[0-9][0-9\.]*)', version):
parts = re.match(r'^v?([0-9][0-9\.]*)', version).group(1).split('.')
parts.append('')
return f"{int('0' + parts[0])}.{int('0' + parts[1])}"
return ''
def create_directory(dir_path, dir_name):
"""If the specified directory does not exist, it will be created. Includes multiple
checks for success to accommodate race condition in Windows.
Arguments:
dir_path {str} -- Path to the directory to create
dir_name {str} -- Name of the directory type (e.g.: 'html')
"""
if not os.path.exists(dir_path):
try:
print(f'Creating the {dir_name} directory: {dir_path}')
os.makedirs(dir_path)
counter = 50
# Multiple checks for success to accommodate race condition in Windows
while counter and not os.path.exists(dir_path):
counter -= 1
time.sleep(.2)
if not counter:
raise CreateDirectoryError
except (CustomError, OSError) as ex:
print(f"\nError creating the {dir_name} directory: {dir_path}")
print(f"Error message: {ex}\n")
exit_with_code(1)
def clean_directory(dir_path, dir_name, create_dir=True):
"""Removes all files and subdirectories for the specified directory. If the specified
directory does not exist, it will be created. Includes multiple checks for success to
accommodate race condition in Windows.
Arguments:
dir_path {str} -- Path to the directory to clean
dir_name {str} -- Name of the directory type (e.g.: 'html')
"""
if os.path.exists(dir_path):
if os.path.isdir(dir_path):
print(f'Emptying the {dir_name} directory: {dir_path}')
if not os.listdir(dir_path):
return
err = None
tries = 5
while tries > 0:
tries -= 1
err = None
if os.path.exists(dir_path):
try:
shutil.rmtree(dir_path)
except (shutil.Error, PermissionError, OSError) as ex:
err = ex
time.sleep(.2)
continue
# Multiple checks for success to accommodate race condition in Windows
counter = 50
while counter and os.path.exists(dir_path):
counter -= 1
time.sleep(.2)
if not counter:
err = CleanDirectoryError()
continue
tries = 0
if err:
print(f"\nError removing the {dir_name} directory: {dir_path}")
print(f"Error message: {err}\n")
exit_with_code(1)
else:
print(f"\nThe {dir_name} directory is not a directory: {dir_path}\n")
exit_with_code(1)
if create_dir and not os.path.exists(dir_path):
create_directory(dir_path=dir_path, dir_name=dir_name)
def remove_dir(dir_path):
"""Remove the specified directory. Includes multiple checks for success to accommodate race
condition in Windows.
Arguments:
dir_path {str} -- Path of directory to remove
"""
if os.path.exists(dir_path):
if os.path.isdir(dir_path):
clean_directory(dir_path, 'temporary build', create_dir=False)
err = None
tries = 5
while tries > 0:
tries -= 1
err = None
if os.path.exists(dir_path):
try:
os.rmdir(dir_path)
except (shutil.Error, PermissionError) as ex:
err = ex
time.sleep(.2)
continue
counter = 50
# Multiple checks for success to accommodate race condition in Windows
while counter and os.path.exists(dir_path):
counter -= 1
time.sleep(.2)
if not counter:
err = RemoveDirectoryError()
continue
tries = 0
if err:
print(f"\nError removing the directory: {dir_path}")
print(f"Error message: {err}\n")
exit_with_code(1)
else:
print(f'\nUnable to remove (not a directory): {dir_path}\n')
exit_with_code(1)
def remove_file(file_path):
"""Removes the specified file. Includes multiple checks for success to accommodate race
condition in Windows.
Arguments:
file_path {str} -- File to remove.
"""
if os.path.exists(file_path):
if os.path.isfile(file_path):
err = None
tries = 5
while tries > 0:
tries -= 1
err = None
if os.path.exists(file_path):
try:
os.remove(file_path)
except (shutil.Error, PermissionError) as ex:
err = ex
time.sleep(.2)
continue
counter = 50
# Multiple checks for success to accommodate race condition in Windows
while counter and os.path.exists(file_path):
counter -= 1
time.sleep(.2)
if not counter:
err = RemoveFileError()
continue
tries = 0
if err:
print(f"\nError removing the file: {file_path}")
print(f"Error message: {err}\n")
exit_with_code(1)
else:
print(f'\nUnable to remove (not a file): {file_path}\n')
exit_with_code(1)
def save_version_info():
"""Save version and language information to a __init__.py file in the docs directory
so that it can be imported and used in the publish.py script. This is used to create
index.html files for use in the top and version directories, as well as the
version_links.js file to provide the list of selectable versions in the options section
of each web page.
"""
create_directory(OUTPUT_DIR, 'Output')
file_name = os.path.join(OUTPUT_DIR, '__init__.py')
remove_file(file_name)
print(f"Saving: {file_name}")
with open(file_name, 'w', encoding='utf8') as ofile:
ofile.write(
"#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n"
'"""Version and Language information used for publishing the documentation.\n"""\n\n'
"# This file is automatically generated.\n\n"
f"VERSION = {repr(conf.version)}\n"
f"DEFAULT_LANGUAGE = {repr(DEFAULT_LANGUAGE)}\n"
f"LANGUAGES = {repr(LANGUAGES)}\n\n"
f"FILE_NAME_ROOT = {repr(FILE_NAME_ROOT)}\n"
f"TAG_MAP_NAME = {repr(TAG_MAP_NAME)}\n"
)
def run_command(command):
"""Executes a command in a subprocess.
Args:
command (str): Command to execute, including all command line parameters.
"""
print(f'\nExecuting command: {command}\n')
try: