Пример #1
0
 def clean(target):
     for item in shelltools.listdir(target):
         path = os.path.join(target, item)
         if os.path.isdir(path):
             shelltools.remove_dir(path)
         else:
             shelltools.remove_file(path)
Пример #2
0
    def clean_obsolete_content(self):
        '''Cleans obsolete content which belogs to previous installs'''
        if self.instdb.find_package(package_name=self.environment.name, \
                package_category=self.environment.category,
                package_slot=self.environment.slot):
            obsolete = self.compare_different_versions()
            if not obsolete:
                return
            out.normal("cleaning obsolete content")
            directories = []
            for item in obsolete:
                target = os.path.join(self.environment.real_root, item[0][1:])
                if not os.path.exists(target):
                    continue
                if os.path.islink(target):
                    os.unlink(target)
                elif os.path.isfile(target):
                    shelltools.remove_file(target)
                else:
                    directories.append(target)

            directories.reverse()
            for directory in directories:
                if not os.listdir(directory):
                    # Remove directory if it does not include anything
                    shelltools.remove_dir(directory)
Пример #3
0
    def clean_obsolete_content(self):
        '''Cleans obsolete content which belogs to previous installs'''
        if self.instdb.find_package(package_name=self.environment.name, \
                package_category=self.environment.category,
                package_slot=self.environment.slot):
            obsolete = self.compare_different_versions()
            if not obsolete:
                return
            out.normal("cleaning obsolete content")
            directories = []
            for item in obsolete:
                target = os.path.join(self.environment.real_root, item[0][1:])
                if not os.path.exists(target):
                    continue
                if os.path.islink(target):
                    os.unlink(target)
                elif os.path.isfile(target):
                    shelltools.remove_file(target)
                else:
                    directories.append(target)

            directories.reverse()
            for directory in directories:
                if not os.listdir(directory):
                    # Remove directory if it does not include anything
                    shelltools.remove_dir(directory)
Пример #4
0
 def clean(target):
     for item in shelltools.listdir(target):
         path = os.path.join(target, item)
         if os.path.isdir(path):
             shelltools.remove_dir(path)
         else:
             shelltools.remove_file(path)
Пример #5
0
    def remove_content(self):
        dirs = []
        for _file in self.filesdb.get_paths_by_package(self.name, category=self.category, version=self.version):
            _file = _file[0]
            target = os.path.join(self.real_root, _file[1:])
            if os.path.dirname(_file[1:]) == cst.info:
                utils.update_info_index(target, dir_path=os.path.join(self.real_root, cst.info, "dir"), delete=True)

            if os.path.islink(target):
                os.unlink(target)
            elif os.path.isfile(target):
                if os.path.exists(target):
                    shelltools.remove_file(target)
            else:
                dirs.append(target)

        dirs.reverse()
        for target in dirs:
            if os.path.isdir(target) and not os.listdir(target):
                shelltools.remove_dir(target)
Пример #6
0
 def clean_temporary_directory(self):
     '''Cleans temporary directory which contains source code and building environment.'''
     def clean(target):
         for item in shelltools.listdir(target):
             path = os.path.join(target, item)
             if os.path.isdir(path):
                 shelltools.remove_dir(path)
             else:
                 shelltools.remove_file(path)
     # dont remove these directories which are located in work_dir
     exceptions = ('install', 'source')
     if shelltools.listdir(self.internals.env.build_dir):
         clean(self.internals.env.build_dir)
     if shelltools.listdir(self.internals.env.install_dir):
         clean(self.internals.env.install_dir)
     # Now, clean workdir
     for item in shelltools.listdir(self.internals.env.work_dir):
         if not item in exceptions:
             path = os.path.join(self.internals.env.work_dir, item)
             if os.path.isdir(path):
                 shelltools.remove_dir(path)
             else:
                 shelltools.remove_file(path)
Пример #7
0
    def clean_temporary_directory(self):
        '''Cleans temporary directory which contains source code and building environment.'''
        def clean(target):
            for item in shelltools.listdir(target):
                path = os.path.join(target, item)
                if os.path.isdir(path):
                    shelltools.remove_dir(path)
                else:
                    shelltools.remove_file(path)

        # dont remove these directories which are located in work_dir
        exceptions = ('install', 'source')
        if shelltools.listdir(self.internals.env.build_dir):
            clean(self.internals.env.build_dir)
        if shelltools.listdir(self.internals.env.install_dir):
            clean(self.internals.env.install_dir)
        # Now, clean workdir
        for item in shelltools.listdir(self.internals.env.work_dir):
            if not item in exceptions:
                path = os.path.join(self.internals.env.work_dir, item)
                if os.path.isdir(path):
                    shelltools.remove_dir(path)
                else:
                    shelltools.remove_file(path)
