Convert from "print ..." to "print(...)"
[hooke.git] / contrib / mfp_igor_scripts / FMjoin.py
1 # Copyright (C) 2010-2012 Alberto Gomez-Casado <a.gomezcasado@tnw.utwente.nl>
2 #                         W. Trevor King <wking@tremily.us>
3 #
4 # This file is part of Hooke.
5 #
6 # Hooke is free software: you can redistribute it and/or modify it under the
7 # terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation, either version 3 of the License, or (at your option) any
9 # later version.
10 #
11 # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
14 # details.
15 #
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
18
19 '''
20 FMjoin.py
21 Copies all .ibw files contained in a folder and its subfolders into a single folder. Useful for force maps.
22
23 Usage: 
24 python FMjoin.py origindir destdir
25 '''
26
27 import os
28 import shutil
29 import sys
30
31 def main(*args):
32         if len(sys.argv) < 2:
33                 print('You must at least specify origin and destination '
34                       'folders.')
35                 return 0
36         origin=sys.argv[1]
37         dest=sys.argv[2]
38    
39         if os.path.exists(origin):
40                 if os.path.exists(dest):
41                         if os.listdir(dest)!=[]:
42                                 print('Destination folder is not empty! Use '
43                                       'another folder.')
44                                 return 0
45                 else:
46                         print('Destination folder does not exist, will create '
47                               'it')
48                         os.mkdir(dest)
49         else:
50                 print('You provided a wrong origin folder name, try again.')
51         
52         origin=os.path.abspath(origin)
53         dest=os.path.abspath(dest)
54         
55         for root, dirs, files in os.walk(origin):
56                 for filename in files:
57                         if filename.split('.')[1]!="ibw":
58                                 continue
59                         filepath=os.path.join(root,filename)
60                         #to avoid overwriting, we collapse unique paths into filenames
61                         rawdest=filepath.split(os.path.commonprefix([origin, filepath]))[1]
62                         rawdest=rawdest.replace('/','') #for linux
63                         rawdest=rawdest.replace('\\','') #for windows
64                         destfile=os.path.join(dest,rawdest)
65                         print('Copying '+rawdest)
66                         shutil.copy(filepath,destfile)
67     
68         return 0
69  
70 if __name__ == '__main__':
71     sys.exit(main(*sys.argv))
72
73