2009-01-19 17:55:00 +00:00
|
|
|
#
|
2009-10-25 13:57:39 +01:00
|
|
|
# misc.py - misc functions for the Ubuntu Developer Tools scripts.
|
2009-01-19 17:55:00 +00:00
|
|
|
#
|
2010-12-24 12:00:03 +02:00
|
|
|
# Copyright (C) 2008, Jonathan Davies <jpds@ubuntu.com>,
|
|
|
|
# 2008-2009, Siegfried-Angel Gevatter Pujals <rainct@ubuntu.com>,
|
|
|
|
# 2010, Stefano Rivera <stefanor@ubuntu.com>
|
2009-01-19 17:55:00 +00:00
|
|
|
#
|
2009-10-25 13:57:39 +01:00
|
|
|
# ##################################################################
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 3
|
|
|
|
# of the License, or (at your option) any later version.
|
2010-12-02 09:33:54 +02:00
|
|
|
#
|
2009-10-25 13:57:39 +01:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2009-01-19 17:55:00 +00:00
|
|
|
#
|
2009-10-25 13:57:39 +01:00
|
|
|
# See file /usr/share/common-licenses/GPL for more details.
|
2009-01-19 17:55:00 +00:00
|
|
|
#
|
2009-10-25 13:57:39 +01:00
|
|
|
# ##################################################################
|
2009-01-19 17:55:00 +00:00
|
|
|
|
|
|
|
# Modules.
|
|
|
|
import os
|
2010-12-24 12:00:03 +02:00
|
|
|
import os.path
|
2010-09-20 18:12:39 +02:00
|
|
|
from subprocess import Popen, PIPE
|
2009-01-19 17:55:00 +00:00
|
|
|
|
2010-03-20 19:45:04 +01:00
|
|
|
from ubuntutools.lp.udtexceptions import PocketDoesNotExistError
|
|
|
|
|
2010-12-02 09:34:09 +02:00
|
|
|
_system_distribution = None
|
2009-10-25 13:57:39 +01:00
|
|
|
def system_distribution():
|
|
|
|
""" system_distro() -> string
|
2010-12-02 09:33:54 +02:00
|
|
|
|
2009-10-25 13:57:39 +01:00
|
|
|
Detect the system's distribution and return it as a string. If the
|
|
|
|
name of the distribution can't be determined, print an error message
|
|
|
|
and return None.
|
|
|
|
"""
|
2010-12-02 09:34:09 +02:00
|
|
|
global _system_distribution
|
|
|
|
if _system_distribution is None:
|
|
|
|
try:
|
|
|
|
if os.path.isfile('/usr/bin/dpkg-vendor'):
|
2010-12-27 22:33:01 +01:00
|
|
|
process = Popen(('dpkg-vendor', '--query', 'vendor'),
|
|
|
|
stdout=PIPE)
|
2010-12-02 09:34:09 +02:00
|
|
|
else:
|
2010-12-27 22:33:01 +01:00
|
|
|
process = Popen(('lsb_release', '-cs'), stdout=PIPE)
|
|
|
|
output = process.communicate()[0]
|
2010-12-02 09:34:09 +02:00
|
|
|
except OSError:
|
2010-12-23 20:42:21 +01:00
|
|
|
print ('Error: Could not determine what distribution you are '
|
|
|
|
'running.')
|
2010-12-02 09:34:09 +02:00
|
|
|
return None
|
2010-12-27 22:33:01 +01:00
|
|
|
if process.returncode != 0:
|
2010-12-02 09:34:09 +02:00
|
|
|
print 'Error determininng system distribution'
|
|
|
|
return None
|
|
|
|
_system_distribution = output.strip()
|
|
|
|
return _system_distribution
|
2009-10-25 13:57:39 +01:00
|
|
|
|
|
|
|
def host_architecture():
|
|
|
|
""" host_architecture -> string
|
2010-12-02 09:33:54 +02:00
|
|
|
|
2010-09-20 18:12:39 +02:00
|
|
|
Detect the host's architecture and return it as a string. If the
|
|
|
|
architecture can't be determined, print an error message and return None.
|
2009-10-25 13:57:39 +01:00
|
|
|
"""
|
2010-12-02 09:33:54 +02:00
|
|
|
|
2010-09-20 18:12:39 +02:00
|
|
|
arch = Popen(['dpkg', '--print-architecture'], stdout=PIPE, \
|
|
|
|
stderr=PIPE).communicate()[0].split()
|
2010-12-02 09:33:54 +02:00
|
|
|
|
2010-09-20 18:12:39 +02:00
|
|
|
if not arch or 'not found' in arch[0]:
|
2009-10-25 13:57:39 +01:00
|
|
|
print 'Error: Not running on a Debian based system; could not ' \
|
|
|
|
'detect its architecture.'
|
|
|
|
return None
|
2010-12-02 09:33:54 +02:00
|
|
|
|
2010-09-20 18:12:39 +02:00
|
|
|
return arch[0]
|
2009-10-25 13:57:39 +01:00
|
|
|
|
2009-01-19 17:55:00 +00:00
|
|
|
def readlist(filename, uniq=True):
|
2009-10-25 13:57:39 +01:00
|
|
|
""" readlist(filename, uniq) -> list
|
2010-12-02 09:33:54 +02:00
|
|
|
|
2009-10-25 13:57:39 +01:00
|
|
|
Read a list of words from the indicated file. If 'uniq' is True, filter
|
|
|
|
out duplicated words.
|
|
|
|
"""
|
2010-12-02 09:33:54 +02:00
|
|
|
|
2009-01-19 17:55:00 +00:00
|
|
|
if not os.path.isfile(filename):
|
|
|
|
print 'File "%s" does not exist.' % filename
|
|
|
|
return False
|
2010-12-02 09:33:54 +02:00
|
|
|
|
2009-01-19 17:55:00 +00:00
|
|
|
content = open(filename).read().replace('\n', ' ').replace(',', ' ')
|
2010-12-02 09:33:54 +02:00
|
|
|
|
2009-01-19 17:55:00 +00:00
|
|
|
if not content.strip():
|
|
|
|
print 'File "%s" is empty.' % filename
|
|
|
|
return False
|
2010-12-02 09:33:54 +02:00
|
|
|
|
2009-01-19 17:55:00 +00:00
|
|
|
items = [item for item in content.split() if item]
|
2010-12-02 09:33:54 +02:00
|
|
|
|
2009-01-19 17:55:00 +00:00
|
|
|
if uniq:
|
|
|
|
items = list(set(items))
|
2010-12-02 09:33:54 +02:00
|
|
|
|
2009-01-19 17:55:00 +00:00
|
|
|
return items
|
2010-03-20 19:45:04 +01:00
|
|
|
|
2010-12-27 22:33:01 +01:00
|
|
|
def split_release_pocket(release):
|
2010-03-20 19:45:04 +01:00
|
|
|
'''Splits the release and pocket name.
|
|
|
|
|
|
|
|
If the argument doesn't contain a pocket name then the 'Release' pocket
|
|
|
|
is assumed.
|
|
|
|
|
|
|
|
Returns the release and pocket name.
|
|
|
|
'''
|
|
|
|
pocket = 'Release'
|
|
|
|
|
|
|
|
if release is None:
|
|
|
|
raise ValueError('No release name specified')
|
|
|
|
|
|
|
|
if '-' in release:
|
|
|
|
(release, pocket) = release.split('-')
|
|
|
|
pocket = pocket.capitalize()
|
|
|
|
|
|
|
|
if pocket not in ('Release', 'Security', 'Updates', 'Proposed',
|
|
|
|
'Backports'):
|
2010-12-23 20:42:21 +01:00
|
|
|
raise PocketDoesNotExistError("Pocket '%s' does not exist." % \
|
|
|
|
pocket)
|
2010-03-20 19:45:04 +01:00
|
|
|
|
|
|
|
return (release, pocket)
|