-
Notifications
You must be signed in to change notification settings - Fork 7
/
arch.py
46 lines (38 loc) · 1.28 KB
/
arch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# rar archiving script
# david.barhuizen@gmail.com
# recurses subdirectories
# selectively includes only files with file extensions specified by FILE_EXTS
# creates archive named APP_NAME_yyyy-mm-dd-hh-mm
from datetime import datetime
import os
APP_NAME = 'py_emv_utils'
FILE_EXTS = ['py', 'txt', 'sql', 'bat', 'html', 'sh', 'dia', '_png', 'js', 'project', 'pydevproject']
FOLDER_PREFIX = '../'
def shell(cmd):
return os.system(cmd)
def files_of_type_present(filenames, file_ext):
for fname in filenames:
ext = fname[len(fname) - len(file_ext) : len(fname)]
if (ext == file_ext):
return True
return False
def time_stamp():
now = datetime.now()
tstamp = '%d-%d-%d-%d-%d' % (now.year, now.month, now.day, now.hour, now.minute)
return tstamp
def archive():
'''
walk sub-branches from root location
check if files of any target extension type are present in subfolder
if present, add files of ext type to archive
'''
date_str = time_stamp()
fname = FOLDER_PREFIX + ('%s_%s' % (APP_NAME, date_str))
info = os.walk('.')
for (dirpath, dirnames, filenames) in info:
for ext in FILE_EXTS:
if files_of_type_present(filenames, ext):
cmd = (r'rar a -r %s ' % fname) + dirpath + '/*.' + ext
shell(cmd)
if __name__ == '__main__':
archive()