From 14baa667df024c5ce2c35985e1676d2bb14310ec Mon Sep 17 00:00:00 2001 From: troyready Date: Thu, 1 Nov 2018 12:19:17 -0700 Subject: [PATCH] add local package sources --- docs/config.rst | 10 ++++++++-- stacker/config/__init__.py | 9 +++++++++ stacker/util.py | 27 +++++++++++++++++++++++---- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/docs/config.rst b/docs/config.rst index a4120c2be..3e57af61e 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -156,8 +156,14 @@ For ``.tar.gz`` & ``zip`` archives on s3, specify a ``bucket`` & ``key``:: # last modified date on S3 changes use_latest: false -Use the ``paths`` option when subdirectories of the repo/archive should be -added to Stacker's ``sys.path``. +Local directories can also be specified:: + + package_sources: + local: + - source: ../vpc + +Use the ``paths`` option when subdirectories of the repo/archive/directory +should be added to Stacker's ``sys.path``. Cloned repos/archives will be cached between builds; the cache location defaults to ~/.stacker but can be manually specified via the **stacker_cache_dir** top diff --git a/stacker/config/__init__.py b/stacker/config/__init__.py index becb97d44..be53e2d97 100644 --- a/stacker/config/__init__.py +++ b/stacker/config/__init__.py @@ -234,6 +234,13 @@ def not_empty_list(value): class AnyType(BaseType): pass +class LocalPackageSource(Model): + source = StringType(required=True) + + paths = ListType(StringType, serialize_when_none=False) + + configs = ListType(StringType, serialize_when_none=False) + class GitPackageSource(Model): uri = StringType(required=True) @@ -264,6 +271,8 @@ class S3PackageSource(Model): class PackageSources(Model): + local = ListType(ModelType(LocalPackageSource)) + git = ListType(ModelType(GitPackageSource)) s3 = ListType(ModelType(S3PackageSource)) diff --git a/stacker/util.py b/stacker/util.py index 92ccf562c..d14242eba 100644 --- a/stacker/util.py +++ b/stacker/util.py @@ -628,6 +628,9 @@ def create_cache_directories(self): def get_package_sources(self): """Make remote python packages available for local use.""" + # Checkout local modules + for config in self.sources.get('local', []): + self.fetch_local_package(config=config) # Checkout S3 repositories specified in config for config in self.sources.get('s3', []): self.fetch_s3_package(config=config) @@ -635,6 +638,18 @@ def get_package_sources(self): for config in self.sources.get('git', []): self.fetch_git_package(config=config) + def fetch_local_package(self, config): + """Make a local path available to current stacker config. + + Args: + config (dict): 'local' path config dictionary + + """ + # Update sys.path & merge in remote configs (if necessary) + self.update_paths_and_config(config=config, + pkg_dir_name=config['source'], + pkg_cache_dir=os.getcwd()) + def fetch_s3_package(self, config): """Make a remote S3 archive available for local use. @@ -773,21 +788,25 @@ def fetch_git_package(self, config): self.update_paths_and_config(config=config, pkg_dir_name=dir_name) - def update_paths_and_config(self, config, pkg_dir_name): + def update_paths_and_config(self, config, pkg_dir_name, + pkg_cache_dir=None): """Handle remote source defined sys.paths & configs. Args: config (dict): git config dictionary pkg_dir_name (string): directory name of the stacker archive + pkg_cache_dir (string): fully qualified path to stacker cache + cache directory """ - cached_dir_path = os.path.join(self.package_cache_dir, pkg_dir_name) + if pkg_cache_dir is None: + pkg_cache_dir = self.package_cache_dir + cached_dir_path = os.path.join(pkg_cache_dir, pkg_dir_name) # Add the appropriate directory (or directories) to sys.path if config.get('paths'): for path in config['paths']: - path_to_append = os.path.join(self.package_cache_dir, - pkg_dir_name, + path_to_append = os.path.join(cached_dir_path, path) logger.debug("Appending \"%s\" to python sys.path", path_to_append)