-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.py
More file actions
201 lines (179 loc) · 8.57 KB
/
utils.py
File metadata and controls
201 lines (179 loc) · 8.57 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""
Helper functions used by the Drycc server.
"""
import base64
import concurrent
import hashlib
import logging
import random
import math
from copy import deepcopy
logger = logging.getLogger(__name__)
def generate_app_name():
"""Return a randomly-generated memorable name."""
adjectives = [
'ablest', 'absurd', 'actual', 'aerial', 'allied', 'artful', 'atomic', 'august',
'bamboo', 'benign', 'blonde', 'blurry', 'bolder', 'breezy', 'bubbly', 'burley',
'candid', 'calmer', 'casual', 'cheery', 'classy', 'clever', 'convex', 'cubist',
'dainty', 'dapper', 'decent', 'deluxe', 'docile', 'dogged', 'drafty', 'dreamy',
'earthy', 'easier', 'echoed', 'edible', 'elfish', 'excess', 'exotic', 'expert',
'fabled', 'famous', 'feline', 'finest', 'flaxen', 'folksy', 'frisky', 'frozen',
'gaslit', 'gentle', 'gifted', 'ginger', 'global', 'golden', 'grassy', 'guided',
'haptic', 'hearty', 'hidden', 'hipper', 'honest', 'humble', 'hungry', 'hushed',
'iambic', 'iciest', 'iconic', 'indoor', 'inward', 'ironic', 'island', 'italic',
'jagged', 'jangly', 'jaunty', 'jicama', 'jiggly', 'jovial', 'joyful', 'junior',
'kabuki', 'karmic', 'keener', 'kiddie', 'kindly', 'kingly', 'klutzy', 'knotty',
'lambda', 'latest', 'leader', 'linear', 'lively', 'lonely', 'loving', 'luxury',
'madcap', 'madras', 'marble', 'mellow', 'metric', 'modest', 'molten', 'mystic',
'native', 'nearby', 'nested', 'newish', 'nickel', 'nimbus', 'nonfat', 'normal',
'oblong', 'oddest', 'offset', 'oldest', 'onside', 'orange', 'outlaw', 'owlish',
'padded', 'pastry', 'peachy', 'pepper', 'player', 'preset', 'proper', 'pulsar',
'quacky', 'quaint', 'quartz', 'queens', 'queued', 'quinoa', 'quirky', 'quoted',
'racing', 'rental', 'ribbed', 'rising', 'rococo', 'rubber', 'rugged', 'rustic',
'sanest', 'scenic', 'seeing', 'shadow', 'skiing', 'stable', 'steely', 'syrupy',
'taller', 'tender', 'tested', 'timely', 'trendy', 'triple', 'truthy', 'twenty',
'ultima', 'unbent', 'unisex', 'united', 'upbeat', 'uphill', 'usable', 'utmost',
'vacuum', 'valued', 'vanity', 'velcro', 'velvet', 'verbal', 'violet', 'vulcan',
'walkup', 'webbed', 'wicker', 'wiggly', 'wilder', 'wonder', 'wooden', 'woodsy',
'yearly', 'yeasty', 'yellow', 'yeoman', 'yogurt', 'yonder', 'youthy', 'yuppie',
'zaftig', 'zanier', 'zephyr', 'zeroed', 'zigzag', 'zipped', 'zircon', 'zydeco',
]
nouns = [
'addendum', 'anaconda', 'airfield', 'aqualung', 'armchair', 'asteroid', 'autoharp',
'babushka', 'backbone', 'bagpiper', 'barbecue', 'bookworm', 'bullfrog', 'buttress',
'caffeine', 'checkers', 'chinbone', 'countess', 'crawfish', 'cucumber', 'cutpurse',
'daffodil', 'darkroom', 'deadbolt', 'doghouse', 'dragster', 'drumroll', 'duckling',
'earrings', 'earthman', 'eggplant', 'electron', 'elephant', 'espresso', 'eyetooth',
'falconer', 'farmland', 'ferryman', 'fireball', 'fishbone', 'footwear', 'frosting',
'gadabout', 'gasworks', 'gatepost', 'gemstone', 'gladness', 'goldfish', 'greenery',
'hacienda', 'handbill', 'hardtack', 'hawthorn', 'headwind', 'henhouse', 'huntress',
'icehouse', 'idealist', 'inchworm', 'instinct', 'inventor', 'insignia', 'ironwood',
'jailbird', 'jamboree', 'jerrycan', 'jetliner', 'jokester', 'joyrider', 'jumpsuit',
'kangaroo', 'keepsake', 'kerchief', 'keypunch', 'kingfish', 'knapsack', 'knothole',
'ladybird', 'lakeside', 'lambskin', 'landmass', 'larkspur', 'lollipop', 'lungfish',
'macaroni', 'mackinaw', 'magician', 'mainsail', 'milepost', 'mongoose', 'moonrise',
'nailhead', 'nautilus', 'neckwear', 'newsreel', 'nonesuch', 'novelist', 'nuthatch',
'occupant', 'odometer', 'offering', 'offshoot', 'original', 'organism', 'overalls',
'pachinko', 'painting', 'pamphlet', 'paneling', 'pendulum', 'playroom', 'ponytail',
'quacking', 'quadrant', 'quantity', 'queendom', 'question', 'quilting', 'quotient',
'rabbitry', 'radiator', 'renegade', 'ricochet', 'riverbed', 'rosewood', 'rucksack',
'sailfish', 'sandwich', 'sculptor', 'seashore', 'seedcake', 'skylight', 'stickpin',
'tabletop', 'tailbone', 'teamwork', 'teaspoon', 'tinkerer', 'traverse', 'turbojet',
'umbrella', 'underdog', 'undertow', 'unicycle', 'universe', 'uptowner', 'utensils',
'vacation', 'vagabond', 'valkyrie', 'variable', 'villager', 'vineyard', 'vocalist',
'waggoner', 'waxworks', 'waterbed', 'wayfarer', 'whitecap', 'windmill', 'woodshed',
'yachting', 'yardbird', 'yardwork', 'yearbook', 'yearling', 'yeomanry', 'yodeling',
'zaniness', 'zeppelin', 'ziggurat', 'zillions', 'zirconia', 'zoologer', 'zucchini',
]
return "{}-{}".format(
random.choice(adjectives), random.choice(nouns))
def dict_diff(dict1, dict2):
"""
Returns the added, changed, and deleted items in dict1 compared with dict2.
:param dict1: a python dict
:param dict2: an earlier version of the same python dict
:return: a new dict, with 'added', 'changed', and 'removed' items if
any were found.
>>> d1 = {1: 'a'}
>>> dict_diff(d1, d1)
{}
>>> d2 = {1: 'a', 2: 'b'}
>>> dict_diff(d2, d1)
{'added': {2: 'b'}}
>>> d3 = {2: 'B', 3: 'c'}
>>> expected = {'added': {3: 'c'}, 'changed': {2: 'B'}, 'deleted': {1: 'a'}}
>>> dict_diff(d3, d2) == expected
True
"""
diff = {}
set1, set2 = set(dict1), set(dict2)
# Find items that were added to dict2
diff['added'] = {k: dict1[k] for k in (set1 - set2)}
# Find common items whose values differ between dict1 and dict2
diff['changed'] = {
k: dict1[k] for k in (set1 & set2) if dict1[k] != dict2[k]
}
# Find items that were deleted from dict2
diff['deleted'] = {k: dict2[k] for k in (set2 - set1)}
return {k: diff[k] for k in diff if diff[k]}
def fingerprint(key):
"""
Return the fingerprint for an SSH Public Key
"""
key = base64.b64decode(key.strip().split()[1].encode('ascii'))
fp_plain = hashlib.md5(key).hexdigest()
return ':'.join(a + b for a, b in zip(fp_plain[::2], fp_plain[1::2]))
def dict_merge(origin, merge):
"""
Recursively merges dict's. not just simple a["key"] = b["key"], if
both a and b have a key who's value is a dict then dict_merge is called
on both values and the result stored in the returned dictionary.
Also handles merging lists if they occur within the dict
"""
if not isinstance(merge, dict):
return merge
result = deepcopy(origin)
for key, value in merge.items():
if key in result and isinstance(result[key], dict):
result[key] = dict_merge(result[key], value)
else:
if isinstance(value, list):
if key not in result:
result[key] = value
else:
# merge lists without leaving potential duplicates
# result[key] = list(set(result[key] + value)) # changes the order as well
for item in value:
if item in result[key]:
continue
result[key].append(item)
else:
result[key] = deepcopy(value)
return result
def apply_tasks(tasks):
"""
run a group of tasks async
Requires the tasks arg to be a list of functools.partial()
"""
if not tasks:
return
executor = concurrent.futures.ThreadPoolExecutor(5)
for future in [executor.submit(task) for task in tasks]:
error = future.exception()
if error is not None:
raise error
executor.shutdown(wait=True)
def unit_to_bytes(size):
"""
size: str
where unit in K, M, G, T convert to B
"""
if size[-2:-1].isalpha() and size[-1].isalpha():
size = size[:-1]
if size[-1].isalpha():
size = size.upper()
_ = float(size[:-1])
if size[-1] == 'K':
_ *= math.pow(1024, 1)
elif size[-1] == 'M':
_ *= math.pow(1024, 2)
elif size[-1] == 'G':
_ *= math.pow(1024, 3)
elif size[-1] == 'G':
_ *= math.pow(1024, 3)
elif size[-1] == 'T':
_ *= math.pow(1024, 4)
elif size[-1] == 'P':
_ *= math.pow(1024, 5)
return round(_)
def unit_to_millicpu(cpu):
cpu = cpu.lower()
if cpu.endswith("m") and cpu[:-1].isdigit():
return int(cpu[:-1])
elif cpu.isdigit():
return int(cpu) * 1000
else:
raise ValueError("Unrecognized CPU unit: %s" % cpu)
if __name__ == "__main__":
import doctest
doctest.testmod()