~rogpeppe/juju-core/438-local-instance-Addresses

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package formula

import (
	"archive/zip"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
)

// ReadDir returns a Dir representing an expanded formula directory.
func ReadDir(path string) (dir *Dir, err os.Error) {
	dir = &Dir{Path: path}
	file, err := os.Open(dir.join("metadata.yaml"))
	if err != nil {
		return nil, err
	}
	dir.meta, err = ReadMeta(file)
	file.Close()
	if err != nil {
		return nil, err
	}
	file, err = os.Open(dir.join("config.yaml"))
	if err != nil {
		return nil, err
	}
	dir.config, err = ReadConfig(file)
	file.Close()
	if err != nil {
		return nil, err
	}
	return dir, nil
}

// The Dir type encapsulates access to data and operations
// on a formula directory.
type Dir struct {
	Path   string
	meta   *Meta
	config *Config
}

// Trick to ensure Dir implements the Formula interface.
var _ Formula = (*Dir)(nil)

// Meta returns the Meta representing the metadata.yaml file
// for the formula expanded in dir.
func (dir *Dir) Meta() *Meta {
	return dir.meta
}

// Config returns the Config representing the config.yaml file
// for the formula expanded in dir.
func (dir *Dir) Config() *Config {
	return dir.config
}

// BundleTo creates a formula file from the formula expanded in dir.
func (dir *Dir) BundleTo(w io.Writer) (err os.Error) {
	zipw := zip.NewWriter(w)
	defer func() {
		zipw.Close()
		handleZipError(&err)
	}()
	visitor := zipVisitor{zipw, dir.Path}
	walk(dir.Path, &visitor)
	return nil
}

type zipVisitor struct {
	*zip.Writer
	root string
}

func (zipw *zipVisitor) VisitDir(path string, f *os.FileInfo) bool {
	relpath, err := filepath_Rel(zipw.root, path)
	zipw.Error(path, err)
	return relpath != "build"
}

func (zipw *zipVisitor) VisitFile(path string, f *os.FileInfo) {
	relpath, err := filepath_Rel(zipw.root, path)
	zipw.Error(path, err)
	w, err := zipw.Create(relpath)
	zipw.Error(path, err)
	data, err := ioutil.ReadFile(path)
	zipw.Error(path, err)
	_, err = w.Write(data)
	zipw.Error(path, err)
}

type zipError os.Error

func (zipw *zipVisitor) Error(path string, err os.Error) {
	if err != nil {
		panic(zipError(err))
	}
}

func handleZipError(err *os.Error) {
	if *err != nil {
		return // Do not override a previous problem
	}
	panicv := recover()
	if panicv == nil {
		return
	}
	if e, ok := panicv.(zipError); ok {
		*err = (os.Error)(e)
		return
	}
	panic(panicv) // Something else
}

// join builds a path rooted at the formula's expended directory
// path and the extra path components provided.
func (dir *Dir) join(parts ...string) string {
	parts = append([]string{dir.Path}, parts...)
	return filepath.Join(parts...)
}