python: เขียนสคริปต์ไว้ update Drupal
เขียนสคริปต์ไว้เพื่อให้เปลี่ยนรุ่น Drupal ง่าย ๆ เผื่อมีหลายไซต์
สมมุติว่าไดเรกทอรี่ติดตั้งอยู่ที่ /var/www/drupal
URL คือ http://www.example.com
ฐานข้อมูลชื่อ DATABASE_NAME
ผู้ใช้ชื่อ ADMIN และรหัสผ่านคือ ADMIN_PASSWORD
อย่าลืมต้องให้ ADMIN อ่านได้เท่านั้น เพราะจะมีรหัสผ่านอยู่ในสคริปต์
$ cd /var/www/drupal
$ touch sed.py
$ chmod 700 sed.py
ขั้นตอน upgrade
$ cd /var/www/drupal
$ wget http://ftp.drupal.org/files/projects/drupal-X.X.tar.gz
$ tar xfx drupal-X.X
$ cd drupal-X.X
$ cp -xa * ..
$ cd ..
$ rm -rf drupal-X.X
$ ./sed.py
### DO UPDATE AT www.example.com/update.php
### SET BACK
$ vi update.php
.. $access_check = TRUE; #$access_check = FALSE; ...
เนื้อไฟล์ sed.py มีดังนี้
$ vi sed.py
#!/usr/bin/env python
# cd /var/www/drupal
# cp -xa ../drupal-5.3/* .
# ./sed.py
# ---> http://www.example.com/update.php
# vi update.php #SET $access_check = TRUE;
import os
import sys
db_url = "$db_url = 'mysql://ADMIN:ADMIN_PASSWORD@localhost/DATABASE_NAME';"
base_url = "$base_url = 'http://www.example.com'; // NO trailing slash!"
basedir = os.path.abspath(os.curdir);
#-------------------------------------------------------------------------
def sed_file(file,dict_txt):
filename = os.path.basename(file)
dirname = os.path.abspath(file)
bakfile = file+'.bakbak'
os.rename(file,bakfile)
f_old = open(bakfile)
f_new = open(file,'w')
txt_old = [i for i in dict_txt]
txt_new = [i for i in dict_txt.values()]
print txt_old
for i in f_old:
line = i.strip()
if line in txt_old:
n = txt_old.index(line)
print 'old=',line
print 'new=',txt_new[n]
f_new.write(txt_new[n]+'\n')
else:
f_new.write(i)
f_new.close()
f_old.close()
#FIRST FILE sites/default/setting.php
file = os.path.join(basedir,'sites/default/settings.php')
tmp_db = "$db_url = 'mysql://username:password@localhost/databasename';"
tmp_base = "# $base_url = 'http://www.example.com'; // NO trailing slash!"
dict_txt = {\
tmp_db: '#'+tmp_db+'\n'+db_url, \
tmp_base: tmp_base+'\n'+base_url \
}
sed_file(file,dict_txt)
#SECOND update.php
file = os.path.join(basedir,'update.php')
dict_txt = {\
"$access_check = TRUE;":\
"#$access_check = TRUE;\n$access_check = FALSE;"\
}
sed_file(file,dict_txt)
- Printer-friendly version
- Log in or register to post comments
- 4424 reads







Recent comments