-
Notifications
You must be signed in to change notification settings - Fork 5
/
scandeb
executable file
·137 lines (103 loc) · 4.13 KB
/
scandeb
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@date: 2011-02-28
@author: shell.xu
@license: GPL-3
Copyright (C) 2014 Shell Xu
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
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/>.
@comment:
This program is used to scan deb pool, make dists directory. That make deb pool can be publish as a apt repository.
All deb package must be put into pool/<release>/.
Release is a codename, like stable/testing/unstable.
Category is classify name, like main/contrib/non-free.
该程序用于对deb包池进行扫描,生成dists目录。这样获得的目录可以用于apt更新发布。
所有deb包必须放置在pool/<release>/下。
release是代号,例如stable/testing/unstable。
category是仓库分类,例如main/contrib/non-free。
'''
import os
import sys
import getopt
import shutil
import subprocess
from os import path
def os_safe(f, p):
try: f(p)
except OSError: pass
def findfile(basepath):
for base, dirs, files in os.walk(basepath):
for f in files: yield path.join(base, f)
def scan_for_arches(dirname):
return set(map(lambda n: path.splitext(n)[0].split('_')[-1], findfile(dirname)))
def scan_dir(base, release, category, arch):
os_safe(os.makedirs, path.join(base, 'dists', release, category, 'binary-%s' % arch))
srcpath = path.join(base, 'pool', release)
destpath = path.join(base, 'dists', release, category,
'binary-%s' % arch, 'Packages')
with open(destpath, 'wb') as fo:
p1 = subprocess.Popen(['apt-ftparchive', 'packages', '-a', arch, srcpath],
stdout=fo)
# stdout=subprocess.PIPE)
# p2 = subprocess.Popen(['gzip', '-9c'], stdin=p1.stdout, stdout=fo)
return p1.wait() # or p2.wait()
def archive_release(base, release):
release_dir = path.join(base, 'dists', release)
release_path = path.join(release_dir, 'Release')
os_safe(os.remove, release_path)
os_safe(os.remove, release_path + '.gpg')
with open(release_path, 'wb') as fo:
return subprocess.Popen([
'apt-ftparchive', 'release', release_dir,
'-o', 'APT::FTPArchive::Release::Origin=shell',
'-o', 'APT::FTPArchive::Release::Codename=%s' % release], stdout=fo).wait()
def scan_pool(base, category):
for release in os.listdir(path.join(base, 'pool')):
release_dir = path.join(base, 'pool', release)
for arch in scan_for_arches(release_dir):
scan_dir(base, release, category, arch)
archive_release(base, release)
def sign_dist(base, user=None):
for release in os.listdir(path.join(base, 'pool')):
release_path = path.join(base, 'dists', release, 'Release')
cmds = ['gpg2', '-abs', '-o', release_path + '.gpg', release_path]
if user:
cmds.extend(['-u', user])
subprocess.Popen(cmds).wait()
def scan_path(base):
os_safe(shutil.rmtree, path.join(base, 'dists'))
scan_pool(base, optdict.get('-c', 'main'))
if '-s' in optdict:
sign_dist(base, optdict.get('-u'))
def main():
'''
usage: scandeb [-h] [-u username] [-s]
scan deb pool under current dir, and make dists.
if run with -s option, sign dist with gpg keys.
-c: category, default is main
-h: help
-s: sign dist
-u: sign with username, pass to gpg2
'''
global optdict
optlist, args = getopt.getopt(sys.argv[1:], 'c:hsu:')
optdict = dict(optlist)
if '-h' in optdict:
print main.__doc__
return
if len(args) != 0:
for p in args:
scan_path(p)
else:
scan_path(os.getcwd())
if __name__ == '__main__':
main()