Example #1
0
    def create_project(self, target, src=None, rewrite=False,
                       description='', linkto='', linkedbuild=''):
        """
        Create new OBS project based on existing project.
        Copy config and repositories from src project to target
        if src exists.
        """

        if src and not self.exists(src):
            raise ObsError('base project: %s not exists' % src)

        if self.exists(target):
            logger.warning('target project: %s exists' % target)
            if rewrite:
                logger.warning('rewriting target project %s' % target)
            else:
                return

        # Create target meta
        meta = '<project name="%s"><title></title>'\
	       '<description>%s</description>'\
               '<person role="maintainer" userid="%s"/>' % \
               (target, description, conf.get_apiurl_usr(self.apiurl))
        if linkto:
            meta += '<link project="%s"/>' % linkto

        # Collect source repos if src project exist
        if src:
            # Copy debuginfo, build, useforbuild and publish meta
            meta += self.get_tags(src, ['debuginfo', 'build',
                                        'useforbuild', 'publish'])
            # Copy repos to target
            repos = self.get_repos_of_project(src)
            for name in repos:
                if linkedbuild:
                    meta += '<repository name="%s" linkedbuild="%s">' % \
                                (name, linkedbuild)
                else:
                    meta += '<repository name="%s">' % name
                meta += '<path project="%s" repository="%s" />' % (src, name)
                for arch in repos[name]:
                    meta += "<arch>%s</arch>\n" % arch
                meta += "</repository>\n"
        else:
            logger.warning('no project repos in target project, please add '
                           'repos from OBS webUI manually, or specify base '
                           'project with -B <base_prj>, then gbs can help to '
                           'set repos using the settings of the specified '
                           'base project.')
        meta += "</project>\n"

        try:
            # Create project and set its meta
            core.edit_meta('prj', path_args=quote_plus(target), data=meta)
        except (urllib2.URLError, M2Crypto.m2urllib2.URLError,
                M2Crypto.SSL.SSLError), err:
            raise ObsError("Can't set meta for %s: %s" % (target, str(err)))
    def __update_meta(self, project, providers, metatype):
        """Extracts a meta xml from rpm and uploads them to project.

        :returns: uploaded pattern names and error messages
        :rtype: tuple(list, list)
        """
        uploaded = []
        errors = []
        for package, targets in providers.items():
            for target, binaries in targets.items():
                for binary in binaries:
                    with Lab(prefix=metatype) as lab:
                        # Download the rpm
                        try:
                            self.obs.getBinary(project, target, package,
                                               binary, lab.real_path(binary))
                        except HTTPError as exc:
                            errors.append("Failed to download %s: HTTP %s %s" %
                                         (binary, exc.code, exc.filename))
                        except Exception as exc:
                            errors.append("Failed to download %s: %s" % (binary, exc))
                        if errors:
                            return uploaded, errors
                        # Extract pattern (xml) files from the rpm
                        for xml in extract_rpm(lab.real_path(binary), lab.path,
                                               ["*.xml"]):
                            meta = os.path.basename(xml)
                            submetatype = os.path.basename(os.path.dirname(xml))
                            print(meta, metatype, submetatype)
                            try:
                                with open(lab.real_path(xml), 'r') as fd:
                                    metadata = [line.replace("@PROJECT@", project) for line in fd.readlines()]
                                # Update meta
                                if submetatype == "aggregates":
                                    pkgname = os.path.splitext(meta)[0]
                                    core.edit_meta(metatype='pkg', path_args=(project, pkgname), template_args=({'name': pkgname, 'user': '******'}), apiurl=self.obs.apiurl)
                                    u = core.makeurl(self.obs.apiurl, ['source', project, pkgname, '_aggregate'])
                                    print u
                                    print metadata
                                    core.http_PUT(u, data="\n".join(metadata))
                                else:
                                    core.edit_meta(metatype, project, data=metadata)
                                uploaded.append(metatype + '/' + meta)
                            except HTTPError as exc:
                                errors.append("Failed to upload %s:\nHTTP %s %s\n%s" %
                                        (meta, exc.code, exc.filename,
                                            exc.fp.read()))
                            except Exception as exc:
                                errors.append("Failed to upload %s: %s" %
                                        (meta, exc))
                    return uploaded, errors
