6
6
7
7
import argparse
8
8
import os
9
+ import sys
9
10
10
- from filetracker .client import Client
11
+ from filetracker .client import Client , FiletrackerError
11
12
from filetracker .scripts import progress_bar
12
13
13
14
# Value used for aligning printed action names
14
15
_ACTION_LENGTH = 25
15
16
16
17
17
18
_DESCRIPTION = """
18
- Uploads all files to a remote filetracker server.
19
+ Uploads files to a remote filetracker server.
20
+
21
+ Use --root parameter to upload only parts of the storage.
19
22
20
23
The intention for this script is to support migration to new filetracker
21
24
servers that change the format of disk storage.
25
28
"""
26
29
27
30
28
- def main ():
31
+ def main (args = None ):
29
32
parser = argparse .ArgumentParser (description = _DESCRIPTION )
30
- parser .add_argument ('files' , help = 'root of the file tree to be uploaded' )
33
+ parser .add_argument ('files' , help = 'file tree to be uploaded' )
31
34
parser .add_argument ('url' , help = 'URL of the filetracker server' )
35
+ parser .add_argument ('--root' ,
36
+ help = 'the directory that corresponds to the storage root' )
32
37
parser .add_argument ('-s' , '--silent' , action = 'store_true' ,
33
38
help = 'if set, progress bar is not printed' )
34
39
35
- args = parser .parse_args ()
36
- root , url , silent = args .files , args .url , args .silent
40
+ args = parser .parse_args (args )
41
+
42
+ upload_root = args .files
43
+ url = args .url
44
+ storage_root = args .root
45
+ silent = args .silent
46
+
47
+ if storage_root is None :
48
+ storage_root = upload_root
37
49
38
50
# Create a client without local cache.
39
51
client = Client (local_store = None , remote_url = url )
@@ -50,7 +62,7 @@ def main():
50
62
51
63
with progress_bar .conditional (show = not silent ,
52
64
widgets = size_widgets ) as bar :
53
- for cur_dir , _ , files in os .walk (root ):
65
+ for cur_dir , _ , files in os .walk (upload_root ):
54
66
for file_name in files :
55
67
total_size += os .path .getsize (os .path .join (cur_dir , file_name ))
56
68
bar .update (total_size )
@@ -69,18 +81,22 @@ def main():
69
81
with progress_bar .conditional (show = not silent ,
70
82
max_value = total_size ,
71
83
widgets = upload_widgets ) as bar :
72
- for cur_dir , _ , files in os .walk (root ):
84
+ for cur_dir , _ , files in os .walk (upload_root ):
73
85
for file_name in files :
74
86
file_path = os .path .join (cur_dir , file_name )
75
- remote_path = '/' + os .path .relpath (file_path , root )
87
+ remote_path = '/' + os .path .relpath (file_path , storage_root )
76
88
77
89
file_stat = os .stat (file_path )
78
90
file_size = file_stat .st_size
79
91
file_version = int (file_stat .st_mtime )
80
92
81
93
remote_name = '{}@{}' .format (remote_path , file_version )
82
94
83
- client .put_file (remote_name , file_path , to_local_store = False )
95
+ try :
96
+ client .put_file (remote_name , file_path , to_local_store = False )
97
+ except FiletrackerError as e :
98
+ print ('ERROR when uploading {}:\n {}' .format (file_path , e ),
99
+ file = sys .stderr )
84
100
85
101
processed_size += file_size
86
102
bar .update (processed_size )
0 commit comments