def getVersions (db = None, doc_id = "", vtype = "review"): """ This function return all versions in a dictionnary of a particular asset. :param db: the database :type db: Database :param doc_id: The asset code. :type doc_id: str :param vtype: Version type *trial/stock* :type vtype: str :returns: dict -- a dictionary with all versions of the asset, the key is a string with the version number without padding. **Example:** >>> db = utils.getDb() >>> getVersions ( db = db, doc_id = "bls_chr_belanus_mod_main" ) >>> {'1': {'files': ['bls_chr_belanus_mod_main.mb'], 'path': '/homeworks/projects/bls/chr/belanus/mod/main/001', 'created': '2013 Mar 08 21:16:34', 'description': 'names cleaned\nnormal softened', 'creator': 'pixo'}, '3': {'files': ['bls_chr_belanus_mod_main.mb'], 'path': '/homeworks/projects/bls/chr/belanus/mod/main/003', 'created': '2013 Mar 08 23:13:54', 'description': 'test export gproject etc', 'creator': 'pixo'}, '2': {'files': ['bls_chr_belanus_mod_main.mb'] ... and so ... } """ if utils.checkVersionType (vtype) : return False # If db is not provided get the current project DB if db==None : db=utils.getDb() # Get Versions from document versions=db [ doc_id ][ vtype ] return versions
def pushFile (db = None, doc_id = False, path = list (), description = "", rename = True, vtype = "review"): """ This function copy the desired file from local workspace to repository. :param db: the database :type db: Database :param doc_id: The asset code :type doc_id: str :param path: The list of files to push :type path: str/list of str :param description: This is the description of the push :type description: str :param rename: Rename the file (default True) :type rename: bool -- if True rename the file(s) :param vtype: Version type *trial/stock* :type vtype: str :returns: str -- Return the directory of the pushed files **Example:** >>> db = utils.getDb() >>> push ( db = db, doc_id = "bls_chr_belanus_mod_main", >>> path = "/homeworks/users/jdoe/projects/bls/chr/belanus/mod/main/file_to_push.mb" ) """ # Make sure vtype exists if utils.checkVersionType (vtype) : return False # Check if DB is provided else get the project DB if (not db) or db=="" : db=utils.getDb() # Check if 'path' is a string if type (path)==str: # If 'path' is a string then append it in a list path=list ([ path ]) # If doc_id not provided if not doc_id: # Get doc_id from path doc_id=getIdFromPath (path = path[0]) # Return the directory of the pushed files result=push (db = db , doc_id = doc_id , path = path , description = description, progressbar = False, msgbar = False, rename = rename, vtype = vtype) return result
def getPathFromId (doc_id = "", local = False, vtype = "review"): """ This function return a path based from a provided 'doc_id'. :param doc_id: The asset code. :type doc_id: str :param local: If true return the user local repository path :type local: bool :param vtype: Version type *trial/stock* :type vtype: str :returns: str -- The asset or task path. **Example:** >>> #Repository >>> getPathFromId ( doc_id = "prod_chr_mickey_mod_a", local = False ) >>> '/homeworks/projects/prod/chr/mickey/mod/a' >>> >>> #Local >>> getPathFromId ( doc_id = "prod_chr_mickey_mod_a", local = True ) >>> '/homeworks/users/jdoe/projects/prod/chr/mickey/mod/a' """ if utils.checkVersionType (vtype) : return False # Get the last part of the path path=doc_id.replace ("_", os.sep) # Get the first part of the path if local : # If true return the local project root root=os.getenv ("HK_USER_REPO") else: # If false return the repository project root root=os.getenv ("HK_REPO") # Check the root path value if (not root) or root=="" : raise RepositoryError ("getPathFromId(): incorrect value for root path ") # Full path path=os.path.join (root, path) if not local : path=os.path.join (path, vtype) return path
def getVersionPath (doc_id = "", version = "last", db = None, vtype = "review"): """ This function return the asset path of a particular version. :param db: the database :type db: Database :param doc_id: The asset code. :type doc_id: str :param version: The asset code. :type version: str/float/int -- 'last' or 1,2,3,4 etc :param vtype: Version type *trial/stock* :type vtype: str :returns: str -- the asset directory **Example:** >>> db = utils.getDb() >>> getVersionPath ( db = db, doc_id = "bls_chr_belanus_mod_main", version = "last" ) >>> '/homeworks/projects/bls/chr/belanus/mod/main/008' """ # old getAssetPath if utils.checkVersionType (vtype) : return False # Get asset versions versions=getVersions (db = db, doc_id = doc_id, vtype = vtype) num=None # If the queried version is the latest if version=="last" : num=int (len (versions)) else: num=int (version) # Get version num attr version=versions [ str (num) ] # Get the version path path=version ["path"] return path
def getLocalVersionPath (doc_id = "", version = 1, vtype = "review"): """ This function return the path of a particular asset version into the user directory . :param doc_id: The asset code. :type doc_id: str :param version: The asset code. :type version: str/float/int -- 1,2,3,4 etc :param vtype: Version type *trial/stock* :type vtype: str :returns: str -- the asset directory path **Example:** >>> getLocalVersionPath ( doc_id = "prod_chr_mickey_mod_a", version = 2 ) >>> '/homeworks/users/jdoe/projects/prod/chr/mickey/mod/a/prod_chr_mickey_main_a.v002.base' """ # old getAssetLocalPath # Make sure vtype exists if utils.checkVersionType (vtype) : return False # Make sure to get the right type for concatenation version=int (version) # Get asset local path fdir=getPathFromId (doc_id = doc_id, local = True, vtype = vtype) name="%s.from_v%03d.base"%(vtype, version) dst=os.path.join (fdir, name) # Return a path that doesn't exist count=1 while os.path.exists (dst) : dst=os.path.join (fdir, name+str (count)) count+=1 return dst
def createWorkspace (doc_id = "", vtype = "review"): """ This function create the asset user path of a particular asset. :param doc_id: The asset code. :type doc_id: str :param vtype: Version type *trial/stock* :type vtype: str :returns: str/bool -- Return the workspace path if path is created else False . **Example:** >>> createWorkspace ( doc_id = "prod_chr_mickey_mod_a" ) >>> '/homeworks/users/jdoe/projects/prod/chr/mickey/mod/a' """ # Make sure vtype exists if utils.checkVersionType (vtype) : return False # Get the local asset path to create from asset id path=getPathFromId (doc_id = doc_id, local = True, vtype = vtype) # Check if the path exist if os.path.exists (path) : print ("createWorkspace(): %s already exist"%path) return False # Create the asset path with the proper permission os.makedirs (path, 0775) # Check if the path was created if not os.path.exists (path) : raise RepositoryError ("createWorkspace(): cannot create directory %s"%path) print ("createWorkspace(): %s created"%path) return path
def pushDir (db = "", doc_id = "", path = list(), description = "", vtype = "review"): """ This function copy the desired file from local workspace to repository. :param db: the database :type db: Database :param doc_id: The asset code :type doc_id: str :param path: directory or The list of directories to push :type path: str/list of str :param description: This is the description of the push :type description: str :param vtype: Version type *trial/stock* :type vtype: str :returns: str -- Return the published directory **Example:** >>> db = utils.getDb() >>> pushDir ( db = db, doc_id = "bls_chr_belanus_mod_main", >>> path = "/homeworks/users/jdoe/projects/bls/chr/belanus/tex/main/directory_to_push", >>> description = "This is a publish" ) """ # Make sure vtype exists if utils.checkVersionType (vtype) : return False # check if the source file exists in the repository if not os.path.exists (path) : print "pushDir(): %s doesn't exist"%path return False # Get root destination directory to push files dst_dir=getPathFromId (doc_id = doc_id, vtype = vtype) # Get temporary destination directory to push files tmp_dir=os.path.join (dst_dir, utils.hashTime ()) os.makedirs (tmp_dir) # Copy all the files in the destination directory files_attr=list () file_list=os.listdir (path) for src in file_list : # file space in case we need to publish directories """ path_src=os.path.join (path, src) dst=os.path.join (tmp_dir, src) if os.path.isfile (path_src): print "pushDir(): copying file %s "%src shutil.copy (path_src, dst) elif os.path.isdir (path_src): print "pushDir(): copying directory %s "%src shutil.copytree (path_src, dst) # Store the files names in a list to avoid to call the database for each source file files_attr.append (src) # Get latest version number because somebody may push a new version during the process doc=db [ doc_id ] ver_attr=getVersions (db = db, doc_id = doc_id, vtype = vtype) ver=len (ver_attr)+1 path_attr=os.path.join (dst_dir, "%03d"%ver) repo=os.path.expandvars (path_attr) # Rename the temp dir os.rename (tmp_dir, repo) os.chmod (repo, 0555) # Create the new version data for the "versions" document's attribute fileinfo={ "creator" : os.getenv ("USER"), "created" : time.time(), "description" : description , "path" : path_attr , "files" : files_attr } # Append the data into the document version attribute copy ver_attr [ ver ]=fileinfo # Replace the original "versions" attribute by our modified version doc [ vtype ]=ver_attr # Push the info into the db db [ doc_id ]=doc # print published file for the user for fil in files_attr: print os.path.join (repo , fil) # Return the published directory return repo
def push (db = "", doc_id = "", path = list(), description = "", progressbar = False, msgbar = False, rename = True, vtype = "review"): """ This function copy the desired file from local workspace to repository. :param db: the database :type db: Database :param doc_id: The asset code :type doc_id: str :param path: The list of files to push :type path: str/list of str :param description: This is the description of the push :type description: str :param progressbar: The pyside progress bar :type progressbar: PySide progressbar :param msg: The pyside message bar :type progressbar: PySide messagebar :param rename: Rename the file (default True) :type rename: bool -- if True rename the file(s) :param vtype: Version type *trial/stock* :type vtype: str :returns: str -- Return the published directory **Example:** >>> db = utils.getDb() >>> push ( db = db, doc_id = "bls_chr_belanus_mod_main", >>> path = "/homeworks/users/jdoe/projects/bls/chr/belanus/mod/main/file_to_push.mb", >>> description = "this is a modeling version of belanus" ) """ # Make sure vtype exists if utils.checkVersionType (vtype) : return False # TODO: Check push for auto screenshot publish # Check the path type is a list if type (path)==str : path=list ([ path ]) # check if the source file exists in the repository file_list=list () for src in path : if os.path.exists (src) : file_list.append (src) else: print "Warning: %s doesn't exist"%src # Get root destination directory to push files dst_dir=getPathFromId (doc_id, vtype = vtype) # Get temporary destination directory to push files tmp_dir=os.path.join (dst_dir, utils.hashTime ()) # Create temporary directory if not os.path.exists (tmp_dir): os.makedirs (tmp_dir) # Copy all the files in the destination directory progress_value=0 progress_step=100.0/len (file_list) files_attr=list () wspace=getPathFromId (doc_id = doc_id, local = True, vtype = vtype) # Iterate over all the provided source files for src in file_list : # Get file dir src_dir=os.path.dirname (src) # file space in case we need to publish directories file_space=src_dir.replace (wspace, "") file_name=os.path.join (file_space, os.path.basename (src)) # Get extension(s) ,UDIMs and frames are commonly separated with this char file_ext="."+file_name.split (".")[-1] # Get screenshot file screenshot=False screenshot_exts=[ ".jpg", ".jpeg", ".png" ] screenshot_ext="" for ext in screenshot_exts : screenpath=file_name+ext screenpath=os.path.join (src_dir, screenpath) if os.path.exists (screenpath) : screenshot_ext=ext screenshot=screenpath break else : screenpath=screenpath.replace ("."+file_ext, ext) if screenpath!=file_name : if os.path.exists (screenpath) : screenshot_ext=ext screenshot=screenpath # Creating the full filename if rename: dst_file=doc_id+file_ext dst_screenshot=doc_id+screenshot_ext else: dst_file=file_name dst_screenshot=screenshot if dst_file [0]==os.sep : dst_file=dst_file [1:] tmp_file=os.path.join (tmp_dir, dst_file) # Store the files names in a list to avoid to call the database for each source file files_attr.append (dst_file) # Copy files to temporary directory shutil.copy (src, tmp_file) # Copy screenshot to temporary directory if screenshot : if dst_screenshot [0]==os.sep : dst_screenshot=dst_screenshot [1:] tmp_screenshot=os.path.join (tmp_dir, dst_screenshot) shutil.copy (screenshot, tmp_screenshot) # Set progress value progress_value+=progress_step if progressbar : progressbar.setProperty ("value", progress_value) else : print (str (progress_value)+"%") if msgbar : msgbar (dst_file) # Get latest version doc=db [ doc_id ] ver_attr=getVersions (db, doc_id, vtype = vtype) ver=len (ver_attr)+1 path_attr=os.path.join (dst_dir, "%03d"%ver) repo=os.path.expandvars (path_attr) # Rename the temp dir os.rename (tmp_dir, repo) # TODO:Replace os.system ( "chmod -R 555 %s" % repo ) by python function os.system ("chmod -R 555 %s"%repo) # Create the new version data for the "versions" document's attribute fileinfo={ "creator" : os.getenv ("USER"), "created" : time.time(), "description" : description , "path" : path_attr , "files" : files_attr, "release" : list() } # Check status status=doc ["status"] if status["tec"]=="ns": status["tec"]="wip" doc ["status"]=status # Append the data into the document version attribute copy ver_attr [ ver ]=fileinfo # Replace the original "versions" attribute by our modified version doc [ vtype ]=ver_attr # Push the info into the db db [ doc_id ]=doc # print published file for the user for fil in files_attr: print os.path.join (repo , fil) # Return the published directory return repo
def pull (db = None, doc_id = "", version = "last", extension = False, progressbar = False, msgbar = False, vtype = "review"): """ This function copy the desired file from repository to local workspace. :param db: the database :type db: Database :param doc_id: The asset code :type doc_id: str :param version: The asset version :type version: int/str :param extension: The file extension :type extension: int/str :param progressbar: The pyside progress bar :type progressbar: PySide progressbar :param msg: The pyside message bar :type progressbar: PySide messagebar :param vtype: Version type *trial/stock* :type vtype: str :returns: list -- a list of the pulled file. **Example:** >>> db = utils.getDb() >>> pull ( db = db, doc_id = "bls_chr_belanus_mod_main", version = 2 ) >>> ['/homeworks/users/jdoe/projects/bls/chr/belanus/mod/main/bls_chr_belanus_mod_main.v002.base/bls_chr_belanus_mod_main.jpg', >>> '/homeworks/users/jdoe/projects/bls/chr/belanus/mod/main/bls_chr_belanus_mod_main.v002.base/bls_chr_belanus_mod_main.mb'] """ # Make sure vtype exists if utils.checkVersionType (vtype) : return False def echoMsg (msg = "", msgbar = None): print msg if msgbar : msgbar (msg) # If db is not provided get the current project DB if db==None : db=utils.getDb() # Check id is respecting the homeworks naming convention docsplit=doc_id.split("_") if len (docsplit)<5: echoMsg (msg = "pull(): Wrong asset id", msgbar = msgbar) return False # Get asset repository and local asset path src=getVersionPath (doc_id = doc_id, version = version, db = db, vtype = vtype) dst=getLocalVersionPath (doc_id = doc_id, version = version, vtype = vtype) # Add/Check files to pull lsdir=list () for root, subFolders, files in os.walk (src): for fil in files: curfile=os.path.join (root, fil) if extension and extension!="" : if os.path.splitext (curfile)[-1]==extension : lsdir.append (curfile) else : lsdir.append (curfile) # Prepare the progress bar if progressbar : progress_value=0 progress_step=100.0/len(lsdir) if len(lsdir)!=0 else 1 # Check there is something to pull if len (lsdir)>0 : os.makedirs (dst, 0775) if not os.path.exists (dst): raise RepositoryError ("Pull(): cannot create %s "%dst) # Pull lsdir file pulled=list() for fil in lsdir: fulldst=fil.replace (src, dst) shutil.copyfile (fil, fulldst) pulled.append (fulldst) # Echo message msg="core.Pull(): %s"%fulldst echoMsg (msg = msg, msgbar = msgbar) if progressbar : progress_value+=progress_step progressbar.setProperty ("value", progress_value) return pulled
def texturePush (db = None, doc_id = "", path = "", description = "", progressbar = False, msgbar = False, rename = False, vtype = "review") : """ This function copy the desired file from local workspace to repository. :param db: the database :type db: Database :param doc_id: The asset code :type doc_id: str :param path: The path that contains the textures :type path: str :param description: This is the description of the push :type description: str :param progressbar: The pyside progress bar :type progressbar: PySide progressbar :param msg: The pyside message bar :type msg: PySide messagebar :param rename: Rename the file (default True) :type rename: bool -- if True rename the file(s) :param vtype: Version type *trial/stock* :type vtype: str :returns: bool/list -- Return False if fail to push the textures else the list of success textures **Example:** >>> db = utils.getDb() >>> texturePush ( db = db, doc_id = "bls_chr_belanus_mod_main", >>> path = "/homeworks/users/jdoe/projects/bls/chr/belanus/mod/main/file_to_push.mb", >>> description = "this is a texture version of belanus" ) """ # Make sure vtype exists if utils.checkVersionType (vtype) : return False # List the directory # TODO: Use glob to get the right textures if not os.path.isdir (path): return False lsdir=os.listdir (path) files=list () # Iterate over files contained in the directory 'path' # and check if the file is a mra or psd for fil in lsdir: # Get file extension ext=os.path.splitext (fil) [-1] if ext in (".mra", ".psd"): # If current file is a Mari or photoshop file # aka extraordinary textures files if not (fil.find (doc_id)==0) : print fil, "should begin with %s"%doc_id return False else: # else add the file for texture check files.append (os.path.join (path, fil)) # Check the none extraordinary textures files texCheck=textureCheck (doc_id, files) # If every textures success the check if len (texCheck)==0 : # Push the directory containing the textures pushed=pushDir (db = db, doc_id = doc_id, path = path, description = description, vtype = vtype) return pushed else : for tex in texCheck : print ("texturePush(): %s is wrong"%tex) simptex="%s_%s_%s.%s.%s"%(doc_id, "<variation>", "<type>", "<udim>", "tif") animtex="%s_%s_%s.%s.%s.%s"%(doc_id, "<variation>", "<type>", "<udim>", "<frame>", "tif") print "texturePush(): expect %s or %s "%(simptex , animtex) return False