-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyscript.py
More file actions
64 lines (53 loc) · 1.64 KB
/
pyscript.py
File metadata and controls
64 lines (53 loc) · 1.64 KB
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
from contextlib import contextmanager
import subprocess
from os import chdir, mkdir
from os import listdir as ls
from os import getcwd as pwd
from os.path import isdir, isfile
from shutil import copy2 as cp
from shutil import move as mv
# https://stackoverflow.com/a/24176022/11272733
@contextmanager
def cd(newdir):
"""Enters the directory newdir.
To be used as:
with cd('dir'):
...
This construct makes sure, that the directory is left after the
block of the 'with' statement
If 'dir' does not exist, it is made.
"""
if not isdir(newdir):
mkdir(newdir)
prevdir = pwd()
chdir(newdir)
try:
yield
finally:
chdir(prevdir)
def dirs():
"""Returns list of subdirectories in current directory."""
return [obj for obj in ls() if isdir(obj)]
def files():
"""Returns list of subdirectories in current directory."""
return [obj for obj in ls() if isfile(obj)]
def grep(filename, string):
"""Returns a list of all lines of a file which include the given
string pattern."""
lines = []
with open(filename, 'r') as grepfile:
for line in grepfile.readlines():
if string in line:
lines.append(line)
return lines
def rm(string, recursive=False):
"""Calls rm, works with wildcards and arguments like -f."""
if recursive:
string = '-r ' + string
run(f'rm {string}')
def run(line):
"""Executes the given string in the shell."""
return subprocess.check_call(line, shell=True)
def sed(filename, str1, str2):
"""Calls sed to replace strings in files."""
run(f'sed -i "s/{str1}/{str2}/g" {filename}')