forked from SFTtech/openage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inplacemodules.py
77 lines (60 loc) · 2.07 KB
/
inplacemodules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Copyright 2015-2018 the openage authors. See copying.md for legal info.
"""
Installs the Python extension modules that were created in the configuration
specific subdirectory of the build directory to the places where they would
be expected.
Attemts to use OS facilities such as hardlinking, and falls back to copying.
"""
import argparse
import os
from pathlib import Path
import shutil
def main():
""" CLI entry point """
cli = argparse.ArgumentParser()
cli.add_argument("module_list_file", help=(
"semicolon-separated list of all modules that shall be installed "
"in-place."
))
cli.add_argument("configuration", help=(
"the build configuration like Debug or Release"
))
cli.add_argument("--clean", action="store_true", help=(
"remove instead of creating"
))
args = cli.parse_args()
with open(args.module_list_file) as fileobj:
modules = fileobj.read().strip().split(';')
if modules == ['']:
modules = []
for module in modules:
sourcefile = Path(module)
if args.configuration in sourcefile.parts:
# If `sourcefile` has a configuration component, remove it.
file_parts = list(sourcefile.parts)
file_parts.remove(args.configuration)
targetfile = Path(*file_parts)
else:
continue
if targetfile.exists():
if args.clean:
print(targetfile)
targetfile.unlink()
continue
if targetfile.stat().st_mtime >= sourcefile.stat().st_mtime:
continue
targetfile.unlink()
if args.clean:
continue
# try to hardlink
try:
os.link(sourcefile, targetfile)
continue
except OSError:
pass
# we don't try to symlink, because then Python may "intelligently"
# determine the actual location, and refuse to work.
# fallback to copying the file
shutil.copy(sourcefile, targetfile)
if __name__ == '__main__':
main()