Repository: packit-service/packit
Pull request #769 (2020-03-24 11:12:00)

get_local_specfile_path() changes
This allows packit to discover `spec` files also in subdirectories.

softwarefactory-project-zuul[bot]: Build succeeded.

- [packit-pre-commit ](https://softwarefactory-project.io/zuul/t/local/build/694390f39e264c17a0df40c61a853efe) : SUCCESS in 4m 09s
- [packit-tests-rpm ](https://softwarefactory-project.io/zuul/t/local/build/8fb73028e0be44978ea3a67c5476901e) : SUCCESS in 12m 07s
- [packit-tests-pip-deps ](https://softwarefactory-project.io/zuul/t/local/build/e02e15e4568f4627ae7ef36acb889b26) : SUCCESS in 7m 45s
- [packit-tests-git-master ](https://softwarefactory-project.io/zuul/t/local/build/1e342cc073b944c8931d679f09a51473) : SUCCESS in 7m 35s
- [packit-tests-rpm-sess-rec ](https://softwarefactory-project.io/zuul/t/local/build/15f018fafcec4f06afe1606401eeebb9) : SUCCESS in 4m 31s
- [packit-tests-pip-deps-sess-rec ](https://softwarefactory-project.io/zuul/t/local/build/6de1c77f46ac4663ace6a1f038d27035) : SUCCESS in 2m 50s
- [packit-tests-git-master-sess-rec ](https://softwarefactory-project.io/zuul/t/local/build/18bf6bf4a5f54a0086636624ac875b53) : SUCCESS in 3m 28s
- [reverse-dep-packit-service-tests ](https://softwarefactory-project.io/zuul/t/local/build/8f11ed9742ad4500accd71ca64650c35) : SUCCESS in 3m 50s


Patch:
diff --git a/packit/config/package_config.py b/packit/config/package_config.py
index 7ee748ce..f60c5039 100644
--- a/packit/config/package_config.py
+++ b/packit/config/package_config.py
@@ -22,8 +22,6 @@
 
 import json
 import logging
-import os
-from glob import glob
 from pathlib import Path
 from typing import Optional, List, Dict, Union
 
@@ -31,9 +29,9 @@ from yaml import safe_load
 
 from ogr.abstract import GitProject
 from packit.actions import ActionName
-from packit.constants import CONFIG_FILE_NAMES, PROD_DISTGIT_URL
 from packit.config.job_config import JobConfig, default_jobs, JobType
 from packit.config.sync_files_config import SyncFilesConfig, SyncFilesItem
+from packit.constants import CONFIG_FILE_NAMES, PROD_DISTGIT_URL
 from packit.exceptions import PackitConfigException
 
 logger = logging.getLogger(__name__)
@@ -272,7 +270,7 @@ def get_local_package_config(
                     loaded_config=loaded_config,
                     config_file_path=str(config_file_name),
                     repo_name=repo_name,
-                    spec_file_path=get_local_specfile_path(directories),
+                    spec_file_path=str(get_local_specfile_path(config_dir)),
                 )
 
             logger.debug(f"The local config file '{config_file_name_full}' not found.")
@@ -341,18 +339,25 @@ def parse_loaded_config(
         raise PackitConfigException(f"Cannot parse package config: {ex}.")
 
 
-def get_local_specfile_path(directories: Union[List[str], List[Path]]) -> Optional[str]:
+def get_local_specfile_path(dir: Path, exclude: List[str] = None) -> Optional[Path]:
     """
-    Get the relative path of the local spec file if present.
-    :param directories: dirs to find the spec file
-    :return: str relative path of the spec file
+    Get the path (relative to dir) of the local spec file if present.
+    If the spec is not found in dir directly, try to search it recursively (rglob)
+    :param dir: to find the spec file in
+    :param exclude: don't include files found in these dirs (default "tests")
+    :return: path (relative to dir) of the first found spec file
     """
-    for dir in directories:
-        files = [
-            os.path.relpath(path, dir) for path in glob(os.path.join(dir, "*.spec"))
-        ]
-        if len(files) > 0:
-            return files[0]
+    files = [path.relative_to(dir) for path in dir.glob("*.spec")] or [
+        path.relative_to(dir) for path in dir.rglob("*.spec")
+    ]
+
+    if len(files) > 0:
+        # Don't take files found in exclude
+        sexclude = set(exclude) if exclude else {"tests"}
+        files = [f for f in files if f.parts[0] not in sexclude]
+
+        logger.debug(f"Local spec files found: {files}. Taking: {files[0]}")
+        return files[0]
 
     return None
 


Test patch:
diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py
index 4ad72424..64fe1e2e 100644
--- a/tests/unit/test_config.py
+++ b/tests/unit/test_config.py
@@ -743,8 +743,8 @@ def test_get_all_files_to_sync(package_config, all_synced_files):
 
 
 def test_get_local_specfile_path():
-    assert get_local_specfile_path([UP_OSBUILD]) == "osbuild.spec"
-    assert not get_local_specfile_path([SYNC_FILES])
+    assert str(get_local_specfile_path(UP_OSBUILD)) == "osbuild.spec"
+    assert not get_local_specfile_path(SYNC_FILES)
 
 
 def test_notifications_section():