Skip to content

Commit

Permalink
Merge pull request #2139 from camptocamp/fix_project_update
Browse files Browse the repository at this point in the history
Fix project update and the 2.0 branch build
  • Loading branch information
sbrunner committed Apr 12, 2016
2 parents 8feb50a + 4173afe commit 4b61bda
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 123 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ install:
- travis/pip.sh install tilecloud_chain
- .build/venv/bin/pip freeze | grep -v '^-e' | grep -v '^#'
- diff -u c2cgeoportal/scaffolds/update/CONST_versions.txt <(.build/venv/bin/pip freeze | grep -v '^-e' | grep -v '^#') | grep '^[+-]'
- .build/venv/bin/pcreate -s c2cgeoportal_create /tmp/test package=test srid=21781 mobile_application_title="Mobile App éàè" apache_vhost=test
- .build/venv/bin/pcreate -s c2cgeoportal_update /tmp/test package=test srid=21781 apache_vhost=test > /dev/null # on create
- .build/venv/bin/pcreate -s c2cgeoportal_create /tmp/test package=test srid=21781 mobile_application_title="Mobile App éàè" apache_vhost=test extent= > /dev/null
- .build/venv/bin/pcreate -s c2cgeoportal_update /tmp/test package=test srid=21781 mobile_application_title="Mobile App éàè" apache_vhost=test > /dev/null # on create
- cp /tmp/test/project.yaml.mako project.yaml
- .build/venv/bin/pcreate -s c2cgeoportal_update /tmp/test package=test srid=21781 apache_vhost=test > /dev/null # on upgrade
- .build/venv/bin/pcreate -s c2cgeoportal_update /tmp/test > /dev/null # on upgrade - don't add any argument on this command
- rm project.yaml
- travis/build-new-project.sh
- make -f travis.mk build
Expand Down
198 changes: 93 additions & 105 deletions c2cgeoportal/scaffolds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

from pyramid.scaffolds.template import Template
from pyramid.compat import input_
from c2cgeoportal.scripts.c2ctool import _colorize, GREEN


class BaseTemplate(Template): # pragma: no cover
Expand All @@ -57,8 +58,6 @@ def pre(self, command, output_dir, vars):

ret = Template.pre(self, command, output_dir, vars)

self._set_package_in_vars(command, vars)

