OCA/server-tools

View on GitHub
auto_backup/models/db_backup.py

Summary

Maintainability
C
1 day
Test Coverage

File db_backup.py has 260 lines of code (exceeds 250 allowed). Consider refactoring.
Open

# -*- coding: utf-8 -*-
# © 2004-2009 Tiny SPRL (<http://tiny.be>).
# © 2015 Agile Business Group <http://www.agilebg.com>
# © 2016 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis
# License AGPL-3.0 or later (http://www.gnu.org/licenses/gpl.html).
Severity: Minor
Found in auto_backup/models/db_backup.py - About 2 hrs to fix

    Function action_backup has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
    Open

        def action_backup(self):
            """Run selected backups."""
            backup = None
            filename = self.filename(datetime.now())
            successful = self.browse()
    Severity: Minor
    Found in auto_backup/models/db_backup.py - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function cleanup has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
    Open

        def cleanup(self):
            """Clean up old backups."""
            for rec in self.filtered("days_to_keep"):
                with rec.cleanup_log():
                    if rec.method == "local":
    Severity: Minor
    Found in auto_backup/models/db_backup.py - About 1 hr to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Avoid deeply nested control flow statements.
    Open

                            with rec.custom_tempdir():
                                db.dump_db(self.env.cr.dbname, destiny)
                            backup = backup or destiny.name
    Severity: Major
    Found in auto_backup/models/db_backup.py - About 45 mins to fix

      Avoid deeply nested control flow statements.
      Open

                              for name in sorted(
                                      n for n in remote.listdir(rec.folder)
                                      if n.endswith(".dump.zip")
                              )[:-rec.days_to_keep]:
                                  remote.unlink('%s/%s' % (rec.folder, name))
      Severity: Major
      Found in auto_backup/models/db_backup.py - About 45 mins to fix

        Avoid deeply nested control flow statements.
        Open

                                with rec.sftp_connection() as remote:
                                    # Directory must exist
                                    try:
                                        remote.makedirs(rec.folder)
                                    except pysftp.ConnectionException:
        Severity: Major
        Found in auto_backup/models/db_backup.py - About 45 mins to fix

          Avoid deeply nested control flow statements.
          Open

                                  with open(backup) as cached:
                                      shutil.copyfileobj(cached, destiny)
                              # Generate new backup
                              else:
          Severity: Major
          Found in auto_backup/models/db_backup.py - About 45 mins to fix

            Similar blocks of code found in 2 locations. Consider refactoring.
            Open

                        self.message_post(
                            "<p>%s</p><pre>%s</pre>" % (
                                _("Database backup failed."),
                                escaped_tb),
                            subtype=self.env.ref(
            Severity: Minor
            Found in auto_backup/models/db_backup.py and 1 other location - About 40 mins to fix
            auto_backup/models/db_backup.py on lines 266..270

            Duplicated Code

            Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

            Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

            When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

            Tuning

            This issue has a mass of 34.

            We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

            The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

            If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

            See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

            Refactorings

            Further Reading

            Similar blocks of code found in 2 locations. Consider refactoring.
            Open

                        self.message_post(
                            "<p>%s</p><pre>%s</pre>" % (
                                _("Cleanup of old database backups failed."),
                                escaped_tb),
                            subtype=self.env.ref("auto_backup.failure"))
            Severity: Minor
            Found in auto_backup/models/db_backup.py and 1 other location - About 40 mins to fix
            auto_backup/models/db_backup.py on lines 223..227

            Duplicated Code

            Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

            Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

            When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

            Tuning

            This issue has a mass of 34.

            We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

            The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

            If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

            See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

            Refactorings

            Further Reading

            There are no issues that match your filters.

            Category
            Status