The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | # common python utility routines for the Bionic tool scripts |
| 2 | |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame^] | 3 | import commands |
| 4 | import logging |
| 5 | import os |
| 6 | import string |
| 7 | import sys |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 8 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 9 | |
| 10 | def panic(msg): |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame^] | 11 | sys.stderr.write(os.path.basename(sys.argv[0]) + ": error: ") |
| 12 | sys.stderr.write(msg) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 13 | sys.exit(1) |
| 14 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 15 | |
| 16 | def find_program_dir(): |
| 17 | return os.path.dirname(sys.argv[0]) |
| 18 | |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame^] | 19 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 20 | class StringOutput: |
| 21 | def __init__(self): |
| 22 | self.line = "" |
| 23 | |
| 24 | def write(self,msg): |
| 25 | self.line += msg |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame^] | 26 | logging.debug("write '%s'" % msg) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 27 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 28 | def get(self): |
| 29 | return self.line |
| 30 | |
| 31 | |
| 32 | def create_file_path(path): |
| 33 | dirs = [] |
| 34 | while 1: |
| 35 | parent = os.path.dirname(path) |
| 36 | #print "parent: %s <- %s" % (parent, path) |
| 37 | if parent == "/" or parent == "": |
| 38 | break |
| 39 | dirs.append(parent) |
| 40 | path = parent |
| 41 | |
| 42 | dirs.reverse() |
| 43 | for dir in dirs: |
| 44 | #print "dir %s" % dir |
| 45 | if os.path.isdir(dir): |
| 46 | continue |
| 47 | os.mkdir(dir) |
| 48 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 49 | |
| 50 | class BatchFileUpdater: |
| 51 | """a class used to edit several files at once""" |
| 52 | def __init__(self): |
| 53 | self.old_files = set() |
| 54 | self.new_files = set() |
| 55 | self.new_data = {} |
| 56 | |
| 57 | def readFile(self,path): |
| 58 | #path = os.path.realpath(path) |
| 59 | if os.path.exists(path): |
| 60 | self.old_files.add(path) |
| 61 | |
| 62 | def readDir(self,path): |
| 63 | #path = os.path.realpath(path) |
| 64 | for root, dirs, files in os.walk(path): |
| 65 | for f in files: |
| 66 | dst = "%s/%s" % (root,f) |
| 67 | self.old_files.add(dst) |
| 68 | |
| 69 | def editFile(self,dst,data): |
| 70 | """edit a destination file. if the file is not mapped from a source, |
| 71 | it will be added. return 0 if the file content wasn't changed, |
| 72 | 1 if it was edited, or 2 if the file is new""" |
| 73 | #dst = os.path.realpath(dst) |
| 74 | result = 1 |
| 75 | if os.path.exists(dst): |
| 76 | f = open(dst, "r") |
| 77 | olddata = f.read() |
| 78 | f.close() |
| 79 | if olddata == data: |
| 80 | self.old_files.remove(dst) |
| 81 | return 0 |
| 82 | else: |
| 83 | result = 2 |
| 84 | |
| 85 | self.new_data[dst] = data |
| 86 | self.new_files.add(dst) |
| 87 | return result |
| 88 | |
| 89 | def getChanges(self): |
| 90 | """determine changes, returns (adds, deletes, edits)""" |
| 91 | adds = set() |
| 92 | edits = set() |
| 93 | deletes = set() |
| 94 | |
| 95 | for dst in self.new_files: |
| 96 | if not (dst in self.old_files): |
| 97 | adds.add(dst) |
| 98 | else: |
| 99 | edits.add(dst) |
| 100 | |
| 101 | for dst in self.old_files: |
| 102 | if not dst in self.new_files: |
| 103 | deletes.add(dst) |
| 104 | |
| 105 | return (adds, deletes, edits) |
| 106 | |
Elliott Hughes | c95eb57 | 2013-01-29 18:15:55 -0800 | [diff] [blame] | 107 | def _writeFile(self,dst): |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 108 | if not os.path.exists(os.path.dirname(dst)): |
| 109 | create_file_path(dst) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 110 | f = open(dst, "w") |
| 111 | f.write(self.new_data[dst]) |
| 112 | f.close() |
| 113 | |
| 114 | def updateFiles(self): |
| 115 | adds, deletes, edits = self.getChanges() |
| 116 | |
| 117 | for dst in sorted(adds): |
| 118 | self._writeFile(dst) |
| 119 | |
| 120 | for dst in sorted(edits): |
| 121 | self._writeFile(dst) |
| 122 | |
| 123 | for dst in sorted(deletes): |
| 124 | os.remove(dst) |
| 125 | |
David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 126 | def updateGitFiles(self): |
| 127 | adds, deletes, edits = self.getChanges() |
| 128 | |
| 129 | if adds: |
| 130 | for dst in sorted(adds): |
| 131 | self._writeFile(dst) |
| 132 | commands.getoutput("git add " + " ".join(adds)) |
| 133 | |
| 134 | if deletes: |
| 135 | commands.getoutput("git rm " + " ".join(deletes)) |
| 136 | |
| 137 | if edits: |
| 138 | for dst in sorted(edits): |
| 139 | self._writeFile(dst) |
| 140 | commands.getoutput("git add " + " ".join(edits)) |