Exemplo n.º 1
0
def upload(local, remote):
  import json
  import ubuntuone.couch.auth as auth
  import mimetypes
  import urllib
 
  # Create remote path (which contains volume path)
  base = "https://one.ubuntu.com/api/file_storage/v1/~/"
  answer = auth.request(base + urllib.quote(remote),
                        http_method="PUT",
                        request_body='{"kind":"file"}')
  node = json.loads(answer[1])
 
  # Read info about local file
  try:
    data = bytearray(open(local, 'rb').read())
    size = len(data)
    content_type = mimetypes.guess_type(local)[0]
    content_type = content_type or 'application/octet-stream'
    headers = {"Content-Length": str(size),
             "Content-Type": content_type}

    # Upload content of local file to content_path from original response
    try: 
      base = "https://files.one.ubuntu.com"
      url = base + urllib.quote(node.get('content_path'), safe="/~")
      auth.request(url, http_method="PUT",
                   headers=headers, request_body=data)
    except TypeError:
      dirs = remote.split('/')
      print dirs[0] + ' is not a volume'

  except IOError as e:
    print local + ":\n\tI/O error({0}): {1}".format(e.errno, e.strerror)
Exemplo n.º 2
0
def put(local, remote):
  import json
  import ubuntuone.couch.auth as auth
  import mimetypes
  import urllib

  # Create remote path (which contains volume path)
  base = "https://one.ubuntu.com/api/file_storage/v1/~/"
  answer = auth.request(base + urllib.quote(remote),
                        http_method="PUT",
                        request_body='{"kind":"file"}')
  node = json.loads(answer[1])

  # Read info about local file
  data = bytearray(open(local, 'rb').read())
  size = len(data)
  content_type = mimetypes.guess_type(local)[0]
  content_type = content_type or 'application/octet-stream'
  headers = {"Content-Length": str(size),
             "Content-Type": content_type}

  # Upload content of local file to content_path from original response
  base = "https://files.one.ubuntu.com"
  url = base + urllib.quote(node.get('content_path'), safe="/~")
  auth.request(url, http_method="PUT",
               headers=headers, request_body=data)
Exemplo n.º 3
0
    def createVolume(self, name):
        import ubuntuone.couch.auth as auth
        import urllib

        try:
            base = "https://one.ubuntu.com/api/file_storage/v1/volumes/~/"
            auth.request(base + urllib.quote(name), http_method="PUT")
            return True
        except:
            return False
Exemplo n.º 4
0
    def get(self, filename, local_path, raise_errors=False):
        """Get file and put in local_path (Path object)"""
        import json
        import ubuntuone.couch.auth as auth
        remote_full = self.meta_base + self.quote(filename)
        answer = auth.request(remote_full)
        self.handle_error(raise_errors, 'get', answer, remote_full, filename)
        node = json.loads(answer[1])

        remote_full = self.content_base + self.quote(node.get('content_path'))
        answer = auth.request(remote_full)
        self.handle_error(raise_errors, 'get', answer, remote_full, filename)
        f = open(local_path.name, 'wb')
        f.write(answer[1])
        local_path.setdata()
Exemplo n.º 5
0
def get(remote, local):
  import json
  import ubuntuone.couch.auth as auth
  import curllib

  # Request metadata
  base = "https://one.ubuntu.com/api/file_storage/v1/~/"
  answer = auth.request(base + urllib.quote(remote))
  node = json.loads(answer[1])

  # Request content
  base = "https://files.one.ubuntu.com"
  url = base + urllib.quote(node.get('content_path'), safe="/~")
  answer = auth.request(url)
  f = open(local, 'wb')
  f.write(answer[1])
Exemplo n.º 6
0
def get_metadata(path,base):
  import json
  import ubuntuone.couch.auth as auth
  import urllib
 
  # Request user metadata
  url = base + urllib.quote(path) + "?include_children=true"
  answer = auth.request(url)
  return answer
Exemplo n.º 7
0
 def delete(self, filename_list, raise_errors=False):
     """Delete all files in filename list"""
     import types
     import ubuntuone.couch.auth as auth
     assert type(filename_list) is not types.StringType
     for filename in filename_list:
         remote_full = self.meta_base + self.quote(filename)
 	    answer = auth.request(remote_full, http_method="DELETE")
         self.handle_error(raise_errors, 'delete', answer, remote_full, ignore=[404])
Exemplo n.º 8
0
def download(remote, local):
    import json
    import ubuntuone.couch.auth as auth
    import urllib
    
    # Request metadata
    base = 'https://one.ubuntu.com/api/file_storage/v1/~/'
    answer = auth.request(base + urllib.quote(remote))
    node = json.loads(answer[1])
    if node.get('content_path'):
      try:
        # Request content
        base = "https://files.one.ubuntu.com"
        url = base + urllib.quote(node.get('content_path'), safe = "/~")
        answer = auth.request(url)
        f = open(local, 'wb')
        f.write(answer[1])
      except:
        print traceback.print_exc()
    else:
      print '%s does not exist' % remote
