Convert prefix-date from bash to python.

This is overkill.
Buddy Sandidge 12 years ago
parent 8012d5fdab
commit d768350e70

@ -1,19 +1,104 @@
#!/usr/bin/env bash #!/usr/bin/env python
function datemv { from __future__ import unicode_literals, division, absolute_import, with_statement, print_function
if [[ $(basename "$1") == $(stat -c "%y" "$1" | cut -d " " -f1)* ]]; then
echo "Do nothing with '$1'" import os
else import re
mv -v "$1" "$(dirname "$1")/$(stat -c "%y" "$1" | cut -d " " -f1)_$(basename "$1")"
fi from datetime import datetime
} from optparse import OptionParser
from os import path
if [ -d $1 ]; then
for file in $(find "$1" -type f | grep -vP "\/\d{4}-\d{2}-\d{2}"); do def add(arr):
datemv "$file" return arr[0] + arr[1]
done assert add([1, 2]) == 3
else
echo "Directory not found '$1'" def mul(arr):
exit return arr[0] * arr[1]
fi assert mul([2, 3]) == 6
def echo(arg):
print(arg)
return arg
def compose(*funcs):
return lambda arg = None: reduce(lambda val, func: func(val), funcs, arg)
test_compose = compose(lambda _: [2, 3], mul, lambda x: [x, 3], mul, lambda x: [x, 1], add)
# 2 * 3 = 6 * 3 = 18 + 1 = 19
assert test_compose() == 19
date_reg_ex = re.compile("\d{4}-\d{2}-\d{2}_")
doesnt_start_with_date = lambda string: date_reg_ex.search(string, 0, 11) is None
assert doesnt_start_with_date("1234-56-78_") == False
assert doesnt_start_with_date("a1234-56-78_")
assert doesnt_start_with_date("1234-56-78")
assert doesnt_start_with_date("1234-56-78_ sdfsakdsdf fjalsdjhfaskj ldfh") == False
def prop(key):
def obj(dic):
return dic[key]
return obj
assert prop('key')({'key': 1}) == 1
def path_join(directory):
def do_join(file_name):
return path.join(directory, file_name)
return do_join
def mk_dir_map(ret, directory):
is_file = compose(path_join(directory), path.isfile)
files = filter(is_file, os.listdir(directory))
not_with_date = filter(doesnt_start_with_date, files)
ret[directory] = map(lambda x: {'original': x}, not_with_date)
return ret
def add_date(ret, directory):
original = prop('original')
add_dir_name = path_join(directory)
def list_date(file_obj):
date_from_obj = compose(
original,
add_dir_name,
path.getctime,
datetime.fromtimestamp
)
date = date_from_obj(file_obj)
file_obj['new'] = date.strftime('%Y-%m-%d_{0}'.format(original(file_obj)))
return file_obj
ret[directory] = map(list_date, ret[directory])
return ret
def rename(ret, directory):
original = compose(
prop('original'),
path_join(directory),
)
new = compose(
prop('new'),
path_join(directory),
)
def do_rename(file_obj):
os.rename(original(file_obj), new(file_obj))
return echo("mv -v {0} {1}".format(original(file_obj), new(file_obj)))
map(do_rename, ret[directory])
return ret
def main(options, directories):
real_directories = filter(path.isdir, directories)
file_tree = reduce(mk_dir_map, real_directories, {})
file_tree = reduce(add_date, file_tree, file_tree)
file_tree = reduce(rename, file_tree, file_tree)
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-f", "--file", dest="filename",
help="write report to FILE", metavar="FILE")
parser.add_option("-q", "--quiet",
action="store_false", dest="verbose", default=True,
help="don't print status messages to stdout")
(options, args) = parser.parse_args()
main(options, args)

Loading…
Cancel
Save