view scripts/spam-remover @ 7752:b2dbab2b34bc

fix(refactor): multiple fixups using ruff linter; more testing. Converting to using the ruff linter and its rulesets. Fixed a number of issues. admin.py: sort imports use immutable tuples as default value markers for parameters where a None value is valid. reduced some loops to list comprehensions for performance used ternary to simplify some if statements named some variables to make them less magic (e.g. _default_savepoint_setting = 1000) fixed some tests for argument counts < 2 becomes != 2 so 3 is an error. moved exception handlers outside of loops for performance where exception handler will abort loop anyway. renamed variables called 'id' or 'dir' as they shadow builtin commands. fix translations of form _("string %s" % value) -> _("string %s") % value so translation will be looked up with the key before substitution. end dicts, tuples with a trailing comma to reduce missing comma errors if modified simplified sorted(list(self.setting.keys())) to sorted(self.setting.keys()) as sorted consumes whole list. in if conditions put compared variable on left and threshold condition on right. (no yoda conditions) multiple noqa: suppression removed unneeded noqa as lint rulesets are a bit different do_get - refactor output printing logic: Use fast return if not special formatting is requested; use isinstance with a tuple rather than two isinstance calls; cleaned up flow and removed comments on algorithm as it can be easily read from the code. do_filter, do_find - refactor output printing logic. Reduce duplicate code. do_find - renamed variable 'value' that was set inside a loop. The loop index variable was also named 'value'. do_pragma - added hint to use list subcommand if setting was not found. Replaced condition 'type(x) is bool' with 'isinstance(x, bool)' for various types. test_admin.py added testing for do_list better test coverage for do_get includes: -S and -d for multilinks, error case for -d with non-link. better testing for do_find including all output modes better testing for do_filter including all output modes fixed expected output for do_pragma that now includes hint to use pragma list if setting not found.
author John Rouillard <rouilj@ieee.org>
date Fri, 01 Mar 2024 14:53:18 -0500
parents c75defc1c2f0
children 9c3ec0a5c7fc
line wrap: on
line source

#! /usr/bin/env python
# Copyright (C) 2012 Dr. Ralf Schlatterbeck Open Source Consulting.
# Reichergasse 131, A-3411 Weidling.
# Web: http://www.runtux.com Email: rsc@runtux.com
# All rights reserved
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included in
#   all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from __future__ import print_function
_doc = '''
%prog [options]
Remove file attachment spam from a tracker:
- Edit the journal of the given issue(s) and remove the links to the
  spam-files
- Set the contents of the spam-files involved to zero length
WARNING:
This is a dangerous operation as it will edit the history *and* remove
data that is not in the journal (the contents of files). Be careful with
the file pattern (start of filename) you specify!
'''

import sys
from   optparse import OptionParser
from   roundup  import instance, hyperdb

def main():
    cmd = OptionParser(usage=_doc)
    cmd.add_option \
        ( "-i", "--instance"
        , help    = "Instance home"
        , default = "."
        )
    cmd.add_option \
        ( "-d", "--designator"
        , dest    = "designators"
        , help    = "Item designator for issue(s), to remove files from,\n"
                    "e.g. issue4711"
        , action  = "append"
        , default = []
        )
    cmd.add_option \
        ( "-f", "--filename"
        , dest    = "filenames"
        , help    = "Exact spam-filename to remove from issue(s)"
        , action  = "append"
        , default = []
        )
    cmd.add_option \
        ( "-a", "--action", "--no-dry-run"
        , dest    = "doit"
        , help    = "Don't perform any action by default unless specified"
        , action  = "store_true"
        )
    cmd.add_option \
        ( "-s", "--file-start-pattern"
        , dest    = "file_pattern"
        , help    = "Start of spam-filename to remove from issue(s)"
        , action  = "append"
        , default = []
        )
    cmd.add_option \
        ( "-u", "--spam-user"
        , dest    = "users"
        , help    = "Username that created the spam-files to remove"
        , action  = "append"
        , default = []
        )
    cmd.add_option \
        ( "-q", "--quiet"
        , dest    = "quiet"
        , help    = "Be quiet about what we're doing"
        , action  = "store_true"
        )
    opt, args = cmd.parse_args()
    # open the instance
    if len(args):
        print("This command doesn't take arguments", file=sys.stderr)
        cmd.show_help()
    tracker = instance.open(opt.instance)
    db = tracker.open('admin')
    db.tx_Source = "cli"

    users = dict.fromkeys (db.user.lookup(u) for u in opt.users)
    files_to_remove = {}
    for fn in opt.filenames:
        for fid in db.file.filter(None,dict(name=fn)):
            if db.file.get(fid,'name') == fn:
                files_to_remove[fid] = True
    for fn in opt.file_pattern:
        for fid in db.file.filter(None,dict(name=fn)):
            if db.file.get(fid,'name').startswith(fn):
                files_to_remove[fid] = True
    files_found = {}
    for d in opt.designators:
        clsname, id = hyperdb.splitDesignator(d)
        cls = db.getclass(clsname)
        issuefiles = dict.fromkeys(cls.get (id, 'files'))
        for fid in list(issuefiles.keys()):
            f = db.file.getnode(fid)
            if fid in files_to_remove or f.creator in users:
                files_to_remove[fid] = True
                files_found[fid] = True
                if not opt.quiet:
                    print("deleting file %s from issue" % f)
                del issuefiles[fid]
        if opt.doit:
            cls.set(id, files=list(issuefiles.keys()))
        journal = oldjournal = db.getjournal(clsname, id)
        # do this twice, we may have file-removals *before* file
        # additions for files to delete and may discover mid-journal
        # that there are new files to remove
        for x in range(2):
            newjournal = []
            for j in journal:
                if j[3] == 'set' and 'files' in j[4]:
                    if j[4]['files'][0][0] not in ('-', '+') :
                        newjournal.append(j)
                        continue
                    changes = dict(j[4]['files'])
                    # only consider file additions by this user
                    if j[2] in users and '+' in changes:
                        f = dict.fromkeys(changes['+'])
                        files_found.update(f)
                        files_to_remove.update(f)
                        del changes['+']
                    # change dict in-place
                    for k, v in list(changes.items()):
                        new_f = []
                        for f in v:
                            if f in files_to_remove:
                                files_found[f] = True
                            else:
                                new_f.append(f)
                        if new_f :
                            changes[k] = new_f
                        else:
                            del changes[k]
                    msg = []
                    if not opt.quiet:
                        msg.append ("Old journal entry: %s" % str(j))
                    if changes:
                        j[4]['files'] = tuple(changes.items())
                    else:
                        del j[4]['files']
                    if j[4]:
                        newjournal.append(j)
                        if not opt.quiet:
                            msg.append ("New journal entry: %s" % str(j))
                    elif not opt.quiet:
                        msg.append ("deleted")
                    if len(msg) == 2 and msg[0][4:] != msg[1][4:]:
                        for m in msg:
                            print(m)
                else:
                    newjournal.append(j)
            journal = newjournal
        if newjournal != oldjournal and opt.doit:
            db.setjournal(clsname, id, newjournal)
    if opt.doit:
        for f in files_found:
            db.file.set(f, content=' ')
        db.commit()
    else:
        print("Database not changed")


if __name__ == '__main__':
    main()

Roundup Issue Tracker: http://roundup-tracker.org/