Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

files: add support for uploading multiple files #171

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 48 additions & 18 deletions cap_client/cli/files_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import click

from cap_client.api import FilesAPI
from cap_client.utils import ColoredGroup, json_dumps, logger, pid_option
from cap_client.utils import ColoredGroup, json_dumps, logger, pid_option, load_file_names_from_directory

pass_api = click.make_pass_decorator(FilesAPI, ensure=True)

Expand Down Expand Up @@ -62,31 +62,61 @@ def get(api, pid):
default=False,
help="Bypasses prompts..Say YES to everything",
)
@click.option(
'--dir-files',
'-df',
type=click.Path(exists=True),
help='Upload all the files individually of the specified directory.',
)
@click.argument(
'file',
'path',
nargs=-1,
type=click.Path(exists=True),
)
@logger
@pass_api
def upload(api, pid, file, output_filename, yes_i_know):
def upload(api, pid, path, output_filename, yes_i_know, dir_files):
"""Upload a file to your analysis."""
if os.path.isdir(file):
if yes_i_know or click.confirm(
'{} is a directory. Do you want to upload a tarball?'.format(
file)):
api.upload_directory(
if not path and not dir_files:
click.echo("Error: Missing argument 'PATH' or option 'DIR_FILES'.")
return

file_names = list(path) if path else []
if dir_files:
file_names += load_file_names_from_directory(dir_files)
if file_names:
if not yes_i_know and not click.confirm(
"{} contains {} files. Do you want to upload them individually?".format(
dir_files, len(file_names)
)
):
click.echo("Use 'PATH' argument for uploading as tarball.")
return
else:
click.echo("No files found in parent directory {}.".format(dir_files))

for _file in file_names:
if os.path.isdir(_file):
if yes_i_know or click.confirm(
"{} is a directory. Do you want to upload a tarball?".format(_file)
):
api.upload_directory(
pid=pid,
filepath=_file,
output_filename=output_filename,
)
click.echo("Directory {} uploaded successfully.".format(_file))
else:
click.echo("Use --dir-files for uploading files individually.")
click.echo("Aborting upload of {}.".format(_file))
continue
elif os.stat(_file).st_size != 0:
api.upload_file(
pid=pid,
filepath=file,
filepath=_file,
output_filename=output_filename,
)
else:
api.upload_file(
pid=pid,
filepath=file,
output_filename=output_filename,
)

click.echo("File uploaded successfully.")
click.echo("File {} uploaded successfully.".format(_file))


@files.command()
Expand Down Expand Up @@ -129,7 +159,7 @@ def download(api, pid, filename, output_file, yes_i_know):
@logger
@pass_api
def remove(api, pid, filename):
"""Removefile from deposit with given pid."""
"""Remove a file from deposit with given pid."""
api.remove(pid=pid, filename=filename)

click.echo("File {} removed.".format(filename))
9 changes: 9 additions & 0 deletions cap_client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ def load_num(ctx, param, value):
raise BadParameter('Not a valid number.')


def load_file_names_from_directory(dir_path):
"""Return the list of file names inside the directory."""
filenames = []
for root, d_names, f_names in os.walk(dir_path):
for f in f_names:
filenames.append(os.path.join(root, f))
return filenames


class NotRequiredIf(click.Option):
"""
Mutually exclusive REQUIRED arguments.
Expand Down
107 changes: 56 additions & 51 deletions docs/chapters/files.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Files

The `files` command group allows the user to manage files attached to their analyses, as well as upload local files and attach them to an analysis.
The `files` command group allows users to manage files attached to their analyses and upload local files and attach them to analysis. The supported commands are the following:

```
**[terminal]
Expand All @@ -15,18 +15,24 @@ Options:
Commands:
download Download file uploaded with given deposit.
get Get list of files attached to analysis with given PID.
remove Removefile from deposit with given pid.
remove Remove a file from deposit with given PID.
upload Upload a file to your analysis.
```

### Download a file from an analysis

#### Download a file from an analysis
#### Description

**Description:**
- Allows the user to download a previously attached file to a specified analysis.
- The supported options are the following:

Allows the user to download a file, that was previously attached to a specified analysis.
| Name | Type | Desc |
| :----------------- | :----- | :---------------------------------------------------- |
| FILENAME | TEXT | The name of the file to be downloaded [required] |
| --pid / -p | TEXT | Your analysis PID (Persistent Identifier) [required] |
| --output-file / -o | PATH | Download file as |

**Usage:**
#### Usage

```
**[terminal]
Expand All @@ -40,22 +46,18 @@ File saved as FILENAME.
File saved as dir/NEWFILE.
```

**Options:**
### Retrieve all the files of an analysis

#### Description

- Allows the user to get a list of the files attached to an analysis with a given PID.
- The supported options are the following:

| Name | Type | Desc |
| :----------------- | :----- | :---------------------------------------------------- |
| FILENAME | TEXT | The name of the file to be downloaded [required] |
| --pid / -p | TEXT | Your analysis PID (Persistent Identifier) [required] |
| --output-file / -o | PATH | Download file as |


#### Retrieve all the files of an analysis

**Description:**

Allows the user to get a list of the files, attached to an analysis with a given PID.

**Usage:**
#### Usage

```
**[terminal]
Expand All @@ -70,63 +72,66 @@ Allows the user to get a list of the files, attached to an analysis with a given
]
```

**Options:**
### Remove a file from an analysis

#### Description

- Allows the user to get a list of the files attached to an analysis with a given PID.
- The supported options are the following:

| Name | Type | Desc |
| :----------------- | :----- | :---------------------------------------------------- |
| --pid / -p | TEXT | Your analysis PID (Persistent Identifier) [required] |


#### Remove a file from an analysis

**Description:**

Allows the user to get a list of the files, attached to an analysis with a given PID.

**Usage:**
#### Usage

```
**[terminal]
**[prompt user@pc]**[path ~]**[delimiter $ ]**[command cap-client files remove --pid <analysis-pid> FILENAME]
File FILENAME removed.
```

**Options:**

| Name | Type | Desc |
| :----------------- | :----- | :---------------------------------------------------- |
| --pid / -p | TEXT | Your analysis PID (Persistent Identifier) [required] |
### Upload a file to an analysis

#### Description

#### Upload a file to an analysis
- Allows the user to upload and attach a file to analysis or a whole directory as a tar.gz file.
- The command enables the user to upload a single file of any type, as well as a whole directory. After a prompt asks the user for confirmation, the directory will be zipped and uploaded as a `.tar.gz` file.
- To avoid the prompt and enable the usage of cap-client inside a CLI script, the flag `--yes-i-know` can be added.
- The supported options are the following:

**Description:**
| Name | Type | Desc |
| :--------------------- | :----- | :----------------------------------------------------------- |
| FILE | TEXT | The name of the file/s to be uploaded [required] |
| --pid / -p | TEXT | Your analysis PID (Persistent Identifier) [required] |
| --output-filename / -o | PATH | Upload file as.. |
| --yes-i-know | FLAG | Bypasses prompts..Say YES to everything |
| --dir-files / -df | TEXT | Upload all the files individually of the specified directory.|

Allows the user to upload and attach a file to an analysis, or a whole directory as a tar.gz file.
#### Usage

**Usage:**
```
**[terminal]
**[prompt user@pc]**[path ~]**[delimiter $ ]**[command cap-client files upload --pid <analysis-pid> FILE1]
File File1 uploaded successfully.
```

```
**[terminal]
**[prompt user@pc]**[path ~]**[delimiter $ ]**[command cap-client files upload --pid <analysis-pid> FILE]
File uploaded successfully.
**[prompt user@pc]**[path ~]**[delimiter $ ]**[command cap-client files upload --pid <analysis-pid> FILE1 FILE2]
File File1 uploaded successfully.
File File2 uploaded successfully.
```

```
**[terminal]
**[prompt user@pc]**[path ~]**[delimiter $ ]**[command cap-client files upload --pid <analysis-pid> --yes-i-know DIR]
File uploaded successfully.
Directory DIR uploaded successfully.
```

**Extended Description:**

The command enables the user to upload a single file of any type, as well as a whole directory. After a prompt asks the user for confirmation, the directory will be zipped and uploaded as a `.tar.gz` file. In order to avoid the prompt, and enable the usage of CAP-Client inside a cli script, the flag `--yes-i-know` can be added.

**Options:**

| Name | Type | Desc |
| :--------------------- | :----- | :---------------------------------------------------- |
| FILE | TEXT | The name of the file to be downloaded [required] |
| --pid / -p | TEXT | Your analysis PID (Persistent Identifier) [required] |
| --output-filename / -o | PATH | Upload file as |
| --yes-i-know | FLAG | Bypasses prompts..Say YES to everything |
```
**[terminal]
**[prompt user@pc]**[path ~]**[delimiter $ ]**[command cap-client files upload --pid <analysis-pid> --dir-files DIR]
File DIR/File1 uploaded successfully.
File DIR/File2 uploaded successfully.
...
```
Loading