Package jazzparser :: Package utils :: Module system
[hide private]
[frames] | no frames]

Source Code for Module jazzparser.utils.system

 1  """Utilities relating to the system the software is running on. 
 2   
 3  """ 
 4  """ 
 5  ============================== License ======================================== 
 6   Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding 
 7    
 8   This file is part of The Jazz Parser. 
 9    
10   The Jazz Parser is free software: you can redistribute it and/or modify 
11   it under the terms of the GNU General Public License as published by 
12   the Free Software Foundation, either version 3 of the License, or 
13   (at your option) any later version. 
14    
15   The Jazz Parser is distributed in the hope that it will be useful, 
16   but WITHOUT ANY WARRANTY; without even the implied warranty of 
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
18   GNU General Public License for more details. 
19    
20   You should have received a copy of the GNU General Public License 
21   along with The Jazz Parser.  If not, see <http://www.gnu.org/licenses/>. 
22   
23  ============================ End license ====================================== 
24   
25  """ 
26  __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"  
27   
28  import platform 
29  import subprocess 
30   
31 -def set_proc_title(title):
32 """ 33 Tries to set the current process title. This is very system-dependent 34 and may fail in many cases. 35 36 @return: True if the process succeeds, False if there's an error 37 38 """ 39 try: 40 import ctypes 41 libc = ctypes.CDLL('libc.so.6') 42 retcode = libc.prctl(15, '%s\0' % title, 0, 0, 0) 43 if retcode: 44 return False 45 except: 46 return False 47 else: 48 return True
49
50 -def get_host_info_string():
51 """ 52 Returns a string containing information to identify the system on which 53 you're currently running. This is useful to logging info. Don't try 54 matching against it - you'll be better off getting the components 55 separately. 56 57 """ 58 import socket, os 59 60 # This should usually give the system's hostname 61 hostname = socket.gethostname() 62 # In case this doesn't give a good answer, we also get all the uname info 63 # (we might want to know something more about the system anyway) 64 uname = ", ".join(os.uname()) 65 return "%s (%s)" % (hostname, uname)
66
67 -def open_file(filename):
68 """ 69 Tries to use a system-recommended application to open the given file. 70 Uses C{xdg-open} on Linux, C{start} on Windows and C{open} 71 on Mac. 72 73 """ 74 if is_windows(): 75 cmd = "start" 76 elif is_mac(): 77 cmd = "open" 78 else: 79 # Otherwise assume linux 80 cmd = "xdg-open" 81 # Run the command 82 subprocess.call([cmd, filename])
83
84 -def is_windows():
85 return platform.system().lower() == 'windows'
86 -def is_mac():
87 return platform.system().lower() == 'darwin'
88 -def is_linux():
89 return platform.system().lower() == 'linux'
90