2014-01-25 23:13:56 +00:00
|
|
|
#!/usr/bin/python3
|
2013-07-12 17:56:24 +00:00
|
|
|
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
|
2014-01-25 23:13:56 +00:00
|
|
|
# Mugshot - Lightweight user configuration utility
|
|
|
|
# Copyright (C) 2013-2014 Sean Davis <smd.seandavis@gmail.com>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify it
|
2014-07-28 00:07:47 +00:00
|
|
|
# under the terms of the GNU General Public License as published by
|
2014-08-24 16:14:29 +00:00
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
2014-07-28 00:07:47 +00:00
|
|
|
# (at your option) any later version.
|
2014-01-25 23:13:56 +00:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2014-07-27 23:55:15 +00:00
|
|
|
import subprocess
|
2013-07-12 17:56:24 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import DistUtilsExtra.auto
|
|
|
|
except ImportError:
|
2014-01-25 23:13:56 +00:00
|
|
|
sys.stderr.write("To build mugshot you need "
|
2014-04-02 01:55:50 +00:00
|
|
|
"https://launchpad.net/python-distutils-extra\n")
|
2013-07-12 17:56:24 +00:00
|
|
|
sys.exit(1)
|
2014-01-25 23:13:56 +00:00
|
|
|
assert DistUtilsExtra.auto.__version__ >= '2.18', \
|
2014-04-02 01:55:50 +00:00
|
|
|
'needs DistUtilsExtra.auto >= 2.18'
|
2013-07-12 17:56:24 +00:00
|
|
|
|
|
|
|
|
2014-01-25 23:13:56 +00:00
|
|
|
def update_config(libdir, values={}):
|
|
|
|
"""Update the configuration file at installation time."""
|
|
|
|
filename = os.path.join(libdir, 'mugshot_lib', 'mugshotconfig.py')
|
2013-07-12 17:56:24 +00:00
|
|
|
oldvalues = {}
|
|
|
|
try:
|
2014-01-25 23:13:56 +00:00
|
|
|
fin = open(filename, 'r')
|
|
|
|
fout = open(filename + '.new', 'w')
|
2013-07-12 17:56:24 +00:00
|
|
|
|
|
|
|
for line in fin:
|
2014-01-25 23:13:56 +00:00
|
|
|
fields = line.split(' = ') # Separate variable from value
|
2013-07-12 17:56:24 +00:00
|
|
|
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)
|
2014-01-25 23:13:56 +00:00
|
|
|
except (OSError, IOError):
|
|
|
|
print(("ERROR: Can't find %s" % filename))
|
2013-07-12 17:56:24 +00:00
|
|
|
sys.exit(1)
|
|
|
|
return oldvalues
|
2014-01-25 23:13:56 +00:00
|
|
|
|
|
|
|
|
2013-07-17 19:57:29 +00:00
|
|
|
def move_icon_file(root, target_data, prefix):
|
2014-01-25 23:13:56 +00:00
|
|
|
"""Move the icon files to their installation prefix."""
|
2014-04-02 01:55:50 +00:00
|
|
|
old_icon_path = os.path.normpath(os.path.join(root, target_data, 'share',
|
|
|
|
'mugshot', 'media'))
|
2014-01-25 23:40:38 +00:00
|
|
|
for icon_size in ['16x16', '22x22', '24x24', '48x48', '64x64', 'scalable']:
|
2013-07-17 20:18:45 +00:00
|
|
|
if icon_size == 'scalable':
|
2014-01-25 23:13:56 +00:00
|
|
|
old_icon_file = os.path.join(old_icon_path, 'mugshot.svg')
|
2013-07-17 20:18:45 +00:00
|
|
|
else:
|
2014-01-25 23:13:56 +00:00
|
|
|
old_icon_file = os.path.join(old_icon_path,
|
2014-04-02 01:55:50 +00:00
|
|
|
'mugshot_%s.svg' %
|
|
|
|
icon_size.split('x')[0])
|
|
|
|
icon_path = os.path.normpath(os.path.join(root, target_data, 'share',
|
|
|
|
'icons', 'hicolor', icon_size, 'apps'))
|
2014-01-25 23:13:56 +00:00
|
|
|
icon_file = os.path.join(icon_path, 'mugshot.svg')
|
|
|
|
old_icon_file = os.path.realpath(old_icon_file)
|
|
|
|
icon_file = os.path.realpath(icon_file)
|
|
|
|
|
2013-07-17 20:18:45 +00:00
|
|
|
if not os.path.exists(old_icon_file):
|
2014-01-25 23:13:56 +00:00
|
|
|
print(("ERROR: Can't find", old_icon_file))
|
2013-07-17 20:18:45 +00:00
|
|
|
sys.exit(1)
|
|
|
|
if not os.path.exists(icon_path):
|
|
|
|
os.makedirs(icon_path)
|
|
|
|
if old_icon_file != icon_file:
|
2014-01-25 23:13:56 +00:00
|
|
|
print(("Moving icon file: %s -> %s" % (old_icon_file, icon_file)))
|
2013-07-17 20:18:45 +00:00
|
|
|
os.rename(old_icon_file, icon_file)
|
2013-07-12 17:56:24 +00:00
|
|
|
|
2014-01-25 23:13:56 +00:00
|
|
|
# Media is now empty
|
|
|
|
if len(os.listdir(old_icon_path)) == 0:
|
2014-01-26 00:44:26 +00:00
|
|
|
print(("Removing empty directory: %s" % old_icon_path))
|
2014-01-25 23:13:56 +00:00
|
|
|
os.rmdir(old_icon_path)
|
2013-07-12 17:56:24 +00:00
|
|
|
|
2014-01-25 23:13:56 +00:00
|
|
|
return icon_file
|
2013-07-12 17:56:24 +00:00
|
|
|
|
|
|
|
|
2014-01-25 23:13:56 +00:00
|
|
|
def get_desktop_file(root, target_data, prefix):
|
|
|
|
"""Move the desktop file to its installation prefix."""
|
2014-04-02 01:55:50 +00:00
|
|
|
desktop_path = os.path.realpath(os.path.join(root, target_data, 'share',
|
|
|
|
'applications'))
|
2014-01-25 23:13:56 +00:00
|
|
|
desktop_file = os.path.join(desktop_path, 'mugshot.desktop')
|
2013-07-12 17:56:24 +00:00
|
|
|
return desktop_file
|
|
|
|
|
|
|
|
|
2014-01-25 23:13:56 +00:00
|
|
|
def update_desktop_file(filename, script_path):
|
|
|
|
"""Update the desktop file with prefixed paths."""
|
2013-07-12 17:56:24 +00:00
|
|
|
try:
|
2014-01-25 23:13:56 +00:00
|
|
|
fin = open(filename, 'r')
|
|
|
|
fout = open(filename + '.new', 'w')
|
2013-07-12 17:56:24 +00:00
|
|
|
|
|
|
|
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)
|
2014-01-25 23:13:56 +00:00
|
|
|
line = "Exec=%s" % os.path.join(script_path, 'mugshot')
|
2013-07-12 17:56:24 +00:00
|
|
|
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)
|
2014-01-25 23:13:56 +00:00
|
|
|
except (OSError, IOError):
|
|
|
|
print(("ERROR: Can't find %s" % filename))
|
2013-07-12 17:56:24 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2014-07-27 23:55:15 +00:00
|
|
|
def write_appdata_file(filename_in):
|
|
|
|
filename_out = filename_in.rstrip('.in')
|
|
|
|
cmd = ["intltool-merge", "-x", "-d", "po", filename_in, filename_out]
|
|
|
|
print(" ".join(cmd))
|
|
|
|
subprocess.call(cmd, shell=False)
|
|
|
|
|
|
|
|
|
|
|
|
# Update AppData with latest translations first.
|
|
|
|
write_appdata_file("data/appdata/mugshot.appdata.xml.in")
|
|
|
|
|
|
|
|
|
2013-07-12 17:56:24 +00:00
|
|
|
class InstallAndUpdateDataDirectory(DistUtilsExtra.auto.install_auto):
|
2015-09-01 02:31:02 +00:00
|
|
|
|
2014-01-25 23:13:56 +00:00
|
|
|
"""Command Class to install and update the directory."""
|
2015-09-01 02:31:02 +00:00
|
|
|
|
2013-07-12 17:56:24 +00:00
|
|
|
def run(self):
|
2014-01-25 23:13:56 +00:00
|
|
|
"""Run the setup commands."""
|
2013-07-12 17:56:24 +00:00
|
|
|
DistUtilsExtra.auto.install_auto.run(self)
|
2014-01-25 23:13:56 +00:00
|
|
|
|
|
|
|
print(("=== Installing %s, version %s ===" %
|
2014-04-02 01:55:50 +00:00
|
|
|
(self.distribution.get_name(), self.distribution.get_version())))
|
2014-01-25 23:13:56 +00:00
|
|
|
|
|
|
|
if not self.prefix:
|
|
|
|
self.prefix = ''
|
|
|
|
|
|
|
|
if self.root:
|
2014-04-02 01:55:50 +00:00
|
|
|
target_data = os.path.relpath(self.install_data, self.root) + \
|
|
|
|
os.sep
|
2014-01-25 23:13:56 +00:00
|
|
|
target_pkgdata = os.path.join(target_data, 'share', 'mugshot', '')
|
|
|
|
target_scripts = os.path.join(self.install_scripts, '')
|
|
|
|
|
|
|
|
data_dir = os.path.join(self.prefix, 'share', 'mugshot', '')
|
|
|
|
script_path = os.path.join(self.prefix, 'bin')
|
|
|
|
else:
|
|
|
|
# --user install
|
2013-07-17 15:32:43 +00:00
|
|
|
self.root = ''
|
2014-01-25 23:13:56 +00:00
|
|
|
target_data = os.path.relpath(self.install_data) + os.sep
|
|
|
|
target_pkgdata = os.path.join(target_data, 'share', 'mugshot', '')
|
|
|
|
target_scripts = os.path.join(self.install_scripts, '')
|
|
|
|
|
|
|
|
# Use absolute paths
|
|
|
|
target_data = os.path.realpath(target_data)
|
|
|
|
target_pkgdata = os.path.realpath(target_pkgdata)
|
|
|
|
target_scripts = os.path.realpath(target_scripts)
|
2013-07-12 17:56:24 +00:00
|
|
|
|
2014-01-25 23:13:56 +00:00
|
|
|
data_dir = target_pkgdata
|
|
|
|
script_path = target_scripts
|
2013-07-12 17:56:24 +00:00
|
|
|
|
2014-01-25 23:13:56 +00:00
|
|
|
print(("Root: %s" % self.root))
|
|
|
|
print(("Prefix: %s\n" % self.prefix))
|
|
|
|
|
|
|
|
print(("Target Data: %s" % target_data))
|
|
|
|
print(("Target PkgData: %s" % target_pkgdata))
|
|
|
|
print(("Target Scripts: %s\n" % target_scripts))
|
|
|
|
print(("Mugshot Data Directory: %s" % data_dir))
|
|
|
|
|
|
|
|
values = {'__mugshot_data_directory__': "'%s'" % (data_dir),
|
2013-07-12 17:56:24 +00:00
|
|
|
'__version__': "'%s'" % self.distribution.get_version()}
|
|
|
|
update_config(self.install_lib, values)
|
|
|
|
|
2014-01-25 23:13:56 +00:00
|
|
|
desktop_file = get_desktop_file(self.root, target_data, self.prefix)
|
|
|
|
print(("Desktop File: %s\n" % desktop_file))
|
|
|
|
move_icon_file(self.root, target_data, self.prefix)
|
|
|
|
update_desktop_file(desktop_file, script_path)
|
2013-07-12 17:56:24 +00:00
|
|
|
|
|
|
|
DistUtilsExtra.auto.setup(
|
|
|
|
name='mugshot',
|
2014-08-31 11:04:32 +00:00
|
|
|
version='0.2.5',
|
2014-07-28 00:14:45 +00:00
|
|
|
license='GPL-3+',
|
2013-07-17 16:53:18 +00:00
|
|
|
author='Sean Davis',
|
|
|
|
author_email='smd.seandavis@gmail.com',
|
2014-01-25 23:13:56 +00:00
|
|
|
description='lightweight user configuration utility',
|
|
|
|
long_description='A lightweight user configuration utility. It allows you '
|
|
|
|
'to easily set profile image and user details for your '
|
|
|
|
'user profile and any supported applications.',
|
2013-07-17 16:53:18 +00:00
|
|
|
url='https://launchpad.net/mugshot',
|
2014-07-27 23:55:15 +00:00
|
|
|
data_files=[('share/man/man1', ['mugshot.1']),
|
|
|
|
('share/appdata', ['data/appdata/mugshot.appdata.xml'])],
|
2013-07-12 17:56:24 +00:00
|
|
|
cmdclass={'install': InstallAndUpdateDataDirectory}
|
2015-09-01 02:31:02 +00:00
|
|
|
)
|