-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
482 lines (409 loc) · 15.2 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/usr/bin/python
#-*- coding:utf-8 -*-
import os,sys
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.checkbox import CheckBox
from kivy.properties import ObjectProperty
from kivy.uix.filechooser import FileChooserListView
from kivy.uix.treeview import TreeView, TreeViewLabel
from kivy.uix.scrollview import ScrollView
from kivy.uix.popup import Popup
from kivy.uix.progressbar import ProgressBar
from kivy.uix.image import Image
from kivyconsole import KivyConsole
import logcat as TLogCat
import listpermission as lstp
from xmlmanager import Xmloeobj
from helper import InfoBubble,LabelInput,PopupWarning
import subprocess
import pkgutil
import imp
class Project():
def __init__(self):
self.open=False
self.saved=False
self.basep=os.path.join(os.gecwd(),'projects')
self.tmpfile='tmp.xml'
self.base_prj_xml='<buildozer><prj></prj></buildozer>'
self.xmlOprj=Xmloeobj()
class TabTools(TabbedPanel):
#workaroud if call __init__() and super class
#kv files dosen't work fine
def init(self):
self.lines=''
self.oldline=''
self.line=''
self.adbpath=''
self.py4a_path=''
self.prj_load_path='~'
self.sett_obj=None
self.lines_count=0
self.obj_builder=None
self.paths={}
self.builder_path=''
self.MainBx=None
self.launcher_recipe=('sqlite3 openssl pyopenssl lxml audiostream cymunk ffmpeg pil pyjnius twisted kivy').split()
self.apktype=''
self.kvc=None
self.pb = None
self.list_module_to_dist=[]
self.list_permission_to_build=[]
def setup(self,console):
self.datalogcat=[]
self.LogCat=None
self.kvc=console
self.LogCat=TLogCat.ThLogCat(self)
self.device_is_connected=False
baseapp=os.path.join(self.py4a_path, 'dist')
baseapp=os.path.join(baseapp, 'default')
self.builder_path=baseapp
self.obj_builder=lstp.get_Android_args()
for tbp in self.tab_list:
if 'Distribuite' in tbp.text:
if self.sett_obj.configured:
self.render_distribuite(tbp)
if 'Build' in tbp.text:
self.render_builder(tbp)
def render_builder(self,tab):
self.MainBx=BoxLayout(orientation='vertical')
self.MainBx.size_hint=( 1, 1)
UpBx=BoxLayout()
UpBx.padding=10
UpBx.spacing=10
UpBx.size_hint = (1,.99)
btnBuild=Button(text='Build',size_hint=(1, 1))
btnBuild.bind(on_press=self.start_build)
btnInstall=Button(text='Install',size_hint=(1,1))
btnInstall.bind(on_press=self.start_install)
btnInstall.bind(on_release=self.install_prj)
btnInstall.disabled=False
BtnBx=BoxLayout()
BtnBx.size_hint = (1,.1)
BtnBx.add_widget(btnBuild)
BtnBx.add_widget(btnInstall)
self.MainBx.add_widget(UpBx)
self.MainBx.add_widget(BtnBx)
DxBox=BoxLayout(orientation='vertical')
DxBox.padding=5
DxBox.spacing=5
ProojIntBx = GridLayout(cols=2) #init
ProojIntBx.size_hint = (1, 0.3)
vl=Label(text=u'Version')
vl.text_size=(self.width, None)
self.vt=TextInput()
self.vt.multiline=False
pnl=Label(text=u'Project Name')
self.pnt=TextInput()
self.pnt.multiline=False
pkgl=Label(text=u'Base Package Name')
self.pkgt=TextInput()
self.pkgt.multiline=False
ProojIntBx.add_widget(vl)
ProojIntBx.add_widget(self.vt)
ProojIntBx.add_widget(pnl)
ProojIntBx.add_widget(self.pnt)
ProojIntBx.add_widget(pkgl)
ProojIntBx.add_widget(self.pkgt)
FileBx=BoxLayout() #add file search and list permission
FileBx.size_hint = (1, 0.95)
self.prj_path=FileChooserListView(path=self.prj_load_path)
self.prj_path.size_hint = (1, 1)
FileBx.add_widget(self.prj_path)
ActionOptBx = GridLayout(cols=4) #debug install ....
ActionOptBx.padding=10
ActionOptBx.spacing=10
ActionOptBx.size_hint = (1, 0.1)
DebugBox=BoxLayout()
DebugBox.add_widget(Label(text='Debug'))
self.chkDebug=CheckBox()
DebugBox.add_widget(self.chkDebug)
DebugBox.add_widget(Label(text='Release'))
self.chkRelease=CheckBox()
DebugBox.add_widget(self.chkRelease)
ActionOptBx.add_widget(DebugBox)
DxBox.add_widget(ProojIntBx)
DxBox.add_widget(FileBx)
DxBox.add_widget(ActionOptBx)
#dxbox -> second in up box
PermissionBx=BoxLayout(orientation='vertical') #treeviw pwrmission
PermissionBx.padding=5
PermissionBx.spacing=5
PermissionBx.pos=(10, 0)
#~ PermissionBx.size_hint = (1, 1)
layouttv = GridLayout(cols=1, spacing=5, size_hint_y=None,pos=(0,10))
layouttv.bind(minimum_height=layouttv.setter('height'))
for x in self.obj_builder.permissions_list:
btn = ToggleButton(text=x, size_hint_y=None, height=40)
btn.bind(state=self.add_remove_permission)
layouttv.add_widget(btn)
root = ScrollView(size_hint=(None, None), size=(400, 500))
root.size_hint = (1, .9)
root.add_widget(layouttv)
PermissionBx.add_widget(root)
#reder all
UpBx.add_widget(DxBox)
UpBx.add_widget(PermissionBx)
#add all in tab item
tab.add_widget(self.MainBx)
def install_prj(self,instance):
popup=None
if self.chkDebug.active:
self.apktype='debug'
elif self.chkRelease.active:
self.apktype='release'
cmd='%s/adb install bin/%s-%s-%s.apk' % (self.sett_obj.path_adb,self.pnt.text,self.vt.text,self.apktype)
print cmd
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,cwd=self.builder_path)
for line in p.stdout.readlines():
if 'Failure' in line:
popup=self.get_popup('Py4A Gui Installer','Installation \n'+line)
popup.open()
elif 'Success' in line:
popup=self.get_popup('Py4A Gui Installer','Installation \n'+line)
popup.open()
instance.text='Install'
def start_install(self,instance):
instance.text='Installing.....'
def prog_install(self,dt):
self.pb.value += dt
def get_popup(self,title,text,progr=False):
btnclose = Button(text='Ok', size_hint_y=None, height='50sp')
content = BoxLayout(orientation='vertical')
content.add_widget(Label(text=text))
self.pb = ProgressBar(max=1000)
content.add_widget(self.pb )
content.add_widget(btnclose)
popup = Popup(content=content, title=title,
size_hint=(None, None), size=('300dp', '300dp'))
btnclose.bind(on_release=popup.dismiss)
if progr:
popup.bind(on_open=self.start_install)
return popup
def start_build(self,instance):
os.environ['ANDROIDSDK'] ='%s' % (self.sett_obj.path_sdk)
args='--name %s ' % self.pnt.text
args+='--package %s.%s ' % (self.pkgt.text,self.pnt.text)
args+='--version %s ' % self.vt.text
args+='--dir %s ' % self.prj_path.path
if len(self.list_permission_to_build)>0:
for p in self.list_permission_to_build:
args+='--permission %s ' % p
if self.chkDebug.active:
self.apktype='debug'
args+='debug'
elif self.chkRelease.active:
self.apktype='release'
args+='release'
cmd4='%s/./build.py %s' % (self.builder_path,args)
print cmd4
cmd="gnome-terminal --working-directory=%s --title='Build Py4 %s' --command='%s' " % (self.builder_path,self.pnt.text,cmd4)
p = subprocess.Popen(cmd,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.STDOUT,env=os.environ,cwd=self.builder_path)
def add_remove_permission(self,instance,value):
if value=='down':
if instance.text not in self.list_permission_to_build:
self.list_permission_to_build.append(instance.text)
else:
for i in range(len(self.list_permission_to_build)-1):
if self.list_permission_to_build[i]==instance.text:
self.list_permission_to_build.pop(i)
def render_distribuite(self,tab):
self.dist_layout = GridLayout(cols=2)
self.Blayout=BoxLayout(orientation='vertical')
self.Blayout.size_hint=( 1, 1)
btnDistr=Button(text='Start Distribuite',size_hint=(1, .1))
btnDistr.bind(on_press=self.start_dist)
self.btnReckivy=ToggleButton(text='Choose recipes for Kivy Launcher',size_hint=(1, .1))
self.btnReckivy.bind(on_release=self.select_kivy_rec)
self.recipe_layout = GridLayout(cols=4)
thedir=os.path.join(self.py4a_path,'recipes')
recipes=[ name for name in os.listdir(thedir) if os.path.isdir(os.path.join(thedir, name)) ]
for r in recipes:
t=ToggleButton(text=r)
t.bind(state=self.add_remove)
self.recipe_layout.add_widget(t)
self.Blayout.add_widget(btnDistr)
self.Blayout.add_widget(self.btnReckivy)
self.Blayout.add_widget(self.recipe_layout)
self.dist_layout.add_widget(self.Blayout)
self.dist_layout.add_widget(self.kvc)
tab.add_widget(self.dist_layout)
def select_kivy_rec(self,instance):
for t in self.recipe_layout.children:
if type(t)==type(ToggleButton()):
if t.text in self.launcher_recipe:
t.trigger_action(duration=0)
def start_dist(self,instance):
self.kvc.size_hint=(.8, 1)
self.Blayout.size_hint=(.2, 1)
command,env=self.sett_obj.distribuite(' '.join(self.list_module_to_dist))
self.kvc.bind(on_subprocess_done=self.end_dist)
self.kvc.environment=env
wr = self.kvc.stdin.write
wr(command)
instance.text='Distribuited'
def end_dist(self,*args):
self.kvc.size_hint=(.01, 1)
self.Blayout.size_hint=(1, 1)
def add_remove(self,instance,value):
if value=='down':
if instance.text not in self.list_module_to_dist:
self.list_module_to_dist.append(instance.text)
else:
for i in range(len(self.list_module_to_dist)-1):
if self.list_module_to_dist[i]==instance.text:
self.list_module_to_dist.pop(i)
def startLogcat(self):
self.device_is_connected=False
self.LogCat.alive=True
self.LogCat.start()
self.rstLogcat.text=''
Clock.schedule_interval(self.load_logcat, 1/30.)
def stopLogcat(self):
self.LogCat.alive=False
self.LogCat=None
Clock.unschedule(self.load_logcat)
self.device_is_connected=False
def refresh_log(self):
self.rstLogcat.text+=self.line
def load_logcat(self, dt):
self.rstLogcat.text+=self.line
class BuildozerGui(Screen):
def __init__(self, **kwargs):
super(Screen, self).__init__(**kwargs)
self.tb = TabTools()
self.setting_obj=None
self.add_widget(self.tb)
self.devices=[]
self.kvc=KivyConsole()
self.kvc.size_hint=(.0, 1)
self.kvc.foreground_color=( .8811, .8811, .8811, 1)
self.kvc.background_color=( .1188, .1188, .1188, 1)
self.kvc.shell=True
self.kvc.font_size= '13.5sp'
def check_devices(self):
df=False
self.devices=[]
cmd='%s/adb devices' % self.path_adb
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
if 'List of devices attached' in line:
df=True
elif df:
self.devices.append(line)
self.lbldevice.text="search device...."
if len(self.devices)>0 and 'device' in self.devices[0] :
Clock.unschedule(self.test_device)
self.lbldevice.text='%s' %self.devices[0].split('\t')[0]
self.wimg.source='data/images/connected.png'
def test_device(self, dt):
self.lbldevice.text="search device...."
self.check_devices()
def addSettPage(self,setting):
self.setting_obj=setting
self.tb.init()
self.path_adb=self.setting_obj.path_adb
self.tb.adbpath=self.path_adb
self.tb.py4a_path=self.setting_obj.paths['py4a_path']
self.tb.sett_obj=self.setting_obj
self.tb.setup(self.kvc)
self.check_devices()
Clock.schedule_interval(self.test_device, 10)
class BuildozerGuiStart(Screen):
pass
class AndroidPageSettings(Screen):
def __init__(self, **kwargs):
super(Screen, self).__init__(**kwargs)
self.wpath=os.getcwd()
self.cfgfile='config_base.xml'
self.xmlOcfg=Xmloeobj()
self.cfg_full_file='%s%s%s'%(self.wpath,os.sep,self.cfgfile)
self.paths={'path_sdk':'','path_ndk':'','ndkver':'','apiver':'','py4a_path':''}
self.path_adb=os.path.join(self.paths['path_sdk'],'platform-tools')
self.configured=False
#~ self.lst_perm=lstp.list
self.load_config()
def load_config(self):
listsett=[]
cf=''
if not os.path.isfile(self.cfg_full_file):
bstr='<buildozer><cfg></cfg></buildozer>'
self.xmlOcfg.objfromstring(bstr)
self.xmlOcfg.writetagattrib('cfg','configured',str(self.configured))
for field_attrib in self.paths:
self.xmlOcfg.addsettagchild('cfg','field')
self.xmlOcfg.writeattrib('name',field_attrib)
self.xmlOcfg.saveinfile(self.cfg_full_file)
else:
self.xmlOcfg.objfromFile(self.cfg_full_file)
self.xmlOcfg.selecttag('cfg')
if self.xmlOcfg.currattr['configured']=='True':
self.paths['path_sdk']=self.xmlOcfg.readStringtagattrib('field','name','path_sdk')
self.lblpathsdk.text=self.paths['path_sdk']
self.paths['path_ndk']=self.xmlOcfg.readStringtagattrib('field','name','path_ndk')
self.lblpathndk.text=self.paths['path_ndk']
self.paths['ndkver']=self.xmlOcfg.readStringtagattrib('field','name','ndkver')
self.lblpathndkv.text=self.paths['ndkver']
self.paths['apiver']=self.xmlOcfg.readStringtagattrib('field','name','apiver')
self.lblpathsdkv.text=self.paths['apiver']
self.paths['py4a_path']=self.xmlOcfg.readStringtagattrib('field','name','py4a_path')
self.lblpathp4a.text=self.paths['py4a_path']
self.path_adb=os.path.join(self.paths['path_sdk'],'platform-tools')
self.configured=True
def save_config(self):
self.paths['apiver']=self.lblpathsdkv.text
self.paths['ndkver']=self.lblpathndkv.text
self.paths['path_ndk']=self.lblpathndk.text
self.paths['path_sdk']=self.lblpathsdk.text
self.paths['py4a_path']=self.lblpathp4a.text
self.xmlOcfg.writeStringtagattrib('field','name','path_sdk',self.paths['path_sdk'])
self.xmlOcfg.writeStringtagattrib('field','name','path_ndk',self.paths['path_ndk'])
self.xmlOcfg.writeStringtagattrib('field','name','ndkver',self.paths['ndkver'])
self.xmlOcfg.writeStringtagattrib('field','name','apiver',self.paths['apiver'])
self.xmlOcfg.writeStringtagattrib('field','name','py4a_path',self.paths['py4a_path'])
self.xmlOcfg.writetagattrib('cfg','configured',str(True))
self.xmlOcfg.saveinfile(self.cfg_full_file)
self.configured=True
def distribuite(self,module):
# Make a copy of the current environment
os.environ['ANDROIDSDK'] ='%s' % (self.paths['path_sdk'])
os.environ['ANDROIDNDK'] ='%s' % (self.paths['path_ndk'])
os.environ['ANDROIDNDKVER'] ='%s' % (self.paths['ndkver'])
os.environ['ANDROIDAPI'] ='%s' % (self.paths['apiver'])
#~ Search librrfaker.so for add to preload env path
cmd_pd='locate librrfaker.so'
p = subprocess.Popen(cmd_pd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
preload_path=''
for line in p.stdout.readlines():
if 'ERROR' not in line:
preload_path=line
os.environ['LD_PRELOAD'] ='%s' % preload_path.split('\n')[0]
d = dict(os.environ.copy())
cmd4='%s/./distribute.sh -m "%s"' % (self.paths['py4a_path'],module)
#~ cmd="gnome-terminal --working-directory=%s --title='Distribuie Py4a modules %s' --command='%s' " % (self.paths['py4a_path'],module,cmd4)
#~ p = subprocess.Popen(cmd, shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.STDOUT,env=os.environ,cwd=self.paths['py4a_path'],close_fds=True)
#~TODO Controll the acrive process!!
return cmd4,d
class BuildozerGuiApp(App):
def build(self):
sm = ScreenManager()
start=BuildozerGuiStart(name='start')
asettp=AndroidPageSettings(name='android_settings')
bdz=BuildozerGui(name='android')
bdz.addSettPage(asettp)
sm.add_widget(start)
sm.add_widget(asettp)
sm.add_widget(bdz)
Clock.max_iteration = 100
return sm
if __name__ == '__main__':
BuildozerGuiApp().run()