~apandada1/typhoon/quickly_trunk

« back to all changes in this revision

Viewing changes to setup.py

  • Committer: Archisman Panigrahi
  • Date: 2014-05-17 16:43:45 UTC
  • Revision ID: apandada1@gmail.com-20140517164345-jren62tak20ng151
commit before release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
3
### BEGIN LICENSE
 
4
# Copyright (C) 2014 Archisman Panigrahi <apandada1@gmail.com>
 
5
# Thanks to Adam Whitcroft <adamwhitcroft.com> for Climacons!
 
6
# This program is free software: you can redistribute it and/or modify it 
 
7
# under the terms of the GNU General Public License version 3, as published 
 
8
# by the Free Software Foundation.
 
9
 
10
# This program is distributed in the hope that it will be useful, but 
 
11
# WITHOUT ANY WARRANTY; without even the implied warranties of 
 
12
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
 
13
# PURPOSE.  See the GNU General Public License for more details.
 
14
 
15
# You should have received a copy of the GNU General Public License along 
 
16
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
### END LICENSE
 
18
 
 
19
###################### DO NOT TOUCH THIS (HEAD TO THE SECOND PART) ######################
 
20
 
 
21
import os
 
22
import sys
 
23
 
 
24
try:
 
25
    import DistUtilsExtra.auto
 
26
except ImportError:
 
27
    print >> sys.stderr, 'To build typhoon you need https://launchpad.net/python-distutils-extra'
 
28
    sys.exit(1)
 
29
assert DistUtilsExtra.auto.__version__ >= '2.18', 'needs DistUtilsExtra.auto >= 2.18'
 
30
 
 
31
def update_config(libdir, values = {}):
 
32
 
 
33
    filename = os.path.join(libdir, 'typhoon_lib/typhoonconfig.py')
 
34
    oldvalues = {}
 
35
    try:
 
36
        fin = file(filename, 'r')
 
37
        fout = file(filename + '.new', 'w')
 
38
 
 
39
        for line in fin:
 
40
            fields = line.split(' = ') # Separate variable from value
 
41
            if fields[0] in values:
 
42
                oldvalues[fields[0]] = fields[1].strip()
 
43
                line = "%s = %s\n" % (fields[0], values[fields[0]])
 
44
            fout.write(line)
 
45
 
 
46
        fout.flush()
 
47
        fout.close()
 
48
        fin.close()
 
49
        os.rename(fout.name, fin.name)
 
50
    except (OSError, IOError), e:
 
51
        print ("ERROR: Can't find %s" % filename)
 
52
        sys.exit(1)
 
53
    return oldvalues
 
54
 
 
55
 
 
56
def move_desktop_file(root, target_data, prefix):
 
57
    # The desktop file is rightly installed into install_data.  But it should
 
58
    # always really be installed into prefix, because while we can install
 
59
    # normal data files anywhere we want, the desktop file needs to exist in
 
60
    # the main system to be found.  Only actually useful for /opt installs.
 
61
 
 
62
    old_desktop_path = os.path.normpath(root + target_data +
 
63
                                        '/share/applications')
 
64
    old_desktop_file = old_desktop_path + '/typhoon.desktop'
 
65
    desktop_path = os.path.normpath(root + prefix + '/share/applications')
 
66
    desktop_file = desktop_path + '/typhoon.desktop'
 
67
 
 
68
    if not os.path.exists(old_desktop_file):
 
69
        print ("ERROR: Can't find", old_desktop_file)
 
70
        sys.exit(1)
 
71
    elif target_data != prefix + '/':
 
72
        # This is an /opt install, so rename desktop file to use extras-
 
73
        desktop_file = desktop_path + '/extras-typhoon.desktop'
 
74
        try:
 
75
            os.makedirs(desktop_path)
 
76
            os.rename(old_desktop_file, desktop_file)
 
77
            os.rmdir(old_desktop_path)
 
78
        except OSError as e:
 
79
            print ("ERROR: Can't rename", old_desktop_file, ":", e)
 
80
            sys.exit(1)
 
81
 
 
82
    return desktop_file
 
83
 
 
84
def update_desktop_file(filename, target_pkgdata, target_scripts):
 
85
 
 
86
    try:
 
87
        fin = file(filename, 'r')
 
88
        fout = file(filename + '.new', 'w')
 
89
 
 
90
        for line in fin:
 
91
            if 'Icon=' in line:
 
92
                line = "Icon=%s\n" % (target_pkgdata + 'media/typhoon.svg')
 
93
            elif 'Exec=' in line:
 
94
                cmd = line.split("=")[1].split(None, 1)
 
95
                line = "Exec=%s" % (target_scripts + 'typhoon')
 
96
                if len(cmd) > 1:
 
97
                    line += " %s" % cmd[1].strip()  # Add script arguments back
 
98
                line += "\n"
 
99
            fout.write(line)
 
100
        fout.flush()
 
101
        fout.close()
 
102
        fin.close()
 
103
        os.rename(fout.name, fin.name)
 
104
    except (OSError, IOError), e:
 
105
        print ("ERROR: Can't find %s" % filename)
 
106
        sys.exit(1)
 
107
 
 
108
def compile_schemas(root, target_data):
 
109
    if target_data == '/usr/':
 
110
        return  # /usr paths don't need this, they will be handled by dpkg
 
111
    schemadir = os.path.normpath(root + target_data + 'share/glib-2.0/schemas')
 
112
    if (os.path.isdir(schemadir) and
 
113
            os.path.isfile('/usr/bin/glib-compile-schemas')):
 
114
        os.system('/usr/bin/glib-compile-schemas "%s"' % schemadir)
 
115
 
 
116
 
 
117
class InstallAndUpdateDataDirectory(DistUtilsExtra.auto.install_auto):
 
118
    def run(self):
 
119
        DistUtilsExtra.auto.install_auto.run(self)
 
120
 
 
121
        target_data = '/' + os.path.relpath(self.install_data, self.root) + '/'
 
122
        target_pkgdata = target_data + 'share/typhoon/'
 
123
        target_scripts = '/' + os.path.relpath(self.install_scripts, self.root) + '/'
 
124
 
 
125
        values = {'__typhoon_data_directory__': "'%s'" % (target_pkgdata),
 
126
                  '__version__': "'%s'" % self.distribution.get_version()}
 
127
        update_config(self.install_lib, values)
 
128
 
 
129
        desktop_file = move_desktop_file(self.root, target_data, self.prefix)
 
130
        update_desktop_file(desktop_file, target_pkgdata, target_scripts)
 
131
        compile_schemas(self.root, target_data)
 
132
 
 
133
        
 
134
##################################################################################
 
135
###################### YOU SHOULD MODIFY ONLY WHAT IS BELOW ######################
 
136
##################################################################################
 
137
 
 
138
DistUtilsExtra.auto.setup(
 
139
    name='typhoon',
 
140
    version='0.8.91',
 
141
    license='GPL-3',
 
142
    author='Archisman Panigrahi',
 
143
    author_email='apandada1@gmail.com',
 
144
    description='Quickly check the weather with this beautiful application',
 
145
    long_description='Typhoon is a free and open source weather application. It is continuation of discontinued Stormcloud 1.1 ,however with some changes. It is and always will be free.                                                                                                                                                  PPA: https://launchpad.net/~apandada1/+archive/typhoon/                                                                                                              Homepage: http://gettyphoon.tk/',
 
146
    url='https://launchpad.net/typhoon',
 
147
    cmdclass={'install': InstallAndUpdateDataDirectory}
 
148
    )
 
149