in src/doc_builder/commands/push.py [0:0]
def push_command_add(args):
"""
Commit file changes using: 1. zip doc build artifacts 2. hf_hub client to upload zip file
Used in: build_main_documentation.yml & build_pr_documentation.yml
"""
max_n_retries = args.n_retries + 1
number_of_retries = args.n_retries
n_seconds_sleep = 5
library_name = args.library_name
path_docs_built = Path(library_name)
doc_version_folder = next(filter(lambda x: not x.is_file(), path_docs_built.glob("*")), None).relative_to(
path_docs_built
)
doc_version_folder = str(doc_version_folder)
zip_file_path = create_zip_name(library_name, doc_version_folder)
# eg create ./transformers/v4.0.zip with '/transformers/v4.0/*' file architecture inside
# Use subprocess.run instead of shutil.make_archive to avoid corrupted files, see https://github.com/huggingface/doc-builder/issues/348
print(f"Running zip command: zip -r {zip_file_path} {path_docs_built}")
subprocess.run(["zip", "-r", zip_file_path, path_docs_built], check=True)
api = HfApi()
time_start = time()
while number_of_retries:
try:
if args.upload_version_yml:
# removing doc artifact folder to upload 2 files using `upload_folder`: _version.yml and zipped doc artifact file
shutil.rmtree(f"{library_name}/{doc_version_folder}")
api.upload_folder(
repo_id=args.doc_build_repo_id,
repo_type=REPO_TYPE,
folder_path=library_name,
path_in_repo=library_name,
commit_message=args.commit_msg,
token=args.token,
)
else:
api.upload_file(
repo_id=args.doc_build_repo_id,
repo_type=REPO_TYPE,
path_or_fileobj=zip_file_path,
path_in_repo=zip_file_path,
commit_message=args.commit_msg,
token=args.token,
)
break
except Exception as e:
number_of_retries -= 1
print(f"push_command_add error occurred: {e}")
if number_of_retries:
print(f"Failed on try #{max_n_retries-number_of_retries}, pushing again in {n_seconds_sleep} seconds")
sleep(n_seconds_sleep)
else:
raise RuntimeError("push_command_add failed") from e
time_end = time()
logging.debug(f"push_command_add took {time_end-time_start:.4f} seconds or {(time_end-time_start)/60.0:.2f} mins")