PEP8: fix E401: multiple imports on one line
[kai/samba-autobuild/.git] / examples / scripts / vfs / media_harmony / trigger_avid_update.py
1 #!/usr/bin/python
2 import os
3 import socket
4 import sys
5 import stat
6
7 ######################################################################
8 ##
9 ##  trigger_avid_update.py for media_harmony VFS module.
10 ##
11 ##  Copyright (C) Andrew Klaassen       2012.
12 ##
13 ##  This program is free software; you can redistribute it and/or modify
14 ##  it under the terms of the GNU General Public License as published by
15 ##  the Free Software Foundation; either version 3 of the License, or
16 ##  (at your option) any later version.
17 ##
18 ##  This program 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 ##
26 ######################################################################
27
28
29 #
30 # Change avid_shares and ip_prefix as appropriate for your network.
31 #
32
33 avid_shares = (
34         '\\\\mediaharmony01\\project1\\',
35         '\\\\mediaharmony01\\project2\\',
36         '\\\\mediaharmony01\\project3\\',
37 )
38
39 ip_prefix = '192.168.1.'
40
41
42 if __name__ == "__main__":
43         my_ips = [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if ip[:len(ip_prefix)] == ip_prefix]
44         if not my_ips:
45                 print 'No IP address found.  Aborting.'
46                 dummy = raw_input("\nPress Enter to finish: ")
47                 sys.exit()
48
49         my_ip = my_ips[0]
50         my_name = os.environ.get('USERNAME')
51
52         for avid_share in avid_shares:
53                 media_dirs = []
54                 omfi_dir = os.path.join(avid_share, 'OMFI MediaFiles')
55                 if os.path.exists(omfi_dir):
56                         media_dirs.append(omfi_dir)
57                 mxf_root = os.path.join(avid_share, 'Avid MediaFiles', 'MXF')
58                 if os.path.exists(mxf_root):
59                         mxf_children = os.listdir(mxf_root)
60                         for child in mxf_children:
61                                 fullpath = os.path.join(mxf_root, child)
62                                 if os.path.isdir(fullpath):
63                                         media_dirs.append(fullpath)
64
65                 for media_dir in media_dirs:
66
67                         print '\nChecking %s...' % media_dir
68
69                         fakepath = '%s_%s_%s' % (media_dir, my_ip, my_name)
70                         print '...fakepath: %s' % fakepath
71
72                         db = os.path.join(media_dir, 'msmMMOB.mdb')
73                         print '...Checking for %s' % db
74                         if os.path.exists(db):
75                                 print '......found %s.' % db
76                                 db_mtime = os.stat(db)[stat.ST_MTIME]
77                                 newer_file = False
78                                 for child in os.listdir(media_dir):
79                                         if child == 'msmMMOB.mdb' or child == 'msmFMID.pmr':
80                                                 continue
81                                         child_mtime = os.stat(os.path.join(media_dir, child))[stat.ST_MTIME]
82                                         if child_mtime > db_mtime:
83                                                 print '......found newer file %s' % child
84                                                 newer_file = True
85                                                 break
86                         else:
87                                 print '......no %s.' % db
88                                 newer_file = True
89
90                         if newer_file:
91                                 utime = None  # Sets to current time.
92                                 print '...Setting fake mtime to NOW.  Will trigger re-index.'
93                         else:
94                                 mtime = os.stat(media_dir)[stat.ST_MTIME]
95                                 utime = (mtime, mtime)
96                                 print '...Setting fake mtime to media_dir mtime.  No re-index.'
97
98                         if not os.path.exists(fakepath):
99                                 tmp_fakepath = '%s.tmp' % fakepath
100                                 open(tmp_fakepath, 'a').close()
101                                 os.utime(tmp_fakepath, utime)
102                                 os.rename(tmp_fakepath, fakepath)
103                         else:
104                                 os.utime(fakepath, utime)
105
106         dummy = raw_input("\nPress Enter to finish: ")