~feng-kylin/youker-assistant/youker-assistant

« back to all changes in this revision

Viewing changes to backends/youker-assistant-daemon/src/beautify/others.py

  • Committer: lixiang
  • Date: 2018-03-06 03:13:06 UTC
  • Revision ID: lixiang@kylinos.cn-20180306031306-fd7qnru3vm4a1xjd
Rewrite with Qt5, and add system monitor

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
# -*- coding: utf-8 -*-
3
 
### BEGIN LICENSE
4
 
 
5
 
# Copyright (C) 2013 ~ 2014 National University of Defense Technology(NUDT) & Kylin Ltd
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
 
import os
20
 
import re
21
 
import shutil
22
 
import Image
23
 
# from _pyio import open
24
 
 
25
 
class Others:
26
 
 
27
 
    # custom plymouth bg by plymouthName
28
 
    def custom_plymouth_bg(self, plymouthName):
29
 
        plymouthName = plymouthName.encode('utf-8')
30
 
        existingDir = '/var/lib/youker-assistant-daemon/plymouth/existing/'
31
 
        linkFileDir = '/lib/plymouth/themes/default.plymouth'
32
 
 
33
 
        # replace the config file
34
 
        shutil.copy(existingDir + plymouthName + '/default.plymouth', linkFileDir)
35
 
 
36
 
    # add new custom plymouth
37
 
    def add_new_plymouth(self, customBG, plymouthName):
38
 
        # if plymouthName exist return false
39
 
        existingPlymouth = self.get_existing_plymouth_list()
40
 
        customBG = customBG.encode('utf-8')
41
 
        plymouthName = plymouthName.encode('utf-8')
42
 
        plymouthName
43
 
        if(plymouthName in existingPlymouth):
44
 
            return False
45
 
        else:
46
 
            existingDir = '/var/lib/youker-assistant-daemon/plymouth/existing/'
47
 
            customScript = '/var/lib/youker-assistant-daemon/plymouth/defaults/only_background.script'
48
 
            defaultplymouthfile = '/var/lib/youker-assistant-daemon/plymouth/defaults/default.plymouth'
49
 
                        
50
 
            # add new plymouth conf dir
51
 
            os.mkdir(existingDir + plymouthName)
52
 
            shutil.copy(defaultplymouthfile, existingDir + plymouthName + '/default.plymouth')
53
 
            # modify config file
54
 
            fileHandle = open(existingDir + plymouthName + '/default.plymouth', 'a')
55
 
            fileHandle.write('ImageDir=/lib/plymouth/themes/' + plymouthName + '\n')
56
 
            fileHandle.write('ScriptFile=/lib/plymouth/themes/' + plymouthName + '/youker.script')
57
 
            fileHandle.close()
58
 
 
59
 
            # add new system plymouth dir
60
 
            os.mkdir('/lib/plymouth/themes/' + plymouthName)
61
 
            shutil.copy(customScript, '/lib/plymouth/themes/' + plymouthName + '/youker.script')
62
 
            #shutil.copy(customBG, '/lib/plymouth/themes/' + plymouthName + '/customBG.png')
63
 
            Image.open(customBG).save('/lib/plymouth/themes/' + plymouthName + '/customBG.png')
64
 
                        
65
 
            return True
66
 
 
67
 
    # get existing plymouth list
68
 
    def get_existing_plymouth_list(self):
69
 
        result = []
70
 
        edir = ('/var/lib/youker-assistant-daemon/plymouth/existing')
71
 
        if os.path.isdir(edir):
72
 
            for f in os.listdir(edir):
73
 
                if os.path.isdir(os.path.join(edir, f)):
74
 
                    result.append(f)
75
 
        result.sort()
76
 
        return result
77
 
 
78
 
    # check and save current plymouth when init
79
 
    def plymouth_init_check(self):
80
 
        existingDir = '/var/lib/youker-assistant-daemon/plymouth/existing/'
81
 
        linkFileDir = '/lib/plymouth/themes/default.plymouth'
82
 
 
83
 
        # add 0619
