~juliank/ftpsync/main

« back to all changes in this revision

Viewing changes to ftpsync

  • Committer: Julian Andres Klode
  • Date: 2007-06-20 16:37:24 UTC
  • Revision ID: jak@jak-linux.org-20070620163724-fhqyx84z6q2wtg8y
* Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
#
 
3
# Copyright (C) 2007 Julian Andres Klode <jak@jak-linux.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
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
#
 
19
# ToDo:
 
20
# - Be portable (use right seperator)
 
21
# - Remove .ignore?
 
22
# - Cleanup
 
23
#
 
24
 
 
25
"""ftpsync 0.1
 
26
 
 
27
 Upload a folder to a ftp site, create indexes and remove obsolete files
 
28
 and folders on target.
 
29
"""
 
30
 
 
31
__version__ = "0.1"
 
32
__copyright__ = 'Copyright (C) 2007 Julian Andres Klode'
 
33
 
 
34
import sys
 
35
from os.path import exists as path_exists
 
36
 
 
37
 
 
38
def ndname(dir):
 
39
        if dir[-1] != '/':
 
40
                return dir + '/'
 
41
        else:
 
42
                return dir
 
43
 
 
44
def cmd_log(dir):
 
45
        if dir[:7] == 'http://' or dir[:6] == 'ftp://' or dir[:8] == 'https://':
 
46
                import urllib2
 
47
                local  = open('/tmp/.rev.gz', 'wb')
 
48
                remote = urllib2.urlopen(ndname(dir) + '.rev.gz')
 
49
                local.write(remote.read())
 
50
                remote.close()
 
51
                local.close()
 
52
                dir="/tmp/"
 
53
        if path_exists(dir + ".rev.gz"):
 
54
                from ftpsynclib.tuned_gzip import GzipFile
 
55
                rev = GzipFile(dir + ".rev.gz", "rb")
 
56
                print rev.read()
 
57
                rev.close()
 
58
        else:
 
59
                print 'E: Folder %s is no ftpsync repo' % dir
 
60
                sys.exit(1)
 
61
 
 
62
def get_ignore(dir):
 
63
        ignore=['.ignore']
 
64
        if path_exists(dir+ '.ignore'): ignore+=file(dir + '.ignore').read().split()
 
65
        return ignore
 
66
 
 
67
def cmd_status(dir):
 
68
        '''Run the status action'''
 
69
        ignore=get_ignore(dir)
 
70
        from ftpsynclib.changes import changes
 
71
        print changes(dir, ignore).msg()
 
72
 
 
73
def cmd_init(dname):
 
74
        '''Write a new config file'''
 
75
        from os import environ
 
76
        import ftpsynclib
 
77
        new_conf = {}
 
78
        conf = ftpsynclib.conf(dname + '.ftpsync')
 
79
        conf['author'] = 'My name'
 
80
        conf['email']  = 'user@localhost'
 
81
        conf['host']   = 'localhost'
 
82
        conf['user']   = 'anonymous'
 
83
        conf['path']   = '/'
 
84
        conf['passwd'] = 'pass'
 
85
        conf.read(ndname(environ['HOME']) + '.ftpsync')
 
86
        conf.read()
 
87
        new_conf['author'] = raw_input('Name [%s]:' % conf['author'])
 
88
        new_conf['email']  = raw_input('E-Mail [%s]:' % conf['email'])
 
89
        new_conf['host']   = raw_input('FTP host [%s]:' % conf['host'])
 
90
        new_conf['path']   = raw_input('FTP path [%s]:' % conf['path'])
 
91
        new_conf['user']   = raw_input('FTP user [%s]:' % conf['user'])
 
92
        new_conf['passwd'] = raw_input('FTP pass [%s]:' % conf['passwd'])
 
93
        conf.append(new_conf)
 
94
        conf.write()
 
95
 
 
96
 
 
97
def cmd_commit(dir, index=True, clean=False):
 
98
        '''Run the commit action'''
 
99
        ignore=get_ignore(dir)
 
100
        from ftpsynclib import conf, progressBar
 
101
        c = conf(dir + '.ftpsync')
 
102
        if index:
 
103
                from ftpsynclib.html_index import html_index
 
104
                html_index(dir, '/', True, ignore)
 
105
 
 
106
        from ftpsynclib.changes import changes
 
107
        a = changes(dir, ignore, clean)
 
108
        if a.commit():
 
109
                if clean: a.rmfiles+= ['.rev.gz', '.jak.gz']
 
110
                else:  a.files+=['.rev.gz', '.jak.gz']
 
111
                from ftpsynclib.upload import FTP
 
112
                go = FTP(host=c['host'], user=c['user'], passwd=c['passwd'], path=c['path'])
 
113
                for i in a.dirs: go.mkd(i)
 
114
                for i in a.rmfiles: go.delete(i)
 
115
                for i in a.rmdirs: go.rmd(i)
 
116
 
 
117
                if len(a.cfiles + a.files):
 
118
                        from os.path import getsize as filesize
 
119
                        maxValue = 0
 
120
                        size=0
 
121
                        # Calculate the size of all files
 
122
                        for i in a.cfiles + a.files: 
 
123
                                maxValue += filesize(dir + i)
 
124
                        # Initialize the progress bar
 
125
                        myBar = progressBar(maxValue = maxValue)
 
126
                        # Upload the files
 
127
                        for i in a.cfiles + a.files: 
 
128
                                go.upload(file=dir + i, target=i, bar=myBar)
 
129
                        print
 
130
 
 
131
if len(sys.argv) > 2: 
 
132
        dir = ndname(sys.argv[2])
 
133
else:
 
134
        dir = ndname(".")
 
135
 
 
136
 
 
137
if not len(sys.argv) in [2,3]:
 
138
        print 'ftpsync %s, %s' % (__version__, __copyright__)
 
139
        print 'usage: ftpsync [command] [folder]'
 
140
        print
 
141
        print 'commands:'
 
142
        print '  init   Initialize a ftpsync repo'
 
143
        print '  clean  Remove all files'
 
144
        print '  commit Upload all Changes'
 
145
        print '  status Show the current status'
 
146
        print '  log    Display Log'
 
147
        print
 
148
        print 'To configure the default settings for your account, run'
 
149
        print '"ftpsync init" in your home folder'
 
150
        sys.exit(1)
 
151
elif sys.argv[1] in ['init', 'initialize']:
 
152
        cmd_init(dir)
 
153
elif sys.argv[1] in ['log', 'l']:
 
154
        cmd_log(dir)
 
155
elif not path_exists(dir + '.ftpsync'):
 
156
        print 'E: Folder %s is no ftpsync repo' % dir
 
157
        sys.exit(1)
 
158
elif sys.argv[1] in ['clean']:
 
159
        cmd_commit(dir, False, True)
 
160
elif sys.argv[1] in ['ci', 'commit', 'apply']:
 
161
        cmd_commit(dir)
 
162
elif sys.argv[1] in ['status', 'stat']:
 
163
        cmd_status(dir)
 
164
 
 
165
else:
 
166
        print "E: ", sys.argv[1] , ": This option is not supported"
 
167