Пример #8
0
    def merge_package(self):
        '''Moves files to the target destination in the most safest way.'''
        def get_perms(path):
            '''Get permissions of given path, it may be file or directory'''
            return {"uid": utils.get_uid(path),
                    "gid": utils.get_gid(path),
                    "mod": utils.get_mod(path)
            }
        out.normal("%s/%s/%s-%s:%s is merging to %s" % (self.environment.repo, self.environment.category, \
                self.environment.name, self.environment.version, self.environment.slot, \
                self.environment.real_root))
        # Remove files db entries for this package:slot if it exists
        self.filesdb.delete_item_by_pkgdata(self.environment.category, self.environment.name, \
            self.environment.previous_version, commit=True)

        # Remove file_relations db entries for this package:slot if it exists
        self.file_relationsdb.delete_item_by_pkgdata(self.environment.category, \
                self.environment.name, self.environment.previous_version, commit=True)

        # Merge the package, now
        walk_iter = os.walk(self.environment.install_dir, followlinks=True)
        while True:
            try:
                parent, directories, files = next(walk_iter)
                # TODO: Check the target path's permissions for writing or reading
                # Remove install_dir from parent to get real parent path
                pruned_parent = parent.replace(self.environment.install_dir, "")
                # create directories
                for directory in directories:
                    source = os.path.join(parent, directory)
                    target = os.path.join(self.environment.real_root, pruned_parent, directory)
                    real_target = "/".join([pruned_parent, directory])
                    if self.is_parent_symlink(target):
                        break
                    if os.path.islink(source):
                        self.symlinks.append(target+"/")
                        realpath = os.path.realpath(source)
                        if os.path.islink(target):
                            shelltools.remove_file(target)
                        # create real directory
                        if len(realpath.split(self.environment.install_dir)) > 1:
                            realpath = realpath.split(self.environment.install_dir)[1][1:]

                        shelltools.makedirs(os.path.join(self.environment.real_root, realpath))
                        # make symlink
                        if os.path.isdir(target):
                            shelltools.remove_dir(target)
                        elif os.path.isfile(target):
                            shelltools.remove_file(target)
                        shelltools.make_symlink(os.readlink(source), target)
                    else:
                        if os.path.isfile(target):
                            # TODO: Rename this file and warn the user
                            shelltools.remove_file(target)
                        shelltools.makedirs(target)
                    # Get permissions
                    perms = get_perms(source)
                    # if path is a symlink, pass permission mumbo-jumbos
                    if not os.path.islink(source):
                        # Set permissions
                        shelltools.set_id(target, perms["uid"], perms["gid"])
                        shelltools.set_mod(target, perms["mod"])
                        # TODO: Common items?
                        # Add the item to filesdb
                        self.append_filesdb("dir", real_target, perms)
                    else:
                        # Add the item to filesdb
                        self.append_filesdb("link", real_target, perms, \
                                realpath=os.path.realpath(source))

                # Merge regular files to the target
                # Firstly, handle reserved files
                reserve_files = []
                if self.environment.reserve_files:
                    if isinstance(self.environment.reserve_files, basestring):
                        reserve_files.extend([f_item for f_item in self.environment.reserve_files.split(" ") \
                                if f_item != ""])
                    elif isinstance(self.environment.reserve_files, list) or isinstance(self.environment.reserve_files, tuple):
                        reserve_files.extend(self.environment.reserve_files)

                if os.path.isfile(os.path.join(cst.user_dir, cst.protect_file)):
                    with open(os.path.join(cst.user_dir, cst.protect_file)) as data:
                        for rf in data.readlines():
                            if not rf.startswith("#"):
                                reserve_files.append(rf.strip())

                # Here we are starting to merge
                for _file in files:
                    source = os.path.join(parent, _file)
                    target = os.path.join(self.environment.real_root, pruned_parent, _file)
                    real_target = "/".join([pruned_parent, _file])
                    if self.is_parent_symlink(target):
                        break
                    # Keep file relations for using after to handle reverse dependencies
                    if os.path.exists(source) and os.access(source, os.X_OK):
                        if utils.get_mimetype(source) in self.binary_filetypes:
                            self.file_relationsdb.append_query((
                                self.environment.repo,
                                self.environment.category,
                                self.environment.name,
                                self.environment.version,
                                target,
                                file_relations.get_depends(source))
                            )
                    # Strip binary files and keep them smaller
                    if self.strip_debug_symbols and utils.get_mimetype(source) in self.binary_filetypes:
                        utils.run_strip(source)
                    if self.environment.ignore_reserve_files:
                        reserve_files = []
                        self.environment.reserve_files = True

                    def add_file_item():
                        # Prevent code duplication
                        if not os.path.islink(target):
                            shelltools.set_id(target, perms["uid"], perms["gid"])
                            shelltools.set_mod(target, perms["mod"])
                            self.append_filesdb("file", real_target, perms, \
                                    sha1sum=utils.sha1sum(target),
                                    size = utils.get_size(source, dec=True)
                            )
                        else:
                            self.append_filesdb("link", real_target, perms,\
                                    realpath=os.path.realpath(source))

                    if self.environment.reserve_files is not False:
                        conf_file = os.path.join(pruned_parent, _file)
                        isconf = (_file.endswith(".conf") or _file.endswith(".cfg"))
                        def is_reserve():
                            if self.environment.ignore_reserve_files:
                                return False
                            elif not conf_file in reserve_files:
                                return False
                            return True

                        if os.path.exists(target) and not is_reserve():
                            if pruned_parent[0:4] == "/etc" or isconf:
                                if os.path.isfile(conf_file) and utils.sha1sum(source) != utils.sha1sum(conf_file):
                                    self.append_merge_conf(conf_file)
                                    target = target+".lpms-backup" 
                                    self.backup.append(target)

                        if os.path.exists(target) and is_reserve():
                            # The file is reserved.
                            # Adds to filesdb
                            add_file_item()
                            # We don't need the following operations
                            continue

                    if os.path.islink(source):
                        sha1 = False
                        realpath = os.readlink(source)
                        if self.environment.install_dir in realpath:
                            realpath = realpath.split(self.environment.install_dir)[1]

                        if os.path.isdir(target):
                            shelltools.remove_dir(target)
                        elif os.path.isfile(target) or os.path.islink(target):
                            shelltools.remove_file(target)
                        shelltools.make_symlink(realpath, target)
                    else:
                        sha1 = utils.sha1sum(source)
                        perms = get_perms(source)
                        shelltools.move(source, target)
                    # Adds to filesdb
                    add_file_item()
            except StopIteration as err:
                break

        self.file_relationsdb.insert_query(commit=True)
        self.filesdb.insert_query(commit=True)

        lpms.logger.info("%s/%s has been merged to %s." % (self.environment.category, self.environment.fullname, \
                self.environment.real_root))
