|
| 1 | +import commands |
| 2 | +import string |
| 3 | +from time import gmtime, strftime |
| 4 | +import sys, smtplib |
| 5 | +import os |
| 6 | +import re |
| 7 | +import utils |
| 8 | +import shutil |
| 9 | +import tempfile |
| 10 | +import config |
| 11 | + |
| 12 | +""" |
| 13 | +Actions implement a unique interface, Python don't support interfaces natively but |
| 14 | +some API use internal implementation - maybe we can revamp this in a more Python way. |
| 15 | +
|
| 16 | +Interface Action: |
| 17 | + |
| 18 | + DESCRIPTION = # a short description |
| 19 | + |
| 20 | + def run(self, update, packager, distRelease): |
| 21 | + # return str(status) |
| 22 | +""" |
| 23 | + |
| 24 | + |
| 25 | +class WriteSpecfile: |
| 26 | + """This action add a changelog entry, update Version and Release tag, |
| 27 | + and update some defines""" |
| 28 | + |
| 29 | + DESCRIPTION = "Write Specfile Action" |
| 30 | + |
| 31 | + def run(self, update, packager, distRelease): |
| 32 | + result = self._applyTemplate(update, packager, distRelease) |
| 33 | + self.outputFile = os.path.join( |
| 34 | + config.OUTPUT_DIR, packager.dist.ID, |
| 35 | + packager.name.replace(" ", "_"), distRelease.tag, update.packageName + ".spec") |
| 36 | + |
| 37 | + utils.writeFile(self.outputFile, result, 'w') |
| 38 | + return "done" |
| 39 | + |
| 40 | + def _applyTemplate(self, update, packager, distRelease): |
| 41 | + content = packager.dist.getSpecfileContent(update.packageName, distRelease.tag) |
| 42 | + p = re.compile('%changelog') |
| 43 | + part = p.split(content) |
| 44 | + specContent = part[0].strip() |
| 45 | + specChangelogEntries = part[1].strip() |
| 46 | + |
| 47 | + # FIXME: It could be better to have a "reference" bundle (the one that give the package version) |
| 48 | + major, minor, micro, qualifier = utils.parseBundleVersion(update.bundles[0].version) |
| 49 | + |
| 50 | + try: |
| 51 | + m = re.search("Epoch:(\ | \t).*([0-9].*)", specContent, re.IGNORECASE) |
| 52 | + epoch = m.group(2).strip() |
| 53 | + if epoch != "": |
| 54 | + epoch += ":" |
| 55 | + except: |
| 56 | + epoch = "" |
| 57 | + |
| 58 | + m = re.search("Version:(\ |\t)+", specContent, re.IGNORECASE) |
| 59 | + g = m.group(0) |
| 60 | + specContent = re.sub("Version:.*", g + update.bundles[0].version, specContent, re.IGNORECASE) |
| 61 | + |
| 62 | + m = re.search("Release:(\ |\t)+", specContent, re.IGNORECASE) |
| 63 | + g = m.group(0) |
| 64 | + specContent = re.sub("Release:(\ |\b|)+([0-9]|\.|_|[a-z])+", g + "1", specContent, re.IGNORECASE) |
| 65 | + |
| 66 | + specContent = self._setDefineValue(specContent, "major", major) |
| 67 | + specContent = self._setDefineValue(specContent, "minor", minor) |
| 68 | + specContent = self._setDefineValue(specContent, "micro", micro) |
| 69 | + specContent = self._setDefineValue(specContent, "qualifier", qualifier) |
| 70 | + |
| 71 | + # used in eclipse-pydev.spec |
| 72 | + specContent = self._setDefineValue(specContent, "maint", micro) |
| 73 | + # used in eclipse.spec |
| 74 | + specContent = self._setDefineValue(specContent, "eclipse_major", major) |
| 75 | + specContent = self._setDefineValue(specContent, "eclipse_minor", minor) |
| 76 | + specContent = self._setDefineValue(specContent, "eclipse_micro", micro) |
| 77 | + |
| 78 | + template = "${SPEC_CONTENT}\n\n" \ |
| 79 | + + "%changelog\n" \ |
| 80 | + + "* ${DATE} ${PACKAGER} <${PACKAGER_MAIL}> ${EPOCH}${VERSION}-1\n" \ |
| 81 | + + "- bump to ${VERSION}\n\n" \ |
| 82 | + + "${CHANGELOG_ENTRIES}" |
| 83 | + |
| 84 | + result = string.Template(template).safe_substitute( |
| 85 | + SPEC_CONTENT=specContent, |
| 86 | + DATE=strftime("%a %b %d %Y", gmtime()), |
| 87 | + VERSION=major + "." + minor + "." + micro, |
| 88 | + EPOCH=epoch, |
| 89 | + PACKAGER=packager.name, |
| 90 | + PACKAGER_MAIL=packager.mail, |
| 91 | + CHANGELOG_ENTRIES=specChangelogEntries) |
| 92 | + return result |
| 93 | + |
| 94 | + def _setDefineValue(self, specContent, define, value): |
| 95 | + try: |
| 96 | + exp = "%define(\ |\t)+" + define + "(\ |\t)+" |
| 97 | + m = re.search(exp, specContent, re.IGNORECASE) |
| 98 | + g = m.group(0) |
| 99 | + return re.sub("%define(\ |\t)+"+ define + ".*", g + value, specContent, re.IGNORECASE) |
| 100 | + except: |
| 101 | + return specContent |
| 102 | + |
| 103 | + |
| 104 | +class SendMail: |
| 105 | + """This action send email about new updates available |
| 106 | + upstream for they packages""" |
| 107 | + |
| 108 | + DESCRIPTION = "Send Mail Action" |
| 109 | + |
| 110 | + def run(self, update, packager, distRelease): |
| 111 | + content = "" |
| 112 | + |
| 113 | + # one mail by update |
| 114 | + reminderStr = packager.id + "-" + distRelease.tag + "-" + update.packageName \ |
| 115 | + + "-" + update.bundles[0].version |
| 116 | + reminders = [] |
| 117 | + try: |
| 118 | + if not os.path.exists(config.REMINDER_FILE): |
| 119 | + utils.writeFile(config.REMINDER_FILE, "# Remove this file if you will re-send mail alerts\n", "w") |
| 120 | + content = utils.readFile(config.REMINDER_FILE); |
| 121 | + reminders = content.splitlines() |
| 122 | + except: |
| 123 | + pass |
| 124 | + for r in reminders: |
| 125 | + if r == reminderStr: |
| 126 | + return "skipped" |
| 127 | + content += reminderStr + "\n" |
| 128 | + utils.writeFile(config.REMINDER_FILE, content, 'w') |
| 129 | + |
| 130 | + toaddrs = packager.mail + "\n" |
| 131 | + subject = "Updates are available for %s \n" % update.packageName |
| 132 | + msg = "To: " + toaddrs + "\n"\ |
| 133 | + + "From: " + config.FROM_ADRS + "\n" \ |
| 134 | + + "Subject: " + subject + "\n" \ |
| 135 | + + "PACKAGE INFO:\n" \ |
| 136 | + + str(update) \ |
| 137 | + + "\nRELEASE INFO:\n" \ |
| 138 | + + str(distRelease) |
| 139 | + server = smtplib.SMTP(config.SMTP_HOSTNAME) |
| 140 | + server.sendmail(config.FROM_ADRS, toaddrs, msg) |
| 141 | + server.quit() |
| 142 | + return "done" |
| 143 | + |
| 144 | + |
| 145 | +class FedoraMakeSRPM: |
| 146 | + """This action must be run after WriteSpecfile""" |
| 147 | + |
| 148 | + DESCRIPTION = "Make SRPM Action" |
| 149 | + |
| 150 | + def run(self, update, packager, distRelease): |
| 151 | + self._prepare(update, packager, distRelease) |
| 152 | + return "done" |
| 153 | + |
| 154 | + def _prepare(self, update, packager, distRelease): |
| 155 | + # create tmp directory |
| 156 | + currentDir = commands.getoutput("pwd"); |
| 157 | + tmpDir = tempfile.mkdtemp("%s-%s_srpm" % (config.APP_NAME, update.packageName)) |
| 158 | + os.chdir(tmpDir) |
| 159 | + cvsDir = "%s/%s" % (update.packageName, distRelease.tag) |
| 160 | + |
| 161 | + # get sources |
| 162 | + status = os. system( "CVSROOT=:pserver:[email protected]:/cvs/pkgs cvs co %s" % cvsDir) |
| 163 | + if status != 0: |
| 164 | + raise |
| 165 | + |
| 166 | + # os.chdir(cvsDir) |
| 167 | + status = os.system("make -C %s" % cvsDir) |
| 168 | + if status != 0: |
| 169 | + raise |
| 170 | + |
| 171 | + major, minor, micro, qualifier = utils.parseBundleVersion(update.bundles[0].version) |
| 172 | + |
| 173 | + specWriter = WriteSpecfile() |
| 174 | + specWriter.run(update, packager, distRelease) |
| 175 | + specContent = utils.readFile(specWriter.outputFile) |
| 176 | + try: |
| 177 | + m = re.search("source[0|:].*[\ |\t]+(.*)", specContent, re.IGNORECASE) |
| 178 | + src_url = m.group(1) |
| 179 | + src_url = src_url.replace("%{major}", major) |
| 180 | + src_url = src_url.replace("%{minor}", minor) |
| 181 | + src_url = src_url.replace("%{micro}", micro) |
| 182 | + # fix eclipse-pydev define?? |
| 183 | + src_url = src_url.replace("%{maint}", micro) |
| 184 | + status = os.system("wget %s" % src_url) |
| 185 | + if status != 0: |
| 186 | + raise |
| 187 | + status = os.system("make -C %s srpm" % cvsDir) |
| 188 | + if status != 0: |
| 189 | + raise |
| 190 | + except: |
| 191 | + # try to grab sources using fetch-* scripts?? |
| 192 | + raise |
| 193 | + os.chdir(currentDir) |
| 194 | + |
0 commit comments