get_upstream_patch: Validate patch application

Currently, get_upstream_patch does not validate that a patch applies to
the current LLVM state.

Add validation before modifying PATCHES.json.

Move several functions into patch_utils to avoid depending on
patch_manager.

BUG=b:227216280
TEST=./get_upstream_patch.py --platform chromiumos --sha [patch SHA]

Change-Id: I97e7d401e7f8fc6d85dbfb9a310e4a77205ef444
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/3711269
Reviewed-by: Adrian Dole <adriandole@google.com>
Commit-Queue: Adrian Dole <adriandole@google.com>
Auto-Submit: Adrian Dole <adriandole@google.com>
Reviewed-by: Jordan Abrahams-Whitehead <ajordanr@google.com>
Tested-by: Adrian Dole <adriandole@google.com>
7 files changed