CCCPaste

create-dpkg-list.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import (
    absolute_import, division, print_function, unicode_literals
)
from nine import (
    IS_PYTHON2, basestring, chr, class_types, filter, integer_types,
    implements_iterator, implements_to_string, implements_repr,
    input, iterkeys, iteritems, itervalues, long, map,
    native_str, nine, nimport, range, range_list, reraise, str, zip
)

argparse = nimport('argparse')
glob = nimport('glob')
grp = nimport('grp')
os = nimport('os')
pwd = nimport('pwd')
subprocess = nimport('subprocess')
sys = nimport('sys')
time = nimport('time')

DEFAULT_PREFIX = 'dpkg_'
DEFAULT_SUFFIX = '.txt'


def run():
    """Runs this program.

    The program runs 'dpkg --list' to store the current package list
    into a time-stamped file in the given destination directory.

    Returns:
        None
    """
    start_time = time.time()
    options = parse_cmd_line(
            default_prefix=DEFAULT_PREFIX,
            default_suffix=DEFAULT_SUFFIX)
    remove_old_files(
        pattern='{destdir}{sep}{prefix}*{suffix}'.format(
            destdir=options['destdir'],
            sep=os.sep,
            prefix=options['prefix'],
            suffix=options['suffix']),
        remove=options['remove'],
        verbose=options['verbose'])
    dest_file = generate_results_file_name(
        dest_dir=options['destdir'],
        prefix=options['prefix'],
        suffix=options['suffix'])
    dpkg_list(
        dest_file=dest_file,
        verbose=options['verbose'])
    change_file_owner_group(
        filename=dest_file,
        user=options['user'],
        group=options['user'],
        verbose=options['verbose'])
    if options['verbose']:
        print('Done, in {} seconds!'.format(time.time() - start_time))


def parse_cmd_line(default_prefix, default_suffix):
    """Parses the command-line arguments.

    Arguments:
        default_prefix: the default prefix for the generated dpkg list file
        default_suffix: the default suffix for the generated dpkg list file

    Returns:
        A dictionary with each of the supplied command-line arguments.
    """
    parser = argparse.ArgumentParser(
        description="Stores 'dpkg --list' results")
    parser.add_argument(
        'user',
        help=' '.join([
            'specify the owner of the dpkg list file;',
            'the dpkg list file user and group will be set to this value']))
    parser.add_argument(
        'destdir',
        help=' '.join([
            'specify the directory that the dpkg list file',
            'will be saved to']))
    parser.add_argument(
        '--prefix', '-p',
        default=default_prefix,
        help=' '.join([
            'specify the prefix to use for the dpkg list file',
            "(default: '%(default)s')"]))
    parser.add_argument(
        '--suffix', '-s',
        default=default_suffix,
        help=' '.join([
            'specify the suffix to use for the dpkg list file',
            "(default: '%(default)s')"]))
    parser.add_argument(
        '--remove', '-r',
        action='store_true',
        default=False,
        help='specify this to enable the removal of obsolete dpkg list files')
    parser.add_argument(
        '--verbose', '-v',
        action='store_true',
        default=False,
        help='specify this to display verbose output')
    # vars() turns Namespace into a regular dictionary
    options = vars(parser.parse_args())
    options['destdir'] = chomp_sep(options['destdir'])
    return options


def chomp_sep(dir_name):
    """Removes any trailing directory separator characters from the given
    directory name.

    Arguments:
        dir_name: the name that has to have any trailing slashes removed

    Returns:
        The directory name with no trailing separator characters
    """
    while dir_name.endswith(os.sep):
        dir_name = dir_name[:-1]
    return dir_name


def remove_old_files(**kwargs):
    """Only keeps the newest files that match the given pattern.

    Arguments:
        kwargs: a dictionary with the following keys:-
            'pattern'   - the glob pattern of existing files
            'remove'    - whether to remove old files
            'verbose'   - whether to output text describing non-fatal events

    Returns:
        None
    """
    pattern = kwargs.pop('pattern')
    remove = kwargs.pop('remove')
    verbose= kwargs.pop('verbose')
    if kwargs:
        raise TypeError('Unexpected **kwargs: %r' % kwargs)
    if not remove:
        return
    for f in glob.glob(pattern):
        os.remove(f)
        if verbose:
            print("Removed file '{filename}'".format(filename=f))


def generate_results_file_name(dest_dir, prefix, suffix):
    """Generates a results file filename prefix.

    Arguments:
        dest_dir:   the directory into which the results file should be saved
        prefix:     the prefix to use for the results file
        suffix:     the suffix to use for the results file

    Returns:
        A unique results file filename of the format:
            /second>
        where the year, month, day, etc. values represent the local time
        at the point at which this function is called.
    """
    localtime = time.localtime()
    return ('{dest_dir}{sep}{prefix}{year:04d}{month:02d}{day:02d}' +
        '{hour:02d}{minute:02d}{second:02d}{suffix}').format(
            dest_dir=dest_dir,
            prefix=prefix,
            suffix=suffix,
            year=localtime[0],
            month=localtime[1],
            day=localtime[2],
            hour=localtime[3],
            minute=localtime[4],
            second=localtime[5],
            sep=os.sep)


def dpkg_list(dest_file, verbose):
    """Creates a clone file into .

    Arguments:
        dest_file:  what to name of the dpkg --list results file
        verbose:    whether to output text describing non-fatal events

    Returns:
        None
   """
    subprocess.call(
        'dpkg --list > "{}"'.format(dest_file), shell=True)
    if verbose:
        print('Performed apt-clone clone to: {}'.format(dest_file))


def change_file_owner_group(**kwargs):
    """Changes the owner and group of the named file.

    Arguments:
        kwargs: a dictionary with the following keys:-
            'filename': the file whose ownership is to be changed
            'user':     the new owner to assign to the file
            'group':    the new group to assign to the file;
                        this is the same as 'user' if not given
            'verbose':  whether to output text describing non-fatal events

    Returns:
        None
    """
    filename = kwargs.pop('filename')
    user = kwargs.pop('user')
    group = kwargs.pop('group', user)
    verbose= kwargs.pop('verbose')
    if kwargs:
        raise TypeError('Unexpected **kwargs: %r' % kwargs)
    try:
        os.chown(filename,
                 pwd.getpwnam(user).pw_uid,
                 grp.getgrnam(group).gr_gid)
        if verbose:
            print("Changed ownership of '{filename}' to {user}:{group}".format(
                  filename=filename,
                  user=user,
                  group=group))
    except os.OSError:
        print("Unable to change ownership of "
            + "'{filename}' to {user}:{group}".format(
                filename=filename,
                user=user,
                group=group),
            file=sys.stderr)


if __name__ == '__main__':
    run()