Exemplo n.º 1
0
 def pop_state():
     """ remove first item from state """
     f = open(Env.state_file(), 'r')
     content = f.read()
     f.close()
     content = content.strip()
     lines = content.split('\n')
     if lines:
         lines.pop(0)
     new_content = ''
     for line in lines:
         new_content += line + '\n'
     f = open(Env.state_file(), 'w')
     f.write(new_content)
     f.close()
Exemplo n.º 2
0
 def state_list():
     """ returns list of undoned transactions from state file """
     f = open(Env.state_file(), 'r')
     content = f.read()
     f.close()
     content = content.strip().split('\n')
     content = [line.strip() for line in content]
     result = []
     for item in content:
         if item != '':
             tmp = {}
             parts = item.split('%')
             tmp['action'] = parts[0]
             tmp['pkg'] = parts[1]
             try:
                 tmp['version'] = parts[2]
             except:
                 tmp['version'] = None
             try:
                 tmp['arch'] = parts[3]
             except:
                 tmp['arch'] = None
             try:
                 tmp['file'] = parts[4]
             except:
                 tmp['file'] = None
             result.append(tmp)
     return result
Exemplo n.º 3
0
    def run(self):
        """ Run test """
        self.assert_equals(
            self.run_command(
                'pkg',
                ['install', 'repository/test-repository/testpkgc-2.0.cati']),
            0)

        state_f = open(Env.state_file(), 'w')
        state_f.write(f'install%testpackage1%1.0%amd64\nremove%anotherpackage')
        state_f.close()

        self.assert_equals(self.run_command('remove', [
            'testpkgc',
            '-y',
        ]), 1)

        self.assert_equals(
            self.run_command(
                'pkg',
                ['install', 'repository/test-repository/testpkgc-2.0.cati']),
            1)

        # tests for cli `state` command
        self.assert_equals(self.run_command('state'), 1)
        self.assert_equals(self.run_command('state', ['--abort', '-y']), 0)
        self.assert_equals(self.run_command('state'), 0)

        self.refresh_env()

        self.assert_equals(
            self.run_command(
                'pkg',
                ['install', 'repository/test-repository/testpkgc-2.0.cati']),
            0)
        self.assert_true(Pkg.is_installed('testpkgc'))
        state_f = open(Env.state_file(), 'w')
        state_f.write(f'remove%testpkgc')
        state_f.close()
        self.assert_equals(self.run_command('state'), 1)
        self.assert_equals(self.run_command('state', ['--complete']), 0)
        self.assert_equals(self.run_command('state'), 0)
        self.assert_true(not Pkg.is_installed('testpkgc'))
Exemplo n.º 4
0
 def add_to_state(calc: Calculator):
     """ add new item to state """
     content = ''
     for item in calc.get_sorted_list():
         content += item['action'] + '%' + item['pkg'].data[
             'name'] + '%' + item['pkg'].data['version'] + '%' + item[
                 'pkg'].data['arch'] + '\n'
     f = open(Env.state_file(), 'w')
     f.write(content)
     f.close()
Exemplo n.º 5
0
def require_root_permission(is_cli=True, die_action=None):
    """
    checks root premission.

    Args:
        is_cli (bool): if is True, when user have not root permission,
            error will print in terminal. but if is False,
            the `die_action` will run as a function.
            (will be disable in testing environment)
        die_action (callable): the function will be run when `is_cli` is False
    """

    # if program is in testing mode don't check permission
    if is_testing:
        return

    if os.getuid() == 0:
        return

    # check write and read access for needed files
    files_to_check = [
        Env.packages_lists(),
        Env.installed_lists(),
        Env.state_file(),
        Env.unremoved_conffiles(),
        Env.security_blacklist(),
        Env.any_scripts(),
        Env.repos_config(),
        Env.repos_config_dir(),
        Env.cache_dir(),
        Env.allowed_archs(),
    ]
    for f in files_to_check:
        if not os.access(f, os.W_OK) or not os.access(f, os.R_OK):
            if is_cli:
                pr.e(ansi.red + sys.argv[0] + ': permission is denied' +
                     ansi.reset)
                pr.exit(1)
                return
            else:
                die_action()
                return
Exemplo n.º 6
0
    def sub_install(self):
        """ install sub command (cati pkg install) """
        if len(self.arguments) <= 1:
            self.message('argument package file(s) required')
            return 1

        require_root_permission()

        # check transactions state before run new transactions
        pr.p('Checking transactions state...')
        state_list = BaseTransaction.state_list(
        )  # get list of undoned transactions
        if state_list:
            # the list is not empty
            StateContentShower.show(state_list)
            return 1

        packages_to_install = []
        i = 1
        while i < len(self.arguments):
            try:
                pkg = archive_factory(self.arguments[i], 'r')
                pkg.read()
                pkg.package_file_path = os.path.abspath(self.arguments[i])
                packages_to_install.append(pkg)
            except FileNotFoundError as ex:
                self.message('file "' + self.arguments[i] + '" not found' +
                             ansi.reset,
                             before=ansi.red)
                return 1
            except:
                self.message('cannot open "' + self.arguments[i] +
                             '": file is corrupt' + ansi.reset,
                             before=ansi.red)
                return 1

            i += 1

        # add packages to state
        state_f = open(Env.state_file(), 'w')
        tmp = ''
        for pkg in packages_to_install:
            tmp += ('install%' + pkg.data['name'] + '%' + pkg.data['version'] +
                    '%' + pkg.data['arch'] + '%' + pkg.package_file_path +
                    '\n')
        state_f.write(tmp)
        state_f.close()

        packages_to_install_names_and_versions = [
            pkg.data['name'] + '@' + pkg.data['version']
            for pkg in packages_to_install
        ]
        i = 0
        while i < len(packages_to_install):
            try:
                pkg = packages_to_install[i]
                tmp = self.install_once(pkg)
                if type(tmp) == int and tmp != 0:
                    if not self.has_option('--dont-ignore-state'):
                        BaseTransaction.finish_all_state()
                    return tmp
                pkg.close()
            except:
                if not self.has_option('--dont-ignore-state'):
                    BaseTransaction.finish_all_state()
                self.message('cannot install "' +
                             packages_to_install[i].data['name'] + '"' +
                             ansi.reset,
                             before=ansi.red)
                return 1
            i += 1
        BaseTransaction.run_any_scripts(
            ['install', packages_to_install_names_and_versions],
            events={
                'start_run_script': self.start_run_any_script_event,
            })
        BaseTransaction.finish_all_state()
Exemplo n.º 7
0
 def finish_all_state():
     """ clear all of states """
     f = open(Env.state_file(), 'w')
     f.write('')
     f.close()