summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorerkin <e.altunbas@vyos.io>2021-06-09 16:23:57 +0300
committererkin <e.altunbas@vyos.io>2021-06-09 16:23:57 +0300
commit1789e0afa8cbdc650ef73bc021fca09ec1ae6056 (patch)
treef06c121b4a9af264e26c845cc1d9c2a1cda400ea /python
parent6bbabdf2adaec1f6fa05926d373020e99e1a6ccd (diff)
downloadvyos-1x-1789e0afa8cbdc650ef73bc021fca09ec1ae6056.tar.gz
vyos-1x-1789e0afa8cbdc650ef73bc021fca09ec1ae6056.zip
remote, util: Move general helper routines from `remote.py` to `util.py`
Diffstat (limited to 'python')
-rw-r--r--python/vyos/remote.py52
-rw-r--r--python/vyos/util.py49
2 files changed, 52 insertions, 49 deletions
diff --git a/python/vyos/remote.py b/python/vyos/remote.py
index b11838ac0..3013c2f24 100644
--- a/python/vyos/remote.py
+++ b/python/vyos/remote.py
@@ -14,7 +14,6 @@
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
from ftplib import FTP
-import math
import os
import shutil
import socket
@@ -25,6 +24,9 @@ import urllib.request as urlreq
from vyos.util import cmd
from vyos.util import ask_yes_no
+from vyos.util import print_error
+from vyos.util import make_progressbar
+from vyos.util import make_incremental_progressbar
from vyos.template import is_ipv6
from vyos.version import get_version
from paramiko import SSHClient
@@ -53,54 +55,6 @@ class InteractivePolicy(MissingHostKeyPolicy):
## Helper routines
-def print_error(str='', end='\n'):
- """
- Print `str` to stderr, terminated with `end`.
- Used for warnings and out-of-band messages to avoid mangling precious
- stdout output.
- """
- sys.stderr.write(str)
- sys.stderr.write(end)
- sys.stderr.flush()
-
-def make_progressbar():
- """
- Make a procedure that takes two arguments `done` and `total` and prints a
- progressbar based on the ratio thereof, whose length is determined by the
- width of the terminal.
- """
- col, _ = shutil.get_terminal_size()
- col = max(col - 15, 20)
- def print_progressbar(done, total):
- if done <= total:
- increment = total / col
- length = math.ceil(done / increment)
- percentage = str(math.ceil(100 * done / total)).rjust(3)
- print_error(f'[{length * "#"}{(col - length) * "_"}] {percentage}%', '\r')
- # Print a newline so that the subsequent prints don't overwrite the full bar.
- if done == total:
- print_error()
- return print_progressbar
-
-def make_incremental_progressbar(increment: float):
- """
- Make a generator that displays a progressbar that grows monotonically with
- every iteration.
- First call displays it at 0% and every subsequent iteration displays it
- at `increment` increments where 0.0 < `increment` < 1.0.
- Intended for FTP and HTTP transfers with stateless callbacks.
- """
- print_progressbar = make_progressbar()
- total = 0.0
- while total < 1.0:
- print_progressbar(total, 1.0)
- yield
- total += increment
- print_progressbar(1, 1)
- # Ignore further calls.
- while True:
- yield
-
def get_authentication_variables(default_username=None, default_password=None):
"""
Return the environment variables `$REMOTE_USERNAME` and `$REMOTE_PASSWORD` and
diff --git a/python/vyos/util.py b/python/vyos/util.py
index 16fcbf10b..7fea6cdc6 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -718,3 +718,52 @@ def cidr_fit(cidr_a, cidr_b, both_directions = False):
if both_directions:
return prefix_a.startswith(prefix_b) or prefix_b.startswith(prefix_a)
return prefix_a.startswith(prefix_b)
+
+def print_error(str='', end='\n'):
+ """
+ Print `str` to stderr, terminated with `end`.
+ Used for warnings and out-of-band messages to avoid mangling precious
+ stdout output.
+ """
+ sys.stderr.write(str)
+ sys.stderr.write(end)
+ sys.stderr.flush()
+
+def make_progressbar():
+ """
+ Make a procedure that takes two arguments `done` and `total` and prints a
+ progressbar based on the ratio thereof, whose length is determined by the
+ width of the terminal.
+ """
+ import shutil, math
+ col, _ = shutil.get_terminal_size()
+ col = max(col - 15, 20)
+ def print_progressbar(done, total):
+ if done <= total:
+ increment = total / col
+ length = math.ceil(done / increment)
+ percentage = str(math.ceil(100 * done / total)).rjust(3)
+ print_error(f'[{length * "#"}{(col - length) * "_"}] {percentage}%', '\r')
+ # Print a newline so that the subsequent prints don't overwrite the full bar.
+ if done == total:
+ print_error()
+ return print_progressbar
+
+def make_incremental_progressbar(increment: float):
+ """
+ Make a generator that displays a progressbar that grows monotonically with
+ every iteration.
+ First call displays it at 0% and every subsequent iteration displays it
+ at `increment` increments where 0.0 < `increment` < 1.0.
+ Intended for FTP and HTTP transfers with stateless callbacks.
+ """
+ print_progressbar = make_progressbar()
+ total = 0.0
+ while total < 1.0:
+ print_progressbar(total, 1.0)
+ yield
+ total += increment
+ print_progressbar(1, 1)
+ # Ignore further calls.
+ while True:
+ yield