| 1 | #!/usr/bin/env python |
|---|
| 2 | # |
|---|
| 3 | # trac-wikifileuploader.py |
|---|
| 4 | # This script help you to import files to trac's wiki. |
|---|
| 5 | # |
|---|
| 6 | # Mon Mar 12 02:51:17 CET 2007 |
|---|
| 7 | # - Eriol (@mornie.org) |
|---|
| 8 | |
|---|
| 9 | import os |
|---|
| 10 | import os.path |
|---|
| 11 | import shutil |
|---|
| 12 | import time |
|---|
| 13 | |
|---|
| 14 | from pysqlite2 import dbapi2 as sqlite |
|---|
| 15 | |
|---|
| 16 | def uploadfile(trac_env_dir, file_dir, upload_page): |
|---|
| 17 | |
|---|
| 18 | con = sqlite.connect(os.path.join(trac_env_dir, 'db/trac.db')) |
|---|
| 19 | cur = con.cursor() |
|---|
| 20 | |
|---|
| 21 | for f in os.listdir(file_dir): |
|---|
| 22 | file_to_upload = os.path.join(file_dir, f) |
|---|
| 23 | |
|---|
| 24 | if os.path.isfile(file_to_upload): |
|---|
| 25 | size = os.stat(file_to_upload)[6] |
|---|
| 26 | q = """INSERT INTO attachment (type, id, filename, size, time, |
|---|
| 27 | description, author, ipnr) values |
|---|
| 28 | (?, ?, ?, ?, ?, ?, ?, ?) """ |
|---|
| 29 | cur.execute(q, ('wiki', upload_page, f, str(size), time.time(), |
|---|
| 30 | 'Imported by trac-wikifileuploader', |
|---|
| 31 | 'trac-wikifileuploader', '127.0.0.1')) |
|---|
| 32 | con.commit() |
|---|
| 33 | |
|---|
| 34 | shutil.copyfile(file_to_upload, |
|---|
| 35 | os.path.join(trac_env_dir, |
|---|
| 36 | 'attachments', |
|---|
| 37 | 'wiki', |
|---|
| 38 | upload_page, |
|---|
| 39 | f)) |
|---|
| 40 | |
|---|
| 41 | con.close() |
|---|
| 42 | |
|---|
| 43 | def usage(): |
|---|
| 44 | use =''' |
|---|
| 45 | Usage: trac-wikifileuploader </path/to/projenv> </path/to/files> <WikiPage> |
|---|
| 46 | |
|---|
| 47 | <WikiPage> must be a page witch exist in your trac's wiki.''' |
|---|
| 48 | return use |
|---|
| 49 | |
|---|
| 50 | if __name__ == '__main__': |
|---|
| 51 | import sys |
|---|
| 52 | args = sys.argv[1:] |
|---|
| 53 | if len(args) != 3: |
|---|
| 54 | print usage() |
|---|
| 55 | sys.exit(2) |
|---|
| 56 | |
|---|
| 57 | uploadfile(args[0], args[1], args[2]) |
|---|