Skip to content

Commit dc04717

Browse files
author
Abdul Dakkak
committed
initial commit
1 parent fd62609 commit dc04717

14 files changed

+585
-0
lines changed

.travis.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
language: go
2+
matrix:
3+
include:
4+
- go: 1.7.x
5+
- go: 1.8.x
6+
- go: tip
7+
allow_failures:
8+
- go: tip
9+
dist: trusty
10+
sudo: false
11+
addons:
12+
apt:
13+
sources:
14+
- sourceline: ppa:masterminds/glide
15+
packages:
16+
- glide
17+
install:
18+
- glide install --force
19+
- rm -fr vendor/github.com/Sirupsen
20+
- find vendor -type f -exec sed -i 's/Sirupsen/sirupsen/g' {} +
21+
- go build
22+
script:
23+
- echo "Skip tests..."
24+
after_script:
25+
- go test -race -v $(glide novendor)

config.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package dldataset
2+
3+
import (
4+
"github.com/k0kubun/pp"
5+
"github.com/rai-project/config"
6+
"github.com/rai-project/vipertags"
7+
)
8+
9+
type dldatasetConfig struct {
10+
WorkingDirectory string `json:"working_directory" config:"dldataset.working_directory" default:""`
11+
done chan struct{} `json:"-" config:"-"`
12+
}
13+
14+
var (
15+
// Config holds the data read by rai-project/config
16+
Config = &dldatasetConfig{
17+
done: make(chan struct{}),
18+
}
19+
)
20+
21+
func (dldatasetConfig) ConfigName() string {
22+
return "DLDataset"
23+
}
24+
25+
func (c *dldatasetConfig) SetDefaults() {
26+
vipertags.SetDefaults(c)
27+
}
28+
29+
func (c *dldatasetConfig) Read() {
30+
defer close(c.done)
31+
config.App.Wait()
32+
vipertags.Fill(c)
33+
if c.WorkingDirectory == "" || c.WorkingDirectory == "default" {
34+
c.WorkingDirectory = config.App.TempDir
35+
}
36+
}
37+
38+
func (c dldatasetConfig) Wait() {
39+
<-c.done
40+
}
41+
42+
func (c dldatasetConfig) String() string {
43+
return pp.Sprintln(c)
44+
}
45+
46+
func (c dldatasetConfig) Debug() {
47+
log.Debug("DLDataset Config = ", c)
48+
}
49+
50+
func init() {
51+
config.Register(Config)
52+
}

dldataset.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package dldataset
2+
3+
import (
4+
"io"
5+
6+
context "golang.org/x/net/context"
7+
)
8+
9+
type LabeledData interface {
10+
Label() string
11+
Data() (io.Reader, error)
12+
}
13+
14+
type Dataset interface {
15+
New(ctx context.Context) (Dataset, error)
16+
Category() string
17+
Name() string
18+
CanonicalName() string
19+
Download(ctx context.Context) error
20+
List(ctx context.Context) ([]string, error)
21+
Get(ctx context.Context, name string) (LabeledData, error)
22+
io.Closer
23+
}

glide.yaml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package: github.com/rai-project/dldataset
2+
import:
3+
- package: github.com/Unknwon/com
4+
version: ~1.0.0
5+
- package: github.com/k0kubun/pp
6+
version: ~2.3.0
7+
- package: github.com/pkg/errors
8+
version: ~0.8.0
9+
- package: github.com/rai-project/config
10+
- package: github.com/rai-project/downloadmanager
11+
- package: github.com/rai-project/logger
12+
- package: github.com/rai-project/vipertags
13+
- package: github.com/sirupsen/logrus
14+
version: ^1.0.0
15+
- package: golang.org/x/net
16+
subpackages:
17+
- context
18+
- package: golang.org/x/sync
19+
subpackages:
20+
- syncmap
21+
- package: github.com/stretchr/testify
22+
version: ~1.1.4
23+
subpackages:
24+
- assert

init.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package dldataset
2+
3+
import (
4+
"github.com/rai-project/config"
5+
"github.com/rai-project/logger"
6+
"github.com/sirupsen/logrus"
7+
)
8+
9+
var (
10+
log *logrus.Entry
11+
)
12+
13+
func init() {
14+
config.AfterInit(func() {
15+
log = logger.New().WithField("pkg", "dldataset")
16+
})
17+
}

register.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package dldataset
2+
3+
import (
4+
"errors"
5+
"path"
6+
"strings"
7+
8+
"golang.org/x/sync/syncmap"
9+
)
10+
11+
var datasets syncmap.Map
12+
13+
func Get(category, name string) (Dataset, error) {
14+
category = strings.ToLower(category)
15+
name = strings.ToLower(name)
16+
key := path.Join(category, name)
17+
val, ok := datasets.Load(key)
18+
if !ok {
19+
log.WithField("category", category).
20+
WithField("name", name).
21+
Warn("cannot find dataset")
22+
return nil, errors.New("cannot find dataset")
23+
}
24+
dataset, ok := val.(Dataset)
25+
if !ok {
26+
log.WithField("category", category).
27+
WithField("name", name).
28+
Warn("invalid dataset")
29+
return nil, errors.New("invalid dataset")
30+
}
31+
return dataset, nil
32+
}
33+
34+
func Register(d Dataset) {
35+
datasets.Store(d.CanonicalName(), d)
36+
}
37+
38+
func Datasets() []string {
39+
names := []string{}
40+
datasets.Range(func(key, _ interface{}) bool {
41+
if name, ok := key.(string); ok {
42+
names = append(names, name)
43+
}
44+
return true
45+
})
46+
return names
47+
}

vision/base.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package vision
2+
3+
import (
4+
"bytes"
5+
"io"
6+
7+
context "golang.org/x/net/context"
8+
)
9+
10+
type base struct {
11+
ctx context.Context
12+
baseWorkingDir string
13+
}
14+
15+
func (base) Category() string {
16+
return "vision"
17+
}
18+
19+
type LabeledImage struct {
20+
label string
21+
data []byte
22+
}
23+
24+
func (l LabeledImage) Label() string {
25+
return l.label
26+
}
27+
28+
func (l LabeledImage) Data() (io.Reader, error) {
29+
return bytes.NewBuffer(l.data), nil
30+
}

vision/checksum.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package vision
2+
3+
import (
4+
"crypto/md5"
5+
"encoding/hex"
6+
"io"
7+
"os"
8+
9+
"github.com/pkg/errors"
10+
)
11+
12+
type md5sumTy struct{}
13+
14+
var md5sum = md5sumTy{}
15+
16+
func (md5sumTy) Check(reader io.Reader, expected string) (bool, error) {
17+
hash := md5.New()
18+
_, err := io.Copy(hash, reader)
19+
if err != nil {
20+
return false, errors.Wrap(err, "failed to copy reader to md5 hash")
21+
}
22+
actual := hex.EncodeToString(hash.Sum(nil))
23+
return actual == expected, nil
24+
}
25+
26+
func (md5sumTy) CheckFile(path string, expected string) (bool, error) {
27+
f, err := os.Open(path)
28+
if err != nil {
29+
return false, errors.Wrapf(err, "failed to open %s while performing md5 checksum", path)
30+
}
31+
defer f.Close()
32+
ok, err := md5sum.Check(f, md5)
33+
if err != nil {
34+
return false, errors.Wrapf(err, "unable to perform md5sum on %s", path)
35+
}
36+
return ok, nil
37+
}

0 commit comments

Comments
 (0)