forked from marguerite/golang-packaging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolang-strip-builddep
executable file
·140 lines (72 loc) · 2.43 KB
/
golang-strip-builddep
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
#!/usr/bin/env ruby
# strip unneeded build dependencies from golang source codes
def find_build_directory()
path = "/home/abuild/rpmbuild/SOURCES"
#path = `pwd`.gsub(/\n/,'')
specfile = Dir.glob(path + "/*.spec")[0]
pkgname = ""
File.open(specfile) do |f|
f.each_line do |l|
build_found = 0
if l.index("Source:") then
unless l.index("%{name}") then
pkgname = l.gsub(/Source:/,'').lstrip!.chomp!.gsub(/^.*\//,'').gsub(/-%.*$/,'')
build_found = 1
end
end
if (build_found == 0 && l.index("Name:")) then
pkgname = l.gsub(/Name:/,'').lstrip!.chomp!.gsub(/^(go|golang)-/,'')
end
end
end
build = Dir.glob("/home/abuild/rpmbuild/BUILD/*#{pkgname}*")[0]
#build = Dir.glob(path + "/*#{pkgname}*")[0]
return build
end
def all_file(dir="",result=nil)
result = [] unless result
Dir.entries(dir).each do |d|
unless (d == "." || d == ".." || d.index("example") || d.index("test")) then
if File.directory?("#{dir}/#{d}") then
all_file("#{dir}/#{d}",result)
else
if ( d.index(".go") && ! d.index(/(test|example)/) ) then
result << "#{dir}/#{d}"
end
end
end
end
return result
end
def get_commandline_options()
options = []
$*.each do |o|
options << o
end
return options
end
def delete_line(filepath="",line="")
require 'fileutils'
newpath = filepath + ".new"
File.open(filepath,'r') do |f1|
File.open(newpath,'w') do |f2|
f1.each_line do |l|
f2.write(l) unless l.index(line)
end
end
end
FileUtils.mv newpath, filepath
end
def strip_unneed_dependency()
build = find_build_directory()
source = all_file(build)
options = get_commandline_options()
options.each do |opt|
source.each do |s|
if File.readlines(s, :encoding => "UTF-8").grep(/#{opt}/).size > 0
delete_line(s,opt)
end
end
end
end
strip_unneed_dependency()