Welcome to mirror list, hosted at ThFree Co, Russian Federation.

update3_0to3_1.py « scripts - github.com/isida/4.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cfd2a9ad5ac376a0ecc2bfc50ef93b3db5e9edb2 (plain)
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
import os, sys

global clean, rename, cur, conn

os.chdir('..')
settings_folder = 'settings/%s'
conference_config_path = settings_folder % 'conference.config'
owner_config_path = settings_folder % 'owner.config'
configname = settings_folder % 'config.py'
topbase_path = settings_folder % 'topbase.db'
aliases_path = settings_folder % 'aliases'
blacklist_path = settings_folder % 'blacklist.db'
conoff_path = settings_folder % 'commonoff'
conf_path = settings_folder % 'conf'
feed_path = settings_folder % 'feed'
hidenrooms_path = settings_folder % 'hidenroom.db'
botignore_path = settings_folder % 'ignore'
botowner_path = settings_folder % 'owner'
locale_path = settings_folder % 'locale'
logroom_path = settings_folder % 'logroom.db'
spy_path = settings_folder % 'spy.db'
tban_path = settings_folder % 'temporary.ban'
saytoowner_path = settings_folder % 'saytoowner.db'
ignoreban_path = settings_folder % 'ignoreban.db'

ar = {'--help':'This page',
	  '--all':'Import all files',
	  '--owner-config':'Import owners config',
	  '--conference-config':'Import conference config',
	  '--top':'Import statictic for `top` command',
	  '--alias':'Import aliases',
	  '--blacklist':'Import blacklist of rooms',
	  '--comm':'Import list of disabled commands',
	  '--rooms':'Import list of bot\'s rooms',
	  '--rss':'Import RSS/ATOM feeds',
	  '--hiden-rooms':'Import list of hiden rooms',
	  '--bot-ignore':'Import patterns for bot ignore',
	  '--locale':'Import current locale name',
	  '--logs':'Import rooms with enabled logs',
	  '--bot-owner':'Import list of bot owners',
	  '--spy':'Import spy for room activity',
	  '--tban':'Import list of temporary bans',
	  '--say-to-owner':'Import base for `msgtoadmin` command',
	  '--ignore-ban':'Import list of rooms with ignore global ban',
	  '--clean':'Remove source file(s) after import',
	  '--rename':'Rename source file(s) after import'
	  }


def readfile(filename):
	fp = file(filename)
	data = fp.read()
	fp.close()
	return data

def errorHandler(text):
	print text
	sys.exit()

def mv(file):
	newfile = file.split('/')
	newfile = '/'.join(newfile[:-1]+['_%s' % newfile[-1]])
	os.system('mv %s %s' % (file,newfile))

def remove(file):
	try: os.remove(file)
	except: pass

def import_file(filename,basename,eval_for,eval_string):
	try:
		print 'Import: %s' % filename
		c = eval(readfile(filename))
		cnt = 0
		for t in eval(eval_for):
			r = eval(eval_string)
			req = 'insert into %s values(%s);' % (basename,','.join(['%'+a for a in 's'*len(r)]))
			cnt += 1
			cur.execute(req,r)
		print '\t%s record(s)' % cnt
		conn.commit()
		if rename: mv(filename)
		elif clean: remove(filename)
	except Exception, SM: print 'Error [%s] while proceed file %s' % (str(SM),filename)

print 'Updater for Isida Jabber Bot from 3.0 to 3.1'
print '(c) Disabler Production Lab.'

base_charset, base_type = 'utf8', 'pgsql'

if os.path.isfile(configname): execfile(configname)
else: errorHandler('%s is missed.' % configname)

try: _,_,_,_,_ = base_name,base_user,base_host,base_pass,base_port
except: errorHandler('Missed settings for SQL DB!')

arg = sys.argv[1:]

