def storage_mk_dir(cls, folders): """ Creates a directory """ buckets = [] try: buckets = list(DataStorage.list()) except (ConfigNotFoundError, requests.exceptions.RequestException, RuntimeError, ValueError) as error: click.echo('Error: %s' % str(error), err=True) sys.exit(1) for original_path in folders: folder = original_path info = DataStorageWrapper.get_data_storage_item_path_info(folder, buckets) error = info[0] current_bucket_identifier = info[1] relative_path = info[2] if error is not None: click.echo(error, err=True) continue if len(relative_path) == 0: click.echo('Cannot create folder \'{}\': already exists'.format(original_path), err=True) continue click.echo('Creating folder {}...'.format(original_path), nl=False) result = None try: result = DataStorage.create_folder(current_bucket_identifier, relative_path) except (ConfigNotFoundError, requests.exceptions.RequestException, RuntimeError, ValueError) as error: click.echo('Error: %s' % str(error), err=True) sys.exit(1) if result is not None and result.error is None: click.echo('done.') elif result is not None and result.error is not None: click.echo('failed.') click.echo(result.error, err=True)
def __print_data_storage_contents(cls, bucket_model, relative_path, show_details, recursive, page_size=None, show_versions=False, show_all=False): items = [] header = None if bucket_model is not None: try: wrapper = S3BucketWrapper(bucket_model, relative_path) manager = S3BucketOperations.get_list_manager(wrapper, show_versions=show_versions) items = manager.list_items(relative_path, recursive=recursive, page_size=page_size, show_all=show_all) except (ConfigNotFoundError, requests.exceptions.RequestException, RuntimeError, ValueError) as error: click.echo('Error: %s' % str(error), err=True) sys.exit(1) else: # If no argument is specified - list brief details of all buckets try: items = list(DataStorage.list()) if not items: click.echo("No datastorages available.") sys.exit(0) except (ConfigNotFoundError, requests.exceptions.RequestException, RuntimeError, ValueError) as error: click.echo('Error: %s' % str(error), err=True) sys.exit(1) if recursive and header is not None: click.echo(header) if show_details: items_table = prettytable.PrettyTable() fields = ["Type", "Labels", "Modified", "Size", "Name"] if show_versions: fields.append("Version") items_table.field_names = fields items_table.align = "l" items_table.border = False items_table.padding_width = 2 items_table.align['Size'] = 'r' for item in items: name = item.name changed = '' size = '' labels = '' if item.type is not None and item.type.lower() == 's3': name = item.path if item.changed is not None: if bucket_model is None: # need to wrap into datetime since bucket listing returns str item_datetime = datetime.datetime.strptime(item.changed, '%Y-%m-%d %H:%M:%S') else: item_datetime = item.changed changed = item_datetime.strftime('%Y-%m-%d %H:%M:%S') if item.size is not None: size = item.size if item.labels is not None and len(item.labels) > 0: labels = ', '.join(map(lambda i: i.value, item.labels)) item_type = "-File" if item.delete_marker else item.type row = [item_type, labels, changed, size, name] if show_versions: row.append('') items_table.add_row(row) if show_versions and item.type == 'File': for version in item.versions: version_type = "-File" if version.delete_marker else "+File" version_label = "{} (latest)".format(version.version) if version.latest else version.version labels = ', '.join(map(lambda i: i.value, version.labels)) size = '' if version.size is None else version.size row = [version_type, labels, version.changed.strftime('%Y-%m-%d %H:%M:%S'), size, name, version_label] items_table.add_row(row) click.echo(items_table) click.echo() else: for item in items: click.echo('{}\t\t'.format(item.path), nl=False) click.echo()