github.com/hy3/cuto@v0.9.8-0.20160830082821-aa6652f877b7/master/jobnet/parser/jobex.go (about) 1 // Copyright 2015 unirita Inc. 2 // Created 2015/04/10 honda 3 4 package parser 5 6 import ( 7 "encoding/csv" 8 "io" 9 "os" 10 "strconv" 11 12 "github.com/unirita/cuto/log" 13 ) 14 15 // 拡張ジョブ情報 16 type JobEx struct { 17 Node string // ノード名 18 Port int // ポート番号 19 FilePath string // ジョブファイルパス 20 Param string // 実行時引数 21 Env string // 実行時環境変数 22 Workspace string // 作業フォルダ 23 WrnRC int // 警告終了判断に使用するリターンコードの下限値 24 WrnPtn string // 警告終了と判断する出力文字列 25 ErrRC int // 異常終了判断に使用するリターンコードの下限値 26 ErrPtn string // 異常終了と判断する出力文字列 27 TimeoutMin int // タイムアウト(分) 28 SecondaryNode string // ノード名 29 SecondaryPort int // ポート番号 30 } 31 32 // CSVファイルの項目数 33 const ( 34 noSecondary = 12 35 withSecondary = 14 36 ) 37 38 // 項目のインデックス 39 const ( 40 nameIdx = iota 41 nodeIdx 42 portIdx 43 pathIdx 44 paramIdx 45 envIdx 46 workIdx 47 wrcIdx 48 wptIdx 49 ercIdx 50 eptIdx 51 tmoutIdx 52 secNodeIdx 53 secPortIdx 54 ) 55 56 // JobEx構造体のオブジェクトを生成しする。 57 // 58 // return : 生成したオブジェクト 59 func NewJobEx() *JobEx { 60 je := new(JobEx) 61 je.TimeoutMin = -1 62 return je 63 } 64 65 // ファイルから拡張ジョブ定義CSVを読み込み、パースする。 66 // 67 // param : fileName ファイル名。 68 // 69 // return : 拡張ジョブ情報のパース後Map。 70 // 71 // return : エラー情報。 72 func ParseJobExFile(fileName string) (map[string]*JobEx, error) { 73 file, err := os.Open(fileName) 74 if err != nil { 75 if os.IsNotExist(err) { 76 // ファイルが存在しない場合は空のマップを返す 77 return make(map[string]*JobEx), nil 78 } else { 79 return nil, err 80 } 81 } 82 defer file.Close() 83 84 return ParseJobEx(file) 85 } 86 87 // readerから読み込んだ拡張ジョブ定義CSVをパースする。 88 // 空のカラムにはゼロ値をセットする。 89 // 90 // param : reader ファイルリーダー。 91 // 92 // return : 拡張ジョブ情報のパース後Map。 93 // 94 // return : エラー情報。 95 func ParseJobEx(reader io.Reader) (map[string]*JobEx, error) { 96 r := csv.NewReader(reader) 97 jobExMap := make(map[string]*JobEx) 98 99 for i := 1; ; i++ { 100 record, err := r.Read() 101 if err == io.EOF { 102 break 103 } else if err != nil { 104 return nil, err 105 } 106 107 if i == 1 { 108 // タイトル行を無視する 109 continue 110 } 111 112 if len(record) != noSecondary && len(record) != withSecondary { 113 log.Info("Jobex line[%d] was ignored: Irregal column count[%d].", i, len(record)) 114 continue 115 } 116 117 name := record[nameIdx] 118 if len(name) == 0 { 119 log.Info("Jobex line[%d] was ignored: Empty job name.", i) 120 continue 121 } 122 123 je := NewJobEx() 124 je.Node = record[nodeIdx] 125 if port, err := strconv.Atoi(record[portIdx]); err == nil { 126 je.Port = port 127 } 128 je.FilePath = record[pathIdx] 129 je.Param = record[paramIdx] 130 je.Env = record[envIdx] 131 je.Workspace = record[workIdx] 132 if wrc, err := strconv.Atoi(record[wrcIdx]); err == nil { 133 je.WrnRC = wrc 134 } 135 je.WrnPtn = record[wptIdx] 136 if erc, err := strconv.Atoi(record[ercIdx]); err == nil { 137 je.ErrRC = erc 138 } 139 je.ErrPtn = record[eptIdx] 140 if tmout, err := strconv.Atoi(record[tmoutIdx]); err == nil { 141 je.TimeoutMin = tmout 142 } 143 144 if len(record) >= withSecondary { 145 je.SecondaryNode = record[secNodeIdx] 146 if port, err := strconv.Atoi(record[secPortIdx]); err == nil { 147 je.SecondaryPort = port 148 } 149 } 150 151 jobExMap[name] = je 152 } 153 154 return jobExMap, nil 155 }