<s> from __future__ import print_function , division , absolute_import <EOL> import re <EOL> import os <EOL> import sys <EOL> import logging <EOL> from os . path import isdir , isfile , join <EOL> from argparse import RawDescriptionHelpFormatter <EOL> import conda . install as install <EOL> import conda . config as config <EOL> from conda . cli import common <EOL> from conda . egg_info import get_egg_info <EOL> descr = "<STR_LIT>" <EOL> examples = """<STR_LIT>""" <EOL> log = logging . getLogger ( __name__ ) <EOL> def configure_parser ( sub_parsers ) : <EOL> p = sub_parsers . add_parser ( <EOL> '<STR_LIT:list>' , <EOL> description = descr , <EOL> help = descr , <EOL> formatter_class = RawDescriptionHelpFormatter , <EOL> epilog = examples , <EOL> add_help = False , <EOL> ) <EOL> common . add_parser_help ( p ) <EOL> common . add_parser_prefix ( p ) <EOL> common . add_parser_json ( p ) <EOL> common . add_parser_show_channel_urls ( p ) <EOL> p . add_argument ( <EOL> '<STR_LIT:-c>' , "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" , <EOL> ) <EOL> p . add_argument ( <EOL> '<STR_LIT>' , "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" , <EOL> ) <EOL> p . add_argument ( <EOL> "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> ) <EOL> p . add_argument ( <EOL> "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" , <EOL> ) <EOL> p . add_argument ( <EOL> '<STR_LIT>' , "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> ) <EOL> p . add_argument ( <EOL> '<STR_LIT>' , "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" , <EOL> ) <EOL> p . add_argument ( <EOL> "<STR_LIT>" , <EOL> action = "<STR_LIT>" , <EOL> default = True , <EOL> dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> p . add_argument ( <EOL> '<STR_LIT>' , <EOL> action = "<STR_LIT:store>" , <EOL> nargs = "<STR_LIT:?>" , <EOL> help = "<STR_LIT>" , <EOL> ) <EOL> p . set_defaults ( func = execute ) <EOL> def print_export_header ( ) : <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % config . subdir ) <EOL> def get_packages ( installed , regex ) : <EOL> pat = re . compile ( regex , re . I ) if regex else None <EOL> for dist in sorted ( installed , key = str . lower ) : <EOL> name = install . name_dist ( dist ) <EOL> if pat and pat . search ( name ) is None : <EOL> continue <EOL> yield dist <EOL> def list_packages ( prefix , installed , regex = None , format = '<STR_LIT>' , <EOL> show_channel_urls = config . show_channel_urls ) : <EOL> res = <NUM_LIT:1> <EOL> result = [ ] <EOL> for dist in get_packages ( installed , regex ) : <EOL> res = <NUM_LIT:0> <EOL> if format == '<STR_LIT>' : <EOL> result . append ( dist ) <EOL> continue <EOL> if format == '<STR_LIT>' : <EOL> result . append ( '<STR_LIT:=>' . join ( dist . rsplit ( '<STR_LIT:->' , <NUM_LIT:2> ) ) ) <EOL> continue <EOL> try : <EOL> info = install . is_linked ( prefix , dist ) <EOL> features = set ( info . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( ) ) <EOL> disp = '<STR_LIT>' % info <EOL> disp += '<STR_LIT>' % common . disp_features ( features ) <EOL> schannel = info . get ( '<STR_LIT>' ) <EOL> if show_channel_urls or show_channel_urls is None and schannel != '<STR_LIT>' : <EOL> disp += '<STR_LIT>' % schannel <EOL> result . append ( disp ) <EOL> except ( AttributeError , IOError , KeyError , ValueError ) as e : <EOL> log . debug ( str ( e ) ) <EOL> result . append ( '<STR_LIT>' % tuple ( dist . rsplit ( '<STR_LIT:->' , <NUM_LIT:2> ) ) ) <EOL> return res , result <EOL> def print_packages ( prefix , regex = None , format = '<STR_LIT>' , piplist = False , <EOL> json = False , show_channel_urls = config . show_channel_urls ) : <EOL> if not isdir ( prefix ) : <EOL> common . error_and_exit ( """<STR_LIT>""" % <EOL> prefix , <EOL> json = json , <EOL> error_type = "<STR_LIT>" ) <EOL> if not json : <EOL> if format == '<STR_LIT>' : <EOL> print ( '<STR_LIT>' % prefix ) <EOL> print ( '<STR_LIT:#>' ) <EOL> if format == '<STR_LIT>' : <EOL> print_export_header ( ) <EOL> installed = install . linked ( prefix ) <EOL> if piplist and config . use_pip and format == '<STR_LIT>' : <EOL> installed . update ( get_egg_info ( prefix ) ) <EOL> exitcode , output = list_packages ( prefix , installed , regex , format = format , <EOL> show_channel_urls = show_channel_urls ) <EOL> if not json : <EOL> print ( '<STR_LIT:\n>' . join ( output ) ) <EOL> else : <EOL> common . stdout_json ( output ) <EOL> return exitcode <EOL> def print_explicit ( prefix , add_md5 = False ) : <EOL> import json <EOL> if not isdir ( prefix ) : <EOL> common . error_and_exit ( "<STR_LIT>" % prefix ) <EOL> print_export_header ( ) <EOL> print ( "<STR_LIT>" ) <EOL> meta_dir = join ( prefix , '<STR_LIT>' ) <EOL> for fn in sorted ( os . listdir ( meta_dir ) ) : <EOL> if not fn . endswith ( '<STR_LIT>' ) : <EOL> continue <EOL> with open ( join ( meta_dir , fn ) ) as fi : <EOL> meta = json . load ( fi ) <EOL> url = meta . get ( '<STR_LIT:url>' ) <EOL> def format_url ( ) : <EOL> return '<STR_LIT>' % ( meta [ '<STR_LIT>' ] , meta [ '<STR_LIT:name>' ] , <EOL> meta [ '<STR_LIT:version>' ] , meta [ '<STR_LIT>' ] ) <EOL> if not url : <EOL> try : <EOL> url = format_url ( ) <EOL> except KeyError : <EOL> print ( '<STR_LIT>' % fn [ : - <NUM_LIT:5> ] ) <EOL> continue <EOL> if url . startswith ( '<STR_LIT:file>' ) : <EOL> try : <EOL> url = format_url ( ) <EOL> except KeyError : <EOL> continue <EOL> md5 = meta . get ( '<STR_LIT>' ) <EOL> print ( url + ( '<STR_LIT>' % md5 if add_md5 and md5 else '<STR_LIT>' ) ) <EOL> def execute ( args , parser ) : <EOL> prefix = common . get_prefix ( args ) <EOL> regex = args . regex <EOL> if args . full_name : <EOL> regex = r'<STR_LIT>' % regex <EOL> if args . revisions : <EOL> from conda . history import History <EOL> h = History ( prefix ) <EOL> if isfile ( h . path ) : <EOL> if not args . json : <EOL> h . print_log ( ) <EOL> else : <EOL> common . stdout_json ( h . object_log ( ) ) <EOL> else : <EOL> common . error_and_exit ( "<STR_LIT>" % h . path , <EOL> json = args . json , <EOL> error_type = "<STR_LIT>" ) <EOL> return <EOL> if args . explicit : <EOL> print_explicit ( prefix , args . md5 ) <EOL> return <EOL> if args . canonical : <EOL> format = '<STR_LIT>' <EOL> elif args . export : <EOL> format = '<STR_LIT>' <EOL> else : <EOL> format = '<STR_LIT>' <EOL> if args . json : <EOL> format = '<STR_LIT>' <EOL> exitcode = print_packages ( prefix , regex , format , piplist = args . pip , <EOL> json = args . json , <EOL> show_channel_urls = args . show_channel_urls ) <EOL> sys . exit ( exitcode ) </s>
