-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
47 lines (41 loc) · 1.38 KB
/
Copy pathfunctions.py
File metadata and controls
47 lines (41 loc) · 1.38 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
# ------------------------------------------------------------------------------
# Python Scripts scriptarium/[functions.py]
# (c) balarabe@protonmail.com
# ------------------------------------------------------------------------------
import os
import re
# describes a file size as a human-friendly value
def format_size(num, suffix='B'):
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti']:
if abs(num) < 1024.0:
return '%3.1f%s%s' % (num, unit, suffix)
num /= 1024.0
return '%.1f%s%s' % (num, 'Pi', suffix)
# returns the first integer from a given string
def get_int(s):
m = re.search(r'\d+', s)
if m == None:
return 0
return int(m.group())
# lists all files in 'dir' and its subfolders
def list_files(dir):
ret = []
for fname in os.listdir(dir):
path = os.path.join(dir, fname)
if os.path.isdir(path):
ret += list_files(path)
else:
ret.append(path)
return ret
# lists all paths within directory 'dir' that contain Git repositories
def list_git_repos(dir):
ret = []
for fname in os.listdir(dir):
if fname == '.git':
ret.append(dir)
else:
path = os.path.join(dir, fname)
if os.path.isdir(path):
ret += list_git_repos(path)
return ret
# end