Debian Codegraph @

lib/jnpr/junos/utils/ftp.py

raw
1 """
2 FTP utility
3 """
4
5 import re
6 import ftplib
7 import os
8 import logging
9
10 logger = logging.getLogger("jnpr.junos.utils.ftp")
11
12
13 class FTP(ftplib.FTP):
14 """
15 FTP utility can be used to transfer files to and from device.
16 """
17
18 def __init__(self, junos, **ftpargs):
19 """
20
21 :param Device junos: Device object
22 :param kvargs ftpargs: any additional args to be passed to ftplib FTP
23
24 Supports python *context-manager* pattern. For example::
25
26 from jnpr.junos.utils.ftp import FTP
27 with FTP(dev) as ftp:
28 ftp.put(package, remote_path)
29 """
30
31 self._junos = junos
32 self._ftpargs = ftpargs
33 ftplib.FTP.__init__(
34 self,
35 self._junos._hostname,
36 self._junos._auth_user,
37 self._junos._auth_password,
38 )
39
40 # dummy function, as connection is created by ftb lib in __init__ only
41 def open(self):
42 return self
43
44 def put(self, local_file, remote_path=None):
45 """
46 This function is used to upload file to the router from local
47 execution server/shell.
48
49 :param local_file: Full path along with filename which has to be
50 copied to router
51 :param remote_path: path in which to receive the files on the remote
52 host. If ignored FILE will be copied to "tmp"
53 :returns: True if the transfer succeeds, else False
54
55 """
56
57 try:
58 mat = re.search("^.*/(.*)$", local_file)
59 if mat:
60 if not remote_path:
61 remote_file = "/tmp/" + mat.group(1)
62 else:
63 if re.search("^.*/(.*)$", remote_path) and re.search(
64 r"\.\w+$", remote_path
65 ):
66 remote_file = remote_path
67 # Looks like remote path is given as file location
68 else:
69 remote_file = os.path.join(remote_path, mat.group(1))
70 else:
71 if not remote_path:
72 remote_file = os.path.join("/tmp/", local_file)
73 else:
74 remote_file = os.path.join(remote_path, local_file)
75
76 with open(local_file, "rb") as open_local_file:
77 self.storbinary(
78 cmd="STOR " + remote_file,
79 fp=open_local_file,
80 callback=self._ftpargs.get("callback"),
81 )
82 except Exception as ex:
83 logger.error(ex)
84 return False
85 return True
86
87 def get(self, remote_file, local_path=os.getcwd()):
88 """
89 This function is used to download file from router to local execution
90 server/shell.
91
92 :param local_path: path in which to receive files locally
93
94 :param remote_file: Full path along with filename on the router. If
95 ignored FILE will be copied to "tmp"
96
97 :returns: True if the transfer succeeds, else False
98 """
99 if os.path.isdir(local_path):
100 mat = re.search("^.*/(.*)$", remote_file)
101 if mat:
102 local_file = os.path.join(local_path, mat.group(1))
103 else:
104 local_file = local_path
105 else:
106 local_file = local_path
107
108 with open(local_file, "wb") as local_fh:
109 args_callback = self._ftpargs.get("callback")
110
111 def callback(data):
112 local_fh.write(data)
113 if args_callback:
114 args_callback(data)
115
116 try:
117 self.retrbinary("RETR " + remote_file, callback)
118 except Exception as ex:
119 logger.error(ex)
120 return False
121 return True
122
123 # -------------------------------------------------------------------------
124 # CONTEXT MANAGER
125 # -------------------------------------------------------------------------
126
127 def __enter__(self):
128 # return self.open(**self._ftpargs)
129 return self
130
131 def __exit__(self, exc_ty, exc_val, exc_tb):
132 return self.close()