|
| 1 | +#!/usr/bin/python |
| 2 | +""" Updates symlinks to repositories for image building to stay in sync with build paths |
| 3 | +
|
| 4 | +
|
| 5 | +:term:`Workitem` fields IN: |
| 6 | +
|
| 7 | +:Parameters: |
| 8 | + ev.namespace(string): |
| 9 | + Namespace to use, see here: |
| 10 | + http://wiki.meego.com/Release_Infrastructure/BOSS/OBS_Event_List |
| 11 | + project(string): |
| 12 | + Name of symlink project to get symlink target from |
| 13 | +
|
| 14 | +:term:`Workitem` fields OUT: |
| 15 | +
|
| 16 | +:Returns: |
| 17 | + result(Boolean): |
| 18 | + True if the update was successfull |
| 19 | + msg(list): |
| 20 | + List of error messages |
| 21 | +
|
| 22 | +""" |
| 23 | +import os |
| 24 | +from boss.obs import BuildServiceParticipant |
| 25 | +from osc import core |
| 26 | + |
| 27 | +class ParticipantHandler(BuildServiceParticipant): |
| 28 | + """Participant class as defined by the SkyNET API.""" |
| 29 | + |
| 30 | + def __init__(self): |
| 31 | + """Initializator.""" |
| 32 | + |
| 33 | + BuildServiceParticipant.__init__(self) |
| 34 | + self.prefix = None |
| 35 | + |
| 36 | + def handle_wi_control(self, ctrl): |
| 37 | + """Job control thread.""" |
| 38 | + pass |
| 39 | + |
| 40 | + @BuildServiceParticipant.get_oscrc |
| 41 | + def handle_lifecycle_control(self, ctrl): |
| 42 | + """Participant control thread.""" |
| 43 | + if ctrl.message == "start": |
| 44 | + self.prefix = ctrl.config.get("releases", "prefix") |
| 45 | + |
| 46 | + @BuildServiceParticipant.setup_obs |
| 47 | + def handle_wi(self, wid): |
| 48 | + """Actual job thread.""" |
| 49 | + wid.result = False |
| 50 | + |
| 51 | + if not wid.fields.project: |
| 52 | + raise RuntimeError("Missing mandatory field 'project'") |
| 53 | + project = wid.fields.project |
| 54 | + |
| 55 | + prj_parts = project.split(":") |
| 56 | + if len(prj_parts) < 2 or len(prj_parts) > 3: |
| 57 | + raise RuntimeError("Don't know how to handle %s" % project) |
| 58 | + |
| 59 | + symlink = prj_parts[-1] |
| 60 | + depth = 2 |
| 61 | + |
| 62 | + while depth > 0: |
| 63 | + release_id = None |
| 64 | + platform = None |
| 65 | + platform, release_id, next_project = self._get_plat_rel(project) |
| 66 | + |
| 67 | + self._update_symlink(platform, release_id, symlink) |
| 68 | + if next_project: |
| 69 | + project = next_project |
| 70 | + |
| 71 | + depth = depth - 1 |
| 72 | + |
| 73 | + def _update_symlink(self, platform, release_id, symlink): |
| 74 | + |
| 75 | + symlink_path = os.path.join(self.prefix, platform, symlink) |
| 76 | + symlink_id_path = "%s.id" % symlink_path |
| 77 | + release_path = os.path.join(self.prefix, platform, release_id) |
| 78 | + |
| 79 | + if not os.path.isdir(release_path): |
| 80 | + raise RuntimeError("Release %s doesn't exist at %s" % (release_id, release_path)) |
| 81 | + |
| 82 | + print "creating symlink %s -> %s" % (symlink_path, release_id) |
| 83 | + old_umask = os.umask(000) |
| 84 | + try: |
| 85 | + if os.path.lexists(symlink_path): |
| 86 | + os.unlink(symlink_path) |
| 87 | + os.symlink(release_id, symlink_path) |
| 88 | + |
| 89 | + with open(symlink_id_path, 'w') as id_file: |
| 90 | + id_file.write(release_id) |
| 91 | + finally: |
| 92 | + os.umask(old_umask) |
| 93 | + |
| 94 | + def _get_plat_rel(self, start_project): |
| 95 | + |
| 96 | + next_project = None |
| 97 | + |
| 98 | + for repo in self.obs.getProjectRepositories(start_project): |
| 99 | + for target in self.obs.getRepositoryTargets(start_project, repo): |
| 100 | + next_project = target.split("/")[0] |
| 101 | + platform = next_project.split(":")[0] |
| 102 | + release_id = next_project.split(":")[-1] |
| 103 | + break |
| 104 | + break |
| 105 | + |
| 106 | + if not release_id: |
| 107 | + raise RuntimeError("Couldn't determine release ID for %s %s" % (platform, symlink)) |
| 108 | + |
| 109 | + return platform, release_id, next_project |
0 commit comments