2020-10-26 10:24:30 -07:00
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
import json
|
|
|
|
import time
|
2020-11-27 05:44:21 -08:00
|
|
|
import shutil
|
2020-10-26 10:24:30 -07:00
|
|
|
|
|
|
|
from subprocess import CalledProcessError
|
|
|
|
from json.decoder import JSONDecodeError
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
SCRIPT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
|
|
|
|
|
def get_current_git_ref():
|
2020-11-27 05:44:21 -08:00
|
|
|
output = subprocess.run(['git', '-C', SCRIPT_DIRECTORY, 'rev-parse', '--verify', 'HEAD'],
|
|
|
|
capture_output=True,
|
|
|
|
encoding='utf-8')
|
2020-10-26 10:24:30 -07:00
|
|
|
if output.returncode == 0:
|
|
|
|
return output.stdout.strip()
|
|
|
|
print(f"Failed to get git ref:", output.stderr.strip(), file=sys.stderr)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def generate_port_versions_db(ports_path, db_path, revision):
|
|
|
|
start_time = time.time()
|
2020-11-27 05:44:21 -08:00
|
|
|
|
|
|
|
# Assume each directory in ${VCPKG_ROOT}/ports is a different port
|
|
|
|
port_names = [item for item in os.listdir(
|
|
|
|
ports_path) if os.path.isdir(os.path.join(ports_path, item))]
|
|
|
|
port_names.sort()
|
2020-10-26 10:24:30 -07:00
|
|
|
total_count = len(port_names)
|
2020-11-27 05:44:21 -08:00
|
|
|
|
|
|
|
# Dictionary to collect the latest version of each port as baseline
|
|
|
|
baseline_objects = {}
|
|
|
|
baseline_objects['default'] = {}
|
|
|
|
|
2020-10-26 10:24:30 -07:00
|
|
|
for counter, port_name in enumerate(port_names):
|
|
|
|
containing_dir = os.path.join(db_path, f'{port_name[0]}-')
|
|
|
|
os.makedirs(containing_dir, exist_ok=True)
|
2020-11-27 05:44:21 -08:00
|
|
|
|
2020-10-26 10:24:30 -07:00
|
|
|
output_filepath = os.path.join(containing_dir, f'{port_name}.json')
|
2020-11-27 05:44:21 -08:00
|
|
|
if not os.path.exists(output_filepath):
|
2020-10-26 10:24:30 -07:00
|
|
|
output = subprocess.run(
|
2020-11-27 05:44:21 -08:00
|
|
|
[os.path.join(SCRIPT_DIRECTORY, '../vcpkg'),
|
|
|
|
'x-history', port_name, '--x-json'],
|
2020-10-26 10:24:30 -07:00
|
|
|
capture_output=True, encoding='utf-8')
|
2020-11-27 05:44:21 -08:00
|
|
|
|
2020-10-26 10:24:30 -07:00
|
|
|
if output.returncode == 0:
|
|
|
|
try:
|
|
|
|
versions_object = json.loads(output.stdout)
|
2020-11-27 05:44:21 -08:00
|
|
|
|
|
|
|
# Put latest version in baseline dictionary
|
|
|
|
latest_version = versions_object["versions"][0]
|
|
|
|
baseline_objects['default'][port_name] = {
|
|
|
|
"version-string": latest_version["version-string"],
|
|
|
|
"port-version": latest_version["port-version"]
|
|
|
|
}
|
2020-10-26 10:24:30 -07:00
|
|
|
with open(output_filepath, 'w') as output_file:
|
|
|
|
json.dump(versions_object, output_file)
|
|
|
|
except JSONDecodeError:
|
2020-11-27 05:44:21 -08:00
|
|
|
print(
|
|
|
|
f'Malformed JSON from vcpkg x-history {port_name}: ', output.stdout.strip(), file=sys.stderr)
|
2020-10-26 10:24:30 -07:00
|
|
|
else:
|
2020-11-27 05:44:21 -08:00
|
|
|
print(f'x-history {port_name} failed: ',
|
|
|
|
output.stdout.strip(), file=sys.stderr)
|
|
|
|
|
2020-10-26 10:24:30 -07:00
|
|
|
# This should be replaced by a progress bar
|
|
|
|
if counter > 0 and counter % 100 == 0:
|
|
|
|
elapsed_time = time.time() - start_time
|
2020-11-27 05:44:21 -08:00
|
|
|
print(
|
|
|
|
f'Processed {counter} out of {total_count}. Elapsed time: {elapsed_time:.2f} seconds')
|
|
|
|
|
|
|
|
# Generate baseline.json
|
|
|
|
baseline_file_path = os.path.join(db_path, 'baseline.json')
|
|
|
|
with open(baseline_file_path, 'w') as baseline_output_file:
|
|
|
|
json.dump(baseline_objects, baseline_output_file)
|
|
|
|
|
|
|
|
# Generate timestamp
|
2020-10-26 10:24:30 -07:00
|
|
|
rev_file = os.path.join(db_path, revision)
|
|
|
|
Path(rev_file).touch()
|
2020-11-27 05:44:21 -08:00
|
|
|
|
2020-10-26 10:24:30 -07:00
|
|
|
elapsed_time = time.time() - start_time
|
2020-11-27 05:44:21 -08:00
|
|
|
print(
|
|
|
|
f'Processed {total_count} total ports. Elapsed time: {elapsed_time:.2f} seconds')
|
|
|
|
|
2020-10-26 10:24:30 -07:00
|
|
|
|
|
|
|
def main(ports_path, db_path):
|
|
|
|
revision = get_current_git_ref()
|
|
|
|
if not revision:
|
|
|
|
print('Couldn\'t fetch current Git revision', file=sys.stderr)
|
|
|
|
sys.exit(1)
|
2020-11-27 05:44:21 -08:00
|
|
|
|
2020-10-26 10:24:30 -07:00
|
|
|
rev_file = os.path.join(db_path, revision)
|
|
|
|
if os.path.exists(rev_file):
|
|
|
|
print(f'Database files already exist for commit {revision}')
|
|
|
|
sys.exit(0)
|
2020-11-27 05:44:21 -08:00
|
|
|
|
|
|
|
if (os.path.exists(db_path)):
|
|
|
|
try:
|
|
|
|
shutil.rmtree(db_path)
|
|
|
|
except OSError as e:
|
|
|
|
print(f'Could not delete folder: {db_path}.\nError: {e.strerror}')
|
|
|
|
|
|
|
|
generate_port_versions_db(ports_path=ports_path,
|
|
|
|
db_path=db_path,
|
|
|
|
revision=revision)
|
2020-10-26 10:24:30 -07:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-11-27 05:44:21 -08:00
|
|
|
main(ports_path=os.path.join(SCRIPT_DIRECTORY, '../ports'),
|
|
|
|
db_path=os.path.join(SCRIPT_DIRECTORY, '../port_versions'))
|