File: install-file.py

package info (click to toggle)
arc-theme 20221218-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 12,444 kB
  • sloc: xml: 4,691; python: 94; makefile: 6
file content (40 lines) | stat: -rwxr-xr-x 1,014 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3

# Copies (installs) file(s) into a new location.
#
# Usage:
#   install-file.py SOURCE... DESTINATION
#
# Copy SOURCE file as DESTINATION file if DESTINATION ends with
# a filename extension, or copy SOURCE file(s) into DESTINATION
# directory.
#
# All leading components of DESTINATION path are created if they
# don't exist. Paths can contain environment variables. If SOURCE
# is a symlink, a new symlink in created instead of installing
# the file the link points to.

import sys
import os
import shutil

source = sys.argv[1:-1]
dest = os.path.expandvars(sys.argv[-1])

if not os.path.splitext(dest)[1]:
  dest_dir = dest
else:
  if len(source) == 1:
    dest_dir = os.path.dirname(dest)
  else:
    raise Exception("Too many source files") 

os.makedirs(dest_dir, exist_ok=True)

for src in source:
  try:
    shutil.copy(src, dest, follow_symlinks=False)
  except FileExistsError:
    if os.path.islink(dest):
      os.remove(dest)
      shutil.copy(src, dest, follow_symlinks=False)