66
77import argparse
88import os
9+ import sys
910
10- from filetracker .client import Client
11+ from filetracker .client import Client , FiletrackerError
1112from filetracker .scripts import progress_bar
1213
1314# Value used for aligning printed action names
1415_ACTION_LENGTH = 25
1516
1617
1718_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.
1922
2023The intention for this script is to support migration to new filetracker
2124servers that change the format of disk storage.
2528"""
2629
2730
28- def main ():
31+ def main (args = None ):
2932 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' )
3134 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' )
3237 parser .add_argument ('-s' , '--silent' , action = 'store_true' ,
3338 help = 'if set, progress bar is not printed' )
3439
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
3749
3850 # Create a client without local cache.
3951 client = Client (local_store = None , remote_url = url )
@@ -50,7 +62,7 @@ def main():
5062
5163 with progress_bar .conditional (show = not silent ,
5264 widgets = size_widgets ) as bar :
53- for cur_dir , _ , files in os .walk (root ):
65+ for cur_dir , _ , files in os .walk (upload_root ):
5466 for file_name in files :
5567 total_size += os .path .getsize (os .path .join (cur_dir , file_name ))
5668 bar .update (total_size )
@@ -69,18 +81,22 @@ def main():
6981 with progress_bar .conditional (show = not silent ,
7082 max_value = total_size ,
7183 widgets = upload_widgets ) as bar :
72- for cur_dir , _ , files in os .walk (root ):
84+ for cur_dir , _ , files in os .walk (upload_root ):
7385 for file_name in files :
7486 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 )
7688
7789 file_stat = os .stat (file_path )
7890 file_size = file_stat .st_size
7991 file_version = int (file_stat .st_mtime )
8092
8193 remote_name = '{}@{}' .format (remote_path , file_version )
8294
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 )
84100
85101 processed_size += file_size
86102 bar .update (processed_size )
0 commit comments