def list_products(self): """ return the work's product's type list :return: a string list """ return fu.read_data(self.data_file)["outputs"]
def read(self): """ read the work data from user work space if the work doesn't exists in user work space, raise a pulse error :return: the updated work """ if not os.path.exists(self.data_file): raise PulseError("work does not exists : " + self.directory) work_data = fu.read_data(self.data_file) self.version = work_data["version"] return self
def trash_product(self, product_type): """ move the specified product to the trash directory raise an error if the product is used by a resource or another product :param product_type: string """ self._check_exists_in_user_workspace() if product_type not in self.list_products(): raise PulseError("product does not exists : " + product_type) product = WorkProduct(self, product_type) if not fu.test_path_write_access(product.directory): raise PulseError("can't move folder " + product.directory) users = product.get_product_users() if users: raise PulseError( "work can't be trashed if its product is used : " + users[0]) # unregister from products for input_product in product.get_inputs(): if os.path.exists(input_product.directory): input_product.remove_product_user(self.directory) # create the trash work directory trash_directory = self._get_trash_directory() if not os.path.exists(trash_directory): os.makedirs(trash_directory) # move folder shutil.move(product.directory, os.path.join(trash_directory, "PRODUCTS", product_type)) # remove the product from work outputs data_dict = fu.read_data(self.data_file) data_dict["outputs"].remove(product_type) fu.write_data(self.data_file, data_dict) # remove the products directory if it's empty products_directory = self.get_products_directory() if not os.listdir(products_directory): shutil.rmtree(products_directory) # remove the product from products local data os.remove(product.product_users_file)
def status(self): """ return the work files changes since last commit. Based on the files modification date time :return: a list a tuple with the filepath and the edit type (edited, removed, added) """ product_directories = [] for product_name in self.list_products(): product_directories.append( os.path.join(self.get_products_directory(), product_name)) diff = fu.compare_directory_content( self._get_work_files(), fu.read_data(self.data_file)["work_files"]) for product_name in self.list_products(): product_directory = os.path.join(self.get_products_directory(), product_name) for root, subdirectories, files in os.walk(product_directory): for f in files: filepath = os.path.join(root, f) relative_path = filepath[len(product_directory):] diff["product-" + product_name + relative_path] = "added" return diff
def create_product(self, product_type): """ create a new product for the work :param product_type: string :return: the new work product object """ self._check_exists_in_user_workspace() outputs = self.list_products() if product_type in outputs: raise PulseError("product already exists : " + product_type) work_product = WorkProduct(self, product_type) os.makedirs(work_product.directory) pulse_filepath = os.path.join(self.get_products_directory(), pulse_filename) if not os.path.exists(pulse_filepath): open(pulse_filepath, 'a').close() # update work pipe file with the new output outputs.append(product_type) data_dict = fu.read_data(self.data_file) data_dict["outputs"] = outputs fu.write_data(self.data_file, data_dict) return work_product