<s> import hashlib <EOL> import importlib <EOL> import logging <EOL> import os <EOL> from django . core . files . uploadedfile import TemporaryUploadedFile <EOL> from django . db . models import FieldDoesNotExist <EOL> from django . db . models . fields . files import FileField <EOL> from django . http import QueryDict <EOL> from django . utils import six <EOL> from django . utils . datastructures import MultiValueDict <EOL> logger = logging . getLogger ( __name__ ) <EOL> def parse_distutils_request ( request ) : <EOL> """<STR_LIT>""" <EOL> body = request . body . decode ( '<STR_LIT>' ) <EOL> if not body . endswith ( '<STR_LIT:\r\n>' ) : <EOL> sep = body . splitlines ( ) [ <NUM_LIT:1> ] <EOL> request . POST = QueryDict ( '<STR_LIT>' , mutable = True ) <EOL> try : <EOL> request . _files = MultiValueDict ( ) <EOL> except Exception : <EOL> pass <EOL> for part in filter ( lambda e : e . strip ( ) , body . split ( sep ) ) : <EOL> try : <EOL> header , content = part . lstrip ( ) . split ( '<STR_LIT:\n>' , <NUM_LIT:1> ) <EOL> except Exception : <EOL> continue <EOL> if content . startswith ( '<STR_LIT:\n>' ) : <EOL> content = content [ <NUM_LIT:1> : ] <EOL> if content . endswith ( '<STR_LIT:\n>' ) : <EOL> content = content [ : - <NUM_LIT:1> ] <EOL> headers = parse_header ( header ) <EOL> if "<STR_LIT:name>" not in headers : <EOL> continue <EOL> if "<STR_LIT:filename>" in headers and headers [ '<STR_LIT:name>' ] == '<STR_LIT:content>' : <EOL> dist = TemporaryUploadedFile ( name = headers [ "<STR_LIT:filename>" ] , <EOL> size = len ( content ) , <EOL> content_type = "<STR_LIT>" , <EOL> charset = '<STR_LIT:utf-8>' ) <EOL> dist . write ( content . encode ( '<STR_LIT:utf-8>' ) ) <EOL> dist . seek ( <NUM_LIT:0> ) <EOL> request . FILES . appendlist ( '<STR_LIT>' , dist ) <EOL> else : <EOL> request . POST . appendlist ( headers [ "<STR_LIT:name>" ] , content ) <EOL> else : <EOL> request . FILES [ '<STR_LIT>' ] = request . FILES [ '<STR_LIT:content>' ] <EOL> for key , value in request . POST . items ( ) : <EOL> if value == '<STR_LIT>' : <EOL> request . POST [ key ] = None <EOL> def parse_header ( header ) : <EOL> headers = { } <EOL> for kvpair in filter ( lambda p : p , <EOL> map ( lambda p : p . strip ( ) , <EOL> header . split ( '<STR_LIT:;>' ) ) ) : <EOL> try : <EOL> key , value = kvpair . split ( "<STR_LIT:=>" , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> continue <EOL> headers [ key . strip ( ) ] = value . strip ( '<STR_LIT:">' ) <EOL> return headers <EOL> def delete_files ( sender , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for fieldname in sender . _meta . get_all_field_names ( ) : <EOL> try : <EOL> field = sender . _meta . get_field ( fieldname ) <EOL> except FieldDoesNotExist : <EOL> continue <EOL> if isinstance ( field , FileField ) : <EOL> instance = kwargs [ '<STR_LIT>' ] <EOL> fieldfile = getattr ( instance , fieldname ) <EOL> if not hasattr ( fieldfile , '<STR_LIT:path>' ) : <EOL> return <EOL> if not os . path . exists ( fieldfile . path ) : <EOL> return <EOL> is_referenced = ( <EOL> instance . __class__ . _default_manager <EOL> . filter ( ** { '<STR_LIT>' % fieldname : fieldfile } ) <EOL> . exclude ( pk = instance . _get_pk_val ( ) ) <EOL> . exists ( ) ) <EOL> if is_referenced : <EOL> return <EOL> try : <EOL> field . storage . delete ( fieldfile . path ) <EOL> except Exception : <EOL> logger . exception ( <EOL> '<STR_LIT>' % ( <EOL> instance . pk , fieldfile . path ) ) <EOL> def md5_hash_file ( fh ) : <EOL> """<STR_LIT>""" <EOL> md5 = hashlib . md5 ( ) <EOL> while True : <EOL> data = fh . read ( <NUM_LIT> ) <EOL> if not data : <EOL> break <EOL> md5 . update ( data ) <EOL> return md5 . hexdigest ( ) <EOL> def get_versio_versioning_scheme ( full_class_path ) : <EOL> """<STR_LIT>""" <EOL> module_path = '<STR_LIT:.>' . join ( full_class_path . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> : - <NUM_LIT:1> ] ) <EOL> class_name = full_class_path . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> try : <EOL> module = importlib . import_module ( module_path ) <EOL> except ImportError : <EOL> raise RuntimeError ( '<STR_LIT>' . format ( full_class_path ) ) <EOL> try : <EOL> return getattr ( module , class_name ) <EOL> except AttributeError : <EOL> raise RuntimeError ( '<STR_LIT>' . format ( class_name , module_path ) ) </s>
