Package conary :: Package conaryclient :: Module filetypes
[hide private]
[frames] | no frames]

Source Code for Module conary.conaryclient.filetypes

  1  # 
  2  ## Copyright (c) 2008 rPath, Inc. 
  3  # 
  4  ## This program is distributed under the terms of the Common Public License, 
  5  # version 1.0. A copy of this license should have been distributed with this 
  6  # # source file in a file called LICENSE. If it is not present, the license 
  7  # # is always available at http://www.rpath.com/permanent/licenses/CPL-1.0. 
  8  # # 
  9  # # This program is distributed in the hope that it will be useful, but 
 10  # # without any warranty; without even the implied warranty of merchantability 
 11  # # or fitness for a particular purpose. See the Common Public License for 
 12  # # full details. 
 13  # # 
 14   
 15  """ 
 16  This module defines helper classes designed to define filestream objects. 
 17  This allows for creation of filestreams without the need to create files 
 18  on the filesystem. 
 19  """ 
 20   
 21  import itertools 
 22  import os 
 23  import sha 
 24  import time 
 25   
 26  from conary import errors, files 
 27  from conary.deps import deps 
 28  from conary.lib import sha1helper, util 
 29  from conary.repository import filecontents 
 30   
31 -class ParameterError(errors.ClientError):
32 """There were errors with the parameters passed into a filestream helper"""
33
34 -class ConflictingFlags(errors.ClientError):
35 """Config, initialContents and transient are mutually exclusive."""
36
37 -class _File(object):
38 """ 39 Abstract base class which all for other filestream helper classes. 40 41 This class is the parent of all filestream helper classes. It should 42 not be directly instantiated. 43 """ 44 needSha1 = False 45 kwargs = {'perms': 0644, 46 'tags': None, 47 'requires': None, 48 'provides': None, 49 'flavor': None, 50 'owner': 'root', 51 'group': 'root', 52 'mtime': None} 53 #'config': False, 54 #'initialContents': False, 55 #'transient': False, 56 aliasedArgs = {'mode': 'perms'} 57
58 - def __init__(self, **kwargs):
59 assert type(self) != _File 60 for arg in kwargs: 61 aliasedArg = self.aliasedArgs.get(arg, arg) 62 if aliasedArg not in self.kwargs: 63 raise ParameterError("'%s' is not allowed for this class" % arg) 64 65 for key, val in self.aliasedArgs.iteritems(): 66 if key in kwargs and val in kwargs: 67 raise ParameterError( \ 68 "'%s' and '%s' cannot be specified together" % \ 69 (key, val)) 70 elif key in kwargs: 71 kwargs[val] = kwargs[key] 72 73 for key, val in self.__class__.kwargs.iteritems(): 74 setattr(self, key, kwargs.get(key, val)) 75 76 self.mtime = int(self.mtime or time.time()) 77 78 if type(self.requires) == str: 79 self.requires = deps.parseDep(self.requires) 80 elif self.requires is None: 81 self.requires = deps.DependencySet() 82 83 if isinstance(self.provides, str): 84 self.provides = deps.parseDep(self.provides) 85 elif self.provides is None: 86 self.provides = deps.DependencySet() 87 88 if type(self.flavor) == str: 89 self.flavor = deps.parseFlavor(self.flavor) 90 if self.flavor is None: 91 self.flavor = deps.Flavor() 92 93 if self.tags is None: 94 self.tags = []
95
96 - def getContents(self):
97 return None
98
99 - def _touchupFileStream(self, fileStream):
100 pass
101
102 - def get(self, pathId):
103 f = self.fileClass(pathId) 104 f.inode = files.InodeStream(self.perms & 07777, self.mtime, 105 self.owner, self.group) 106 self._touchupFileStream(f) 107 if self.needSha1: 108 sha1 = sha.new() 109 contents = self.contents.get() 110 devnull = open(os.devnull, 'w') 111 util.copyfileobj(contents, devnull, digest = sha1) 112 devnull.close() 113 114 f.contents = files.RegularFileStream() 115 f.contents.size.set(contents.tell()) 116 f.contents.sha1.set(sha1.digest()) 117 f.provides.set(self.provides) 118 f.requires.set(self.requires) 119 f.flavor.set(self.flavor) 120 for tag in self.tags: 121 f.tags.set(tag) 122 return f
123