-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
565 lines (536 loc) · 14.2 KB
/
helper.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package main
import (
"fmt"
"go/build"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
)
func DepCheck(pkg, src string, re bool) {
x, er := ListPkgImports(pkg, src)
if er != nil {
fmt.Println("Get imports error:", er)
}
for i := range x {
fmt.Println(x[i])
}
}
func PackPackage(src, target string, clearGit, jumpOver bool) {
AbsSrc, er := filepath.Abs(src)
if er != nil {
fmt.Println("Error convert path!", er)
os.Exit(1)
}
AbsTar, er := filepath.Abs(target)
if er != nil {
fmt.Println("Error convert path!", er)
os.Exit(1)
}
var Pkg []string
Pkg, er = WalkPkg(AbsSrc)
if er != nil {
fmt.Println("Error walk path!", er)
os.Exit(1)
}
fmt.Println("Find Packages:", Pkg)
fmt.Println("pack packages")
for i := range Pkg {
name, er := filepath.Rel(AbsSrc, Pkg[i])
if er != nil {
fmt.Println("Error convert path!", Pkg[i], er)
}
name = strings.Replace(name, string(os.PathSeparator), "+", -1)
if clearGit {
fmt.Println("Try remove .git ", Pkg[i])
RemoveGit(Pkg[i])
}
/* fmt.Println("Gen Deps ", Pkg[i])
GenDep(Pkg[i])*/
fmt.Println("Packgeing ", Pkg[i])
Pack(Pkg[i], name, AbsTar, jumpOver)
dep := filepath.Join(Pkg[i], "dep.txt")
if FileExist(dep) {
os.RemoveAll(dep)
}
}
}
func InstallPackage(src, target, pkg string) {
pkg = strings.Replace(pkg, "/", string(os.PathSeparator), -1)
AbsSrc, er := filepath.Abs(src)
if er != nil {
fmt.Println("[S]ource path error:", er)
os.Exit(1)
}
AbsTar, err := filepath.Abs(target)
if err != nil {
fmt.Println("[T]arget path error:", er)
os.Exit(1)
}
if !IsDir(AbsSrc) {
fmt.Println("[S]ource path error: not folder")
os.Exit(1)
}
if !IsDir(AbsTar) {
fmt.Println("[T]arget path error: not folder")
os.Exit(1)
}
if ExtraPkg(pkg, AbsSrc, AbsTar) {
fmt.Println("Install package success! may be some depends not installed,Pls check log!")
return
} else {
fmt.Println("Install package Failed!")
}
}
//ReadDirNames list all file names under path p
func ReadDirNames(p string) ([]string, error) {
f, err := os.Open(p)
if err != nil {
return nil, err
}
names, err := f.Readdirnames(-1)
f.Close()
if err != nil {
return nil, err
}
sort.Strings(names)
return names, nil
}
func IsDir(p string) bool {
stat, er := os.Stat(p)
if er != nil {
return false
}
return stat.IsDir()
}
func Pack(p, name, Target string, jumpOver bool) {
lf, _ := os.OpenFile(LogPath, os.O_APPEND, 0644)
defer lf.Close()
tar := filepath.Join(Target, name+".7z")
if FileExist(tar) {
if jumpOver {
os.Remove(tar)
} else {
fmt.Fprintln(lf, "pass ", tar)
fmt.Println("pass ", tar)
return
}
}
cmd := exec.Command(z7a, "a", tar, p)
if er := cmd.Start(); er != nil {
fmt.Fprintln(lf, "Compress Error", er)
fmt.Println("Compress Error", er)
os.Exit(1)
}
cmd.Wait()
}
func FileExist(p string) bool {
if _, ok := os.Stat(p); os.IsNotExist(ok) {
return false
}
return true
}
func RemoveGit(p string) {
git := filepath.Join(p, ".git")
if FileExist(git) {
if er := os.RemoveAll(git); er != nil {
panic(er)
}
}
}
func WalkPkg(P string) ([]string, error) {
var (
Dir []string //第二层
TDir []string //第一层
Pkg []string
er error
Abs string
)
Abs, er = filepath.Abs(P) //绝对路径 **.src
if er != nil {
return nil, er
}
//读取第一层
TDir, er = ReadDirNames(Abs) //读取源列表 src/...
if er != nil {
return nil, er
}
for I := 1; I < 5; I++ {
var outD, loopD *[]string
if I%2 == 0 { //偶数
loopD = &Dir
TDir = []string{}
outD = &TDir
} else {
loopD = &TDir
Dir = []string{}
outD = &Dir
}
for k := range *loopD {
//tp := filepath.Join(Abs, Dir[k]) //获取绝对路径 src/github.com/Abs
if !strings.Contains((*loopD)[k], ":") {
(*loopD)[k] = filepath.Join(Abs, (*loopD)[k])
}
TD1 := []string{} //下层目录
Last := false
if IsDir((*loopD)[k]) { //是目录
TD, er := ReadDirNames((*loopD)[k]) //读取
if er != nil {
return nil, er
}
for i := range TD { //遍历读取的数据
f := filepath.Join((*loopD)[k], TD[i])
if IsDir(f) && strings.Index(TD[i], ".") != 0 { //跳过.文件和 .文件夹
TD1 = append(TD1, filepath.Join((*loopD)[k], TD[i]))
} else if strings.Contains(TD[i], ".git") ||
strings.Contains(TD[i], ".go") ||
strings.Contains(TD[i], "README") ||
strings.Contains(TD[i], ".gitignore") { //包含git或源代码
Last = true
}
}
if Last {
Pkg = append(Pkg, (*loopD)[k]) //添加到包
} else {
*outD = append(*outD, TD1...) //添加到继续遍历
}
}
}
}
return Pkg, nil
}
func ExtraPkg(pk, src, tar string) bool {
lf, _ := os.OpenFile(LogPath, os.O_APPEND, 0644)
defer lf.Close()
pk = strings.Replace(pk, "/", string(os.PathSeparator), -1)
pkgT := filepath.Join(tar, pk)
pkgf := filepath.Join(src, PkgToZip(pk)) + ".7z"
//fmt.Fprintln(lf, "Package file1:", pkgf)
if !FileExist(pkgf) {
//check uper package is exists?
PKS := strings.Split(pk, string(os.PathSeparator))
for i := len(PKS); i >= 0; i-- {
pkn := filepath.Join(src, PkgToZip(JoinPkg(PKS[:i]...))) + ".7z"
// fmt.Fprintln(lf, "Package file test1:", pkn)
if !FileExist(pkn) {
continue
} else {
pkgf = pkn
}
}
if !FileExist(pkgf) {
fmt.Fprintln(lf, "Package file2", pkgf, " not found!")
fmt.Println("Package file", pkgf, " not found!")
return false
}
}
if !UnPack(pkgf, pkgT) {
return false
}
dep := filepath.Join(tar, ZipToPkg(pk))
if Deps, ok := DepLoad(pk, dep); ok && len(Deps) > 0 {
for _, x := range Deps {
//x =
ExtraPkg(x, src, tar)
}
}
return true
}
func JoinPkg(p ...string) string {
return filepath.Join(p...)
}
func UnPack(f, p string) bool {
lf, _ := os.OpenFile(LogPath, os.O_APPEND, 0644)
defer lf.Close()
tar := filepath.Dir(p)
if FileExist(p) {
fmt.Fprintln(lf, "Package already exists", p)
fmt.Println("Package already exists")
os.Exit(1)
} else {
os.MkdirAll(tar, 0644)
}
cmd := exec.Command(z7a, "x", "-o"+tar, f)
cmd.Stdout = lf
cmd.Stderr = lf
if er := cmd.Start(); er != nil {
fmt.Fprintln(lf, "Decompress error:", er)
fmt.Println("Decompress error:", er)
os.Exit(1)
}
cmd.Wait()
return true
}
func DepLoad(pkg, src string) ([]string, bool) {
pk, er := ListPkgImports(pkg, src)
return pk, er == nil
}
//ZipToPkg convert package 7z name to package name
func ZipToPkg(p string) string {
return strings.Replace(p, "+", string(os.PathSeparator), -1)
}
//PkgToZip convert package name to package 7z name(with out .7z)
func PkgToZip(p string) string {
return strings.Replace(p, string(os.PathSeparator), "+", -1)
}
func ListImports(importPath, srcPath, rootPath string) ([]string, error) {
ctxt := build.Default
ctxt.BuildTags = []string{}
ctxt.GOPATH = os.Getenv("GOPATH")
pkg, err := ctxt.Import(importPath, srcPath, build.AllowBinary)
if err != nil {
if _, ok := err.(*build.NoGoError); !ok {
return nil, fmt.Errorf("fail to get imports(%s): %v", importPath, err)
}
//log.Warn("Getting imports: %v", err)
}
rawImports := pkg.Imports
numImports := len(rawImports)
//Not Test
//rawImports = append(rawImports, pkg.TestImports...)
//numImports = len(rawImports)
imports := make([]string, 0, numImports)
for _, name := range rawImports {
if IsGoRepoPath(name) {
continue
} else if strings.HasPrefix(name, rootPath) {
moreImports, err := ListImports(name, srcPath, rootPath)
if err != nil {
return nil, err
}
imports = append(imports, moreImports...)
continue
}
imports = append(imports, name)
}
return imports, nil
}
func IsGoRepoPath(importPath string) bool {
return goRepoPath[importPath]
}
var goRepoPath = map[string]bool{}
func init() {
for p := range standardPath {
for {
goRepoPath[p] = true
i := strings.LastIndex(p, "/")
if i < 0 {
break
}
p = p[:i]
}
}
}
var standardPath = map[string]bool{
"builtin": true,
// go list -f '"{{.ImportPath}}": true,' std
"archive/tar": true,
"archive/zip": true,
"bufio": true,
"bytes": true,
"compress/bzip2": true,
"compress/flate": true,
"compress/gzip": true,
"compress/lzw": true,
"compress/zlib": true,
"container/heap": true,
"container/list": true,
"container/ring": true,
"context": true,
"crypto": true,
"crypto/aes": true,
"crypto/cipher": true,
"crypto/des": true,
"crypto/dsa": true,
"crypto/ecdsa": true,
"crypto/elliptic": true,
"crypto/hmac": true,
"crypto/md5": true,
"crypto/rand": true,
"crypto/rc4": true,
"crypto/rsa": true,
"crypto/sha1": true,
"crypto/sha256": true,
"crypto/sha512": true,
"crypto/subtle": true,
"crypto/tls": true,
"crypto/x509": true,
"crypto/x509/pkix": true,
"database/sql": true,
"database/sql/driver": true,
"debug/dwarf": true,
"debug/elf": true,
"debug/gosym": true,
"debug/macho": true,
"debug/pe": true,
"debug/plan9obj": true,
"encoding": true,
"encoding/ascii85": true,
"encoding/asn1": true,
"encoding/base32": true,
"encoding/base64": true,
"encoding/binary": true,
"encoding/csv": true,
"encoding/gob": true,
"encoding/hex": true,
"encoding/json": true,
"encoding/pem": true,
"encoding/xml": true,
"errors": true,
"expvar": true,
"flag": true,
"fmt": true,
"go/ast": true,
"go/build": true,
"go/constant": true,
"go/doc": true,
"go/format": true,
"go/importer": true,
"go/internal/gccgoimporter": true,
"go/internal/gcimporter": true,
"go/parser": true,
"go/printer": true,
"go/scanner": true,
"go/token": true,
"go/types": true,
"hash": true,
"hash/adler32": true,
"hash/crc32": true,
"hash/crc64": true,
"hash/fnv": true,
"html": true,
"html/template": true,
"image": true,
"image/color": true,
"image/color/palette": true,
"image/draw": true,
"image/gif": true,
"image/internal/imageutil": true,
"image/jpeg": true,
"image/png": true,
"index/suffixarray": true,
"internal/race": true,
"internal/singleflight": true,
"internal/testenv": true,
"internal/trace": true,
"io": true,
"io/ioutil": true,
"log": true,
"log/syslog": true,
"math": true,
"math/big": true,
"math/cmplx": true,
"math/rand": true,
"mime": true,
"mime/multipart": true,
"mime/quotedprintable": true,
"net": true,
"net/http": true,
"net/http/cgi": true,
"net/http/cookiejar": true,
"net/http/fcgi": true,
"net/http/httptest": true,
"net/http/httputil": true,
"net/http/internal": true,
"net/http/pprof": true,
"net/internal/socktest": true,
"net/mail": true,
"net/rpc": true,
"net/rpc/jsonrpc": true,
"net/smtp": true,
"net/textproto": true,
"net/url": true,
"os": true,
"os/exec": true,
"os/signal": true,
"os/user": true,
"path": true,
"path/filepath": true,
"reflect": true,
"regexp": true,
"regexp/syntax": true,
"runtime": true,
"runtime/cgo": true,
"runtime/debug": true,
"runtime/internal/atomic": true,
"runtime/internal/sys": true,
"runtime/pprof": true,
"runtime/race": true,
"runtime/trace": true,
"sort": true,
"strconv": true,
"strings": true,
"sync": true,
"sync/atomic": true,
"syscall": true,
"testing": true,
"testing/iotest": true,
"testing/quick": true,
"text/scanner": true,
"text/tabwriter": true,
"text/template": true,
"text/template/parse": true,
"time": true,
"unicode": true,
"unicode/utf16": true,
"unicode/utf8": true,
"unsafe": true,
}
func ListPkgImports(pkg, pkgPath string) ([]string, error) {
if strings.Contains(pkg, string(os.PathSeparator)) {
pkg = strings.Replace(pkg, string(os.PathSeparator), "/", -1)
}
x, err := ListImports(pkg, pkgPath, pkg)
if err != nil {
return x, err
}
SortViaLen(x)
x1 := make([]string, 0, len(x))
for i := range x {
if !strings.HasPrefix(x[i], pkg) {
if !SearchHasPrefix(x1, x[i]) {
x1 = append(x1, x[i])
}
}
}
return x1, nil
}
func SearchMatch(a []string, x string) bool {
//sort.Strings(a)
//return sort.Search(len(a), func(i int) bool { return a[i] == x })
for _, v := range a {
if v == x {
return true
}
}
return false
}
func SearchHasPrefix(a []string, x string) bool {
for _, v := range a {
switch {
case len(v) < len(x):
if strings.HasPrefix(x, v) {
return true
}
default:
if strings.HasPrefix(v, x) {
return true
}
}
}
return false
}
func SortViaLen(a []string) {
sort.Sort(ByLength(a))
}
type ByLength []string
func (s ByLength) Len() int {
return len(s)
}
func (s ByLength) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ByLength) Less(i, j int) bool {
return len(s[i]) < len(s[j])
}