dir-to-xml 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/dir-to-xml.rb +92 -0
  2. metadata +55 -0
data/lib/dir-to-xml.rb ADDED
@@ -0,0 +1,92 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # file: dir-to-xml.rb
4
+
5
+ require 'rexml/document'
6
+
7
+ class DirToXML
8
+ #include PrettyXML
9
+ include REXML
10
+
11
+ attr_reader :status
12
+
13
+ def initialize(path= '.')
14
+ Dir.chdir path
15
+
16
+ a = Dir.glob("*").sort
17
+ command = a.include?('dir.xml') ? 'run' : 'new_run'
18
+ doc, @status = self.send command, a
19
+ end
20
+
21
+ def new_element(name, text=nil)
22
+ new_node = Element.new(name)
23
+ new_node.text = text if text
24
+ new_node
25
+ end
26
+
27
+ def new_file(name, type, ext, ctime, mtime, atime)
28
+ node = Element.new('file')
29
+ node.add_element new_element('name', name)
30
+ node.add_element new_element('type', type)
31
+ node.add_element new_element('ext', ext)
32
+ node.add_element new_element('created', ctime)
33
+ node.add_element new_element('last_modified', mtime)
34
+ node.add_element new_element('last_accessed', atime)
35
+ node.add_element new_element('description')
36
+ node.add_element new_element('owner')
37
+ node.add_element new_element('group')
38
+ node.add_element new_element('permissions')
39
+ node
40
+ end
41
+
42
+ def add_files(doc, a)
43
+ dir_files = a.map do |x|
44
+ [x, File.extname(x), File::ftype(x), File::ctime(x), File::mtime(x), File::atime(x)]
45
+ end
46
+
47
+ dir_files.each do |name, ext, type, ctime, mtime, atime|
48
+ doc.root.elements['records'].add_element new_file(name, type, ext, ctime, mtime, atime)
49
+ end
50
+ end
51
+
52
+ def new_run(a)
53
+
54
+ summary = "<summary><title>Index of %s</title><file_path>%s</file_path></summary>" % [File.basename(Dir.pwd), File.dirname(Dir.pwd)[/\/home\/james\/heroku\/(.*)/,1]]
55
+ #summary = "<summary/>"
56
+ buffer = "<?xml version='1.0' encoding='UTF-8'?><directory>%s<records/></directory>" % summary
57
+ doc = Document.new(buffer)
58
+ add_files(doc, a)
59
+
60
+ doc.root.elements['records'].add_element new_file(name='dir.xml', type=nil, ext='.xml', *[Time.now] * 3)
61
+
62
+ #File.open('dir.xml','w'){|f| f.write pretty_xml(doc)}
63
+ File.open('dir.xml','w'){|f| doc.write f}
64
+ [doc, "created"]
65
+ end
66
+
67
+ def run(a)
68
+
69
+ doc = Document.new(File.open('dir.xml','r').read)
70
+ #puts 'before a)dir'
71
+ a_dir = XPath.match(doc.root, 'records/file/name/text()').map {|x| x.to_s}.sort
72
+
73
+ return [doc, "nothing new"] if a == a_dir
74
+
75
+ # files to add
76
+ files_to_insert = a - a_dir
77
+ add_files(doc, files_to_insert)
78
+
79
+ # files to delete
80
+ files_to_delete = a_dir - a
81
+ files_to_delete.each do |filename|
82
+ node = doc.root.elements["records/file[name='#{filename}']"]
83
+ node.parent.delete node
84
+ end
85
+
86
+ #File.open('dir.xml','w'){|f| f.write pretty_xml(doc)}
87
+ File.open('dir.xml','w'){|f| doc.write f}
88
+ [doc, "updated"]
89
+ end
90
+ end
91
+
92
+
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dir-to-xml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors: []
7
+
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-06-05 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/dir-to-xml.rb
26
+ has_rdoc: true
27
+ homepage:
28
+ licenses: []
29
+
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.3.5
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: dir-to-xml
54
+ test_files: []
55
+