forked from microsoft/vcpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_external.py
80 lines (66 loc) · 2.8 KB
/
get_external.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
#! /usr/bin/env python3
# Download additional external files from Dulik FTP server
import os
from ftplib import FTP
def main():
print('Downloading external files from Dulik FTP server...')
cwd_to_script(__file__)
ftp = FtpClient()
ftp.download_tree('/pub/SDK/vcpkg/', '.')
print('Download successful. Now run copy-external.bat to copy the files.')
class FtpClient:
'''Client for Dulik FTP server.'''
def __init__(self):
self.ftp = FTP('dulik.dev.tbscz', 'anonymous', '')
def __exit__(self):
self.ftp.quit()
def download_tree(self, remote_path, local_path, show_just_finished = False):
try:
self.ftp.cwd(remote_path)
os.makedirs(local_path, exist_ok=True)
filenames = self.ftp.nlst()
for filename in filenames:
lpath = os.path.join(local_path, filename)
if "." in filename:
print(f'Downloading from FTP: {filename}')
self.ftp.voidcmd('TYPE I')
tracker = _FtpProgressTracker(self.ftp.size(filename), show_just_finished)
file = open(lpath, 'wb')
self.ftp.retrbinary("RETR " + filename, lambda *args: self.__writeFile(file, tracker, *args), 8192)
file.close()
print('') # after progress print new line
else:
self.download_tree(filename, lpath, show_just_finished)
except Exception as e:
print("Error:", e)
def __writeFile(self, file, tracker, block):
file.write(block)
tracker.handle(block)
class _FtpProgressTracker:
sizeWritten = 0
totalSize = 0
showJustFinished = False
def __init__(self, totalSize, showJustFinished = False):
self.totalSize = totalSize
self.showJustFinished = showJustFinished
def handle(self, block):
self.sizeWritten += len(block)
percentComplete = int(self.sizeWritten * 100 / self.totalSize)
if not self.showJustFinished or (self.showJustFinished and percentComplete == 100):
readableCurrent = readableSize(self.sizeWritten)
readableTotal = readableSize(self.totalSize)
fillerSpaces = ' '
print(f'Progress: {percentComplete}% {readableCurrent} / {readableTotal}{fillerSpaces}', end='\r')
def readableSize(size, precision=2):
suffixes=['B','KB','MB','GB','TB']
suffixIndex = 0
while size >= 1024 and suffixIndex < len(suffixes):
suffixIndex += 1
size = size/1024.0
return "%.*f%s"%(precision,size,suffixes[suffixIndex])
def cwd_to_script(file: str):
# cwd to the script's directory
script_path = os.path.abspath(os.path.dirname(file))
os.chdir(script_path)
if __name__ == '__main__':
main()