doc gen: fix doc generator
[csit.git] / resources / tools / doc_gen / gen_rst.py
index 1cb1005..d3f7d96 100755 (executable)
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-
-
-from os import walk, listdir
+from os import walk, listdir, scandir, environ
 from os.path import isfile, isdir, join, getsize
 
-# Temporary working directory. It is created and deleted by run_doc.sh
-WORKING_DIR = u"tmp"
+# Temporary working directory. It is created and deleted by docs.sh
+WORKING_DIR = environ.get("WORKING_DIR")
 
 # Directory with resources to be documented.
 RESOURCES_DIR = u"resources"
@@ -83,7 +81,7 @@ def get_files(path, extension):
     for root, dirs, files in walk(path):
         for filename in files:
             if extension:
-                if filename.endswith(extension):
+                if filename.endswith(extension) and u"__init__" not in filename:
                     file_list.append(join(root, filename))
             else:
                 file_list.append(join(root, filename))
@@ -126,6 +124,34 @@ def create_rst_file_names_set(files, start):
     return file_names
 
 
+def add_nested_folders_in_rst_set(file_names, path):
+    """Add RST files from folders where are only folders without tests.
+
+    :param file_names: List of all files to be documented with path beginning
+        in the working directory.
+    :param path: Path where it starts adding missing RST files.
+    :type file_names: list
+    :type path: str
+    """
+
+    # When we split directory tree by "/" we don't need to create RST file in
+    # folders in depth <= 5. It's because the WORKING_DIR folder structure i
+    # as following:
+    # /tmp/tmp-csitXXX/tests/<subject_of_test>/<type_of_test>/<what_is_tested>
+    # That splits to ie:
+    # ['', 'tmp', 'tmp-csitXXX', 'tests', 'vpp', 'device', 'container_memif']
+    # We need to generate RST files for folders after <subject_of_test> which
+    # is in depth > 5
+
+    for directory in fast_scandir(path):
+        dir_list = directory.split(u"/")
+        if len(dir_list) > 5:
+            # cut ['', 'tmp', 'tmp-csitXXX']
+            dir_rst = u".".join(dir_list[3:]) + u".rst"
+            if dir_rst not in file_names and u"__pycache__" not in dir_rst:
+                file_names.add(dir_rst)
+
+
 def scan_dir(path):
     """Create a list of files and directories in the given directory.
 
@@ -157,8 +183,8 @@ def write_toc(fh, path, dirs):
     :type dirs: list
     """
     fh.write(rst_toc)
-    for dir in dirs:
-        fh.write(f"    {u'.'.join(path)}.{dir}\n")
+    for directory in dirs:
+        fh.write(f"    {u'.'.join(path)}.{directory}\n")
 
 
 def write_module_title(fh, module_name):
@@ -274,12 +300,20 @@ def generate_tests_rst_files():
 
     tests = get_files(PATH_TESTS, RF_EXT)
     file_names = create_rst_file_names_set(tests, TESTS_DIR)
+    add_nested_folders_in_rst_set(file_names, PATH_TESTS)
 
     generate_rf_rst_files(
         file_names, incl_suite_setup=True, incl_variables=True
     )
 
 
+def fast_scandir(dirname):
+    subfolders = [f.path for f in scandir(dirname) if f.is_dir()]
+    for dirname in list(subfolders):
+        subfolders.extend(fast_scandir(dirname))
+    return subfolders
+
+
 if __name__ == u"__main__":
 
     # Generate all rst files: