Difference between revisions of "Tidy-rpm-cache"

From Notes_Wiki
m
m
Line 54: Line 54:
Cur1_name = element(2, Cur1),
Cur1_name = element(2, Cur1),
Prev1_name = element(2, Prev1),
Prev1_name = element(2, Prev1),
                                Cur1_arch = element(4, Cur1),
                                Prev1_arch = element(4, Prev1),
if
if
    %%Current is newer version of Previous
    %%Current is newer version of Previous
    Prev1_name =:= Cur1_name ->
                                    (Prev1_name =:= Cur1_name) and (Cur1_arch =:= Prev1_arch) ->
Prev1_filename=((Dir1 ++ "/") ++ element(1, Prev1)),
Prev1_filename=((Dir1 ++ "/") ++ element(1, Prev1)),
if  
if  

Revision as of 03:46, 12 May 2014

<yambe:breadcrumb>Package management tools</yambe:breadcrumb> <yambe:breadcrumb>Useful erlang scripts</yambe:breadcrumb>

Script to delete older versions of rpm files from 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.


<yambe:breadcrumb>Package management tools</yambe:breadcrumb> <yambe:breadcrumb>Useful erlang scripts</yambe:breadcrumb>