Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

90 percent of new examples setup

  • Loading branch information...
commit 96abbc1f3dc284cd7eb55d7680cc8136748db6f7 1 parent a4b7aee
C Anthony Risinger authored
View
30 examples/__main__.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+import os
+import sys
+import subprocess
+
+import _util
+
+
+head = os.path.dirname(__file__)
+examples = [
+ example
+ for example in os.listdir(head)
+ if os.path.isfile(os.path.join(head, example, '__main__.py'))
+ and not example.startswith('_')
+]
+
+env = os.environ.copy()
+env.setdefault('PYJS_BIN_PYTHON', _util._find_python())
+env.setdefault('PYJS_BIN_PYJSBUILD', _util._find_pyjsbuild(head))
+
+for example in examples:
+ header = ''.ljust(10, '-') + ' Building {0} '
+ header = header.format(example.upper()).ljust(69, '-') + '\n'
+ sys.stdout.write(header)
+ sys.stdout.flush()
+ e = subprocess.Popen([env['PYJS_BIN_PYTHON'], os.path.join(head, example)] + sys.argv[1:], env=env)
+ e.wait()
View
95 examples/_util.py
@@ -0,0 +1,95 @@
+# -*- coding: utf-8 -*-
+
+
+import sys
+import os
+import subprocess
+
+
+ENV = None
+PATH = None
+TARGETS = None
+
+
+def _find_python():
+ if sys.version_info.major == 2 and sys.executable and os.path.isfile(sys.executable):
+ return sys.executable
+ for python in ('python2', 'python2.7', 'python2.6'):
+ try:
+ subprocess.call([python, '-c', '"raise SystemExit"'])
+ return python
+ except OSError:
+ pass
+ return 'python'
+
+
+def _find_pyjsbuild(path):
+ lim = 2
+ while lim > 0:
+ path = os.path.join(path, '..')
+ boot = os.path.join(path, 'bootstrap.py')
+ if os.path.isfile(boot):
+ path = os.path.abspath(path)
+ boot = os.path.abspath(boot)
+ break
+ lim = lim - 1
+ if lim == 0:
+ raise RuntimeError('Unable to locate pyjamas root.')
+ pyjsbuild = os.path.join(path, 'bin', 'pyjsbuild')
+ # Bootstrap on test failure; attempts to fix a couple issues at once
+ null = open(os.devnull, 'wb')
+ try:
+ if subprocess.call(['python', pyjsbuild], stdout=null, stderr=subprocess.STDOUT) > 0:
+ raise OSError
+ except OSError:
+ subprocess.call(['python', boot], stdout=null, stderr=subprocess.STDOUT)
+ null.close()
+ return pyjsbuild
+
+
+def _process_environ():
+ return dict([
+ (k[5:], v[:])
+ for k, v in os.environ.items()
+ if k.startswith('PYJS')
+ ])
+
+
+def _process_opts(args):
+ opts = {}
+ possible = ('downloads',)
+ for name in possible:
+ for flag in ('yes', 'no'):
+ opt = '--{0}-{1}'.format(name, flag)
+ if opt in args:
+ args.remove(opt)
+ opts[opt] = flag
+ return opts
+
+
+def _process_args(args):
+ return {'ARG_PYJSBUILD': args or ['-O']}
+
+
+def setup(path, targets):
+ for target in targets:
+ if not os.path.isfile(os.path.join(path, target)):
+ raise TypeError('Target `{0}` does not exist.'.format(target))
+ global ENV, PATH, TARGETS
+ args = sys.argv[1:]
+ ENV = {}
+ ENV.update(_process_environ())
+ ENV.update(_process_opts(args))
+ ENV.update(_process_args(args))
+ ENV.setdefault('BIN_PYTHON', _find_python())
+ ENV.setdefault('BIN_PYJSBUILD', _find_pyjsbuild(path))
+ ENV['DIR_EXAMPLE'] = path
+ PATH = path
+ TARGETS = targets
+
+
+def translate():
+ for target in TARGETS:
+ cmd = [ENV['BIN_PYTHON'], ENV['BIN_PYJSBUILD'], ' '.join(ENV['ARG_PYJSBUILD']), target]
+ e = subprocess.Popen(cmd, cwd=PATH)
+ ret = e.wait()
View
42 examples/helloworld/__main__.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+TARGETS = [
+ 'Hello.py',
+]
+
+
+def prepare():
+ '''Prepare example for translation before util.setup() is called'''
+ pass
+
+
+def translate():
+ '''Translate example'''
+ util.translate()
+
+
+def finalize():
+ '''Cleanup example'''
+ pass
+
+
+#####################################
+# ---------- DO NOT EDIT ---------- #
+#####################################
+
+
+import sys
+import os
+
+
+head = os.path.dirname(__file__)
+sys.path.insert(0, os.path.join(head, '..'))
+import _util as util
+sys.path.pop(0)
+
+prepare()
+util.setup(head, TARGETS)
+translate()
+finalize()
Please sign in to comment.
Something went wrong with that request. Please try again.