-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfabutils.py
executable file
·142 lines (117 loc) · 3.82 KB
/
fabutils.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function
import difflib
import filecmp
import os
import sys
import addict
import termcolor
import yaml
CLIENTS = "clients.yaml"
def get_yaml_cfg(afile):
"""From a yaml file, return the yaml structure.
"""
data = None
if os.path.isfile(afile):
with open(afile, "r") as f:
txt = f.read()
data = yaml.load(txt)
return data
def select_client_cfg(letters, quiet=False):
"""Get the client that matches the letters given as argument.
exple: select_client("loc") will select localhost.
return: the client's configuration dictionnary.
"""
cfg = get_yaml_cfg(CLIENTS)
cfg = addict.Dict(cfg)
clients = cfg.clients
cl = filter(lambda it: it.name.startswith(letters), clients)
if len(cl) > 1:
print("Found more than one possible clients, can't decide: {}".format([it.name for it in cl]))
# return []
exit(1)
if not cl:
if not quiet:
print("No client found with '{}'".format(sys.argv[1]))
print("existing clients:")
print([it.name for it in cfg.clients])
exit(1)
return []
return cl[0]
def whose_port(number, cfg):
"""
"""
clients = cfg.clients
clt = filter(lambda it: it.port == number, clients)
if len(clt) > 1:
print("Warning ! Many clients have the same port number")
return clt
def _print_status(status):
if not status:
return "?"
if "abandoned" in status:
return termcolor.colored(status, "red")
if "staling" in status:
return termcolor.colored(status, "yellow")
if "prod" in status:
return termcolor.colored(status, "blue")
return status
def print_client(client):
print(termcolor.colored("- {:15} ".format(client.name), "blue") + "\t {}".format(client.port) + "\t{}".format(_print_status(client.status)))
def wd(client, cfg):
"""Get the working directory.
"""
cfg = get_yaml_cfg(CLIENTS)
cfg = addict.Dict(cfg)
if client.wd:
return client.wd
home = "/home/root/"
if client.home:
home = client.home
else:
home = '/home/{}'.format(client.user or cfg.user)
path = os.path.join(home, cfg.dir, client.name, cfg.project_name)
return path
def bundle_needs_update():
"""Was package.json modified since last time we
uploaded our bundled js dependencies ?
Simply check if these files changed, against a cached version.
Note: uneeded ? We rely on rsync.
Return: bool
"""
ret = False
package_json = "package.json"
package_cache = "package.json.bundlecache"
# The caches exist ?
if not os.path.exists(package_cache):
print(termcolor.colored("package.json has no cache, so we don't know if it changed since last upload. Need rebuild.", "yellow"))
ret = True
if ret:
return True
# Do the files look different ?
ret = False
if not filecmp.cmp(package_json, package_cache):
print(termcolor.colored("package.json seems to have changed", "yellow"))
package_lines = open(package_json, "r").readlines()
package_cache_lines = open(package_cache, "r").readlines()
diff = difflib.unified_diff(package_cache_lines, package_lines)
diff_print(diff)
ret = True # they don't seem equal, they need an update
return ret
def diff_print(generator):
"""Colorful print.
- generator: given by unified diff
"""
for line in generator:
if line.startswith("-"):
print(termcolor.colored(line, "red"))
elif line.startswith("+"):
print(termcolor.colored(line, "green"))
else:
print(line)
def yes_answer(answer):
"""
"""
return answer in ["Y", "y", "yes", "O", "o"]