in tools/cloud-composer-migration-complexity-assessment/airflow-v1-to-v2-migration/misc/migration.py [0:0]
def migrate_files(self, comment_flag, comment):
impacted_files = []
change_count = 0
inp_len = len(self.input_dir)
for root,dirs,files in os.walk(self.input_dir):
for file in files:
if file.endswith(".py"):
filepath = root + "/"+file
new_dir = self.output_dir+root[inp_len:]
isExist = os.path.exists(new_dir)
if not isExist:
os.makedirs(new_dir)
new_file = new_dir+"/"+file
change_count = 0
with open(filepath, 'r') as f, open(new_file, 'w') as temp:
for line in f:
# check if a comment
# add feature to identify multi-line comments and ignore
if line.startswith('#'):
temp.write(line)
continue
# check if this is an import statement
mod_name, imported_names = parse_import_statement(line)
if mod_name is not None:
tmpLine = ''
for idx, rec in enumerate(imported_names):
imp_stmt = ''
if mod_name:
imp_stmt = 'from ' + mod_name + ' '
imp_stmt += 'import ' + rec
if imp_stmt in self.replacement_dict:
change_count += 1
if self.add_comments:
if self.comments:
comment = '# ' + self.comments + '\n'
else:
comment = '# Migration Utility Generated Comment -- Change Type = ' + \
self.replacement_dict[imp_stmt][1] + " , Impact = " + \
self.replacement_dict[imp_stmt][3] + '\n'
temp.write(comment)
tmpLine = tmpLine + self.replacement_dict[imp_stmt][2] + '\n'
else:
tmpLine = line
line = tmpLine
else:
# extract function call
matches = re.findall(self.function_regex, line)
if matches:
# search if required to replace
for rec in matches:
if rec in self.replacement_dict:
change_count += 1
if self.add_comments:
if self.comments:
comment = '# ' + self.comments + '\n'
else:
comment = '# Migration Utility Generated Comment -- Change Type = ' + \
self.replacement_dict[rec][1] + " , Impact = " + \
self.replacement_dict[rec][3] + '\n'
temp.write(comment)
line = line.replace(rec, self.replacement_dict[rec][2])
if rec+"(" in self.replacement_dict or rec+" (" in self.replacement_dict:
change_count +=1
space_count = len(line) - len(line.lstrip())
spaces=''
for i in range(space_count+4) :
spaces = ' '+spaces
if self.add_comments:
if self.comments:
comment = '# ' + self.comments + '\n'
else:
comment = '# Migration Utility Generated Comment -- Change Type = ' + \
self.replacement_dict[rec+"("][1] + " , Impact = " + \
self.replacement_dict[rec+"("][3] + '\n'
temp.write(comment)
line = line+spaces +self.replacement_dict[rec+"("][2]+",\n"
for word, replacement in self.replacement_dict.items():
if replacement[1] == "Argument Replace" and replacement[4] == "TRUE" and replacement[5] == "FALSE":
if word in line:
change_count +=1
if self.add_comments:
if self.comments:
comment = '# ' + self.comments + '\n'
else:
comment = '# Migration Utility Generated Comment -- Change Type = ' + \
replacement[1] + " , Impact = " + \
replacement[3] + '\n'
temp.write(comment)
line = line.replace(word,replacement[2])
break
temp.write(line)
if change_count > 0:
impacted_files.append(file)
if self.report_generation:
self.print_report(impacted_files)