| 1 |
|
| 2 |
|
| 3 |
from io import open |
| 4 |
from os import makedirs |
| 5 |
from os.path import dirname, isdir, isfile |
| 6 |
|
| 7 |
def createDirFor(filePath): |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
dirPath = dirname(filePath) |
| 12 |
if dirPath and not isdir(dirPath): |
| 13 |
makedirs(dirPath) |
| 14 |
|
| 15 |
def rewriteIfChanged(path, lines): |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
newLines = [line + '\n' for line in lines] |
| 21 |
|
| 22 |
if isfile(path): |
| 23 |
with open(path, 'r', encoding='utf-8') as inp: |
| 24 |
oldLines = inp.readlines() |
| 25 |
if newLines == oldLines: |
| 26 |
print('Up to date: %s' % path) |
| 27 |
return False |
| 28 |
else: |
| 29 |
print('Updating %s...' % path) |
| 30 |
else: |
| 31 |
print('Creating %s...' % path) |
| 32 |
createDirFor(path) |
| 33 |
|
| 34 |
with open(path, 'w', encoding='utf-8') as out: |
| 35 |
out.writelines(newLines) |
| 36 |
return True |