if vars["package"] == "site":
raise ValueError(
"Sorry, you may not name your package 'site'. "
Expand All @@ -74,39 +73,15 @@ def pre(self, command, output_dir, vars):

return ret

def _set_package_in_vars(self, command, vars):
"""
Set the package into the vars dict.
"""
for arg in command.args:
m = re.match("package=(\w+)", arg)
if m:
vars["package"] = m.group(1)
break

def _set_srid_in_vars(self, command, vars):
"""
Set the SRID into the vars dict.
"""
srid = None
for arg in command.args:
m = re.match("srid=(\d+)", arg)
if m:
srid = m.group(1)
break
if srid is None:
prompt = "Spatial Reference System Identifier " \
"(e.g. 21781): "
srid = input_(prompt).strip()
try:
vars["srid"] = int(srid)
except ValueError:
raise ValueError(
"Specified SRID is not an integer")

def out(self, msg):
print(msg)

def _args_to_vars(self, args, vars):
for arg in args:
m = re.match("(.+)=(.*)", arg)
if m:
vars[m.group(1)] = m.group(2)


class TemplateCreate(BaseTemplate): # pragma: no cover
_template_dir = "create"
Expand All @@ -117,84 +92,87 @@ def pre(self, command, output_dir, vars):
Overrides the base template, adding the "srid" variable to
the variables list.
"""
self._set_apache_vhost_in_vars(command, vars)
self._set_srid_in_vars(command, vars)
self._set_extent_in_vars(command, vars)
self._set_mobile_title_in_vars(command, vars)

self._args_to_vars(command.args, vars)

self._get_vars(vars, "package", "Get a package name: ")
self._get_vars(vars, "apache_vhost", "The Apache vhost name: ")
self._get_vars(
vars, "srid",
"Spatial Reference System Identifier (e.g. 21781): ", int,
)
self._get_vars(
vars, "mobile_application_title",
"The mobile application title: "
)
srid = vars["srid"]
extent = self._epsg2bbox(srid)
self._get_vars(
vars, "extent",
"Extent (minx miny maxx maxy): in EPSG: {srid} projection, default is "
"[{bbox[0]} {bbox[1]} {bbox[2]} {bbox[3]}]: ".format(srid=srid, bbox=extent)
if extent else
"Extent (minx miny maxx maxy): in EPSG: {srid} projection: ".format(srid=srid)
)
match = re.match(r"(\d+)[,; ] *(\d+)[,; ] *(\d+)[,; ] *(\d+)", vars["extent"])
if match is not None:
extent = [match.group(n + 1) for n in range(4)]
vars["extent_mapserver"] = " ".join(extent)
vars["extent_viewer"] = json.dumps(extent)

return BaseTemplate.pre(self, command, output_dir, vars)

def post(self, command, output_dir, vars):
def _get_vars(self, vars, name, prompt, type=None):
"""
Overrides the base template class to print "Welcome to c2cgeoportal!"
after a successful scaffolding rendering.
Set an attribute in the vars dict.
"""

self.out("Welcome to c2cgeoportal!")
return BaseTemplate.post(self, command, output_dir, vars)
value = vars.get(name)

def _set_apache_vhost_in_vars(self, command, vars):
"""
Set the apache_vhost into vars dict.
"""
apache_vhost = None
for arg in command.args:
m = re.match("apache_vhost=(.+)", arg)
if m:
apache_vhost = m.group(1)
break
if value is None:
value = input_(prompt).strip()

if apache_vhost is None:
prompt = "The Apache vhost name:"
apache_vhost = input_(prompt).strip()
if type is not None:
try:
type(value)
except ValueError:
exit("The attribute {} is not a {}".format(name, type))

vars["apache_vhost"] = apache_vhost
vars[name] = value

def _set_mobile_title_in_vars(self, command, vars):
def post(self, command, output_dir, vars):
"""
Set the mobile_title into the vars dict.
Overrides the base template class to print the next step.
"""
mobile_title = None
for arg in command.args:
m = re.match("mobile_application_title=(.+)", arg)
if m:
mobile_title = m.group(1)
break

if mobile_title is None:
prompt = "The mobile application title:"
mobile_title = input_(prompt).strip()
self.out("\nContinue with:")
self.out(_colorize(
".build/venv/bin/pcreate -s c2cgeoportal_update ../{vars[project]} "
"package={vars[package]} srid={vars[srid]} "
"mobile_application_title='{vars[mobile_application_title]}'".format(vars=vars),
GREEN
))

vars["mobile_application_title"] = mobile_title

def _set_extent_in_vars(self, command, vars):
"""
Set the Extent into the vars dict.
"""
extent = None
for arg in command.args:
m = re.match("extent=(\d+, ){3}(\d+ )", arg)
if m:
extent = m.group(1)
break
if extent is None:
extent = self._epsg2bbox(vars["srid"])
if extent is None:
prompt = "Extent (minx, miny, maxx, maxy): " \
"in EPSG:"+vars["srid"]+" projection: "
extent = input_(prompt).strip().split(",")
vars["extent_mapserver"] = " ".join(extent)
vars["extent_viewer"] = json.dumps(extent)
return BaseTemplate.post(self, command, output_dir, vars)

def _epsg2bbox(self, srid):
r = requests.get("http://epsg.io/?format=json&q=%i" % (srid))
bbox = r.json()["results"][0]["bbox"]
r = requests.get("http://epsg.io/trans?s_srs=4326&t_srs=%i&data=%i,%i"
% (srid, bbox[1], bbox[0]))
r1 = r.json()[0]
r = requests.get("http://epsg.io/trans?s_srs=4326&t_srs=%i&data=%i,%i"
% (srid, bbox[3], bbox[2]))
r2 = r.json()[0]
return [r1["x"], r2["y"], r2["x"], r1["y"]]
try:
r = requests.get("http://epsg.io/?format=json&q={}".format(srid))
bbox = r.json()["results"][0]["bbox"]
r = requests.get(
"http://epsg.io/trans?s_srs=4326&t_srs={srid}&data={bbox[1]},{bbox[0]}"
.format(srid=srid, bbox=bbox)
)
r1 = r.json()[0]
r = requests.get(
"http://epsg.io/trans?s_srs=4326&t_srs={srid}&data={bbox[3]},{bbox[2]}"
.format(srid=srid, bbox=bbox)
)
r2 = r.json()[0]
return [r1["x"], r2["y"], r2["x"], r1["y"]]
except IndexError:
print("Unable to get the bbox")
return None


class TemplateUpdate(BaseTemplate): # pragma: no cover
Expand All @@ -207,16 +185,26 @@ def pre(self, command, output_dir, vars):
the variables list.
"""

self._set_srid_in_vars(command, vars)

# Init defaults
vars["mobile_application_title"] = "Geoportal Mobile Application"

if path.exists("project.yaml"):
project = load(file("project.yaml", "r"))
if "template_vars" in project:
for key, value in project["template_vars"].items():
if isinstance(value, string_types):
vars[key] = value.encode("utf-8")
with open("project.yaml", "r") as f:
project = load(f)
if "template_vars" in project:
for key, value in project["template_vars"].items():
vars[key] = \
value.encode("utf-8") \
if isinstance(value, string_types) \
else value

self._args_to_vars(command.args, vars)

return BaseTemplate.pre(self, command, output_dir, vars)

def post(self, command, output_dir, vars):
"""
Overrides the base template class to print "Welcome to c2cgeoportal!"
after a successful scaffolding rendering.
"""

self.out(_colorize("\nWelcome to c2cgeoportal!", GREEN))

return BaseTemplate.post(self, command, output_dir, vars)
2 changes: 2 additions & 0 deletions c2cgeoportal/scaffolds/create/project.yaml.mako_tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ project_package: {{package}}
host: <host>
checker_path: /${instanceid}/wsgi/check_collector?
template_vars:
package: {{package}}
srid: {{srid}}
mobile_application_title: {{mobile_application_title}}
apache_vhost: {{apache_vhost}}
22 changes: 12 additions & 10 deletions c2cgeoportal/scripts/c2ctool.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import shutil
import argparse
import httplib2
from yaml import load
import yaml
from subprocess import check_call, CalledProcessError
from argparse import ArgumentParser
from alembic.config import Config
Expand Down Expand Up @@ -201,7 +201,8 @@ def get_project(self):
print("Unable to find the required 'project.yaml' file.")
exit(1)

return load(file("project.yaml", "r"))
with open("project.yaml", "r") as f:
return yaml.load(f)

def test_checkers(self):
http = httplib2.Http()
Expand Down Expand Up @@ -360,16 +361,17 @@ def step1(self):

check_call([
"%s/pcreate" % self.venv_bin, "--overwrite", "--scaffold=c2cgeoportal_update",
"../%s" % self.project["project_folder"], "package=%s" % self.project["project_package"]
"../%s" % self.project["project_folder"]
])
check_call([
"%s/pcreate" % self.venv_bin, "-s", "c2cgeoportal_create",
pcreate_cmd = [
"%s/pcreate" % self.venv_bin, "--scaffold=c2cgeoportal_create",
"/tmp/%s" % self.project["project_folder"],
"package=%s" % self.project["project_package"],
"mobile_application_title=%s" %
self.project["template_vars"]["mobile_application_title"],
"srid=%s" % self.project["template_vars"].get("srid", 21781),
])
]
pcreate_cmd += [
"{}={}".format(name, value)
for name, value in self.project["template_vars"].items()
]
check_call(pcreate_cmd)
check_call(["make", "-f", self.options.file, self.options.clean])

diff_file = open("changelog.diff", "w")
Expand Down
11 changes: 6 additions & 5 deletions doc/integrator/create_application.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ To create the application first apply the ``c2cgeoportal_create`` skeleton:

.. prompt:: bash

.build/venv/bin/pcreate -s c2cgeoportal_create ../<project> package=<package>
.build/venv/bin/pcreate -s c2cgeoportal_create ../<project>

.. note::

Expand All @@ -124,9 +124,9 @@ it later.

.. prompt:: bash

.build/venv/bin/pcreate -s c2cgeoportal_create ../<project>
package=<package> srid=21781 extent="420000, 30000, 900000, 350000"
apache_vhost=geomapfish
.build/venv/bin/pcreate -s c2cgeoportal_create ../<project> \
package=<package> srid=21781 extent="420000 30000 900000 350000" \
apache_vhost=<vhost> mobile_application_title=<title>

This will create a directory named ``<project>`` that will be next to the
``c2cgeoportal`` directory, or to the directory of the application you're
Expand All @@ -136,7 +136,8 @@ Now apply the ``c2cgeoportal_update`` skeleton:

.. prompt:: bash

.build/venv/bin/pcreate -s c2cgeoportal_update ../<project> package=<package>
.build/venv/bin/pcreate -s c2cgeoportal_update ../<project> package=<package> \
package=<package> srid=<srid> apache_vhost=<vhost> mobile_application_title=<title>

.. note::

Expand Down

0 comments on commit 4b61bda

Please sign in to comment.