58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
#!/usr/bin/env python
|
|
#
|
|
# trac-wikifileuploader.py
|
|
# This script help you to import files to trac's wiki.
|
|
#
|
|
# Mon Mar 12 02:51:17 CET 2007
|
|
# - Eriol (@mornie.org)
|
|
|
|
import os
|
|
import os.path
|
|
import shutil
|
|
import time
|
|
|
|
from pysqlite2 import dbapi2 as sqlite
|
|
|
|
def uploadfile(trac_env_dir, file_dir, upload_page):
|
|
|
|
con = sqlite.connect(os.path.join(trac_env_dir, 'db/trac.db'))
|
|
cur = con.cursor()
|
|
|
|
for f in os.listdir(file_dir):
|
|
file_to_upload = os.path.join(file_dir, f)
|
|
|
|
if os.path.isfile(file_to_upload):
|
|
size = os.stat(file_to_upload)[6]
|
|
q = """INSERT INTO attachment (type, id, filename, size, time,
|
|
description, author, ipnr) values
|
|
(?, ?, ?, ?, ?, ?, ?, ?) """
|
|
cur.execute(q, ('wiki', upload_page, f, str(size), time.time(),
|
|
'Imported by trac-wikifileuploader',
|
|
'trac-wikifileuploader', '127.0.0.1'))
|
|
con.commit()
|
|
|
|
shutil.copyfile(file_to_upload,
|
|
os.path.join(trac_env_dir,
|
|
'attachments',
|
|
'wiki',
|
|
upload_page,
|
|
f))
|
|
|
|
con.close()
|
|
|
|
def usage():
|
|
use ='''
|
|
Usage: trac-wikifileuploader </path/to/projenv> </path/to/files> <WikiPage>
|
|
|
|
<WikiPage> must be a page witch exist in your trac's wiki.'''
|
|
return use
|
|
|
|
if __name__ == '__main__':
|
|
import sys
|
|
args = sys.argv[1:]
|
|
if len(args) != 3:
|
|
print usage()
|
|
sys.exit(2)
|
|
|
|
uploadfile(args[0], args[1], args[2])
|