Difference between revisions of "Tidy-rpm-cache"

From Notes_Wiki
m
m
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
<yambe:breadcrumb>Package management tools</yambe:breadcrumb>
[[Main_Page|Home]] > [[Erlang]] > [[Useful erlang scripts]] > [[Tidy-rpm-cache|Script to delete older versions of rpm files from cache]]
=tidy-rpm-cache.py (previous rm_obsolete_rpms.py)=


We can download latest version of tidy-rpm from internet and then use it to delete obsolete packages from yum-cahce. This script saves considerable disk-space and keeps only latest version of files in cache / repository.
[[Main Page|Home]] > [[CentOS]] > [[CentOS 6.x]] > [[System administration tools]] > [[Package management tools]] > [[Tidy-rpm-cache]]


To see list of obsolete RPMs inside a directory recursively and then get option of deleting obsolete RPMs use:
Both tidy_rpm_cache and delete_obsolete_rpm seem to have been removed from all download locations. Wierd!!.  A simple escript which can achieve similar results is mentioned below.  Steps for using the scripts are:
#Save the script as file "tidy_rpm_cache.escript"
#Set execute permissions (chmod +x) on file
#Install erlang (yum -y install erlang). 
#Execute escript as root user with arguments "no" &lt;dir&gt; for obtaining list of files that would get deleted upon confirmation.  Pipe output to less as the list could be long.
#Change argument "no" to "yes" to confirm and delete old packages.  List of files deleted is printed.
<pre>
<pre>
tidy-rpm-cache.py --dir=/var/cache/yum
#!/usr/bin/env escript
</pre>
 
%% Invoke as
%%    sudo tidy_rpm_cache.escript "yes" */packages
%% from /var/cache/yum/x86_64/6/ folder
%%
 
main([Confirm | Dir_list]) ->
    lists:foreach(fun(Dir1) -> tidy_rpm_cache(Dir1, Confirm) end, Dir_list),
    ok.
 
 
tidy_rpm_cache(Dir1, Confirm) ->
    case file:list_dir(Dir1) of
{ok, File_list1} ->
    File_list2=lists:filter(fun(File1) ->
    string_ends_with(File1, ".rpm") or string_ends_with(File1, ".drpm")
    end,
    File_list1),
    File_list3=lists:sort(File_list2),
    Name_list1 = lists:map(fun(File1) ->
  %%Tokenize by . to get second last element as architecture
  Tokens1=string:tokens(File1, "."),
  Length = length(Tokens1),
  Architecture = lists:nth(Length - 1, Tokens1),
  Remaining1 = lists:sublist(Tokens1, Length-2),
  Remaining2 = string:join(Remaining1, "."),


  %%Tokenize remaining by - as first non-digit thing as version
  Tokens2 = string:tokens(Remaining2, "-"),
  {Name1, Version1} = lists:splitwith(fun(Current_token1) ->
    Char1=hd(Current_token1),
    (Char1 =< $0) or (Char1 >= $9)
    end,
    Tokens2),
  Name2=string:join(Name1, "-"),
  Version2=string:join(Version1, "-"),
  {File1, Name2, Version2, Architecture}
  end,
  File_list3),
    io:format("For ~p list is ~p~n", [Dir1, Name_list1]),
    lists:foldl(fun(Cur1, Prev1) ->
Cur1_name = element(2, Cur1),
Prev1_name = element(2, Prev1),
                                Cur1_arch = element(4, Cur1),
                                Prev1_arch = element(4, Prev1),
if
    %%Current is newer version of Previous
                                    (Prev1_name =:= Cur1_name) and (Cur1_arch =:= Prev1_arch) ->
Prev1_filename=((Dir1 ++ "/") ++ element(1, Prev1)),
if
    Confirm =:= "yes" ->
io:format("Deleting ~p~n", [Prev1_filename]),
file:delete(Prev1_filename);
    true ->
io:format("Will delete ~p on confirmation~n", [Prev1_filename])
end;


==Information on rm_obsolete_rpms==
    %%Current and Previous are different packages
    true ->
continue
end,
Cur1
end,
{"", "", "", ""},
Name_list1);
   
{error, Reason1} ->
    io:format("Cannot open dir ~p because of ~p~n", [Dir1, Reason1])
    end.


Normally yum packages are stored in sub-directories inside '<tt>/var/cache/yum</tt>' and they occupy lot of space. Multiple versions of updates / install files for same package can be present in yum cache which leads to wastage of hard-disk space. We may want to keep only the latest versions in cache so that we can copy cache to other machine and avoid re-download of package over internet. But for this we do not need older versions of packages.


Hence to selectively remove older versions of packages we can use '<tt>rm_obsolete_rpms</tt>' python script.


If we use this script on 64-bit systems than it cannot understand that both 32-bit and 64-bit versions of package must be kept. Hence we can run the script as:
string_ends_with(String1, String2) ->
<pre>
    Length2=length(String2),
