From 7253898ffff71c889d0a8ad2b5c0f4ca44146eee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul=20=28ACSONE=29?= Date: Sat, 28 Apr 2018 18:35:07 +0200 Subject: Refactor hg and git file finders to use the same algorithm Also make the test parametric. --- setuptools_scm/file_finder.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 setuptools_scm/file_finder.py (limited to 'setuptools_scm/file_finder.py') diff --git a/setuptools_scm/file_finder.py b/setuptools_scm/file_finder.py new file mode 100644 index 0000000..fb7648a --- /dev/null +++ b/setuptools_scm/file_finder.py @@ -0,0 +1,33 @@ +import os + + +def scm_find_files(path, scm_files, scm_dirs): + """ setuptools compatible file finder that follows symlinks + + - path: the root directory from which to search + - scm_files: set of scm controlled files + - scm_files: set of scm controlled directories + + scm_files and scm_dirs must be absolute with symlinks resolved (realpath), + with normalized case (normcase) + + Spec here: http://setuptools.readthedocs.io/en/latest/setuptools.html#\ + adding-support-for-revision-control-systems + """ + realpath = os.path.normcase(os.path.realpath(path)) + seen = set() + res = [] + for dirpath, dirnames, filenames in os.walk(realpath, followlinks=True): + # dirpath with symlinks resolved + realdirpath = os.path.normcase(os.path.realpath(dirpath)) + if realdirpath not in scm_dirs or realdirpath in seen: + dirnames[:] = [] + continue + for filename in filenames: + # dirpath + filename with symlinks preserved + fullfilename = os.path.join(dirpath, filename) + if os.path.normcase(os.path.realpath(fullfilename)) in scm_files: + res.append( + os.path.join(path, os.path.relpath(fullfilename, path))) + seen.add(realdirpath) + return res -- cgit v1.2.1