Пример #9
0
    def set_environment_variables(self):
        '''Sets environment variables that used interpreter and other parts of lpms'''
        # TODO: This part seems dirty
        if self.inline_option_targets is not None and \
                self.package.id in self.inline_option_targets:
            self.internals.env.inline_option_targets = self.inline_option_targets[self.package.id]
        if self.conditional_versions is not None and \
                self.package.id in self.conditional_versions:
            self.internals.env.conditional_versions = self.conditional_versions[self.package.id]

        self.internals.env.package = self.package
        if self.dependencies is not None:
            self.internals.env.dependencies = self.dependencies.get(self.package.id, None)
        installed_package = self.instdb.find_package(package_name=self.package.name, \
                package_category=self.package.category, package_slot=self.package.slot)
        self.internals.env.previous_version = installed_package.get(0).version \
                if installed_package else None

        # Handle package conflicts and remove that conflicts if required
        # TODO: This mech. is obsolete
        if self.conflicts is not None and self.package.id in self.conflicts:
            conflict_instruct = self.instruction
            conflict_instruct.count = len(self.conflicts[self.package.id])
            for index, conflict in enumerate(self.conflicts[self.package.id], 1):
                conflict_instruct['index'] = index
                conflict_category, conflict_name, conflict_slot = self.conflict.split("/")
                conflict_package = self.instdb.find_package(package_name=conflict_name, \
                        package_category=conflict_category, \
                        package_slot=conflict_slot).get(0)
                if not initpreter.InitializeInterpreter(conflict_package, conflict_instruct,
                        ['remove'], remove=True).initialize():
                    out.error("an error occured during remove operation: %s/%s/%s-%s" % \
                            (conflict_package.repo, conflict_package.category, \
                            conflict_package.name, conflict_package.version))

        # FIXME: This is no good, perhaps, we should only import some variables to internal environment
        self.internals.env.raw.update(self.instruction.raw)

        # Absolute path of the spec file.
        self.internals.env.spec_file = os.path.join(
                cst.repos,
                self.package.repo,
                self.package.category,
                self.package.name,
                self.package.name+"-"+self.package.version+cst.spec_suffix
        )

        # Set metadata fields from the spec file.
        metadata_fields = ('repo', 'name', 'category', 'name', 'version', 'slot', 'options')
        for field in metadata_fields:
            setattr(self.internals.env, field, getattr(self.package, field))

        # Fullname of the package thats consists of its name and version
        self.internals.env.fullname = self.internals.env.name+"-"+self.internals.env.version

        # applied options is a set that contains options which will be applied to the package
        if self.options is not None and self.package.id in self.options:
            self.internals.env.applied_options = self.options[self.package.id]

        # set local environment variable
        if not self.instruction.unset_env_variables:
           self.set_local_environment_variables()

        interphase = re.search(r'-r[0-9][0-9]', self.internals.env.version)
        if not interphase:
            interphase = re.search(r'-r[0-9]', self.internals.env.version)
        # Before setting raw_version and revision, set their initial values
        self.internals.env.revision = ""
        self.internals.env.raw_version = self.internals.env.version

        # Now, set real values of these variables if package revisioned. 
        if interphase is not None and interphase.group():
            self.internals.env.raw_version = self.internals.env.version.replace(interphase.group(), "")
            self.internals.env.revision = interphase.group()

        # Import the spec
        self.mangle_spec()
        metadata = utils.metadata_parser(self.internals.env.metadata)
        if metadata.has_key("src_url"):
            self.internals.env.src_url = metadata["src_url"]
        else:
            if not hasattr(self.internals.env, "src_url"):
                self.internals.env.src_url = None

        if self.internals.env.srcdir is None:
            # Cut revision number from srcdir prevent unpacking fails
            srcdir = self.internals.env.name+"-"\
                    +self.internals.env.version.replace(self.internals.env.revision, "")
            self.internals.env.srcdir = srcdir

        filesdir = os.path.join(
                cst.repos,
                self.internals.env.repo,
                self.internals.env.category,
                self.internals.env.name,
                cst.files_dir
        )
        setattr(self.internals.env, "filesdir", filesdir)

        # TODO: What is src_cache?
        setattr(self.internals.env, "src_cache", cst.src_cache)

        # Set sandbox variable to switch sandbox
        if not self.config.sandbox and self.instruction.enable_sandbox:
            self.internals.env.sandbox = True
        elif self.config.sandbox and self.instruction.disable_sandbox:
            self.internals.env.sandbox = False
        else:
            self.internals.env.sandbox = self.config.sandbox

        # Set work_dir, build_dir and install_dir variables to lpms' internal build environment.
        self.internals.env.work_dir = os.path.join(
                self.config.build_dir,
                self.internals.env.category,
                self.internals.env.fullname
        )
        self.internals.env.build_dir = os.path.join(
                self.config.build_dir,
                self.internals.env.category,
                self.internals.env.fullname,
                "source",
                self.internals.env.srcdir)
        self.internals.env.install_dir = os.path.join(
                self.config.build_dir,
                self.internals.env.category,
                self.internals.env.fullname,
                "install")

        # Create these directories
        for target in ('build_dir', 'install_dir'):
            if not os.path.isdir(getattr(self.internals.env, target)):
                os.makedirs(getattr(self.internals.env, target))
        if not self.instruction.resume_build and len(os.listdir(self.internals.env.install_dir)):
            shelltools.remove_dir(self.internals.env.install_dir)
