Project

General

Profile

« Previous | Next » 

Revision 982d29ac

Added by david.sorber over 11 years ago

Cleaned up the first version of the script and simplified it a bit as well.

View differences:

software/apt-tools/get_deps.py
#!/usr/bin/python3
import argparse
import copy
from distutils.version import LooseVersion
import sys
import apt
# List of "additional" packages to install
ADDTIONAL_PACKAGES = [
'htop',
'iotop',
'vim',
'openssh-server',
'build-essential']
# Virtual package mapping
virtual_mapping = {'awk': 'gawk'}
def main():
# Create cache
cache = apt.Cache()
cache.open()
in_packages = copy.copy(ADDTIONAL_PACKAGES)
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('input_package_files', action='store')
args = parser.parse_args()
# Input packages
in_packages = []
# Parse input package files
for filename in args.input_package_files.split(','):
with open(filename) as in_file:
for line in in_file:
line = line.strip()
if line:
in_packages.append(line)
# Open APT cache
cache = apt.Cache(memonly=True)
cache.open()
packages = set()
package_deps = {}
while True:
try:
# Grab next package
pkg_name = in_packages.pop()
print('Package: {:s}'.format(pkg_name))
#~ print('Package: {:s}'.format(pkg_name))
if pkg_name not in cache:
if pkg_name in virtual_mapping:
pkg_name = virtual_mapping[pkg_name]
else:
continue
# Add this package (with version) to the list of packages
if pkg_name not in packages:
packages.add(pkg_name)
try:
package_deps[pkg_name] = cache[pkg_name].versions[0].dependencies
except KeyError:
continue
for pdep in package_deps[pkg_name]:
# Get package dependencies
for pdep in cache[pkg_name].versions[0].dependencies:
dep_name = pdep[0].name
print('\tDependency: {:s}'.format(dep_name))
#~ print('\tDependency: {:s}'.format(dep_name))
if dep_name not in packages:
if dep_name in virtual_mapping:
dep_name = virtual_mapping[dep_name]
packages.add(dep_name)
in_packages.insert(0, dep_name)
except IndexError:
break
print('\n\n')
for package in packages:
# Print out the gathered package names
for package in sorted(packages):
print(package)
print('\n\n')
if __name__ == '__main__':
sys.exit(main())

Also available in: Unified diff