Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 13 Jun 2021 12:49:41 GMT
From:      Ceri Davies <ceri@FreeBSD.org>
To:        doc-committers@FreeBSD.org, dev-commits-doc-all@FreeBSD.org
Subject:   git: c4db9c153e - main - tools/books*py: improve argument handling of DOC_LANG, and others
Message-ID:  <202106131249.15DCnf0b029039@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by ceri:

URL: https://cgit.FreeBSD.org/doc/commit/?id=c4db9c153e4664a3eec66a68de305a57845c4a5a

commit c4db9c153e4664a3eec66a68de305a57845c4a5a
Author:     Ceri Davies <ceri@FreeBSD.org>
AuthorDate: 2021-06-13 12:45:07 +0000
Commit:     Ceri Davies <ceri@FreeBSD.org>
CommitDate: 2021-06-13 12:45:07 +0000

    tools/books*py: improve argument handling of DOC_LANG, and others
    
    1) fix shebang lines;
    2) add a -o option that makes no changes but prints the files that
        would be updated/created;
    3) allow the -l argument (and therefore DOC_LANG) to be whitespace
        separated, comma separated, or a mixture of both
    
    Item 2 allows us to start tracking dependencies in Makefiles if we wish
    and therefore reintroduce incremental builds, reducing the amount of
    .PHONY targets that are not truly phony.
    
    Item 3 restores compability with the documented DOC_LANG variable, which
    was previously whitespace separated.
    
    Reviewed by:    dbaio, blackend
    Differential Revision:  https://reviews.freebsd.org/D30732
---
 documentation/tools/books-toc-creator.py          | 30 +++++++++++-----
 documentation/tools/books-toc-examples-creator.py | 31 +++++++++++-----
 documentation/tools/books-toc-figures-creator.py  | 33 +++++++++++------
 documentation/tools/books-toc-parts-creator.py    | 43 ++++++++++++++++-------
 documentation/tools/books-toc-tables-creator.py   | 30 +++++++++++-----
 5 files changed, 117 insertions(+), 50 deletions(-)

diff --git a/documentation/tools/books-toc-creator.py b/documentation/tools/books-toc-creator.py
index 340deec339..bd85b7e923 100644
--- a/documentation/tools/books-toc-creator.py
+++ b/documentation/tools/books-toc-creator.py
@@ -1,11 +1,11 @@
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 """
 BSD 2-Clause License
 Copyright (c) 2020-2021, The FreeBSD Project
 Copyright (c) 2020-2021, Sergio Carlavilla <carlavilla@FreeBSD.org>
-This script will generate the Table of Contents of the Handbook
+This script will generate the Table of Contents of the books.
 """
-#!/usr/bin/env python3
 
 import sys, getopt
 import re
@@ -124,19 +124,28 @@ def checkIsAppendix(chapterContent):
 
 def main(argv):
 
+  justPrintOutput = False
+  langargs = []
   try:
-    opts, args = getopt.getopt(argv,"hl:",["language="])
+    opts, args = getopt.gnu_getopt(argv,"hl:o",["language="])
   except getopt.GetoptError:
-    print('books-toc-creator.py -l <language>')
+    print('books-toc-creator.py [-o] -l <language>')
     sys.exit(2)
   for opt, arg in opts:
     if opt == '-h':
-      print('books-toc-creator.py -l <language>')
+      print('books-toc-creator.py [-o] -l <language>')
       sys.exit()
+    elif opt == '-o':
+      justPrintOutput = True
     elif opt in ("-l", "--language"):
-      languages = arg.split(',')
+      langargs = arg.replace(" ",",").split(',')
+
+  # treat additional arguments as languages, but check if they
+  #  contain ','
+  for l in args:
+    langargs.extend(l.replace(" ",",").split(','))
 
-  for language in languages:
+  for language in langargs:
 
     with open('./content/{}/books/books.adoc'.format(language), 'r', encoding = 'utf-8') as booksFile:
       books = [line.strip() for line in booksFile]
@@ -199,8 +208,11 @@ def main(argv):
 
         toc += "--\n"
 