Пример #10
0
    def merge_package(self):
        '''Moves files to the target destination in the most safest way.'''
        def get_perms(path):
            '''Get permissions of given path, it may be file or directory'''
            return {
                "uid": utils.get_uid(path),
                "gid": utils.get_gid(path),
                "mod": utils.get_mod(path)
            }
        out.normal("%s/%s/%s-%s:%s is merging to %s" % (self.environment.repo, self.environment.category, \
                self.environment.name, self.environment.version, self.environment.slot, \
                self.environment.real_root))
        # Remove files db entries for this package:slot if it exists
        self.filesdb.delete_item_by_pkgdata(self.environment.category, self.environment.name, \
            self.environment.previous_version, commit=True)

        # Remove file_relations db entries for this package:slot if it exists
        self.file_relationsdb.delete_item_by_pkgdata(self.environment.category, \
                self.environment.name, self.environment.previous_version, commit=True)

        # Merge the package, now
        walk_iter = os.walk(self.environment.install_dir, followlinks=True)
        while True:
            try:
                parent, directories, files = next(walk_iter)
                # TODO: Check the target path's permissions for writing or reading
                # Remove install_dir from parent to get real parent path
                pruned_parent = parent.replace(self.environment.install_dir,
                                               "")
                # create directories
                for directory in directories:
                    source = os.path.join(parent, directory)
                    target = os.path.join(self.environment.real_root,
                                          pruned_parent, directory)
                    real_target = "/".join([pruned_parent, directory])
                    if self.is_parent_symlink(target):
                        break
                    if os.path.islink(source):
                        self.symlinks.append(target + "/")
                        realpath = os.path.realpath(source)
                        if os.path.islink(target):
                            shelltools.remove_file(target)
                        # create real directory
                        if len(realpath.split(
                                self.environment.install_dir)) > 1:
                            realpath = realpath.split(
                                self.environment.install_dir)[1][1:]

                        shelltools.makedirs(
                            os.path.join(self.environment.real_root, realpath))
                        # make symlink
                        if os.path.isdir(target):
                            shelltools.remove_dir(target)
                        elif os.path.isfile(target):
                            shelltools.remove_file(target)
                        shelltools.make_symlink(os.readlink(source), target)
                    else:
                        if os.path.isfile(target):
                            # TODO: Rename this file and warn the user
                            shelltools.remove_file(target)
                        shelltools.makedirs(target)
                    # Get permissions
                    perms = get_perms(source)
                    # if path is a symlink, pass permission mumbo-jumbos
                    if not os.path.islink(source):
                        # Set permissions
                        shelltools.set_id(target, perms["uid"], perms["gid"])
                        shelltools.set_mod(target, perms["mod"])
                        # TODO: Common items?
                        # Add the item to filesdb
                        self.append_filesdb("dir", real_target, perms)
                    else:
                        # Add the item to filesdb
                        self.append_filesdb("link", real_target, perms, \
                                realpath=os.path.realpath(source))

                # Merge regular files to the target
                # Firstly, handle reserved files
                reserve_files = []
                if self.environment.reserve_files:
                    if isinstance(self.environment.reserve_files, basestring):
                        reserve_files.extend([f_item for f_item in self.environment.reserve_files.split(" ") \
                                if f_item != ""])
                    elif isinstance(self.environment.reserve_files,
                                    list) or isinstance(
                                        self.environment.reserve_files, tuple):
                        reserve_files.extend(self.environment.reserve_files)

                if os.path.isfile(os.path.join(cst.user_dir,
                                               cst.protect_file)):
                    with open(os.path.join(cst.user_dir,
                                           cst.protect_file)) as data:
                        for rf in data.readlines():
                            if not rf.startswith("#"):
                                reserve_files.append(rf.strip())

                # Here we are starting to merge
                for _file in files:
                    source = os.path.join(parent, _file)
                    target = os.path.join(self.environment.real_root,
                                          pruned_parent, _file)
                    real_target = "/".join([pruned_parent, _file])
                    if self.is_parent_symlink(target):
                        break
                    # Keep file relations for using after to handle reverse dependencies
                    if os.path.exists(source) and os.access(source, os.X_OK):
                        if utils.get_mimetype(source) in self.binary_filetypes:
                            self.file_relationsdb.append_query(
                                (self.environment.repo,
                                 self.environment.category,
                                 self.environment.name,
                                 self.environment.version, target,
                                 file_relations.get_depends(source)))
                    # Strip binary files and keep them smaller
                    if self.strip_debug_symbols and utils.get_mimetype(
                            source) in self.binary_filetypes:
                        utils.run_strip(source)
                    if self.environment.ignore_reserve_files:
                        reserve_files = []
                        self.environment.reserve_files = True

                    def add_file_item():
                        # Prevent code duplication
                        if not os.path.islink(target):
                            shelltools.set_id(target, perms["uid"],
                                              perms["gid"])
                            shelltools.set_mod(target, perms["mod"])
                            self.append_filesdb("file", real_target, perms, \
                                    sha1sum=utils.sha1sum(target),
                                    size = utils.get_size(source, dec=True)
                            )
                        else:
                            self.append_filesdb("link", real_target, perms,\
                                    realpath=os.path.realpath(source))

                    if self.environment.reserve_files is not False:
                        conf_file = os.path.join(pruned_parent, _file)
                        isconf = (_file.endswith(".conf")
                                  or _file.endswith(".cfg"))

                        def is_reserve():
                            if self.environment.ignore_reserve_files:
                                return False
                            elif not conf_file in reserve_files:
                                return False
                            return True

                        if os.path.exists(target) and not is_reserve():
                            if pruned_parent[0:4] == "/etc" or isconf:
                                if os.path.isfile(conf_file) and utils.sha1sum(
                                        source) != utils.sha1sum(conf_file):
                                    self.append_merge_conf(conf_file)
                                    target = target + ".lpms-backup"
                                    self.backup.append(target)

                        if os.path.exists(target) and is_reserve():
                            # The file is reserved.
                            # Adds to filesdb
                            add_file_item()
                            # We don't need the following operations
                            continue

                    if os.path.islink(source):
                        sha1 = False
                        realpath = os.readlink(source)
                        if self.environment.install_dir in realpath:
                            realpath = realpath.split(
                                self.environment.install_dir)[1]

                        if os.path.isdir(target):
                            shelltools.remove_dir(target)
                        elif os.path.isfile(target) or os.path.islink(target):
                            shelltools.remove_file(target)
                        shelltools.make_symlink(realpath, target)
                    else:
                        sha1 = utils.sha1sum(source)
                        perms = get_perms(source)
                        shelltools.move(source, target)
                    # Adds to filesdb
                    add_file_item()
            except StopIteration as err:
                break

        self.file_relationsdb.insert_query(commit=True)
        self.filesdb.insert_query(commit=True)

        lpms.logger.info("%s/%s has been merged to %s." % (self.environment.category, self.environment.fullname, \
                self.environment.real_root))
