<s> from __future__ import unicode_literals <EOL> import errno <EOL> import logging <EOL> import os <EOL> import subprocess as sp <EOL> import sys <EOL> import time <EOL> import unicodedata <EOL> log = logging . getLogger ( __name__ ) <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> : <EOL> def enc ( text ) : <EOL> if isinstance ( text , bytes ) : <EOL> return text <EOL> return text . encode ( ) <EOL> def dec ( text ) : <EOL> if isinstance ( text , bytes ) : <EOL> return text . decode ( '<STR_LIT:utf-8>' ) <EOL> return text <EOL> def write ( pipe , data ) : <EOL> try : <EOL> pipe . stdin . write ( data ) <EOL> except IOError as e : <EOL> if e . errno != errno . EPIPE : <EOL> raise <EOL> else : <EOL> def enc ( text ) : <EOL> if isinstance ( text , unicode ) : <EOL> return text . encode ( '<STR_LIT:utf-8>' ) <EOL> return text <EOL> def dec ( text ) : <EOL> if isinstance ( text , unicode ) : <EOL> return text <EOL> return text . decode ( '<STR_LIT:utf-8>' ) <EOL> def write ( pipe , data ) : <EOL> pipe . stdin . write ( data ) <EOL> def normalize_path ( path ) : <EOL> if sys . platform == "<STR_LIT>" : <EOL> return unicodedata . normalize ( "<STR_LIT>" , dec ( path ) ) <EOL> return path <EOL> def try_rebase ( remote , branch ) : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' % ( remote , branch ) ] <EOL> p = sp . Popen ( cmd , stdin = sp . PIPE , stdout = sp . PIPE , stderr = sp . PIPE ) <EOL> ( rev , _ ) = p . communicate ( ) <EOL> if p . wait ( ) != <NUM_LIT:0> : <EOL> return True <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' % branch , dec ( rev . strip ( ) ) ] <EOL> if sp . call ( cmd ) != <NUM_LIT:0> : <EOL> return False <EOL> return True <EOL> def get_config ( key ) : <EOL> p = sp . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , key ] , stdin = sp . PIPE , stdout = sp . PIPE ) <EOL> ( value , _ ) = p . communicate ( ) <EOL> return value . decode ( '<STR_LIT:utf-8>' ) . strip ( ) <EOL> def get_prev_commit ( branch ) : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , branch , '<STR_LIT>' ] <EOL> p = sp . Popen ( cmd , stdin = sp . PIPE , stdout = sp . PIPE , stderr = sp . PIPE ) <EOL> ( rev , _ ) = p . communicate ( ) <EOL> if p . wait ( ) != <NUM_LIT:0> : <EOL> return None <EOL> return rev . decode ( '<STR_LIT:utf-8>' ) . strip ( ) <EOL> def mk_when ( timestamp = None ) : <EOL> if timestamp is None : <EOL> timestamp = int ( time . time ( ) ) <EOL> currtz = "<STR_LIT>" % ( - <NUM_LIT:1> * time . timezone / <NUM_LIT> ) <EOL> return "<STR_LIT>" % ( timestamp , currtz ) <EOL> def start_commit ( pipe , branch , message ) : <EOL> uname = dec ( get_config ( "<STR_LIT>" ) ) <EOL> email = dec ( get_config ( "<STR_LIT>" ) ) <EOL> write ( pipe , enc ( '<STR_LIT>' % branch ) ) <EOL> write ( pipe , enc ( '<STR_LIT>' % ( uname , email , mk_when ( ) ) ) ) <EOL> write ( pipe , enc ( '<STR_LIT>' % ( len ( message ) , message ) ) ) <EOL> head = get_prev_commit ( branch ) <EOL> if head : <EOL> write ( pipe , enc ( '<STR_LIT>' % head ) ) <EOL> write ( pipe , enc ( '<STR_LIT>' ) ) <EOL> def add_file ( pipe , srcpath , tgtpath ) : <EOL> with open ( srcpath , "<STR_LIT:rb>" ) as handle : <EOL> if os . access ( srcpath , os . X_OK ) : <EOL> write ( pipe , enc ( '<STR_LIT>' % tgtpath ) ) <EOL> else : <EOL> write ( pipe , enc ( '<STR_LIT>' % tgtpath ) ) <EOL> data = handle . read ( ) <EOL> write ( pipe , enc ( '<STR_LIT>' % len ( data ) ) ) <EOL> write ( pipe , enc ( data ) ) <EOL> write ( pipe , enc ( '<STR_LIT:\n>' ) ) <EOL> def add_nojekyll ( pipe ) : <EOL> write ( pipe , enc ( '<STR_LIT>' ) ) <EOL> write ( pipe , enc ( '<STR_LIT>' ) ) <EOL> write ( pipe , enc ( '<STR_LIT:\n>' ) ) <EOL> def gitpath ( fname ) : <EOL> norm = os . path . normpath ( fname ) <EOL> return "<STR_LIT:/>" . join ( norm . split ( os . path . sep ) ) <EOL> def run_import ( srcdir , branch , message , nojekyll ) : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> kwargs = { "<STR_LIT>" : sp . PIPE } <EOL> if sys . version_info >= ( <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:0> ) : <EOL> kwargs [ "<STR_LIT>" ] = False <EOL> pipe = sp . Popen ( cmd , ** kwargs ) <EOL> start_commit ( pipe , branch , message ) <EOL> for path , _ , fnames in os . walk ( srcdir ) : <EOL> for fn in fnames : <EOL> fpath = os . path . join ( path , fn ) <EOL> fpath = normalize_path ( fpath ) <EOL> gpath = gitpath ( os . path . relpath ( fpath , start = srcdir ) ) <EOL> add_file ( pipe , fpath , gpath ) <EOL> if nojekyll : <EOL> add_nojekyll ( pipe ) <EOL> write ( pipe , enc ( '<STR_LIT:\n>' ) ) <EOL> pipe . stdin . close ( ) <EOL> if pipe . wait ( ) != <NUM_LIT:0> : <EOL> sys . stdout . write ( enc ( "<STR_LIT>" ) ) <EOL> def ghp_import ( directory , message , remote = '<STR_LIT>' , branch = '<STR_LIT>' ) : <EOL> if not try_rebase ( remote , branch ) : <EOL> log . error ( "<STR_LIT>" , branch ) <EOL> nojekyll = True <EOL> run_import ( directory , branch , message , nojekyll ) <EOL> proc = sp . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , remote , branch ] , <EOL> stdout = sp . PIPE , stderr = sp . PIPE ) <EOL> proc . communicate ( ) <EOL> return proc . wait ( ) == <NUM_LIT:0> </s>
