<commit_before>import subprocess

from moulinette import m18n
from yunohost.utils.error import YunohostError
from moulinette.utils.log import getActionLogger

from yunohost.tools import Migration
from yunohost.utils.filesystem import free_space_in_directory, space_used_by_directory

logger = getActionLogger('yunohost.migration')


class MyMigration(Migration):

    "Migrate DBs from Postgresql 9.4 to 9.6 after migrating to Stretch"

    def migrate(self):

        if not self.package_is_installed("postgresql-9.4"):
            logger.warning(m18n.n("migration_0005_postgresql_94_not_installed"))
            return

        if not self.package_is_installed("postgresql-9.6"):
            raise YunohostError("migration_0005_postgresql_96_not_installed")

        if not space_used_by_directory("/var/lib/postgresql/9.4") > free_space_in_directory("/var/lib/postgresql"):
            raise YunohostError("migration_0005_not_enough_space", path="/var/lib/postgresql/")

        subprocess.check_call("service postgresql stop", shell=True)
        subprocess.check_call("pg_dropcluster --stop 9.6 main", shell=True)
        subprocess.check_call("pg_upgradecluster -m upgrade 9.4 main", shell=True)
        subprocess.check_call("pg_dropcluster --stop 9.4 main", shell=True)
        subprocess.check_call("service postgresql start", shell=True)

    def backward(self):

        pass

    def package_is_installed(self, package_name):

        p = subprocess.Popen("dpkg --list | grep -q -w {}".format(package_name), shell=True)
        p.communicate()
        return p.returncode == 0<commit_msg>Fix an issue where postgresql-9.4 was being detected as installed whereas it was in fact not<commit_after>import subprocess

from moulinette import m18n
from yunohost.utils.error import YunohostError
from moulinette.utils.log import getActionLogger

from yunohost.tools import Migration
from yunohost.utils.filesystem import free_space_in_directory, space_used_by_directory

logger = getActionLogger('yunohost.migration')


class MyMigration(Migration):

    "Migrate DBs from Postgresql 9.4 to 9.6 after migrating to Stretch"

    def migrate(self):

        if not self.package_is_installed("postgresql-9.4"):
            logger.warning(m18n.n("migration_0005_postgresql_94_not_installed"))
            return

        if not self.package_is_installed("postgresql-9.6"):
            raise YunohostError("migration_0005_postgresql_96_not_installed")

        if not space_used_by_directory("/var/lib/postgresql/9.4") > free_space_in_directory("/var/lib/postgresql"):
            raise YunohostError("migration_0005_not_enough_space", path="/var/lib/postgresql/")

        subprocess.check_call("service postgresql stop", shell=True)
        subprocess.check_call("pg_dropcluster --stop 9.6 main", shell=True)
        subprocess.check_call("pg_upgradecluster -m upgrade 9.4 main", shell=True)
        subprocess.check_call("pg_dropcluster --stop 9.4 main", shell=True)
        subprocess.check_call("service postgresql start", shell=True)

    def backward(self):

        pass

    def package_is_installed(self, package_name):

        p = subprocess.Popen("dpkg --list | grep '^ii ' | grep -q -w {}".format(package_name), shell=True)
        p.communicate()
        return p.returncode == 0
{'fraction_non_alphanumeric': 0.09839052535681748, 'fraction_numerical': 0.023079259034315215, 'mean_word_length': 3.8441176470588236, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3137296', 'n_tokens_mistral': 1101, 'n_tokens_neox': 1029, 'n_words': 214}