-
Notifications
You must be signed in to change notification settings - Fork 1
/
opkg-make-index
executable file
·236 lines (215 loc) · 7.81 KB
/
opkg-make-index
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
#!/usr/bin/env python
from __future__ import absolute_import
from __future__ import print_function
import sys, os, posixpath
import subprocess
import opkg
import getopt
import re
verbose = 0
def usage():
sys.stderr.write("%s [-h] [-s] [-m] [-l Packages.filelist] [-p Packages] [-r Packages.old] [-L localesdir] [-v] packagesdir\n" % (sys.argv[0],))
sys.exit(-1)
def to_morgue(filename):
morgue_dir = pkg_dir + "/morgue"
if verbose:
sys.stderr.write ("Moving " + filename + " to morgue\n")
if not os.path.exists(morgue_dir):
os.mkdir(morgue_dir)
if os.path.exists(pkg_dir + "/" + filename):
os.rename(pkg_dir + "/" + filename, morgue_dir + "/" + filename)
if os.path.exists(pkg_dir + "/" + filename + ".asc"):
os.rename(pkg_dir + "/" + filename + ".asc", morgue_dir + "/" + filename + ".asc")
locales_dir = None
def to_locale(filename, locale):
locale_dir = pkg_dir + '/' + locales_dir + '/' + locale + "/"
if verbose:
sys.stderr.write ("Moving " + filename + " to " + locale_dir + "\n")
if not os.path.exists(locale_dir):
os.mkdir(locale_dir)
os.rename(pkg_dir + "/" + filename, locale_dir + "/" + filename)
if os.path.exists(pkg_dir + "/" + filename + ".asc"):
os.rename(pkg_dir + "/" + filename + ".asc", locale_dir + "/" + filename + ".asc")
old_filename = None
packages_filename = None
filelist_filename = None
stamplist_filename = "Packages.stamps"
opt_s = 0
opt_m = 0
(opts, remaining_args) = getopt.getopt(sys.argv[1:], "hl:p:vsmr:L:")
for (optkey, optval) in opts:
if optkey == '-h':
usage()
if optkey == '-s':
opt_s = 1
if optkey == '-p':
packages_filename = optval
stamplist_filename = optval + ".stamps"
if optkey == '-l':
filelist_filename = optval
if optkey == '-v':
verbose = 1
if optkey == '-m':
opt_m = 1
if optkey == '-r':
old_filename = optval
if optkey == '-L':
locales_dir = optval
if ( not remaining_args ):
usage()
pkg_dir=remaining_args[0]
packages = opkg.Packages()
old_pkg_hash = {}
if packages_filename and not old_filename and os.path.exists(packages_filename):
old_filename = packages_filename
pkgsStamps = {}
if old_filename:
if (verbose):
sys.stderr.write("Reading package list from " + old_filename + "\n")
old_packages = opkg.Packages()
old_packages.read_packages_file(old_filename)
for k in list(old_packages.packages.keys()):
p = old_packages.packages[k]
old_pkg_hash[p.filename] = p
try:
f = open(stamplist_filename, "r")
for l in f:
l = l.strip()
s, f = l.split(" ", 1)
pkgsStamps[f] = int(s)
except IOError:
pass
if (verbose):
sys.stderr.write("Reading in all the package info from %s\n" % (pkg_dir, ))
files=[]
opkg_extensions=['.ipk','.opk','.deb']
for dirpath, dirnames, filenames in os.walk(pkg_dir):
for f in filenames:
ext = os.path.splitext(f)[1]
if ext in opkg_extensions:
files.append(os.path.join(dirpath, f))
files.sort()
for abspath in files:
try:
filename = os.path.relpath(abspath, pkg_dir)
pkg = None
fnameStat = os.stat(abspath)
if filename in old_pkg_hash:
if filename in pkgsStamps and int(fnameStat.st_mtime) == pkgsStamps[filename]:
if (verbose):
sys.stderr.write("Found %s in Packages\n" % (filename,))
pkg = old_pkg_hash[filename]
else:
sys.stderr.write("Found %s in Packages, but mtime differs - re-reading\n" % (filename,))
if not pkg:
if (verbose):
sys.stderr.write("Reading info for package %s\n" % (filename,))
pkg = opkg.Package(abspath, relpath=pkg_dir)
pkg_key = ("%s:%s" % (pkg.package, pkg.architecture))
if (pkg_key in packages.packages):
old_filename = packages.packages[pkg_key].filename
else:
old_filename = ""
s = packages.add_package(pkg)
pkgsStamps[filename] = fnameStat.st_mtime
if s == 0:
if old_filename:
# old package was displaced by newer
if opt_m:
to_morgue(old_filename)
if opt_s:
print(("%s/%s" % (pkg_dir, old_filename)))
else:
if opt_m:
to_morgue(filename)
if opt_s:
print(filename)
except OSError as e:
sys.stderr.write("Package %s disappeared on us!\n(%s)\n" % (filename, e))
continue
except IOError as e:
sys.stderr.write("Package %s disappeared on us!\n(%s)\n" % (filename, e))
continue
pkgsStampsFile = open(stamplist_filename, "w")
for f in list(pkgsStamps.keys()):
pkgsStampsFile.write("%d %s\n" % (pkgsStamps[f], f))
pkgsStampsFile.close()
if opt_s:
sys.exit(0)
if verbose:
sys.stderr.write("Generating Packages file\n")
if packages_filename:
tmp_packages_filename = ("%s.%d" % (packages_filename, os.getpid()))
pkgsFile = open(tmp_packages_filename, "w")
names = list(packages.packages.keys())
names.sort()
for name in names:
try:
pkg = packages.packages[name]
if locales_dir and pkg.depends:
depends = pkg.depends.split(',')
locale = None
for d in depends:
m = re.match('.*virtual-locale-([a-zA-Z]+).*', d)
mp = re.match('locale-base-([a-zA-Z]+)([-+])?.*', pkg.package)
if m:
locale = m.group(1)
if mp:
locale = mp.group(1)
if locale:
to_locale(pkg.filename, locale)
continue
if (verbose):
sys.stderr.write("Writing info for package %s\n" % (pkg.package,))
if packages_filename:
pkgsFile.write(str(pkg))
else:
print(pkg)
except OSError as e:
sys.stderr.write("Package %s disappeared on us!\n(%s)\n" % (name, e))
continue
except IOError as e:
sys.stderr.write("Package %s disappeared on us!\n(%s)\n" % (name, e))
continue
if packages_filename:
pkgsFile.close()
gzip_filename = ("%s.gz" % packages_filename)
tmp_gzip_filename = ("%s.%d" % (gzip_filename, os.getpid()))
gzip_cmd = "gzip -9c < %s > %s" % (tmp_packages_filename, tmp_gzip_filename)
subprocess.call(gzip_cmd, shell=True)
os.rename(tmp_packages_filename, packages_filename)
os.rename(tmp_gzip_filename, gzip_filename)
if filelist_filename:
if verbose:
sys.stderr.write("Generate Packages.filelist file\n")
files = {}
names = list(packages.packages.keys())
names.sort()
for name in names:
try:
if verbose:
sys.stderr.write("Reading filelist for package '%s'\n" % name)
# sys.stderr.write("Package for name '%s':\n'%s'\n" % (name, packages[name]))
fnlist = packages[name].get_file_list_dir(pkg_dir)
# sys.stderr.write("Filelist for package '%s': '%s'\n" % (name, fnlist))
except OSError as e:
sys.stderr.write("Package %s disappeared on us!\n(%s)\n" % (name, e))
continue
except IOError as e:
sys.stderr.write("Package %s disappeared on us!\n(%s)\n" % (name, e))
continue
for fn in fnlist:
(h,t) = os.path.split(fn)
if not t: continue
if t not in files: files[t] = name+':'+fn
else: files[t] = files[t] + ',' + name+':'+fn
tmp_filelist_filename = ("%s.%d" % (filelist_filename, os.getpid()))
f = open(tmp_filelist_filename, "w")
names = list(files.keys())
names.sort()
for name in names:
f.write("%s %s\n" % (name, files[name]))
f.close()
if posixpath.exists(filelist_filename):
os.unlink(filelist_filename)
os.rename(tmp_filelist_filename, filelist_filename)