Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rm checksum for dependencies from local path and fix symlink #296

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,6 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro
} else if d.IsFromLocal() && !utils.DirExists(d.GetLocalFullPath(kclPkg.HomePath)) {
return reporter.NewErrorEvent(reporter.DependencyNotFound, fmt.Errorf("dependency '%s' not found in '%s'", d.Name, searchFullPath))
} else if d.IsFromLocal() && utils.DirExists(d.GetLocalFullPath(kclPkg.HomePath)) {
sum, err := utils.HashDir(d.GetLocalFullPath(kclPkg.HomePath))
if err != nil {
return reporter.NewErrorEvent(reporter.CalSumFailed, err, fmt.Sprintf("failed to calculate checksum for '%s' in '%s'", d.Name, searchFullPath))
}
d.Sum = sum
depPkg, err := c.LoadPkgFromPath(d.GetLocalFullPath(kclPkg.HomePath))
if err != nil {
return err
Expand Down Expand Up @@ -718,7 +713,7 @@ func (c *KpmClient) VendorDeps(kclPkg *pkg.KclPkg) error {
}
vendorFullPath := filepath.Join(vendorPath, d.FullName)
// If the package already exists in the 'vendor', do nothing.
if utils.DirExists(vendorFullPath) && check(d, vendorFullPath) {
if depExisted(vendorFullPath, d) {
continue
} else {
// If not in the 'vendor', check the global cache.
Expand All @@ -729,7 +724,7 @@ func (c *KpmClient) VendorDeps(kclPkg *pkg.KclPkg) error {
if err != nil {
return err
}
} else if utils.DirExists(d.GetLocalFullPath(kclPkg.HomePath)) && check(d, d.GetLocalFullPath(kclPkg.HomePath)) {
} else if depExisted(d.GetLocalFullPath(kclPkg.HomePath), d) {
// If there is, copy it into the 'vendor' directory.
err := copy.Copy(d.GetLocalFullPath(kclPkg.HomePath), vendorFullPath)
if err != nil {
Expand All @@ -754,6 +749,11 @@ func (c *KpmClient) VendorDeps(kclPkg *pkg.KclPkg) error {
return nil
}

func depExisted(localPath string, dep pkg.Dependency) bool {
Peefy marked this conversation as resolved.
Show resolved Hide resolved
return (utils.DirExists(localPath) && check(dep, localPath)) ||
(utils.DirExists(localPath) && dep.IsFromLocal())
}

// FillDepInfo will fill registry information for a dependency.
func (c *KpmClient) FillDepInfo(dep *pkg.Dependency, homepath string) error {
if dep.Source.Local != nil {
Expand Down Expand Up @@ -851,14 +851,16 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*
dep.FromKclPkg(kpkg)
}

var err error
dep.Sum, err = utils.HashDir(dep.LocalFullPath)
if err != nil {
return nil, reporter.NewErrorEvent(
reporter.FailedHashPkg,
err,
fmt.Sprintf("failed to hash the kcl package '%s' in '%s'.", dep.Name, dep.LocalFullPath),
)
if dep.Source.Local == nil {
var err error
dep.Sum, err = utils.HashDir(dep.LocalFullPath)
if err != nil {
return nil, reporter.NewErrorEvent(
reporter.FailedHashPkg,
err,
fmt.Sprintf("failed to hash the kcl package '%s' in '%s'.", dep.Name, dep.LocalFullPath),
)
}
}

return dep, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
name = "dep_pkg"
full_name = "dep_pkg_0.0.1"
version = "0.0.1"
sum = "C3TjaZJVdt4G8TtbiCxTUO/zAlf7fWJTAvs/CDMnlxY="
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.1.1"
Expand Down
1 change: 1 addition & 0 deletions pkg/utils/test_data/test_link/is_link_exist/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test for create link
48 changes: 46 additions & 2 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,44 @@ func DirExists(path string) bool {
return err == nil
}

// IsSymlinkValidAndExists will check whether the symlink exists and points to a valid target
// return three values: whether the symlink exists, whether it points to a valid target, and any error encountered
func IsSymlinkValidAndExists(symlinkPath string) (bool, bool, error) {
Peefy marked this conversation as resolved.
Show resolved Hide resolved
// check if the symlink exists
info, err := os.Lstat(symlinkPath)
if err != nil && os.IsNotExist(err) {
// symlink does not exist
return false, false, nil
} else if err != nil {
// other error
return false, false, err
}

// check if the file is a symlink
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
// get the target of the symlink
target, err := os.Readlink(symlinkPath)
if err != nil {
// can not read the target
return true, false, err
}

// check if the target exists
_, err = os.Stat(target)
if err == nil {
// target exists
return true, true, nil
}
if os.IsNotExist(err) {
// target does not exist
return true, false, nil
}
return true, false, err
}

return false, false, fmt.Errorf("%s exists but is not a symlink", symlinkPath)
}

// DefaultKpmHome create the '.kpm' in the user home and return the path of ".kpm".
func CreateSubdirInUserHome(subdir string) (string, error) {
homeDir, err := os.UserHomeDir()
Expand All @@ -305,14 +343,20 @@ func CreateSubdirInUserHome(subdir string) (string, error) {
// CreateSymlink will create symbolic link named 'newName' for 'oldName',
// and if the symbolic link already exists, it will be deleted and recreated.
func CreateSymlink(oldName, newName string) error {
if DirExists(newName) {
symExist, _, err := IsSymlinkValidAndExists(newName)

if err != nil {
return err
}

if symExist {
err := os.Remove(newName)
if err != nil {
return err
}
}

err := os.Symlink(oldName, newName)
err = os.Symlink(oldName, newName)
if err != nil {
return err
}
Expand Down
42 changes: 42 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,45 @@ func TestAbsTarPath(t *testing.T) {
assert.NotEqual(t, err, nil)
assert.Equal(t, abs, "")
}

func TestIsSymlinkExist(t *testing.T) {
testPath := filepath.Join(getTestDir("test_link"), "is_link_exist")

link_target_not_exist := filepath.Join(testPath, "link_target_not_exist")

linkExist, targetExist, err := IsSymlinkValidAndExists(link_target_not_exist)
assert.Equal(t, err, nil)
assert.Equal(t, linkExist, true)
assert.Equal(t, targetExist, false)

linkExist, targetExist, err = IsSymlinkValidAndExists("invalid_link")
assert.Equal(t, err, nil)
assert.Equal(t, linkExist, false)
assert.Equal(t, targetExist, false)

filename := filepath.Join(testPath, "test.txt")
validLink := filepath.Join(testPath, "valid_link")
err = CreateSymlink(filename, validLink)
assert.Equal(t, err, nil)

linkExist, targetExist, err = IsSymlinkValidAndExists(validLink)
assert.Equal(t, err, nil)
assert.Equal(t, linkExist, true)
assert.Equal(t, targetExist, true)

anotherValidLink := filepath.Join(testPath, "another_valid_link")
err = CreateSymlink(filename, anotherValidLink)
assert.Equal(t, err, nil)

linkExist, targetExist, err = IsSymlinkValidAndExists(anotherValidLink)
assert.Equal(t, err, nil)
assert.Equal(t, linkExist, true)
assert.Equal(t, targetExist, true)
// Defer the removal of the symlink
defer func() {
err := os.Remove(anotherValidLink)
assert.Equal(t, err, nil)
err = os.Remove(validLink)
assert.Equal(t, err, nil)
}()
}
Loading