From ea11f9cb69c61c74a94a7ce645939d861b99d5c2 Mon Sep 17 00:00:00 2001 From: Christoph Reiter Date: Sun, 6 Jan 2019 14:01:11 +0100 Subject: scanner: rework source root guessing code commonprefix doesn't work on relative paths and doesn't return directories so so for the g-i build case it returned an empty string resulting in paths relative to the working directory, making the build not reproducible. To somehwat improve this make sure the paths are absolute, use commonpath and if no common dir can be found just fall back to passing all directories so we only write the basenames. I guess we should look into passing --sources-top-dirs in the g-i build. --- giscanner/scannermain.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/giscanner/scannermain.py b/giscanner/scannermain.py index a2d7ea5d..154fe483 100644 --- a/giscanner/scannermain.py +++ b/giscanner/scannermain.py @@ -428,6 +428,7 @@ def create_source_scanner(options, args): filenames = extract_filelist(options) else: filenames = extract_filenames(args) + filenames = [os.path.realpath(f) for f in filenames] if platform.system() == 'Darwin': options.cpp_undefines.append('__BLOCKS__') @@ -490,6 +491,26 @@ def write_output(data, options): _error("while writing output: %s" % (e.strerror, )) +def get_source_root_dirs(options, filenames): + if options.sources_top_dirs: + return options.sources_top_dir + + # None passed, we need to guess + assert all(os.path.isabs(f) for f in filenames) + dirs = sorted(set([os.path.dirname(f) for f in filenames])) + + # We need commonpath (3.5+), otherwise give up + if not hasattr(os.path, "commonpath"): + return dirs + + common = os.path.commonpath(dirs) + # If the only common path is the root directory give up + if os.path.dirname(common) == common: + return dirs + + return [common] + + def scanner_main(args): parser = _get_option_parser() (options, args) = parser.parse_args(args) @@ -579,9 +600,9 @@ def scanner_main(args): transformer.namespace.c_includes = options.c_includes transformer.namespace.exported_packages = exported_packages - if not options.sources_top_dirs: - options.sources_top_dirs = [os.path.commonprefix(filenames).rpartition(os.path.sep)[0]] - writer = Writer(transformer.namespace, options.sources_top_dirs) + + sources_top_dirs = get_source_root_dirs(options, filenames) + writer = Writer(transformer.namespace, sources_top_dirs) data = writer.get_encoded_xml() write_output(data, options) -- cgit v1.2.1