mirror of https://github.com/databricks/cli.git
86 lines
2.0 KiB
Go
86 lines
2.0 KiB
Go
package libraries
|
|
|
|
import (
|
|
"net/url"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
// IsLocalPath returns true if the specified path indicates that
|
|
// it should be interpreted as a path on the local file system.
|
|
//
|
|
// The following paths are considered local:
|
|
//
|
|
// - myfile.txt
|
|
// - ./myfile.txt
|
|
// - ../myfile.txt
|
|
// - file:///foo/bar/myfile.txt
|
|
//
|
|
// The following paths are considered remote:
|
|
//
|
|
// - dbfs:/mnt/myfile.txt
|
|
// - s3:/mybucket/myfile.txt
|
|
// - /Users/jane@doe.com/myfile.txt
|
|
func IsLocalPath(p string) bool {
|
|
// If the path has the explicit file scheme, it's a local path.
|
|
if strings.HasPrefix(p, "file://") {
|
|
return true
|
|
}
|
|
|
|
// If the path has another scheme, it's a remote path.
|
|
if isRemoteStorageScheme(p) {
|
|
return false
|
|
}
|
|
|
|
// If path starts with /, it's a remote absolute path
|
|
return !path.IsAbs(p)
|
|
}
|
|
|
|
// IsLibraryLocal returns true if the specified library or environment dependency
|
|
// should be interpreted as a local path.
|
|
// We use this to check if the dependency in environment spec is local or that library is local.
|
|
// We can't use IsLocalPath beacuse environment dependencies can be
|
|
// a pypi package name which can be misinterpreted as a local path by IsLocalPath.
|
|
func IsLibraryLocal(dep string) bool {
|
|
possiblePrefixes := []string{
|
|
".",
|
|
}
|
|
|
|
for _, prefix := range possiblePrefixes {
|
|
if strings.HasPrefix(dep, prefix) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
// If the dependency is a requirements file, it's not a valid local path
|
|
if strings.HasPrefix(dep, "-r") {
|
|
return false
|
|
}
|
|
|
|
// If the dependency has no extension, it's a PyPi package name
|
|
if isPackage(dep) {
|
|
return false
|
|
}
|
|
|
|
return IsLocalPath(dep)
|
|
}
|
|
|
|
func isPackage(name string) bool {
|
|
// If the dependency has no extension, it's a PyPi package name
|
|
return path.Ext(name) == ""
|
|
}
|
|
|
|
func isRemoteStorageScheme(path string) bool {
|
|
url, err := url.Parse(path)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
if url.Scheme == "" {
|
|
return false
|
|
}
|
|
|
|
// If the path starts with scheme:/ format (not file), it's a correct remote storage scheme
|
|
return strings.HasPrefix(path, url.Scheme+":/") && url.Scheme != "file"
|
|
}
|