def test_get_unused_media_include_models(self):
        self._media_create(u'tests/include_models.txt')
        used_media = get_unused_media(include_models=['tests'])
        expect(used_media).to_be_instance_of(list).to_length(1)
        expect(used_media[0]).to_match(r'^.*tests/include_models.txt')
        # self._media_remove(u'tests/include_models.txt')

        used_media = get_unused_media(include_models=['files'])
        expect(used_media).to_be_instance_of(list).to_length(0)
    def test_quarantine(self):
        expect(get_unused_media()).to_be_empty()
        self._media_create(u'some_folder/notused.txt')
        expect(get_unused_media()).Not.to_be_empty()
        move_media_to_quarantine(get_unused_media())
        expect(get_unused_media()).to_be_empty()

        now = datetime.datetime.now()
        now_str = now.strftime('%Y-%m-%d_%H:%M')
        path = u'quarantine/{0}/some_folder/notused.txt'.format(now_str)
        expect(self._media_exists(path)).to_be_true()
    def test_quarantine_file_already_exists(self):
        self._media_create(u'some_folder/notused.txt')
        move_media_to_quarantine(get_unused_media())
        expect(get_unused_media()).to_be_empty()
        self._media_create(u'some_folder/notused.txt')
        move_media_to_quarantine(get_unused_media())

        now = datetime.datetime.now()
        now_str = now.strftime('%Y-%m-%d_%H:%M')
        path = u'quarantine/{0}/some_folder/notused*.txt'.format(now_str)
        expect(self._find_files(path))\
            .to_be_instance_of(list).to_length(2)
    def handle(self, *args, **options):

        unused_media = get_unused_media(options.get('exclude') or [])

        if not unused_media:
            self.stdout.write('Nothing to delete. Exit')
            return

        if options.get('interactive'):

            self.stdout.write('These files will be deleted:')

            for f in unused_media:
                self.stdout.write(f)

            # ask user

            if six.moves.input(
                    'Are you sure you want to remove %s unused files? (Y/n)' %
                    len(unused_media)) != 'Y':
                self.stdout.write('Interrupted by user. Exit.')
                return

        for f in unused_media:
            self.stdout.write('Remove %s' % f)
            os.remove(os.path.join(settings.MEDIA_ROOT, f))

        remove_empty_dirs()

        self.stdout.write('Done. %s unused files have been removed' %
                          len(unused_media))
    def handle(self, *args, **options):

        if 'verbosity' in options:
            self.verbosity = options['verbosity']

        unused_media = get_unused_media(options.get('exclude') or [])

        if not unused_media:
            self.info('Nothing to delete. Exit')
            return

        if options.get('dry_run'):
            self._show_files_to_delete(unused_media)
            self.info('Dry run. Exit.')
            return

        if options.get('interactive'):
            self._show_files_to_delete(unused_media)

            # ask user

            question = 'Are you sure you want to remove {} unused files? (y/N)'.format(len(unused_media))

            if six.moves.input(question).upper() != 'Y':
                self.info('Interrupted by user. Exit.')
                return

        for f in unused_media:
            self.debug('Remove %s' % f)
            os.remove(os.path.join(settings.MEDIA_ROOT, f))

        if options.get('remove_empty_dirs'):
            remove_empty_dirs()

        self.info('Done. Total files removed: {}'.format(len(unused_media)))
    def handle(self, *args, **options):
        exclusions = options.get('exclude', [])
        folders = options.get('folder', [])
        unused_media = get_unused_media(exclusions, folders)

        if not unused_media:
            self.stdout.write('Nothing to delete. Exit')
            return

        if options.get('interactive'):

            self.stdout.write('These files will be deleted:')

            for f in unused_media:
                self.stdout.write(f)

            # ask user

            if six.moves.input('Are you sure you want to remove %s unused files? (Y/n)' % len(unused_media)) != 'Y':
                self.stdout.write('Interrupted by user. Exit.')
                return

        for f in unused_media:
            self.stdout.write('Remove %s' % f)
            os.remove(os.path.join(settings.MEDIA_ROOT, f))

        remove_empty_dirs()

        self.stdout.write('Done. %s unused files have been removed' % len(unused_media))
