-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathnpm
executable file
·53 lines (41 loc) · 1.25 KB
/
npm
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
#!/usr/bin/env python3
# Copyright 2019 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Run npm with the right settings."""
import contextlib
import os
from pathlib import Path
import sys
import libdot
@contextlib.contextmanager
def temp_switch_path_owner(path: os.PathLike):
"""Change |path| owner to root temporarily.
Starting with node-16, npm will automatically change uid/gid when the
current user is root. It determines that target uid/gid based on the
owner of the cwd. To defeat that logic, we have to temporarily change
the cwd ownership to root.
"""
switch = os.getuid() == 0
if switch:
st = path.stat()
uid = st.st_uid
gid = st.st_gid
os.chown(path, 0, 0)
try:
yield
finally:
if switch:
os.chown(path, uid, gid)
def run(cmd, **kwargs):
"""Run the npm |cmd|."""
cwd = Path(kwargs.get("cwd", "."))
with temp_switch_path_owner(cwd):
return libdot.node.run(["npm"] + cmd, **kwargs)
def main(argv):
"""The main func!"""
libdot.setup_logging()
libdot.node_and_npm_setup()
run(argv)
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))