Exemplo n.º 9
0
def query(path):
  import json
  import ubuntuone.couch.auth as auth
  import urllib
 
  # Request metadata
  base = "https://one.ubuntu.com/api/file_storage/v1/~/"
  url = base + urllib.quote(path)
  answer = auth.request(url)
  node = json.loads(answer[1])
 
  # Print interesting info
  print 'Size:', node.get('size')
Exemplo n.º 10
0
def query(path):
  import json
  import ubuntuone.couch.auth as auth
  import urllib

  # Request metadata
  base = "https://one.ubuntu.com/api/file_storage/v1/~/"
  url = base + urllib.quote(path)
  answer = auth.request(url)
  node = json.loads(answer[1])

  # Print interesting info
  print 'Size:', node.get('size')
Exemplo n.º 11
0
 def list(self, raise_errors=False):
     """List files in that directory"""
     import json
     import ubuntuone.couch.auth as auth
     import urllib
     remote_full = self.meta_base + "?include_children=true"
     answer = auth.request(remote_full)
     self.handle_error(raise_errors, 'list', answer, remote_full)
     filelist = []
     node = json.loads(answer[1])
     if node.get('has_children') == True:
         for child in node.get('children'):
             path = urllib.unquote(child.get('path')).lstrip('/')
             filelist += [path]
     return filelist
Exemplo n.º 12
0
    def put(self, source_path, remote_filename = None, raise_errors=False):
        """Copy file to remote"""
        import json
        import ubuntuone.couch.auth as auth
        import mimetypes
        if not remote_filename:
            remote_filename = source_path.get_filename()
        remote_full = self.meta_base + self.quote(remote_filename)
        answer = auth.request(remote_full,
                              http_method="PUT",
                              request_body='{"kind":"file"}')
        self.handle_error(raise_errors, 'put', answer, source_path.name, remote_full)
        node = json.loads(answer[1])

        remote_full = self.content_base + self.quote(node.get('content_path'))
        data = bytearray(open(source_path.name, 'rb').read())
        size = len(data)
        content_type = mimetypes.guess_type(source_path.name)[0]
        content_type = content_type or 'application/octet-stream'
        headers = {"Content-Length": str(size),
    	           "Content-Type": content_type}
        answer = auth.request(remote_full, http_method="PUT",
                              headers=headers, request_body=data)
        self.handle_error(raise_errors, 'put', answer, source_path.name, remote_full)
Exemplo n.º 13
0
def get_children(path):
  import json
  import ubuntuone.couch.auth as auth
  import urllib

  # Request children metadata
  base = "https://one.ubuntu.com/api/file_storage/v1/~/"
  url = base + urllib.quote(path) + "?include_children=true"
  answer = auth.request(url)

  # Create file list out of json data
  filelist = []
  node = json.loads(answer[1])
  if node.get('has_children') == True:
    for child in node.get('children'):
      child_path = urllib.unquote(child.get('path')).lstrip('/')
      filelist += [child_path]
  print filelist
Exemplo n.º 14
0
    def _query_file_info(self, filename, raise_errors=False):
        """Query attributes on filename"""
        import json
        import ubuntuone.couch.auth as auth
        from duplicity import log
        remote_full = self.meta_base + self.quote(filename)
        answer = auth.request(remote_full)

        code = self.parse_error(answer)
        if code is not None:
            if code == log.ErrorCode.backend_not_found:
                return {'size': -1}
            elif raise_errors:
                self.handle_error(raise_errors, 'query', answer, remote_full, filename)
            else:
                return {'size': None}

        node = json.loads(answer[1])
        size = node.get('size')
        return {'size': size}
Exemplo n.º 15
0
def delete(path):
  import ubuntuone.couch.auth as auth
  import urllib
  base = "https://one.ubuntu.com/api/file_storage/v1/~/"
  auth.request(base + urllib.quote(path), http_method="DELETE")
Exemplo n.º 16
0
def delete(path):
  import ubuntuone.couch.auth as auth
  import urllib
  base = "https://one.ubuntu.com/api/file_storage/v1/~/"
  auth.request(base + urllib.quote(path), http_method="DELETE")
Exemplo n.º 17
0
def create_volume(path):
   import ubuntuone.couch.auth as auth
   import urllib
   base = "https://one.ubuntu.com/api/file_storage/v1/volumes/~/"
   auth.request(base + urllib.quote(path), http_method="PUT")
Exemplo n.º 18
0
def create_volume(path):
  import ubuntuone.couch.auth as auth
  import urllib
  base = "https://one.ubuntu.com/api/file_storage/v1/volumes/~/"
  auth.request(base + urllib.quote(path), http_method="PUT")
Exemplo n.º 19
0
 def create_volume(self, raise_errors=False):
     import ubuntuone.couch.auth as auth
     answer = auth.request(self.volume_uri, http_method="PUT")
     self.handle_error(raise_errors, 'put', answer, self.volume_uri)