Exemplo n.º 7
0
    def handle(self, *args, **options):

        unused_media = get_unused_media(options.get('exclude') or [])

        if not unused_media:
            self.stdout.write('Nothing to delete. Exit')
            return

        if options.get('dry_run'):
            self._show_files_to_delete(unused_media)
            self.stdout.write('Dry run. Exit.')
            return

        elif options.get('interactive'):

            self._show_files_to_delete(unused_media)

            # ask user

            question = 'Are you sure you want to remove {} unused files? (y/N)'.format(
                len(unused_media))

            if six.moves.input(question).upper() != 'Y':
                self.stdout.write('Interrupted by user. Exit.')
                return

        for f in unused_media:
            self.stdout.write('Remove %s' % f)
            os.remove(os.path.join(settings.MEDIA_ROOT, f))

        if options.get('remove_empty_dirs'):
            remove_empty_dirs()

        self.stdout.write('Done. {} unused files have been removed'.format(
            len(unused_media)))
Exemplo n.º 8
0
 def test_ascii_filenames(self):
     self._media_create(u'Тест.txt')
     used_media = get_unused_media()
     expect(used_media).to_be_instance_of(set).to_length(1)
     expect(next(iter(used_media))).to_be_instance_of(six.text_type)
     expect(next(iter(used_media))).to_equal(
         self._media_abs_path(u'Тест.txt'))
Exemplo n.º 9
0
    def handle(self, *args, **options):

        if 'verbosity' in options:
            self.verbosity = options['verbosity']

        if options.get('cleanup_quarantine'):
            self.info('Cleaning up quarantine')
            clean_quarantine()
            return

        if options.get('show_possible_models'):
            self.info("Possible models are:")
            self._print_file_models()
            return

        if options.get('include_models'):
            all_clear = verify_user_file_models(options.get('include_models'))
            if not all_clear:
                self.info(
                    "Stopped processing. Incorrect input of the --include-models argument. "
                    "Fix the errors and run the task again.")
                self.info("Possible options for --include-models are: ")
                self._print_file_models()
                return

        unused_media = get_unused_media(
            options.get('exclude') or [],
            options.get('include_models') or [])

        if not unused_media:
            self.info('Nothing to do. Exit')
            return

        if options.get('dry_run'):
            self._show_files_to_delete(unused_media)
            self.info('Dry run. Exit.')
            return

        if options.get('interactive'):
            self._show_files_to_delete(unused_media)

            # ask user
            question = 'Are you sure you want to place {} unused files in quarantine? (y/N)'.format(
                len(unused_media))

            if six.moves.input(question).upper() != 'Y':
                self.info('Interrupted by user. Exit.')
                return

        self.debug('Moving files to quarantine')
        move_media_to_quarantine(unused_media)
        for f in unused_media:
            self.debug('Placed %s to quarantine' % f)

        if options.get('remove_empty_dirs'):
            remove_empty_dirs()

        self.info('Done. Total files placed in quarantine: {}'.format(
            len(unused_media)))
Exemplo n.º 10
0
    def handle(self, *args, **options):

        if 'verbosity' in options:
            self.verbosity = options['verbosity']

        unused_media = get_unused_media(
            exclude=options.get('exclude'),
            minimum_file_age=options.get('minimum_file_age'),
        )

        if not unused_media:
            self.info('Nothing to delete. Exit')
            return

        if options.get('dry_run'):
            self._show_files_to_delete(unused_media)
            self.info('Dry run. Exit.')
            return

        if options.get('interactive'):
            self._show_files_to_delete(unused_media)

            # ask user

            question = 'Are you sure you want to remove {} unused files? (y/N)'.format(
                len(unused_media))

            if six.moves.input(question).upper() != 'Y':
                self.info('Interrupted by user. Exit.')
                return

        for f in unused_media:
            self.debug('Remove %s' % f)
            os.remove(os.path.join(settings.MEDIA_ROOT, f))

        if options.get('remove_empty_dirs'):
            remove_empty_dirs()

        self.info('Done. Total files removed: {}'.format(len(unused_media)))
