def _create_status(self): existing = ProductRepresentationStatus.list( product_representation=self._product_repr.spec, location=current_location_code(), ) if len(existing) == 1: self._product_repr_status = existing.pop() if self.interactive: print "\nProduct representation status exists: " + \ Style.bright + self._product_repr_status.spec + Style.reset else: try: self._product_repr_status = ProductRepresentationStatus.create( product_representation=self._product_repr.spec, location=current_location_code(), status=1, ) except ProductRepresentationStatusError as e: raise ActionError( "Unable to create product representation status: " + str(e)) else: if self.interactive: print "\nCreated product representation status: " + \ Style.bright + self._product_repr_status.spec + \ Style.reset
def verify(self): ptask_field = 'PTask' latest_version_field = 'Latest version' from_field = 'From' to_field = 'To' output = Output() output.header_names = [ ptask_field, latest_version_field, from_field, to_field, ] output.add_item( { ptask_field: self.ptask.spec, latest_version_field: self.ptask_latest_version.number_padded, from_field: self.ptask_latest_version.location_code, to_field: current_location_code(), }, color_all=Style.bright, ) output.title = "Confirm transfer:" output.dump() if not Output.prompt_yes_no(Style.bright + "Transfer" + Style.reset): raise ActionAborted("User chose not to proceed.")
def _create_version(self): new_version = self.ptask.next_version_number location_code = current_location_code() try: self._new_ptask_version = PTaskVersion.create( current_username(), "in progress...".format(n=new_version), location_code, ptask_spec=self.ptask.spec, number=new_version, parent_spec=self.source_version.spec, ) except PTaskVersionError as e: raise ActionError("Failed to create ptask version: " + str(e)) else: print "\nNew version successfully created in the database." # ---- provision a version directory in the ptask area try: self.ptask.area.provision( self.ptask.area.dir(version=new_version, verify=False) ) except PTaskAreaError as e: raise ActionError("Unable to provision version directory.") else: print "\nSuccessfully provisioned directory for version: " + \ str(new_version)
def _create_representation(self): existing = ProductRepresentation.list( product_version=self._product_version.spec, resolution=self._resolution, representation_type=self._file_type, ) if len(existing) == 1: self._product_repr = existing.pop() if self.interactive: print "\nProduct representation exists: " + \ Style.bright + self._product_repr.spec + Style.reset else: try: self._product_repr = ProductRepresentation.create( product_version=self._product_version.spec, resolution=self._resolution, representation_type=self._file_type, creation_location=current_location_code(), creator=current_username(), ) except ProductRepresentationError as e: raise ActionError( "Unable to create product representation: " + str(e)) else: if self.interactive: print "\nCreated product representation: " + \ Style.bright + self._product_repr.spec + Style.reset
def setup_cl_args(cls, parser): parser.add_argument( "code", nargs="?", default=current_location_code(), help="Print info for the supplied location code." )
def validate(self): super(PTaskSourceAction, self).validate() # ---- make sure the destination location is the current location. cur_loc_code = current_location_code() if self.destination_version: dest_loc_code = self.destination_version.location_code else: dest_loc_code = self.destination_latest_version.location_code if cur_loc_code != dest_loc_code: raise ActionError("Destination location must be this location.")
def execute(self): # sync the contents of the latest version to this location self._sync_latest_remote() # update the latest version and set it's location to this location try: self.ptask_latest_version.update( location=current_location_code(), ) except PTaskVersionError as e: raise ActionError( "Failed to update the location of latest version of the " + \ "ptask: " + str(e) ) print "\nTransfer successful!\n"
def _get_path(self, ptask, version=None, latest_version=None, directory=None, location_override=None): cur_loc_code = current_location_code() if version: version_num = version.number else: if not latest_version: latest_version = ptask.latest_version version = latest_version version_num = None if location_override: loc_code = location_override.code else: loc_code = version.location_code if loc_code == cur_loc_code: try: path = ptask.area.dir( version=version_num, dir_name=directory, ) except PTaskAreaError: raise ActionError( "Path for 's' does not exist.".format(s=ptask.spec), ) path += "/" else: location = version.location if not location.host: raise ActionError( "Unable to sync with location '{l}'. Unknown host.".\ format(l=location.name) ) path = ptask.area.dir( version=version_num, dir_name=directory, root=location.filesystem_root, verify=False, ) path = current_username() + "@" + location.host + ":" + path + "/" return path
def validate(self): # make sure the ptask evaluates to a real ptask try: self._ptask = PTask.get(self.ptask) except PTaskError: raise ActionError("Unable to determine ptask from: " + str(self.ptask)) # for efficiency self._ptask_latest_version = self.ptask.latest_version # make sure the latest version is not this location if self.ptask_latest_version.location_code == current_location_code(): raise ActionError( "Latest version of {b}{p}{r} already owned by this location.".\ format( b=Style.bright, p=self.ptask.spec, r=Style.reset, ) )
def validate(self): cur_loc_code = current_location_code() try: self._ptask = PTask.get(self.ptask) except PTaskError as e: raise ActionError( "Unable to determine ptask from spec: " + str(self.ptask) ) # just sync the supplied version if self.version: try: ptask_version = _get_ptask_version( self.ptask, self.version, ) except TypeError as e: raise ActionError("Unable to retrieve ptask version: " + str(e)) # make sure we got a version and it's not this location if ptask_version is None: raise ActionError( "No ptask version matching: " + str(self.version) ) # make sure the location is not this location elif ptask_version.location_code == cur_loc_code: raise ActionError("Specified version exists in this location.") versions = [ptask_version] # sync all ptask versions that are not in this location else: versions = [v for v in self.ptask.versions if v.location_code != cur_loc_code] self._versions = versions
def _create_product_area(product_context): ptask = product_context.ptask ptask_area = ptask.area if not ptask_area.exists(): raise PTaskAreaError( "PTask '{pt}' does not exists in location '{l}'.".format( pt=ptask.spec, l=current_location_code(), )) product_context_area = PTaskArea(product_context.spec, validate=False) product_spec = product_context_area.product_spec product_spec_parts = product_spec.split(PTaskSpec.SEPARATOR) directory = "" while product_spec_parts: directory = os.path.join(directory, product_spec_parts.pop(0)) ptask_area.provision(directory) return PTaskArea(product_context.spec)
def _create_ptask_version(self): version = 1 location_code = current_location_code() if self.ptask.versions: if not self.force: raise ActionError("PTask version already exists.") else: try: ptask_version = PTaskVersion.create( self.creator, "In progress...", location_code, ptask_spec=self.ptask.spec, number=version, ) except PTaskVersionError as e: raise ActionError("Failed to create ptask version: " + str(e)) # provision a version directory in the ptask area self.ptask_area.provision( self.ptask_area.dir(version=version, verify=False))
def validate(self): # make sure the ptask evaluates to a real ptask try: self._ptask = PTask.get(self.ptask) except PTaskError: raise ActionError( "Unable to determine ptask from: " + str(self.ptask) ) # for efficiency self._ptask_latest_version = self.ptask.latest_version # make sure the latest version is not this location if self.ptask_latest_version.location_code == current_location_code(): raise ActionError( "Latest version of {b}{p}{r} already owned by this location.".\ format( b=Style.bright, p=self.ptask.spec, r=Style.reset, ) )
def validate(self): try: self._ptask = PTask.get(self.spec) except PTaskError: raise ActionError( "Unable to determine ptask from: " + str(self.spec) ) self._latest_version = self.ptask.latest_version # determine the source version. also store a reference to the latest # version for efficiency if self.source_version: source_version = self.ptask.version(self.source_version) if source_version is None: raise ActionError( "Could not determine source version from: " + \ self.source_version ) self._source_version = source_version else: self._source_version = self.latest_version # latest version must exist in this location. if self.latest_version.location_code != current_location_code(): raise ActionError( "The latest version of this ptask is not owned by this " + "location.\nOwnership of this ptask must first be " + "transferred to this location." ) if self.source_version.ptask != self.ptask: raise ActionError( "Source version's ptask does not match the ptask being " + \ "versioned." )
def _create_ptask_version(self): version = 1 location_code = current_location_code() if self.ptask.versions: if not self.force: raise ActionError("PTask version already exists.") else: try: ptask_version = PTaskVersion.create( self.creator, "In progress...", location_code, ptask_spec=self.ptask.spec, number=version, ) except PTaskVersionError as e: raise ActionError("Failed to create ptask version: " + str(e)) # provision a version directory in the ptask area self.ptask_area.provision( self.ptask_area.dir(version=version, verify=False) )
def _create_product_area(product_context): ptask = product_context.ptask ptask_area = ptask.area if not ptask_area.exists(): raise PTaskAreaError( "PTask '{pt}' does not exists in location '{l}'.".format( pt=ptask.spec, l=current_location_code(), ) ) product_context_area = PTaskArea(product_context.spec, validate=False) product_spec = product_context_area.product_spec product_spec_parts = product_spec.split(PTaskSpec.SEPARATOR) directory = "" while product_spec_parts: directory = os.path.join(directory, product_spec_parts.pop(0)) ptask_area.provision(directory) return PTaskArea(product_context.spec)
def validate(self): # ---- make sure the supplied specs match actual ptasks, # set the properties try: self._source = PTask.get(self.source) except PTaskError: raise ActionError( "Unable to retrieve ptask from source argument: " + \ str(self.source) ) try: self._destination = PTask.get(self.destination) except PTaskError: raise ActinError( "Unable to retrieve ptask from destination argument: " + \ str(self.destination), self, ) self._source_latest_version = self.source.latest_version self._destination_latest_version = self.destination.latest_version # ---- make sure the ptasks are of the same type #if self.source.type != self.destination.type: # raise ActionError( # "Source and destination ptasks must be of the same type. " + \ # self.source.type + " != " + self.destination.type # ) # ---- if the target_type is not ptask, then the calling code has # overridden the target type. make sure the source and destination # types match the target type. target_type = self.__class__.target_type if target_type != "ptask": if self.source.type.lower() != target_type: raise ActionError("Source type must be a " + target_type) elif self.destination.type.lower() != target_type: raise ActionError("Destination type must a " + target_type) # ---- determine the source and destination versions and their locations if self.source_version: try: self._source_version = _get_ptask_version( self.source, self.source_version, ) except TypeError as e: raise ActionError(str(e)) source_location_code = self.source_version.location_code else: source_location_code = self.source_latest_version.location_code if self.destination_version: try: self._destination_version = _get_ptask_version( self.destination, self.destination_version, ) except TypeError as e: raise ActionError(str(e)) destination_location_code = self.destination_version.location_code else: destination_location_code = \ self.destination_latest_version.location_code # one of source or dest must be the current loation, unless the source # and destination are the same ptask. In that case, we'll assume the # goal is to sync the ptask to the current location or to source # directories/versions within the ptask cur_loc_code = current_location_code() if self.source == self.destination: location_override = Location.current() else: if (source_location_code != cur_loc_code and destination_location_code != cur_loc_code): raise ActionError( "One of source or destination must be this location.", ) location_override = None # ---- determine the source and desination paths self._source_path = self._get_path( ptask=self.source, version=self.source_version, latest_version=self.source_latest_version, directory=self.source_directory, ) self._destination_path = self._get_path( ptask=self.destination, version=self.destination_version, latest_version=self.destination_latest_version, directory=self.destination_directory, location_override=location_override, ) # ---- get the includes/excludes based on filter rules (includes, excludes) = self._get_filter_rules(self.destination) # exclude child ptask directories from the source for child in self.source.children: child_dir = os.path.sep + child.name excludes.append(child_dir) self._includes = includes self._excludes = excludes
def setup_cl_args(cls, parser): parser.add_argument("code", nargs="?", default=current_location_code(), help="Print info for the supplied location code.")