-
Notifications
You must be signed in to change notification settings - Fork 2
/
existdb-config.py
executable file
·84 lines (73 loc) · 2.82 KB
/
existdb-config.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
#!/usr/bin/env python
# coding: utf-8
import datetime
import getopt
import os
import sys
import shutil
import tarfile
def done():
pass
print("done.")
def getOpts():
argv = sys.argv[1:]
#argv = ["-s", "./v0.0.1/", "-t", "./target/"]
opts, args = getopt.getopt(argv, "hs:t:")
opts2 = {}
for opt in opts:
opts2[opt[0]] = opt[1]
if "-h" in opts2.keys():
print("usage: ./existdb-config.py [-h | -s <sourcePath> -t <targetPath>]")
print("e.g. : ./existdb-config.py -s ./v5.1.1/ -t ../exist-distribution-5.1.1/")
return(None)
count = 0
if "-s" in opts2.keys():
count += 1
if "-t" in opts2.keys():
count += 1
if count!=2:
print("must set both '-s' and '-t' with parameters, see '-h'")
return(None)
return(opts2)
def getReplaceFileNames(opts2):
replaceFileNames = []
for (subDir, subSubDir, fileNames) in os.walk(opts2["-s"]):
replaceFileNames.extend(list(map(lambda n: os.path.join(*n), zip([os.path.join(*(subDir.split(os.path.sep)[2:]))]*len(fileNames), fileNames))))
return(replaceFileNames)
def checkFilePermissions(replaceFileNames, opts2):
for file in replaceFileNames:
dstPathFileName = os.path.join(opts2["-t"], file)
srcPathFileName = os.path.join(opts2["-s"], file)
tmpPathFileName = "".join([dstPathFileName, "_existdb-config_temp"])
if (os.access(srcPathFileName, os.F_OK|os.R_OK), os.access(os.path.dirname(tmpPathFileName), os.F_OK|os.W_OK), os.access(dstPathFileName, os.F_OK|os.W_OK)) != (True, True, True):
raise Exception("permission error")
def createBackup(replaceFileNames, opts2):
tarFileName = os.path.join(opts2["-t"], "existdb-config_%s.tar" % (datetime.datetime.utcnow().replace(microsecond=0).isoformat().replace(":", "")))
if os.path.isfile(tarFileName):
raise Exception("backup tar already exists")
with tarfile.open(tarFileName, mode='w') as tf:
_ = list(map(lambda x: tf.add(os.path.join(opts2["-t"], x), arcname=x), replaceFileNames))
print("created backup in target folder: %s" % (tarFileName))
def createTmpFiles(replaceFileNames, opts2):
for file in replaceFileNames:
dstPathFileName = os.path.join(opts2["-t"], file)
srcPathFileName = os.path.join(opts2["-s"], file)
tmpPathFileName = "".join([dstPathFileName, "_existdb-config_temp"])
shutil.copyfile(srcPathFileName, tmpPathFileName)
def switchToNewFiles(replaceFileNames, opts2):
for file in replaceFileNames:
dstPathFileName = os.path.join(opts2["-t"], file)
srcPathFileName = os.path.join(opts2["-s"], file)
tmpPathFileName = "".join([dstPathFileName, "_existdb-config_temp"])
shutil.move(tmpPathFileName, dstPathFileName)
if __name__=="__main__":
opts = getOpts()
if opts == None:
exit(0)
replaceFileNames = getReplaceFileNames(opts)
checkFilePermissions(replaceFileNames, opts)
createBackup(replaceFileNames, opts)
createTmpFiles(replaceFileNames, opts)
switchToNewFiles(replaceFileNames, opts)
done()
exit(0)