def archive(self, target): logger.info('Building terrarium bundle') bin_dir = os.path.join(target, 'bin') Terrarium.wipe_all_precompiled_python_files_in_dir(target) Terrarium.make_bin_dir_paths_relative(bin_dir, target) archive = '%s.tar' % target # Create an archive of the environment call_subprocess([ 'tar', '--exclude', '.svn', '--exclude', '.git', '--exclude', '.bzr', '--exclude', '.hg', '--exclude', 'bin/python', '-cf', archive, '-C', target, '.' ]) if self.args.compress: # Compress the tarball call_subprocess(['gzip', archive]) archive = '%s.gz' % archive Terrarium.make_bin_dir_paths_absolute(bin_dir, target) return archive
def archive(self, target): logger.info('Building terrarium bundle') bin_dir = os.path.join(target, 'bin') Terrarium.wipe_all_precompiled_python_files_in_dir(target) Terrarium.make_bin_dir_paths_relative(bin_dir, target) archive = '%s.tar' % target # Create an archive of the environment call_subprocess([ 'tar', '--exclude-vcs', '--exclude', 'bin/python', '-cf', archive, '-C', target, '.' ]) if self.args.compress: # Compress the tarball call_subprocess(['gzip', archive]) archive = '%s.gz' % archive Terrarium.make_bin_dir_paths_absolute(bin_dir, target) return archive
def extract(self, archive, target): logger.info('Extracting terrarium bundle') archive_type = get_type(archive) if archive_type == 'GZIP': tar_op = 'xzf' elif archive_type == 'BZIP': tar_op = 'xjf' elif archive_type == 'TAR': tar_op = 'xf' else: logger.error( 'Failed to extract archive, unknown or unsupported file type') return if not os.path.exists(target): os.mkdir(target) call_subprocess(['tar', tar_op, archive, '-C', target]) bin_dir = os.path.join(target, 'bin') # Restore python binary path_to_python = sys.executable dest = python_binary = os.path.basename(path_to_python) if python_binary.startswith('python') and python_binary != 'python': dest = 'python' dest = os.path.join(bin_dir, dest) if not os.path.exists(dest): call_subprocess([ 'cp', path_to_python, dest, ]) # Fix up paths Terrarium.make_bin_dir_paths_absolute(bin_dir, target)
def wipe_all_precompiled_python_files_in_dir(path): return call_subprocess([ 'find', path, '-type', 'f', '-name', '*.py[c|o]', '-delete' ])
def install(self): logger.debug('Running install') old_target = self.get_target_location() old_target_backup = self.get_backup_location() new_target = old_target prompt = os.path.basename(new_target) # Are we building a new environment, or replacing an existing one? old_target_exists = self.environment_exists(old_target) if old_target_exists and not self.args.replace: args = [ 'bash', '-c', 'source {}/bin/activate && pip install -r {}'.format(old_target, ' '.join(self.args.reqs)) ] # call_subprocess(args) logger.info('Virtual environment exists, re-installing requirements...') return 0 if old_target_exists: new_target = tempfile.mkdtemp( prefix='%s.' % os.path.basename(old_target), dir=os.path.dirname(old_target), ) # Can the requested environment be downloaded? downloaded = False if self.args.download: downloaded = self.download(new_target) if not downloaded: if self.args.require_download: logger.error( 'Failed to download bundle and download is ' 'required. Refusing to build a new bundle.' ) return 1 # Create a self-contained script to create a virtual environment # and install all of the requested requirements logger.info('Building new environment') fd, bootstrap = tempfile.mkstemp( prefix='terrarium_bootstrap-', suffix='.py', ) self.create_bootstrap(bootstrap) # Run the bootstrap script which pip installs everything that has # been defined as a requirement call_subprocess([ sys.executable, bootstrap, '--prompt=(%s)' % prompt, new_target ]) # Do we want to copy the bootstrap into the environment for future # use? if self.args.bootstrap: logger.info('Copying bootstrap script to new environment') dest = os.path.join( new_target, 'bin', 'terrarium_bootstrap.py') shutil.copyfile(bootstrap, dest) os.chmod(dest, 0744) os.close(fd) os.unlink(bootstrap) if self.args.upload: self.upload(new_target) if old_target_exists: logger.info('Moving old environment out of the way') if os.path.exists(old_target_backup): if not rmtree(old_target_backup): old_target_backup = tempfile.mkdtemp( prefix='terrarium_old_backup_target-' ) old_target_backup = os.path.join(old_target_backup, prompt) logger.info( 'Backing environment up to %s' % old_target_backup) try: os.rename(old_target, old_target_backup) except OSError, why: logger.error( 'Failed to move environment out of the way. ' 'Check that you have the correct permissions. ' '%s' % why ) return 1 # Fix paths Terrarium.replace_all_in_directory( os.path.join(new_target, 'bin'), new_target, old_target, )
def wipe_all_precompiled_python_files_in_dir(path): return call_subprocess( ['find', path, '-type', 'f', '-name', '*.py[c|o]', '-delete'])
def install(self): logger.debug('Running install') old_target = self.get_target_location() old_target_backup = self.get_backup_location() new_target = old_target prompt = os.path.basename(new_target) # Are we building a new environment, or replacing an existing one? old_target_exists = self.environment_exists(old_target) if old_target_exists: new_target = tempfile.mkdtemp( prefix='%s.' % os.path.basename(old_target), dir=os.path.dirname(old_target), ) # Can the requested environment be downloaded? downloaded = False if self.args.download: downloaded = self.download(new_target) if not downloaded: if self.args.require_download: logger.error('Failed to download bundle and download is ' 'required. Refusing to build a new bundle.') return 1 # Create a self-contained script to create a virtual environment # and install all of the requested requirements logger.info('Building new environment') fd, bootstrap = tempfile.mkstemp( prefix='terrarium_bootstrap-', suffix='.py', ) self.create_bootstrap(bootstrap) # Run the bootstrap script which pip installs everything that has # been defined as a requirement call_subprocess([ sys.executable, bootstrap, '--prompt=(%s)' % prompt, new_target ]) # Do we want to copy the bootstrap into the environment for future # use? if self.args.bootstrap: logger.info('Copying bootstrap script to new environment') dest = os.path.join(new_target, 'bin', 'terrarium_bootstrap.py') shutil.copyfile(bootstrap, dest) os.chmod(dest, 0744) os.close(fd) os.unlink(bootstrap) if self.args.upload: self.upload(new_target) if old_target_exists: logger.info('Moving old environment out of the way') if os.path.exists(old_target_backup): if not rmtree(old_target_backup): old_target_backup = tempfile.mkdtemp( prefix='terrarium_old_backup_target-') old_target_backup = os.path.join(old_target_backup, prompt) logger.info('Backing environment up to %s' % old_target_backup) try: os.rename(old_target, old_target_backup) except OSError, why: logger.error('Failed to move environment out of the way. ' 'Check that you have the correct permissions. ' '%s' % why) return 1 # Fix paths Terrarium.replace_all_in_directory( os.path.join(new_target, 'bin'), new_target, old_target, )
def install(self): logger.debug('Running install') old_target = os.path.abspath(self.args.target) new_target = old_target prompt = os.path.basename(new_target) # Are we building a new environment, or replacing an existing one? old_target_exists = os.path.exists(os.path.join( old_target, 'bin', 'activate', )) if old_target_exists: new_target = tempfile.mkdtemp( prefix='%s.' % os.path.basename(old_target), dir=os.path.dirname(old_target), ) #logger.info('new_target %s', new_target) # Can the requested environment be downloaded? downloaded = False if self.args.download: downloaded = self.download(new_target) if not downloaded: # Create a self-contained script to create a virtual environment # and install all of the requested requirements logger.info('Building new environment') fd, bootstrap = tempfile.mkstemp( prefix='terrarium_bootstrap-', suffix='.py', ) self.create_bootstrap(bootstrap) # Run the bootstrap script which pip installs everything that has # been defined as a requirement call_subprocess([ sys.executable, bootstrap, '--prompt=(%s)' % prompt, new_target ]) # Do we want to copy the bootstrap into the environment for future # use? if self.args.bootstrap: logger.info('Copying bootstrap script to new environment') dest = os.path.join( new_target, 'bin', 'terrarium_bootstrap.py') shutil.copyfile(bootstrap, dest) os.chmod(dest, 0744) os.close(fd) os.unlink(bootstrap) if self.args.upload: self.upload(new_target) old_target_backup = '%s%s' % (old_target, self.args.backup_suffix) if old_target_exists: logger.info('Moving old environment out of the way') if os.path.exists(old_target_backup): rmtree(old_target_backup) os.rename(old_target, old_target_backup) # Fix paths Terrarium.replace_all_in_directory( os.path.join(new_target, 'bin'), new_target, old_target, ) # move the new environment into the target's place os.rename(new_target, old_target) # Do we keep a backup of the old environment around or wipe it? if os.path.isdir(old_target_backup) and not self.args.backup: logger.info('Deleting old environment') rmtree(old_target_backup)