-
Notifications
You must be signed in to change notification settings - Fork 12
/
pokerconfigupgrade
executable file
·122 lines (111 loc) · 4.07 KB
/
pokerconfigupgrade
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
#!/usr/bin/env python
# -*- py-indent-offset: 4; coding: utf-8; mode: python -*-
# Copyright (C) 2006 - 2010 Loic Dachary <[email protected]>
# Copyright (C) 2005, 2006 Mekensleep
#
# Mekensleep
# 26 rue des rosiers
# 75004 Paris
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Authors:
# Loic Dachary <[email protected]>
#
import sys, os, getopt, stat, string
sys.path.insert(0, os.path.dirname(__file__))
def usage():
print """
pokerconfigupgrade [--module=<python_module>] [--help] [--verbose=<level>] [--dry-run]
[--upgrades=<directory>] [--reference=<directory>]
[directory or file.xml] ...
"""
def my_import(name):
mod = __import__(name)
components = name.split('.')
for comp in components[1:]:
mod = getattr(mod, comp)
return mod
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "hvdumr", ["help", "verbose=", "dry-run", "upgrades=", "module=", "reference=" ])
except getopt.GetoptError:
usage()
sys.exit(2)
upgrades = None
configuration_file = None
module = "pokerengine.pokerengineconfig"
reference = None
verbose = 0
dry_run = False
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit(0)
if o in ("-d", "--dry-run"):
dry_run = True
if o in ("-u", "--upgrades"):
upgrades = a
if o in ("-m", "--module"):
module = a
if o in ("-r", "--reference"):
reference = a
if o in ("-v", "--verbose"):
verbose = int(a)
if not upgrades and not reference:
print "must specify either --upgrades or --reference"
sys.exit(3)
if upgrades:
config_module = my_import(module)
config_class = config_module.__dict__['Config']
files = []
for path in args:
if os.path.exists(path):
if os.path.isfile(path):
files.append(path)
elif os.path.isdir(path):
files_tmp = map(lambda file: path + "/" + file, os.listdir(path))
files.extend(filter(lambda file: os.path.isfile(file), files_tmp))
else:
raise Exception, "The type of the file " + path + " is unknown"
else:
raise Exception, "File not found " + path
files = filter(lambda file: string.find(file, ".xml") >= 0, files)
for file in files:
mode = os.stat(file)[stat.ST_MODE]
if not mode & stat.S_IWUSR:
os.chmod(file, mode | 0200)
config_class.upgrades_repository = upgrades
config_class.verbose = verbose
config_class.upgrade_dry_run = dry_run
config = config_class([''])
for file in files:
config.load(file)
if reference:
if ( len(args) != 1 or not os.path.isdir(args[0]) ):
print "--reference requires a single directory argument, target of the upgrade"
sys.exit(4)
target = args[0]
rsync_verbose = verbose and "-v" or ""
rsync_command = "/usr/bin/env rsync --exclude CVS -a " + rsync_verbose + " --ignore-times " + reference + "/ " + target + "/"
if verbose:
print rsync_command
if not dry_run:
if os.system(rsync_command):
sys.exit(5)
return 0
if __name__ == "__main__":
sys.exit(main())