./rm_pbsolete_rpms -n 1 -d /var/cache/yum
    Length1=length(String1),
    Substr1=string:substr(String1, Length1-Length2+1, Length2),
    Substr1 =:= String2.
</pre>
</pre>
so that on 64-bit systems last two version of packages are kept. In this case both 64-bit and 32-bit versions of package are left and only older versions are deleted from cache.
 
 
 
[[Main_Page|Home]] > [[Erlang]] > [[Useful erlang scripts]] > [[Tidy-rpm-cache|Script to delete older versions of rpm files from cache]]
 
[[Main Page|Home]] > [[CentOS]] > [[CentOS 6.x]] > [[System administration tools]] > [[Package management tools]] > [[Tidy-rpm-cache]]

Latest revision as of 12:20, 28 July 2022

Home > Erlang > Useful erlang scripts > Script to delete older versions of rpm files from cache

Home > CentOS > CentOS 6.x > System administration tools > Package management tools > Tidy-rpm-cache

Both tidy_rpm_cache and delete_obsolete_rpm seem to have been removed from all download locations. Wierd!!. A simple escript which can achieve similar results is mentioned below. Steps for using the scripts are:

  1. Save the script as file "tidy_rpm_cache.escript"
  2. Set execute permissions (chmod +x) on file
  3. Install erlang (yum -y install erlang).
  4. Execute escript as root user with arguments "no" <dir> for obtaining list of files that would get deleted upon confirmation. Pipe output to less as the list could be long.
  5. Change argument "no" to "yes" to confirm and delete old packages. List of files deleted is printed.
#!/usr/bin/env escript

%% Invoke as
%%    sudo tidy_rpm_cache.escript "yes" */packages
%% from /var/cache/yum/x86_64/6/ folder
%%

main([Confirm | Dir_list]) ->
    lists:foreach(fun(Dir1) -> tidy_rpm_cache(Dir1, Confirm) end, Dir_list),
    ok.


tidy_rpm_cache(Dir1, Confirm) ->
    case file:list_dir(Dir1) of
	{ok, File_list1} ->
	    File_list2=lists:filter(fun(File1) ->
					    string_ends_with(File1, ".rpm") or string_ends_with(File1, ".drpm")
				    end,
				    File_list1),
	    File_list3=lists:sort(File_list2),
	    Name_list1 = lists:map(fun(File1) ->
					   %%Tokenize by . to get second last element as architecture
					   Tokens1=string:tokens(File1, "."),
					   Length = length(Tokens1),
					   Architecture = lists:nth(Length - 1, Tokens1),
					   Remaining1 = lists:sublist(Tokens1, Length-2),
					   Remaining2 = string:join(Remaining1, "."),

					   %%Tokenize remaining by - as first non-digit thing as version
					   Tokens2 = string:tokens(Remaining2, "-"),
					   {Name1, Version1} = lists:splitwith(fun(Current_token1) ->
										     Char1=hd(Current_token1),
										     (Char1 =< $0) or (Char1 >= $9)
									     end,
									     Tokens2),
					   Name2=string:join(Name1, "-"),
					   Version2=string:join(Version1, "-"),
					   {File1, Name2, Version2, Architecture}
				   end,
				   File_list3),
	    io:format("For ~p list is ~p~n", [Dir1, Name_list1]),
	    lists:foldl(fun(Cur1, Prev1) ->
				Cur1_name = element(2, Cur1),
				Prev1_name = element(2, Prev1),
                                Cur1_arch = element(4, Cur1),
                                Prev1_arch = element(4, Prev1),
				if
				    %%Current is newer version of Previous
                                    (Prev1_name =:= Cur1_name) and (Cur1_arch =:= Prev1_arch) ->
					Prev1_filename=((Dir1 ++ "/") ++ element(1, Prev1)),
					if 
					    Confirm =:= "yes" ->
						io:format("Deleting ~p~n", [Prev1_filename]),
						file:delete(Prev1_filename);
					    true ->
						io:format("Will delete ~p on confirmation~n", [Prev1_filename])
					end;

				    %%Current and Previous are different packages
				    true ->
					continue
				end,
				Cur1
			end,
			{"", "", "", ""},
			Name_list1);
	    
	{error, Reason1} ->
	    io:format("Cannot open dir ~p because of ~p~n", [Dir1, Reason1])
    end.



string_ends_with(String1, String2) ->
    Length2=length(String2),
    Length1=length(String1),
    Substr1=string:substr(String1, Length1-Length2+1, Length2),
    Substr1 =:= String2.


Home > Erlang > Useful erlang scripts > Script to delete older versions of rpm files from cache

Home > CentOS > CentOS 6.x > System administration tools > Package management tools > Tidy-rpm-cache