Skip to content

Commit

Permalink
g.proj: fix reading input WKT (OSGeo#1582)
Browse files Browse the repository at this point in the history
properly terminate input WKT string

Co-authored-by: Marc Jansen <jansen@terrestris.de>
  • Loading branch information
a0x8o and marcjansen committed May 17, 2021
1 parent 7e8f036 commit 7c10386
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 26 deletions.
2 changes: 1 addition & 1 deletion docker/alpine/Dockerfile_alpine
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ COPY --from=build /usr/local/grass* /usr/local/grass/
RUN apk add curl && curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python3 get-pip.py pip==20.0.2 && rm get-pip.py

# install external Python API
RUN pip3 install --upgrade grass-session
RUN pip3 install --upgrade pip six grass-session --ignore-installed six

RUN ln -s /usr/local/grass /usr/local/grass7
RUN ln -s /usr/local/grass `grass --config path`
Expand Down
10 changes: 8 additions & 2 deletions general/g.proj/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static void set_authnamecode(OGRSpatialReferenceH);
int input_wkt(char *wktfile)
{
FILE *infd;
char buff[8000], *tmpwkt;
char buff[8192], *tmpwkt;
OGRSpatialReferenceH hSRS;
char *papszOptions[3];
int ret;
Expand All @@ -117,11 +117,17 @@ int input_wkt(char *wktfile)
infd = fopen(wktfile, "r");

if (infd) {
fread(buff, sizeof(buff), 1, infd);
size_t wktlen;

wktlen = fread(buff, 1, sizeof(buff), infd);
if (wktlen == sizeof(buff))
G_fatal_error(_("Input WKT definition is too long"));
if (ferror(infd))
G_fatal_error(_("Error reading WKT definition"));
else
fclose(infd);
/* terminate WKT string */
buff[wktlen] = '\0';
/* Get rid of newlines */
G_squeeze(buff);
}
Expand Down
3 changes: 2 additions & 1 deletion gui/wxpython/dbmgr/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4047,7 +4047,8 @@ def Update(self, driver, database, table, column):
)
varSum = 0
for var in decode(dataVar).splitlines():
varSum += float(var)
if var:
varSum += float(var)
stddev = math.sqrt(varSum / count)

self.SetTitle(_("Field statistics <%s>") % column)
Expand Down
8 changes: 8 additions & 0 deletions gui/wxpython/gui_core/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ def __init__(self, parent, giface, menuModel):
# list of traced commands
self.commands = list()

# reload map lists when needed
if giface:
giface.currentMapsetChanged.connect(self._reloadListOfMaps)
giface.grassdbChanged.connect(self._reloadListOfMaps)

def _readHistory(self):
"""Get list of commands from history file"""
hist = list()
Expand Down Expand Up @@ -110,6 +115,9 @@ def _getListOfMaps(self):

return result

def _reloadListOfMaps(self):
self.mapList = self._getListOfMaps()

def _runCmd(self, cmdString):
"""Run command
Expand Down
26 changes: 22 additions & 4 deletions python/grass/.flake8
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
ignore =
E203, # whitespace before ':' (Black)
W503, # line break before binary operator (Black)
E722, # do not use bare 'except'

per-file-ignores =
# C wrappers call libgis.G_gisinit before importing other modules.
Expand All @@ -29,12 +28,31 @@ per-file-ignores =
script/db.py: E501
script/task.py: W605
script/vector.py: E501 # Long doctest lines which need review anyway
temporal/*.py: E501, F841
temporal/abstract_space_time_dataset.py: W605, E501, F841
temporal/temporal_algebra.py: E741, E501, F841
temporal/abstract_map_dataset.py: E501
temporal/abstract_space_time_dataset.py: W605, E501, F841, E722
temporal/aggregation.py: E501
temporal/base.py: E501
temporal/c_libraries_interface.py: E501, F841, E722
temporal/core.py: E501, E722
temporal/datetime_math.py: E501, F841, E722
temporal/list_stds.py: E501
temporal/metadata.py: E501
temporal/open_stds.py: F841
temporal/register.py: E501
temporal/space_time_datasets.py: E501
temporal/spatial_extent.py: E501
temporal/spatial_topology_dataset_connector.py: E501, E722
temporal/spatio_temporal_relationships.py: E501
temporal/temporal_algebra.py: E741, E501, F841, E722
temporal/temporal_extent.py: E501
temporal/temporal_granularity.py: E501, F841, E722
temporal/temporal_operator.py: E501
temporal/temporal_raster_algebra.py: E741
temporal/temporal_raster_base_algebra.py: E501, F841, E722
temporal/temporal_raster3d_algebra.py: E741
temporal/temporal_topology_dataset_connector.py: E501, E722
temporal/temporal_vector_algebra.py: E741, E501, F841
temporal/univar_statistics.py: E501
# Current benchmarks/tests are changing sys.path before import.
# Possibly, a different approach should be taken there anyway.
pygrass/tests/benchmark.py: E501, E402, F401, F821
Expand Down
3 changes: 0 additions & 3 deletions python/grass/temporal/stds_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,6 @@ def import_stds(
)

os.chdir(old_cwd)
except:
raise

# Make sure the location is switched back correctly
finally:
if location:
Expand Down
2 changes: 1 addition & 1 deletion python/grass/temporal/temporal_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
try:
import ply.lex as lex
import ply.yacc as yacc
except:
except ImportError:
pass


Expand Down
2 changes: 1 addition & 1 deletion python/grass/temporal/temporal_raster3d_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

try:
import ply.yacc as yacc
except:
except ImportError:
pass

from .temporal_raster_base_algebra import (
Expand Down
2 changes: 1 addition & 1 deletion python/grass/temporal/temporal_raster_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

try:
import ply.yacc as yacc
except:
except ImportError:
pass

from .temporal_raster_base_algebra import (
Expand Down
2 changes: 1 addition & 1 deletion python/grass/temporal/temporal_vector_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

try:
import ply.yacc as yacc
except:
except ImportError:
pass

import grass.pygrass.modules as pygrass
Expand Down
39 changes: 28 additions & 11 deletions scripts/g.extension/g.extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
import fileinput
import http
import os
import codecs
import sys
import re
import atexit
Expand Down Expand Up @@ -190,6 +191,29 @@
HTTP_STATUS_CODES = list(http.HTTPStatus)


def replace_shebang_win(python_file):
"""
Replaces "python" with "python3" in python files
using UTF8 encoding on MS Windows
"""

cur_dir = os.path.dirname(python_file)
tmp_name = os.path.join(cur_dir, gscript.tempname(12))

with codecs.open(python_file, "r", encoding="utf8") as in_file, codecs.open(
tmp_name, "w", encoding="utf8"
) as out_file:

for line in in_file:
new_line = line.replace(
"#!/usr/bin/env python\n", "#!/usr/bin/env python3\n"
)
out_file.write(new_line)

os.remove(python_file) # remove original
os.rename(tmp_name, python_file) # rename temp to original name


def urlretrieve(url, filename, *args, **kwargs):
"""Same function as 'urlretrieve', but with the ability to
define headers.
Expand Down Expand Up @@ -1291,11 +1315,9 @@ def install_extension_win(name):
module_list = list()
for r, d, f in os.walk(srcdir):
for file in f:
if file.endswith(".py"):
modulename = file.rsplit(".py")[0]
module_list.append(modulename)
if file.endswith(".exe"):
modulename = file.rsplit(".exe")[0]
# Filter GRASS module name patterns
if re.search(r"^[d,db,g,i,m,p,ps,r,r3,s,t,v,wx]\..*[\.py,\.exe]$", file):
modulename = os.path.splitext(file)[0]
module_list.append(modulename)
# remove duplicates in case there are .exe wrappers for python scripts
module_list = set(module_list)
Expand All @@ -1308,12 +1330,7 @@ def install_extension_win(name):
pyfiles.append(os.path.join(r, file))

for filename in pyfiles:
with fileinput.FileInput(filename, inplace=True) as file:
for line in file:
print(
line.replace("#!/usr/bin/env python\n", "#!/usr/bin/env python3\n"),
end="",
)
replace_shebang_win(filename)

# collect old files
old_file_list = list()
Expand Down

0 comments on commit 7c10386

Please sign in to comment.