1 | #!/usr/bin/python |
---|
2 | from BuildManager.optionparser import * |
---|
3 | from BuildManager.clean import * |
---|
4 | from BuildManager import * |
---|
5 | import fnmatch |
---|
6 | import logging |
---|
7 | import sys, os |
---|
8 | import pwd |
---|
9 | |
---|
10 | AUTHOR = "Gustavo Niemeyer <niemeyer@conectiva.com>" |
---|
11 | VERSION = "2.2" |
---|
12 | |
---|
13 | def parse_options(): |
---|
14 | parser = OptionParser("%prog [OPTIONS] <srpm files>", |
---|
15 | version="%prog "+VERSION) |
---|
16 | parser.add_option("--remove", action="store_true", |
---|
17 | help="remove old packages (default)") |
---|
18 | parser.add_option("--move", metavar="DIR", |
---|
19 | help="move old packages to given directory") |
---|
20 | parser.add_option("--copy", metavar="DIR", |
---|
21 | help="copy old packages to given directory") |
---|
22 | parser.add_option("--check", metavar="DIR", action="append", default=[], |
---|
23 | help="check given directory for newer pkgs, " |
---|
24 | "but don't remove files there") |
---|
25 | |
---|
26 | parser.add_option("--dryrun", dest="dryrun", action="store_true", |
---|
27 | help="do not commit changes to the system") |
---|
28 | parser.add_option("--log", dest="loglevel", metavar="LEVEL", |
---|
29 | help="set logging level to LEVEL (debug, info, " |
---|
30 | "warning, error)", default="info") |
---|
31 | opts, args = parser.parse_args() |
---|
32 | opts.args = args |
---|
33 | |
---|
34 | if not opts.args: |
---|
35 | raise Error, "you must specify one or more files to process" |
---|
36 | |
---|
37 | actions = 0 |
---|
38 | if opts.remove: |
---|
39 | actions += 1 |
---|
40 | if opts.copy: |
---|
41 | actions += 1 |
---|
42 | if opts.move: |
---|
43 | actions += 1 |
---|
44 | |
---|
45 | if actions > 1: |
---|
46 | raise Error, "you can't specify more than one action to perform" |
---|
47 | |
---|
48 | for attr in ["move", "copy", "check"]: |
---|
49 | attrval = getattr(opts, attr) |
---|
50 | error = False |
---|
51 | if type(attrval) is list: |
---|
52 | for attrval in attrval: |
---|
53 | if attrval and not os.path.isdir(attrval): |
---|
54 | error = True |
---|
55 | break |
---|
56 | else: |
---|
57 | if attrval and not os.path.isdir(attrval): |
---|
58 | error = True |
---|
59 | if error: |
---|
60 | raise Error, "value of --%s must be a directory" \ |
---|
61 | % attr.replace("_", "-") |
---|
62 | |
---|
63 | return opts |
---|
64 | |
---|
65 | def main(): |
---|
66 | # Get the right $HOME, even when using sudo. |
---|
67 | if os.getuid() == 0: |
---|
68 | os.environ["HOME"] = pwd.getpwuid(0)[5] |
---|
69 | try: |
---|
70 | opts = parse_options() |
---|
71 | logger.setLevel(logging.getLevelName(opts.loglevel.upper())) |
---|
72 | logger.debug("starting bmclean") |
---|
73 | cleaner = PackageCleaner(opts) |
---|
74 | status = cleaner.run() |
---|
75 | except Error, e: |
---|
76 | logger.error(str(e)) |
---|
77 | logger.debug("finishing bmclean with error") |
---|
78 | sys.exit(1) |
---|
79 | else: |
---|
80 | logger.debug("finishing bmclean") |
---|
81 | sys.exit(int(not status)) |
---|
82 | |
---|
83 | if __name__ == "__main__": |
---|
84 | main() |
---|
85 | |
---|
86 | # vim:et:ts=4:sw=4 |
---|