Move the long option name enum from cl.h into main.c, because it is
[svn.git] / tools / examples / putfile.py
blobef567b881b2fea66130adb26d90de16bc641fbfd
1 #!/usr/bin/env python
3 # USAGE: putfile.py [-m commitmsg] [-u username] file repos-path
5 # put a file into an SVN repository
8 import sys
9 import os
10 import getopt
11 try:
12 my_getopt = getopt.gnu_getopt
13 except AttributeError:
14 my_getopt = getopt.getopt
16 from svn import fs, core, repos, delta
18 def putfile(fname, rpath, uname="", commitmsg=""):
19 rpath = core.svn_path_canonicalize(rpath)
20 repos_ptr = repos.open(rpath)
21 fsob = repos.fs(repos_ptr)
23 # open a transaction against HEAD
24 rev = fs.youngest_rev(fsob)
26 txn = repos.fs_begin_txn_for_commit(repos_ptr, rev, uname, commitmsg)
28 root = fs.txn_root(txn)
29 rev_root = fs.revision_root(fsob, rev)
31 kind = fs.check_path(root, fname)
32 if kind == core.svn_node_none:
33 print "file '%s' does not exist, creating..." % fname
34 fs.make_file(root, fname)
35 elif kind == core.svn_node_dir:
36 print "File '%s' is a dir." % fname
37 return
38 else:
39 print "Updating file '%s'" % fname
41 handler, baton = fs.apply_textdelta(root, fname, None, None)
43 ### it would be nice to get an svn_stream_t. for now, just load in the
44 ### whole file and shove it into the FS.
45 delta.svn_txdelta_send_string(open(fname, 'rb').read(),
46 handler, baton)
48 newrev = repos.fs_commit_txn(repos_ptr, txn)
49 print "revision: ", newrev
51 def usage():
52 print "USAGE: putfile.py [-m commitmsg] [-u username] file repos-path"
53 sys.exit(1)
55 def main():
56 opts, args = my_getopt(sys.argv[1:], 'm:u:')
57 if len(args) != 2:
58 usage()
60 uname = commitmsg = ""
62 for name, value in opts:
63 if name == '-u':
64 uname = value
65 if name == '-m':
66 commitmsg = value
67 putfile(args[0], args[1], uname, commitmsg)
69 if __name__ == '__main__':
70 main()