Debian Codegraph @

tools/ci/push_docs_to_repo.py

raw
1 #!/usr/bin/env python3
2
3 import argparse
4 import os
5 import shutil
6 import subprocess
7 import sys
8 import tempfile
9
10 parser = argparse.ArgumentParser(
11 description='Upload files to a remote repo, replacing existing content'
12 )
13 parser.add_argument('dir', help='directory of which content will be uploaded')
14 parser.add_argument('remote', help='remote to which content will be pushed')
15 parser.add_argument('--message', default='Commit bot upload',
16 help='commit message to use')
17 parser.add_argument('--committer', default='numpy-commit-bot',
18 help='Name of the git committer')
19 parser.add_argument('--email', default='numpy-commit-bot@nomail',
20 help='Email of the git committer')
21 parser.add_argument('--count', default=1, type=int,
22 help="minimum number of expected files, defaults to 1")
23
24 parser.add_argument(
25 '--force', action='store_true',
26 help='hereby acknowledge that remote repo content will be overwritten'
27 )
28 args = parser.parse_args()
29 args.dir = os.path.abspath(args.dir)
30
31 if not os.path.exists(args.dir):
32 print('Content directory does not exist')
33 sys.exit(1)
34
35 count = len([name for name in os.listdir(args.dir)
36 if os.path.isfile(os.path.join(args.dir, name))])
37
38 if count < args.count:
39 print(f"Expected {args.count} top-directory files to upload, got {count}")
40 sys.exit(1)
41
42 def run(cmd, stdout=True):
43 pipe = None if stdout else subprocess.DEVNULL
44 try:
45 subprocess.check_call(cmd, stdout=pipe, stderr=pipe)
46 except subprocess.CalledProcessError:
47 print(f"\n! Error executing: `{' '.join(cmd)};` aborting")
48 sys.exit(1)
49
50
51 workdir = tempfile.mkdtemp()
52 os.chdir(workdir)
53
54 run(['git', 'init'])
55 # ensure the working branch is called "main"
56 # (`--initial-branch=main` appeared to have failed on older git versions):
57 run(['git', 'checkout', '-b', 'main'])
58 run(['git', 'remote', 'add', 'origin', args.remote])
59 run(['git', 'config', '--local', 'user.name', args.committer])
60 run(['git', 'config', '--local', 'user.email', args.email])
61
62 print(f'- committing new content: "{args.message}"')
63 run(['cp', '-R', os.path.join(args.dir, '.'), '.'])
64 run(['git', 'add', '.'], stdout=False)
65 run(['git', 'commit', '--allow-empty', '-m', args.message], stdout=False)
66
67 print(f'- uploading as {args.committer} <{args.email}>')
68 if args.force:
69 run(['git', 'push', 'origin', 'main', '--force'])
70 else:
71 print('\n!! No `--force` argument specified; aborting')
72 print('!! Before enabling that flag, make sure you know what it does\n')
73 sys.exit(1)
74
75 shutil.rmtree(workdir)