This repository has been archived by the owner on Apr 22, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mirrorkan_generate_api.py
129 lines (97 loc) · 4.41 KB
/
mirrorkan_generate_api.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
#!/usr/bin/env python
import os, sys
from distutils.version import LooseVersion
from dateutil.parser import parse
from datetime import datetime, timedelta
import json
import operator
import pytz
from mirrorkan import parse_ckan_metadata_directory
from mirrorkan_conf import *
from mirrorkan_util import find_files_with_extension
def main():
print 'Building CKAN-API..'
ckan_files, ckan_json = parse_ckan_metadata_directory(LOCAL_CKAN_PATH)
latest_versions = {}
all_modules = {}
latest_versions_by_identifier = {}
for ckan_module in ckan_json:
identifier = ckan_module[0]['identifier']
version = ckan_module[0]['version']
py_version = LooseVersion(version)
if identifier in latest_versions:
py_version2 = LooseVersion(latest_versions[identifier])
if py_version > py_version2:
print 'Version %s > %s' % (py_version, py_version2)
latest_versions[identifier] = version
latest_versions_by_identifier[identifier] = ckan_module
else:
latest_versions[identifier] = version
latest_versions_by_identifier[identifier] = ckan_module
for ckan_module in ckan_json:
identifier = ckan_module[0]['identifier']
print 'Generating API for module %s' % identifier
root_path = os.path.join(API_PATH, identifier)
if not os.path.exists(root_path):
os.makedirs(root_path)
version = ckan_module[0]['version']
last_modified = 'unknown'
try:
last_modified = str(parse(ckan_module[0]['x_last_updated']))
except:
pass
if identifier in all_modules:
all_modules[identifier] += [ { 'version': version, 'path': '/%s/%s' % (identifier, version) } ]
else:
all_modules[identifier] = [ { 'version': version, 'path': '/%s/%s' % (identifier, version) } ]
ckan_path = ckan_module[1]
version_path = os.path.join(root_path, version)
if os.path.exists(version_path):
if os.path.isdir(version_path):
os.removedirs(version_path)
else:
os.remove(version_path)
local_path = os.path.join(LOCAL_CKAN_PATH, ckan_path)
print 'Symlink: %s -> %s' % (local_path, version_path)
if os.path.exists(version_path):
os.remove(version_path)
os.symlink(local_path, version_path)
for identifier, version in latest_versions.iteritems():
root_path = os.path.join(API_PATH, identifier)
version_path = os.path.join(root_path, version)
latest_path = os.path.join(root_path, 'latest')
if identifier in all_modules:
all_modules[identifier] += [ { 'version': 'latest', 'path': '/%s/%s' % (identifier, version) } ]
else:
all_modules[identifier] = [ { 'version': 'latest', 'path': '/%s/%s' % (identifier, version) } ]
print 'Symlink: %s -> %s' % (version_path, latest_path)
if os.path.exists(latest_path):
os.remove(latest_path)
os.symlink(version_path, latest_path)
all_path = os.path.join(API_PATH, 'all')
with open(all_path, 'w') as all_file:
print 'Writing %s' % all_path
json.dump(all_modules, all_file)
last_modified = {}
for identifier, version in latest_versions.iteritems():
ckan_module = latest_versions_by_identifier[identifier]
parsed_date = None
try:
parsed_date = parse(ckan_module[0]['x_last_updated'])
except:
pass
if parsed_date != None:
now = pytz.utc.localize(datetime.now() - timedelta(hours=24))
if parsed_date > now:
last_modified['/%s/%s' % (identifier, version)] = str(parsed_date)
last_modified = sorted(last_modified.items(), key=operator.itemgetter(1), reverse=True)
pretty_last_modified = []
for url, time in last_modified:
pretty_last_modified += [ { 'module': url, 'last_updated': time } ]
latest_path = os.path.join(API_PATH, 'latest')
with open(latest_path, 'w') as latest_file:
print 'Writing %s' % latest_path
json.dump(pretty_last_modified, latest_file)
print 'Done!'
if __name__ == "__main__":
main()