From b6e4709fc7430b7806cc8fa1fca711f694a8b2e8 Mon Sep 17 00:00:00 2001 From: JornVernee Date: Thu, 8 Nov 2018 12:31:34 +0100 Subject: [PATCH 1/2] Replaced calls to os.path.relpath with a safe alternative, since not every path can be made relative on Windows, due to different drives. rename safe_relpath to maybe_relpath per suggestion Also rename callsites to maybe_relpath (oops) Revert replacements to do it again step-by-step to better examine use cases of os.path.relpath Fix regex to also work with windows path separators. Use maybe_relpath as a replacement for os.path.relpath a point where `mx ideinit` and `mx gate` are passing. Added special handling for PROJECTDIR relative paths. Added maybe_relpath to the other dependency branches as well linter fixes Removed duplicate '$PROJECT_DIR$/' replace other uses of PROJECT_DIR and relpath with safe equivalent. Consolidate code paths pash relpath_or_absolute calls into make_library since that is where they are joined with PROJECT_DIR (or not) using os.path.join instead of a hardcoded path separator. Revert "using os.path.join instead of a hardcoded path separator." since these are URIs and not actual paths Revert "Revert "using os.path.join instead of a hardcoded path separator." since these are URIs and not actual paths" Simplified things remove accidental diffs undiff more undiff more remove spurious withespace --- mx.py | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/mx.py b/mx.py index f6956b4f..53353d33 100755 --- a/mx.py +++ b/mx.py @@ -227,6 +227,17 @@ def __exit__(self, tpe, value, traceback): currently_loading_suite = DynamicVar(None) +def relpath_or_absolute(path, start, prefix=""): + """ + Finds a relative path and joins it to 'prefix', or otherwise tries to use 'path' as an absolute path. + If 'path' is not an absolute path, an error is thrown. + """ + try: + return join(prefix, os.path.relpath(path, start)) + except ValueError: + if not os.path.isabs(path): + raise ValueError('can not find a relative path to dependency and path is not absolute: ' + path) + return path # Support for Python 2.6 def check_output(*popenargs, **kwargs): @@ -15412,7 +15423,7 @@ def intellij_read_sdks(): warn("IntelliJ SDK definitions not found") return sdks - verRE = re.compile(r'^.*/\.?(IntelliJIdea|IdeaIC)([^/]+)/.*$') + verRE = re.compile(r'^.*[/\\]\.?(IntelliJIdea|IdeaIC)([^/\\]+)[/\\].*$') def verSort(path): match = verRE.match(path) return match.group(2) + (".a" if match.group(1) == "IntellijIC" else ".b") @@ -15757,7 +15768,7 @@ def _mx_projects_suite(visited_suite, suite_import): if not module_files_only: declared_modules.add(basename(s.mxDir)) moduleFilePath = "$PROJECT_DIR$/" + os.path.relpath(moduleFile, s.dir) - modulesXml.element('module', attributes={'fileurl': 'file://' + moduleFilePath, 'filepath': moduleFilePath}) + modulesXml.element('module', attributes={'fileurl': 'file://$' + moduleFilePath, 'filepath': moduleFilePath}) declared_modules.add(basename(_mx_suite.dir)) mxModuleFile = join(_mx_suite.dir, basename(_mx_suite.dir) + '.iml') @@ -15782,21 +15793,24 @@ def _mx_projects_suite(visited_suite, suite_import): ensure_dir_exists(librariesDirectory) - def make_library(name, path, source_path): + def make_library(name, path, source_path, suite_dir): libraryXml = XMLDoc() libraryXml.open('component', attributes={'name': 'libraryTable'}) libraryXml.open('library', attributes={'name': name}) libraryXml.open('CLASSES') - libraryXml.element('root', attributes={'url': 'jar://$PROJECT_DIR$/' + path + '!/'}) + pathX = relpath_or_absolute(path, suite_dir, prefix='$PROJECT_DIR$') + libraryXml.element('root', attributes={'url': 'jar://' + pathX + '!/'}) libraryXml.close('CLASSES') libraryXml.element('JAVADOC') if sourcePath: libraryXml.open('SOURCES') if os.path.isdir(sourcePath): - libraryXml.element('root', attributes={'url': 'file://$PROJECT_DIR$/' + sourcePath}) + sourcePathX = relpath_or_absolute(sourcePath, suite_dir, prefix='$PROJECT_DIR$') + libraryXml.element('root', attributes={'url': 'file://' + sourcePathX}) else: - libraryXml.element('root', attributes={'url': 'jar://$PROJECT_DIR$/' + source_path + '!/'}) + source_pathX = relpath_or_absolute(source_path, suite_dir, prefix='$PROJECT_DIR$') + libraryXml.element('root', attributes={'url': 'jar://' + source_pathX + '!/'}) libraryXml.close('SOURCES') else: libraryXml.element('SOURCES') @@ -15810,25 +15824,25 @@ def make_library(name, path, source_path): for library in libraries: sourcePath = None if library.isLibrary(): - path = os.path.relpath(library.get_path(True), s.dir) + path = library.get_path(True) if library.sourcePath: - sourcePath = os.path.relpath(library.get_source_path(True), s.dir) + sourcePath = library.get_source_path(True) elif library.isMavenProject(): - path = os.path.relpath(library.get_path(True), s.dir) - sourcePath = os.path.relpath(library.get_source_path(True), s.dir) + path = library.get_path(True) + sourcePath = library.get_source_path(True) elif library.isJARDistribution(): - path = os.path.relpath(library.path, s.dir) + path = library.path if library.sourcesPath: - sourcePath = os.path.relpath(library.sourcesPath, s.dir) + sourcePath = library.sourcesPath else: abort('Dependency not supported: {} ({})'.format(library.name, library.__class__.__name__)) - make_library(library.name, path, sourcePath) + make_library(library.name, path, sourcePath, s.dir) jdk = get_jdk() updated = False for library in jdk_libraries: if library.classpath_repr(jdk) is not None: - if make_library(library.name, os.path.relpath(library.classpath_repr(jdk), s.dir), os.path.relpath(library.get_source_path(jdk), s.dir)): + if make_library(library.name, library.classpath_repr(jdk), library.get_source_path(jdk), s.dir): updated = True if jdk_libraries and updated: log("Setting up JDK libraries using {0}".format(jdk)) @@ -15859,9 +15873,9 @@ def make_library(name, path, source_path): for apDep in processors: def processApDep(dep, edge): if dep.isLibrary() or dep.isJARDistribution(): - compilerXml.element('entry', attributes={'name': '$PROJECT_DIR$/' + os.path.relpath(dep.path, s.dir)}) + compilerXml.element('entry', attributes={'name': relpath_or_absolute(dep.path, s.dir, prefix='$PROJECT_DIR$')}) elif dep.isProject(): - compilerXml.element('entry', attributes={'name': '$PROJECT_DIR$/' + os.path.relpath(dep.output_dir(), s.dir)}) + compilerXml.element('entry', attributes={'name': relpath_or_absolute(dep.output_dir(), s.dir, prefix='$PROJECT_DIR$')}) apDep.walk_deps(visit=processApDep) compilerXml.close('processorPath') for module in modules: From f22fbc04fc427bb06969ca551e6fb0c54f786fa0 Mon Sep 17 00:00:00 2001 From: JornVernee Date: Fri, 9 Nov 2018 12:10:00 +0100 Subject: [PATCH 2/2] remove spurious '$' --- mx.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mx.py b/mx.py index 53353d33..80184340 100755 --- a/mx.py +++ b/mx.py @@ -15768,7 +15768,7 @@ def _mx_projects_suite(visited_suite, suite_import): if not module_files_only: declared_modules.add(basename(s.mxDir)) moduleFilePath = "$PROJECT_DIR$/" + os.path.relpath(moduleFile, s.dir) - modulesXml.element('module', attributes={'fileurl': 'file://$' + moduleFilePath, 'filepath': moduleFilePath}) + modulesXml.element('module', attributes={'fileurl': 'file://' + moduleFilePath, 'filepath': moduleFilePath}) declared_modules.add(basename(_mx_suite.dir)) mxModuleFile = join(_mx_suite.dir, basename(_mx_suite.dir) + '.iml')