Changeset 312

Show
Ignore:
Timestamp:
11/10/08 14:17:17 (2 months ago)
Author:
apdavison
Message:

Bug-fixes to srblib module. Unit tests and doctests now pass.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/doc/testdocs.py

    r128 r312  
    22""" 
    33Script to run doctests. 
     4 
     5Usage: testdocs.py [options] FILE 
     6 
     7Options: 
     8  -h, --help  show this help message and exit 
     9  --strict    Use the original doctest output checker, not the more lax local 
     10              version. 
    411""" 
    512 
  • trunk/doc/utilities.srblib.txt

    r306 r312  
    4444 
    4545    >>> from NeuroTools.utilities.srblib import * 
    46     >>> S = SRBServer('facets.inria.fr') 
     46    >>> S = SRBFileSystem('facets.inria.fr') 
    4747     
    4848(It is also possible to override the contents of ``.MdasEnv``, or even to have 
     
    5555    >>> S.cd("/INRIA/home/INRIA/WP5") 
    5656    >>> S.ls() 
    57     >>> S.get("srb_file", "/tmp/localfile") 
    58     >>> S.put("/tmp/localfile", "srb_file_copy") 
     57    >>> S.get("srbtestfile", "/tmp/localfile") 
     58    >>> S.put("/tmp/localfile", "srbtestfile_copy") 
    5959 
    6060You can also open files and read to/write from them more-or-less as if they were 
    6161on your local filesystem:: 
    6262 
    63     >>> f = S.open("test_file", "w") 
     63    >>> f = S.open("srbtestfile2", "w") 
    6464    >>> f.write("Mate, this bird wouldn't 'voom' if you put four million volts through it!\n") 
    6565    >>> f.close() 
    66     >>> f = S.open("test_file", "r") 
     66    >>> f = S.open("srbtestfile2", "r") 
    6767    >>> print f.read() 
    6868    Mate, this bird wouldn't 'voom' if you put four million volts through it! 
     
    7676"``srb://``" URL to the ``urlopen`` or ``urlretrieve`` functions:: 
    7777 
    78     >>> f = urlopen("srb://facets.inria.fr/INRIA/home/INRIA/WP5/test_file") 
     78    >>> f = urlopen("srb://facets.inria.fr/INRIA/home/INRIA/WP5/srbtestfile2") 
    7979    >>> print f.read() 
    8080    Mate, this bird wouldn't 'voom' if you put four million volts through it! 
  • trunk/src/utilities/srblib.py

    r237 r312  
    108108        self._pathoffset = len(self._root) 
    109109        self.default_resource = default_resource 
     110        if not default_resource: 
     111            if DEFAULT_CONNECTION: 
     112                self.default_resource = DEFAULT_CONNECTION['default_resource'] 
    110113        self.pwd = "/" 
    111114     
     
    159162        else: 
    160163            self.pwd = self.pwd + "/" + dir 
    161         if self.pwd[-1] == "/": # strip off trailing '/' 
    162             self.pwd = self.pwd[:-1] 
    163         if self.pwd == '': 
    164             self.pwd = '/' 
    165      
    166     def open(self, filename, resource='', mode='r'): 
     164        self.pwd = "/" + self.pwd.strip("/") # strip off trailing '/' and make sure there is only a single '/' at the start 
     165     
     166    def open(self, filename, mode='r', resource=''): 
    167167        """ 
    168168        Open a file in the current directory. If the file does not exist, it is 
     
    170170        """ 
    171171        resource = resource or self.default_resource 
    172         return SRBFile(self._connection, os.path.join(self._root, self.pwd[1:], filename), resource, mode) 
     172        return SRBFile(self._connection, os.path.join("/", self._root, self.pwd[1:], filename), resource, mode) 
    173173             
    174174    def get(self, srbfilename, localpath=''): 
     
    193193    def put(self, localpath, srbfilename, resource=''): 
    194194        """ 
    195         Copy a local file to the current directory on the SRB server. 
     195        Copy a local file to the current directory on the SRB server. If a file 
     196        with the same name already exists, it is deleted before the new file is 
     197        copied. 
    196198        """ 
    197199        assert srbfilename.find("/") < 0 # srbfilename should be a file name, not a path 
    198200        resource = resource or self.default_resource 
    199         srbfile = self.open(srbfilename, resource, mode='w')       
     201        self.rm(srbfilename) # this is not very nice. Should really check if a file with the same name already exists 
     202                             # and have a flag which determines whether to raise an Exception or overwrite the file 
     203                             # also, the file should be first copied to a temporary name and only deleted if the upload 
     204                             # succeeds. 
     205        srbfile = self.open(srbfilename, mode='w', resource=resource)       
    200206        local_file = open(localpath, 'r') 
    201207        srbfile.write(local_file.read()) 
     
    211217        (self.path, self.filename) = os.path.split(path) 
    212218        c = self._connection.id 
     219        if mode == 'w': # we delete the existing file first - see comments in "put()" above on how this needs to be improved. 
     220            status = srb.obj_delete(c, self.filename, 0, self.path) 
    213221        # first try to open the file 
    214222        self.id = srb.obj_open(c, self.path, self.filename, 0)