Changeset 7486


Ignore:
Timestamp:
Sep 7, 2009, 12:15:04 PM (11 years ago)
Author:
ole
Message:

Reverted changeset:6860 as there are too many types of output from svnversion.
We are now back to svn info.
Also moved nested function declarations out and made them Python style internals using
Added simple unit test of this functionality.

Location:
anuga_core/source/anuga/utilities
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/utilities/system_tools.py

    r7483 r7486  
    5656    return host   
    5757
     58   
     59   
     60   
     61   
     62def __get_revision_from_svn_entries__():
     63    """Get a subversion revision number from the .svn/entries file."""
     64
     65   
     66    msg = '''
     67No version info stored and command 'svn' is not recognised on the system PATH.
     68
     69If ANUGA has been installed from a distribution e.g. as obtained from SourceForge,
     70the version info should be available in the automatically generated file
     71'stored_version_info.py' in the anuga root directory.
     72
     73If run from a Subversion sandpit, ANUGA will try to obtain the version info by
     74using the command 'svn info'.  In this case, make sure the command line client
     75'svn' is accessible on the system path.  Simply aliasing 'svn' to the binary will
     76not work.
     77
     78If you are using Windows, you have to install the file svn.exe which can be
     79obtained from http://www.collab.net/downloads/subversion.
     80
     81Good luck!
     82'''
     83
     84    try:
     85        fd = open(os.path.join('.svn', 'entries'))
     86    except:
     87        raise Exception, msg
     88
     89    line = fd.readlines()[3]
     90    fd.close()
     91    try:
     92        revision_number = int(line)
     93    except:
     94        msg = ".svn/entries, line 4 was '%s'?" % line.strip()
     95        raise Exception, msg
     96
     97    return revision_number
     98
     99def __get_revision_from_svn_client__():
     100    """Get a subversion revision number from an svn client."""
     101
     102    if sys.platform[0:3] == 'win':
     103        try:
     104            fid = os.popen(r'C:\Program Files\TortoiseSVN\bin\SubWCRev.exe')
     105        except:
     106            return __get_revision_from_svn_entries__()
     107        else:
     108            version_info = fid.read()
     109            if version_info == '':
     110                return __get_revision_from_svn_entries__()
     111
     112        # split revision number from data
     113        for line in version_info.split('\n'):
     114            if line.startswith('Updated to revision '):
     115                break
     116
     117        fields = line.split(' ')
     118        msg = 'Keyword "Revision" was not found anywhere in text: %s' % version_info
     119        assert fields[0].startswith('Updated'), msg
     120
     121        try:
     122            revision_number = int(fields[3])
     123        except:
     124            msg = ('Revision number must be an integer. I got "%s" from '
     125                   '"SubWCRev.exe".' % fields[3])
     126            raise Exception, msg
     127    else:                   # assume Linux
     128        try:
     129            fid = os.popen('svn info . 2>/dev/null')
     130        except:
     131            return __get_revision_from_svn_entries__()
     132        else:
     133            version_info = fid.read()
     134            if version_info == '':
     135                return __get_revision_from_svn_entries__()
     136
     137        # Split revision number from data
     138        for line in version_info.split('\n'):
     139            if line.startswith('Revision:'):
     140                break
     141       
     142        fields = line.split(':')
     143        msg = 'Keyword "Revision" was not found anywhere in text: %s' % version_info
     144        assert fields[0].startswith('Revision'), msg
     145       
     146       
     147        #if ':' in version_info:
     148        #    revision_number, _ = version_info.split(':', 1)
     149        #    msg = ('Some modules have not been checked in. '
     150        #           'Using last version from repository: %s' % revision_number)
     151        #    warnings.warn(msg)
     152        #else:
     153        #    revision_number = version_info
     154
     155        try:
     156            revision_number = int(fields[1])
     157        except:
     158            msg = ("Revision number must be an integer. I got '%s' from "
     159                   "'svn'." % fields[1])
     160            raise Exception, msg
     161
     162    return revision_number
     163
     164   
     165   
     166   
     167   
    58168def get_revision_number():
    59169    """Get the version number of this repository copy.
     
    68178    """
    69179
    70     def get_revision_from_svn_entries():
    71         '''Get a subversion revision number from the .svn/entries file.'''
    72 
    73         msg = '''
    74 No version info stored and command 'svn' is not recognised on the system PATH.
    75 
    76 If ANUGA has been installed from a distribution e.g. as obtained from SourceForge,
    77 the version info should be available in the automatically generated file
    78 'stored_version_info.py' in the anuga root directory.
    79 
    80 If run from a Subversion sandpit, ANUGA will try to obtain the version info by
    81 using the command 'svn info'.  In this case, make sure the command line client
    82 'svn' is accessible on the system path.  Simply aliasing 'svn' to the binary will
    83 not work.
    84 
    85 If you are using Windows, you have to install the file svn.exe which can be
    86 obtained from http://www.collab.net/downloads/subversion.
    87 
    88 Good luck!
    89 '''
    90 
    91         try:
    92             fd = open(os.path.join('.svn', 'entries'))
    93         except:
    94             raise Exception, msg
    95 
    96         line = fd.readlines()[3]
    97         fd.close()
    98         try:
    99             revision_number = int(line)
    100         except:
    101             msg = ".svn/entries, line 4 was '%s'?" % line.strip()
    102             raise Exception, msg
    103 
    104         return revision_number
    105 
    106     def get_revision_from_svn_client():
    107         '''Get a subversion revision number from an svn client.'''
    108 
    109         if sys.platform[0:3] == 'win':
    110             try:
    111                 fid = os.popen(r'C:\Program Files\TortoiseSVN\bin\SubWCRev.exe')
    112             except:
    113                 return get_revision_from_svn_entries()
    114             else:
    115                 version_info = fid.read()
    116                 if version_info == '':
    117                     return get_revision_from_svn_entries()
    118 
    119             # split revision number from data
    120             for line in version_info.split('\n'):
    121                 if line.startswith('Updated to revision '):
    122                     break
    123 
    124             fields = line.split(' ')
    125             msg = 'Keyword "Revision" was not found anywhere in text: %s' % version_info
    126             assert fields[0].startswith('Updated'), msg
    127 
    128             try:
    129                 revision_number = int(fields[3])
    130             except:
    131                 msg = ("Revision number must be an integer. I got '%s' from "
    132                        "'SubWCRev.exe'." % fields[3])
    133                 raise Exception, msg
    134         else:                   # assume Linux
    135             try:
    136                 fid = os.popen('svnversion -n . 2>/dev/null')
    137             except:
    138                 return get_revision_from_svn_entries()
    139             else:
    140                 version_info = fid.read()
    141                 if version_info == '':
    142                     return get_revision_from_svn_entries()
    143 
    144             # Split revision number from data
    145             if ':' in version_info:
    146                 revision_number, _ = version_info.split(':',1)
    147                 msg = ('Some modules have not been checked in. '
    148                        'Using last version from repository: %s' % revision_number)
    149                 warnings.warn(msg)
    150             else:
    151                 revision_number = version_info
    152 
    153             try:
    154                 revision_number = int(revision_number)
    155             except:
    156                 msg = ("Revision number must be an integer. I got '%s' from "
    157                        "'svn'." % version_info)
    158                 raise Exception, msg
    159 
    160         return revision_number
    161 
    162180    # try to get revision information from stored_version_info.py
    163181    try:
    164182        from anuga.stored_version_info import version_info
    165183    except:
    166         return get_revision_from_svn_client()
     184        return __get_revision_from_svn_client__()
    167185
    168186    # split revision number from data
  • anuga_core/source/anuga/utilities/test_system_tools.py

    r7276 r7486  
    461461        msg = 'Expected file_length() to return 1000, but got %d' % size4
    462462        self.failUnless(size4 == 1000, msg)
     463       
     464       
     465       
     466    def test_get_revision_number(self):
     467        """test_get_revision_number
     468       
     469        Test that a revision number is returned.
     470        This should work both from a sandpit with access to Subversion
     471        and also in distributions where revision number has been stored
     472        explicitly in anuga.stored_version_info.version_info
     473        """
     474
     475        x = get_revision_number()
     476        assert int(x)
    463477
    464478################################################################################
Note: See TracChangeset for help on using the changeset viewer.