def check(dirname): foo = os.path.join(dirname, 'foo') assert 'foo' == subdirectory_relative_to_directory(foo, dirname) foobar = os.path.join(dirname, os.path.join('foo', 'bar')) assert os.path.join('foo', 'bar') == subdirectory_relative_to_directory( foobar, dirname) # keep the path absolute if it isn't inside the parent parent_of_dirname = os.path.dirname(dirname) assert parent_of_dirname == subdirectory_relative_to_directory( parent_of_dirname, dirname)
def environ_set_prefix(environ, prefix, varname=conda_prefix_variable()): prefix = os.path.normpath(prefix) environ[varname] = prefix if varname != 'CONDA_DEFAULT_ENV': # This case matters on both Unix and Windows # with conda >= 4.1.4 since requirement.env_var # is CONDA_PREFIX, and matters on Unix only pre-4.1.4 # when requirement.env_var is CONDA_ENV_PATH. global _envs_dirs global _root_dir if _envs_dirs is None: i = info() _envs_dirs = [os.path.normpath(d) for d in i.get('envs_dirs', [])] _root_dir = os.path.normpath(i.get('root_prefix')) if prefix == _root_dir: name = 'root' else: for d in _envs_dirs: name = subdirectory_relative_to_directory(prefix, d) if name != prefix: break environ['CONDA_DEFAULT_ENV'] = name
def _archive_project(project, filename): """Make an archive of the non-ignored files in the project. Args: project (``Project``): the project filename (str): name for the new zip or tar.gz archive file Returns: a ``Status``, if failed has ``errors`` """ failed = project.problems_status() if failed is not None: for error in failed.errors: project.frontend.error(error) return failed frontend = _new_error_recorder(project.frontend) if not os.path.exists(project.project_file.filename): frontend.error("%s does not exist." % project.project_file.basename) return SimpleStatus(success=False, description="Can't create an archive.", errors=frontend.pop_errors()) # this would most likely happen in a GUI editor, if it reloaded # the project from memory but hadn't saved yet. if project.project_file.has_unsaved_changes: frontend.error("%s has been modified but not saved." % project.project_file.basename) return SimpleStatus(success=False, description="Can't create an archive.", errors=frontend.pop_errors()) infos = _enumerate_archive_files( project.directory_path, frontend, requirements=project.union_of_requirements_for_all_envs) if infos is None: return SimpleStatus(success=False, description="Failed to list files in the project.", errors=frontend.pop_errors()) # don't put the destination zip into itself, since it's fairly natural to # create a archive right in the project directory relative_dest_file = subdirectory_relative_to_directory( filename, project.directory_path) if not os.path.isabs(relative_dest_file): infos = [ info for info in infos if info.relative_path != relative_dest_file ] tmp_filename = filename + ".tmp-" + str(uuid.uuid4()) try: if filename.lower().endswith(".zip"): _write_zip(project.name, infos, tmp_filename, frontend) elif filename.lower().endswith(".tar.gz"): _write_tar(project.name, infos, tmp_filename, compression="gz", frontend=frontend) elif filename.lower().endswith(".tar.bz2"): _write_tar(project.name, infos, tmp_filename, compression="bz2", frontend=frontend) elif filename.lower().endswith(".tar"): _write_tar(project.name, infos, tmp_filename, compression=None, frontend=frontend) else: frontend.error("Unsupported archive filename %s." % (filename)) return SimpleStatus( success=False, description= "Project archive filename must be a .zip, .tar.gz, or .tar.bz2.", errors=frontend.pop_errors()) rename_over_existing(tmp_filename, filename) except IOError as e: frontend.error(str(e)) return SimpleStatus( success=False, description=("Failed to write project archive %s." % (filename)), errors=frontend.pop_errors()) finally: try: os.remove(tmp_filename) except (IOError, OSError): pass unlocked = [] for env_spec in project.env_specs.values(): if env_spec.lock_set.disabled: unlocked.append(env_spec.name) if len(unlocked) > 0: frontend.info( "Warning: env specs are not locked, which means they may not " "work consistently for others or when deployed.") frontend.info( " Consider using the 'anaconda-project lock' command to lock the project." ) if len(unlocked) != len(project.env_specs): frontend.info(" Unlocked env specs are: " + (", ".join(sorted(unlocked)))) return SimpleStatus(success=True, description=("Created project archive %s" % filename))
def _archive_project(project, filename): """Make an archive of the non-ignored files in the project. Args: project (``Project``): the project filename (str): name for the new zip or tar.gz archive file Returns: a ``Status``, if failed has ``errors`` """ failed = project.problems_status() if failed is not None: return failed if not os.path.exists(project.project_file.filename): return SimpleStatus(success=False, description="Can't create an archive.", errors=[("%s does not exist." % project.project_file.basename)]) # this would most likely happen in a GUI editor, if it reloaded # the project from memory but hadn't saved yet. if project.project_file.has_unsaved_changes: return SimpleStatus(success=False, description="Can't create an archive.", errors=[("%s has been modified but not saved." % project.project_file.basename)]) errors = [] infos = _enumerate_archive_files(project.directory_path, errors, requirements=project.requirements) if infos is None: return SimpleStatus(success=False, description="Failed to list files in the project.", errors=errors) # don't put the destination zip into itself, since it's fairly natural to # create a archive right in the project directory relative_dest_file = subdirectory_relative_to_directory( filename, project.directory_path) if not os.path.isabs(relative_dest_file): infos = [ info for info in infos if info.relative_path != relative_dest_file ] logs = [] tmp_filename = filename + ".tmp-" + str(uuid.uuid4()) try: if filename.lower().endswith(".zip"): _write_zip(project.name, infos, tmp_filename, logs) elif filename.lower().endswith(".tar.gz"): _write_tar(project.name, infos, tmp_filename, compression="gz", logs=logs) elif filename.lower().endswith(".tar.bz2"): _write_tar(project.name, infos, tmp_filename, compression="bz2", logs=logs) elif filename.lower().endswith(".tar"): _write_tar(project.name, infos, tmp_filename, compression=None, logs=logs) else: return SimpleStatus( success=False, description= "Project archive filename must be a .zip, .tar.gz, or .tar.bz2.", errors=["Unsupported archive filename %s." % (filename)]) rename_over_existing(tmp_filename, filename) except IOError as e: return SimpleStatus( success=False, description=("Failed to write project archive %s." % (filename)), errors=[str(e)]) finally: try: os.remove(tmp_filename) except (IOError, OSError): pass return SimpleStatus(success=True, description=("Created project archive %s" % filename), logs=logs)