]> git.uio.no Git - python-TSD.git/blame - bin/tsdfx-sns-migrate
First draft script to migrate nettskjema files from import to data/durable/sns/.
[python-TSD.git] / bin / tsdfx-sns-migrate
CommitLineData
71eec77a
PR
1#!/usr/bin/env python
2#
3# Copy files from /tsd/pXX/fx/import/sns/ to
4# /tsd/pXX/data/durable/sns/ to keep the submitted netskjema forms.
5# Form files older than <keeparound> days are removed from
6# /tsd/pXX/fx/import/sns/. Copy all changed files (checking size, and mtime)
7#
8# Implemented in python instead of using rsync and find to get a
9# script that work on both Unix and Windows.
10
11import os
12import shutil
13import errno
14import time
15from optparse import OptionParser
16
17def copy_file(srcdir, dstdir, relname, isdir=False, purgedaysold = None):
18 global options
19# print "f:", relname
20 dst = os.path.join(dstdir, relname)
21 src = os.path.join(srcdir, relname)
22# print src, dst
23 srcst = os.lstat(src)
24 if not os.path.exists(dst):
25 print "migrate fresh %s to %s" % (src, dst)
26 if not options.dryrun:
27 if isdir:
28 os.mkdir(dst)
29 else:
30 shutil.copy(src, dst)
31 srcst = os.lstat(src)
32 os.utime(dst, (srcst.st_atime, srcst.st_mtime))
33 else:
34 dstst = os.lstat(dst)
35# print srcst
36 if srcst.st_size != dstst.st_size \
37 or 1 < abs(srcst.st_mtime - dstst.st_mtime):
38 if not isdir:
39 print "Updating copy %s" % dst
40 if not options.dryrun:
41 shutil.copy(src, dst)
42 os.utime(dst, (srcst.st_atime, srcst.st_mtime))
43
44 if purgedaysold is not None:
45# print "Checking if file is older than %s" % purgedaysold
46 if srcst.st_ctime < (time.time() - int(purgedaysold) * (24*60*60)):
47 if not isdir:
48 print "remove old %s" % src
49 if not options.dryrun:
50 os.unlink(src)
51
52def migrate_data(srcdir, dstdir, purgedaysold = None):
53 for root, dirs, files in os.walk(srcdir):
54 for dirname in dirs:
55 fullpath = os.path.join(root, dirname)
56 relname = os.path.relpath(fullpath, start=srcdir)
57 copy_file(srcdir, dstdir, relname, isdir=True, purgedaysold=purgedaysold);
58 for filename in files:
59 fullpath = os.path.join(root, filename)
60 relname = os.path.relpath(fullpath, start=srcdir)
61 copy_file(srcdir, dstdir, relname, isdir=False, purgedaysold=purgedaysold);
62
63def main():
64 global options
65 parser = OptionParser()
66 parser.add_option("-d", "--dryrun",
67 action="store_true", dest="dryrun", default=False,
68 help="enable dryrun mode, do not copy, only state what would be done")
69 parser.add_option("-k", "--keeparound",
70 dest="keeparound", default=None, metavar='days',
71 help="remove source files unchanged for this number of days")
72 parser.add_option("-v", "--verbose",
73 action="store_true", dest="verbose", default=False,
74 help="enable verbose mode, print more messages")
75 (options, args) = parser.parse_args()
76 if options.verbose:
77 print options
78 print args
79
80 if (not args):
81 parser.print_help()
82 return 1
83
84 pdir = args[0]
85 srcdir = pdir + "/fx/imports/sns"
86 dstdir = pdir + "/data/durable/sns"
87
88 if not os.path.isdir(srcdir):
89 print "Source %s is not a directory" % srcdir
90 return 1
91
92 if not os.path.isdir(dstdir):
93 print "Destination %s is not a directory" % dstdir
94 return 1
95
96 migrate_data(srcdir, dstdir, options.keeparound)
97 return 0
98
99if __name__ == "__main__":
100 main()