Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from boto3.s3.transfer import S3Transfer
  4. import boto3
  5. import getpass
  6. import os
  7. import subprocess
  8. import configparser
  9. import sys
  10. import threading
  11.  
  12.  
  13. # Credentials required for getmail to grab emails from Gmail
  14. print"Please enter the credentials for the mailbox you wish to archive"
  15. username = raw_input("Username: ")
  16. password = getpass.getpass("Password: ")
  17.  
  18.  
  19. def archive_mailbox():
  20. # Variables
  21. base_dir = os.path.expanduser("~/getmail/")
  22. config_path = "{base}getmail_config".format(base=base_dir)
  23. global mbox
  24. mbox = "{0}{1}.mbox".format(base_dir, username)
  25.  
  26. # Create config file for getmail
  27. config = configparser.ConfigParser()
  28. config['retriever'] = {'type': 'SimpleIMAPSSLRetriever',
  29. 'server': 'imap.gmail.com',
  30. 'mailboxes': '("Inbox", "[Gmail]/Sent Mail")',
  31. 'username': username,
  32. 'password': password}
  33.  
  34. config['destination'] = {'type': 'Mboxrd',
  35. 'path': mbox}
  36.  
  37. config['options'] = {'delete': 'true',
  38. 'message_log': '{0}getmail_log'.format(base_dir)}
  39.  
  40. # Create getmail and it's required directories
  41. try:
  42. if not os.path.exists(base_dir):
  43. print "Creating the base dir for getmail '{0}'".format(base_dir)
  44. os.makedirs(base_dir, 0775)
  45.  
  46. dirs = ["cur", "new", "tmp"]
  47.  
  48. for entry in dirs:
  49. print "Creating the '{0}' dir for getmail '{1}'".format(
  50. entry,
  51. base_dir
  52. )
  53. os.makedirs("{0}{1}".format(base_dir, entry), 0775)
  54.  
  55. open(mbox, 'w').close()
  56.  
  57. with open(config_path, 'w') as configfile:
  58. config.write(configfile)
  59.  
  60. # Download emails from Gmail using getmail
  61. print "Downloading the mailbox for {0} to `{1}`".format(
  62. username,
  63. mbox
  64. )
  65.  
  66. subprocess.call(["getmail", "--all",
  67. "--getmaildir={base}".format(base=base_dir),
  68. "--dont-delete",
  69. "--rcfile={config}".format(config=config_path)
  70. ])
  71. except Exception as e:
  72. print(e)
  73. finally:
  74. # Remove config file as it contains sensitive information
  75. print "Removing '{0}' as it contains secrets".format(config_path)
  76. os.remove(config_path)
  77.  
  78.  
  79. # Required for the progress bar when uploading to S3
  80. class ProgressPercentage(object):
  81. def __init__(self, filename):
  82. self._filename = filename
  83. self._size = float(os.path.getsize(filename))
  84. self._seen_so_far = 0
  85. self._lock = threading.Lock()
  86.  
  87. def __call__(self, bytes_amount):
  88. # To simplify we'll assume this is hooked up
  89. # to a single filename.
  90. with self._lock:
  91. self._seen_so_far += bytes_amount
  92. percentage = (self._seen_so_far / self._size) * 100
  93. sys.stdout.write(
  94. "\r%s %s / %s (%.2f%%)" % (
  95. self._filename, self._seen_so_far, self._size,
  96. percentage))
  97. sys.stdout.flush()
  98.  
  99.  
  100. def s3_upload():
  101. # Create an S3 client
  102. s3 = boto3.client('s3')
  103. transfer = S3Transfer(s3)
  104.  
  105. # Variables
  106. print"Please enter the S3 bucket you wish to upload the .mbox archive to"
  107. bucket_name = raw_input("S3 Bucket name: ")
  108. key = "{0}/{0}.mbox".format(username)
  109.  
  110. # Upload files to S3 bucket
  111. try:
  112. transfer.upload_file(
  113. mbox,
  114. bucket_name,
  115. key,
  116. callback=ProgressPercentage(mbox)
  117. )
  118. except Exception as e:
  119. print(e)
  120.  
  121.  
  122. def main():
  123. archive_mailbox()
  124. s3_upload()
  125.  
  126.  
  127. if __name__ == "__main__":
  128. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement