forked from geometer/FBReaderJ
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconverter.py
202 lines (177 loc) · 8.34 KB
/
converter.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
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
202
#!/usr/bin/env python2
import os, sys
# Retreived from Android Project Import Summary, README.md
unmoved = [
"HowToBuild",
"TODO.1.5",
"TODO.2pages",
"TODO.bookInfo",
"TODO.catalogs",
"TODO.dictionary",
"TODO.favorites",
"TODO.highlighting",
"TODO.honeycomb",
"TODO.library",
"TODO.libraryService",
"TODO.litres",
"TODO.network",
"TODO.selection",
"TODO.tips",
"help/"
]
ignored = [
"ChangeLog",
"VERSION",
"AndroidManifest.xml.pattern",
"scripts/",
"project.properties"
]
added = [
"docs/",
"third-party/"
]
conversionMap = {
#In AmbilWarna:
#third-party/drag-sort-listview/library/
"third-party/AmbilWarna/AndroidManifest.xml" : "ambilWarna/src/main/AndroidManifest.xml",
"third-party/AmbilWarna/res/" : "ambilWarna/src/main/res/",
"third-party/AmbilWarna/src/" : "ambilWarna/src/main/java/",
#In code:
#third-party/android-filechooser/code/
"third-party/android-filechooser/code/AndroidManifest.xml" : "code/src/main/AndroidManifest.xml",
"third-party/android-filechooser/code/res/" : "code/src/main/res/",
"third-party/android-filechooser/code/src/" : "code/src/main/java/",
#In library:
#third-party/drag-sort-listview/library/
"third-party/drag-sort-listview/library/AndroidManifest.xml" : "library/src/main/AndroidManifest.xml",
"third-party/drag-sort-listview/library/res/" : "library/src/main/res/",
"third-party/drag-sort-listview/library/src/" : "library/src/main/java/",
#In FBReaderJ:
"AndroidManifest.xml" : "fBReaderJ/src/main/AndroidManifest.xml",
"assets/" : "fBReaderJ/src/main/assets/",
"jni/" : "fBReaderJ/src/main/jni/",
"libs/httpmime-4.2.5.jar" : "fBReaderJ/libs/httpmime-4.2.5.jar",
"libs/json-simple-1.1.1.jar" : "fBReaderJ/libs/json-simple-1.1.1.jar",
"libs/LingvoIntegration_2.5.2.12.jar" : "fBReaderJ/libs/LingvoIntegration_2.5.2.12.jar",
"libs/nanohttpd-2.0.5.jar" : "fBReaderJ/libs/nanohttpd-2.0.5.jar",
"libs/open-dictionary-api-1.2.1.jar" : "fBReaderJ/libs/open-dictionary-api-1.2.1.jar",
"libs/pdfparse.jar" : "fBReaderJ/libs/pdfparse.jar",
"res/" : "fBReaderJ/src/main/res/",
"src/" : "fBReaderJ/src/main/java/",
"src/org/geometerplus/android/fbreader/api/ApiInterface.aidl" : "fBReaderJ/src/main/aidl/org/geometerplus/android/fbreader/api/ApiInterface.aidl",
"src/org/geometerplus/android/fbreader/api/ApiObject.aidl" : "fBReaderJ/src/main/aidl/org/geometerplus/android/fbreader/api/ApiObject.aidl",
"src/org/geometerplus/android/fbreader/api/TextPosition.aidl" : "fBReaderJ/src/main/aidl/org/geometerplus/android/fbreader/api/TextPosition.aidl",
"src/org/geometerplus/android/fbreader/config/ConfigInterface.aidl" : "fBReaderJ/src/main/aidl/org/geometerplus/android/fbreader/config/ConfigInterface.aidl",
"src/org/geometerplus/android/fbreader/formatPlugin/CoverReader.aidl" : "fBReaderJ/src/main/aidl/org/geometerplus/android/fbreader/formatPlugin/CoverReader.aidl",
"src/org/geometerplus/android/fbreader/httpd/DataInterface.aidl" : "fBReaderJ/src/main/aidl/org/geometerplus/android/fbreader/httpd/DataInterface.aidl",
"src/org/geometerplus/android/fbreader/libraryService/LibraryInterface.aidl" : "fBReaderJ/src/main/aidl/org/geometerplus/android/fbreader/libraryService/LibraryInterface.aidl",
"src/org/geometerplus/android/fbreader/libraryService/PositionWithTimestamp.aidl" : "fBReaderJ/src/main/aidl/org/geometerplus/android/fbreader/libraryService/PositionWithTimestamp.aidl",
"src/org/geometerplus/android/fbreader/network/BookDownloaderInterface.aidl" : "fBReaderJ/src/main/aidl/org/geometerplus/android/fbreader/network/BookDownloaderInterface.aidl",
"fbreader/app/" : "fBReaderJ/",
#src/com/paragon/dictionary/fbreader/OpenDictionaryActivity.java ? fbreader/app/src/main/java/com/paragon/dictionary/fbreader/OpenDictionaryActivity.java
}
def getABFileNames(line):
split = line.split()
fileA = ""
fileB = ""
for word in split:
if word.startswith("a/"):
fileA = word[2:]
elif word.startswith("b/"):
fileB = word[2:]
return fileA, fileB
deleteLine = False
deleteLineMarker = "@@[[{{!!TO_BE_DELETED!!}}]]@@"
def convertToAndroidStudioPaths(line):
global deleteLine
print line
a, b = getABFileNames(line)
fullPath = a if b == "" else b
splitPaths = fullPath.split('/')
firstPath = splitPaths[0] + '/' if len(splitPaths) > 1 else splitPaths[0]
firstAndSecondPath = firstPath + (splitPaths[1] + '/') if len(splitPaths) > 1 else ''
deleteLine = False
converted = ""
if firstPath in conversionMap:
if fullPath in conversionMap:
# Then this file is an .aidl file
print "OK: AIDL file \'" + firstPath + "\' is in map as " + conversionMap[firstPath]
converted = conversionMap[fullPath]
else:
print "OK: \'" + firstPath + "\' is in map as " + conversionMap[firstPath]
splitPaths[0] = conversionMap[firstPath][:-1] if len(splitPaths) > 1 else conversionMap[firstPath]
converted = '/'.join(splitPaths)
# purposeful directory renaming in upstream (preparing for gradle build system 2c1d5c53e254a) discovered at 2/5/2016
elif firstAndSecondPath in conversionMap:
print "OK: \'" + firstAndSecondPath + "\' is in map as " + conversionMap[firstAndSecondPath] + "(narrowing procedure)"
# throw away the extra element
splitPaths = splitPaths[1:]
splitPaths[0] = conversionMap[firstAndSecondPath][:-1]
converted = '/'.join(splitPaths)
else:
if firstPath in unmoved:
converted = fullPath
print "UNMOVED: \'" + firstPath + "\' is unmoved"
elif firstPath in ignored:
deleteLine = True
converted = deleteLineMarker
print "IGNORED: \'" + firstPath + "\' is ignored"
elif firstPath in added:
converted = fullPath
print "ADDED: \'" + firstPath + "\' is added"
else:
print "FAIL: \'" + firstPath + "\' is not in map"
sys.exit()
print converted + '\n'
convertedLine = ""
if line.startswith("diff --git a/"):
convertedLine = "diff --git a/" + converted + " b/" + converted
elif line.startswith("--- a/"):
convertedLine = "--- a/" + converted
elif line.startswith("+++ b/"):
convertedLine = "+++ b/" + converted
return deleteLineMarker if deleteLine else convertedLine
def isADiffHeader(line):
return line.startswith("diff --git a/") or line.startswith("--- a/") or line.startswith("+++ b/")
def keepLine(line):
return deleteLineMarker if deleteLine else line
inputFileName = "patch"
outputFileName = ""
# Optional arguments to specify input/output files
try:
inputFileName = sys.argv[1]
outputFileName = sys.argv[2]
except:
{}
# Do the conversion
try:
with open(inputFileName, 'r') as originalPatch:
# Read all lines into memory
lines = [line.rstrip() for line in originalPatch.readlines()]
# Convert paths to android studio paths if it's a diff header, and mark
# the line for deletion if it is a diff for an ignored file
altered_lines = [convertToAndroidStudioPaths(line) if isADiffHeader(line) else keepLine(line) for line in lines]
# Remove the lines marked by deleteLineMarker from our new diff
altered_lines = [line for line in altered_lines if line != deleteLineMarker]
outputFileName = inputFileName + "NEW" if outputFileName == "" else outputFileName
# Open as binary to avoid EOL errors
with open(outputFileName, 'wb') as convertedPatch:
# Rejoin the lines and write to the new file
convertedPatch.write('\n'.join(altered_lines) + '\n')
print "SUCCESS: Converted diff saved as \'" + outputFileName + "\'"
except Exception as e:
print str(e)
"""
Usage: converter.py [inputFile] [outputFile]
This script converts FBReaderJ diffs from the Eclipse project
structure to the Android Studio project structure
git checkout upstream_master
git pull
git diff (SHA of last full sync) > ../patch
git checkout android_studio
converter.py ../patch ../patchNEW
git apply ../patchNEW
Unrecognized files will immediately stop conversion
Default [inputFile] is file name "patch" if no arguments are supplied
Appends "NEW" onto output file name if [outputFile] is not supplied
"""