Commit 9201436b8d2b9616ce2aa080841f24a1e2765eee
1 parent
80092251
Fix several Python issues in comgendb
Showing
1 changed file
with
48 additions
and
9 deletions
modules/comscripts/comgendb.py
| @@ -105,7 +105,7 @@ class comgendb(ctools.cscript): | @@ -105,7 +105,7 @@ class comgendb(ctools.cscript): | ||
| 105 | """ | 105 | """ |
| 106 | # Set archive directory name | 106 | # Set archive directory name |
| 107 | dirname = gammalib.expand_env(self['archive'].string()) | 107 | dirname = gammalib.expand_env(self['archive'].string()) |
| 108 | - | 108 | + |
| 109 | # Raise an exception if the archive does not exist | 109 | # Raise an exception if the archive does not exist |
| 110 | if not os.path.isdir(dirname): | 110 | if not os.path.isdir(dirname): |
| 111 | msg = ('Archive directory "%s" does not exist.' % dirname) | 111 | msg = ('Archive directory "%s" does not exist.' % dirname) |
| @@ -121,6 +121,45 @@ class comgendb(ctools.cscript): | @@ -121,6 +121,45 @@ class comgendb(ctools.cscript): | ||
| 121 | # Return phases | 121 | # Return phases |
| 122 | return phases | 122 | return phases |
| 123 | 123 | ||
| 124 | + def _get_relpath(self, path, dest): | ||
| 125 | + """ | ||
| 126 | + Get relative path with respect to dest | ||
| 127 | + | ||
| 128 | + Parameters | ||
| 129 | + ---------- | ||
| 130 | + path : str | ||
| 131 | + Full path name | ||
| 132 | + dest : str | ||
| 133 | + Destination path name | ||
| 134 | + | ||
| 135 | + Returns | ||
| 136 | + ------- | ||
| 137 | + relpath : str | ||
| 138 | + Relative path | ||
| 139 | + """ | ||
| 140 | + # Split path and destination in segments | ||
| 141 | + path_split = path.split('/') | ||
| 142 | + dest_split = dest.split('/') | ||
| 143 | + | ||
| 144 | + # Get index of last agreement | ||
| 145 | + index = -1 | ||
| 146 | + for i, segment in enumerate(dest_split): | ||
| 147 | + if segment == path_split[i]: | ||
| 148 | + index = i | ||
| 149 | + | ||
| 150 | + # Set relative path | ||
| 151 | + if index == -1: | ||
| 152 | + relpath = path | ||
| 153 | + else: | ||
| 154 | + relpath = '' | ||
| 155 | + for i in range(index+1,len(path_split)): | ||
| 156 | + if relpath != '': | ||
| 157 | + relpath += '/' | ||
| 158 | + relpath += path_split[i] | ||
| 159 | + | ||
| 160 | + # Return relative path | ||
| 161 | + return relpath | ||
| 162 | + | ||
| 124 | def _get_evp_list(self): | 163 | def _get_evp_list(self): |
| 125 | """ | 164 | """ |
| 126 | Get EVP list | 165 | Get EVP list |
| @@ -189,7 +228,7 @@ class comgendb(ctools.cscript): | @@ -189,7 +228,7 @@ class comgendb(ctools.cscript): | ||
| 189 | for evp in evps: | 228 | for evp in evps: |
| 190 | 229 | ||
| 191 | # Set EVP filename | 230 | # Set EVP filename |
| 192 | - evpname = archive+'/'+os.path.relpath(evp, archive_path).strip('.gz') | 231 | + evpname = archive+'/'+self._get_relpath(evp, archive_path).strip('.gz') |
| 193 | 232 | ||
| 194 | # Try opening file | 233 | # Try opening file |
| 195 | try: | 234 | try: |
| @@ -423,7 +462,7 @@ class comgendb(ctools.cscript): | @@ -423,7 +462,7 @@ class comgendb(ctools.cscript): | ||
| 423 | for oad in oads: | 462 | for oad in oads: |
| 424 | 463 | ||
| 425 | # Set OAD filename | 464 | # Set OAD filename |
| 426 | - oadname = archive+'/'+os.path.relpath(oad, archive_path).strip('.gz') | 465 | + oadname = archive+'/'+self._get_relpath(oad, archive_path).strip('.gz') |
| 427 | 466 | ||
| 428 | # Try opening file | 467 | # Try opening file |
| 429 | try: | 468 | try: |
| @@ -610,7 +649,7 @@ class comgendb(ctools.cscript): | @@ -610,7 +649,7 @@ class comgendb(ctools.cscript): | ||
| 610 | for tim in tims: | 649 | for tim in tims: |
| 611 | 650 | ||
| 612 | # Set TIM filename | 651 | # Set TIM filename |
| 613 | - timname = archive+'/'+os.path.relpath(tim, archive_path).strip('.gz') | 652 | + timname = archive+'/'+self._get_relpath(tim, archive_path).strip('.gz') |
| 614 | 653 | ||
| 615 | # Try opening file | 654 | # Try opening file |
| 616 | try: | 655 | try: |
| @@ -830,7 +869,7 @@ class comgendb(ctools.cscript): | @@ -830,7 +869,7 @@ class comgendb(ctools.cscript): | ||
| 830 | vpname = os.path.basename(vp) | 869 | vpname = os.path.basename(vp) |
| 831 | 870 | ||
| 832 | # Get all TIMs for VP | 871 | # Get all TIMs for VP |
| 833 | - tims = filter(lambda k: k['vp'] == vpname, tim_list) | 872 | + tims = list(filter(lambda k: k['vp'] == vpname, tim_list)) |
| 834 | 873 | ||
| 835 | # Continue if list is empty | 874 | # Continue if list is empty |
| 836 | if len(tims) < 1: | 875 | if len(tims) < 1: |
| @@ -840,7 +879,7 @@ class comgendb(ctools.cscript): | @@ -840,7 +879,7 @@ class comgendb(ctools.cscript): | ||
| 840 | quality = max([tim['quality'] for tim in tims]) | 879 | quality = max([tim['quality'] for tim in tims]) |
| 841 | 880 | ||
| 842 | # Get all TIMs for VP with maximum quality | 881 | # Get all TIMs for VP with maximum quality |
| 843 | - best_tims = filter(lambda k: k['quality'] == quality, tims) | 882 | + best_tims = list(filter(lambda k: k['quality'] == quality, tims)) |
| 844 | 883 | ||
| 845 | # Append these TIMs to the filtered list | 884 | # Append these TIMs to the filtered list |
| 846 | filtered_tim_list.extend(best_tims) | 885 | filtered_tim_list.extend(best_tims) |
| @@ -972,7 +1011,7 @@ class comgendb(ctools.cscript): | @@ -972,7 +1011,7 @@ class comgendb(ctools.cscript): | ||
| 972 | vpname = os.path.basename(vp) | 1011 | vpname = os.path.basename(vp) |
| 973 | 1012 | ||
| 974 | # Get all EVPs for VP | 1013 | # Get all EVPs for VP |
| 975 | - evps = filter(lambda k: k['vp'] == vpname, evp_list) | 1014 | + evps = list(filter(lambda k: k['vp'] == vpname, evp_list)) |
| 976 | 1015 | ||
| 977 | # Initialise best EVP | 1016 | # Initialise best EVP |
| 978 | best_verno_evp = None | 1017 | best_verno_evp = None |
| @@ -1025,7 +1064,7 @@ class comgendb(ctools.cscript): | @@ -1025,7 +1064,7 @@ class comgendb(ctools.cscript): | ||
| 1025 | 1064 | ||
| 1026 | # If there are several EVPs with highest version then select | 1065 | # If there are several EVPs with highest version then select |
| 1027 | # the one with the best quality | 1066 | # the one with the best quality |
| 1028 | - evps = filter(lambda k: k['vp'] == vpname and k['verno'] == best_verno, evp_list) | 1067 | + evps = list(filter(lambda k: k['vp'] == vpname and k['verno'] == best_verno, evp_list)) |
| 1029 | if len(evps) > 1: | 1068 | if len(evps) > 1: |
| 1030 | self._log_string(gammalib.NORMAL, '--- Several EVPs for %s ' | 1069 | self._log_string(gammalib.NORMAL, '--- Several EVPs for %s ' |
| 1031 | 'have highest version' % (vpname)) | 1070 | 'have highest version' % (vpname)) |
| @@ -1062,7 +1101,7 @@ class comgendb(ctools.cscript): | @@ -1062,7 +1101,7 @@ class comgendb(ctools.cscript): | ||
| 1062 | 1101 | ||
| 1063 | # If there are several EVPs with the best quality then select the one | 1102 | # If there are several EVPs with the best quality then select the one |
| 1064 | # with the highest version | 1103 | # with the highest version |
| 1065 | - evps = filter(lambda k: k['vp'] == vpname and k['quality'] == best_quality, evp_list) | 1104 | + evps = list(filter(lambda k: k['vp'] == vpname and k['quality'] == best_quality, evp_list)) |
| 1066 | if len(evps) > 1: | 1105 | if len(evps) > 1: |
| 1067 | self._log_string(gammalib.NORMAL, '--- Several EVPs for %s have ' | 1106 | self._log_string(gammalib.NORMAL, '--- Several EVPs for %s have ' |
| 1068 | 'best quality' % (vpname)) | 1107 | 'best quality' % (vpname)) |