<s> from __future__ import ( absolute_import , division , print_function , unicode_literals ) <EOL> import sys <EOL> import argparse <EOL> import os <EOL> import re <EOL> import imp <EOL> import shutil <EOL> import subprocess <EOL> from envy import VERSION <EOL> from envy . decorators import validate_pkg , validate_env <EOL> def get_envy_base ( ) : <EOL> return os . path . expanduser ( "<STR_LIT>" ) <EOL> def get_active_venv ( ) : <EOL> if '<STR_LIT>' in os . environ : <EOL> return os . environ [ '<STR_LIT>' ] . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> if '<STR_LIT>' in os . environ : <EOL> venv_root = os . environ [ '<STR_LIT>' ] . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> return re . search ( "<STR_LIT>" . format ( venv_root ) , sys . prefix ) . group ( <NUM_LIT:1> ) <EOL> return re . search ( "<STR_LIT>" , sys . prefix ) . group ( <NUM_LIT:1> ) <EOL> def get_package_name ( pkg_path ) : <EOL> if ( '<STR_LIT:/>' not in pkg_path and pkg_path . endswith ( '<STR_LIT>' ) ) or pkg_path == '<STR_LIT:.>' : <EOL> return os . getcwd ( ) . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:1> ] <EOL> return pkg_path . split ( '<STR_LIT:/>' ) [ <NUM_LIT:0> ] <EOL> def get_envy_path ( pkg_path ) : <EOL> return os . path . expanduser ( "<STR_LIT>" . format ( get_active_venv ( ) , get_package_name ( pkg_path ) ) ) <EOL> def get_venv_full_package_path ( pkg_path ) : <EOL> return imp . find_module ( get_package_name ( pkg_path ) ) [ <NUM_LIT:1> ] <EOL> def original_backed_up ( pkg_path ) : <EOL> if not os . path . isdir ( get_envy_base ( ) ) : <EOL> os . makedirs ( '<STR_LIT:{}>' . format ( get_envy_base ( ) ) ) <EOL> if not os . path . isdir ( get_envy_base ( ) + "<STR_LIT>" . format ( get_active_venv ( ) ) ) : <EOL> os . makedirs ( '<STR_LIT:{}>' . format ( get_envy_base ( ) + "<STR_LIT>" . format ( get_active_venv ( ) ) ) ) <EOL> return os . path . isdir ( get_envy_path ( pkg_path ) ) <EOL> def back_up ( venv_pkg_path , pkg_path ) : <EOL> shutil . copytree ( venv_pkg_path , get_envy_path ( pkg_path ) ) <EOL> def get_editor ( ) : <EOL> editor = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if editor == '<STR_LIT>' : editor = '<STR_LIT>' <EOL> return editor <EOL> def get_file_path ( path ) : <EOL> split_file_path = path . split ( "<STR_LIT:/>" ) [ <NUM_LIT:1> : ] <EOL> return "<STR_LIT:/>" . join ( split_file_path ) <EOL> @ validate_env <EOL> def edit ( args ) : <EOL> pkg_name_given_in_arg = args . path [ <NUM_LIT:0> ] . split ( '<STR_LIT:/>' ) [ <NUM_LIT:0> ] <EOL> full_package_path = get_venv_full_package_path ( pkg_name_given_in_arg ) <EOL> file_path = get_file_path ( args . path [ <NUM_LIT:0> ] ) <EOL> if not original_backed_up ( args . path [ <NUM_LIT:0> ] ) : <EOL> print ( "<STR_LIT>" . format ( full_package_path ) ) <EOL> back_up ( full_package_path , args . path [ <NUM_LIT:0> ] ) <EOL> else : <EOL> print ( "<STR_LIT>" . format ( pkg_name_given_in_arg ) ) <EOL> editor = get_editor ( ) <EOL> subprocess . call ( [ editor , os . path . join ( full_package_path , file_path ) ] , shell = ( editor == '<STR_LIT>' ) ) <EOL> @ validate_env <EOL> @ validate_pkg <EOL> def sync ( args ) : <EOL> venv_pkg_path = get_venv_full_package_path ( args . package [ <NUM_LIT:0> ] ) <EOL> if not original_backed_up ( args . package [ <NUM_LIT:0> ] ) : <EOL> print ( "<STR_LIT>" . format ( venv_pkg_path ) ) <EOL> back_up ( venv_pkg_path , args . package [ <NUM_LIT:0> ] ) <EOL> else : <EOL> print ( "<STR_LIT>" . format ( args . package [ <NUM_LIT:0> ] . split ( '<STR_LIT:/>' ) [ <NUM_LIT:0> ] ) ) <EOL> try : <EOL> print ( "<STR_LIT>" ) <EOL> copytree ( args . package [ <NUM_LIT:0> ] , venv_pkg_path ) <EOL> print ( "<STR_LIT>" ) <EOL> except Exception as e : <EOL> clean ( args ) <EOL> raise e <EOL> @ validate_env <EOL> def clean ( args ) : <EOL> if args . all : <EOL> for package in os . listdir ( get_envy_base ( ) + "<STR_LIT>" . format ( get_active_venv ( ) ) ) : <EOL> restore_environment ( package ) <EOL> else : <EOL> restore_environment ( args . package [ <NUM_LIT:0> ] ) <EOL> def restore_environment ( package_name ) : <EOL> if not os . path . isdir ( get_envy_path ( package_name ) ) : <EOL> print ( "<STR_LIT>" . format ( get_envy_path ( package_name ) ) ) <EOL> return <EOL> venv_pkg_path = get_venv_full_package_path ( package_name ) <EOL> print ( "<STR_LIT>" . format ( package_name ) ) <EOL> shutil . rmtree ( venv_pkg_path ) <EOL> print ( "<STR_LIT>" . format ( package_name ) ) <EOL> shutil . copytree ( get_envy_path ( package_name ) , venv_pkg_path ) <EOL> if os . path . isdir ( venv_pkg_path ) : <EOL> print ( "<STR_LIT>" ) <EOL> shutil . rmtree ( get_envy_path ( package_name ) ) <EOL> def copytree ( src , dst ) : <EOL> if not os . path . exists ( dst ) : <EOL> os . makedirs ( dst ) <EOL> shutil . copystat ( src , dst ) <EOL> if os . path . isfile ( src ) : <EOL> shutil . copy2 ( src , dst ) <EOL> return <EOL> if not os . path . isdir ( src ) : <EOL> src = os . path . expanduser ( src ) <EOL> for item in os . listdir ( src ) : <EOL> ss = os . path . join ( src , item ) <EOL> dd = os . path . join ( dst , item ) <EOL> if os . path . isdir ( ss ) : <EOL> copytree ( ss , dd ) <EOL> else : <EOL> shutil . copy2 ( ss , dd ) <EOL> def prepare_parser ( ) : <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:version>' , version = '<STR_LIT>' + VERSION ) <EOL> subparsers = parser . add_subparsers ( dest = '<STR_LIT>' ) <EOL> parser_edit = subparsers . add_parser ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser_edit . set_defaults ( func = edit ) <EOL> parser_edit . add_argument ( '<STR_LIT:path>' , nargs = <NUM_LIT:1> ) <EOL> parser_clean = subparsers . add_parser ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser_clean . set_defaults ( func = clean ) <EOL> parser_clean . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:?>' , help = '<STR_LIT>' ) <EOL> parser_clean . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) <EOL> parser_sync = subparsers . add_parser ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser_sync . set_defaults ( func = sync ) <EOL> parser_sync . add_argument ( '<STR_LIT>' , nargs = <NUM_LIT:1> , help = '<STR_LIT>' ) <EOL> return parser <EOL> def main ( ) : <EOL> parser = prepare_parser ( ) <EOL> args = parser . parse_args ( ) <EOL> if hasattr ( args , '<STR_LIT>' ) : <EOL> args . func ( args ) <EOL> else : <EOL> parser . print_help ( ) </s>
