From 32a50940e1c34cbaf5c141cc9356a798024c163e Mon Sep 17 00:00:00 2001 From: shimoda Date: Sun, 5 Mar 2017 01:09:14 +0900 Subject: [PATCH] updated sample scripts --- python-build/python-scripts/pip_console.py | 1 + .../python-scripts/take_cacert_pem.py | 17 +++-- python-build/python-scripts/test.py | 71 +++++++++++++++---- python3-alpha/LATEST_VERSION_SCRIPTS | 2 +- .../python-scripts/take_cacert_pem.py | 1 + .../python-scripts/wifiap-connect.py3 | 45 ++++++++++++ 6 files changed, 118 insertions(+), 19 deletions(-) create mode 120000 python-build/python-scripts/pip_console.py create mode 120000 python3-alpha/python-scripts/take_cacert_pem.py create mode 100644 python3-alpha/python-scripts/wifiap-connect.py3 diff --git a/python-build/python-scripts/pip_console.py b/python-build/python-scripts/pip_console.py new file mode 120000 index 00000000..5832d98e --- /dev/null +++ b/python-build/python-scripts/pip_console.py @@ -0,0 +1 @@ +../../python3-alpha/python-scripts/pip_console.py3 \ No newline at end of file diff --git a/python-build/python-scripts/take_cacert_pem.py b/python-build/python-scripts/take_cacert_pem.py index a72d9504..ba5b94fb 100644 --- a/python-build/python-scripts/take_cacert_pem.py +++ b/python-build/python-scripts/take_cacert_pem.py @@ -1,13 +1,20 @@ +import sys import os -import urllib + + +if sys.version_info[0] == 2: + from urllib import urlretrieve +else: + from urllib.request import urlretrieve def main(): - fname = "/mnt/sdcard/cacert.pem" + fname = os.path.dirname(__file__) + fname = os.path.join(fname, "cacert.pem") if os.path.exists(fname): - return False - urllib.urlretrieve("http://curl.haxx.se/ca/cacert.pem", fname) - return False + return fname + urlretrieve("http://curl.haxx.se/ca/cacert.pem", fname) + return fname if __name__ == "__main__": diff --git a/python-build/python-scripts/test.py b/python-build/python-scripts/test.py index 1d715495..5e826588 100644 --- a/python-build/python-scripts/test.py +++ b/python-build/python-scripts/test.py @@ -15,9 +15,9 @@ def test_029_isfile(): # issue #29 {{{1 import os # FIXME: determine path to sdcard. like: path = os.environ[""] - path = "/sdcard" - fname = os.path.join(path, "sl4a", "test_isfile") - file(fname, "w").write("this is test") + path = os.path.dirname(__file__) + fname = os.path.abspath(os.path.join(path, "test_isfile")) + open(fname, "w").write("this is test") os.path.isfile(fname) os.remove(fname) try: @@ -41,7 +41,10 @@ def test_047_ttyname(): # issue #47 {{{1 def test_071_anydbm(): # issue #71 {{{1 import os - import anydbm + if sys.version_info[0] == 2: + import anydbm + else: + import dbm as anydbm # FIXME: determine path to sdcard. like: path = os.environ[""] del os.chmod for fname in ( @@ -65,13 +68,18 @@ def test_071_anydbm(): # issue #71 {{{1 def test_075_httpserver(): # issue #75 {{{1 import time import threading - import BaseHTTPServer + if sys.version_info[0] == 2: + from BaseHTTPServer import BaseHTTPRequestHandler as handler + from BaseHTTPServer import HTTPServer + else: + from http.server import BaseHTTPRequestHandler as handler + from http.server import HTTPServer fname = "/sdcard/sl4a/test_075.html" port = 9090 - class Handler(BaseHTTPServer.BaseHTTPRequestHandler): + class Handler(handler): def do_GET(s): - file(fname, "w").write(""" + open(fname, "w").write(""" fine 075""") html = open(fname, 'rb') s.send_response(200) @@ -79,7 +87,7 @@ def do_GET(s): s.end_headers() s.wfile.write(html.read()) - server_class = BaseHTTPServer.HTTPServer + server_class = HTTPServer httpd = server_class(('', port), Handler) if not skip_gui: # and manual test has passed, open http://127.0.0.1:9090 in browser. @@ -93,12 +101,17 @@ def do_GET(s): def test_106_https_certification_failed(): # issue #106 {{{1 - import urllib2 + if sys.version_info[0] == 2: + import urllib2 + else: + from urllib import request as urllib2 + import os import take_cacert_pem - if take_cacert_pem.main(): + fname = take_cacert_pem.main() + if not fname: return False - os.environ["SSL_CERT_FILE"] = "/mnt/sdcard/cacert.pem" + os.environ["SSL_CERT_FILE"] = fname # input = input.replace("!web ", "") url = "https://ccc.de/" # url = "https://www.openssl.org/" @@ -148,7 +161,7 @@ def test_013s_scanBarcode(): # issue sl4a #13 {{{1 if ext is None: return False if 'SCAN_RESULT_BYTES' not in ext or 'SCAN_RESULT' not in ext: - print("no results:" % ext) + print("no results:" + str(ext)) return False bts = ext['SCAN_RESULT_BYTES'] msg = ext['SCAN_RESULT'] @@ -169,6 +182,33 @@ def test_009s_airplanemode(): # issue sl4a #9 {{{1 return True +def test_032s_wificonnect(): # issue sl4a #32 {{{1 + method = "WPA2" + if method == "no-security": + cfg = dict( + SSID="invalidwifi", + # below parameters are not used in example of my expalation site. + # BSSID=, + # hiddenSSID=False, + # priority=, + # apBand=, + ) + elif method == "WEP": + cfg = dict( + SSID="invalidwifi", + wepKeys=["key0"], + wepTxKeyIndex=0, + ) + else: # elif method == "WPA2": + cfg = dict( + SSID="invalidwifi", + preSharedKey="kuaihuawifi123", + # or you can use: password="presharedkey", + # be careful SL4A can't allow 64byte key. + ) + droid.wifiConnect(cfg) + return True + # tests for some facade {{{1 def event_loop(): for i in range(10): @@ -541,7 +581,12 @@ def test_xmpp(): if __name__ == '__main__': # {{{1 - import android + try: + import android + except: + import os + sys.path.insert(0, os.path.dirname(__file__)) + import android_mock as android # for PC debugging droid = android.Android() def boilerplate(f): diff --git a/python3-alpha/LATEST_VERSION_SCRIPTS b/python3-alpha/LATEST_VERSION_SCRIPTS index ad2f1825..f7205a24 100644 --- a/python3-alpha/LATEST_VERSION_SCRIPTS +++ b/python3-alpha/LATEST_VERSION_SCRIPTS @@ -1 +1 @@ -r30 +r31 diff --git a/python3-alpha/python-scripts/take_cacert_pem.py b/python3-alpha/python-scripts/take_cacert_pem.py new file mode 120000 index 00000000..9e613673 --- /dev/null +++ b/python3-alpha/python-scripts/take_cacert_pem.py @@ -0,0 +1 @@ +../../python-build/python-scripts/take_cacert_pem.py \ No newline at end of file diff --git a/python3-alpha/python-scripts/wifiap-connect.py3 b/python3-alpha/python-scripts/wifiap-connect.py3 new file mode 100644 index 00000000..ce707242 --- /dev/null +++ b/python3-alpha/python-scripts/wifiap-connect.py3 @@ -0,0 +1,45 @@ +from __future__ import print_function, unicode_literals +import sys + + +# tests for python modification for android {{{1 +def test_032s_wificonnect(): # issue sl4a #32 {{{1 + method = "WPA2" + if method == "no-security": + cfg = dict( + SSID="invalidwifi", + # below parameters are not used in example of my expalation site. + # BSSID=, + # hiddenSSID=False, + # priority=, + # apBand=, + ) + elif method == "WEP": + cfg = dict( + SSID="invalidwifi", + wepKeys=["key0"], + wepTxKeyIndex=0, + ) + else: # elif method == "WPA2": + cfg = dict( + SSID="invalidwifi", + password="testtesttest", + # or you can use: presharedkey="0123456789ABCDEF...64B", + # be careful SL4A can't allow 64byte key. + ) + droid.wifiConnect(cfg) + return True + + +if __name__ == '__main__': # {{{1 + try: + import android + except: + import os + sys.path.insert(0, os.path.dirname(__file__)) + import android_mock as android # for PC debugging + droid = android.Android() + + test_032s_wificonnect() + +# vi: ft=python:et:ts=4:fdm=marker