azure_delete.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/usr/bin/env python
  2. from azure_storage.methods import client_prep, create_parent_parser, delete_container, delete_file, \
  3. delete_folder, setup_arguments, set_blob_retention_policy
  4. from argparse import ArgumentParser, RawTextHelpFormatter
  5. import coloredlogs
  6. import logging
  7. import sys
  8. import os
  9. class AzureContainerDelete(object):
  10. def main(self):
  11. self.container_name, self.connect_str, self.blob_service_client, container_client = \
  12. client_prep(container_name=self.container_name,
  13. passphrase=self.passphrase,
  14. account_name=self.account_name)
  15. # Hide the INFO-level messages sent to the logger from Azure by increasing the logging level to WARNING
  16. logging.getLogger().setLevel(logging.WARNING)
  17. delete_container(blob_service_client=self.blob_service_client,
  18. container_name=self.container_name,
  19. account_name=self.account_name)
  20. def __init__(self, container_name, account_name, passphrase):
  21. # Set the container name variable
  22. self.container_name = container_name
  23. # Initialise necessary class variables
  24. self.passphrase = passphrase
  25. self.account_name = account_name
  26. self.connect_str = str()
  27. self.blob_service_client = None
  28. class AzureDelete(object):
  29. def main(self):
  30. self.container_name, self.connect_str, self.blob_service_client, self.container_client = \
  31. client_prep(container_name=self.container_name,
  32. passphrase=self.passphrase,
  33. account_name=self.account_name)
  34. # Hide the INFO-level messages sent to the logger from Azure by increasing the logging level to WARNING
  35. logging.getLogger().setLevel(logging.WARNING)
  36. # Set the file retention policy
  37. self.blob_service_client = set_blob_retention_policy(blob_service_client=self.blob_service_client,
  38. days=self.retention_time)
  39. # Run the proper method depending on whether a file or a folder is requested
  40. if self.category == 'file':
  41. delete_file(container_client=self.container_client,
  42. object_name=self.object_name,
  43. blob_service_client=self.blob_service_client,
  44. container_name=self.container_name)
  45. elif self.category == 'folder':
  46. delete_folder(container_client=self.container_client,
  47. object_name=self.object_name,
  48. blob_service_client=self.blob_service_client,
  49. container_name=self.container_name,
  50. account_name=self.account_name)
  51. else:
  52. logging.error(f'Something is wrong. There is no {self.category} option available')
  53. raise SystemExit
  54. def __init__(self, object_name, container_name, account_name, passphrase, retention_time, category):
  55. self.object_name = object_name
  56. # Set the container name variable
  57. self.container_name = container_name
  58. # Initialise necessary class variables
  59. self.passphrase = passphrase
  60. self.account_name = account_name
  61. self.retention_time = retention_time
  62. # Ensure that the retention time provided is valid
  63. try:
  64. assert 0 < self.retention_time < 366
  65. except AssertionError:
  66. logging.error(f'The provided retention time ({self.retention_time}) is invalid. '
  67. f'It must be between 1 and 365 days')
  68. raise SystemExit
  69. self.category = category
  70. self.connect_str = str()
  71. self.blob_service_client = None
  72. self.container_client = None
  73. def container_delete(args):
  74. """
  75. Run the AzureContainerDelete method
  76. :param args: type ArgumentParser arguments
  77. """
  78. logging.info(f'Deleting container {args.container_name} from Azure storage account {args.account_name}')
  79. del_container = AzureContainerDelete(container_name=args.container_name,
  80. account_name=args.account_name,
  81. passphrase=args.passphrase)
  82. del_container.main()
  83. def file_delete(args):
  84. """
  85. Run the AzureDelete method for a file
  86. :param args: type ArgumentParser arguments
  87. """
  88. logging.info(f'Deleting file {args.file} from container {args.container_name} in Azure storage account '
  89. f'{args.account_name}')
  90. del_file = AzureDelete(object_name=args.file,
  91. container_name=args.container_name,
  92. account_name=args.account_name,
  93. passphrase=args.passphrase,
  94. retention_time=args.retention_time,
  95. category='file')
  96. del_file.main()
  97. def folder_delete(args):
  98. """
  99. Run the AzureDelete method for a folder
  100. :param args: type ArgumentParser arguments
  101. """
  102. logging.info(f'Deleting folder {args.folder} from container {args.container_name} in Azure storage account '
  103. f'{args.account_name}')
  104. del_folder = AzureDelete(object_name=args.folder,
  105. container_name=args.container_name,
  106. account_name=args.account_name,
  107. passphrase=args.passphrase,
  108. retention_time=args.retention_time,
  109. category='folder')
  110. del_folder.main()
  111. def cli():
  112. parser = ArgumentParser(description='Move and/or delete containers, files, or folders in Azure storage')
  113. # Create the parental parser, and the subparser
  114. subparsers, parent_parser = create_parent_parser(parser=parser)
  115. # Container delete subparser
  116. container_delete_subparser = subparsers.add_parser(parents=[parent_parser],
  117. name='container',
  118. description='Delete a container in Azure storage',
  119. formatter_class=RawTextHelpFormatter,
  120. help='Delete a container in Azure storage')
  121. container_delete_subparser.set_defaults(func=container_delete)
  122. # File delete subparser
  123. file_delete_subparser = subparsers.add_parser(parents=[parent_parser],
  124. name='file',
  125. description='Delete a file in Azure storage',
  126. formatter_class=RawTextHelpFormatter,
  127. help='Delete a file in Azure storage')
  128. file_delete_subparser.add_argument('-f', '--file',
  129. type=str,
  130. required=True,
  131. help='Name of blob file to delete in Azure storage. '
  132. 'e.g. 2022-SEQ-0001_S1_L001_R1_001.fastq.gz')
  133. file_delete_subparser.add_argument('-r', '--retention_time',
  134. type=int,
  135. default=8,
  136. help='Retention time for deleted files. Default is 8 days. Must be between '
  137. '1 and 365')
  138. file_delete_subparser.set_defaults(func=file_delete)
  139. # Folder delete subparser
  140. folder_delete_subparser = subparsers.add_parser(parents=[parent_parser],
  141. name='folder',
  142. description='Delete a folder in Azure storage',
  143. formatter_class=RawTextHelpFormatter,
  144. help='Delete a folder in Azure storage')
  145. folder_delete_subparser.add_argument('-f', '--folder',
  146. type=str,
  147. required=True,
  148. help='Name of folder to delete in Azure storage. '
  149. 'e.g. InterOp')
  150. folder_delete_subparser.add_argument('-r', '--retention_time',
  151. type=int,
  152. default=8,
  153. help='Retention time for deleted files. Default is 8 days')
  154. folder_delete_subparser.set_defaults(func=folder_delete)
  155. # Set up the arguments, and run the appropriate subparser
  156. arguments = setup_arguments(parser=parser)
  157. # Return to the requested logging level, as it has been increased to WARNING to suppress the log being filled with
  158. # information from azure.core.pipeline.policies.http_logging_policy
  159. coloredlogs.install(level=arguments.verbosity.upper())
  160. logging.info('Deletion complete')
  161. # Prevent the arguments being printed to the console (they are returned in order for the tests to work)
  162. sys.stderr = open(os.devnull, 'w')
  163. return arguments
  164. if __name__ == '__main__':
  165. cli()