-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate.py
executable file
·103 lines (80 loc) · 2.92 KB
/
update.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
#!/usr/bin/env python2.7
import simplejson as json
import os
import urllib2
import subprocess
import difflib
import argparse
def getURL(url):
try:
req = urllib2.Request(url)
fp = urllib2.urlopen(req)
except urllib2.HTTPError, e:
print e.code
print e.read()
return fp
def writeAndCommit(fileName, fileContents, comment):
githubWadl = open(fileName, 'w')
githubWadl.write(fileContents)
githubWadl.close()
subprocess.call(['git', 'add' , '.'])
subprocess.call(['git', 'add' , fileName])
subprocess.call(['git', 'commit' , '-m', comment])
os.chdir("..")
def repoHandle(dct):
if os.path.exists(dct['name']):
os.chdir(dct['name'])
diffHandle(dct, getURL(dct['wadlUrl']))
else:
os.mkdir(dct['name'])
os.chdir(dct['name'])
wadlHandle(dct, getURL(dct['wadlUrl']))
def wadlHandle(dct, apigeeWadl):
filename = dct['wadlUrl'].split('/')[-1] \
.split('#')[0] \
.split('?')[0]
comment = 'Added directory "{0}" and file "{1}"'.format(dct['name'], filename)
writeAndCommit(filename, apigeeWadl.read(), comment)
def diffHandle(dct, apigeeWadl):
fileList = os.listdir(".")
if len(fileList) > 1:
multFileDiff(dct, apigeeWadl, fileList)
elif len(fileList) == 1:
fc = apigeeWadl.read()
if difflib.SequenceMatcher(lambda x: x in " \t", fc,fileList[0]).real_quick_ratio() > 0:
comment = 'Updated file: "{0}" in "{1}" directory'.format(fileList[0], dct['name'])
writeAndCommit(fileList[0], fc, comment)
else:
os.chdir('..')
else:
wadlHandle(dct, apigeeWadl)
"""Will get called each time in a new directory"""
def multFileDiff(dct, apigeeWadl, fileList):
fc = apigeeWadl.read()
lowestRatio = float
lowestRatio = difflib.SequenceMatcher(lambda x: x in " \t", fc,fileList[0]).real_quick_ratio()
lowestFile = fileList[0]
for i in fileList[1:]:
tempRatio = float
tempRatio = difflib.SequenceMatcher(lambda x: x in " \t", fc,i).real_quick_ratio()
if tempRatio < lowestRatio:
lowestRatio = tempRatio
lowestFile = i
if lowestRatio > float(0):
comment = 'Updated file: "{0}" in "{1}" directory'.format(lowestFile, dct['name'])
writeAndCommit(lowestFile, fc, comment)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--directory', required=False)
args = parser.parse_args()
if args.directory:
os.chdir(args.directory)
else:
# Clone git repo to local system
subprocess.call(['git', 'clone', 'git://github.com/apigee/wadl-library.git', 'wadl-library'])
os.chdir("wadl-library")
urlObj = getURL("http://api.apigee.com/v1/consoles.json")
jsonData = json.load(urlObj)
map(repoHandle, jsonData['console'])
if __name__ == '__main__':
main()