Skip to content

Commit

Permalink
Add a method to discover if a library is being modified
Browse files Browse the repository at this point in the history
To be in isBeingModified state, a library must:
- have one of its file modified in the last 24 hours
- have a time difference between oldest and newest file bigger than 5 minutes (aka, the lib has not just been extracted from a zip)
  • Loading branch information
facchinm committed Oct 9, 2018
1 parent f9d69df commit 96f366d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
27 changes: 27 additions & 0 deletions libraries_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@
package builder

import (
"math"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/arduino/arduino-builder/constants"
"github.com/arduino/arduino-builder/i18n"
Expand Down Expand Up @@ -153,6 +156,11 @@ func makeNewLibrary(libraryFolder string, debugLevel int, logger i18n.Logger) (*
return nil, i18n.WrapError(err)
}

timeDiff, newestFile := newestAndOldestFileDifferBy(libraryFolder)
if timeDiff > (5*time.Minute) && time.Since(newestFile) < 24*time.Hour {
library.IsBeingModified = true
}

if debugLevel >= 0 {
for _, subFolder := range subFolders {
if utils.IsSCCSOrHiddenFile(subFolder) {
Expand Down Expand Up @@ -220,3 +228,22 @@ func appendPathToLibrariesFolders(librariesFolders []string, newLibrariesFolder

return utils.AppendIfNotPresent(librariesFolders, newLibrariesFolder)
}

func newestAndOldestFileDifferBy(path string) (time.Duration, time.Time) {
var newestTime int64 = 0
var oldestTime int64 = math.MaxInt64

filepath.Walk(path,
func(path string, info os.FileInfo, err error) error {
currTime := info.ModTime().Unix()
if currTime > newestTime {
newestTime = currTime
}
if currTime < oldestTime {
oldestTime = currTime
}
return nil
})
duration, _ := time.ParseDuration(strconv.FormatInt(newestTime-oldestTime, 10) + "s")
return duration, time.Unix(newestTime, 0)
}
41 changes: 21 additions & 20 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,26 +162,27 @@ const (
)

type Library struct {
Folder string
SrcFolder string
UtilityFolder string
Layout LibraryLayout
Name string
RealName string
Archs []string
DotALinkage bool
Precompiled bool
LDflags string
IsLegacy bool
Version string
Author string
Maintainer string
Sentence string
Paragraph string
URL string
Category string
License string
Properties map[string]string
Folder string
SrcFolder string
UtilityFolder string
Layout LibraryLayout
Name string
RealName string
Archs []string
DotALinkage bool
Precompiled bool
LDflags string
IsLegacy bool
Version string
Author string
Maintainer string
Sentence string
Paragraph string
URL string
Category string
License string
Properties map[string]string
IsBeingModified bool
}

func (library *Library) String() string {
Expand Down

0 comments on commit 96f366d

Please sign in to comment.