source: anuga_core/source/anuga/utilities/system_tools.py @ 4986

Last change on this file since 4986 was 4986, checked in by steve, 16 years ago

Fixed problem with zlib.crc32 and binascii.crc32 on 64 bit machine,
they gave different answers.

File size: 5.8 KB
Line 
1"""Implementation of tools to do with system administration made as platform independent as possible.
2
3
4"""
5
6import sys
7import os
8
9def get_user_name():
10    """Get user name provide by operating system
11    """
12
13    if sys.platform == 'win32':
14        #user = os.getenv('USERPROFILE')
15        user = os.getenv('USERNAME')
16    else:
17        user = os.getenv('LOGNAME')
18
19
20    return user   
21
22def get_host_name():
23    """Get host name provide by operating system
24    """
25
26    if sys.platform == 'win32':
27        host = os.getenv('COMPUTERNAME')
28    else:
29        host = os.uname()[1]
30
31
32    return host   
33
34def get_revision_number():
35    """Get the version number of the SVN
36    NOTE: This requires that the command svn is on the system PATH
37    (simply aliasing svn to the binary will not work)
38    """
39
40    # Create dummy info
41    #info = 'Revision: Version info could not be obtained.'
42    #info += 'A command line version of svn must be availbable '
43    #info += 'on the system PATH, access to the subversion '
44    #info += 'repository is necessary and the output must '
45    #info += 'contain a line starting with "Revision:"'
46   
47
48    #FIXME (Ole): Change this so that svn info is attempted first.
49    # If that fails, try to read a stored file with that same info (this would be created by e.g. the release script). Failing that, throw an exception.
50
51    #FIXME (Ole): Move this and store_version_info to utilities
52
53
54    try:
55        from anuga.stored_version_info import version_info
56    except:
57        msg = 'No version info stored and command "svn" is not '
58        msg += 'recognised on the system PATH.\n\n'
59        msg += 'If ANUGA has been installed from a distribution e.g. as '
60        msg += 'obtained from SourceForge,\n'
61        msg += 'the version info should be '
62        msg += 'available in the automatically generated file '
63        msg += 'stored_version_info.py\n'
64        msg += 'in the anuga root directory.\n'
65        msg += 'If run from a Subversion sandpit, '
66        msg += 'ANUGA will try to obtain the version info '
67        msg += 'by using the command: "svn info".\n'
68        msg += 'In this case, make sure svn is accessible on the system path. '
69        msg += 'Simply aliasing svn to the binary will not work. '
70        msg += 'Good luck!'
71
72        # No file available - try using Subversion
73        try:
74            # The null stuff is so this section fails quitly.
75            # This could cause the svn info command to fail due to
76            # the redirection being bad on some platforms.
77            # If that occurs then change this code.
78            if sys.platform[0:3] == 'win':
79                fid = os.popen('svn info 2> null')
80            else:
81                fid = os.popen('svn info 2>/dev/null')
82       
83        except:
84            raise Exception(msg)
85        else:
86            #print 'Got version from svn'           
87            version_info = fid.read()
88           
89            if version_info == '':
90                raise Exception(msg)   
91    else:
92        pass
93        #print 'Got version from file'
94
95           
96    for line in version_info.split('\n'):
97        if line.startswith('Revision:'):
98            break
99
100    fields = line.split(':')
101    msg = 'Keyword "Revision" was not found anywhere in text: %s' %version_info
102    assert fields[0].startswith('Revision'), msg           
103
104    try:
105        revision_number = int(fields[1])
106    except:
107        msg = 'Revision number must be an integer. I got %s' %fields[1]
108        msg += 'Check that the command svn is on the system path' 
109        raise Exception(msg)               
110       
111    return revision_number
112
113
114def store_version_info(destination_path='.', verbose=False):
115    """Obtain current version from Subversion and store it.
116   
117    Title: store_version_info()
118
119    Author: Ole Nielsen (Ole.Nielsen@ga.gov.au)
120
121    CreationDate: January 2006
122
123    Description:
124        This function obtains current version from Subversion and stores it
125        is a Python file named 'stored_version_info.py' for use with
126        get_version_info()
127
128        If svn is not available on the system PATH, an Exception is thrown
129    """
130
131    # Note (Ole): This function should not be unit tested as it will only
132    # work when running out of the sandpit. End users downloading the
133    # ANUGA distribution would see a failure.
134    #
135    # FIXME: This function should really only be used by developers (
136    # (e.g. for creating new ANUGA releases), so maybe it should move
137    # to somewhere else.
138   
139    import config
140
141    try:
142        fid = os.popen('svn info')
143    except:
144        msg = 'Command "svn" is not recognised on the system PATH'
145        raise Exception(msg)
146    else:   
147        txt = fid.read()
148        fid.close()
149
150
151        # Determine absolute filename
152        if destination_path[-1] != os.sep:
153            destination_path += os.sep
154           
155        filename = destination_path + config.version_filename
156
157        fid = open(filename, 'w')
158
159        docstring = 'Stored version info.\n\n'
160        docstring += 'This file provides the version for distributions '
161        docstring += 'that are not accessing Subversion directly.\n'
162        docstring += 'The file is automatically generated and should not '
163        docstring += 'be modified manually.\n'
164        fid.write('"""%s"""\n\n' %docstring)
165       
166        fid.write('version_info = """\n%s"""' %txt)
167        fid.close()
168
169
170        if verbose is True:
171            print 'Version info stored to %s' %filename
172           
173
174def compute_checksum(filename, max_length=2**20):
175    """Compute the CRC32 checksum for specified file
176
177    Optional parameter max_length sets the maximum number
178    of bytes used to limit time used with large files.
179    Default = 2**20 (1MB)
180    """
181
182    from zlib import crc32
183    # On bogong binascii.crc32 gave a different answer to
184    # zlib.crc32
185    #from binascii import crc32 #(works as well on 32 bit machines)
186   
187    fid = open(filename)
188    crcval = crc32(fid.read(max_length))
189
190    fid.close()
191    return crcval
Note: See TracBrowser for help on using the repository browser.