Example #3
0
    def __update_meta(self, project, providers, metatype):
        """Extracts a meta xml from rpm and uploads them to project.

        :returns: uploaded pattern names and error messages
        :rtype: tuple(list, list)
        """
        uploaded = []
        errors = []
        for package, targets in providers.items():
            for target, binaries in targets.items():
                for binary in binaries:
                    with Lab(prefix=metatype) as lab:
                        # Download the rpm
                        try:
                            self.obs.getBinary(project, target, package,
                                               binary, lab.real_path(binary))
                        except HTTPError as exc:
                            errors.append("Failed to download %s: HTTP %s %s" %
                                          (binary, exc.code, exc.filename))
                        except Exception as exc:
                            errors.append("Failed to download %s: %s" %
                                          (binary, exc))
                        if errors:
                            return uploaded, errors
                        # Extract pattern (xml) files from the rpm
                        for xml in extract_rpm(lab.real_path(binary), lab.path,
                                               ["*.xml"]):
                            meta = os.path.basename(xml)
                            try:
                                with open(lab.real_path(xml), 'r') as fd:
                                    metadata = [
                                        line.replace("@PROJECT@", project)
                                        for line in fd.readlines()
                                    ]
                                # Update meta
                                core.edit_meta(metatype,
                                               project,
                                               data=metadata)
                                uploaded.append(meta)
                            except HTTPError as exc:
                                errors.append(
                                    "Failed to upload %s:\nHTTP %s %s\n%s" %
                                    (meta, exc.code, exc.filename,
                                     exc.fp.read()))
                            except Exception as exc:
                                errors.append("Failed to upload %s: %s" %
                                              (meta, exc))
                    return uploaded, errors
    def __update_meta(self, project, providers, metatype):
        """Extracts a meta xml from rpm and uploads them to project.

        :returns: uploaded pattern names and error messages
        :rtype: tuple(list, list)
        """
        uploaded = []
        errors = []
        for package, targets in providers.items():
            for target, binaries in targets.items():
                for binary in binaries:
                    with Lab(prefix=metatype) as lab:
                        # Download the rpm
                        try:
                            self.obs.getBinary(project, target, package, binary, lab.real_path(binary))
                        except HTTPError as exc:
                            errors.append("Failed to download %s: HTTP %s %s" % (binary, exc.code, exc.filename))
                        except Exception as exc:
                            errors.append("Failed to download %s: %s" % (binary, exc))
                        if errors:
                            return uploaded, errors
                        # Extract pattern (xml) files from the rpm
                        for xml in extract_rpm(lab.real_path(binary), lab.path, ["*.xml"]):
                            meta = os.path.basename(xml)
                            try:
                                with open(lab.real_path(xml), "r") as fd:
                                    metadata = [line.replace("@PROJECT@", project) for line in fd.readlines()]
                                # Update meta
                                core.edit_meta(metatype, project, data=metadata)
                                uploaded.append(meta)
                            except HTTPError as exc:
                                errors.append(
                                    "Failed to upload %s:\nHTTP %s %s\n%s"
                                    % (meta, exc.code, exc.filename, exc.fp.read())
                                )
                            except Exception as exc:
                                errors.append("Failed to upload %s: %s" % (meta, exc))
                    return uploaded, errors
    def __update_meta(self, project, providers, metatype):
        """Extracts a meta xml from rpm and uploads them to project.

        :returns: uploaded pattern names and error messages
        :rtype: tuple(list, list)
        """
        uploaded = []
        errors = []
        for package, targets in providers.items():
            for target, binaries in targets.items():
                for binary in binaries:
                    with Lab(prefix=metatype) as lab:
                        # Download the rpm
                        try:
                            self.obs.getBinary(project, target, package,
                                               binary, lab.real_path(binary))
                        except HTTPError as exc:
                            errors.append("Failed to download %s: HTTP %s %s" %
                                          (binary, exc.code, exc.filename))
                        except Exception as exc:
                            errors.append("Failed to download %s: %s" %
                                          (binary, exc))
                        if errors:
                            return uploaded, errors
                        # Extract pattern (xml) files from the rpm
                        for xml in extract_rpm(lab.real_path(binary), lab.path,
                                               ["*.xml"]):
                            meta = os.path.basename(xml)
                            submetatype = os.path.basename(
                                os.path.dirname(xml))
                            print(meta, metatype, submetatype)
                            try:
                                with open(lab.real_path(xml), 'r') as fd:
                                    metadata = [
                                        line.replace("@PROJECT@", project)
                                        for line in fd.readlines()
                                    ]
                                # Update meta
                                if submetatype == "aggregates":
                                    pkgname = os.path.splitext(meta)[0]
                                    core.edit_meta(metatype='pkg',
                                                   path_args=(project,
                                                              pkgname),
                                                   template_args=({
                                                       'name':
                                                       pkgname,
                                                       'user':
                                                       '******'
                                                   }),
                                                   apiurl=self.obs.apiurl)
                                    u = core.makeurl(self.obs.apiurl, [
                                        'source', project, pkgname,
                                        '_aggregate'
                                    ])
                                    print u
                                    print metadata
                                    core.http_PUT(u, data="\n".join(metadata))
                                else:
                                    core.edit_meta(metatype,
                                                   project,
                                                   data=metadata)
                                uploaded.append(metatype + '/' + meta)
                            except HTTPError as exc:
                                errors.append(
                                    "Failed to upload %s:\nHTTP %s %s\n%s" %
                                    (meta, exc.code, exc.filename,
                                     exc.fp.read()))
                            except Exception as exc:
                                errors.append("Failed to upload %s: %s" %
                                              (meta, exc))
                    return uploaded, errors
    def test(self, logView):
        """Check for availablilty of API
        @param logView: logView to print messages
        @type logView: Instance of logView
        
        This function checks for the project and the package.
        If not present, they'll be created.
        """
        # tweak stdout into logView
        zen=StringIO.StringIO()
        sys.stdout=zen
        #sys.stdout=sys.__stdout__
        if self.configDict.has_key("api_url"):
            #
            #
            logView.appendLines(self.configDict["api_url"]+"\n")
            api = urlparse.urlparse(self.configDict["api_url"])[1]
            logView.appendLines(api+"\n")
            
            # host known ?
            # (check ~/.oscrc)
            if not conf.config.has_key('api_host_options'):
                # key missing !
                msg = str("Konfigurieren sie Benutzername und Passwort für den\nAPI-SERVER in der Datei ~/.oscrc !\n\n".encode("utf-8"))
                WARNING_URL_GUI = GUI_warning_popup.GUI_warning_popup(self.factory, self.myParent, msg) 
                WARNING_URL_GUI.handleEvent()
                return False
            if conf.config['api_host_options'].has_key(api):
                self.apiobject = conf.config['api_host_options'][api]
                self.apiuser = conf.config['api_host_options'][api]['user']
                self.project = "imaging:"+str(self.apiuser)
                self.prjname = self.project
                #
                # check if project exists
                prjlist = core.meta_get_project_list(self.configDict["api_url"])
                #
                # if not there, create it !
                if not self.project in prjlist:
                    #
                    #
                    #create the project  xml-data as in "osc meta prj -e"
                    mydata = ['<project name="'+str(self.project)+'">\n', 
                              '  <title>Imaging Project for'+str(self.apiuser)+'</title>\n', 
                              '  <description>This project provides the imaging.\n', 
                              '\n', 
                              '\n', 
                              '  </description>\n', 
                              '  <person role="maintainer" userid="'+str(self.apiuser)+'"/>\n', 
                              '  <person role="bugowner" userid="'+str(self.apiuser)+'"/>\n', 
                              '  <build>\n', 
                              '    <enable/>\n', 
                              '  </build>\n', 
                              '  <publish>\n', 
                              '    <disable/>\n', 
                              '  </publish>\n', 
                              '  <debuginfo>\n', 
                              '    <disable/>\n', 
                              '  </debuginfo>\n', 
                              '  <repository name="image">\n',]
                    for i in self.configDict["repo_active"]:
                        key_url = str("repo_input_url_"+str(i))
                        tmp = str(str(urlparse.urlparse(self.configDict[key_url])[2]).strip("/"))
                        repository = tmp.split("/")[-1] # last part
                        project_slash = tmp.split(str("/"+repository))[0] # first x parts
                        project_slash_split = project_slash.split("/")  # remove "/"
                        project_name = ""
                        for i in project_slash_split:
                            project_name = str(project_name)+str(i)
                        # ^^^ need to find a more elegant way to do this
                        myappend = '    <path repository="'+str(repository)+'" project="'+str(project_name)+'"/>\n'
                        #mydata.append('    <path repository="%s" project="%s"/>\n')  %(repository, project_name)
                        mydata.append(myappend) 
                    mydata.append('    <arch>i586</arch>\n') 
                    mydata.append('  </repository>\n')
                    mydata.append('</project>\n')
                    apiurl2 = self.configDict["api_url"]
                    logView.appendLines(apiurl2+"\n")
                    #
                    # write the data now
                    core.edit_meta( 
                              data=mydata,
                              metatype="prj", 
                              edit=False, 
                              path_args=quote_plus(self.project), 
                              apiurl=apiurl2
                              )
                    #
                    #write the special prjconf
                    mydata = ['Ignore: distribution-release\n', 'Ignore: distribution-release\n', '\n', '\n', '%if "%_repository" == "image"\n', 'Type: kiwi\n', '%else\n', 'Type: spec\n', '%endif\n']
                    core.edit_meta( 
                              data=mydata,
                              metatype="prjconf", 
                              edit=False, 
                              path_args=quote_plus(self.project), 
                              apiurl=apiurl2
                              )
                #
                # check if it was created
                prjlist = core.meta_get_project_list(self.configDict["api_url"])
                if not self.project in prjlist:
                    #
                    #bail out
                    return False
                #
                #check for the package
                pkglist = core.meta_get_packagelist(self.configDict["api_url"], self.project)
                if not self.pkgname in pkglist:
                    #
                    #create the package
                    files = []
                    mydata = ['<package name="imaging" project="'+str(self.project)+'">\n', '  <title>Limbo imaging Target</title>\n', '  <description>\n', 'LONG DESCRIPTION \n', 'GOES \n', 'HERE\n', '  </description>\n', '  <person role="maintainer" userid="'+str(self.apiuser)+'"/>\n', '  <person role="bugowner" userid="'+str(self.apiuser)+'"/>\n', '  <url>PUT_UPSTREAM_URL_HERE</url>\n', '</package>\n']
                    #
                    # edit_meta
                    core.edit_meta(
                              data=mydata,
                              metatype="pkg", 
                              path_args=(quote_plus(self.project), quote_plus(self.pkgname)), 
                              template_args=({'name': self.pkgname,
                                              'user': self.apiuser
                                              }),
                              apiurl=self.configDict["api_url"]
                              )
