filebydate 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +1 -0
- data/README +44 -0
- data/Rakefile +86 -0
- data/bin/renwdate +19 -0
- data/lib/filebydate/version.rb +9 -0
- data/lib/filebydate.rb +86 -0
- data/test/data/2008-03-20 norename.txt +1 -0
- data/test/data/Alpha.txt +1 -0
- data/test/data/Delta +0 -0
- data/test/data/data/2008-03-20 norename.txt +1 -0
- data/test/data/data/Alpha.txt +1 -0
- data/test/data/data/Delta +0 -0
- data/test/data/data/subdir/Epsilon +1 -0
- data/test/data/subdir/Epsilon +1 -0
- data/test/filebydate_test.rb +72 -0
- data/test/test_helper.rb +2 -0
- metadata +77 -0
data/CHANGELOG
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1 Initial Version
|
data/README
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
README for filebydate
|
2
|
+
=====================
|
3
|
+
|
4
|
+
This gem is a small set of libraries and bin scripts to help organize
|
5
|
+
files by the date they were created. This is a nice, simple system to
|
6
|
+
organize certain types of data, like digital pictures (which was the
|
7
|
+
original motivation to create this library), using a combination of these
|
8
|
+
scripts and the built-in UI of the Mac/Win/Linux OS.
|
9
|
+
|
10
|
+
Features
|
11
|
+
* FileByDate class with rename() method
|
12
|
+
* renwdate script (added to path) to run from the command line (common case)
|
13
|
+
* Rename works by prepending last modification date to the filename
|
14
|
+
e.g. renwdate *
|
15
|
+
with myimg.img in the directory
|
16
|
+
renames the file to something like "2006-06-14 110554 myimg.png"
|
17
|
+
* Preserves the last modification date of the file
|
18
|
+
* Safe to run twice/many times on the same directory, won't prepend twice
|
19
|
+
|
20
|
+
You may want to organize your files into a directory structure like
|
21
|
+
2006
|
22
|
+
2006-08
|
23
|
+
2006-06
|
24
|
+
2006-06-14 110554 myimg.png
|
25
|
+
To keep a clean, date-oriented structure
|
26
|
+
|
27
|
+
Details:
|
28
|
+
|
29
|
+
renwdate takes a wildcard as input, reads the date on each file, and
|
30
|
+
prepends that date into the file name (if the filename doesn't already
|
31
|
+
have a YYYY-MM-DD in it). The purpose is to allow files (usually image
|
32
|
+
files) to be tagged with their creation date in the filename itself, so
|
33
|
+
that the creation date isn't lost when the file is further renamed (beyond
|
34
|
+
the date characters) or manipulated (such as rotating it or fixing red eye).
|
35
|
+
The filename is also used by some on-line photo printing services to print
|
36
|
+
the description of the picture, and this makes sure the date is part of
|
37
|
+
that string.
|
38
|
+
|
39
|
+
In general, it can be useful in any sort of filing system where you want
|
40
|
+
the files primarily organized by date, and have that date quite visible
|
41
|
+
and persistent for each file.
|
42
|
+
|
43
|
+
Originally written in Perl Bernie Thompson Jan 2001 for organizing my own
|
44
|
+
image libraries (over 10K photos of the kids and such)
|
data/Rakefile
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rake/packagetask'
|
6
|
+
require 'rake/gempackagetask'
|
7
|
+
require 'rake/rdoctask'
|
8
|
+
require 'rake/contrib/rubyforgepublisher'
|
9
|
+
require 'fileutils'
|
10
|
+
include FileUtils
|
11
|
+
require File.join(File.dirname(__FILE__), 'lib', 'filebydate', 'version')
|
12
|
+
|
13
|
+
AUTHOR = "Bernie Thompson"
|
14
|
+
EMAIL = "bernie@leancode.com"
|
15
|
+
DESCRIPTION = "Filedate is a set of libraries and scripts for organizing files (e.g. digital photos) by date using the capabilities of the filesystem and OS"
|
16
|
+
RUBYFORGE_PROJECT = "filebydate"
|
17
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
18
|
+
BIN_FILES = %w( renwdate )
|
19
|
+
|
20
|
+
|
21
|
+
NAME = "filebydate"
|
22
|
+
REV = File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
|
23
|
+
VERS = ENV['VERSION'] || (Filebydate::VERSION::STRING + (REV ? ".#{REV}" : ""))
|
24
|
+
CLEAN.include ['**/.*.sw?', '*.gem', '.config']
|
25
|
+
RDOC_OPTS = ['--quiet', '--title', "filebydate documentation",
|
26
|
+
"--opname", "index.html",
|
27
|
+
"--line-numbers",
|
28
|
+
"--main", "README",
|
29
|
+
"--inline-source"]
|
30
|
+
|
31
|
+
desc "Packages up filebydate gem."
|
32
|
+
task :default => [:test]
|
33
|
+
task :package => [:clean]
|
34
|
+
|
35
|
+
Rake::TestTask.new("test") { |t|
|
36
|
+
t.libs << "test"
|
37
|
+
t.pattern = "test/**/*_test.rb"
|
38
|
+
t.verbose = true
|
39
|
+
}
|
40
|
+
|
41
|
+
spec =
|
42
|
+
Gem::Specification.new do |s|
|
43
|
+
s.name = NAME
|
44
|
+
s.version = VERS
|
45
|
+
s.platform = Gem::Platform::RUBY
|
46
|
+
s.has_rdoc = true
|
47
|
+
s.extra_rdoc_files = ["README", "CHANGELOG"]
|
48
|
+
s.rdoc_options += RDOC_OPTS + ['--exclude', '^(examples|extras)/']
|
49
|
+
s.summary = DESCRIPTION
|
50
|
+
s.description = DESCRIPTION
|
51
|
+
s.author = AUTHOR
|
52
|
+
s.email = EMAIL
|
53
|
+
s.homepage = HOMEPATH
|
54
|
+
s.rubyforge_project = RUBYFORGE_PROJECT
|
55
|
+
s.require_path = "lib"
|
56
|
+
s.autorequire = "filebydate"
|
57
|
+
|
58
|
+
#s.add_dependency('activesupport', '>=1.3.1')
|
59
|
+
#s.required_ruby_version = '>= 1.8.2'
|
60
|
+
|
61
|
+
s.bindir = "bin"
|
62
|
+
s.executables = BIN_FILES
|
63
|
+
|
64
|
+
s.files = %w(README CHANGELOG Rakefile) +
|
65
|
+
Dir.glob("{bin,doc,test,lib,templates,generator,extras,website,script}/**/*") +
|
66
|
+
Dir.glob("ext/**/*.{h,c,rb}") +
|
67
|
+
Dir.glob("examples/**/*.rb") +
|
68
|
+
Dir.glob("tools/*.rb")
|
69
|
+
|
70
|
+
# s.extensions = FileList["ext/**/extconf.rb"].to_a
|
71
|
+
end
|
72
|
+
|
73
|
+
Rake::GemPackageTask.new(spec) do |p|
|
74
|
+
p.need_tar = true
|
75
|
+
p.gem_spec = spec
|
76
|
+
end
|
77
|
+
|
78
|
+
task :install do
|
79
|
+
name = "#{NAME}-#{VERS}.gem"
|
80
|
+
sh %{rake package}
|
81
|
+
sh %{sudo gem install pkg/#{name}}
|
82
|
+
end
|
83
|
+
|
84
|
+
task :uninstall => [:clean] do
|
85
|
+
sh %{sudo gem uninstall #{NAME}}
|
86
|
+
end
|
data/bin/renwdate
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env ruby -w
|
2
|
+
require File.dirname(__FILE__) + "/../lib/filebydate.rb"
|
3
|
+
|
4
|
+
bname = File.basename(__FILE__)
|
5
|
+
|
6
|
+
if ARGV.length == 0
|
7
|
+
puts "\n"
|
8
|
+
puts bname + " -- REName With DATE"
|
9
|
+
puts "\nRenames files, prepending their last modification time into the filename."
|
10
|
+
puts "Makes for a simple way of organizing files (such as digital pictures)"
|
11
|
+
puts "by date using just this utility and built-in OS functionality."
|
12
|
+
puts "\n"
|
13
|
+
puts "Examples: "
|
14
|
+
puts " " + bname + " " + File.join("somedirectory", "*.jpg")
|
15
|
+
puts " " + bname + " *"
|
16
|
+
puts "\n"
|
17
|
+
else
|
18
|
+
FileByDate::rename(ARGV)
|
19
|
+
end
|
data/lib/filebydate.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
Dir[File.join(File.dirname(__FILE__), 'filebydate/**/*.rb')].sort.each { |lib| require lib }
|
2
|
+
|
3
|
+
# This module provides functionality for organizing files by date
|
4
|
+
#--
|
5
|
+
# Copyright (c) 2006 Bernie Thompson, Leancode, LLC.
|
6
|
+
# MIT license
|
7
|
+
module FileByDate
|
8
|
+
|
9
|
+
EXCLUDE_PATTERN = /\d\d\d\d-\d\d-\d\d/
|
10
|
+
RENAME_PATTERN = "%04d-%02d-%02d %02d%02d%02d %s"
|
11
|
+
|
12
|
+
# Given an array of filespecs (filenames or wildcards),
|
13
|
+
# this method will rename those files to prepend the
|
14
|
+
# last modification date into the filename.
|
15
|
+
# * Directories are skipped
|
16
|
+
# * Files already having a prepended date are skipped
|
17
|
+
# The function returns an array with the newly renamed filenames
|
18
|
+
# There are not yet any valid options
|
19
|
+
def self.rename(paths, options = {})
|
20
|
+
renamed_list = []
|
21
|
+
|
22
|
+
paths.each do |path|
|
23
|
+
|
24
|
+
if File.basename(path).match(/\*|\?/)
|
25
|
+
# We were passed a wildcard (e.g. Windows), so expand it
|
26
|
+
# On unix, we'll never get a wildcard because the shell will do the expansion
|
27
|
+
rename(Dir.glob(path), options)
|
28
|
+
elsif not File.directory?(path)
|
29
|
+
renamed_list << rename_file(path, options)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
return renamed_list
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
# Renames a single file to prepend a date, starting with year, month, day, hour, min, sec
|
39
|
+
def self.rename_file(file, options = {})
|
40
|
+
|
41
|
+
newfile = nil
|
42
|
+
|
43
|
+
# matches a date ANYWHERE in the filename. So if you want to prepend something
|
44
|
+
# to a file that's already been converted, it won't fight with you and prepend another date.
|
45
|
+
if not (file.match( EXCLUDE_PATTERN ))
|
46
|
+
atime = File.atime(file)
|
47
|
+
mtime = File.mtime(file)
|
48
|
+
|
49
|
+
# figure out new basename+ext, based on modification time of the file
|
50
|
+
newfile = sprintf(RENAME_PATTERN,
|
51
|
+
mtime.year,mtime.month,mtime.day,
|
52
|
+
mtime.hour, mtime.min, mtime.sec, File.basename(file))
|
53
|
+
newfile = File.join(File.dirname(file), newfile)
|
54
|
+
|
55
|
+
# output each of the files converted
|
56
|
+
puts newfile + "\n"
|
57
|
+
|
58
|
+
# rename the file
|
59
|
+
File.rename(file, newfile)
|
60
|
+
|
61
|
+
# set the modification time back to the original, before renaming
|
62
|
+
File.utime(atime, mtime)
|
63
|
+
end
|
64
|
+
|
65
|
+
return newfile
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# original 'filedate' Perl script written to organize image files
|
70
|
+
#
|
71
|
+
# use File::Basename;
|
72
|
+
#
|
73
|
+
# @files= <$ARGV[0]>;
|
74
|
+
#
|
75
|
+
# foreach $oldname (@files)
|
76
|
+
# {
|
77
|
+
# if ($oldname =~ m/\d\d\d\d-\d\d-\d\d/) { next; }
|
78
|
+
# ($readtime, $writetime) = (stat($oldname))[8,9];
|
79
|
+
# ($name, $dir, $ext) = fileparse($oldname,'\..*');
|
80
|
+
# ($sec, $min, $hour, $day, $month, $year, $junk) = (localtime($writetime));
|
81
|
+
# $year += 1900; $month += 1;
|
82
|
+
# $newname = sprintf("%s%04d-%02d-%02d %02d%02d%02d %s%s%s", $dir, $year, $month, $day, $hour, $min, $sec, $name, $ext);
|
83
|
+
# print "$newname\n";
|
84
|
+
# rename $oldname, $newname;
|
85
|
+
# utime($readtime, $writetime, $newname);
|
86
|
+
# }
|
@@ -0,0 +1 @@
|
|
1
|
+
Test data
|
data/test/data/Alpha.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Test data
|
data/test/data/Delta
ADDED
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
Test data
|
@@ -0,0 +1 @@
|
|
1
|
+
Test data
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
Test data
|
@@ -0,0 +1 @@
|
|
1
|
+
Test data
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
require "tempfile"
|
3
|
+
require "fileutils"
|
4
|
+
|
5
|
+
class FilebydateTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
@testfile = ""
|
8
|
+
@testpath = ""
|
9
|
+
@binpath = ""
|
10
|
+
|
11
|
+
#
|
12
|
+
# We keep some test data in subversion (alternately, we could create it fresh here)
|
13
|
+
# and copy it to a temp directory to run the filebydate tests against
|
14
|
+
# Test::Unit makes sure this is done before each test,
|
15
|
+
# giving us the same clean test data to start with each time
|
16
|
+
#
|
17
|
+
def setup
|
18
|
+
f = Tempfile.new("FileDateTest")
|
19
|
+
@testfile = f.path
|
20
|
+
@testpath = File.join(File.dirname(@testfile), "data")
|
21
|
+
@binpath = File.join(File.dirname(__FILE__), '..', 'bin')
|
22
|
+
|
23
|
+
f.write("This is a test file")
|
24
|
+
f.close
|
25
|
+
if File.exists?(@testpath)
|
26
|
+
FileUtils::rm_r(@testpath)
|
27
|
+
end
|
28
|
+
FileUtils::cp_r(File.join(File.dirname(__FILE__), 'data'), @testpath)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_single_file_renamed
|
32
|
+
FileByDate::rename(@testfile)
|
33
|
+
assert(!File.exists?(@testfile))
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_wildcard_files_renamed
|
37
|
+
assert(File.exists?(File.join(@testpath, "Delta")))
|
38
|
+
FileByDate::rename([File.join(@testpath, '*')])
|
39
|
+
assert(!File.exists?(File.join(@testpath, "Delta")))
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_mtime_preserved
|
43
|
+
file = File.join(@testpath, "Delta")
|
44
|
+
time = File.mtime(file)
|
45
|
+
newlist = FileByDate::rename([file])
|
46
|
+
assert_equal(time, File.mtime(newlist[0]))
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_bin_script_dir
|
50
|
+
assert(File.exists?(File.join(@testpath, "Alpha.txt")))
|
51
|
+
assert(File.exists?(File.join(@testpath, "Delta")))
|
52
|
+
system(@binpath + '/renwdate ' + File.join(@testpath, '*.txt'))
|
53
|
+
assert(!File.exists?(File.join(@testpath, "Alpha.txt")))
|
54
|
+
assert(File.exists?(File.join(@testpath, "Delta")))
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_bin_script_wildcard
|
58
|
+
assert(File.exists?(File.join(@testpath, "Delta")))
|
59
|
+
assert(File.exists?(File.join(@testpath, 'subdir', 'Epsilon')))
|
60
|
+
system(@binpath + '/renwdate ' + File.join(@testpath,'*'))
|
61
|
+
|
62
|
+
assert(!File.exists?(File.join(@testpath, "Delta")))
|
63
|
+
# Without recurse option, files in subdirectory should not be renamed
|
64
|
+
assert(File.exists?(File.join(@testpath, 'subdir', 'Epsilon')))
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_no_double_rename
|
68
|
+
assert(File.exists?(File.join(@testpath, "2008-03-20 norename.txt")))
|
69
|
+
FileByDate::rename([File.join(@testpath, '*')])
|
70
|
+
assert(File.exists?(File.join(@testpath, "2008-03-20 norename.txt")))
|
71
|
+
end
|
72
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.0
|
3
|
+
specification_version: 1
|
4
|
+
name: filebydate
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.1
|
7
|
+
date: 2006-11-06 00:00:00 -08:00
|
8
|
+
summary: Filedate is a set of libraries and scripts for organizing files (e.g. digital photos) by date using the capabilities of the filesystem and OS
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: bernie@leancode.com
|
12
|
+
homepage: http://filebydate.rubyforge.org
|
13
|
+
rubyforge_project: filebydate
|
14
|
+
description: Filedate is a set of libraries and scripts for organizing files (e.g. digital photos) by date using the capabilities of the filesystem and OS
|
15
|
+
autorequire: filebydate
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Bernie Thompson
|
31
|
+
files:
|
32
|
+
- README
|
33
|
+
- CHANGELOG
|
34
|
+
- Rakefile
|
35
|
+
- bin/renwdate
|
36
|
+
- test/data
|
37
|
+
- test/filebydate_test.rb
|
38
|
+
- test/test_helper.rb
|
39
|
+
- test/data/2008-03-20 norename.txt
|
40
|
+
- test/data/Alpha.txt
|
41
|
+
- test/data/data
|
42
|
+
- test/data/Delta
|
43
|
+
- test/data/subdir
|
44
|
+
- test/data/data/2008-03-20 norename.txt
|
45
|
+
- test/data/data/Alpha.txt
|
46
|
+
- test/data/data/Delta
|
47
|
+
- test/data/data/subdir
|
48
|
+
- test/data/data/subdir/Epsilon
|
49
|
+
- test/data/subdir/Epsilon
|
50
|
+
- lib/filebydate
|
51
|
+
- lib/filebydate.rb
|
52
|
+
- lib/filebydate/version.rb
|
53
|
+
test_files: []
|
54
|
+
|
55
|
+
rdoc_options:
|
56
|
+
- --quiet
|
57
|
+
- --title
|
58
|
+
- filebydate documentation
|
59
|
+
- --opname
|
60
|
+
- index.html
|
61
|
+
- --line-numbers
|
62
|
+
- --main
|
63
|
+
- README
|
64
|
+
- --inline-source
|
65
|
+
- --exclude
|
66
|
+
- ^(examples|extras)/
|
67
|
+
extra_rdoc_files:
|
68
|
+
- README
|
69
|
+
- CHANGELOG
|
70
|
+
executables:
|
71
|
+
- renwdate
|
72
|
+
extensions: []
|
73
|
+
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
dependencies: []
|
77
|
+
|