<s> """<STR_LIT>""" <EOL> from __future__ import division , print_function , unicode_literals <EOL> import logging <EOL> import platform <EOL> from rpaths import Path <EOL> import subprocess <EOL> import time <EOL> from reprozip . common import Package <EOL> from reprozip . utils import listvalues <EOL> magic_dirs = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> system_dirs = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class PkgManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . unknown_files = set ( ) <EOL> self . packages = { } <EOL> def filter_files ( self , files ) : <EOL> seen_files = set ( ) <EOL> for f in files : <EOL> if f . path not in seen_files : <EOL> if not self . _filter ( f ) : <EOL> yield f <EOL> seen_files . add ( f . path ) <EOL> def search_for_files ( self , files ) : <EOL> for f in self . filter_files ( files ) : <EOL> pkgname = self . _get_package_for_file ( f . path ) <EOL> if pkgname is None : <EOL> self . unknown_files . add ( f ) <EOL> else : <EOL> if pkgname in self . packages : <EOL> self . packages [ pkgname ] . add_file ( f ) <EOL> else : <EOL> pkg = self . _create_package ( pkgname ) <EOL> pkg . add_file ( f ) <EOL> self . packages [ pkgname ] = pkg <EOL> def _filter ( self , f ) : <EOL> if any ( f . path . lies_under ( c ) for c in magic_dirs ) : <EOL> return True <EOL> if ( f . path . lies_under ( '<STR_LIT>' ) or <EOL> not any ( f . path . lies_under ( c ) for c in system_dirs ) ) : <EOL> self . unknown_files . add ( f ) <EOL> return True <EOL> return False <EOL> def _get_package_for_file ( self , filename ) : <EOL> raise NotImplementedError <EOL> def _create_package ( self , pkgname ) : <EOL> raise NotImplementedError <EOL> class DpkgManager ( PkgManager ) : <EOL> """<STR_LIT>""" <EOL> def search_for_files ( self , files ) : <EOL> requested = dict ( ( f . path , f ) for f in self . filter_files ( files ) ) <EOL> for listfile in Path ( '<STR_LIT>' ) . listdir ( ) : <EOL> package = None <EOL> if not listfile . unicodename . endswith ( '<STR_LIT>' ) : <EOL> continue <EOL> with listfile . open ( '<STR_LIT:rb>' ) as fp : <EOL> l = fp . readline ( ) <EOL> while l : <EOL> if l [ - <NUM_LIT:1> : ] == b'<STR_LIT:\n>' : <EOL> l = l [ : - <NUM_LIT:1> ] <EOL> path = Path ( l ) <EOL> if path in requested : <EOL> if package is None : <EOL> pkgname = listfile . unicodename [ : - <NUM_LIT:5> ] <EOL> pkgname = pkgname . split ( '<STR_LIT::>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if pkgname in self . packages : <EOL> package = self . packages [ pkgname ] <EOL> else : <EOL> package = self . _create_package ( pkgname ) <EOL> self . packages [ pkgname ] = package <EOL> package . add_file ( requested . pop ( path ) ) <EOL> l = fp . readline ( ) <EOL> self . unknown_files . update ( f for f in files if f . path in requested ) <EOL> def _filter ( self , f ) : <EOL> if f . path . is_dir ( ) : <EOL> self . unknown_files . add ( f ) <EOL> return True <EOL> return super ( DpkgManager , self ) . _filter ( f ) <EOL> def _get_package_for_file ( self , filename ) : <EOL> assert False <EOL> def _create_package ( self , pkgname ) : <EOL> p = subprocess . Popen ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> pkgname ] , <EOL> stdout = subprocess . PIPE ) <EOL> try : <EOL> size = version = None <EOL> for l in p . stdout : <EOL> fields = l . split ( ) <EOL> name = fields [ <NUM_LIT:0> ] . decode ( '<STR_LIT:ascii>' ) . split ( '<STR_LIT::>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if name == pkgname : <EOL> version = fields [ <NUM_LIT:1> ] . decode ( '<STR_LIT:ascii>' ) <EOL> size = int ( fields [ <NUM_LIT:2> ] . decode ( '<STR_LIT:ascii>' ) ) * <NUM_LIT> <EOL> break <EOL> finally : <EOL> p . wait ( ) <EOL> assert p . returncode == <NUM_LIT:0> <EOL> return Package ( pkgname , version , size = size ) <EOL> def identify_packages ( files ) : <EOL> """<STR_LIT>""" <EOL> distribution = platform . linux_distribution ( ) [ <NUM_LIT:0> ] . lower ( ) <EOL> if distribution == '<STR_LIT>' : <EOL> manager = DpkgManager ( ) <EOL> elif distribution == '<STR_LIT>' : <EOL> manager = DpkgManager ( ) <EOL> else : <EOL> return files , [ ] <EOL> begin = time . time ( ) <EOL> manager . search_for_files ( files ) <EOL> logging . debug ( "<STR_LIT>" , <EOL> ( time . time ( ) - begin ) ) <EOL> return manager . unknown_files , listvalues ( manager . packages ) </s>
