def __init__(self, projpath): """Initializes a Project containing the project found in the specified directory or creates a new project if one doesn't exist. projpath: str Path to the project's directory. """ self.path = expand_path(projpath) modeldir = os.path.join(self.path, 'model') self.activate() if os.path.isdir(projpath): # locate file containing state, create it if it doesn't exist statefile = os.path.join(projpath, '_project_state') if os.path.exists(statefile): try: with open(statefile, 'r') as f: self.__dict__ = pickle.load(f) except Exception, e: print 'Unable to restore project state:', e self.top = Assembly() else: self.top = set_as_top(Assembly()) self.path = expand_path( projpath) # set again in case loading project state changed it
def __init__(self, projpath): """Initializes a Project containing the project found in the specified directory or creates a new project if one doesn't exist. projpath: str Path to the project's directory """ self.path = expand_path(projpath) modeldir = os.path.join(self.path, 'model') self.activate() if os.path.isdir(projpath): # I don't think model dir and state should be manadatory # e.g. when creating a project from an existing non-Project directory # if not _is_valid_project_dir(projpath): # raise RuntimeError("Directory '%s' is not a valid OpenMDAO project directory") # locate file containing state, create it if it doesn't exist statefile = os.path.join(projpath, '_project_state') if os.path.exists(statefile): try: with open(statefile, 'r') as f: self.__dict__ = pickle.load(f) except Exception, e: print 'Unable to restore project state:',e self.top = Assembly() else: self.top = set_as_top(Assembly()) self.path = expand_path(projpath) # set again in case loading project state changed it
def project_from_archive(archive_name, proj_name=None, dest_dir=None, create=True, overwrite=False): """Expand the given project archive file in the specified destination directory and return a Project object that points to the newly expanded project. archive_name: str Path to the project archive to be expanded. proj_name: str (optional) Name of the new project. Defaults to the name of the project contained in the name of the archive. dest_dir: str (optional) Directory where the project directory for the expanded archive will reside. Defaults to the directory where the archive is located. create: bool (optional) If True, create and return a Project object. Otherwise just unpack the project directory. """ archive_name = expand_path(archive_name) if dest_dir is None: dest_dir = os.path.dirname(archive_name) else: dest_dir = expand_path(dest_dir) if proj_name is None: proj_name = parse_archive_name(archive_name) projpath = os.path.join(dest_dir, proj_name) if not overwrite and os.path.exists(projpath): raise RuntimeError("Directory '%s' already exists" % projpath) if not os.path.exists(projpath): os.mkdir(projpath) if os.path.getsize(archive_name) > 0: try: f = open(archive_name, 'rb') tf = tarfile.open(fileobj=f, mode='r') tf.extractall(projpath) except Exception as err: logger.error(str(err)) print "Error expanding project archive:", err finally: tf.close() if create: return Project(projpath)
def project_from_archive(archive_name, proj_name=None, dest_dir=None, create=True, overwrite=False): """Expand the given project archive file in the specified destination directory and return a Project object that points to the newly expanded project. archive_name: str Path to the project archive to be expanded. proj_name: str (optional) Name of the new project. Defaults to the name of the project contained in the name of the archive. dest_dir: str (optional) Directory where the project directory for the expanded archive will reside. Defaults to the directory where the archive is located. create: bool (optional) If True, create and return a Project object. Otherwise, just unpack the project directory. """ archive_name = expand_path(archive_name) if dest_dir is None: dest_dir = os.path.dirname(archive_name) else: dest_dir = expand_path(dest_dir) if proj_name is None: proj_name = parse_archive_name(archive_name) projpath = os.path.join(dest_dir, proj_name) if not overwrite and os.path.exists(projpath): raise RuntimeError("Directory '%s' already exists" % projpath) if not os.path.exists(projpath): os.mkdir(projpath) if os.path.getsize(archive_name) > 0: try: f = open(archive_name, 'rb') tf = tarfile.open(fileobj=f, mode='r') tf.extractall(projpath) except Exception as err: logger.error(str(err)) print "Error expanding project archive:", err finally: tf.close() if create: return Project(projpath)
def export(self, projname=None, destdir='.'): """Creates an archive of the current project for export. projname: str (optional) The name that the project in the archive will have. Defaults to the current project name. destdir: str (optional) The directory where the project archive will be placed. Defaults to the current directory. """ ddir = expand_path(destdir) if projname is None: projname = self.name projpath = os.path.join(ddir, projname) if ddir.startswith(self.path): # the project contains the dest directory... bad raise RuntimeError("Destination directory for export (%s) is within project directory (%s)" % (ddir, self.path)) self.save() startdir = os.getcwd() os.chdir(self.path) try: try: tf = tarfile.open(os.path.join(ddir,projname+PROJ_FILE_EXT), mode='w:gz') for entry in os.listdir(self.path): tf.add(entry) finally: tf.close() finally: os.chdir(startdir)
def __init__(self, projpath): """Initializes a Project containing the project found in the specified directory or creates a new project if one doesn't exist. projpath: str Path to the project's directory. """ self._recorded_cmds = [] self._cmds_to_save = [] self.path = expand_path(projpath) self._model_globals = {} self.macrodir = os.path.join(self.path, '_macros') self.macro = 'default' if not os.path.isdir(self.macrodir): os.makedirs(self.macrodir) settings = os.path.join(self.path, '_settings.cfg') if not os.path.isfile(settings): self._create_config() self.config = SafeConfigParser() self.config.optionxform = str # Preserve case. files = self.config.read(settings) if not files: logger.error("Failed to read project config file")
def project_from_archive(archive_name, proj_name=None, dest_dir=None): """Expand the given project archive file in the specified destination directory and return a Project object that points to the newly expanded project. archive_name: str Path to the project archive to be expanded. proj_name: str (optional) Name of the new project. Defaults to the name of the project contained in the name of the archive. dest_dir: str (optional) Directory where the project directory for the expanded archive will reside. Defaults to the directory where the archive is located. """ archive_name = expand_path(archive_name) if dest_dir is None: dest_dir = os.path.dirname(archive_name) else: dest_dir = expand_path(dest_dir) if proj_name is None: proj_name = _parse_archive_name(archive_name) projpath = os.path.join(dest_dir, proj_name) if os.path.exists(projpath): raise RuntimeError("Directory '%s' already exists" % projpath) os.mkdir(projpath) startdir = os.getcwd() if os.path.getsize(archive_name) > 0: try: f = open(archive_name, 'rb') tf = tarfile.open(fileobj=f, mode='r') tf.extractall(projpath) except Exception, err: print "Error expanding project archive:", err finally:
def project_from_archive(archive_name, proj_name=None, dest_dir=None): """Expand the given project archive file in the specified destination directory and return a Project object that points to the newly expanded project. archive_name: str Path to the project archive to be expanded proj_name: str (optional) Name of the new project. Defaults to the name of the project contained in the name of the archive. dest_dir: str (optional) Directory where the project directory for the expanded archive will reside. Defaults to the directory where the archive is located. """ archive_name = expand_path(archive_name) if dest_dir is None: dest_dir = os.path.dirname(archive_name) else: dest_dir = expand_path(dest_dir) if proj_name is None: proj_name = _parse_archive_name(archive_name) projpath = os.path.join(dest_dir, proj_name) if os.path.exists(projpath): raise RuntimeError("Directory '%s' already exists" % projpath) os.mkdir(projpath) startdir = os.getcwd() if os.path.getsize(archive_name) > 0: tf = tarfile.open(archive_name) try: tf.extractall(projpath) finally: tf.close() proj = Project(projpath) return proj
def __init__(self, projpath): """Initializes a Project containing the project found in the specified directory or creates a new project if one doesn't exist. projpath: str Path to the project's directory. """ self.path = expand_path(projpath) modeldir = os.path.join(self.path, 'model') self.activate() if os.path.isdir(projpath): # locate file containing state, create it if it doesn't exist statefile = os.path.join(projpath, '_project_state') if os.path.exists(statefile): try: with open(statefile, 'r') as f: self.__dict__ = pickle.load(f) except Exception, e: print 'Unable to restore project state:',e self.top = Assembly() else: self.top = set_as_top(Assembly()) self.path = expand_path(projpath) # set again in case loading project state changed it
def export(self, projname=None, destdir='.'): """Creates an archive of the current project for export. projname: str (optional) The name that the project in the archive will have. Defaults to the current project name. destdir: str (optional) The directory where the project archive will be placed. Defaults to the current directory. """ export_repo = self.config.getboolean("preferences", "export_repo") excludes = ['.git', '.bzr', '.hg', '.projrepo'] ddir = expand_path(destdir) if projname is None: projname = self.name if os.path.basename(ddir) not in excludes and ddir.startswith( self.path): # the project contains the dest directory... bad raise RuntimeError( "Destination directory for export (%s) is within" " project directory (%s)" % (ddir, self.path)) startdir = os.getcwd() os.chdir(self.path) try: try: fname = os.path.join(ddir, projname + PROJ_FILE_EXT) f = open(fname, 'wb') tf = tarfile.open(fileobj=f, mode='w:gz') for entry in os.listdir(self.path): if export_repo or entry not in excludes: tf.add(entry) except Exception as err: print "Error creating project archive:", err fname = None finally: tf.close() finally: os.chdir(startdir) return fname
def export(self, projname=None, destdir='.'): """Creates an archive of the current project for export. projname: str (optional) The name that the project in the archive will have. Defaults to the current project name. destdir: str (optional) The directory where the project archive will be placed. Defaults to the current directory. """ export_repo = self.config.getboolean("preferences", "export_repo") excludes = ['.git', '.bzr', '.hg', '.projrepo'] ddir = expand_path(destdir) if projname is None: projname = self.name if os.path.basename(ddir) not in excludes and ddir.startswith(self.path): # the project contains the dest directory... bad raise RuntimeError("Destination directory for export (%s) is within" " project directory (%s)" % (ddir, self.path)) startdir = os.getcwd() os.chdir(self.path) try: try: fname = os.path.join(ddir, projname + PROJ_FILE_EXT) f = open(fname, 'wb') tf = tarfile.open(fileobj=f, mode='w:gz') for entry in os.listdir(self.path): if export_repo or entry not in excludes: tf.add(entry) except Exception as err: print "Error creating project archive:", err fname = None finally: tf.close() finally: os.chdir(startdir) return fname
def export(self, projname=None, destdir='.'): """Creates an archive of the current project for export. projname: str (optional) The name that the project in the archive will have. Defaults to the current project name. destdir: str (optional) The directory where the project archive will be placed. Defaults to the current directory. """ ddir = expand_path(destdir) if projname is None: projname = self.name projpath = os.path.join(ddir, projname) if ddir.startswith( self.path): # the project contains the dest directory... bad raise RuntimeError( "Destination directory for export (%s) is within project directory (%s)" % (ddir, self.path)) self.save() startdir = os.getcwd() os.chdir(self.path) try: try: fname = os.path.join(ddir, projname + PROJ_FILE_EXT) f = open(fname, 'wb') tf = tarfile.open(fileobj=f, mode='w:gz') for entry in os.listdir(self.path): tf.add(entry) except Exception, err: print "Error creating project archive:", err finally: tf.close()
def __init__(self, projpath, gui=True, globals_dict=None): """Initializes a Project containing the project found in the specified directory or creates a new project if 'create' is True and one doesn't exist. projpath: str Path to the project's directory. gui: bool (optional) (default=True) GUI is running. This determines how the project is loaded. globals_dict: dict (optional) If this is not None, it will be populated with the objects from the project model. Otherwise the Project will use its own internal dict. """ self._recorded_cmds = [] self._cmds_to_save = [] self.path = expand_path(projpath) self._project_globals = globals_dict if globals_dict is not None else {} self._gui = gui self.macrodir = os.path.join(self.path, '_macros') self.macro = 'default' if gui and not os.path.isdir(self.macrodir): os.makedirs(self.macrodir) settings = os.path.join(self.path, '_settings.cfg') if gui and not os.path.isfile(settings): self._create_config() self.config = SafeConfigParser() self.config.optionxform = str # Preserve case. files = self.config.read(settings) if not files: self._error("Failed to read project config file")