def load_labbook_from_directory(self, path: str, author: Optional[GitAuthor] = None ) -> LabBook: """ Load and return a LabBook object from a given path on disk Warning - Deprecated !! Use load_labbook instead Args: path: Full path on disk to LabBook author: Labbook Author of commits Returns: LabBook object """ try: lb = LabBook(config_file=self.config_file) lb._set_root_dir(path) lb._load_gigantum_data() lb._validate_gigantum_data() lb.author = author return lb except Exception as e: logger.error(e) raise InventoryException(e)
def load_labbook(self, username: str, owner: str, labbook_name: str, author: Optional[GitAuthor] = None) -> LabBook: try: lb = LabBook(self.config_file) lbroot = os.path.join(self.inventory_root, username, owner, 'labbooks', labbook_name) lb._set_root_dir(lbroot) lb._load_gigantum_data() lb._validate_gigantum_data() lb.author = author return lb except Exception as e: raise InventoryException( f"Cannot retrieve " f"({username}, {owner}, {labbook_name}): {e}")
def _new_labbook(self, owner: Dict[str, str], name: str, username: str, description: Optional[str] = None, bypass_lfs: bool = False, author: Optional[GitAuthor] = None) -> str: """Method to create a new minimal LabBook instance on disk NOTE: This is the exact same as the old LabBook.new() method. This method is deprecated and will be re-worked in the near future. Args: owner(dict): Owner information. Can be a user or a team/org. name(str): Name of the LabBook username(str): Username of the logged in user. Used to store the LabBook in the proper location. If omitted the owner username is used description(str): A short description of the LabBook bypass_lfs: If True does not use LFS to track input and output dirs. author: Git author for commits Returns: str: Path to the LabBook contents """ if not username: logger.warning( "Using owner username `{}` when making new labbook".format( owner['username'])) username = owner["username"] labbook = LabBook(config_file=self.config_file, author=author) # Build data file contents buildinfo = Configuration(self.config_file).config['build_info'] labbook._data = { "schema": LABBOOK_CURRENT_SCHEMA, "id": uuid.uuid4().hex, "name": name, "description": description or '', "build_info": buildinfo, "created_on": str(datetime.datetime.utcnow().isoformat()) } labbook._validate_gigantum_data() logger.info("Creating new labbook on disk for {}/{}/{} ...".format( username, owner, name)) # lock while creating initial directory with labbook.lock( lock_key= f"new_labbook_lock|{username}|{owner['username']}|{name}"): # Verify or Create user subdirectory # Make sure you expand a user dir string starting_dir = os.path.expanduser( labbook.client_config.config["git"]["working_directory"]) user_dir = os.path.join(starting_dir, username) if not os.path.isdir(user_dir): os.makedirs(user_dir) # Create owner dir - store LabBooks in working dir > logged in user > owner owner_dir = os.path.join(user_dir, owner["username"]) if not os.path.isdir(owner_dir): os.makedirs(owner_dir) # Create `labbooks` subdir in the owner dir owner_dir = os.path.join(owner_dir, "labbooks") else: owner_dir = os.path.join(owner_dir, "labbooks") # Verify name not already in use if os.path.isdir(os.path.join(owner_dir, name)): raise ValueError( f"LabBook `{name}` already exists locally. Choose a new LabBook name" ) # Create LabBook subdirectory new_root_dir = os.path.join(owner_dir, name) os.makedirs(new_root_dir) labbook._set_root_dir(new_root_dir) # Init repository labbook.git.initialize() # Setup LFS if labbook.client_config.config["git"][ "lfs_enabled"] and not bypass_lfs: # Make sure LFS install is setup and rack input and output directories call_subprocess(["git", "lfs", "install"], cwd=new_root_dir) call_subprocess(["git", "lfs", "track", "input/**"], cwd=new_root_dir) call_subprocess(["git", "lfs", "track", "output/**"], cwd=new_root_dir) # Commit .gitattributes file labbook.git.add( os.path.join(labbook.root_dir, ".gitattributes")) labbook.git.commit("Configuring LFS") # Create Directory Structure dirs = [ 'code', 'input', 'output', 'output/untracked', '.gigantum', os.path.join('.gigantum', 'env'), os.path.join('.gigantum', 'env', 'base'), os.path.join('.gigantum', 'env', 'custom'), os.path.join('.gigantum', 'env', 'package_manager'), os.path.join('.gigantum', 'activity'), os.path.join('.gigantum', 'activity', 'log'), os.path.join('.gigantum', 'activity', 'index'), os.path.join('.gigantum', 'activity', 'importance'), ] for d in dirs: p = os.path.join(labbook.root_dir, d, '.gitkeep') os.makedirs(os.path.dirname(p), exist_ok=True) with open(p, 'w') as gk: gk.write( "This file is necessary to keep this directory tracked by Git" " and archivable by compression tools. Do not delete or modify!" ) labbook._save_gigantum_data() # Create .gitignore default file shutil.copyfile( os.path.join(resource_filename('gtmcore', 'labbook'), 'gitignore.default'), os.path.join(labbook.root_dir, ".gitignore")) labbook.git.add_all() # NOTE: this string is used to indicate there are no more activity records to get. Changing the string will # break activity paging. # TODO: Improve method for detecting the first activity record labbook.git.commit(f"Creating new empty LabBook: {name}") return labbook.root_dir