Add --inline option to legacy_fields_migrator
https://github.com/bazelbuild/bazel/issues/5883
RELNOTES: None.
PiperOrigin-RevId: 227827560
diff --git a/tools/migration/legacy_fields_migrator.py b/tools/migration/legacy_fields_migrator.py
index 806d09d..cc1bb41 100644
--- a/tools/migration/legacy_fields_migrator.py
+++ b/tools/migration/legacy_fields_migrator.py
@@ -24,6 +24,7 @@
flags.DEFINE_string("input", None, "Input CROSSTOOL file to be migrated")
flags.DEFINE_string("output", None,
"Output path where to write migrated CROSSTOOL.")
+flags.DEFINE_boolean("inline", None, "Overwrite --input file")
def main(unused_argv):
@@ -31,31 +32,34 @@
input_filename = flags.FLAGS.input
output_filename = flags.FLAGS.output
+ inline = flags.FLAGS.inline
if not input_filename:
- raise app.UsageError("ERROR input unspecified")
- if not output_filename:
- raise app.UsageError("ERROR output unspecified")
+ raise app.UsageError("ERROR --input unspecified")
+ if not output_filename and not inline:
+ raise app.UsageError("ERROR --output unspecified and --inline not passed")
+ if output_filename and inline:
+ raise app.UsageError("ERROR both --output and --inline passed")
- f = open(to_absolute_path(input_filename), "r")
- input_text = f.read()
+ with open(to_absolute_path(input_filename), "r") as f:
+ input_text = f.read()
+
text_format.Merge(input_text, crosstool)
- f.close()
- output_text = migrate_legacy_fields(crosstool)
-
- f = open(to_absolute_path(output_filename), "w")
+ migrate_legacy_fields(crosstool)
output_text = text_format.MessageToString(crosstool)
- f.write(output_text)
- f.close()
+ resolved_output_filename = to_absolute_path(
+ input_filename if inline else output_filename)
+ with open(resolved_output_filename, "w") as f:
+ f.write(output_text)
def to_absolute_path(path):
path = os.path.expanduser(path)
if os.path.isabs(path):
return path
else:
- if os.environ["BUILD_WORKING_DIRECTORY"]:
+ if "BUILD_WORKING_DIRECTORY" in os.environ:
return os.path.join(os.environ["BUILD_WORKING_DIRECTORY"], path)
else:
return path