OSG_Mugshot_fork/setup.py

174 lines
6.7 KiB
Python
Raw Normal View History

2013-07-12 17:56:24 +00:00
#!/usr/bin/env python
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
2013-07-13 15:09:49 +00:00
# Copyright (C) 2013 Sean Davis <smd.seandavis@gmail.com>
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
2013-07-12 17:56:24 +00:00
### END LICENSE
###################### DO NOT TOUCH THIS (HEAD TO THE SECOND PART) ######################
import os
import sys
try:
import DistUtilsExtra.auto
except ImportError:
print >> sys.stderr, 'To build mugshot you need https://launchpad.net/python-distutils-extra'
sys.exit(1)
assert DistUtilsExtra.auto.__version__ >= '2.18', 'needs DistUtilsExtra.auto >= 2.18'
def update_config(libdir, values = {}):
filename = os.path.join(libdir, 'mugshot_lib/mugshotconfig.py')
oldvalues = {}
try:
fin = file(filename, 'r')
fout = file(filename + '.new', 'w')
for line in fin:
fields = line.split(' = ') # Separate variable from value
if fields[0] in values:
oldvalues[fields[0]] = fields[1].strip()
line = "%s = %s\n" % (fields[0], values[fields[0]])
fout.write(line)
fout.flush()
fout.close()
fin.close()
os.rename(fout.name, fin.name)
except (OSError, IOError), e:
print ("ERROR: Can't find %s" % filename)
sys.exit(1)
return oldvalues
def move_icon_file(root, target_data, prefix):
old_icon_path = os.path.normpath(root + target_data + '/share/mugshot/media')
2013-07-18 13:10:02 +00:00
for icon_size in ['16x16', '22x22', '24x24', '48x48', '64x64', 'scalable']:
2013-07-17 20:18:45 +00:00
if icon_size == 'scalable':
old_icon_file = old_icon_path + '/mugshot.svg'
else:
old_icon_file = old_icon_path + '/mugshot_%s.svg' % icon_size.split('x')[0]
icon_path = os.path.normpath(root + prefix + '/share/icons/hicolor/%s/apps' % icon_size)
icon_file = icon_path + '/mugshot.svg'
if not os.path.exists(old_icon_file):
print ("ERROR: Can't find", old_icon_file)
sys.exit(1)
if not os.path.exists(icon_path):
os.makedirs(icon_path)
if old_icon_file != icon_file:
os.rename(old_icon_file, icon_file)
return icon_file
2013-07-12 17:56:24 +00:00
def move_desktop_file(root, target_data, prefix):
# The desktop file is rightly installed into install_data. But it should
# always really be installed into prefix, because while we can install
# normal data files anywhere we want, the desktop file needs to exist in
# the main system to be found. Only actually useful for /opt installs.
old_desktop_path = os.path.normpath(root + target_data +
'/share/applications')
old_desktop_file = old_desktop_path + '/mugshot.desktop'
desktop_path = os.path.normpath(root + prefix + '/share/applications')
desktop_file = desktop_path + '/mugshot.desktop'
if not os.path.exists(old_desktop_file):
print ("ERROR: Can't find", old_desktop_file)
sys.exit(1)
elif target_data != prefix + '/':
# This is an /opt install, so rename desktop file to use extras-
desktop_file = desktop_path + '/extras-mugshot.desktop'
try:
2013-07-17 15:32:43 +00:00
if not os.path.exists(desktop_path):
os.makedirs(desktop_path)
if old_desktop_file != desktop_file:
os.rename(old_desktop_file, desktop_file)
for filename in os.listdir(old_desktop_path):
os.remove( os.path.join(old_desktop_path, filename) )
2013-07-12 17:56:24 +00:00
os.rmdir(old_desktop_path)
except OSError as e:
print ("ERROR: Can't rename", old_desktop_file, ":", e)
sys.exit(1)
return desktop_file
def update_desktop_file(filename, target_pkgdata, target_scripts):
try:
fin = file(filename, 'r')
fout = file(filename + '.new', 'w')
for line in fin:
2013-07-17 17:17:16 +00:00
if 'Exec=' in line:
2013-07-12 17:56:24 +00:00
cmd = line.split("=")[1].split(None, 1)
line = "Exec=%s" % (target_scripts + 'mugshot')
if len(cmd) > 1:
line += " %s" % cmd[1].strip() # Add script arguments back
line += "\n"
fout.write(line)
fout.flush()
fout.close()
fin.close()
os.rename(fout.name, fin.name)
except (OSError, IOError), e:
print ("ERROR: Can't find %s" % filename)
sys.exit(1)
def compile_schemas(root, target_data):
if target_data == '/usr/':
return # /usr paths don't need this, they will be handled by dpkg
schemadir = os.path.normpath(root + target_data + 'share/glib-2.0/schemas')
if (os.path.isdir(schemadir) and
os.path.isfile('/usr/bin/glib-compile-schemas')):
os.system('/usr/bin/glib-compile-schemas "%s"' % schemadir)
class InstallAndUpdateDataDirectory(DistUtilsExtra.auto.install_auto):
def run(self):
DistUtilsExtra.auto.install_auto.run(self)
2013-07-17 15:32:43 +00:00
if not self.root:
self.root = ''
2013-07-12 17:56:24 +00:00
target_data = '/' + os.path.relpath(self.install_data, self.root) + '/'
target_pkgdata = target_data + 'share/mugshot/'
target_scripts = '/' + os.path.relpath(self.install_scripts, self.root) + '/'
values = {'__mugshot_data_directory__': "'%s'" % (target_pkgdata),
'__version__': "'%s'" % self.distribution.get_version()}
update_config(self.install_lib, values)
desktop_file = move_desktop_file(self.root, target_data, self.prefix)
icon_file = move_icon_file(self.root, target_data, self.prefix)
2013-07-12 17:56:24 +00:00
update_desktop_file(desktop_file, target_pkgdata, target_scripts)
compile_schemas(self.root, target_data)
##################################################################################
###################### YOU SHOULD MODIFY ONLY WHAT IS BELOW ######################
##################################################################################
DistUtilsExtra.auto.setup(
name='mugshot',
version='0.1',
2013-07-13 15:09:49 +00:00
license='GPL-3',
2013-07-17 16:53:18 +00:00
author='Sean Davis',
author_email='smd.seandavis@gmail.com',
2013-07-12 17:56:24 +00:00
#description='UI for managing …',
#long_description='Here a longer description',
2013-07-17 16:53:18 +00:00
url='https://launchpad.net/mugshot',
2013-07-12 17:56:24 +00:00
cmdclass={'install': InstallAndUpdateDataDirectory}
)