Move code for status to reusable function
[mw] / src / mw / metadir.py
1 ###
2 # mw - VCS-like nonsense for MediaWiki websites
3 # Copyright (C) 2010  Ian Weller <ian@ianweller.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program.  If not, see <http://www.gnu.org/licenses/>.
17 ###
18
19 import codecs
20 import ConfigParser
21 import json
22 import mw.api
23 import os
24 import sys
25
26
27 class Metadir(object):
28
29     def __init__(self):
30         self.me = os.path.basename(sys.argv[0])
31         root = os.getcwd()
32         while True:
33             if '.mw' in os.listdir(root):
34                 self.root = root
35                 break
36             head = os.path.split(root)[0]
37             if head == root:
38                 self.root = os.getcwd()
39                 break
40             root = head
41         self.location = os.path.join(self.root, '.mw')
42         self.config_loc = os.path.join(self.location, 'config')
43         if os.path.isdir(self.location) and \
44            os.path.isfile(self.config_loc):
45             self.config = ConfigParser.RawConfigParser()
46             self.config.read(self.config_loc)
47         else:
48             self.config = None
49
50     def create(self, api_url):
51         # create the directory
52         if os.path.isdir(self.location):
53             print '%s: you are already in a mw repo' % self.me
54             sys.exit(1)
55         else:
56             os.mkdir(self.location, 0755)
57         # metadir versioning
58         fd = file(os.path.join(self.location, 'version'), 'w')
59         fd.write('1') # XXX THIS API VERSION NOT LOCKED IN YET
60         fd.close()
61         # create config
62         self.config = ConfigParser.RawConfigParser()
63         self.config.add_section('remote')
64         self.config.set('remote', 'api_url', api_url)
65         with open(self.config_loc, 'wb') as config_file:
66             self.config.write(config_file)
67         # create cache/
68         os.mkdir(os.path.join(self.location, 'cache'))
69         # create cache/pagedict
70         fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'w')
71         fd.write(json.dumps({}))
72         fd.close()
73         # create cache/pages/
74         os.mkdir(os.path.join(self.location, 'cache', 'pages'), 0755)
75
76     def pagedict_add(self, pagename, pageid, currentrv):
77         fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'r+')
78         pagedict = json.loads(fd.read())
79         pagedict[pagename] = {'id': int(pageid), 'currentrv': int(currentrv)}
80         fd.seek(0)
81         fd.write(json.dumps(pagedict))
82         fd.truncate()
83         fd.close()
84
85     def get_pageid_from_pagename(self, pagename):
86         fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'r')
87         pagedict = json.loads(fd.read())
88         if pagename in pagedict.keys():
89             return pagedict[pagename]
90         else:
91             return None
92
93     def pages_add_rv(self, pageid, rv):
94         pagefile = os.path.join(self.location, 'cache', 'pages', str(pageid))
95         fd = file(pagefile, 'w+')
96         pagedata_raw = fd.read()
97         if pagedata_raw == '':
98             pagedata = {}
99         else:
100             pagedata = json.loads(pagedata_raw)
101         rvid = int(rv['revid'])
102         pagedata[rvid] = {
103                 'user': rv['user'], 'timestamp': rv['timestamp']
104         }
105         if '*' in rv.keys():
106             pagedata[rvid]['content'] = rv['*']
107         fd.seek(0)
108         fd.write(json.dumps(pagedata))
109         fd.truncate()
110         fd.close()
111
112     def pages_get_rv_list(self, pageid):
113         pagefile = os.path.join(self.location, 'cache', 'pages',
114                                 str(pageid['id']))
115         fd = file(pagefile, 'r')
116         pagedata = json.loads(fd.read())
117         rvs = [int(x) for x in pagedata.keys()]
118         rvs.sort()
119         return rvs
120
121     def pages_get_rv(self, pageid, rvid):
122         pagefile = os.path.join(self.location, 'cache', 'pages',
123                                 str(pageid['id']))
124         fd = file(pagefile, 'r')
125         pagedata = json.loads(fd.read())
126         return pagedata[str(rvid)]
127
128     def working_dir_status(self):
129         status = {}
130         check = []
131         for root, dirs, files in os.walk(self.root):
132             if root == self.root:
133                 dirs.remove('.mw')
134             for name in files:
135                 check.append(os.path.join(root, name))
136         check.sort()
137         for full in check:
138             name = os.path.split(full)[1]
139             if name[-5:] == '.wiki':
140                 pagename = mw.api.filename_to_pagename(name[:-5])
141                 pageid = self.get_pageid_from_pagename(pagename)
142                 if not pageid:
143                     status[os.path.relpath(full, self.root)] = '?'
144                 else:
145                     rvid = self.pages_get_rv_list(pageid)[-1]
146                     rv = self.pages_get_rv(pageid, rvid)
147                     cur_content = codecs.open(full, 'r', 'utf-8').read()
148                     if cur_content != rv['content']:
149                         status[os.path.relpath(full, self.root)] = 'U'
150         return status

Benjamin Mako Hill || Want to submit a patch?