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 import roslib.exceptions
40 import os
41 import sys
42 import subprocess
43 import types
44 import tempfile
45 import distutils.version
46
47 if sys.hexversion > 0x03000000:
48 python3 = True
49 else:
50 python3 = False
51
52
54 pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
55 (std_out, std_err) = pop.communicate()
56 if python3:
57 return std_out.decode()
58 else:
59 return std_out
60
62 """
63 Linux: wrapper around lsb_release to get the current OS
64 """
65 try:
66 return _read_stdout(['lsb_release', '-si']).strip()
67 except:
68 return None
69
71 """
72 Linux: wrapper around lsb_release to get the current OS codename
73 """
74 try:
75 return _read_stdout(['lsb_release', '-sc']).strip()
76 except:
77 return None
78
80 """
81 Linux: wrapper around lsb_release to get the current OS version
82 """
83 try:
84 return _read_stdout(['lsb_release', '-sr']).strip()
85 except:
86 return None
87
89 """
90 Linux: wrapper around uname to determine if OS is 64-bit
91 """
92 try:
93 return _read_stdout(['uname', '-m']).strip()
94 except:
95 return None
96
97
98
99
102 self._os_name = "uninitialized from ROS_OS_OVERRIDE=name:version"
103 self._os_version = "uninitialized from ROS_OS_OVERRIDE=name:version"
104
106 try:
107 (self._os_name, self._os_version) = os.environ["ROS_OS_OVERRIDE"].split(':')
108 sys.stderr.write("Using environment variable ROS_OS_OVERRIDE name = %s version = %s\n"%(self._os_name, self._os_version))
109 return True
110 except:
111 return False
112
114 return self._os_version
115
118
119
121
123 """
124 This defines the API used for OS detection within the os_detect
125 module for roslib. All OS specific instantiantions must inherit
126 and override these methods.
127 """
129 """
130 Return if the specific OS which this class is designed to
131 detect is present. Only one version of this class should return for
132 any version.
133 """
134 raise OSDetectException("check_presence unimplemented")
135
137 """
138 Return the standardized name for this OS. (ala Ubuntu Hardy Heron = "ubuntu")
139 """
140 raise OSDetectException("get_name unimplemented")
141
143 """
144 Return the standardized version for this OS. (ala Ubuntu Hardy Heron = "8.04")
145 """
146 raise OSDetectException("get_version unimplemented")
147
148
149
151 """
152 Detect Debian OS.
153 """
155 if "Debian" == lsb_get_os():
156 return True
157 return False
158
163
164
165
166
168 """
169 Detect Mandriva OS. The returned version will be the year release (e.g.
170 2010.0) concatenated with the machine architecture (e.g. x86_64), resulting
171 in something like 2010.0x86_64.
172 """
174 if "MandrivaLinux" == lsb_get_os():
175 return True
176 return False
177
182
183
184
185
186
187
189 """ This is an implementation of a standard interface for
190 interacting with rosdep. This defines all Ubuntu sepecific
191 methods, including detecting the OS/Version number. As well as
192 how to check for and install packages."""
194 if "Ubuntu" == lsb_get_os():
195 return True
196 return False
197
202
203
204
205
207 """
208 Detect Mint variants of Debian.
209 """
211 if "LinuxMint" == lsb_get_os():
212 return True
213 return False
214
217
220
221
222
224 """
225 Detect OpenSuse OS.
226 """
228 try:
229 filename = "/etc/SuSE-brand"
230 if os.path.exists(filename):
231 with open(filename, 'r') as fh:
232 os_list = fh.read().split()
233 if len(os_list) > 0 and os_list[0] == "openSUSE":
234 return True
235 except:
236 pass
237 return False
238
240 try:
241 filename = "/etc/SuSE-brand"
242 if os.path.exists(filename):
243 with open(filename, 'r') as fh:
244 os_list = fh.read().strip().split('\n')
245 if len(os_list) == 2:
246 os_list = os_list[1].split(' = ')
247 if os_list[0] == "VERSION":
248 return os_list[1]
249 except:
250 return False
251
252 return False
253
256
257
258
259
260
262 """
263 Detect Fedora OS.
264 """
266 try:
267 filename = "/etc/redhat-release"
268 if os.path.exists(filename):
269 with open(filename, 'r') as fh:
270 os_list = fh.read().split()
271 if os_list and os_list[0] == "Fedora" and os_list[1] == "release":
272 return True
273 except:
274 pass
275 return False
276
278 try:
279 filename = "/etc/issue"
280 if os.path.exists(filename):
281 with open(filename, 'r') as fh:
282 os_list = fh.read().split()
283 if os_list[0] == "Fedora" and os_list[1] == "release":
284 return os_list[2]
285 except:
286 sys.stderr.write("Fedora failed to get version\n")
287 return False
288
289 return False
290
293
294
295
296
298 """
299 Detect Redhat OS.
300 """
302 try:
303 filename = "/etc/redhat-release"
304 if os.path.exists(filename):
305 with open(filename, 'r') as fh:
306 os_list = fh.read().split()
307 if os_list and os_list[2] == "Enterprise":
308 return True
309 except:
310 pass
311 return False
312
314 try:
315 filename = "/etc/issue"
316 if os.path.exists(filename):
317 with open(filename, 'r') as fh:
318 os_list = fh.read().split()
319 if os_list and os_list[2] == "Enterprise":
320 return os_list[6]
321 except:
322 sys.stderr.write("Rhel failed to get version\n")
323 return False
324
325 return False
326
329
330
331
332
334 """
335 Detect presence of Macports by running "port installed" command.
336 """
337 std_out = _read_stdout(['port', 'installed', p])
338 return (std_out.count("(active)") > 0)
339
341 """
342 Detect OS X
343 """
345 filename = "/usr/bin/sw_vers"
346 if os.path.exists(filename):
347 return True
348 return False
349
351
352 std_out = _read_stdout(['/usr/bin/sw_vers','-productVersion'])
353 ver = distutils.version.StrictVersion(std_out).version
354 if len(ver) < 2:
355 raise OSDetectException("invalid version string: %s"%(std_out))
356 major, minor = ver[0:2]
357
358 if major == 10 and minor == 4:
359 return 'tiger'
360 elif major == 10 and minor == 5:
361 return 'leopard'
362 elif major == 10 and minor == 6:
363 return 'snow'
364 elif major == 10 and minor == 7:
365 return 'lion'
366 else:
367 raise OSDetectException("unrecognized version: %s"%(std_out))
368
371
373 """
374 Detect OS X
375 """
376
380
382
383 std_out = _read_stdout(['/usr/bin/sw_vers','-productVersion'])
384 ver = distutils.version.StrictVersion(std_out).version
385 if len(ver) < 2:
386 raise OSDetectException("invalid version string: %s"%(std_out))
387 major, minor = ver[0:2]
388
389 if major == 10 and minor == 4:
390 return 'tiger'
391 elif major == 10 and minor == 5:
392 return 'leopard'
393 elif major == 10 and minor == 6:
394 return 'snow'
395 elif major == 10 and minor == 7:
396 return 'lion'
397 else:
398 raise OSDetectException("unrecognized version: %s"%(std_out))
399
402
403
404
405
407 """
408 Detect Arch Linux.
409 """
410
412 filename = "/etc/arch-release"
413 if os.path.exists(filename):
414 return True
415 return False
416
419
422
423
424
425
426
428 """
429 Detect Cygwin presence on Windows OS.
430 """
432 filename = "/usr/bin/cygwin1.dll"
433 if os.path.exists(filename):
434 return True
435 return False
436
438 return _read_stdout(['uname','-r']).strip()
439
442
443
444
445
447 """
448 Detect Gentoo OS.
449 """
451 try:
452 filename = "/etc/gentoo-release"
453 if os.path.exists(filename):
454 with open(filename, 'r') as fh:
455 os_list = fh.read().split()
456 if os_list and os_list[0] == "Gentoo" and os_list[1] == "Base":
457 return True
458 except:
459 pass
460 return False
461
463 try:
464 filename = "/etc/gentoo-release"
465 if os.path.exists(filename):
466 with open(filename, 'r') as fh:
467 os_list = fh.read().split()
468 if os_list[0] == "Gentoo" and os_list[1] == "Base":
469 return os_list[4]
470 except:
471 sys.stderr.write("Gentoo failed to get version\n")
472 return False
473
474 return False
475
478
479
480
481
483 """
484 Detect FreeBSD OS.
485 """
487 try:
488 filename = "/usr/bin/uname"
489 if os.path.exists(filename):
490 std_out = _read_stdout([filename])
491 if std_out.strip() == "FreeBSD":
492 return True
493 else:
494 return False
495 except:
496 pass
497 return False
498
500 try:
501 filename = "/usr/bin/uname"
502 if os.path.exists(filename):
503 return _read_stdout([filename, "-r"]).strip()
504 else:
505 return False
506 except:
507 sys.stderr.write("FreeBSD failed to get version\n")
508 return False
509
510 return False
511
514
515
516
517
518
519
520
521
523 """ This class will iterate over registered classes to lookup the
524 active OS and version"""
525 - def __init__(self, os_list = [Debian(), Mandriva(), Ubuntu(), Mint(), Osx(), OsxBrew(), Arch(), OpenSuse(), Fedora(), Rhel(), Gentoo(), Cygwin(), FreeBSD()]):
526 self._os_list = os_list
527 for o in self._os_list:
528 if not isinstance(o, OSBase):
529 raise OSDetectException("Class [%s] not derived from OSBase"%o.__class__.__name__)
530
531 self._os_class = None
532 self._os_name = None
533 self._os_version = None
534
535 self.detect_os()
536
538 self._os_list.append(class_ref)
539
540
542 override = OSOverride()
543 if override.check_presence():
544 for os_class in self._os_list:
545 if os_class.get_name() == override.get_name():
546 self._os_name = override.get_name()
547 self._os_version = override.get_version()
548 self._os_class = os_class
549 return True
550
551 for os_class in self._os_list:
552 if os_class.check_presence():
553 self._os_name = os_class.get_name()
554 self._os_version = os_class.get_version()
555 self._os_class = os_class
556 return True
557
558
559 attempted_oss = [o.get_name() for o in self._os_list]
560 raise OSDetectException("Could not detect OS, tried %s"%attempted_oss)
561 return False
562
564 if not self._os_class:
565 self.detect_os()
566 return self._os_class
567
569 if not self._os_name:
570 self.detect_os()
571 return self._os_name
572
574 if not self._os_version:
575 not self.detect_os()
576 return self._os_version
577