#===============================================================================
#                    if False:
#                        #
#                        # display the correct dir when sending the changes
#                        olddir = os.getcwd()
#                        p = core.Package(self.bsDir)
#                        p.todo = files
#                        jahr, monat, tag, stunde, minuten, sekunden = time.localtime()[0:6]
#                        msg = "Auto-checkin by LiRE at %s:%s:%s %s/%s/%s" % (stunde, minuten, sekunden, tag, monat, jahr) 
#                        p.commit(str(msg))
#                        core.set_state(self.pkgname, ' ')
#                        os.chdir(olddir)
# 
#                    pass
#===============================================================================
                #
                # check if it was created
                pkglist = core.meta_get_packagelist(self.configDict["api_url"], self.project)
                if not self.pkgname in pkglist:
                    #
                    #bail out
                    return False
                sys.stdout=sys.__stdout__
                line = zen.getvalue()
                logView.appendLines(line.encode("utf-8"))
                return True
            else:
                msg = str("Konfigurieren sie Benutzername und Passwort für den\nAPI-SERVER in der Datei ~/.oscrc !\n\n".encode("utf-8"))
                WARNING_URL_GUI = GUI_warning_popup.GUI_warning_popup(self.factory, self.myParent, msg) 
                WARNING_URL_GUI.handleEvent()
                return False
        else:
            msg = str("Kkeine API-URL gesetzt!\n\n".encode("utf-8"))
            WARNING_URL_GUI = GUI_warning_popup.GUI_warning_popup(self.factory, self.myParent, msg) 
            WARNING_URL_GUI.handleEvent()
            return False