This project has sit gathering dust for a long time, try and kickstart it again
[Dotbuddy.git] / dotbuddy.py
blobd1aba1b32c13ec3adc5f2b3a201d422037a6aa30
1 #!/usr/bin/env python
3 ###########################################################################
4 # #
5 # ConfigParser.h Copyright (C) 2008 by J Rumble #
6 # #
7 # j.w.rumble@reading.ac.uk #
8 # #
9 # #
10 # This file is part of dotbuddy, #
11 # #
12 # dotbuddy is free software: you can redistribute it and/or modify #
13 # it under the terms of the GNU General Public License as published by #
14 # the Free Software Foundation, either version 2 of the License, or #
15 # (at your option) any later version. #
16 # #
17 # #
18 # dotbuddy is distributed in the hope that it will be useful, #
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
21 # GNU General Public License for more details. #
22 # #
23 # You should have received a copy of the GNU General Public License #
24 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
25 ###########################################################################
27 __version__='v0.1'
29 import os, sys
30 import tarfile
31 import zipfile
32 import ConfigParser
33 import time
34 import md5
36 #Settings
38 settings = {
39 'archive_type' : 'bz2',
40 'confdir' : '.dotbuddy/',
41 'confile' : 'dotbuddy.conf',
42 'create_archive' : True,
43 'create_audit_report' : False,
44 'filelist' : 'filelist',
45 'logfile' : None,
46 'override_perms' : False,
47 'silent_override' : True,
48 'verbose' : True,
52 file_list_names = []
54 def create_conf_files():
55 """
56 If a config file is not supplied, or we can't find one at the default
57 location, this will attempt to create one using the default config
58 values
59 """
60 cparser = ConfigParser.ConfigParser()
61 npath = os.path.join(os.path.expanduser('~'),settings['confdir'])
63 if os.access(npath, os.F_OK) is not True:
64 try:
65 #Create Path and config file...
66 os.mkdir(npath)
67 file = open(os.path.join(npath,'dotbuddy.conf'),'w')
68 except IOError:
69 print "Can't create config file !!"
70 sys.exit()
71 else:
72 cparser.readfp(file,os.path.join(npath,'dotbuddy.conf'))
73 cparser.add_section('Options')
75 for option, value in settings.iteritems():
76 cparser.set('Options',option,value)
78 cparser.write(file)
79 file.close()
80 print cparser.items('Options')
81 def main():
82 """
83 Our main Method
84 """
86 #Find users homedir
87 user_home_path = os.path.expanduser("~")
89 os.chdir(user_home_path)
90 if os.access(settings['confdir'],os.R_OK) is False:
91 try:
92 pass
93 create_conf_files()
94 except OSError:
95 print "Can't create files.. Exiting !!"
96 sys.exit(1)
97 else:
98 create_filelist()
99 def read_conf():
101 Reads Options from config file, and creates one with defaults if it
102 doesnt exist.
104 session_cfg = ConfigParser.ConfigParser()
105 session_cfg = cfg.read('dotbuddy.conf')
107 def create_filelist():
108 infile = open(os.path.join(settings['confdir'],settings['filelist']))
109 for line in infile:
110 if line.startswith('#'):
111 continue
112 elif line.startswith('\n'):
113 continue
114 else:
115 file_list_names.append(line.rstrip())
117 if settings['create_audit_report'] is True:
118 audit_files()
119 if settings['create_archive'] is True:
120 archive_files()
122 def archive_files():
123 #if zip is True:
124 # zip_files()
125 #elif gzip is True:
126 # gzip_files()
127 #else:
128 bzip2_files()
130 def zip_files():
131 pass
133 def gzip_files():
134 pass
136 def bzip2_files():
137 date = time.strftime("%d-%b-%y",time.localtime())
138 arc_name = "HOME-Backup-" + date + ".tar.bz2"
139 archive = tarfile.open(arc_name,"w:bz2")
140 try:
141 for tfile in file_list_names:
142 try:
143 print "Backing up: %s.." % tfile
144 archive.add(tfile)
145 except OSError:
146 print "Error backing up file, skipping.."
147 continue
148 print "Finished..\n"
150 finally: archive.close()
152 def audit_files():
153 for tfile in file_list_names:
154 fdata = open(tfile,'r').read()
155 md5sig = md5.md5(fdata).hexdigest()
156 print "Name: %s MD5 Sig: %s" % (tfile,md5sig)
160 if __name__ == '__main__':
161 main()