Пример #11
0
    def set_environment_variables(self):
        '''Sets environment variables that used interpreter and other parts of lpms'''
        # TODO: This part seems dirty
        if self.inline_option_targets is not None and \
                self.package.id in self.inline_option_targets:
            self.internals.env.inline_option_targets = self.inline_option_targets[
                self.package.id]
        if self.conditional_versions is not None and \
                self.package.id in self.conditional_versions:
            self.internals.env.conditional_versions = self.conditional_versions[
                self.package.id]

        self.internals.env.package = self.package
        if self.dependencies is not None:
            self.internals.env.dependencies = self.dependencies.get(
                self.package.id, None)
        installed_package = self.instdb.find_package(package_name=self.package.name, \
                package_category=self.package.category, package_slot=self.package.slot)
        self.internals.env.previous_version = installed_package.get(0).version \
                if installed_package else None

        # Handle package conflicts and remove that conflicts if required
        # TODO: This mech. is obsolete
        if self.conflicts is not None and self.package.id in self.conflicts:
            conflict_instruct = self.instruction
            conflict_instruct.count = len(self.conflicts[self.package.id])
            for index, conflict in enumerate(self.conflicts[self.package.id],
                                             1):
                conflict_instruct['index'] = index
                conflict_category, conflict_name, conflict_slot = self.conflict.split(
                    "/")
                conflict_package = self.instdb.find_package(package_name=conflict_name, \
                        package_category=conflict_category, \
                        package_slot=conflict_slot).get(0)
                if not initpreter.InitializeInterpreter(
                        conflict_package,
                        conflict_instruct, ['remove'],
                        remove=True).initialize():
                    out.error("an error occured during remove operation: %s/%s/%s-%s" % \
                            (conflict_package.repo, conflict_package.category, \
                            conflict_package.name, conflict_package.version))

        # FIXME: This is no good, perhaps, we should only import some variables to internal environment
        self.internals.env.raw.update(self.instruction.raw)

        # Absolute path of the spec file.
        self.internals.env.spec_file = os.path.join(
            cst.repos, self.package.repo, self.package.category,
            self.package.name,
            self.package.name + "-" + self.package.version + cst.spec_suffix)

        # Set metadata fields from the spec file.
        metadata_fields = ('repo', 'name', 'category', 'name', 'version',
                           'slot', 'options')
        for field in metadata_fields:
            setattr(self.internals.env, field, getattr(self.package, field))

        # Fullname of the package thats consists of its name and version
        self.internals.env.fullname = self.internals.env.name + "-" + self.internals.env.version

        # applied options is a set that contains options which will be applied to the package
        if self.options is not None and self.package.id in self.options:
            self.internals.env.applied_options = self.options[self.package.id]

        # set local environment variable
        if not self.instruction.unset_env_variables:
            self.set_local_environment_variables()

        interphase = re.search(r'-r[0-9][0-9]', self.internals.env.version)
        if not interphase:
            interphase = re.search(r'-r[0-9]', self.internals.env.version)
        # Before setting raw_version and revision, set their initial values
        self.internals.env.revision = ""
        self.internals.env.raw_version = self.internals.env.version

        # Now, set real values of these variables if package revisioned.
        if interphase is not None and interphase.group():
            self.internals.env.raw_version = self.internals.env.version.replace(
                interphase.group(), "")
            self.internals.env.revision = interphase.group()

        # Import the spec
        self.mangle_spec()
        metadata = utils.metadata_parser(self.internals.env.metadata)
        if metadata.has_key("src_url"):
            self.internals.env.src_url = metadata["src_url"]
        else:
            if not hasattr(self.internals.env, "src_url"):
                self.internals.env.src_url = None

        if self.internals.env.srcdir is None:
            # Cut revision number from srcdir prevent unpacking fails
            srcdir = self.internals.env.name+"-"\
                    +self.internals.env.version.replace(self.internals.env.revision, "")
            self.internals.env.srcdir = srcdir

        filesdir = os.path.join(cst.repos, self.internals.env.repo,
                                self.internals.env.category,
                                self.internals.env.name, cst.files_dir)
        setattr(self.internals.env, "filesdir", filesdir)

        # TODO: What is src_cache?
        setattr(self.internals.env, "src_cache", cst.src_cache)

        # Set sandbox variable to switch sandbox
        if not self.config.sandbox and self.instruction.enable_sandbox:
            self.internals.env.sandbox = True
        elif self.config.sandbox and self.instruction.disable_sandbox:
            self.internals.env.sandbox = False
        else:
            self.internals.env.sandbox = self.config.sandbox

        # Set work_dir, build_dir and install_dir variables to lpms' internal build environment.
        self.internals.env.work_dir = os.path.join(self.config.build_dir,
                                                   self.internals.env.category,
                                                   self.internals.env.fullname)
        self.internals.env.build_dir = os.path.join(
            self.config.build_dir, self.internals.env.category,
            self.internals.env.fullname, "source", self.internals.env.srcdir)
        self.internals.env.install_dir = os.path.join(
            self.config.build_dir, self.internals.env.category,
            self.internals.env.fullname, "install")

        # Create these directories
        for target in ('build_dir', 'install_dir'):
            if not os.path.isdir(getattr(self.internals.env, target)):
                os.makedirs(getattr(self.internals.env, target))
        if not self.instruction.resume_build and len(
                os.listdir(self.internals.env.install_dir)):
            shelltools.remove_dir(self.internals.env.install_dir)
