Improve running project from other directory (not CWD)

This commit is contained in:
Ivan Kravets
2015-05-25 23:26:35 +03:00
parent 7649769437
commit be70047233
3 changed files with 23 additions and 15 deletions

View File

@ -2,7 +2,7 @@
# See LICENSE for details. # See LICENSE for details.
from datetime import datetime from datetime import datetime
from os import chdir, getcwd from os import getcwd
from os.path import getmtime, isdir, join from os.path import getmtime, isdir, join
from shutil import rmtree from shutil import rmtree
from time import time from time import time
@ -28,9 +28,7 @@ from platformio.platforms.base import PlatformFactory
@click.pass_context @click.pass_context
def cli(ctx, environment, target, upload_port, # pylint: disable=R0913,R0914 def cli(ctx, environment, target, upload_port, # pylint: disable=R0913,R0914
project_dir, verbose): project_dir, verbose):
initial_cwd = getcwd() with util.cd(project_dir):
chdir(project_dir)
try:
config = util.get_project_config() config = util.get_project_config()
if not config.sections(): if not config.sections():
@ -74,8 +72,6 @@ def cli(ctx, environment, target, upload_port, # pylint: disable=R0913,R0914
if not all(results): if not all(results):
raise exception.ReturnErrorCode() raise exception.ReturnErrorCode()
finally:
chdir(initial_cwd)
class EnvironmentProcessor(object): class EnvironmentProcessor(object):

View File

@ -26,16 +26,16 @@ class ProjectGenerator(object):
return [d for d in listdir(tpls_dir) return [d for d in listdir(tpls_dir)
if isdir(join(tpls_dir, d))] if isdir(join(tpls_dir, d))]
@staticmethod def get_project_env(self):
def get_project_env():
data = {} data = {}
config = util.get_project_config() with util.cd(self.project_dir):
for section in config.sections(): config = util.get_project_config()
if not section.startswith("env:"): for section in config.sections():
continue if not section.startswith("env:"):
data['env_name'] = section[4:] continue
for k, v in config.items(section): data['env_name'] = section[4:]
data[k] = v for k, v in config.items(section):
data[k] = v
return data return data
@util.memoized @util.memoized

View File

@ -55,6 +55,18 @@ class AsyncPipe(Thread):
self.join() self.join()
class cd:
def __init__(self, new_path):
self.new_path = new_path
self.prev_path = os.getcwd()
def __enter__(self):
os.chdir(self.new_path)
def __exit__(self, etype, value, traceback):
os.chdir(self.prev_path)
class memoized(object): class memoized(object):
''' '''
Decorator. Caches a function's return value each time it is called. Decorator. Caches a function's return value each time it is called.