forked from gofed/gofed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchImportPaths.py
194 lines (153 loc) · 4.99 KB
/
fetchImportPaths.py
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
from modules.Packages import Package
from modules.Packages import loadPackages, savePackageInfo, LocalDB
import optparse
from modules.ImportPathDB import ImportPathDB
from modules.Utils import FormatedPrint
from modules.ImportPathDB import ImportPathDBCache
from modules.Config import Config
from time import time, strftime, gmtime
import sys
import os
def createDB(full=False, verbose=False):
scan_time_start = time()
packages = []
outdated = {}
valid = {}
if full:
packages = loadPackages()
else:
print "Creating list of updated builds..."
err, outdated = LocalDB().getOutdatedBuilds()
if err != []:
print "Warning: " + "\nWarning: ".join(err)
packages = outdated.keys()
if verbose:
print "Packages to be scanned:"
for pkg in packages:
print "\t%s" % pkg
print ""
pkg_cnt = len(packages)
pkg_idx = 1
pkg_name_len = 0
for package in packages:
l = len(package)
if l > pkg_name_len:
pkg_name_len = l
pkg_cnt_len = len("%s" % pkg_cnt)
print "Updating spec file provides..."
err, ret = LocalDB().updatePackages(packages)
if not ret:
print "Error:\n" + "\n".join(err)
return False
ipdb_cache = ImportPathDBCache()
if not ipdb_cache.load():
print "Error: %s" % ipdb_cache.getError()
return False
golang_pkg = Config().getGolangPkgdb()
for package in packages:
starttime = time()
# len of pkg_idx
pkg_idx_len = len("%s" % pkg_idx)
sys.stdout.write("Scanning %s %s %s%s/%s " % (package, (pkg_name_len - len(package) + 3) * ".", (pkg_cnt_len - pkg_idx_len) * " " , pkg_idx, pkg_cnt))
sys.stdout.flush()
pkg = Package(package)
info = pkg.getInfo()
# save xml into file
errs = savePackageInfo(info)
if errs != []:
print ""
print "\n".join(errs)
else:
if not full:
valid[package] = outdated[package]
pkg_idx += 1
endtime = time()
elapsedtime = endtime - starttime
print strftime("[%Hh %Mm %Ss]", gmtime(elapsedtime))
# update cache of imported and provided packages
for item in info:
devel_name = item
# This is hacky and depends on info data type represenation
pkg2xml_obj = info[item]['xmlobj']
imports = pkg2xml_obj.getImportedPackages()
provides = pkg2xml_obj.getProvidedPackages()
ipdb_cache.updateBuild(devel_name, provides, imports, package)
if not ipdb_cache.flush():
print ipdb_cache.getError()
scan_time_end = time()
print strftime("Elapsed time %Hh %Mm %Ss", gmtime(scan_time_end - scan_time_start))
if not full:
LocalDB().updateBuildsInCache(valid)
return True
def displayPaths(paths, prefix = '', minimal = False):
for pkg in paths:
found = False
ips = []
for path in paths[pkg]:
if path.startswith(prefix):
found = True
ips.append(path)
if found:
print pkg
if not minimal:
for ip in ips:
print "\t%s" % ip
if __name__ == "__main__":
parser = optparse.OptionParser("%prog [-c [-f]] [-i|-p [-s [-m]]]")
#parser.add_option_group( optparse.OptionGroup(parser, "directory", "Directory to inspect. If empty, current directory is used.") )
parser.add_option(
"", "-c", "--create", dest="create", action = "store_true", default = False,
help = "Create database of import and imported paths for all available builds of golang devel source packages"
)
parser.add_option(
"", "-f", "--full", dest="full", action = "store_true", default = False,
help = "Regenerate the entire database. Default is to regenerate only updated builds."
)
parser.add_option(
"", "-i", "--imported", dest="imports", action = "store_true", default = False,
help = "List all import paths devel packages need"
)
parser.add_option(
"", "-p", "--provided", dest="provides", action = "store_true", default = False,
help = "List all import paths devel packages provide"
)
parser.add_option(
"", "-s", "--prefix", dest="prefix", default = "",
help = "Prefix of import paths to display. Used with -i and -p options."
)
parser.add_option(
"", "-m", "--minimal", dest="minimal", action = "store_true", default = False,
help = "List only packages. Used with -s option."
)
parser.add_option(
"", "-v", "--verbose", dest="verbose", action = "store_true", default = False,
help = "Verbose mode."
)
options, args = parser.parse_args()
fmt_obj = FormatedPrint(formated=False)
if options.imports and options.provides:
print "You can not set both -i and -o options at the same time"
exit(1)
if options.create:
if createDB(full=options.full, verbose=options.verbose):
print "DB updated"
else:
print "DB not updated"
elif options.imports:
ipdb_obj = ImportPathDB()
if not ipdb_obj.load():
fmt_obj.printError(ipdb_obj.getError())
exit(1)
paths = ipdb_obj.getImportedPaths()
displayPaths(paths, options.prefix, options.minimal)
elif options.provides:
ipdb_obj = ImportPathDB()
if not ipdb_obj.load():
fmt_obj.printError(ipdb_obj.getError())
exit(1)
paths = ipdb_obj.getProvidedPaths()
displayPaths(paths, options.prefix, options.minimal)
else:
print "Synopsis: prog [-c [-f]] [-i|-p [-s [-m]]]"
exit(1)
exit(0)