if arg:
	clean = '--clean' in arg
	rename = '--rename' in arg
	all = '--all' in arg
	if '--help' in arg:
		ark = ar.keys()
		ark.sort()
		print 'Usage:\n%s' % '\n'.join(['%-32s%s' % (t,ar[t]) for t in ark])
	else:
		for t in arg:
			if t not in ar.keys():
				print 'Unknown option: %s\nUse `--help` option' % t
				os._exit(0)
		if base_type == 'pgsql':
			import psycopg2
			conn = psycopg2.connect(database=base_name, user=base_user, host=base_host, password=base_pass, port=base_port)
		elif base_type == 'mysql':
			import MySQLdb
			conn = MySQLdb.connect(db=base_name, user=base_user, host=base_host, passwd=base_pass, port=int(base_port), charset=base_charset)
		else: errorHandler('Unknown database backend!')

		print 'Base type: %s' % base_type
		cur = conn.cursor()

		# --------- Conference config  --------- #
		if '--conference-config' in arg or all:
			try:
				print 'Import: %s' % conference_config_path
				c = eval(readfile(conference_config_path))
				to_base,cnt = 'config_conf',0
				for t in c.keys():
					for tmp in c[t].keys():
						r = (t,tmp,unicode(c[t][tmp]))
						req = 'insert into %s values(%s);' % (to_base,','.join(['%'+a for a in 's'*len(r)]))
						cnt += 1
						cur.execute(req,r)
				print '\t%s record(s)' % cnt
				conn.commit()
				if rename: mv(conference_config_path)
				elif clean: remove(conference_config_path)
			except Exception, SM: print 'Error [%s] while proceed file %s' % (str(SM),conference_config_path)

		# --------- Owner config  --------- #
		if '--owner-config' in arg or all: import_file(owner_config_path,'config_owner','c.keys()','(t,unicode(c[t]))')

		# --------- Top command  --------- #
		if '--top' in arg or all: import_file(topbase_path,'top','c','t')

		# --------- Aliases  --------- #
		if '--alias' in arg or all: import_file(aliases_path,'alias','c','t')

		# --------- Blacklist of rooms  --------- #
		if '--blacklist' in arg or all: import_file(blacklist_path,'blacklist','c','(t,)')

		# --------- Comm ON/OFF  --------- #
		if '--comm' in arg or all: import_file(conoff_path,'commonoff','c','t')
			
		# --------- Conferences list  --------- #
		if '--rooms' in arg or all: import_file(conf_path,'conference','c',"list(t.split('\\n',1)+[''])[:2]")

		# --------- Feeds  --------- #
		if '--rss' in arg or all: import_file(feed_path,'feed','c','t')

		# --------- Hiden rooms  --------- #
		if '--hiden-rooms' in arg or all: import_file(hidenrooms_path,'hiden_rooms','c','(t,)')
			
		# --------- Bot Ignore  --------- #
		if '--bot-ignore' in arg or all: import_file(botignore_path,'bot_ignore','c',"[('%%%s%%' % t,),(t,)]['@' in t]")
			
		# --------- Bot Locale  --------- #
		if '--locale' in arg or all: import_file(locale_path,'config_owner','range(0,1)',"('bot_locale',c)")

		# --------- Logs in rooms  --------- #
		if '--logs' in arg or all: import_file(logroom_path,'log_rooms','c','(t,)')

		# --------- Bot owners  --------- #
		if '--bot-owner' in arg or all: import_file(botowner_path,'bot_owner','c','(t,)')

		# --------- Spy for rooms activity  --------- #
		if '--spy' in arg or all: import_file(spy_path,'spy','c','t')

		# --------- Temporary ban  --------- #
		if '--tban' in arg or all: import_file(tban_path,'tmp_ban','c','t')

		# --------- Say to owner  --------- #
		if '--say-to-owner' in arg or all: import_file(saytoowner_path,'saytoowner','c.keys()','(t,c[t])')

		# --------- Ignore ban --------- #
		if '--ignore-ban' in arg or all: import_file(ignoreban_path,'ignore_ban','c','(t,)')

		print 'Finished!'
		cur.close()
		conn.commit()
		conn.close()

else: print 'use `--help` option'

# The end is near!