initial, not yet working gnunet_pytools. dev/ng0/python-modularized
authorng0 <ng0@n0.is>
Sun, 27 Oct 2019 15:00:27 +0000 (15:00 +0000)
committerng0 <ng0@n0.is>
Sun, 27 Oct 2019 15:00:27 +0000 (15:00 +0000)
contrib/scripts/.gitignore
contrib/scripts/Makefile.am
contrib/scripts/gnunet_pytools.py.in [new file with mode: 0644]

index 547c891850b01d96eb07fc8642e76a43e08683f3..b1ddb96991509e0614bb7c49cb74631c84fb01e4 100644 (file)
@@ -1,2 +1,3 @@
 gnunet-chk.py
 removetrailingwhitespace.py
+gnunet_pytools.py
index 5dc94439b41bcf82315f233f96da767fcf98affb..525031761051fc3ffa30b812dd2a26b98e7a1f93 100644 (file)
@@ -9,7 +9,8 @@ noinst_SCRIPTS = \
  removetrailingwhitespace.py \
  gnunet_pyexpect.py \
  gnunet_janitor.py \
- gnunet-chk.py
+ gnunet-chk.py \
+ gnunet_pytools.py
 
 bin_SCRIPTS = \
  gnunet-bugreport
@@ -20,6 +21,7 @@ EXTRA_DIST = \
  gnunet_pyexpect.py.in \
  gnunet_janitor.py.in \
  gnunet-chk.py.in \
+ gnunet_pytools.py.in \
  $(SCRIPTS) \
  removetrailingwhitespace.py.in \
  pydiffer.py.in
diff --git a/contrib/scripts/gnunet_pytools.py.in b/contrib/scripts/gnunet_pytools.py.in
new file mode 100644 (file)
index 0000000..cc02d13
--- /dev/null
@@ -0,0 +1,78 @@
+#!@PYTHON@
+# This file is part of GNUnet
+# (C) 2019 GNUnet e.V.
+#
+# Authors:
+# Author: ng0 <ng0@taler.net>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE
+# LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+# OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
+# THIS SOFTWARE.
+#
+# SPDX-License-Identifier: 0BSD
+
+import os
+from distutils.spawn import find_executable
+
+
+def existence(name):
+    return find_executable(name) is not None
+
+
+# GNUNET_TMP which we suggest to use in place of the absolute definition of
+# /tmp.  So instead of /tmp/foo you would write $GNUNET_TMP/foo.  The usage
+# of $GNUNET_TMP/foo, will result in $TMPDIR/gnunet/foo, or $TMP/gnunet/foo
+# and finally, if TMPDIR is undefined, /tmp/gnunet/foo.
+# 'TEMP' is not (yet) covered by GNUNET_TMP.
+# TODO: merge these two functions?
+def gnunet_tmp_path():
+    if os.environ.get('GNUNET_TMP'):
+        return os.environ.get('GNUNET_TMP')
+    elif os.environ.get('TMPDIR'):
+        return os.environ.get('TMPDIR') + '/gnunet'
+    elif os.environ.get('TMP'):
+        return os.environ.get('TMP') + '/gnunet'
+    elif os.environ.get('TEMP'):
+        return os.environ.get('TEMP') + '/gnunet'
+    else:
+        return '/tmp/gnunet'
+
+
+def tmp_path():
+    if os.environ.get('TMPDIR'):
+        return os.environ.get('TMPDIR')
+    elif os.environ.get('TMP'):
+        return os.environ.get('TMP')
+    elif os.environ.get('TEMP'):
+        return os.environ.get('TEMP')
+    else:
+        return '/tmp'
+
+
+def get_path(name):
+    dpath = {
+        'exec_prefix': '@exec_prefix@',
+        'libexecdir': '@libexecdir@',
+        'gnunet_prefix': os.environ.get('GNUNET_PREFIX', None),
+        'tmp': tmp_path(),
+        'gnunet_tmp': gnunet_tmp_path()
+    }
+    try:
+        return dpath[name]
+    except KeyError:
+        return []
+
+
+def suffix_executable(name):
+    if os.name == 'posix':
+        return string(name)
+    if os.name == 'nt':
+        return string(name + '.exe')