From a97572757ded5f323d312ab208d944d000321be9 Mon Sep 17 00:00:00 2001 From: Alex Barreto Date: Mon, 17 May 2021 17:02:08 -0400 Subject: [PATCH] g.proj: fix reading input WKT (#1582) properly terminate input WKT string Co-authored-by: Marc Jansen --- docker/alpine/Dockerfile | 2 +- general/g.proj/input.c | 16 ++++++++++++++ gui/wxpython/gui_core/prompt.py | 35 ++++++++++++++++++++++++++++++ python/grass/.flake8 | 26 ++++++++++++++++++---- scripts/g.extension/g.extension.py | 27 +++++++++++++++++++++++ 5 files changed, 101 insertions(+), 5 deletions(-) diff --git a/docker/alpine/Dockerfile b/docker/alpine/Dockerfile index 5b4cee43faf..365e9b123cb 100644 --- a/docker/alpine/Dockerfile +++ b/docker/alpine/Dockerfile @@ -200,7 +200,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` diff --git a/general/g.proj/input.c b/general/g.proj/input.c index c4232bdb9dd..e89e15f31b2 100644 --- a/general/g.proj/input.c +++ b/general/g.proj/input.c @@ -115,6 +115,7 @@ int input_wkt(char *wktfile) infd = fopen(wktfile, "r"); if (infd) { +<<<<<<< HEAD size_t wktlen; wktlen = fread(buff, 1, sizeof(buff), infd); @@ -128,6 +129,21 @@ int input_wkt(char *wktfile) buff[wktlen] = '\0'; /* Get rid of newlines */ G_squeeze(buff); +======= + 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); +>>>>>>> b3579a4902 (g.proj: fix reading input WKT (#1582)) } else G_fatal_error(_("Unable to open file '%s' for reading"), wktfile); diff --git a/gui/wxpython/gui_core/prompt.py b/gui/wxpython/gui_core/prompt.py index 6734de169a9..81c050c4494 100644 --- a/gui/wxpython/gui_core/prompt.py +++ b/gui/wxpython/gui_core/prompt.py @@ -71,6 +71,41 @@ def __init__(self, parent, giface, menuModel): if giface: giface.currentMapsetChanged.connect(self._reloadListOfMaps) giface.grassdbChanged.connect(self._reloadListOfMaps) +<<<<<<< HEAD +======= + + def _readHistory(self): + """Get list of commands from history file""" + hist = list() + env = grass.gisenv() + try: + fileHistory = codecs.open( + os.path.join( + env["GISDBASE"], + env["LOCATION_NAME"], + env["MAPSET"], + ".bash_history", + ), + encoding="utf-8", + mode="r", + errors="replace", + ) + except IOError: + return hist + + try: + for line in fileHistory.readlines(): + hist.append(line.replace("\n", "")) + finally: + fileHistory.close() + + return hist + + def _loadHistory(self): + """Load history from a history file to data structures""" + self.cmdbuffer = self._readHistory() + self.cmdindex = len(self.cmdbuffer) +>>>>>>> b3579a4902 (g.proj: fix reading input WKT (#1582)) def _getListOfMaps(self): """Get list of maps""" diff --git a/python/grass/.flake8 b/python/grass/.flake8 index d2d3831bb84..bcecb594efa 100644 --- a/python/grass/.flake8 +++ b/python/grass/.flake8 @@ -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. @@ -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 diff --git a/scripts/g.extension/g.extension.py b/scripts/g.extension/g.extension.py index 194f498d49c..19b26a1eeb9 100644 --- a/scripts/g.extension/g.extension.py +++ b/scripts/g.extension/g.extension.py @@ -484,6 +484,29 @@ def replace_shebang_win(python_file): os.rename(tmp_name, python_file) # rename temp to original name +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. @@ -1633,7 +1656,11 @@ def install_extension_win(name): for r, d, f in os.walk(srcdir): for file in f: # Filter GRASS module name patterns +<<<<<<< HEAD if re.search(module_name_pattern, file): +======= + if re.search(r"^[d,db,g,i,m,p,ps,r,r3,s,t,v,wx]\..*[\.py,\.exe]$", file): +>>>>>>> b3579a4902 (g.proj: fix reading input WKT (#1582)) modulename = os.path.splitext(file)[0] module_list.append(modulename) # remove duplicates in case there are .exe wrappers for python scripts