File: clean_stage.py

package info (click to toggle)
cmake-format 0.6.13-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,436 kB
  • sloc: python: 16,990; makefile: 14
file content (65 lines) | stat: -rw-r--r-- 1,624 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Given a staging directory and a file containing a list of what should be in
that directory, delete any files that shouldn't be there.
"""

import argparse
import io
import logging
import os
import sys

logger = logging.getLogger(__name__)


def setup_argparser(argparser):
  argparser.add_argument(
      "manifest_path", help="path to the manifest file")
  argparser.add_argument(
      "stage_path", help="path to the staging directory")


def get_argdict(args):
  out = {}
  for key, value in vars(args).items():
    if key.startswith("_"):
      continue
    out[key] = value
  return out


def inner_main(manifest_path, stage_path):
  with io.open(manifest_path, "r", encoding="utf-8") as infile:
    manifest = set(line.strip() for line in infile)

  for directory, _dirnames, filenames in os.walk(stage_path):
    relpath_dir = os.path.relpath(directory, stage_path)

    for filename in filenames:
      if relpath_dir == ".":
        relpath_file = filename
      else:
        relpath_file = os.path.join(relpath_dir, filename)
      fullpath_file = os.path.join(directory, filename)

      if relpath_file not in manifest and fullpath_file not in manifest:
        logger.info("Deleting %s", relpath_file)
        os.unlink(fullpath_file)


def main():
  logging.basicConfig(level=logging.INFO)
  argparser = argparse.ArgumentParser(description=__doc__)
  setup_argparser(argparser)
  try:
    import argcomplete
    argcomplete.autocomplete(argparser)
  except ImportError:
    pass
  args = argparser.parse_args()
  inner_main(**get_argdict(args))
  return 0


if __name__ == "__main__":
  sys.exit(main())