Mercurial > repo
comparison share/lua/5.2/luarocks/remove.lua @ 1132:d137f631bad5
<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
author | HackBot |
---|---|
date | Fri, 14 Dec 2012 22:24:27 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1131:ff65dfb63263 | 1132:d137f631bad5 |
---|---|
1 | |
2 --- Module implementing the LuaRocks "remove" command. | |
3 -- Uninstalls rocks. | |
4 module("luarocks.remove", package.seeall) | |
5 | |
6 local search = require("luarocks.search") | |
7 local deps = require("luarocks.deps") | |
8 local fetch = require("luarocks.fetch") | |
9 local repos = require("luarocks.repos") | |
10 local path = require("luarocks.path") | |
11 local util = require("luarocks.util") | |
12 local cfg = require("luarocks.cfg") | |
13 local manif = require("luarocks.manif") | |
14 local fs = require("luarocks.fs") | |
15 | |
16 help_summary = "Uninstall a rock." | |
17 help_arguments = "[--force] <name> [<version>]" | |
18 help = [[ | |
19 Argument is the name of a rock to be uninstalled. | |
20 If a version is not given, try to remove all versions at once. | |
21 Will only perform the removal if it does not break dependencies. | |
22 To override this check and force the removal, use --force. | |
23 ]] | |
24 | |
25 --- Obtain a list of packages that depend on the given set of packages | |
26 -- (where all packages of the set are versions of one program). | |
27 -- @param name string: the name of a program | |
28 -- @param versions array of string: the versions to be deleted. | |
29 -- @return array of string: an empty table if no packages depend on any | |
30 -- of the given list, or an array of strings in "name/version" format. | |
31 local function check_dependents(name, versions, deps_mode) | |
32 local dependents = {} | |
33 local blacklist = {} | |
34 blacklist[name] = {} | |
35 for version, _ in pairs(versions) do | |
36 blacklist[name][version] = true | |
37 end | |
38 local local_rocks = {} | |
39 local query_all = search.make_query("") | |
40 query_all.exact_name = false | |
41 search.manifest_search(local_rocks, cfg.rocks_dir, query_all) | |
42 local_rocks[name] = nil | |
43 for rock_name, rock_versions in pairs(local_rocks) do | |
44 for rock_version, _ in pairs(rock_versions) do | |
45 local rockspec, err = fetch.load_rockspec(path.rockspec_file(rock_name, rock_version)) | |
46 if rockspec then | |
47 local _, missing = deps.match_deps(rockspec, blacklist, deps_mode) | |
48 if missing[name] then | |
49 table.insert(dependents, { name = rock_name, version = rock_version }) | |
50 end | |
51 end | |
52 end | |
53 end | |
54 return dependents | |
55 end | |
56 | |
57 --- Delete given versions of a program. | |
58 -- @param name string: the name of a program | |
59 -- @param versions array of string: the versions to be deleted. | |
60 -- @return boolean or (nil, string): true on success or nil and an error message. | |
61 local function delete_versions(name, versions) | |
62 | |
63 for version, _ in pairs(versions) do | |
64 util.printout("Removing "..name.." "..version.."...") | |
65 local ok, err = repos.delete_version(name, version) | |
66 if not ok then return nil, err end | |
67 end | |
68 | |
69 return true | |
70 end | |
71 | |
72 --- Driver function for the "remove" command. | |
73 -- @param name string: name of a rock. If a version is given, refer to | |
74 -- a specific version; otherwise, try to remove all versions. | |
75 -- @param version string: When passing a package name, a version number | |
76 -- may also be given. | |
77 -- @return boolean or (nil, string): True if removal was | |
78 -- successful, nil and an error message otherwise. | |
79 function run(...) | |
80 local flags, name, version = util.parse_flags(...) | |
81 | |
82 if type(name) ~= "string" then | |
83 return nil, "Argument missing, see help." | |
84 end | |
85 | |
86 local deps_mode = flags["deps-mode"] or cfg.deps_mode | |
87 | |
88 local ok, err = fs.check_command_permissions(flags) | |
89 if not ok then return nil, err end | |
90 | |
91 local results = {} | |
92 search.manifest_search(results, cfg.rocks_dir, search.make_query(name, version)) | |
93 | |
94 local versions = results[name] | |
95 if not versions then | |
96 return nil, "Could not find rock '"..name..(version and " "..version or "").."' in local tree." | |
97 else | |
98 local version = next(versions) | |
99 local second = next(versions, version) | |
100 | |
101 util.printout("Checking stability of dependencies on the absence of") | |
102 util.printout(name.." "..table.concat(util.keys(versions), ", ").."...") | |
103 util.printout() | |
104 | |
105 local dependents = check_dependents(name, versions, deps_mode) | |
106 | |
107 if #dependents == 0 or flags["force"] then | |
108 if #dependents > 0 then | |
109 util.printerr("The following packages may be broken by this forced removal:") | |
110 for _, dependent in ipairs(dependents) do | |
111 util.printerr(dependent.name.." "..dependent.version) | |
112 end | |
113 util.printerr() | |
114 end | |
115 local ok, err = delete_versions(name, versions) | |
116 if not ok then return nil, err end | |
117 ok, err = manif.make_manifest(cfg.rocks_dir, deps_mode) | |
118 if not ok then return nil, err end | |
119 else | |
120 if not second then | |
121 util.printerr("Will not remove "..name.." "..version..".") | |
122 util.printerr("Removing it would break dependencies for: ") | |
123 else | |
124 util.printerr("Will not remove all versions of "..name..".") | |
125 util.printerr("Removing them would break dependencies for: ") | |
126 end | |
127 for _, dependent in ipairs(dependents) do | |
128 util.printerr(dependent.name.." "..dependent.version) | |
129 end | |
130 util.printerr() | |
131 util.printerr("Use --force to force removal (warning: this may break modules).") | |
132 return nil, "Failed removing." | |
133 end | |
134 end | |
135 util.printout("Removal successful.") | |
136 return true | |
137 end |