Example #1
0
def remove_egg_info(p):
    for dirpath, dirnames, filenames in os.walk(p):
        for filename in dirnames + filenames:
            if 'egg-info' in filename:
                remove_path(os.path.join(dirpath, filename))
        for directory in dirnames:
            subdir = os.path.join(dirpath, directory)
            remove_egg_info(subdir)
def remove_egg_info(p):
    for dirpath, dirnames, filenames in os.walk(p):
        for filename in dirnames + filenames:
            if 'egg-info' in filename:
                remove_path(
                    os.path.join(dirpath, filename)
                )
        for directory in dirnames:
            subdir = os.path.join(dirpath, directory)
            remove_egg_info(subdir)
def webbuilder_process(context, request):
    valid_actions = ["submit_cgwbDownload"]
    errors = []
    output_dir_prefix = None
    params = dict(request.params)
    actions = [action for action in request.params if re.match("^submit_", action)]
    [params.pop(action) for action in actions]
    configuration = params.pop("configuration", None)
    download_path = None
    if not configuration:
        errors.append("Choose a configuration")
    if not configuration in root.configurations:
        errors.append("Invalid configurations")
    if not errors:
        for action in actions:
            output_dir_prefix = os.path.join(get_generation_path(), "%s" % uuid4())
            if action in valid_actions:
                try:
                    paster = get_paster(configuration)
                    createcmd = [a for a in pkg_resources.iter_entry_points("paste.global_paster_command", "create")][
                        0
                    ].load()
                    [params.update({param: True}) for param in params if params[param] in [u"on", u"checkbox_enabled"]]
                    project = params.pop("project", None)
                    if not project:
                        project = ""
                    project = project.strip()
                    if not project:
                        raise Exception("Project does not exist or is empty")
                    if os.path.exists(output_dir_prefix) and len(os.listdir(output_dir_prefix)) > 0:
                        if not get_settings().get("debug", "").lower() == "true":
                            raise Exception("Directory %s exists " "and is not empty" % (output_dir_prefix))
                    # keep track of boolean options for descendant templates
                    boolean_consumed_options = {}
                    for template in paster.templates_data:
                        cparams = params.copy()
                        output_dir = output_dir_prefix
                        if template["self"].output:
                            output_dir = os.path.join(output_dir, template["self"].output)
                        top = ["-q", "-t", template["name"], "--no-interactive", "-o", output_dir, project]
                        if template["aliases"]:
                            for var, alias, default in template["aliases"]:
                                # we have a value
                                if alias in cparams:
                                    cparams[var.name] = cparams[alias]
                                # blank value, resetting
                                else:
                                    if var.name in cparams:
                                        del cparams[var.name]

                        # set to false unticked options
                        for g in template["groups"]:
                            for myo in g["options"]:
                                if myo[1] == "boolean":
                                    boolean_consumed_options[myo[0].name] = cparams.get(myo[0].name, False)
                        for option in boolean_consumed_options:
                            if not option in cparams:
                                cparams[option] = False

                        # cleanup variables
                        names = dict([(var.name, var) for var in template["template"].vars])
                        for iv in cparams.keys():
                            if not iv in names:
                                del cparams[iv]
                                continue
                            else:
                                if not cparams[iv]:
                                    if not names[iv].default:
                                        del cparams[iv]
                                        continue

                        # be sure not to have unicode params there
                        # because paster will swallow them up
                        top.extend(["%s=%s" % i for i in cparams.items()])
                        cmd = createcmd("create")
                        try:
                            cmd.run(top)
                        except Exception, e:
                            remove_path(output_dir)
                            raise
                    # do post genration stuff
                    postprocess(paster, output_dir_prefix, project, params)

                    if action == "submit_cgwbDownload":
                        qsparams = dict(request.POST)
                        qsparams.update(params)
                        qsparams.update(boolean_consumed_options)
                        qs = urllib.urlencode(qsparams)
                        ficp = os.path.join(output_dir_prefix, "LINK_TO_REGENERATE.html")
                        burl = request.route_url("collect", configuration=request.POST.get("configuration"))
                        genurl = burl + "?%s" % urllib.urlencode(dict(oldparams=zlib.compress(qs, 9).encode("base64")))
                        fic = open(ficp, "w")
                        fic.write(
                            "<html><body>"
                            '<a href="%s">'
                            "Click here to go to the generation service</a>"
                            "</body></html>" % genurl
                        )
                        fic.close()
                        ts = "%s" % datetime.datetime.now()
                        ts = ts.replace(":", "-").replace(" ", "_")[:-4]
                        filename = "%s-%s.tar.gz" % (project, ts)
                        file_path = os.path.join(get_generation_path(), filename)
                        fic = tarfile.open(file_path, "w:gz")
                        fic.add(output_dir_prefix, "./")
                        fic.close()
                        download_path = os.path.basename(file_path)
                        resp = Response(
                            body=open(file_path).read(),
                            content_type="application/x-tar-gz",
                            headerlist=[
                                (
                                    "Content-Disposition",
                                    str('attachment; filename="%s"' % (os.path.basename(file_path))),
                                ),
                                ("Content-Transfer-Encoding", "binary"),
                                ("Content-Length", "%s" % (os.path.getsize(file_path))),
                            ],
                            request=request,
                        )
                        os.remove(file_path)

                        return resp
                except NoSuchConfigurationError:
                    errors.append("%s/ The required configuration " "does not exists : %s" % (action, configuration))
                except Parser.ParseError, e:
                    errors.append(
                        '<div class="error">'
                        "<p>%s/ Error while reading paster variables:</p>"
                        '<p class="pythonerror">%r</p>'
                        '<p class="pythonerror"><pre>%s</pre></p>'
                        "</div>" % (action, e, e.report())
                    )
                except Exception, e:
                    trace = traceback.format_exc()
                    # raise
                    errors.append(
                        '<div class="error">'
                        "<p>%s/ -- Error while reading paster variables:</p>"
                        '<p class="pythonerror">%r</p>'
                        '<p class="pythonerror">%s</p>'
                        '<p class="pythonerror"><pre>%s</pre>/p>'
                        "</div>" % (action, e, e, trace)
                    )
                        '<p class="pythonerror"><pre>%s</pre></p>'
                        "</div>" % (action, e, e.report())
                    )
                except Exception, e:
                    trace = traceback.format_exc()
                    # raise
                    errors.append(
                        '<div class="error">'
                        "<p>%s/ -- Error while reading paster variables:</p>"
                        '<p class="pythonerror">%r</p>'
                        '<p class="pythonerror">%s</p>'
                        '<p class="pythonerror"><pre>%s</pre>/p>'
                        "</div>" % (action, e, e, trace)
                    )
                finally:
                    remove_path(output_dir_prefix)
    main = get_template("templates/main_template.pt").implementation()

    return render_to_response(
        "templates/process.pt",
        dict(
            errors=errors, context=context, root=root, output=output_dir_prefix, download_path=download_path, main=main
        ),
        request=request,
    )


