forked from gofed/gofed
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepo2gospec.py
349 lines (272 loc) · 9.22 KB
/
repo2gospec.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
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
import optparse
from modules.Utils import ENDC, RED, GREEN
from modules.Utils import runCommand, FormatedPrint
from modules.Packages import packageInPkgdb
from modules.ImportPath import ImportPath
from modules.PackageInfo import PackageInfo
from modules.SpecGenerator import SpecGenerator
from modules.ImportPathsDecomposer import ImportPathsDecomposer
from modules.Config import Config
import os
import sys
import errno
def setOptions():
parser = optparse.OptionParser("%prog [-e] [-d] file [file [file ...]]")
sln = not (os.path.basename(sys.argv[0]) == "repo2gospec.py")
github = os.path.basename(sys.argv[0]) == "github2gospec"
googlecode = os.path.basename(sys.argv[0]) == "googlecode2gospec"
bitbucket = os.path.basename(sys.argv[0]) == "bitbucket2gospec"
SH = optparse.SUPPRESS_HELP
parser.add_option(
"", "", "--github", dest="github", action="store_true", default = False,
help = SH if sln else "github.com repository"
)
parser.add_option(
"", "", "--googlecode", dest="googlecode", action="store_true", default = False,
help = SH if sln else "code.google.com repository"
)
parser.add_option(
"", "", "--bitbucket", dest="bitbucket", action="store_true", default = False,
help = SH if sln else "bitbucket.org repository"
)
parser.add_option(
"", "", "--detect", dest="detect", default = "",
help = SH if sln else "Detect repository from import path"
)
parser.add_option(
"", "", "--skip-errors", dest="skiperrors", action="store_true", default = False,
help = SH if sln else "Skip errors during Go symbol parsing"
)
if github:
help_text = "Repository name, github.com/project/REPO"
elif googlecode:
help_text = "Repository name, code.google.com/p/REPO"
elif bitbucket:
help_text = "Repository name, bitbucket.org/project/REPO"
else:
help_text = "Repository name, e.g. github.com/project/REPO"
parser.add_option(
"", "-r", "--repo", dest="repo", default = "",
help = help_text
)
if github:
help_text = "Repository name, github.com/PROJECT/repository"
elif bitbucket:
help_text = "Repository name, bitbucket.org/PROJECT/repository"
else:
help_text = "Repository name, e.g. github.com/PROJECT/repository"
parser.add_option(
"", "-p", "--project", dest="project", default = "",
help = SH if googlecode else help_text
)
if googlecode:
parser.add_option(
"", "-c", "--rev", dest="revision", default = "",
help = "Revision"
)
else:
parser.add_option(
"", "-c", "--commit", dest="commit", default = "",
help = "Commit. If not specified the latest is taken."
)
parser.add_option(
"", "-f", "--format", dest="format", action="store_true", default = False,
help = "Make messages more shiny"
)
parser.add_option(
"", "", "--force", dest="force", action="store_true", default = False,
help = "Generate spec file even if it is already in Fedora"
)
parser.add_option(
"", "", "--scan-all-dirs", dest="scanalldirs", action = "store_true", default = False,
help = "Scan all dirs, including Godeps directory"
)
parser.add_option(
"", "", "--skip-dirs", dest="skipdirs", default = "",
help = "Scan all dirs except specified via SKIPDIRS. Directories are comma separated list."
)
return parser.parse_args()
def checkOptions(options):
fail = False
if options.detect != "":
return False
if not options.github and not options.googlecode and not options.bitbucket:
print "No provider specified"
fail = True
if options.github or options.googlecode or options.bitbucket:
if options.repo == "":
print "Repository missing"
fail = True
if options.github or options.bitbucket:
if options.project == "":
print "Project missing"
fail = True
if options.googlecode:
if options.revision == "":
print "Revision missing"
fail = True
return fail
def printBasicInfo(url, commit, name, formated=True):
fmt_obj = FormatedPrint(formated)
fmt_obj.printInfo("Repo URL: %s" % url)
fmt_obj.printInfo("Commit: %s" % commit)
fmt_obj.printInfo("Name: %s" % name)
# http://stackoverflow.com/questions/273192/in-python-check-if-a-directory-exists-and-create-it-if-necessary
def make_sure_path_exists(path):
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
def isPkgInPkgDB(name, force):
if packageInPkgdb(name):
print "%sPackage %s already exists%s" % (RED, name, ENDC)
if not force:
exit(1)
def createBasicDirectories(name):
make_sure_path_exists("%s/fedora/%s" % (name, name))
os.chdir("%s/fedora/%s" % (name, name))
def downloadTarball(archive_url):
so, se, rc = runCommand("wget -nv %s --no-check-certificate" % archive_url)
if rc != 0:
print "%sUnable to download tarball:\n%s%s" % (RED, se, ENDC)
exit(1)
if __name__ == "__main__":
options, args = setOptions()
if checkOptions(options):
exit(1)
fmt_obj = FormatedPrint(options.format)
if not options.format:
ENDC = ""
RED = ""
GREEN = ""
if options.detect == "":
# collect spec file information
project = options.project
repo = options.repo
if project == "":
fmt_obj.printError("Project missing")
exit(1)
if repo == "":
fmt_obj.printError("Repository missing")
exit(1)
if options.github:
import_path = "github.com/%s/%s" % (project, repo)
commit = options.commit
elif options.googlecode:
import_path = "code.google.com/p/%s" % repo
commit = options.rev
elif options.bitbucket:
import_path = "bitbucket.org/%s/%s" % (project, repo)
commit = options.commit
else:
fmt_obj.printError("Provider not supported")
exit(1)
else:
import_path = options.detect
commit = options.commit
if not options.scanalldirs:
noGodeps = Config().getSkippedDirectories()
else:
noGodeps = []
if options.skipdirs:
for dir in options.skipdirs.split(','):
dir = dir.strip()
if dir == "":
continue
noGodeps.append(dir)
# 1. decode some package info (name, archive url, ...)
# 2. set path to downloaded tarball
# 3. retrieve project info from tarball
# 4. generate spec file
pkg_obj = PackageInfo(import_path, commit, noGodeps)
if not pkg_obj.decodeRepository():
fmt_obj.printError(pkg_obj.getError())
exit(1)
name = pkg_obj.getName()
if name == "":
fmt_obj.printError("Unable to generate package name for %s" % url)
exit(1)
specfile = "%s.spec" % name
total = 4
# print basic information
repository_info = pkg_obj.getRepositoryInfo()
if repository_info == None:
fmt_obj.printError("RepositoryInfo not set")
exit(1)
url = repository_info.getImportPathInfo().getPrefix()
commit = repository_info.getCommit()
archive_url = repository_info.getArchiveInfo().archive_url
archive = repository_info.getArchiveInfo().archive
printBasicInfo(url, commit, name, options.format)
print ""
# is the package already in Fedora
fmt_obj.printProgress("(1/%s) Checking if the package already exists in PkgDB" % total)
isPkgInPkgDB(name, options.force)
# creating basic folder structure
createBasicDirectories(name)
# download tarball
fmt_obj.printProgress("(2/%s) Downloading tarball" % total)
downloadTarball(archive_url)
so, se, rc = runCommand("tar -xf %s" % archive)
if rc != 0:
fmt_obj.printErr("Unable to extract %s" % archive)
exit(1)
# generate spec file
fmt_obj.printProgress("(3/%s) Generating spec file" % total)
if not pkg_obj.decodeProject(os.getcwd()):
fmt_obj.printError(pkg_obj.getError())
exit(1)
spec = SpecGenerator(import_path, commit, skiperrors = options.skiperrors)
spec.setPackageInfo(pkg_obj)
try:
file = open("%s" % specfile, "w")
spec.setOutputFile(file)
if not spec.generate():
fmt_obj.printErr("Unable to generate spec file: %s" % spec.getError())
exit(1)
except IOError:
fmt_obj.printErr("Error: can\'t open %s file" % specfile)
exit(1)
file.close()
so, se, rc = runCommand("rpmdev-bumpspec %s -c \"First package for Fedora\"" % specfile)
if rc != 0:
fmt_obj.printErr("Unable to bump spec file: %s" % se)
exit(1)
fmt_obj.printProgress("(4/%s) Discovering golang dependencies" % total)
prj_info = pkg_obj.getProjectInfo()
if prj_info == None:
fmt_obj.printErr("Unable to bump spec file: %s" % se)
exit(1)
ip_used = prj_info.getImportedPackages()
ipd = ImportPathsDecomposer(ip_used)
if not ipd.decompose():
fmt_obj.printErr(ipd.getError())
exit(1)
warn = ipd.getWarning()
if warn != "":
fmt_obj.printWarning(warn)
classes = ipd.getClasses()
sorted_classes = sorted(classes.keys())
for element in sorted_classes:
if element == "Native":
continue
if element == "Unknown":
fmt_obj.printWarning("Some import paths were not detected. Please run gofed ggi -c on extracted tarball manually")
continue
if element.startswith(url):
continue
ip_obj = ImportPath(element)
if not ip_obj.parse():
fmt_obj.printWarning("Unable to translate %s to package name" % element)
continue
pkg_name = ip_obj.getPackageName()
pkg_in_pkgdb = False
if pkg_name != "":
pkg_in_pkgdb = packageInPkgdb(pkg_name)
if pkg_in_pkgdb:
print (GREEN + "Class: %s (%s) PkgDB=%s" + ENDC) % (element, pkg_name, pkg_in_pkgdb)
else:
print (RED + "Class: %s (%s) PkgDB=%s" + ENDC ) % (element, pkg_name, pkg_in_pkgdb)
print ""
fmt_obj.printInfo("Spec file %s at %s" % (specfile, os.getcwd()))