onnxruntime/tools/ci_build/replace_urls_in_deps.py
Changming Sun 29ed8811e5
Move C/C++ deps' URLs to deps.txt (#13769)
### Description
1. Move C/C++ deps' URLs to deps.txt, and download the dependencies from
Azure Devops Artifacts instead of github.
2. Add "EXCLUDE_FROM_ALL" keyword to the cmake external projects, so
that we only build the parts we need and avoid installing the 3rd-party
dependencies when people run `make install` in ORT's build directory.
However, at this moment cmake itself doesn't have the feature. So I
copied their code to cmake/external/helper_functions.cmake and modified
it.

This PR is split from #13523, to make that one smaller. 

### Motivation and Context
1. Secure the supply chain
2. Make it be possible to automatically detect if ORT has an old
dependency that hasn't been updated from a long time.
2022-11-29 18:06:35 -08:00

74 lines
2.3 KiB
Python

#!/usr/bin/env python3
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# This file replaces https URLs in deps.txt to local file paths. It runs after we download the dependencies from Azure
# DevOps Artifacts
import argparse
import csv
import os
from dataclasses import dataclass
from pathlib import Path
@dataclass(frozen=True)
class Dep:
name: str
url: str
sha1_hash: str
def parse_arguments():
parser = argparse.ArgumentParser()
# The directory that contains downloaded zip files
parser.add_argument("--new_dir", required=False)
return parser.parse_args()
def main():
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
REPO_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, "..", ".."))
args = parse_arguments()
new_dir = None
if args.new_dir:
new_dir = Path(args.new_dir)
else:
BUILD_BINARIESDIRECTORY = os.environ.get("BUILD_BINARIESDIRECTORY")
if BUILD_BINARIESDIRECTORY is None:
raise NameError("Please specify --new_dir or set the env var BUILD_BINARIESDIRECTORY")
new_dir = Path(BUILD_BINARIESDIRECTORY) / "deps"
# Here we intentionally do not check if new_dir exists, because it might be used in a docker container instead.
deps = []
csv_file_path = Path(REPO_DIR) / "cmake" / "deps.txt"
# Read the whole file into memory first
with csv_file_path.open("r", encoding="utf-8") as f:
depfile_reader = csv.reader(f, delimiter=";")
for row in depfile_reader:
if len(row) != 3:
continue
# Lines start with "#" are comments
if row[0].startswith("#"):
continue
deps.append(Dep(row[0], row[1], row[2]))
# Write updated content back
with csv_file_path.open("w", newline="", encoding="utf-8") as f:
depfile_writer = csv.writer(f, delimiter=";")
for dep in deps:
if dep.url.startswith("https://"):
new_url = new_dir / dep.url[8:]
depfile_writer.writerow([dep.name, new_url.as_posix(), dep.sha1_hash])
else:
# Write the original thing back
depfile_writer.writerow([dep.name, dep.url, dep.sha1_hash])
if __name__ == "__main__":
main()