-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge3.py
More file actions
executable file
·108 lines (89 loc) · 3.87 KB
/
challenge3.py
File metadata and controls
executable file
·108 lines (89 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Challenge3 - Write a script that accepts a directory as an argument as well
# as a container name. The script should upload the contents of the specified
# directory to the container (or create it if it doesn't exist). The script
# should handle errors appropriately. (Check for invalid paths, etc.)
# Copyright 2013 Scott Gilbert
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# Required Parameters:
# SourceDir source directory containing files to be uploaded
# Container destination cloudfiles container
#
# Optional Parameters:
# -h, --help show help message and exit
# --region REGION Region in which to create container (DFW or ORD)
import sys
import os
import time
import argparse
import pyrax
import challenge1 as c1
def upload_dir_to_container(cf, ULdirectory, ULContainer):
""" Upload contents of a local directory to a CloudFiles Container
If the specified CloudFiles container does not already exist, then it
is created.
A simple progress message is displayed every 2 seconds and function
does not return until upload completes.
"""
# if container does not already exist, then create it!
try:
cont = cf.get_container(ULContainer)
print "The container %s already exists. We'll use it!" % ULContainer
except:
print "The container %s did not yet exit - creating it now!" % ULContainer
cont = cf.create_container(ULContainer)
print "\nUploading the contents of %s to CloudFiles container %s:" % \
(ULdirectory, ULContainer)
upload_key, total_bytes = cf.upload_folder(ULdirectory, cont)
uploaded = 0
while uploaded < total_bytes:
time.sleep(2)
uploaded = cf.get_uploaded(upload_key)
print "Progress: %4.2f%%" % ((uploaded * 100.0) / total_bytes)
print "Done!"
if __name__ == "__main__":
print "\nChallenge3 - Write a script that accepts a directory as an argument"
print "as well as a container name. The script should upload the contents"
print "of the specified directory to the container (or create it if it"
print "doesn't exist). The script should handle errors appropriately."
print "(Check for invalid paths, etc.)\n\n"
parser = argparse.ArgumentParser()
parser.add_argument("Directory",
help="Directory containing files to upload to CONTAINER")
parser.add_argument("Container",
help="CloudFiles container")
parser.add_argument("--region", default='DFW',
help="Region in which to create container (DFW or ORD)")
args = parser.parse_args()
credential_file=os.path.expanduser("~/.rackspace_cloud_credentials")
pyrax.set_credential_file(credential_file)
if c1.is_valid_region(args.region, 'object_store'):
cf = pyrax.connect_to_cloudfiles(region=args.region)
else:
print "The region you requested is not valid: %s" % args.region
sys.exit(2)
ULDir = os.path.expanduser(args.Directory)
ULContainer = args.Container
# pyrax silently ignores 'no premissions to read dir' errors, so we check
# for "read permissions" before calling pyrax
if os.access(ULDir, os.R_OK):
try:
upload_dir_to_container(cf, ULDir, ULContainer)
except pyrax.exceptions.FolderNotFound:
print 'The specified directory "%s" does not exist' % ULDir
else:
print 'ERROR!\nYou do not have read access to directory "%s"' % ULDir
# vim: ts=2 sw=2 tw=78 expandtab