def execute(self): if self._existing_sub: try: ProductSubscription.delete(self.existing_sub.ptask_version_spec, self.existing_sub.product_version_spec) except ProductSubscriptionError as e: raise ActionError("Subscription removal failed: " + str(e)) else: if self.interactive: print "\nExisting subscription removed.\n" try: sub = ProductSubscription.create(self.ptask_version, self.product_version) except ProductSubscriptionError as e: raise ActionError("Subscription failed: " + str(e)) else: if self.interactive: print "New subscription created." if self._no_refresh: return # refresh the subscriptions on disk refresh_action_cls = ActionRegistry().get_action("refresh", "subs") if not refresh_action_cls: raise ActionError("Could not find sub refresh action.") try: refresh_action = refresh_action_cls(self.ptask) refresh_action.interactive = False refresh_action() except ActionError as e: raise ActionError("Failed to refresh subs on disk: " + str(e))
def execute(self): try: ProductSubscription.delete( self.subscription.ptask_version_spec, self.subscription.product_version_spec, ) except ProductSubscriptionError as e: raise ActionError("Subscription removal failed: " + str(e)) else: if self.interactive: print "\nSubscription removed.\n" if self._no_refresh: return # refresh the subscriptions on disk refresh_action_cls = ActionRegistry().get_action('refresh', 'subs') if not refresh_action_cls: raise ActionError("Could not find sub refresh action.") try: refresh_action = refresh_action_cls( self.subscription.ptask_version.ptask) refresh_action.interactive = False refresh_action() except ActionError as e: raise ActionError("Failed to refresh subs on disk: " + str(e))
def validate(self): try: self._sub_id = int(self._sub_id) except ValueError: raise ActionError("Invalid subscription id.") # make sure subscription exists matches = ProductSubscription.list(id=self._sub_id) if len(matches) != 1: raise ActionError( "Unable to identify subscription for id: " + str(self._sub_id) ) self._subscription = matches[0] # make sure subsription is not locked if self._subscription.locked: raise ActionError("Can't remove locked subscription.") # make sure ptask version has no published products if self._subscription.ptask_version.published: raise ActionError( "Can't modify subscriptions. " + \ "The ptask version has published products." )
def _ids_to_subs(self, sub_ids): subs = [] if not sub_ids: return subs for sub_id in sub_ids: if isinstance(sub_id, ProductSubscription): subs.append(sub_id) continue try: sub = int(sub_id) except: raise ActionError("Could not determine sub from: {s}".\ format(s=sub_id)) else: matches = ProductSubscription.list(id=sub_id) if len(matches) != 1: raise ActionError("Unable to identify sub for id: " + \ str(sub_id)) else: subs.append(matches[0]) return subs
def _copy_subs(self): if self.interactive: print "\nCopying forward subscriptions:" ptask_version_spec = self.new_ptask_version exceptions = [] for sub in self.source_version.subscriptions: try: new_sub = ProductSubscription.create( ptask_version_spec, sub.product_version_spec, ) except ProductSubscriptionError as e: exceptions.append((sub, e)) else: print " " + Style.bright + \ str(sub.product_version_spec) + Style.normal if exceptions: msgs = [] for (sub, e) in exceptions: msgs.append(sub.product_version_spec + ": " + str(e)) raise ActionError( "Unable to copy forward some subscriptions:\n\n" + \ "\n".join(msgs) )
def execute(self): for sub in self._subs: update_map = self._update_map[sub.id] cur_ver = update_map['old'] new_ver = update_map['new'] if not new_ver: continue # unsubscribe from the current version try: ProductSubscription.delete( self._ptask_version.spec, cur_ver.spec) except ProductSubscriptionError as e: raise ActionError("Unsubscribe failed: " + str(e)) # subscribe to the new version try: sub = ProductSubscription.create(self._ptask_version, new_ver) except ProductSubscriptionError as e: raise ActionError("Subscribe failed: " + str(e)) if not self._no_refresh: # refresh the subscriptions on disk refresh_action_cls = ActionRegistry().get_action('refresh', 'subs') if not refresh_action_cls: raise ActionError("Could not find sub refresh action.") try: refresh_action = refresh_action_cls(self.ptask) refresh_action.interactive = False refresh_action() except ActionError as e: raise ActionError("Failed to refresh subs on disk: " + str(e)) if self.interactive: print "\nUpdate complete!\n"
def dependent_ptasks(self): """:returns: list of ptasks with subs to a version of this product.""" from dpa.product.subscription import ProductSubscription ptasks = [] # XXX db heavy. see about reducing number of queries here subs = ProductSubscription.list(search=self.spec) for sub in subs: ptasks.append(sub.ptask_version.ptask) return ptasks
def is_subscribed(self, product): from dpa.product.subscription import ProductSubscription from dpa.ptask.spec import PTaskSpec sub_spec = self.spec + "," + product.spec + PTaskSpec.SEPARATOR subs = ProductSubscription.list(search=sub_spec) if len(subs) == 0: return None elif len(subs) > 1: raise PTaskVersionError( "Subscribed to multiple version of the same product! " + \ "This should not be possible. Please report to the " + \ "pipeline team." ) else: return subs[0]
def _source_subs(self, source_ptask, dest_ptask): if self.interactive: print "\nSourcing subscriptions:" dest_ptask_version_spec = dest_ptask.latest_version.spec exceptions = [] for sub in source_ptask.latest_version.subscriptions: try: new_sub = ProductSubscription.create( dest_ptask_version_spec, sub.product_version_spec, ) except ProductSubscriptionError as e: exceptions.append((sub, e)) else: print " " + Style.bright + \ str(sub.product_version_spec) + Style.normal if exceptions: msgs = [] for (sub, e) in exceptions: msgs.append(sub.product_version_spec + ": " + str(e)) raise ActionError( "Unable to copy forward some subscriptions:\n\n" + \ "\n".join(msgs) ) else: # build the import directory... if self.interactive: print "\nRefreshing subscriptions." # refresh the subscriptions on disk refresh_action_cls = ActionRegistry().get_action('refresh', 'subs') if not refresh_action_cls: raise ActionError("Could not find sub refresh action.") try: refresh_action = refresh_action_cls(dest_ptask) refresh_action.interactive = False refresh_action() except ActionError as e: raise ActionError("Failed to refresh subs on disk: " + str(e))
def subscriptions(self): """:returns: a list of ProductSubscriptions for this ptask version.""" from dpa.product.subscription import ProductSubscription return ProductSubscription.list(ptask_version=self.spec)
def validate(self): # validate the ptask if not isinstance(self._ptask, PTask): try: cur_spec = PTaskArea.current().spec full_spec = PTaskSpec.get(self._ptask, relative_to=cur_spec) self._ptask = PTask.get(full_spec) except PTaskError: raise ActionError("Could not determine ptask from: {p}".format( p=self._ptask)) # find the version of the ptask to update if isinstance(self._ptask_version, PTaskVersion): if not self._ptask_version.ptask_spec == self._ptask_spec: raise ActionError( "Supplied ptask version doesn't match supplied ptask.") elif self._ptask_version: matches = PTaskVersion.list( ptask=self._ptask.spec, number=self._ptask_version ) if len(matches) != 1: raise ActionError( "Unable to find ptask '{p}' at version '{v}'".format( p=self._ptask.spec, v=self._ptask_version ) ) else: self._ptask_version = matches[0] else: self._ptask_version = self._ptask.latest_version # XXX rule # don't allow if ptask_version already has published products if self._ptask_version.published: raise ActionError( "Subscriptions can not be modified." + \ "Version {v} of {p} has published products.".format( v=self._ptask_version.number_padded, p=self._ptask.spec )) # XXX subs = [] # valdiate the subscriptions to update if self._subs: # get explicit subs for sub in self._subs: if isinstance(sub, ProductSubscription): subs_to_udpate.append(sub) continue try: sub = int(sub) except: raise ActionError("Could not determine sub from: {s}".\ format(s=sub)) else: matches = ProductSubscription.list(id=sub) if len(matches) != 1: raise ActionError("Unable to identify sub for id: " + \ str(sub)) else: subs.append(matches[0]) else: # all subs for ptask version subs.extend(self._ptask_version.subscriptions) self._subs = subs update_map = defaultdict(dict) for sub in subs: sub_product_ver = sub.product_version update_map[sub.id]['old'] = sub_product_ver if sub.locked: update_map[sub.id]['new'] = None update_map[sub.id]['note'] = 'Subscription locked' continue if sub_product_ver.is_official: update_map[sub.id]['new'] = None update_map[sub.id]['note'] = 'Already subscribed to official' continue sub_product = sub_product_ver.product official_ver = sub_product.official_version if official_ver and official_ver.number > sub_product_ver.number: update_map[sub.id]['new'] = official_ver update_map[sub.id]['note'] = 'Official version' continue if sub.product_version.product.ptask.spec == self.ptask.spec: all_vers = [v for v in sub_product.versions] else: all_vers = [v for v in sub_product.versions if v.published] all_vers.sort(key=lambda v: v.number_padded) if all_vers: latest = all_vers[-1] if latest.number > sub_product_ver.number: update_map[sub.id]['new'] = latest if latest.published: update_map[sub.id]['note'] = 'Latest published version' else: update_map[sub.id]['note'] = 'Latest version' continue else: update_map[sub.id]['new'] = None if sub_product_ver.published: update_map[sub.id]['note'] = \ 'Already using latest published' else: update_map[sub.id]['note'] = 'Already using latest' continue else: update_map[sub.id]['new'] = None update_map[sub.id]['note'] = 'No new versions' continue self._update_map = update_map
def subscriptions(self): from dpa.product.subscription import ProductSubscription return ProductSubscription.list(product_version=self.spec)