-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-upload.py
230 lines (180 loc) · 6.04 KB
/
server-upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""Server Upload
This is a python program used only as a script.
The purpose of this script is to upload the PCRC files on the PARCO Lab
servers in a easy way.
To use this script you have to create a configuration file. You can find a
template of the configuration file in "<project-root>/spec/defconfig.ini". I
suggest to copy this file in the <project-root> directory and edit with your
data. You have to fill the configuration file with:
- Server address;
- Username registered in the server;
- Paths of the folder and file you want to upload relative to the
<project-root> folder.
- Port of the server ssh connection (optional, default 4080);
- Remote server destination folder (optional, default ~/pcrc);
!!! the paths has to be specified with the ", " separator !!!
Template of the configuration file:
[SERVER-UPLOAD]
Paths =
Server =
User =
Port =
RemoteFolder =
Script Usage:
python3 server-upload.py [-h] [--config CONFIG_FILE]
where the CONFIG_FILE is the configuration file path that you have created
with all your server info and by default is the "<project-root>/config.ini"
file.
Functions
---------
get_file_instance(file)
get class File instance of file
compare_hashcode(file1, file2)
calculate the hashcode of file1 and file2 and return True if equals
get_files_path(dir)
get the list of files path in the directory tree
detect_clone(dir)
detect the clone pairs filepath in the dir directory tree
"""
from paramiko import SSHClient
from scp import SCPClient
import sys
import argparse
import getpass
import configparser
import os
# Build the argparse.
parser = argparse.ArgumentParser()
parser.add_argument("--config",
"-c",
dest="config_file",
required=False,
help="Configuration file with all server specification")
args = parser.parse_args()
def get_password(server, username):
"""Get password from console.
Parameters
----------
server: str
Server address.
username: str
Username registered in the server.
"""
return getpass.getpass(prompt="{}@{} password: ".format(username, server))
def connect(server, user, password, port=4080):
"""Connect SSH and SCP client to the server.
Parameters
----------
server: str
Server address.
user: str
Username registered in the server.
password: str
Password for the server authentication.
port: int, optional
Server SSH connection port.
Returns
-------
SCPClient
SCP object related to the server connection.
"""
# Init SSH.
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect(server, port, user, password)
# Callback that prints the current percentage completed for the connection.
# def progress(filename, size, sent):
# sys.stdout.write("%s\'s progress: %.2f%% \r"
# % (filename, float(sent)/float(size)*100) )
def progress(filename, size, sent, peername):
sys.stdout.write("(%s:%s) %s\'s progress: %.2f%% \r" %
(peername[0], peername[1], filename, float(sent)/float(size)*100))
# SCPCLient takes a paramiko transport.
scp = SCPClient(ssh.get_transport(), progress4=progress)
return scp
def close(scp):
"""Free all the script resource allocated.
Parameters
----------
scp: SCPClient
Object related to the SCP server connection.
"""
scp.close()
def upload(scp, folders, destination="~/pcrc"):
"""Upload all the folders to the server.
Parameters
----------
scp: SCPClient
Object related to the SCP server connection.
folders: list
List of path string of the folders and files to upload.
destination: str, optional
Remote destination server path.
"""
# Upload each folder.
for folder in folders:
if not os.path.exists(folder):
continue
scp.put(os.path.join(os.path.dirname(__file__), folder),
recursive=True, remote_path=destination)
sys.stdout.write("\033[K") # Clear line.
print("Completed")
def get_folders_to_upload(paths):
"""Parse folders to upload to the server.
Parameters
----------
paths: str
String of the paths of the folders and files to upload.
Returns
-------
list
List of the paths string of the folders and files to upload.
"""
return paths.split(", ")
def main():
"""Script wrapper"""
# Parse the arguments.
config_file = args.config_file
# Check configuration file.
if not config_file:
print("Config file not selected, take the default config file")
config_file = os.path.join(os.path.dirname(__file__), "config.ini")
# Check if source directory exist
if not os.path.exists(config_file):
print("Path of configuration file specified doesn't exist")
exit()
# Init config parser and retrieve data from configuration file.
config = configparser.ConfigParser()
config.read(config_file)
# Set the global data environment.
paths = config["SERVER-UPLOAD"]["Paths"]
server = config["SERVER-UPLOAD"]["Server"]
user = config["SERVER-UPLOAD"]["User"]
port = config["SERVER-UPLOAD"]["Port"]
remote_folder = config["SERVER-UPLOAD"]["RemoteFolder"]
# Check global data environment.
if not paths:
print("No path specified to upload in configuration file.")
exit()
if not server:
print("No server address specified in configuration file.")
exit()
if not user:
print("No username specified in configuration file.")
exit()
# Get password from user.
password = get_password(server, user)
# Connect using SSH.
if port:
scp = connect(server, user, password, port)
else:
scp = connect(server, user, password)
# Upload files.
if remote_folder:
upload(scp, get_folders_to_upload(paths), remote_folder)
else:
upload(scp, get_folders_to_upload(paths))
# Free memory.
close(scp)
if __name__ == "__main__":
main()