1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 """
32 Library for detecting the current OS, including detecting specific
33 Linux distributions.
34
35 The APIs of this library are still very coupled with the rosdep
36 command-line tool.
37 """
38
39 from __future__ import with_statement
40
41 import roslib.exceptions
42 import roslib.rospack
43 import roslib.stacks
44 import os
45 import sys
46 import subprocess
47 import types
48 import tempfile
49 import yaml
50
51
53 """
54 Linux: wrapper around lsb_release to get the current OS
55 """
56 try:
57 cmd = ['lsb_release', '-si']
58 pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
59 (std_out, std_err) = pop.communicate()
60 return std_out.strip()
61 except:
62 return None
63
65 """
66 Linux: wrapper around lsb_release to get the current OS codename
67 """
68 try:
69 cmd = ['lsb_release', '-sc']
70 pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
71 (std_out, std_err) = pop.communicate()
72 return std_out.strip()
73 except:
74 return None
75
77 """
78 Linux: wrapper around lsb_release to get the current OS version
79 """
80 try:
81 cmd = ['lsb_release', '-sr']
82 pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
83 (std_out, std_err) = pop.communicate()
84 return std_out.strip()
85 except:
86 return None
87
88
91 self._os_name = "uninitialized from ROS_OS_OVERRIDE=name:version"
92 self._os_version = "uninitialized from ROS_OS_OVERRIDE=name:version"
93
95 try:
96 (self._os_name, self._os_version) = os.environ["ROS_OS_OVERRIDE"].split(':')
97 print >> sys.stderr, "Using environment variable ROS_OS_OVERRIDE name = %s version = %s"%(self._os_name, self._os_version)
98 return True
99 except:
100 return False
101
103 return self._os_version
104
107
108
110
112 """
113 This defines the API used for OS detection within the os_detect
114 module for roslib. All OS specific instantiantions must inherit
115 and override these methods.
116 """
118 """
119 Return if the specific OS which this class is designed to
120 detect is present. Only one version of this class should return for
121 any version.
122 """
123 raise OSDetectException("check_presence unimplemented")
124
126 """
127 Return the standardized name for this OS. (ala Ubuntu Hardy Heron = "ubuntu")
128 """
129 raise OSDetectException("get_name unimplemented")
130
132 """
133 Return the standardized version for this OS. (ala Ubuntu Hardy Heron = "8.04")
134 """
135 raise OSDetectException("get_version unimplemented")
136
137
138
140 """
141 Detect Debian OS.
142 """
144 if "Debian" == lsb_get_os():
145 return True
146 return False
147
152
153
154
155
156
158 """ This is an implementation of a standard interface for
159 interacting with rosdep. This defines all Ubuntu sepecific
160 methods, including detecting the OS/Version number. As well as
161 how to check for and install packages."""
163 if "Ubuntu" == lsb_get_os():
164 return True
165 return False
166
171
172
173
174
176 """
177 Detect Mint variants of Debian.
178 """
180 if "LinuxMint" == lsb_get_os():
181 return True
182 return False
183
186
189
190
191
193 """
194 Detect OpenSuse OS.
195 """
197 try:
198 filename = "/etc/SuSE-brand"
199 if os.path.exists(filename):
200 with open(filename, 'r') as fh:
201 os_list = fh.read().split()
202 if len(os_list) > 0 and os_list[0] == "openSUSE":
203 return True
204 except:
205 pass
206 return False
207
209 try:
210 filename = "/etc/SuSE-brand"
211 if os.path.exists(filename):
212 with open(filename, 'r') as fh:
213 os_list = fh.read().strip().split('\n')
214 if len(os_list) == 2:
215 os_list = os_list[1].split(' = ')
216 if os_list[0] == "VERSION":
217 return os_list[1]
218 except:
219 return False
220
221 return False
222
225
226
227
228
229
231 """
232 Detect Fedora OS.
233 """
235 try:
236 filename = "/etc/redhat-release"
237 if os.path.exists(filename):
238 with open(filename, 'r') as fh:
239 os_list = fh.read().split()
240 if os_list and os_list[0] == "Fedora" and os_list[1] == "release":
241 return True
242 except:
243 pass
244 return False
245
247 try:
248 filename = "/etc/issue"
249 if os.path.exists(filename):
250 with open(filename, 'r') as fh:
251 os_list = fh.read().split()
252 if os_list[0] == "Fedora" and os_list[1] == "release":
253 return os_list[2]
254 except:
255 print "Fedora failed to get version"
256 return False
257
258 return False
259
262
263
264
265
267 """
268 Detect Redhat OS.
269 """
271 try:
272 filename = "/etc/redhat-release"
273 if os.path.exists(filename):
274 with open(filename, 'r') as fh:
275 os_list = fh.read().split()
276 if os_list and os_list[2] == "Enterprise":
277 return True
278 except:
279 pass
280 return False
281
283 try:
284 filename = "/etc/issue"
285 if os.path.exists(filename):
286 with open(filename, 'r') as fh:
287 os_list = fh.read().split()
288 if os_list and os_list[2] == "Enterprise":
289 return os_list[6]
290 except:
291 print "Rhel failed to get version"
292 return False
293
294 return False
295
298
299
300
301
303 """
304 Detect presence of Macports by running "port installed" command.
305 """
306 cmd = ['port', 'installed', p]
307 pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
308 (std_out, std_err) = pop.communicate()
309
310 return (std_out.count("(active)") > 0)
311
313 """
314 Detect OS X and Macports.
315 """
317 filename = "/usr/bin/sw_vers"
318 if os.path.exists(filename):
319 return True
320 return False
321
324
327
328
329
330
332 """
333 Detect Arch Linux.
334 """
335
337 filename = "/etc/arch-release"
338 if os.path.exists(filename):
339 return True
340 return False
341
343 return ""
344
345 try:
346 filename = "/etc/issue"
347 if os.path.exists(filename):
348 with open(filename, 'r') as fh:
349 os_list = fh.read().split()
350 if os_list[0] == "Linux" and os_list[1] == "Arch":
351 return os_list[2]
352 except:
353 print "Arch failed to get version"
354 return False
355
356 return False
357
360
361
362
363
364
366 """
367 Detect Cygwin presence on Windows OS.
368 """
370 filename = "/usr/bin/cygwin1.dll"
371 if os.path.exists(filename):
372 return True
373 return False
374
376 cmd = ['uname','-r'];
377 pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
378 (std_out, std_err) = pop.communicate()
379 return std_out.strip()
380
383
384
385
386
388 """
389 Detect Gentoo OS.
390 """
392 try:
393 filename = "/etc/gentoo-release"
394 if os.path.exists(filename):
395 with open(filename, 'r') as fh:
396 os_list = fh.read().split()
397 if os_list and os_list[0] == "Gentoo" and os_list[1] == "Base":
398 return True
399 except:
400 pass
401 return False
402
404 try:
405 filename = "/etc/gentoo-release"
406 if os.path.exists(filename):
407 with open(filename, 'r') as fh:
408 os_list = fh.read().split()
409 if os_list[0] == "Gentoo" and os_list[1] == "Base":
410 return os_list[4]
411 except:
412 print >> sys.stderr, "Gentoo failed to get version"
413 return False
414
415 return False
416
419
420
421
422
424 """
425 Detect FreeBSD OS.
426 """
428 try:
429 filename = "/usr/bin/uname"
430 if os.path.exists(filename):
431 pop = subprocess.Popen([filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
432 (std_out, std_err) = pop.communicate()
433 if std_out.strip() == "FreeBSD":
434 return True
435 else:
436 return False
437 except:
438 pass
439 return False
440
442 try:
443 filename = "/usr/bin/uname"
444 if os.path.exists(filename):
445 pop = subprocess.Popen([filename, "-r"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
446 (std_out, std_err) = pop.communicate()
447 return std_out.strip()
448 else:
449 return False
450 except:
451 print >> sys.stderr, "FreeBSD failed to get version"
452 return False
453
454 return False
455
458
459
460
461
462
463
464
465
467 """ This class will iterate over registered classes to lookup the
468 active OS and version"""
469 - def __init__(self, os_list = [Debian(), Ubuntu(), Mint(), Macports(), Arch(), OpenSuse(), Fedora(), Rhel(), Gentoo(), Cygwin(), FreeBSD()]):
470 self._os_list = os_list
471 for o in self._os_list:
472 if not isinstance(o, OSBase):
473 raise OSDetectException("Class [%s] not derived from OSBase"%o.__class__.__name__)
474
475 self._os_class = None
476 self._os_name = None
477 self._os_version = None
478
479 self.detect_os()
480
482 self._os_list.append(class_ref)
483
484
486 override = OSOverride()
487 if override.check_presence():
488 for os_class in self._os_list:
489 if os_class.get_name() == override.get_name():
490 self._os_name = override.get_name()
491 self._os_version = override.get_version()
492 self._os_class = os_class
493 return True
494
495 for os_class in self._os_list:
496 if os_class.check_presence():
497 self._os_name = os_class.get_name()
498 self._os_version = os_class.get_version()
499 self._os_class = os_class
500 return True
501
502
503 attempted_oss = [o.get_name() for o in self._os_list]
504 raise OSDetectException("Could not detect OS, tried %s"%attempted_oss)
505 return False
506
508 if not self._os_class:
509 self.detect_os()
510 return self._os_class
511
513 if not self._os_name:
514 self.detect_os()
515 return self._os_name
516
518 if not self._os_version:
519 not self.detect_os()
520 return self._os_version
521