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

Added permissions fixing after running scratch-copy and implemented a new module #332

Merged
merged 10 commits into from
Sep 10, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Code contributions to the new version:
- Added full execution support for the MAG template [#321](https://github.com/BU-ISCIII/buisciii-tools/pull/321).
- Added labels to services.json and updated bioinfo_doc.py and jinja_template_delivery.j2 so that software versions data is displayed in the delivery pdf [#330](https://github.com/BU-ISCIII/buisciii-tools/pull/330).
- Updated several templates (singularity images, outdated paths, improvements, etc) [#331](https://github.com/BU-ISCIII/buisciii-tools/pull/331)
- Added permissions fixing after running scratch_copy, as well as a new fix-permissions module in the tools [#332](https://github.com/BU-ISCIII/buisciii-tools/pull/332).

### Modules

Expand Down
26 changes: 26 additions & 0 deletions bu_isciii/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,5 +649,31 @@ def autoclean_sftp(ctx, sftp_folder, days):
sftp_clean.handle_autoclean_sftp()


# FIX PERMISSIONS
@bu_isciii_cli.command(help_priority=9)
@click.option(
"-d",
"--input_directory",
type=click.Path(),
default=None,
required=True,
help="Input directory to fix permissions (absolute path)",
)
@click.pass_context
def fix_permissions(ctx, input_directory):
"""
Fix permissions
"""
if not os.path.isdir(input_directory):
exit("Invalid input directory")
conf = bu_isciii.config_json.ConfigJson()
permissions = conf.get_configuration("global").get("permissions")
bu_isciii.utils.remake_permissions(input_directory, permissions)
stderr = rich.console.Console(
stderr=True, force_terminal=bu_isciii.utils.rich_force_colors()
)
stderr.print(f"[green]Correct permissions were applied to {input_directory}")


if __name__ == "__main__":
run_bu_isciii()
6 changes: 5 additions & 1 deletion bu_isciii/conf/configuration.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"global": {
"data_path": "/data/bi",
"archived_path": "/archived/bi",
"yaml_conf_path": "~/buisciii_config.yml"
"yaml_conf_path": "~/buisciii_config.yml",
"permissions": {
"directory_chmod": "2775",
"file_chmod": "664"
}
},
"sftp_copy": {
"protocol": "rsync",
Expand Down
10 changes: 10 additions & 0 deletions bu_isciii/scratch.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@ def revert_copy_scratch(self):
sync_source_contents=False,
)
self.srun_command(self.srun_settings, rsync_command)

# After successful rsync, apply correct permissions
conf = bu_isciii.config_json.ConfigJson()
permissions_config = conf.get_configuration("global").get(
"permissions"
)
bu_isciii.utils.remake_permissions(
self.full_path, permissions_config
)

stderr.print(
"[green]Successfully copied the directory to %s"
% dest_folder,
Expand Down
30 changes: 30 additions & 0 deletions bu_isciii/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import tarfile
import sys
import subprocess

import questionary
import rich
Expand Down Expand Up @@ -495,3 +496,32 @@ def process_yaml_file(yaml_file):

def validate_date(date_previous, date_posterior):
return


def remake_permissions(copied_folder_path, permissions_config):
"""
Change permissions of all files and directories in a given absolute path.

Args:
copied_folder_path: The path to the folder that was copied
permissions_config: Dictionary containing permissions configuration (e.g., {'directory_chmod': '755', 'file_chmod': '664'})
"""
subprocess.run(
f"chown -R $(whoami):bi {copied_folder_path}", shell=True, check=True
)

# Change permissions for directories
if "directory_chmod" in permissions_config:
subprocess.run(
f"find {copied_folder_path} -type d -exec chmod {permissions_config['directory_chmod']} {{}} \;",
shell=True,
check=True,
)

# Change permissions for files
if "file_chmod" in permissions_config:
subprocess.run(
f"find {copied_folder_path} -type f -exec chmod {permissions_config['file_chmod']} {{}} \;",
shell=True,
check=True,
)
Loading