This repository was archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodules.py
More file actions
132 lines (99 loc) · 3.62 KB
/
modules.py
File metadata and controls
132 lines (99 loc) · 3.62 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
import os
import redis
import re
import logging
from bottle import abort
from distutils.version import LooseVersion
"""
magerun --skip-root-check --root-dir=/data/web/public --no-interaction sys:modules:list --status=active --format=json | curl -d @- -X POST -H "Content-Type: application/json" http://modules.hypernode.com/query.txt
"""
POSTDATA_PATH = os.path.dirname(os.path.realpath(__file__)) + '/../fixtures/magerundumps'
REDIS_VERSION_HASH = 'magmodules'
REDIS_HOST = 'localhost'
redis_client = redis.Redis(
host=REDIS_HOST,
decode_responses=True,
)
def magerun(request, format):
ip = request.environ.get('REMOTE_ADDR')
# temp for debugging erroneous magerun output
with open(POSTDATA_PATH + '/' + ip, 'wb') as f:
f.write(request.body.read())
try:
decoded_json = request.json
assert decoded_json
except:
abort(400, "Invalid post data, could not decode JSON")
latest_versions = store_and_find_latest_versions(decoded_json.values())
if format == 'json':
return dict(versions=latest_versions)
else:
return versions_to_text(latest_versions)
def store_and_find_latest_versions(modules):
latest_versions = []
for module in modules:
# empty or invalid version
if not is_valid_version(module['Version']):
logging.warning("Cannot parse version '%(Version)s' for module '%(Name)s', skipping" % module)
continue
try:
latest = store_and_get_latest_version(module)
except (TypeError, AttributeError) as e:
# LooseVersion is not very good
logging.error("oeps, exception for module: %s %s" % (e, module))
continue
# do this after storing, because i am interested in gathering core versions as well
if module['codePool'] == 'core':
continue
latest_versions.append(latest)
return sorted(latest_versions, key=lambda x: x['name'])
def versions_to_text(versions):
lines = []
for v in versions:
if v['latest'] != v['current']:
lines.append("%(name)-32s %(current)-12s %(latest)-12s\n" % v)
if not lines:
return "All local/community modules seem up to date!\n"
header = [
"%-32s %-12s %-12s\n" % ('Name', 'Current', 'Latest known'),
"==============================================================\n"
]
return ''.join(header + lines)
def is_valid_version(v):
if not v:
return False
if not LooseVersion(v):
return False
return re.match('^[\.\w\d\-\_]+$', v)
def store_and_get_latest_version(module):
"""
module =
{
"codePool": "community",
"Name": "Phoenix_Moneybookers",
"Version": "1.6.0.0",
"Status": "active"
}
"""
name = module['Name']
given = module['Version']
stored = get_version(name)
if not stored or LooseVersion(given) > LooseVersion(stored):
# print("Storing new version! Name, old, new:", name, old, new)
store_version(name, given)
latest = given
else:
latest = stored
return dict(name=name, latest=latest, current=given)
def get_version(name):
return redis_client.hget(REDIS_VERSION_HASH, name)
def store_version(name, version):
rv = redis_client.hset(REDIS_VERSION_HASH, name, version)
logging.info("Storing version for %(name)s, %(version)s, %(rv)s" % locals())
return rv
if __name__ == '__main__':
assert is_valid_version('1.0.1')
assert is_valid_version('1.03-patch')
assert not is_valid_version('1.0\nrm%20-rf /')
assert not is_valid_version('')
assert not is_valid_version(None)