package-info 0.0.0

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/package-info.rb +117 -0
  3. metadata +47 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 28897fb75936668a0423fecd9d20c41c4b486e65
4
+ data.tar.gz: 5cd63566c0fcbfce9acc543adf603ed9c5c1e4ed
5
+ SHA512:
6
+ metadata.gz: 869abc30bc750ce6fe435b9f52f43120c30d9a7a96b1d54b28f15cb9ff4ff42c2023a1d083671766b8832fc6bfdee06e8206f738f63a0b6bf1e63530a52eba2a
7
+ data.tar.gz: e7c68ad82694179ba86b1ba1ee509d89ebae9a6a7f976140aa1258d82569131558447bafc5687491af60afec8a0a8b31ff092b73a93997e506c4f67552f89aa8
@@ -0,0 +1,117 @@
1
+ # encoding: utf-8
2
+ require 'csv'
3
+
4
+ class PackageInfo
5
+ COPYRIGHT_FIELDS = ["Format", "Upstream-Name", "Upstream-Contact",
6
+ "Source", "Disclaimer", "Comment", "License",
7
+ "Copyright", "Files"]
8
+
9
+ LICENSE_TAGS = ["gpl", "mit", "license", "license"]
10
+
11
+ COPYRIGHT_TAGS = ["copyright"]
12
+
13
+ UPSTREAM_NAME_TAGS = ["author", "developer", "upstream"]
14
+
15
+ FILES_TAGS = ["files"]
16
+
17
+ DISCLAIMER_TAGS = ["disclaimer"]
18
+
19
+ FORMAT_TAGS = ["format", "html", "txt"]
20
+
21
+ def create_package_file filename = "packages@#{`hostname`}.csv"
22
+ write_header(filename)
23
+ CSV.open(filename, "ab") do |csv|
24
+ get_package_list.each {|package| csv << get_package_info(package).values}
25
+ end
26
+ end
27
+
28
+ def get_package_list
29
+ `dpkg --get-selections | grep -v "deinstall" | cut -f1 | cut -d':' -f1`.split("\n")
30
+ end
31
+
32
+ def get_package_info package
33
+ puts "retrieve info for #{package}"
34
+ [parse_package_info(package), parse_copyright_file(package)].inject(:merge)
35
+ end
36
+
37
+ def parse_package_info package
38
+ fields, string_fields = {}, `apt-cache show #{package}`.split("\n")
39
+ string_fields.each {|field| fields[field.split(": ")[0]] = field.split(": ")[1]}
40
+ fields
41
+ end
42
+
43
+ def parse_copyright_file package
44
+ return {} if !File.exists?("/usr/share/doc/#{package}/copyright")
45
+ text = File.read("/usr/share/doc/#{package}/copyright")
46
+ is_dep_5_compatible?(text) ? parse_dep_5_compatible(text) : parse_non_dep_5_compatible(text)
47
+ end
48
+
49
+ private
50
+
51
+ def write_header filename
52
+ CSV.open(filename, "w") do |csv|
53
+ csv << get_package_info(get_package_list.first).keys
54
+ end
55
+ end
56
+
57
+ def is_dep_5_compatible? text
58
+ text.include?("Copyright:")
59
+ end
60
+
61
+ def parse_dep_5_compatible text
62
+ fields, lines = {}, text.split(/\n/)
63
+ field_name, field_content = "", ""
64
+
65
+ lines.each do |line|
66
+ field_name, field_content = parse_dep_5_line(line, field_name, field_content)
67
+ if COPYRIGHT_FIELDS.include?(field_name)
68
+ fields[field_name] = field_content
69
+ end
70
+ end
71
+ fields
72
+ end
73
+
74
+ def parse_non_dep_5_compatible text
75
+ fields, paragraphs = init_fields_hash, text.split(/^$\n/)
76
+
77
+ paragraphs.each do |paragraph|
78
+ case
79
+ when match_tags(LICENSE_TAGS, paragraph)
80
+ fields["License"] += paragraph
81
+ when match_tags(COPYRIGHT_TAGS, paragraph)
82
+ fields["Copyright"] += paragraph
83
+ when match_tags(UPSTREAM_NAME_TAGS, paragraph)
84
+ fields["Upstream-Name"] += paragraph
85
+ when match_tags(FILES_TAGS, paragraph)
86
+ fields["Files"] += paragraph
87
+ when match_tags(DISCLAIMER_TAGS, paragraph)
88
+ fields["Disclaimer"] += paragraph
89
+ when match_tags(FORMAT_TAGS, paragraph)
90
+ fields["Format"] += paragraph
91
+ end
92
+ end
93
+ fields
94
+ end
95
+
96
+ def parse_dep_5_line line, field_name, field_content
97
+ if line.include?(": ")
98
+ field_name, field_content = line.split(": ")[0], line.split(": ")[1]
99
+ else
100
+ line = "" if line.nil?
101
+ field_content = "" if field_content.nil?
102
+ field_content += "\n" + line
103
+ end
104
+ return field_name, field_content
105
+ end
106
+
107
+ def init_fields_hash
108
+ fields = {}
109
+ COPYRIGHT_FIELDS.each {|f| fields[f] = ""}
110
+ fields
111
+ end
112
+
113
+ def match_tags tags, paragraph
114
+ tags.each {|tag| return true if paragraph.downcase.include?(tag)}
115
+ false
116
+ end
117
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: package-info
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Angelos Kapsimanis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |2
14
+ Retrieves information for all installed packages
15
+ in Debian based distributions and outputs them in
16
+ the console or a CSV file.
17
+ email: angelos.kapsimanis@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/package-info.rb
23
+ homepage: https://github.com/akxs14/package-info
24
+ licenses:
25
+ - GPL/V3
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.2.2
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Displays package information in Debian-based distros.
47
+ test_files: []