def init(self): try: process = subprocess.Popen( [self.execpath, "init", str(self.filepath)], cwd=self.filepath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() if process.returncode > 0: raise GitExecutionError( __("error", "controller.code.driver.git.init", str(stderr))) except subprocess.CalledProcessError as e: raise GitExecutionError( __("error", "controller.code.driver.git.init", str(e))) try: code_refs_success = self.ensure_code_refs_dir() except Exception as e: raise FileIOError( __("error", "controller.code.driver.git.init.file", str(e))) try: datmo_files_ignored_success = self.ensure_datmo_files_ignored() except Exception as e: raise FileIOError( __("error", "controller.code.driver.git.init.file", str(e))) return code_refs_success and datmo_files_ignored_success
def _config_setup(self, incoming_dictionary, create_dict): """ Fills in snapshot config by having one of the following: 1. config = JSON object 2. config_filepath = some location where a json file exists 3. config_filename = just the file name Parameters ---------- incoming_dictionary : dict dictionary for the create function defined above create_dict : dict dictionary for creating the Snapshot entity Raises ------ FileIOError """ if "config" in incoming_dictionary: create_dict['config'] = incoming_dictionary['config'] elif "config_filepath" in incoming_dictionary: if not os.path.isfile(incoming_dictionary['config_filepath']): raise FileIOError( __("error", "controller.snapshot.create.file_config")) # If path exists transform file to config dict config_json_driver = JSONStore( incoming_dictionary['config_filepath']) create_dict['config'] = config_json_driver.to_dict() elif "config_filename" in incoming_dictionary: config_filename = incoming_dictionary['config_filename'] create_dict['config'] = self._find_in_filecollection( config_filename, create_dict['file_collection_id']) else: config_filename = "config.json" create_dict['config'] = self._find_in_filecollection( config_filename, create_dict['file_collection_id'])
def delete_ref(self, commit_id): self.ensure_code_refs_dir() code_ref_path = os.path.join(self.filepath, ".git/refs/datmo/", commit_id) if not self.exists_ref(commit_id): raise FileIOError( __("error", "controller.code.driver.git.delete_ref")) os.remove(code_ref_path) return True
def ensure_code_refs_dir(self): dir = ".git/refs/datmo" try: if not os.path.isdir(os.path.join(self.filepath, dir)): os.makedirs(os.path.join(self.filepath, dir)) except Exception as e: raise FileIOError( __("error", "controller.code.driver.git.ensure_code_refs_dir", str(e))) return True
def delete_code_refs_dir(self): dir = ".git/refs/datmo" dir_path = os.path.join(self.filepath, dir) try: if os.path.isdir(dir_path): shutil.rmtree(dir_path) except Exception as e: raise FileIOError( __("error", "controller.code.driver.git.delete_code_refs_dir", str(e))) return True
def ensure_datmo_files_ignored(self): exclude_file = os.path.join(self.filepath, ".git/info/exclude") try: if not self.exists_datmo_files_ignored(): with open(exclude_file, "ab") as f: f.write(to_bytes("\n.datmo/*\n")) except Exception as e: raise FileIOError( __("error", "controller.code.driver.git.ensure_code_refs_dir", str(e))) return True
def exists_datmo_files_ignored(self): exclude_file = os.path.join(self.filepath, ".git/info/exclude") try: if ".datmo" not in open(exclude_file, "r").read(): return False else: return True except Exception as e: raise FileIOError( __("error", "controller.code.driver.git.ensure_code_refs_dir", str(e)))
def to_dict(self): output_dict = dict() # reading json file if os.path.exists(self.filepath): with open(self.filepath) as data_file: meta_data_string = data_file.read() if not meta_data_string: return {} try: output_dict = json.loads(meta_data_string) output_dict = yaml.safe_load(json.dumps(output_dict)) except Exception as err: raise FileIOError(err) return output_dict
def init(self): try: # Ensure the Hidden Datmo file structure exists self.ensure_hidden_datmo_file_structure() # Ensure the empty collection exists if not os.path.isdir( os.path.join(self.datmo_directory, "collections", "d41d8cd98f00b204e9800998ecf8427e")): self.create(os.path.join(self.datmo_directory, "collections", "d41d8cd98f00b204e9800998ecf8427e"), directory=True) except Exception as e: raise FileIOError( __("error", "controller.file.driver.local.init", str(e))) return True
def delete_ref(self, commit_id): """Removes the commit hash file, but not the file references Raises ------ CodeNotInitialized error if not initialized (must initialize first) """ if not self.is_initialized: raise CodeNotInitialized() if not self.exists_ref(commit_id): raise FileIOError( __("error", "controller.code.driver.file.delete_ref")) commit_filepath = os.path.join(self._code_filepath, commit_id) os.remove(commit_filepath) return True
def checkout_ref(self, commit_id): """Checkout to specific commit Raises ------ CodeNotInitialized error if not initialized (must initialize first) UnstagedChanges error if not there exists unstaged changes in code """ if not self.is_initialized: raise CodeNotInitialized() if not self.exists_ref(commit_id): raise FileIOError( __("error", "controller.code.driver.file.checkout_ref")) # Check if unstaged changes exist if self._has_unstaged_changes(): raise UnstagedChanges() # Check if commit given is same as current tracked_filepaths = self._get_tracked_files() if self._calculate_commit_hash(tracked_filepaths) == commit_id: return True # Remove all tracked files from repository for tracked_filepath in self._get_tracked_files(): absolute_filepath = os.path.join(self.filepath, tracked_filepath) os.remove(absolute_filepath) # Add in files from the commit commit_filepath = os.path.join(self._code_filepath, commit_id) with open(commit_filepath, "r") as f: for line in f: tracked_filepath, filehash = line.rstrip().split(",") source_absolute_filepath = os.path.join( self._code_filepath, tracked_filepath, filehash) destination_absolute_filepath = os.path.join( self.filepath, tracked_filepath) shutil.copy2(source_absolute_filepath, destination_absolute_filepath) return True