forked from hwaf/hwaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_setup.go
182 lines (156 loc) · 4.51 KB
/
cmd_setup.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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/gonuts/commander"
gocfg "github.com/gonuts/config"
"github.com/gonuts/flag"
"github.com/hwaf/hwaf/hwaflib"
)
func hwaf_make_cmd_setup() *commander.Command {
cmd := &commander.Command{
Run: hwaf_run_cmd_setup,
UsageLine: "setup [options] <workarea>",
Short: "setup an existing workarea",
Long: `
setup sets up an existing workarea.
ex:
$ hwaf setup
$ hwaf setup .
$ hwaf setup my-work-area
$ hwaf setup -p=/opt/sw/mana/mana-core/20121207 my-work-area
$ hwaf setup -p=/path1:/path2 my-work-area
$ hwaf setup -cfg=${HWAF_CFG}/usr.cfg my-work-area
$ hwaf setup -tags=ATLAS,NEED_PYCOOL my-work-area
$ hwaf setup -variant=x86_64-slc6-gcc47-opt my-work-area
`,
Flag: *flag.NewFlagSet("hwaf-setup", flag.ExitOnError),
}
cmd.Flag.String("p", "", "List of paths to projects to setup against")
cmd.Flag.String("cfg", "", "Path to a configuration file")
cmd.Flag.String("pkgdir", "src", "Directory under which to checkout packages")
cmd.Flag.String("variant", "", "quadruplet (e.g. x86_64-slc6-gcc47-opt) identifying the target to build for")
cmd.Flag.String("tags", "", "comma-separated list of tags to enable for this build")
cmd.Flag.Bool("v", false, "enable verbose output")
return cmd
}
func hwaf_run_cmd_setup(cmd *commander.Command, args []string) {
var err error
n := "hwaf-" + cmd.Name()
dirname := "."
switch len(args) {
case 0:
dirname = "."
case 1:
dirname = args[0]
default:
err = fmt.Errorf("%s: you need to give a directory name", n)
handle_err(err)
}
dirname = os.ExpandEnv(dirname)
dirname = filepath.Clean(dirname)
verbose := cmd.Flag.Lookup("v").Value.Get().(bool)
cfg_fname := cmd.Flag.Lookup("cfg").Value.Get().(string)
pkgdir := cmd.Flag.Lookup("pkgdir").Value.Get().(string)
variant := cmd.Flag.Lookup("variant").Value.Get().(string)
tags := cmd.Flag.Lookup("tags").Value.Get().(string)
projdirs := []string{}
const pathsep = string(os.PathListSeparator)
for _, v := range strings.Split(cmd.Flag.Lookup("p").Value.Get().(string), pathsep) {
if v != "" {
v = os.ExpandEnv(v)
v = filepath.Clean(v)
projdirs = append(projdirs, v)
}
}
if verbose {
fmt.Printf("%s: setup workarea [%s]...\n", n, dirname)
fmt.Printf("%s: projects=%v\n", n, projdirs)
if cfg_fname != "" {
fmt.Printf("%s: cfg-file=%s\n", n, cfg_fname)
}
}
if cfg_fname != "" && !path_exists(cfg_fname) {
err = fmt.Errorf("configuration file [%s] does not exist (or is not readable)", cfg_fname)
handle_err(err)
}
for _, projdir := range projdirs {
if !path_exists(projdir) {
err = fmt.Errorf("no such directory: [%s]", projdir)
handle_err(err)
}
pinfo := filepath.Join(projdir, "project.info")
if !path_exists(pinfo) {
err = fmt.Errorf("no such file: [%s]", pinfo)
handle_err(err)
}
}
pwd, err := os.Getwd()
handle_err(err)
defer os.Chdir(pwd)
err = os.Chdir(dirname)
handle_err(err)
if verbose {
fmt.Printf("%s: create local config...\n", n)
}
var lcfg *gocfg.Config
lcfg_fname := "local.conf"
// if the user provided a configuration file use that as a default
if cfg_fname != "" && path_exists(cfg_fname) {
lcfg, err = gocfg.ReadDefault(cfg_fname)
handle_err(err)
} else {
if path_exists(lcfg_fname) {
lcfg, err = gocfg.ReadDefault(lcfg_fname)
handle_err(err)
} else {
lcfg = gocfg.NewDefault()
}
}
section := "hwaf-cfg"
if !lcfg.HasSection(section) && !lcfg.AddSection(section) {
err = fmt.Errorf("%s: could not create section [%s] in file [%s]",
n, section, lcfg_fname)
handle_err(err)
}
// fetch a few informations from the first project.info
cmtcfg := g_ctx.Cmtcfg()
//projvers := time.Now().Format("20060102")
if len(projdirs) > 0 {
pinfo, err := hwaflib.NewProjectInfo(filepath.Join(projdirs[0], "project.info"))
handle_err(err)
cmtcfg, err = pinfo.Get("CMTCFG")
handle_err(err)
}
if variant != "" {
cmtcfg = variant
}
if tags != "" {
tags_slice := strings.Split(tags, ",")
tags = strings.Join(tags_slice, " ")
}
for k, v := range map[string]string{
"projects": strings.Join(projdirs, pathsep),
"pkgdir": pkgdir,
"cmtcfg": cmtcfg,
"tags": tags,
} {
if lcfg.HasOption(section, k) {
lcfg.RemoveOption(section, k)
}
if !lcfg.AddOption(section, k, v) {
err := fmt.Errorf("%s: could not add option [%s] to section [%s]",
n, k, section,
)
handle_err(err)
}
}
err = lcfg.WriteFile(lcfg_fname, 0600, "")
handle_err(err)
if verbose {
fmt.Printf("%s: setup workarea [%s]... [ok]\n", n, dirname)
}
}
// EOF