forked from hwaf/hwaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_self_bdist.go
200 lines (167 loc) · 4.47 KB
/
cmd_self_bdist.go
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"time"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"github.com/hwaf/gas"
)
func hwaf_make_cmd_self_bdist() *commander.Command {
cmd := &commander.Command{
Run: hwaf_run_cmd_self_bdist,
UsageLine: "bdist [options]",
Short: "create a binary distribution of hwaf itself",
Long: `
self bdist creates a binary distribution of hwaf itself.
ex:
$ hwaf self bdist
$ hwaf self bdist -version=20130101
`,
Flag: *flag.NewFlagSet("hwaf-self-bdist", flag.ExitOnError),
}
cmd.Flag.Bool("v", false, "enable verbose output")
cmd.Flag.String("version", "", "version of the binary distribution (default: 'time now')")
return cmd
}
func hwaf_run_cmd_self_bdist(cmd *commander.Command, args []string) {
var err error
n := "hwaf-self-" + cmd.Name()
switch len(args) {
case 0:
// ok
default:
err = fmt.Errorf("%s: does NOT take any argument", n)
handle_err(err)
}
verbose := cmd.Flag.Lookup("v").Value.Get().(bool)
bdist_name := "hwaf"
bdist_vers := cmd.Flag.Lookup("version").Value.Get().(string)
bdist_cmtcfg := fmt.Sprintf("%s-%s", runtime.GOOS, runtime.GOARCH)
if bdist_vers == "" {
bdist_vers = time.Now().Format("20060102")
}
dirname := fmt.Sprintf("%s-%s-%s", bdist_name, bdist_vers, bdist_cmtcfg)
fname := dirname + ".tar.gz"
if verbose {
fmt.Printf("%s [%s]...\n", n, fname)
}
tmpdir, err := ioutil.TempDir("", "hwaf-self-bdist-")
handle_err(err)
defer os.RemoveAll(tmpdir)
//fmt.Printf(">>> [%s]\n", tmpdir)
//
top := filepath.Join(tmpdir, dirname)
// create hierarchy of dirs for bdist
for _, dir := range []string{
"bin",
"share",
filepath.Join("share", "hwaf"),
} {
err = os.MkdirAll(filepath.Join(top, dir), 0755)
handle_err(err)
}
// add hep-waftools cache
hwaf_dir, err := gas.Abs("github.com/hwaf/hwaf")
handle_err(err)
src_hwaf_tools := filepath.Join(hwaf_dir, "py-hwaftools")
hwaf_tools := filepath.Join(top, "share", "hwaf", "tools")
err = copytree(hwaf_tools, src_hwaf_tools)
handle_err(err)
// remove git stuff
err = os.RemoveAll(filepath.Join(hwaf_tools, ".git"))
handle_err(err)
// add share/hwaf/hwaf.conf
err = ioutil.WriteFile(
filepath.Join(top, "share", "hwaf", "hwaf.conf"),
[]byte(`# hwaf config file
[hwaf]
## EOF ##
`),
0644,
)
handle_err(err)
// temporary GOPATH - install go-deps
gopath := filepath.Join(tmpdir, "gocode")
err = os.MkdirAll(gopath, 0755)
handle_err(err)
orig_gopath := os.Getenv("GOPATH")
err = os.Setenv("GOPATH", gopath)
handle_err(err)
defer os.Setenv("GOPATH", orig_gopath)
for _, gopkg := range []string{
"github.com/hwaf/hwaf",
"github.com/hwaf/git-tools/git-archive-all",
"github.com/hwaf/git-tools/git-rm-submodule",
"github.com/hwaf/git-tools/git-check-clean",
"github.com/hwaf/git-tools/git-check-non-tracking",
"github.com/hwaf/git-tools/git-check-unpushed",
} {
goget := exec.Command("go", "get", "-v", gopkg)
goget.Dir = gopath
if verbose {
goget.Stdout = os.Stdout
goget.Stderr = os.Stderr
}
err = goget.Run()
handle_err(err)
// install under /bin
dst_fname := filepath.Join(top, "bin", filepath.Base(gopkg))
dst, err := os.OpenFile(dst_fname, os.O_WRONLY|os.O_CREATE, 0755)
handle_err(err)
defer func(dst *os.File) {
err := dst.Sync()
handle_err(err)
err = dst.Close()
handle_err(err)
}(dst)
src_fname := filepath.Join(gopath, "bin", filepath.Base(gopkg))
if !path_exists(src_fname) {
// maybe a cross-compilation ?
src_fname = filepath.Join(gopath, "bin", runtime.GOOS+"_"+runtime.GOARCH, filepath.Base(gopkg))
}
src, err := os.Open(src_fname)
handle_err(err)
defer func(src *os.File) {
err := src.Close()
handle_err(err)
}(src)
_, err = io.Copy(dst, src)
handle_err(err)
}
// add waf-bin
waf_fname := filepath.Join(top, "bin", "waf")
if path_exists(waf_fname) {
err = os.Remove(waf_fname)
handle_err(err)
}
waf_dst, err := os.OpenFile(waf_fname, os.O_WRONLY|os.O_CREATE, 0777)
handle_err(err)
defer func() {
err = waf_dst.Sync()
handle_err(err)
err = waf_dst.Close()
handle_err(err)
}()
waf_src, err := os.Open(filepath.Join(
gopath, "src", "github.com", "hwaf", "hwaf", "waf"),
)
handle_err(err)
defer waf_src.Close()
_, err = io.Copy(waf_dst, waf_src)
handle_err(err)
pwd, err := os.Getwd()
handle_err(err)
// package everything up
err = _tar_gz(filepath.Join(pwd, fname), top)
handle_err(err)
if verbose {
fmt.Printf("%s [%s]... [ok]\n", n, fname)
}
}
// EOF