cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
utils.py
Go to the documentation of this file.
1 #!/usr/bin/env python2.7
2 # -*- coding: utf-8 -*-
3 # @COPYRIGHT_begin
4 #
5 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 # @COPYRIGHT_end
20 
21 ##
22 # @package src.ec2
23 #
24 # @copyright Copyright (c) 2012 Institute of Nuclear Physics PAS <http://www.ifj.edu.pl/>
25 # @author Łukasz Chrząszcz <l.chrzaszcz@gmail.com>
26 #
27 import os
28 from ec2.settings import BUCKETS_PATH, USER_QUOTA
29 from ec2.error import InvalidURI
30 
31 
32 def get_user_used_space(user_name):
33  user_path = os.path.join(BUCKETS_PATH, user_name)
34 
35  total_size = 0
36  for dirpath, dirnames, filenames in os.walk(user_path):
37  for f in filenames:
38  fp = os.path.join(dirpath, f)
39  total_size += os.path.getsize(fp)
40 
41  print 'Used space:', total_size
42  return total_size
43 
44 def get_user_free_space(user_name):
45  # config unit is Gb but Content-Length is given in bytes so USER_QUOTA*1024^3
46  free_space = USER_QUOTA*1024*1024*1024 - get_user_used_space(user_name)
47  return free_space
48 
49 def check_user_quota(user_name, chunk_size):
50  print 'Chunk size:', chunk_size
51  free_space = get_user_free_space(user_name)
52  print 'Free space:', free_space
53  print 'chunk_size type:',type(chunk_size)
54  print 'free_space type:',type(free_space)
55  if chunk_size <= free_space:
56  print 'User has enough space'
57  return True
58  print 'NO SPACE'
59  return False
60 
61  # return chunk_size <= free_space
62 
63 def get_path(user_name, path_info):
64 
65  # remove preceding slash to concatenate it with user_name
66  if path_info.startswith('/'):
67  path_info = path_info[1:]
68 
69  # some/bucket/and/object
70 
71  path = os.path.join(user_name, path_info)
72 
73  # user/some/bucket/and/object
74 
75  path = os.path.join(BUCKETS_PATH, path)
76 
77  # /usr/lib/cc1/ec2/storage/buckets/user/some/bucket/and/object - unsafe
78 
79  path = os.path.abspath(path)
80 
81  safe_path = os.path.join(BUCKETS_PATH, user_name)
82 
83  if not path.startswith(safe_path):
84  raise InvalidURI
85 
86  return path
87 
88 def get_bucket_name(path_info):
89  bucket_path = path_info
90  slash = bucket_path.find('/')
91  if slash != -1:
92  bucket_name = bucket_path[:slash+1]
93  else:
94  bucket_name = bucket_path
95  return bucket_name
96