def mkdir(path, nested=False): ''' Create a new directory. ''' options = '-p ' if nested else '' cmd = 'mkdir {0}{1}'.format(options, path) # Run the command. ssh.run(cmd)
def chown(path, user, group=None): ''' Change ownership of a path recursively to the specified user and group. ''' if group: cmd = 'chown -R {0}:{1} {2}'.format(user, group, path) else: cmd = 'chown -R {0} {1}'.format(user, path) ssh.run(cmd)
def upload(self): ''' Start the upload operation. ''' remote_upload_path = tmp_path() remote_extract_path = tmp_path() if not self.paths: raise RuntimeError('No files to be uploaded.') self.update(PREPARING) self.prepare_bundle() # Compress the files to be uploaded. self.update(COMPRESSING) compress(self.bundle_path, self.tar_path) total_size = os.path.getsize(self.tar_path) self.update(COMPRESSED, total=total_size) def put_callback(sent, total): self.update(UPLOADING, sent=sent, total=total) # Upload the tar zipped file to the remote. # The compressed folder gets uploaded to a temp path first. # Then later is extracted to the provided path on the remote. self.update(PREPARING_TO_UPLOAD, total=total_size) put(self.tar_path, remote_upload_path, put_callback) # Extract the files to the remote directory self.update(FINALIZING) moves = [] for (filename, destination) in self.paths: src = os.path.join(remote_extract_path, filename) # Ensure destination directory exists before moving. base_dir = os.path.dirname(destination) moves.append('mkdir -p {base_dir} && mv {src} {dest}'.format( base_dir=base_dir, src=src, dest=destination)) run( tar_extract_remote( remote_upload_path, remote_extract_path, dry_run=True) + moves) os.remove(self.tar_path) self.update(DONE)
def upload(self, remote_path): ''' Start the upload operation. ''' self.update(PREPARING) remote_path = normalize_path(remote_path) total_size = os.path.getsize(self.path) def put_callback(sent, total): self.update(UPLOADING, sent=sent, total=total) # Upload the file to a tmp path. self.update(PREPARING_TO_UPLOAD, total=total_size) put(self.path, self.remote_tmp_path, put_callback) self.update(FINALIZING) # Move the uploaded file to the remote_path. run('mv {} {}'.format(self.remote_tmp_path, remote_path)) self.update(DONE)
def rm_rf(path): ''' Remote the specified path recursively (both files and directories). ''' removal_path = path # If path is not a string but a list of multiple paths, # remove them all. if is_iterable(path) and not is_string(path): removal_path = ' '.join(path) return ssh.run('rm -rf {}'.format(removal_path))
def tar_extract_remote(src, dest, dry_run=False): ''' Extract tar archive on the remote host. ''' commands = [ 'mkdir -p {}'.format(dest), 'tar zxvf {src} --strip-components=1 -C {dest}'.format(src=src, dest=dest), 'rm {}'.format(src) ] if dry_run: return commands return run(commands)
def update_symlink(src, link_path): ''' Update the current build symlink. ''' cmd = 'ln -sfn {} {}'.format(src, link_path) ssh.run(cmd)
def glob(path): ''' Glob a directory path to get the list of files. ''' result = ssh.run('ls -1 {}'.format(path)) return map(strip_ansi, result)
def tar_extract(src, dest): ''' Extract a source tar archive to the specified destination path. ''' cmd = 'tar zxvf {} --strip-components=1 -C {}'.format(src, dest) ssh.run(cmd)
def tar_archive(name, path): ''' Compress the source path into a tar archive. ''' cmd = 'tar -czvf {} {}'.format(name, path) ssh.run(cmd)
def rm(path): ''' Remove a file given by the path. ''' ssh.run('rm ' + path)