-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapt-build-package-list.py
executable file
·87 lines (74 loc) · 3.47 KB
/
apt-build-package-list.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
#!/usr/bin/python
#
# Copyright (C) 2017 Olivier Martin <[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, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Example of usage:
# ../tools/apt-build-package-list.py debian jessie amd64 $PWD
# ../tools/apt-build-package-list.py ubuntu trusty amd64 $PWD
import argparse
import gzip
import os
import sqlite3
import urllib
parser = argparse.ArgumentParser(description='Build package list for Linux Distribution.')
parser.add_argument('distribution', choices=['debian', 'ubuntu'], help='Linux distribution')
parser.add_argument('distribution_version', type=str, help='Linux distribution')
parser.add_argument('architecture', choices=['amd64', 'arm64'], help='CPU Architecture')
parser.add_argument('build_root', type=str, help='Location to store the Linux Distribution package.')
args = parser.parse_args()
# Build URL command line
if args.distribution == "debian":
distribution_packages_url = "http://ftp.uk.debian.org/debian/dists/%s/main/binary-%s/Packages.gz" % (args.distribution_version, args.architecture)
elif args.distribution == "ubuntu":
distribution_packages_url = "http://gb.archive.ubuntu.com/ubuntu/dists/%s/main/binary-%s/Packages.gz" % (args.distribution_version, args.architecture)
#
# Create Database
#
sqlite_database_filepath = args.build_root + "/%s-%s-%s-Packages.db" % (args.distribution, args.distribution_version, args.architecture)
if os.path.isfile(sqlite_database_filepath):
os.remove(sqlite_database_filepath)
sql_conn = sqlite3.connect(sqlite_database_filepath)
sql_cur = sql_conn.cursor()
sql_cur.execute("CREATE TABLE Packages(ID integer primary key autoincrement, Name, Version, Filename, Dependencies)")
# Generate the file for the Linux Distribution Packages
distribution_packages_local_file = args.build_root + "/%s-%s-%s-Packages.gz" % (args.distribution, args.distribution_version, args.architecture)
print "Download file %s" % distribution_packages_url
urllib.urlretrieve(distribution_packages_url, distribution_packages_local_file)
print "Decompress file %s" % distribution_packages_local_file
with gzip.open(distribution_packages_local_file, 'rb') as fp:
line = fp.readline().strip()
while line:
if line.startswith('Package:'):
package_name = line[9:]
line = fp.readline().strip()
while line and (len(line) > 0):
if line.startswith('Version:'):
package_version = line[9:]
if line.startswith('Depends:'):
package_dependency = line[9:]
else:
package_dependency = None
if line.startswith('Filename:'):
package_filename = line[10:]
line = fp.readline().strip()
sql_cur.execute("INSERT INTO Packages (Name, Version, Filename, Dependencies) VALUES (?,?,?,?)", (package_name, package_version, package_filename, package_dependency))
# End of file
if line is None:
break
line = fp.readline().strip()
sql_conn.commit()
sql_cur.close()