def start(ctx, profile=None, logcat=False): """ Start kognitivo inside the current device (emulator or real device) """ import buildozer profile = profile or "" buildo = buildozer.Buildozer() buildozer_config = buildo.config buildo.config_profile = profile buildo._merge_config_profile() tprint("Starting %s on android" % buildozer_config.get('app', 'package.name')) ctx.run("%(adb)s shell input keyevent 82" % { "adb": ctx["adb"], }) ctx.run( "%(adb)s shell am start -n %(package_id)s/org.kivy.android.PythonActivity" % { "adb": ctx["adb"], "package_id": "%s.%s" % (buildozer_config.get('app', 'package.domain'), buildozer_config.get('app', 'package.name')) }) if logcat: log(ctx, all=True)
def release(ctx, deploy=True, logcat=False): """ Create release apk for google play """ patched = patch(ctx) if deploy: ensure_devices(ctx) tprint("Building and installing android in release mode...") os.environ['P4A_RELEASE_KEYSTORE'] = ctx['keystore'] os.environ['P4A_RELEASE_KEYALIAS'] = ctx['keystore_alias'] os.environ['P4A_RELEASE_KEYSTORE_PASSWD'] = ctx['keystore_password'] os.environ['P4A_RELEASE_KEYALIAS_PASSWD'] = ctx['keystore_password'] ctx.run("buildozer android release", pty=True) if not patched: patch(ctx) ctx.run("buildozer android release", pty=True) if deploy: install(ctx, profile='release', debug=False) start(ctx, profile='release') if logcat: log(ctx, all=True)
def devices(ctx): """ List currently available devices """ tprint("Checking devices...") ds = ctx.run('%(adb)s devices' % { 'adb': ctx['adb'], }) ds = re.split('\\n', ds.stdout) serial_pattern = re.compile(r"([0-9a-z]{8})") ip_pattern = re.compile(r"(\d+\.\d+\.\d+\.\d+):\d+") ds_names = [] for i, line in enumerate(ds): match = serial_pattern.search(line) if match and i != 0: print "Serial device %s" % match.groups()[0] ds_names.append(match.groups()[0]) match = ip_pattern.search(line) if match: print "IP device %s" % match.groups()[0] ds_names.append(match.groups()[0]) if not ds_names: print "No devices found..." else: return ds_names
def avd_manager(ctx): """ Start android SDK manager """ tprint("Starting android emulators manager...") process = multiprocessing.Process(target=ctx.run, args=(ctx['sdk_manager'] + " avd", )) process.daemon = True process.start() sleep(5)
def projects_build(ctx): """ Build third party android projects like google play services and facebook SDK """ tprint("Building google play services project...") ctx.run( "%s update project -t 1 -p ./google-play-services_lib --subprojects" % ctx['sdk_manager']) ctx.run( "%s update project -t 1 -p ./facebook-android-sdk/facebook --subprojects" % ctx['sdk_manager'])
def ensure_devices(ctx): """ Check if genymotion emulator is running """ tprint("Start devices...") ds = devices(ctx) if not ds: genymotion(ctx) ds = devices(ctx) if not ds: fprint( "Tried to start emulator, still no devices found. Something is wrong." ) exit(1) iprint("Found %s devices" % len(ds))
def db(ctx): tprint("Filling database with chunks...") from managers.database import database_manager import settings import os db_path = os.path.join(settings.PROJECT_DIR, settings.DATABASE_NAME) if os.path.exists(db_path): os.remove(db_path) for i in xrange(14 * 3): created = datetime.now() - timedelta(days=randint(0, 13), hours=randint(0, 23)) database_manager.add_stat(efficiency=uniform(.5, 1.5), duration=10, key=choice(settings.TASKS.keys()), created=created)
def mo(ctx): tprint("Creating i18n files...") import settings for lang in settings.LANGUAGES + ['system']: print 'Processing locale %s' % lang ctx.run("mkdir -p data/locales/%s/LC_MESSAGES" % lang) report = ctx.run( "msgfmt -c %(stat)s -o data/locales/%(lang)s/LC_MESSAGES/kognitivo.mo po/%(lang_po)s.po" % { "lang": lang, "lang_po": lang if lang != 'system' else 'en', "stat": "" if lang in ['en', 'system'] else "--statistics" }).stderr if "untranslated message" in report or "fuzzy" in report: wprint("Language %s has untranslated or fuzzy messages!" % lang) exit(1)
def genymotion(ctx): """ Start emulator. Device id is taken from invoke.json """ tprint("Starting genymotion device...") print "Known virtual machines:" devices = ctx.run("VBoxManage list vms") devices = re.split('\\n|\\t', devices.stdout) pattern = re.compile(r"\"(.*?)\" \{(.*?)\}") device_names = {} for line in devices: if not line: continue match = pattern.search(line) if match: device_names[match.groups()[1]] = match.groups()[0] else: print "Can't parse machine name: %s" % line try: if not ctx['genymotion_device_id'] in device_names: fprint( "Genymotion device %s is not found in installed genymotion machines." % (ctx['genymotion_device_id'], )) else: iprint("Starting %s..." % device_names[ctx['genymotion_device_id']]) command = "%(genymotion)s --vm-name %(device_id)s&" % { "genymotion": os.path.join(ctx['genymotion_path'], 'player'), "device_id": ctx['genymotion_device_id'] } process = multiprocessing.Process(target=ctx.run, args=(command, )) process.daemon = True process.start() print 'Waiting genymotion to load' sleep(20) except KeyError: fprint( "Genymotion device is not set. Set 'genymotion_device_id' in invoke.json" )
def debug(ctx, deploy=True, logcat=False): """ Create debug apk """ patched = patch(ctx) if deploy: ensure_devices(ctx) tprint("Building and installing android in debug mode...") ctx.run("buildozer android debug", pty=True) if not patched: patch(ctx) ctx.run("buildozer android debug", pty=True) if deploy: install(ctx, profile='', debug=True) start(ctx, profile='') if logcat: log(ctx, all=True)
def atlas(ctx): tprint("Creating atlas...") os.chdir("data/atlas") try: ctx.run("rm *.png") except Failure: pass ctx.run( "python -m kivy.atlas menu 512x1024 `find ../img/atlas_source/menu -name \*.png`" ) ctx.run( "python -m kivy.atlas activity 1024x1024 `find ../img/atlas_source/activity -name \*.png`" ) ctx.run( "python -m kivy.atlas purchases 512x512 `find ../img/atlas_source/purchases -name \*.png`" ) ctx.run( "python -m kivy.atlas tasks 1024x1024 `find ../img/atlas_source/tasks -name \*.png`" )
def log(ctx, all=False): """ Start logging from current device (emulator or real device) """ tprint("Starting logcat...") try: if all: print "Capturing all..." ctx.run( "%(adb)s logcat -c; ADB=%(adb)s logcat-color --config=logcat-color" % {"adb": ctx['adb']}, pty=True) else: print "Capturing python only..." ctx.run( "%(adb)s logcat -c; ADB=%(adb)s logcat-color --config=logcat-color| egrep 'python'" % {"adb": ctx['adb']}, pty=True) except KeyboardInterrupt: exit(0)
def po(ctx): tprint("Creating i18n po files...") import settings ctx.run( '(find . -iname "*.py" -not -path "./.buildozer/*" -not -path "./tests/*" &&' 'find . -iname "*.kv" -not -path "./.buildozer/*" -not -path "./tests/*")' '| ' 'xargs xgettext --from-code=UTF-8 -Lpython --output=messages.pot ' 'settings.translate.json main.py') ctx.run(""" sed -i '/POT-Creation-Date*/d' messages.pot """) ctx.run(""" sed 's/_(//g;s/\")/"/g;' settings.translate.json > settings.json """) for lang in settings.LANGUAGES + ['system']: ctx.run("msgmerge --update po/%s.po messages.pot" % (lang if lang != 'system' else 'en'))
def patch(ctx, profile=''): """ Patch p4a android project with the updated versions of files, placed in patch/ directory """ import buildozer buildo = buildozer.Buildozer() buildo.config_profile = profile buildo._merge_config_profile() buildozer_config = buildo.config package_name = buildozer_config.get('app', 'package.name') dist_path = ".buildozer/android/platform/build/dists/%s" % package_name if not os.path.exists(dist_path): fprint("Android project directory %s does not exist, won't patch" % dist_path) return False tprint("Patching android project: %s" % dist_path) ctx.run("rsync -rav ./patch/android/%s/* " "%s" % (package_name, dist_path)) tprint("Patching done") return True
def profile(ctx): tprint("Profiling...") p = pstats.Stats('kognitivo.profile') p.sort_stats('time').print_stats(40)