#!/usr/bin/env python

"""
Installs packages built from the specs in <component-specs-dir>.
"""


import sys
import os
import glob
import subprocess
import shutil
import argparse

from collections import namedtuple

import json
from planex.globals import RPMS_DIR

CONFIG = "install.json"


ValidationResult = namedtuple('ValidationResult', 'failed, message')


class SpecsDir(object):
    def __init__(self, filesystem, path):
        self.filesystem = filesystem
        self.path = path

    @property
    def has_config(self):
        return self.filesystem.file_exists(self.config_path)

    @property
    def config_path(self):
        return self.filesystem.join(self.path, CONFIG)

    @property
    def config_is_json(self):
        contents = self.filesystem.contents_of(self.config_path)
        try:
            json.loads(contents)
            return True
        except ValueError:
            return False

    def validate(self):
        if not self.filesystem.directory_exists(self.path):
            return ValidationResult(
                failed=True,
                message='Invalid specs dir: [{0}] is not an existing directory'.format(
                    self.path))
        if self.has_config:
            if not self.config_is_json:
                return ValidationResult(
                    failed=True,
                    message='Invalid specs dir: [{0}] is not a json file'.format(
                        self.config_path))
        return ValidationResult(
            failed=False,
            message=None)

    def get_packages(self):
        pkgs = json.loads(self.filesystem.contents_of(self.config_path))
        return [pkg['package-name'] for pkg in pkgs]




def parse_config(config_path):
    """Returns list of package names to install. These can be used in
    conjunction with rpm query to find the relevant .rpm files that need
    installing.
    """
    config_file = open(config_path, "r")
    pkgs = json.load(config_file)
    config_file.close()
    return [pkg['package-name'] for pkg in pkgs]


def build_map(rpms_dir):
    """Returns a map from package name to rpm file"""
    result = {}
    for rpm_file in glob.glob(os.path.join(rpms_dir, '*.rpm')):
        pkg_name = subprocess.Popen(
            ["rpm", "-qp", rpm_file, "--qf", "%{name}"],
            stdout=subprocess.PIPE).communicate()[0].strip()
        result[pkg_name] = rpm_file
    return result


def parse_args_or_exit(argv=None):
    parser = argparse.ArgumentParser()
    parser.add_argument('component_dir', help='Specs directory')
    parser.add_argument('dest_dir', help='Destination directory')
    return parser.parse_args(argv)


def main():
    """Main entry point.   Installs packages from <component-dir>
    to <destination-dir>"""
    if len(sys.argv[1:]) != 2:
        print "Usage: %s <component-dir> <destination-dir>" % __file__
        sys.exit(1)
    (component_dir, dest_dir) = sys.argv[1:]

    if not os.path.exists(component_dir):
        print "Error: directory %s does not exist." % component_dir
        sys.exit(1)

    config_path = os.path.join(component_dir, CONFIG)
    if not os.path.exists(config_path):
        print ("Config file %s not found, assuming no RPMs need packaging." %
               config_path)
        sys.exit(0)

    if not os.path.exists(dest_dir):
        os.makedirs(dest_dir)

    config = parse_config(config_path)

    pkg_to_rpm = build_map(RPMS_DIR)

    for pkg_name in config:
        rpm_path = pkg_to_rpm[pkg_name]
        print "Copying:  %s -> %s" % (rpm_path, dest_dir)
        shutil.copy(rpm_path, dest_dir)


if __name__ == '__main__':
    main()

@@ -2766,174 +2766,40 @@
 %22%22%22%0A
+%0A
     
-if len(sys.argv%5B1:%5D) != 2:%0A        print %22Usage: %25s %3Ccomponent-dir%3E %3Cdestination-dir%3E%22 %25 __file__%0A        sys.
+args = parse_args_or_
 exit(
-1
 )
-%0A    (component_dir, dest_dir) = sys.argv%5B1:%5D
 %0A%0A  
@@ -2822,16 +2822,21 @@
 .exists(
+args.
 componen
@@ -2897,16 +2897,21 @@
 ist.%22 %25 
+args.
 componen
@@ -2968,16 +2968,21 @@
 th.join(
+args.
 componen
@@ -3189,16 +3189,21 @@
 .exists(
+args.
 dest_dir
@@ -3225,16 +3225,21 @@
 akedirs(
+args.
 dest_dir
@@ -3427,32 +3427,37 @@
 s%22 %25 (rpm_path, 
+args.
 dest_dir)%0A      
@@ -3480,16 +3480,21 @@
 m_path, 
+args.
 dest_dir

Use parse_args_or_exit