source: generate.py @ 343:506edfba4894

Last change on this file since 343:506edfba4894 was 342:3a5cc5a4f0a6, checked in by Evgeny Stambulchik <Evgeny.Stambulchik@…>, 11 years ago

qx-3.0.2.

  • Property exe set to *
File size: 4.9 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3################################################################################
4#
5#  qooxdoo - the new era of web development
6#
7#  http://qooxdoo.org
8#
9#  Copyright:
10#    2008 - 2012 1&1 Internet AG, Germany, http://www.1und1.de
11#
12#  License:
13#    LGPL: http://www.gnu.org/licenses/lgpl.html
14#    EPL: http://www.eclipse.org/org/documents/epl-v10.php
15#    See the LICENSE file in the project's top-level directory for details.
16#
17#  Authors:
18#    * Thomas Herchenroeder (thron7)
19#
20################################################################################
21
22##
23# This is a stub proxy for the real generator.py
24##
25
26import sys, os, re, subprocess, codecs, optparse
27
28CMD_PYTHON = sys.executable
29QOOXDOO_PATH = '..'
30QX_PYLIB = "tool/pylib"
31
32##
33# A derived OptionParser class that ignores unknown options (The parent
34# class raises in those cases, and stops further processing).
35# We need this, as we are only interested in -c/--config on this level, and
36# want to ignore pot. other options.
37#
38class IgnoringUnknownOptionParser(optparse.OptionParser):
39    ##
40    # <rargs> is the raw argument list. The original _process_args mutates
41    # rargs, processing options into <values> and copying interspersed args
42    # into <largs>. This overridden version ignores unknown or ambiguous
43    # options.
44    def _process_args(self, largs, rargs, values):
45        while rargs:
46            try:
47                optparse.OptionParser._process_args(self, largs, rargs, values)
48            except (optparse.BadOptionError, optparse.AmbiguousOptionError):
49                pass
50
51
52def parseArgs():
53    parser = IgnoringUnknownOptionParser(add_help_option=False)
54    parser.add_option(
55        "-c", "--config", dest="config", metavar="CFGFILE",
56        default="config.json", help="path to configuration file"
57    )
58    parser.add_option(
59        "-v", "--verbose", dest="verbose", action="store_true",
60        default=False, help="run in verbose mode"
61    )
62    (options, args) = parser.parse_args(sys.argv[1:])
63    return options, args
64
65ShellOptions, ShellArgs = parseArgs()
66
67
68# this is from misc.json, duplicated for decoupling
69_eolComment = re.compile(r'(?<![a-zA-Z]:)//.*$', re.M) # double $ for string.Template
70_mulComment = re.compile(r'/\*.*?\*/', re.S)
71def stripComments(s):
72    b = _eolComment.sub('',s)
73    b = _mulComment.sub('',b)
74    return b
75
76def getQxPath():
77    path = QOOXDOO_PATH
78    # OS env takes precedence
79    if os.environ.has_key("QOOXDOO_PATH"):
80        path = os.environ["QOOXDOO_PATH"]
81
82    # else use QOOXDOO_PATH from config.json
83    else:
84        config_file = ShellOptions.config
85        if os.path.exists(config_file):
86            # try json parsing with qx json
87            if not path.startswith('${'): # template macro has been resolved
88                sys.path.insert(0, os.path.join(path, QX_PYLIB))
89                try:
90                    from misc import json
91                    got_json = True
92                except:
93                    got_json = False
94
95            got_path = False
96            if got_json:
97                config_str = codecs.open(config_file, "r", "utf-8").read()
98                #config_str = stripComments(config_str)  # not necessary under demjson
99                config = json.loads(config_str)
100                p = config.get("let")
101                if p:
102                    p = p.get("QOOXDOO_PATH")
103                    if p:
104                        path = p
105                        got_path = True
106
107            # regex parsing - error prone
108            if not got_path:
109                qpathr=re.compile(r'"QOOXDOO_PATH"\s*:\s*"([^"]*)"\s*,?')
110                conffile = codecs.open(config_file, "r", "utf-8")
111                aconffile = conffile.readlines()
112                for line in aconffile:
113                    mo = qpathr.search(line)
114                    if mo:
115                        path = mo.group(1)
116                        break # assume first occurrence is ok
117
118    path = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), path))
119
120    return path
121
122os.chdir(os.path.dirname(os.path.abspath(sys.argv[0])))  # switch to skeleton dir
123qxpath = getQxPath()
124REAL_GENERATOR = os.path.join(qxpath, 'tool', 'bin', 'generator.py')
125
126if not os.path.exists(REAL_GENERATOR):
127    print "Cannot find real generator script under: \"%s\"; aborting" % REAL_GENERATOR
128    sys.exit(1)
129elif ShellOptions.verbose:
130    print "\nInvoking real generator under %s ..." % REAL_GENERATOR
131
132argList = []
133argList.append(CMD_PYTHON)
134argList.append(REAL_GENERATOR)
135argList.extend(sys.argv[1:])
136if sys.platform == "win32":
137    argList1=[]
138    for arg in argList:
139        if arg.find(' ')>-1:
140            argList1.append('"%s"' % arg)
141        else:
142            argList1.append(arg)
143    argList = argList1
144else:
145    argList = ['"%s"' % x for x in argList]  # quote argv elements
146
147cmd = " ".join(argList)
148retval = subprocess.call(cmd, shell=True)
149sys.exit(retval)
Note: See TracBrowser for help on using the repository browser.