-
Notifications
You must be signed in to change notification settings - Fork 2
/
spikeless.py
executable file
·347 lines (309 loc) · 13.3 KB
/
spikeless.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
spike – a package manager running on top of git
Copyright © 2012, 2013, 2014 Mattias Andrée ([email protected])
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import os
import sys
from dragonsuite import *
from auxiliary.scrollmagick import *
SOFTWARE_SHAREABLE = 1
SOFTWARE_COMMERCIAL = 2
SOFTWARE_DERIVATIVE = 4
MEDIA_SHAREABLE = 8
MEDIA_COMMERCIAL = 16
MEDIA_DERIVATIVE = 32
TRADEMARKED = 64
PATENTED = 128
MEDIA = MEDIA_SHAREABLE | MEDIA_COMMERCIAL | MEDIA_DERIVATIVE
SOFTWARE = SOFTWARE_SHAREABLE | SOFTWARE_COMMERCIAL | SOFTWARE_DERIVATIVE
UNSUPPORTED = 0
SUPPORTED = 1
MANDITORY = 2
NO_TRADEMARKS = 1
NO_PATENTS = 2
CONTRACT_BASED = 4
COMMERCIAL = 8
DERIVATIVE = 16
FSF_APPROVED = 32
OSI_APPROVED = 64
GPL_COMPATIBLE = 128
COPYLEFT = 256
class Spikeless():
'''
Spike but without the package managing, it just installs scrolls
'''
@staticmethod
def install(scroll, startdir, pinpal = '', private = False, fresh_installation = True, buildpatch = None, checkpatch = None, packagepatch = None, inspection = None):
'''
Installs a scroll, but does not do any package managing
@param scroll:str Scroll to install, by filename
@param startdir:str Scroll base working directory
@param pinpal:str Installation root
@param private:bool Whether to install privately
@param fresh_installation:bool Whether it is a fresh installation
@param buildpatch:(srcdir:str, pkgdir:str)?→void Scroll build patch function
@param checkpatch:(srcdir:str, pkgdir:str)?→void Scroll check patch function
@param packagepatch:(srcdir:str, pkgdir:str)?→void Scroll package patch function
@param inspection:(pkgdir:str)?→bool Function that checks that the package can be installed
@return (pre, pkgdir, post):((list<str>)→void, str, (list<str>)→void) Preinstall functor, director with files to install, postinstall functor.
The functors takes the files installed by the scrolls, before and after the installation, respectively.
Between calling the functor you just install file files in the returned directory
'''
global _dragonsuite_output
_dragonsuite_output = sys.stdout.buffer
ScrollMagick.init_methods(globals())
ScrollMagick.init_fields(globals())
scrolldir = os.path.abspath(dirname(scroll))
cwd = os.getcwd()
ScrollMagick.export_environment()
environ = {}
for var in os.environ:
environ[var] = os.environ[var]
def reset_environ(reset_to, keep_flags = True):
delete = []
s = set(reset_to.keys())
for var in os.environ:
if (not keep_flags) or (var.lower().startswith('i_use_')):
if var not in s:
delete.append(var)
else:
os.putenv(var, reset_to[var])
os.environ[var] = reset_to[var]
for var in delete:
os.unsetenv(var)
del os.environ[var]
cd(startdir)
startdir = os.getcwd()
cd(cwd)
srcdir = startdir + os.sep + 'src'
pkgdir = startdir + os.sep + 'pkg'
if not os.path.exists(srcdir):
os.makedirs(srcdir)
if not os.path.exists(pkgdir):
os.mkdir(pkgdir)
ScrollMagick.execute_scroll(scroll, globals())
def sources(scrolldir):
global noextract, source, sha3sums
noextract = set([] if noextract is None else noextract)
extract = []
pushd(scrolldir)
for i in range(len(source)):
src = source[i]
extras = None
if not isinstance(src, str):
extras = list(src[1:])
src = src[0]
_src = src
if src.startswith('file:'):
src = src[5:]
if src.startswith('//'):
src = src[2:]
elif ':' in src:
if extras is not None:
src = [src] + extras
source[i] = (src, _src not in noextract)
continue
src = os.path.abspath(src)
src = 'file://' + src
if extras is not None:
src = [src] + extras
source[i] = (src, _src not in noextract)
popd()
def inetget(params, dest, sha3sum):
if os.path.exists(dest):
if sha3sum is not None:
if sha3sum(dest) != sha3sum.upper():
wget(params)
else:
wget(params)
i = 0
for (src, extractsrc) in source:
dest = None
d = None
if isinstance(src, str):
dest = src[src.rfind('/'):]
if dest == '':
dest = 'index.html'
d = dest
dest = startdir + os.sep + dest
if ':' not in src:
cp(src.replace('/', os.sep), dest)
elif src.startswith('file:'):
src = src[5:]
if src.startswith('//'):
src = src[2:]
cp(src.replace('/', os.sep), dest)
else:
inetget(src, dest, sha3sums[i])
else:
extras = src[2:]
(src, dest) = src[:2]
if dest is None:
dest = src[src.rfind('/'):]
if dest == '':
dest = 'index.html'
d = dest
dest = startdir + os.sep + dest
if ':' not in src:
cp(src.replace('/', os.sep), dest)
elif src.startswith('file:'):
src = src[5:]
if src.startswith('//'):
src = src[2:]
cp(src.replace('/', os.sep), dest)
else:
inetget([src, '-O', dest] + extras, dest, sha3sums[i])
if sha3sums[i] is not None:
sha3 = sha3sum(sumdests)
if sha3 != sha3sums[i].upper():
raise Exception('sha3sum is not matching for %s' % d)
if extractsrc:
extract.append(os.path.abspath(dest))
i += 1
cd('src')
decompress(extract)
cd('..')
cd(startdir)
msg('Fetching sources')
sources(scrolldir)
if build is not None:
if buildpatch is not None:
msg('Patching build process')
reset_environ(environ)
os.chdir(startdir)
os.umask(0o022)
buildpatch(srcdir, pkgdir)
msg('Building')
reset_environ(environ)
os.chdir(startdir)
os.umask(0o022)
build(startdir, srcdir, pkgdir, private)
if check is not None:
if checkpatch is not None:
msg('Patching check process')
reset_environ(environ)
os.chdir(startdir)
os.umask(0o022)
checkpatch(srcdir, pkgdir)
msg('Checking')
reset_environ(environ)
os.chdir(startdir)
os.umask(0o022)
check(startdir, srcdir, pkgdir, private)
if package is not None:
if packagepatch is not None:
msg('Patching package process')
reset_environ(environ)
os.chdir(startdir)
os.umask(0o022)
packagepatch(srcdir, pkgdir)
msg('Packaging')
reset_environ(environ)
os.chdir(startdir)
os.umask(0o022)
package(startdir, srcdir, pkgdir, private)
os.chdir(cwd)
reset_environ(environ)
global useopts ## TODO default options should be load
if useopts is None:
useopts = set(['strip', 'licenses' 'changelogs', 'libtool', 'upx'])
if options is not None:
for opt in options:
if opt.startswith('!'):
if opt[1:] in useopts:
del useopts[opt[1:]]
else:
if opt not in useopts:
useopts.add(opt)
## TODO apply options
if inspection is not None:
inspection(pkgdir)
if not os.path.exists(pinpal):
os.makedirs(pinpal + os.sep + 'src')
class preFunctor():
def __init__(self, fresh, start, root, priv, env):
self.fresh = fresh
self.start = start
self.root = root
self.env = env
def __call__(self, installedfiles = []):
global pre_install, pre_upgrade
cwd = os.getcwd()
os.chdir(self.root)
os.umask(0o022)
if self.fresh:
if pre_install is not None:
msg('Preinstall processing')
tmpdir = self.start + os.sep + 'pretmp'
if not os.path.exists(tmpdir):
os.mkdir(tmpdir)
pre_install(tmpdir, self.root, self.priv)
else:
if pre_upgrade is not None:
msg('Preupgrade processing')
tmpdir = self.start + os.sep + 'pretmp'
if not os.path.exists(tmpdir):
os.mkdir(tmpdir)
pre_install(tmpdir, self.root, installedfiles, self.priv)
reset_environ(self.env)
os.chdir(cwd)
class postFunctor():
def __init__(self, fresh, start, root, priv, env):
self.fresh = fresh
self.start = start
self.root = root
self.env = env
def __call__(self, installedfiles = []):
global post_install, post_upgrade
cwd = os.getcwd()
os.chdir(self.root)
os.umask(0o022)
if self.fresh:
if post_install is not None:
msg('Postinstall processing')
tmpdir = self.start + os.sep + 'posttmp'
if not os.path.exists(tmpdir):
os.mkdir(tmpdir)
post_install(tmpdir, self.root, installedfiles, self.priv)
else:
if post_upgrade is not None:
msg('Postupgrade processing')
tmpdir = self.start + os.sep + 'posttmp'
if not os.path.exists(tmpdir):
os.mkdir(tmpdir)
post_install(tmpdir, self.root, installedfiles, self.priv)
reset_environ(self.env)
os.chdir(cwd)
pre = preFunctor(fresh_installation, startdir, pinpal, private, environ)
post = postFunctor(fresh_installation, startdir, pinpal, private, environ)
reset_environ(environ, False)
return (pre, pkgdir, post)
if __name__ == '__main__': # sic
if len(sys.argv) < 3:
print('USAGE: spikeless SCROLL STARTDIR PINPAL [private]')
sys.exit(1)
global useopts
useopts = None
def installdir(src, dest):
if not os.path.exists(dest):
os.makedirs(dest)
files = [src + os.sep + f for f in os.listdir(src)]
cp_r(files, dest)
scroll = os.path.abspath(sys.argv[1])
startdir = os.path.abspath(sys.argv[2])
pinpal = os.path.abspath(sys.argv[3])
(_a, pkgdir, _b) = Spikeless.install(scroll, startdir, pinpal, 'private' in sys.argv[4:])
msg('Installing packaged files')
installdir(pkgdir, pinpal)