diff --git a/client/solo/commands/cleanup.py b/client/solo/commands/cleanup.py new file mode 100644 index 000000000..121e63205 --- /dev/null +++ b/client/solo/commands/cleanup.py @@ -0,0 +1,99 @@ +# -*- coding: utf-8 -*- +""" + + @author: Fabio Erculiani + @contact: lxnay@sabayon.org + @copyright: Fabio Erculiani + @license: GPL-2 + + B{Entropy Command Line Client}. + +""" +import os +import sys +import argparse + +from entropy.i18n import _ +from entropy.const import etpConst + +from solo.commands.descriptor import SoloCommandDescriptor +from solo.commands.command import SoloCommand +from solo.utils import cleanup + +class SoloCleanup(SoloCommand): + """ + Main Solo Match command. + """ + + NAME = "cleanup" + ALIASES = [] + + INTRODUCTION = """\ +Remove downloaded packages and clean temporary directories. +""" + SEE_ALSO = "equo-cache(1)" + + def __init__(self, args): + SoloCommand.__init__(self, args) + + def man(self): + """ + Overridden from SoloCommand. + """ + return self._man() + + def bashcomp(self, last_arg): + """ + Overridden from SoloCommand. + """ + return self._bashcomp(sys.stdout, last_arg, []) + + def _get_parser(self): + """ + Overridden from SoloCommand. + """ + self._real_command = sys.argv[0] + descriptor = SoloCommandDescriptor.obtain_descriptor( + SoloCleanup.NAME) + parser = argparse.ArgumentParser( + description=descriptor.get_description(), + formatter_class=argparse.RawDescriptionHelpFormatter, + prog="%s %s" % (sys.argv[0], SoloCleanup.NAME)) + + return parser + + def parse(self): + """ + Parse command. + """ + parser = self._get_parser() + try: + nsargs = parser.parse_args(self._args) + except IOError as err: + sys.stderr.write("%s\n" % (err,)) + return parser.print_help, [] + + return self._call_locked, [self._cleanup] + + def _cleanup(self, entropy_client): + """ + Solo Cleanup command. + """ + dirs = [etpConst['logdir'], etpConst['entropyunpackdir']] + for rel in etpConst['packagesrelativepaths']: + # backward compatibility, packages are moved to packages/ dir, + # including nonfree, restricted etc. + dirs.append(os.path.join(etpConst['entropyworkdir'], rel)) + # new location + dirs.append(os.path.join( + etpConst['entropypackagesworkdir'], + rel)) + cleanup(entropy_client, dirs) + return 0 + +SoloCommandDescriptor.register( + SoloCommandDescriptor( + SoloCleanup, + SoloCleanup.NAME, + _("remove downloaded packages and clean temp. directories")) + ) diff --git a/client/solo/utils.py b/client/solo/utils.py index 8a541c560..5c3e8dd96 100644 --- a/client/solo/utils.py +++ b/client/solo/utils.py @@ -11,6 +11,7 @@ """ import os import codecs +import subprocess from entropy.const import etpConst, const_convert_to_unicode from entropy.i18n import _ @@ -43,6 +44,36 @@ def read_client_release(): return "0" +def cleanup(entropy_client, directories): + """ + Temporary files cleaner. + + @param directories: list of directory paths + @type directories: list + @return: exit status + @rtype: int + """ + counter = 0 + for xdir in directories: + if not os.path.isdir(xdir): + continue + entropy_client.output( + "%s %s %s..." % ( + _("Cleaning"), darkgreen(xdir), + _("directory"),), + back = True) + for data in os.listdir(xdir): + subprocess.call(["rm", "-rf", os.path.join(xdir, data)]) + counter += 1 + + entropy_client.output( + "%s: %s %s" % ( + _("Cleaned"), + counter, + _("files and directories"),) + ) + return 0 + def print_table(lines_data, cell_spacing = 2, cell_padding = 0, side_color = darkgreen): """ diff --git a/docs/TODO b/docs/TODO index 3d1f00bb1..546d8510c 100644 --- a/docs/TODO +++ b/docs/TODO @@ -169,7 +169,6 @@ Backlog (raw) --verbose --quiet - cleanup --info solo.utils.show_you_meant !