From 6c67b9fe986dbd17cab30eeda4bb8e0d53a47051 Mon Sep 17 00:00:00 2001 From: Maximilian Downey Twiss Date: Fri, 5 Apr 2024 16:25:56 +1100 Subject: [PATCH] Split file command into separate file (#9601) --- bin/crew | 40 +++------------------------------------- commands/files.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ lib/const.rb | 4 ++-- 3 files changed, 47 insertions(+), 39 deletions(-) create mode 100644 commands/files.rb diff --git a/bin/crew b/bin/crew index 341a8b605..21c6ecdb5 100755 --- a/bin/crew +++ b/bin/crew @@ -5,6 +5,7 @@ require 'json' require 'fileutils' require 'tmpdir' require_relative '../commands/const' +require_relative '../commands/files' require_relative '../commands/help' require_relative '../commands/list' require_relative '../commands/prop' @@ -12,7 +13,6 @@ require_relative '../commands/remove' require_relative '../commands/sysinfo' require_relative '../lib/color' require_relative '../lib/const' -require_relative '../lib/convert_size' require_relative '../lib/deb_utils' require_relative '../lib/docopt' require_relative '../lib/downloader' @@ -264,38 +264,6 @@ def cache_build end end -def files(pkg_name) - local_filelist = File.join(CREW_META_PATH, "#{pkg_name}.filelist") - manifest_filelist = File.join(CREW_LIB_PATH, "manifest/#{ARCH}/#{pkg_name[0]}/#{pkg_name}.filelist") - - if File.exist?(local_filelist) - # search for local filelist first - filelist_path = local_filelist - elsif File.exist?(manifest_filelist) - # search for manifest directory if not installed - filelist_path = manifest_filelist - else - # package does not have any filelist available - warn "Package #{pkg_name} is not installed. :(".lightred - return false - end - - filelist = File.readlines(filelist_path, chomp: true).sort - lines = filelist.size - size = 0 - - filelist.each do |filename| - # ignore symlinks to prevent duplicating calculation - size += File.size(filename) if File.file?(filename) && !File.symlink?(filename) - end - - puts filelist, <<~EOT.lightgreen - - Total found: #{lines} - Disk usage: #{human_size(size)} - EOT -end - def whatprovides(regex_pat) matched_list = `grep -R "#{regex_pat}" #{CREW_LIB_PATH}/manifest/#{ARCH}`.lines(chomp: true).flat_map do |result| filelist, matched_file = result.split(':', 2) @@ -1755,10 +1723,8 @@ end def files_command(args) args[''].each do |name| - @pkg_name = name - search @pkg_name - print_current_package - files name + search name + Command.files(@pkg) end end diff --git a/commands/files.rb b/commands/files.rb new file mode 100644 index 000000000..1ef21c054 --- /dev/null +++ b/commands/files.rb @@ -0,0 +1,42 @@ +require 'json' +require_relative '../lib/const' +require_relative '../lib/convert_size' + +class Command + def self.files(pkg) + # Check if the package is even installed first, as this is the most likely reason we cannot find a filelist. + device_json = JSON.load_file(File.join(CREW_CONFIG_PATH, 'device.json')) + if device_json['installed_packages'].none? { |entry| entry['name'] == pkg.name } + puts "Package #{pkg.name} is not installed.".lightred + return + end + + # We can't do anything if we don't have the filelist. + unless File.file?(filelist_path = File.join(CREW_META_PATH, "#{pkg.name}.filelist")) + puts "Package #{pkg.name} does not have a filelist :(".lightred + return + end + + # Print the name and description of the package. + puts pkg.name.lightgreen + ": #{pkg.description}".lightblue + + # Read the file into memory as an array of lines. + filelist = File.readlines(filelist_path, chomp: true) + size = 0 + + filelist.each do |filename| + # Skip calculating the filesize if the file doesn't exist. + next unless File.file?(filename) + # Ignore symlinks to prevent duplicating calculation. + next if File.symlink?(filename) + # Add the size of the file to the total size. + size += File.size(filename) + end + + # Print the filelist, the total number of files, and the total size of those files. + puts filelist + puts "\nTotal found: #{filelist.count}".lightgreen + puts "Disk usage: #{human_size(size)}".lightgreen + end +end + diff --git a/lib/const.rb b/lib/const.rb index 3f42c6584..5c572a5c9 100644 --- a/lib/const.rb +++ b/lib/const.rb @@ -1,7 +1,7 @@ # lib/const.rb # Defines common constants used in different parts of crew -CREW_VERSION = '1.46.3' +CREW_VERSION = '1.46.4' # kernel architecture KERN_ARCH = `uname -m`.chomp @@ -337,7 +337,7 @@ CREW_DOCOPT = <<~DOCOPT crew const [-v|--verbose] [ ...] crew deps [options] [--deep] [-t|--tree] [-b|--include-build-deps] [--exclude-buildessential] [-v|--verbose] ... crew download [options] [-s|--source] [-v|--verbose] ... - crew files [options] [-v|--verbose] ... + crew files ... crew help [] [-v|--verbose] [] crew install [options] [-k|--keep] [-s|--source] [-S|--recursive-build] [-v|--verbose] ... crew list [options] [-v|--verbose] (available|installed|compatible|incompatible)