Пример #12
0
def rmdir(target):
    paths = glob.glob(fix_target_path(target))
    if not paths:
        raise BuildError("no directory matched pattern: %s" % fix_target_path(target))
    for path in paths:
        shelltools.remove_dir(path)
Пример #13
0
    def package_mangler(self, **kwargs):
        def collision_check():
            # TODO: This is a temporary solution. collision_check function
            # must be a reusable part for using in remove operation
            out.normal("checking file collisions...")
            lpms.logger.info("checking file collisions")
            collision_object = file_collisions.CollisionProtect(
                environment.category,
                environment.name,
                environment.slot,
                real_root=environment.real_root,
                source_dir=environment.install_dir)
            collision_object.handle_collisions()
            if collision_object.orphans:
                out.write(
                    out.color(" > ", "brightyellow") +
                    "these files are orphan. the package will adopt the files:\n"
                )
                index = 0
                for orphan in collision_object.orphans:
                    out.notify(orphan)
                    index += 1
                    if index > 100:
                        # FIXME: the files must be logged
                        out.write(
                            out.color(" > ", "brightyellow") +
                            "...and many others.")
                        break

            if collision_object.collisions:
                out.write(
                    out.color(" > ", "brightyellow") +
                    "file collisions detected:\n")
            for item in collision_object.collisions:
                (category, name, slot, version), path = item
                out.write(out.color(" -- ", "red")+category+"/"+name+"-"\
                        +version+":"+slot+" -> "+path+"\n")
            if collision_object.collisions and self.config.collision_protect:
                if environment.force_file_collision:
                    out.warn(
                        "Disregarding these collisions, you have been warned!")
                else:
                    return False
            return True

        names = kwargs.get("names", self.request.names)
        # Prepare build environment
        out.normal("resolving dependencies")
        targets = api.resolve_dependencies(names, self.request.instruction)
        if self.request.instruction.pretend:
            out.write("\n")
            out.normal("these packages will be merged, respectively:\n")
            showplan.show(targets.packages,
                          targets.conflicts,
                          targets.options,
                          installdb=dbapi.InstallDB())
            out.write("\ntotal %s package(s) listed.\n\n" \
                    % out.color(str(len(targets.packages)), "green"))
            raise LpmsTerminate

        if self.request.instruction.ask:
            out.write("\n")
            out.normal("these packages will be merged, respectively:\n")
            showplan.show(targets.packages,
                          targets.conflicts,
                          targets.options,
                          installdb=dbapi.InstallDB())
            utils.xterm_title("lpms: confirmation request")
            out.write("\ntotal %s package(s) will be merged.\n\n" \
                    % out.color(str(len(targets.packages)), "green"))

            if not utils.confirm("Would you like to continue?"):
                # Reset terminal title and terminate lpms.
                utils.xterm_title_reset()
                raise LpmsTerminate

        self.request.instruction.count = len(targets.packages)
        for index, package in enumerate(targets.packages, 1):
            self.request.instruction.index = index
            retval, environment = api.prepare_environment(
                    package,
                    self.request.instruction,
                    dependencies=targets.dependencies[package.id] if package.id in \
                            targets.dependencies else None,
                    options=targets.options[package.id] if package.id in \
                            targets.options else None,
                    conditional_versions=targets.conditional_versions[package.id] \
                            if package.id in targets.conditional_versions else None,
                    conflicts=targets.conflicts[package.id] if package.id \
                            in targets.conflicts else None,
                    inline_option_targets=targets.inline_option_targets[package.id] \
                            if package.id in targets.inline_option_targets else None
            )
            if not retval:
                out.error(
                    "There are some errors while preparing environment to build FOO."
                )
                out.error(
                    "So you should submit a bug report to fix the issue.")
                raise LpmsTerminate("thanks to flying with lpms.")
            # Now, run package script(spec) for configuring, building and install
            retval, environment = self.interpreter.initialize(environment)
            if retval is False:
                out.error(
                    "There are some errors while building FOO from source.")
                out.error("Error messages should be seen above.")
                out.error(
                    "If you want to submit a bug report, please attatch BAR or send above messages in a proper way."
                )
                raise LpmsTerminate("thanks to flying with lpms.")
            elif retval is None:
                raise LpmsTerminate

            if not collision_check():
                out.error(
                    "File collisions detected. If you want to overwrite these files,"
                )
                out.error(
                    "You have to use --force-file-collisions parameter or disable collision_protect in configuration file."
                )
                raise LpmsTerminate("thanks to flying with lpms.")

            # Merge package to livefs
            if environment.not_merge:
                raise LpmsTerminate("not merging...")
            retval, environment = merge.Merge(environment).perform_operation()
            if not retval:
                raise LpmsTerminate("Some errors occured while merging %s" %
                                    environment.fullname)

            lpms.logger.info("finished %s/%s/%s-%s" %
                             (package.repo, package.category, package.name,
                              package.version))

            utils.xterm_title("lpms: %s/%s finished" %
                              (package.category, package.name))

            out.normal("Cleaning build directory")
            shelltools.remove_dir(os.path.dirname(environment.install_dir))
            catdir = os.path.dirname(os.path.dirname(environment.install_dir))
            if not os.listdir(catdir):
                shelltools.remove_dir(catdir)

            # There is no error, exitting...
            out.normal("Completed.")