84
 
        if(os.path.exists('/lib/plymouth/themes/ubuntukylin-logo') == False):
85
 
            shutil.copytree('/var/lib/youker-assistant-daemon/plymouth/ubuntukylin-logo/', '/lib/plymouth/themes/ubuntukylin-logo')
86
 
 
87
 
        # locate the current plymouth theme dir
88
 
        linkFile = open(linkFileDir)
89
 
        fullString = linkFile.read()
90
 
        linkFile.close()
91
 
        index = fullString.find('ScriptFile=')
92
 
        theLine = fullString[index:]
93
 
        # cut 'ScriptFile=' & '\n'
94
 
        scriptFile = theLine[theLine.find('/'):theLine.find('\n')]
95
 
#        scriptFile = theLine[theLine.find('/'):]
96
 
        scriptDir = scriptFile[0:scriptFile.rfind('/')]
97
 
        scriptName = scriptFile[scriptFile.rfind('/') + 1:]
98
 
        plymouthName = scriptDir[scriptDir.rfind('/') + 1:]
99
 
        
100
 
        # check and save current pl        linkFile.close()ymouth
101
 
        if(os.path.exists(existingDir + plymouthName) == False):
102
 
            os.mkdir(existingDir + plymouthName)
103
 
            shutil.copy(scriptFile, existingDir + plymouthName + '/' + scriptName)
104
 
            shutil.copy(linkFileDir, existingDir + plymouthName + '/default.plymouth')
105
 
 
106
 
    def get_image_path(self,name):
107
 
        name = name.encode('utf-8')
108
 
#        if not os.path.exists('/lib/plymouth/themes/' + name + '/customBG.png') :
109
 
        if not os.path.exists('/lib/plymouth/themes/' + name + '/' + name + '.script') :
110
 
            if not os.path.exists('/lib/plymouth/themes/' + name + '/youker.script') :
111
 
                if not os.path.exists('/lib/plymouth/themes/' + name + '/customBG.png') :
112
 
                    return "False"
113
 
                else:
114
 
                    return "True"
115
 
            else:
116
 
                return "True"
117
 
        else :
118
 
            return "True"
119
 
#            path = '/lib/plymouth/themes/' + name + '/customBG.png'
120
 
#            return path
121
 
 
122
 
    def delete_plymouth(self,plymouthName):
123
 
        plymouthName = plymouthName.encode('utf-8')
124
 
        fd = open('/lib/plymouth/themes/default.plymouth','r')
125
 
        animation = fd.read()
126
 
        fd.close()
127
 
        used = animation[animation.index('themes/')+len('themes/'):]
128
 
        used = used[:used.index('\n')]
129
 
        if used == plymouthName :
130
 
            return 'use'
131
 
        elif plymouthName == 'ubuntukylin-logo':
132
 
            return 'sys'
133
 
        else :
134
 
            shutil.rmtree('/var/lib/youker-assistant-daemon/plymouth/existing/' + plymouthName)
135
 
            shutil.rmtree('/lib/plymouth/themes/' + plymouthName)
136
 
            return 'ok'
137
 
#        plymouthName = plymouthName.encode('utf-8')
138
 
#        fd = open('/lib/plymouth/themes/default.plymouth','r')
139
 
#        animation = fd.read()
140
 
#        fd.close()
141
 
#        used = animation[animation.index('themes/')+len('themes/'):]
142
 
#        used = used[:used.index('\n')]
143
 
#        if used == plymouthName :
144
 
#            return False
145
 
#        else :
146
 
#            shutil.rmtree('/var/lib/youker-assistant-daemon/plymouth/existing/' + plymouthName)
147
 
#            shutil.rmtree('/lib/plymouth/themes/' + plymouthName)
148
 
#            return True
149
 
 
150
 
if __name__ == '__main__':
151
 
    ooo = Others()
152
 
#       print ooo.get_existing_plymouth_list()
153
 
#       ooo.add_new_plymouth('/home/shine/heihei.png', 'hoho')
154
 
#       ooo.custom_plymouth_bg('hoho')
155
 
#       ooo.plymouth_init_check()