def webbuilder_collectinformation(context, request):
    errors, added_options = [], []
    paster, default_group_data, templates_data = None, None, None
    try:
Example #5
0
def webbuilder_process(context, request):
    valid_actions = ['submit_cgwbDownload']
    errors = []
    output_dir_prefix = None
    params = dict(request.params)
    actions = [
        action for action in request.params if re.match('^submit_', action)
    ]
    [params.pop(action) for action in actions]
    configuration = params.pop('configuration', None)
    download_path = None
    if not configuration:
        errors.append('Choose a configuration')
    if not configuration in root.configurations:
        errors.append('Invalid configurations')
    if not errors:
        for action in actions:
            output_dir_prefix = os.path.join(get_generation_path(),
                                             "%s" % uuid4())
            if action in valid_actions:
                try:
                    paster = get_paster(configuration)
                    createcmd = [
                        a for a in pkg_resources.iter_entry_points(
                            'paste.global_paster_command', 'create')
                    ][0].load()
                    [
                        params.update({param: True}) for param in params
                        if params[param] in [u'on', u'checkbox_enabled']
                    ]
                    project = params.pop('project', None)
                    if not project:
                        project = ''
                    project = project.strip()
                    if not project:
                        raise Exception('Project does not exist or is empty')
                    if (os.path.exists(output_dir_prefix)
                            and len(os.listdir(output_dir_prefix)) > 0):
                        if (not get_settings().get('debug', '').lower()
                                == 'true'):
                            raise Exception('Directory %s exists '
                                            'and is not empty' %
                                            (output_dir_prefix))
                    # keep track of boolean options for descendant templates
                    boolean_consumed_options = {}
                    for template in paster.templates_data:
                        cparams = params.copy()
                        output_dir = output_dir_prefix
                        if template['self'].output:
                            output_dir = os.path.join(output_dir,
                                                      template['self'].output)
                        top = [
                            '-q', '-t', template['name'], '--no-interactive',
                            '-o', output_dir, project
                        ]
                        if template['aliases']:
                            for var, alias, default in template['aliases']:
                                # we have a value
                                if alias in cparams:
                                    cparams[var.name] = cparams[alias]
                                #blank value, resetting
                                else:
                                    if var.name in cparams:
                                        del cparams[var.name]

                        # set to false unticked options
                        for g in template['groups']:
                            for myo in g['options']:
                                if myo[1] == 'boolean':
                                    boolean_consumed_options[myo[0].name] = (
                                        cparams.get(myo[0].name, False))
                        for option in boolean_consumed_options:
                            if not option in cparams:
                                cparams[option] = False

                        # cleanup variables
                        names = dict([(var.name, var)
                                      for var in template['template'].vars])
                        for iv in cparams.keys():
                            if not iv in names:
                                del cparams[iv]
                                continue
                            else:
                                if not cparams[iv]:
                                    if not names[iv].default:
                                        del cparams[iv]
                                        continue

                        # be sure not to have unicode params there
                        # because paster will swallow them up
                        top.extend(["%s=%s" % i for i in cparams.items()])
                        cmd = createcmd('create')
                        try:
                            cmd.run(top)
                        except Exception, e:
                            remove_path(output_dir)
                            raise
                    # do post genration stuff
                    postprocess(paster, output_dir_prefix, project, params)

                    if action == 'submit_cgwbDownload':
                        qsparams = dict(request.POST)
                        qsparams.update(params)
                        qsparams.update(boolean_consumed_options)
                        qs = urllib.urlencode(qsparams)
                        ficp = os.path.join(output_dir_prefix,
                                            'LINK_TO_REGENERATE.html')
                        burl = request.route_url(
                            'collect',
                            configuration=request.POST.get('configuration'))
                        genurl = burl + '?%s' % urllib.urlencode(
                            dict(oldparams=zlib.compress(qs, 9).encode(
                                'base64')))
                        fic = open(ficp, 'w')
                        fic.write(
                            '<html><body>'
                            '<a href="%s">'
                            'Click here to go to the generation service</a>'
                            '</body></html>' % genurl)
                        fic.close()
                        ts = '%s' % datetime.datetime.now()
                        ts = ts.replace(':', '-').replace(' ', '_')[:-4]
                        filename = '%s-%s.tar.gz' % (project, ts)
                        file_path = os.path.join(get_generation_path(),
                                                 filename)
                        fic = tarfile.open(file_path, 'w:gz')
                        fic.add(output_dir_prefix, './')
                        fic.close()
                        download_path = os.path.basename(file_path)
                        resp = Response(
                            body=open(file_path).read(),
                            content_type='application/x-tar-gz',
                            headerlist=[
                                ('Content-Disposition',
                                 str('attachment; filename="%s"' %
                                     (os.path.basename(file_path)))),
                                ('Content-Transfer-Encoding', 'binary'),
                                ('Content-Length',
                                 '%s' % (os.path.getsize(file_path))),
                            ],
                            request=request,
                        )
                        os.remove(file_path)

                        return resp
                except NoSuchConfigurationError:
                    errors.append('%s/ The required configuration '
                                  'does not exists : %s' %
                                  (action, configuration))
                except Parser.ParseError, e:
                    errors.append(
                        '<div class="error">'
                        '<p>%s/ Error while reading paster variables:</p>'
                        '<p class="pythonerror">%r</p>'
                        '<p class="pythonerror"><pre>%s</pre></p>'
                        '</div>' % (action, e, e.report()))
                except Exception, e:
                    trace = traceback.format_exc()
                    # raise
                    errors.append(
                        '<div class="error">'
                        '<p>%s/ -- Error while reading paster variables:</p>'
                        '<p class="pythonerror">%r</p>'
                        '<p class="pythonerror">%s</p>'
                        '<p class="pythonerror"><pre>%s</pre>/p>'
                        '</div>' % (action, e, e, trace))
Example #6
0
                        '<p>%s/ Error while reading paster variables:</p>'
                        '<p class="pythonerror">%r</p>'
                        '<p class="pythonerror"><pre>%s</pre></p>'
                        '</div>' % (action, e, e.report()))
                except Exception, e:
                    trace = traceback.format_exc()
                    # raise
                    errors.append(
                        '<div class="error">'
                        '<p>%s/ -- Error while reading paster variables:</p>'
                        '<p class="pythonerror">%r</p>'
                        '<p class="pythonerror">%s</p>'
                        '<p class="pythonerror"><pre>%s</pre>/p>'
                        '</div>' % (action, e, e, trace))
                finally:
                    remove_path(output_dir_prefix)
    main = get_template('templates/main_template.pt').implementation()

    return render_to_response(
        'templates/process.pt',
        dict(errors=errors,
             context=context,
             root=root,
             output=output_dir_prefix,
             download_path=download_path,
             main=main),
        request=request,
    )


def webbuilder_collectinformation(context, request):