/
shpy.py
64 lines (55 loc) · 1.77 KB
/
shpy.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
import os
import shutil
import glob
import subprocess
def glob_all(paths):
for path in paths:
yield from sorted(glob.glob(path))
def cp(*paths, recursive=None):
*sources, dest = glob_all(paths)
if len(sources) == 1 and not os.path.isdir(dest):
if not os.path.isdir(sources[0]) or recursive:
shutil.copyfile(sources[0], dest)
else:
for source in sources:
if not os.path.isdir(source) or recursive:
target = os.path.join(dest, os.path.basename(source))
shutil.copyfile(source, target)
else:
raise Exception("Can't copy directory without recursive flag")
def ls(path=None, all=None):
if all:
return os.listdir(path)
return [fname for fname in os.listdir(path) if not fname.startswith('.')]
def mkdir(path, parents=None):
if parents:
os.makedirs(path)
else:
os.mkdir(path)
def mv(*paths):
*sources, dest = glob_all(paths)
if len(sources) == 1 and not os.path.isdir(dest):
os.rename(sources[0], dest)
else:
for source in sources:
target = os.path.join(dest, os.path.basename(source))
os.rename(source, target)
def touch(path):
with open(path, 'a'):
os.utime(path)
def rm(*paths, recursive=None):
for path in glob_all(paths):
if not os.path.isdir(path):
os.remove(path)
elif recursive:
shutil.rmtree(path)
else:
raise Exception("Can't remove directory without recursive flag")
def run(*cmdlist, stdin=None, stderr=subprocess.STDOUT, check_exit=False):
return subprocess.run(
cmdlist,
stdout=subprocess.PIPE,
stdin=stdin,
stderr=stderr,
check=check_exit,
).stdout.decode()