~donadigo/power-installer/power-installer-legacy

« back to all changes in this revision

Viewing changes to scripts/input.py

  • Committer: donadigo
  • Date: 2014-12-25 20:06:51 UTC
  • Revision ID: donadigos159@gmail.com-20141225200651-au1ereqidbfaexct
Port from bash to python complete; FIXED: generated scripts failed due to no TERM variable was exported; NEW FEATURE: writing logs when task fails

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python -B
2
 
#*** part of power installer ***#
 
2
## part of power installer
3
3
 
4
4
import sys
5
5
import os
13
13
pkg = sys.argv[2]
14
14
cinput = sys.argv[3]
15
15
link = sys.argv[4]
16
 
option = sys.argv[5]
 
16
opt = sys.argv[5]
17
17
installed = sys.argv[6]
18
 
first_run = sys.argv[7]
19
18
 
20
19
def ppa_get_code(sin_ppa, opt = None):
21
20
    if opt != None:
22
 
        callcmd = subprocess.Popen(['add-apt-repository', '-y', opt, sin_ppa])
 
21
        call_cmd = subprocess.Popen(['add-apt-repository', '-y', opt, sin_ppa])
23
22
    else:
24
 
        callcmd = subprocess.Popen(['add-apt-repository', '-y', sin_ppa])
 
23
        call_cmd = subprocess.Popen(['add-apt-repository', '-y', sin_ppa])
25
24
 
26
 
    streamwaitppa = callcmd.communicate()[0]
27
 
    cmdoutcode = callcmd.returncode
 
25
    streamwaitppa = call_cmd.communicate()[0]
 
26
    cmdoutcode = call_cmd.returncode
28
27
    return cmdoutcode
29
28
 
30
29
def ppa_error(sin_ppa):
31
 
    show('Something went wrong. Check the syntax of repostiory: ' + sin_ppa + ' and try again.', 'true')
 
30
    show('Something went wrong. Check the syntax of repostiory: ' + sin_ppa + ' and try again.', True)
32
31
 
33
32
def pkg_error(sin_pkg):
34
33
    show('Something went wrong. Check names of the packages and try again.', 'true')
35
34
 
36
35
def call_dragdrop(fullpath, theme='false', icon='false', plank='false'):
37
 
    # temporary solution
38
36
    subprocess.call(['python', corepath + '/dragdrop.py', fullpath, theme, icon, plank, 'false', 'false'])
39
37
 
40
38
def ppa_pkg(ppa, pkg):
41
 
    ppasucess = ''
42
 
    pkgsucess = ''
 
39
    ppasucess = None
 
40
    pkgsucess = None
43
41
    if ppa != '':
44
42
        if installed == 'false':
45
43
            print('\nPOWERINSTALLER: Adding repositories...\n')
120
118
    if cinput != '':
121
119
        if link == 'false':
122
120
            fd, input_path = mkstemp(prefix='.', suffix='_pi_input_cmd.sh')
123
 
            input_file = open(input_path, 'w')
 
121
            input_file = open(input_path, 'w+r')
124
122
 
125
123
            # input data
126
124
            input_data = '''#!/bin/bash
127
125
# AUTOGENERATED BY POWER INSTALLER
128
126
cmd() {
129
 
    USER="''' + user + '"' + '''
130
 
    HOME="''' + homedir + '"\n' + cinput + '''
 
127
    export TERM=xterm
 
128
    export USER="''' + user + '"' + '''
 
129
    export HOME="''' + homedir + '"\n' + 'cd "$HOME"\n' + cinput + '''
131
130
}
132
131
(set -e; while true; do echo "y"; sleep 2; done) | cmd
133
132
exit "$?"'''
134
133
            input_file.write(input_data)
135
134
            input_file.close()
136
135
 
137
 
            run_cmd = subprocess.Popen(['bash', input_path])
 
136
            logcmdpath = '/tmp/pi_cmd_log.txt'
 
137
            logcmdfile = open(logcmdpath, 'w+r')
 
138
            run_cmd = subprocess.Popen(['bash', input_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
 
139
 
 
140
            # write line to stdout and to file
 
141
            for line in run_cmd.stdout:
 
142
                sys.stdout.write(line)
 
143
                logcmdfile.write(line)
 
144
 
138
145
            run_cmd.communicate()[0]
139
146
            out = run_cmd.returncode
140
147
 
141
148
            if out == 0:
142
149
                show('All commands have been executed successfully!', 'false')
 
150
                os.remove(logcmdpath)
 
151
 
143
152
            else:
144
 
                show('Something went wrong. Check the syntax of your commands and try again.', 'true')
 
153
                show('Something went wrong. Check out: /tmp/pi_cmd_log.txt for more information.', 'true')
145
154
 
146
155
            if os.path.isfile(input_path):
147
156
                os.remove(input_path)
155
164
            if not os.path.isfile(fullpath):
156
165
                show('Something went wrong. The file could not be downloaded, check if your link is vaild!', 'true')
157
166
            else:
158
 
                if(option == '0' or option == '1'):
 
167
                if(opt == 'a' or opt == 'b'):
159
168
                    call_dragdrop(fullpath)
160
 
                elif option == '2':
 
169
                elif opt == 'c':
161
170
                    call_dragdrop(fullpath, theme='true')
162
 
                elif option == '3':
 
171
                elif opt == 'd':
163
172
                    call_dragdrop(fullpath, icon='true')
164
 
                elif option == '4':
 
173
                elif opt == 'e':
165
174
                    call_dragdrop(fullpath, plank='true')
166
175
 
167
176
            # remove temporary directory
168
177
            rmtree(tmpname)
169
178
 
170
179
if ppa + pkg + cinput != '':
171
 
    if first_run == 'true':
172
 
        show('', '')
173
 
 
174
180
    print("\nPOWERINSTALLER: USER SESSION OPENED\n")
175
181
    ppa_pkg(ppa, pkg)
176
182
    exe_cmd(cinput)