Exemplo n.º 11
0
 def test_ascii_filenames(self):
     create_file_and_write(u'Тест.txt')
     used_media = get_unused_media()
     expect(used_media).to_be_instance_of(list).to_length(1)
     expect(used_media[0]).to_be_instance_of(six.text_type)
     expect(used_media[0]).to_equal(u'Тест.txt')
Exemplo n.º 12
0
 def test_remove_unused_media(self):
     expect(get_unused_media()).to_be_empty()
     create_file_and_write(u'notused.txt')
     expect(get_unused_media()).Not.to_be_empty()
     remove_unused_media()
     expect(get_unused_media()).to_be_empty()
Exemplo n.º 13
0
 def test_get_unused_media_subfolder(self):
     create_file_and_write(u'subfolder/notused.txt')
     used_media = get_unused_media()
     expect(used_media).to_be_instance_of(list).to_length(1)
     expect(used_media[0]).to_match(r'^.*subfolder/notused.txt$')
Exemplo n.º 14
0
 def test_get_unused_media_empty(self):
     expect(get_unused_media()).to_be_empty()
Exemplo n.º 15
0
 def test_remove_unused_media(self):
     expect(get_unused_media()).to_be_empty()
     self._media_create(u'notused.txt')
     expect(get_unused_media()).Not.to_be_empty()
     remove_unused_media()
     expect(get_unused_media()).to_be_empty()
Exemplo n.º 16
0
 def test_get_unused_media(self):
     self._media_create(u'notused.txt')
     used_media = get_unused_media()
     expect(used_media).to_be_instance_of(list).to_length(1)
     expect(used_media[0]).to_match(r'^.*notused.txt')
Exemplo n.º 17
0
 def test_ascii_filenames(self):
     self._media_create(u'Тест.txt')
     used_media = get_unused_media()
     expect(used_media).to_be_instance_of(set).to_length(1)
     expect(next(iter(used_media))).to_be_instance_of(six.text_type)
     expect(next(iter(used_media))).to_equal(self._media_abs_path(u'Тест.txt'))
Exemplo n.º 18
0
 def test_remove_unused_media(self):
     expect(get_unused_media()).to_be_empty()
     self._media_create(u'notused.txt')
     expect(get_unused_media()).Not.to_be_empty()
     remove_unused_media()
     expect(get_unused_media()).to_be_empty()
Exemplo n.º 19
0
 def test_get_unused_media_subfolder(self):
     self._media_create(u'subfolder/notused.txt')
     used_media = get_unused_media()
     expect(used_media).to_be_instance_of(set).to_length(1)
     expect(next(iter(used_media))).to_match(r'^.*subfolder/notused.txt$')
Exemplo n.º 20
0
 def test_get_unused_media_empty(self):
     expect(get_unused_media()).to_be_empty()
Exemplo n.º 21
0
 def test_get_unused_media_subfolder(self):
     self._media_create(u'subfolder/notused.txt')
     used_media = get_unused_media()
     expect(used_media).to_be_instance_of(set).to_length(1)
     expect(next(iter(used_media))).to_match(r'^.*subfolder/notused.txt$')
 def test_get_unused_media_subfolder(self):
     create_file_and_write('subfolder/notused.txt')
     used_media = get_unused_media()
     expect(used_media).to_be_instance_of(list).to_length(1)
     expect(used_media[0]).to_match(r'^.*subfolder/notused.txt$')
 def test_remove_unused_media(self):
     expect(get_unused_media()).to_be_empty()
     create_file_and_write('notused.txt')
     expect(get_unused_media()).Not.to_be_empty()
     remove_unused_media()
     expect(get_unused_media()).to_be_empty()