-        with open('./content/{0}/books/{1}/toc.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile:
-          tocFile.write(toc)
+        if justPrintOutput == False:
+          with open('./content/{0}/books/{1}/toc.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile:
+            tocFile.write(toc)
+        else:
+          print('./content/{0}/books/{1}/toc.adoc'.format(language, book))
 
 if __name__ == "__main__":
   main(sys.argv[1:])
diff --git a/documentation/tools/books-toc-examples-creator.py b/documentation/tools/books-toc-examples-creator.py
index f72ffa945e..423239c30d 100644
--- a/documentation/tools/books-toc-examples-creator.py
+++ b/documentation/tools/books-toc-examples-creator.py
@@ -1,3 +1,4 @@
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 """
 BSD 2-Clause License
@@ -5,9 +6,9 @@ BSD 2-Clause License
 Copyright (c) 2020-2021, The FreeBSD Project
 Copyright (c) 2020-2021, Sergio Carlavilla <carlavilla@FreeBSD.org>
 
-This script will generate the Table of Contents of the Handbook
+This script will generate the Table of Contents of any [example]s in the
+books.
 """
-#!/usr/bin/env python3
 
 import sys, getopt
 import re
@@ -54,19 +55,28 @@ def setTOCTitle(language):
 
 def main(argv):
 
+  justPrintOutput = False
+  langargs = []
   try:
-    opts, args = getopt.getopt(argv,"hl:",["language="])
+    opts, args = getopt.gnu_getopt(argv,"hl:o",["language="])
   except getopt.GetoptError:
-    print('books-toc-examples-creator.py -l <language>')
+    print('books-toc-examples-creator.py [-o] -l <language>')
     sys.exit(2)
   for opt, arg in opts:
     if opt == '-h':
-      print('books-toc-examples-creator.py -l <language>')
+      print('books-toc-examples-creator.py [-o] -l <language>')
       sys.exit()
+    if opt == '-o':
+      justPrintOutput = True
     elif opt in ("-l", "--language"):
-      languages = arg.split(',')
+      langargs = arg.replace(" ",",").split(',')
+
+  # treat additional arguments as languages, but check if they
+  #  contain ','
+  for l in args:
+    langargs.extend(l.replace(" ",",").split(','))
 
-  for language in languages:
+  for language in langargs:
 
     with open('./content/{}/books/books.adoc'.format(language), 'r', encoding = 'utf-8') as booksFile:
       books = [line.strip() for line in booksFile]
@@ -115,8 +125,11 @@ def main(argv):
 
         toc += "--\n"
 
-        with open('./content/{0}/books/{1}/toc-examples.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile:
-          tocFile.write(toc)
+        if justPrintOutput == False:
+          with open('./content/{0}/books/{1}/toc-examples.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile:
+            tocFile.write(toc)
+        else:
+          print('./content/{0}/books/{1}/toc-examples.adoc'.format(language, book))
 
 if __name__ == "__main__":
   main(sys.argv[1:])
diff --git a/documentation/tools/books-toc-figures-creator.py b/documentation/tools/books-toc-figures-creator.py
index 46bea6226b..fbe1d62c65 100644
--- a/documentation/tools/books-toc-figures-creator.py
+++ b/documentation/tools/books-toc-figures-creator.py
@@ -1,3 +1,4 @@
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 """
 BSD 2-Clause License
@@ -5,9 +6,9 @@ BSD 2-Clause License
 Copyright (c) 2020-2021, The FreeBSD Project
 Copyright (c) 2020-2021, Sergio Carlavilla <carlavilla@FreeBSD.org>
 
-This script will generate the Table of Contents of the Handbook
+This script will generate the Table of Contents for any figures/images
+in the books.
 """
-#!/usr/bin/env python3
 
 import sys, getopt
 import re
@@ -53,20 +54,29 @@ def setTOCTitle(language):
   return languages.get(language)
 
 def main(argv):
-
+  
+  justPrintOutput = False
+  langargs = []
   try:
-    opts, args = getopt.getopt(argv,"hl:",["language="])
+    opts, args = getopt.gnu_getopt(argv,"hl:o",["language="])
   except getopt.GetoptError:
-    print('books-toc-figures-creator.py -l <language>')
+    print('books-toc-figures-creator.py [-o] -l <language>')
     sys.exit(2)
   for opt, arg in opts:
     if opt == '-h':
-      print('books-toc-figures-creator.py -l <language>')
+      print('books-toc-figures-creator.py [-o] -l <language>')
       sys.exit()
+    if opt == '-o':
+      justPrintOutput = True
     elif opt in ("-l", "--language"):
-      languages = arg.split(',')
+      langargs = arg.replace(" ",",").split(',')
+
+  # treat additional arguments as languages, but check if they
+  #  contain ','
+  for l in args:
+    langargs.extend(l.replace(" ",",").split(','))
 
-  for language in languages:
+  for language in langargs:
 
     with open('./content/{}/books/books.adoc'.format(language), 'r', encoding = 'utf-8') as booksFile:
       books = [line.strip() for line in booksFile]
@@ -114,8 +124,11 @@ def main(argv):
 
         toc += "--\n"
 
-        with open('./content/{0}/books/{1}/toc-figures.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile:
-          tocFile.write(toc)
+        if justPrintOutput == False:
+          with open('./content/{0}/books/{1}/toc-figures.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile:
+            tocFile.write(toc)
+        else:
+          print('./content/{0}/books/{1}/toc-figures.adoc'.format(language, book))
 
 if __name__ == "__main__":
   main(sys.argv[1:])
diff --git a/documentation/tools/books-toc-parts-creator.py b/documentation/tools/books-toc-parts-creator.py
index b05aeb9dc5..aeb035b8cb 100644
--- a/documentation/tools/books-toc-parts-creator.py
+++ b/documentation/tools/books-toc-parts-creator.py
@@ -1,11 +1,12 @@
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 """
 BSD 2-Clause License
 Copyright (c) 2020-2021, The FreeBSD Project
 Copyright (c) 2020-2021, Sergio Carlavilla <carlavilla@FreeBSD.org>
-This script will generate the Table of Contents of the Handbook
+This script will generate the Table of Contents for any [preface],
+[appendix] or other "parts" as defined by asciidoc.
 """
-#!/usr/bin/env python3
 
 import sys, getopt
 import re
@@ -14,12 +15,18 @@ import os.path
 languages = []
 
 def cleanTocFile(language, tocCounter, book):
-    path = './content/{0}/books/{1}/toc-{2}.adoc'.format(language, book, tocCounter)
+  path = './content/{0}/books/{1}/toc-{2}.adoc'.format(language, book, tocCounter)
+  if justPrintOutput == True:
+    print(path)
+  else:
     if os.path.exists(path):
-        tocFile = open(path, 'r+', encoding = 'utf-8')
-        tocFile.truncate(0)
+      tocFile = open(path, 'r+', encoding = 'utf-8')
+      tocFile.truncate(0)
+
+def prependCommentAndTitle(language, tocCounter, tocContent, book):
+  if justPrintOutput == True:
+    return True
 
-def appendCommendAndTitle(language, tocCounter, tocContent, book):
   toc =  "// Code generated by the FreeBSD Documentation toolchain. DO NOT EDIT.\n"
   toc += "// Please don't change this file manually but run `make` to update it.\n"
   toc += "// For more information, please read the FreeBSD Documentation Project Primer\n\n"
@@ -112,20 +119,30 @@ def setTOCTitle(language):
   return languages.get(language)
 
 def main(argv):
+  global justPrintOutput
 
+  justPrintOutput = False
+  langargs=[]
   try:
-    opts, args = getopt.getopt(argv,"hl:",["language="])
+    opts, args = getopt.gnu_getopt(argv,"hl:o",["language="])
   except getopt.GetoptError:
-    print('books-toc-creator.py -l <language>')
+    print('books-toc-creator.py [-o] -l <language>')
     sys.exit(2)
   for opt, arg in opts:
     if opt == '-h':
-      print('books-toc-creator.py -l <language>')
+      print('books-toc-creator.py [-o] -l <language>')
       sys.exit()
+    elif opt == '-o':
+      justPrintOutput = True
     elif opt in ("-l", "--language"):
-      languages = arg.split(',')
+      langargs = arg.replace(" ",",").split(',')
+
+  # treat additional arguments as languages, but check if they
+  #  contain ','
+  for l in args:
+    langargs.extend(l.replace(" ",",").split(','))
 
-  for language in languages:
+  for language in langargs:
 
     with open('./content/{}/books/books.adoc'.format(language), 'r', encoding = 'utf-8') as booksFile:
       books = [line.strip() for line in booksFile]
@@ -149,7 +166,7 @@ def main(argv):
 
               if partCounter > 0:
                 cleanTocFile(language, partCounter, book)
-                appendCommendAndTitle(language, partCounter, toc, book)
+                prependCommentAndTitle(language, partCounter, toc, book)
                 toc = "" # Clean toc content
 
               partCounter += 1
@@ -181,7 +198,7 @@ def main(argv):
 
         if partCounter > 0:
           cleanTocFile(language, partCounter, book)
-          appendCommendAndTitle(language, partCounter, toc, book) # For the last part
+          prependCommentAndTitle(language, partCounter, toc, book) # For the last part
 
 if __name__ == "__main__":
   main(sys.argv[1:])
diff --git a/documentation/tools/books-toc-tables-creator.py b/documentation/tools/books-toc-tables-creator.py
index 592330b7a4..67f1733d45 100644
--- a/documentation/tools/books-toc-tables-creator.py
+++ b/documentation/tools/books-toc-tables-creator.py
@@ -1,3 +1,4 @@
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 """
 BSD 2-Clause License
@@ -5,9 +6,8 @@ BSD 2-Clause License
 Copyright (c) 2020-2021, The FreeBSD Project
 Copyright (c) 2020-2021, Sergio Carlavilla <carlavilla@FreeBSD.org>
 
-This script will generate the Table of Contents of the Handbook
+This script will generate the Table of Contents for tables in the books.
 """
-#!/usr/bin/env python3
 
 import sys, getopt
 import re
@@ -54,19 +54,28 @@ def setTOCTitle(language):
 
 def main(argv):
 
+  justPrintOutput = False
+  langargs= []
   try:
-    opts, args = getopt.getopt(argv,"hl:",["language="])
+    opts, args = getopt.gnu_getopt(argv,"hl:o",["language="])
   except getopt.GetoptError:
-    print('books-toc-tables-creator.py -l <language>')
+    print('books-toc-tables-creator.py [-o] -l <language>')
     sys.exit(2)
   for opt, arg in opts:
     if opt == '-h':
-      print('books-toc-tables-creator.py -l <language>')
+      print('books-toc-tables-creator.py [-o] -l <language>')
       sys.exit()
+    if opt == '-o':
+      justPrintOutput = True
     elif opt in ("-l", "--language"):
-      languages = arg.split(',')
+      langargs = arg.replace(" ",",").split(',')
+
+  # treat additional arguments as languages, but check if they
+  #  contain ','
+  for l in args:
+    langargs.extend(l.replace(" ",",").split(','))
 
-  for language in languages:
+  for language in langargs:
 
     with open('./content/{}/books/books.adoc'.format(language), 'r', encoding = 'utf-8') as booksFile:
       books = [line.strip() for line in booksFile]
@@ -114,8 +123,11 @@ def main(argv):
 
         toc += "--\n"
 
-        with open('./content/{0}/books/{1}/toc-tables.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile:
-          tocFile.write(toc)
+        if justPrintOutput == False:
+          with open('./content/{0}/books/{1}/toc-tables.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile:
+            tocFile.write(toc)
+        else:
+          print('./content/{0}/books/{1}/toc-tables.adoc'.format(language, book))
 
 if __name__ == "__main__":
   main(sys.argv[1:])



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202106131249.15DCnf0b029039>