Exemplo n.º 1
0
def update(PAK, MIRROR, WORKDIR):
    """
    Update package 'PAK' in packages.rules.
    MIRROR is either a http or a ftp URL
    WORKDIR is the root directory to install everything to.
    """
    if( MIRROR.endswith("/PAK/") == False ):
        MIRROR = MIRROR+"/PAK/"
    elif( MIRROR.endswith("PAK/") == False):
        MIRROR = MIRROR+"PAK/"
    # --- Below this line: SETUP VARIABLES ---
    if(PAK.endswith(".json")==False):
        if(os.path.isdir("packages.rules"+os.sep+PAK)):
            return
        PAK=PAK+".json"  # Easier JSON reading in the future.
                            # Works better than constantly appending '.json' to PAK.
    TARFILE=packages.getTarfile(PAK)
    if (TARFILE == None):
        print( PAK+" doesn't point to a file. What do?")
        sys.exit(1)
    if (TARFILE.startswith("http://") == True or TARFILE.startswith("ftp://") == True or TARFILE.startswith("https://") == True):
        FILE = TARFILE.split("/")[-1]
        MIRROR=TARFILE[0:-len(FILE)]
        TARFILE=TARFILE[-len(FILE):]
    if( TARFILE.endswith("bz2") == True):
        TARTYPE = "bz2"
    elif( TARFILE.endswith( "gz") == True):
        TARTYPE = "gz"
    else:
        print( bcolors.RED+"* WARNING"+bcolors.ENDC+": Tar file isn't bz2 or gzip")
    NAME = json.loads( open( "packages.rules"+os.sep+PAK).read() ).get("name")
    if (NAME == None):
        NAME=TARFILE[:-8]
    INSTDIR=packages.getInstdir(PAK)
    if( os.path.exists(WORKDIR) == False):
        os.mkdir(WORKDIR)
    DEPENDS = []
    DEPENDS=packages.getDepend(PAK)
    bcolors.YES = bcolors.GREEN+"Y"+bcolors.ENDC
    bcolors.NO = bcolors.RED+"n"+bcolors.ENDC
    if( DEPENDS != None):
        for DEPEND in DEPENDS:
            DEPENDTAR = packages.getTarfile(DEPEND+".json")
            if( os.path.exists( "packages.checksum"+os.sep+DEPENDTAR+".checksum" ) == True):
                DEPENDS.remove(DEPEND)
                continue
        if( DEPENDS != [] ):
            print( PAK+" depends on" )
            print( DEPENDS )
            print( "Install above dependencies?"+" ["+bcolors.YES+"/"+bcolors.NO+"]")
            IN = input()
            if( IN.endswith("\r")==True ):
                IN = IN[:-1]
            while( IN.lower() != "y" or IN.lower() != "yes"):
                if( IN.lower() == "n" or IN.lower() == "no"):
                    return
                if( IN.lower() == "y" or IN.lower() == "yes"):
                    break
                if( IN.lower() == "" ):
                    break
                print( "Sorry, answer not recognized '"+IN+"'" )
                IN = input()
                if( IN.endswith("\r")==True ):
                    IN = IN[:-1]
            for DEPEND in DEPENDS:
                RETCODE = update(DEPEND, MIRROR, WORKDIR)
                if( RETCODE == 1):
                    sys.exit(1)
                DEPENDS.remove(DEPEND)
    INSTALLSCRIPT = packages.getInstallScript(PAK) # If this isn't needed, we won't use it.

    # --- Below this line: PRINT PACKAGE CRITICAL INFO ---
    print( bcolors.GREEN+"*"+bcolors.ENDC+" "+"Name"+": "+NAME)
    print( bcolors.GREEN+"*"+bcolors.ENDC+" "+"Package JSON"+": "+PAK)
    print( bcolors.GREEN+"*"+bcolors.ENDC+" "+"Package file"+": "+TARFILE)

    # --- Below this line: ACTUALLY UPDATE PACKAGE 'PAK' ---
    print(">>> Fetching checksum"+" '"+TARFILE+".checksum'")
    get.download(args.VERBOSE,MIRROR+TARFILE+".checksum", WORKDIR+TARFILE+".checksum")
    m = open(WORKDIR+TARFILE+".checksum", 'r').read()
    print( bcolors.GREEN+"*"+bcolors.ENDC+" SHA-1: "+m.split("\n")[0] )
    if ( os.path.exists("packages.checksum"+os.sep+TARFILE+".checksum") == False ):
        if ( os.access("packages.checksum"+os.sep, os.F_OK) == False):
            os.mkdir("packages.checksum")
        print( ">>> Downloading"+" '"+TARFILE+"' -> '"+WORKDIR+TARFILE+"'")
        get.download( True, MIRROR+TARFILE, WORKDIR+TARFILE )
        m = hashlib.sha1(open(WORKDIR+TARFILE, "rb").read()).hexdigest()
    else:
        mm = open("packages.checksum"+os.sep+TARFILE+".checksum", "r").read()
        if ( m == mm ):
            print( "Project is up-to-date!")
            clean.rm(WORKDIR+TARFILE+".checksum")
            return
        else:
            print( ">>> Downloading"+" '"+TARFILE+"' -> '"+WORKDIR+TARFILE+"'")
            get.download( True, MIRROR+TARFILE, WORKDIR+TARFILE )
            m = hashlib.sha1(open(WORKDIR+TARFILE, "rb").read()).hexdigest()
            
    # --- Below this line: EXTRACT UPDATE PACKAGE 'PAK' ---
    FILE = tarfile.open(WORKDIR+TARFILE, 'r')
    if( args.VERBOSE == False ):
        print( ">>> Extracting '"+TARFILE+"' -> '"+WORKDIR+INSTDIR+"'")
        FILE.extractall(WORKDIR+INSTDIR)
    else:
        print( ">>> Extracting '"+FILE.name.split(os.sep)[-1]+"'" )
        MEMBERS=FILE.getmembers()
        for MEMBS in MEMBERS:
            print( ">>> '"+WORKDIR+INSTDIR+MEMBS.name+"'")
            FILE.extract(MEMBS.name, WORKDIR+INSTDIR)
    FILE.close()
    del FILE
    clean.rm(WORKDIR+TARFILE)
    
    # --- Below this line: INSTALLSCRIPT ---
    CXX = compile.check(PAK, WORKDIR=WORKDIR)
    if( CXX != None):
        print( bcolors.GREEN+"*"+bcolors.ENDC+" CXX: "+CXX)
        CXX = compile.compile(PAK, CXX, WORKDIR)
    if( CXX != None and CXX != 0 ):
        print( bcolors.RED+"[ERROR]: Couldn't compile!" )
        print( ">>> Cleaning up and returning 1!"+bcolors.ENDC )
        # clean.rm( WORKDIR+INSTDIR ) -- NO U
        clean.rm( WORKDIR+TARFILE )
        clean.rm( WORKDIR+TARFILE+".checksum" )
        return 1
    del CXX

    # --- Below this line: sha1 hash generation ---
    mOut = open("packages.checksum"+os.sep+TARFILE+".checksum", "w")
    mOut.write(m)
    mOut.close()
    del m
    os.remove(WORKDIR+TARFILE+".checksum")

    # --- Below this line: Project is now installed, write to "packages.installed" ---
    if( os.path.isfile( "packages.installed" ) == False):
        PACKINST = []
    else:
        PACKINST = open( "packages.installed", "r" ).read().split("\n")
    while( PACKINST.count(PAK[:-5]) > 0 ):
        PACKINST.remove(PAK[:-5])
    while( PACKINST.count("\n") > 0 ):
        PACKINST.remove("\n")
    while( PACKINST.count("") > 0 ):
        PACKINST.remove("")
    PACKOUT = open( "packages.installed", "w" )
    for LINES in PACKINST:
        PACKOUT.write(LINES+"\n")
    PACKOUT.write(PAK[:-5]+"\n")
    PACKOUT.close()
    del PACKOUT

    # --- Below this line: REMOVE UNNEEDED FILES (IF THEY EXIST) ---
    CLEANUP = [WORKDIR+TARFILE, WORKDIR+TARFILE+".checksum"]
    for FILES in CLEANUP:
        if( os.path.exists(FILES) == False):
            continue
        clean.rm( FILES )
    del CLEANUP
Exemplo n.º 2
0
def backup(OBJ, WORKDIR, BACKUPDIR):
    """
    Backup OBJ from WORKDIR, to BACKUPDIR.
    The installation directory of OBJ will be read from OBJ+'.json'
    OBJ will be packaged in BACKUPDIR as NAME+'.tar.bz2'
    """
    if(os.access("packages.rules"+os.sep+OBJ+".json", os.R_OK) == True):
            INSTDIR = packages.getInstdir(OBJ+".json")
            BACKUPTAR = packages.getTarfile(OBJ+".json")
            if(BACKUPTAR.startswith("http://") == True):
                BACKUPTAR="%s" % (
                    BACKUPTAR.split("/")[-1]
                )
            if (INSTDIR == None):
                INSTDIR=WORKDIR+BACKUPTAR[:-8]
            else:
                INSTDIR=WORKDIR+INSTDIR
            INSTDIR=varCheck.do(INSTDIR)
            if (INSTDIR.endswith(os.sep) == False):
                INSTDIR=INSTDIR+os.sep
            if(os.access(BACKUPDIR, os.F_OK) == False):
                os.makedirs(BACKUPDIR)
            if(os.access(WORKDIR, os.F_OK) == False):
                print( bcolors.RED+"Workdir '"+WORKDIR+"' does NOT exist!"+bcolors.ENDC)
                sys.exit(1)
            if(os.access(INSTDIR, os.F_OK) == False):
                print( bcolors.RED+"Package '"+OBJ+"' is NOT installed."+bcolors.ENDC)
                return
            TARFILE=tarfile.open(BACKUPDIR+BACKUPTAR, "w:bz2")
            if(args.QUIET==False or None):
                print( bcolors.GREEN+"+"+bcolors.ENDC+"--"+OBJ+".json" )
            if( INSTDIR==(WORKDIR+"."+os.sep) ):
                print( "(WIP)")
                return
            else:
                HOME=os.path.abspath(os.curdir)
                os.chdir(INSTDIR)
                for FILES in os.listdir(os.curdir):
                    if((FILES+os.sep)==BACKUPDIR):
                        continue
                    elif((FILES+os.sep)==WORKDIR):
                        continue
                    if(args.QUIET==False or None):
                        print( bcolors.YELLOW+"|"+bcolors.ENDC+"-"+FILES)
                    TARFILE.add(FILES)
                os.chdir(HOME)
            TARFILE.close()
            del TARFILE
            m = hashlib.sha1(open(BACKUPDIR+BACKUPTAR, "rb").read()).hexdigest()
            if(args.NEW == True):
                if(os.access("packages.checksum"+os.sep+BACKUPTAR+".checksum", os.W_OK) == False):
                    print( "Please re-run this script with write-access to 'packages.checksum"+os.sep+"'.")
                    return
                mOut = open("packages.checksum"+os.sep+BACKUPTAR+".checksum", "w")
                mOut.write(m)
                mOut.close()
            mOut = open(BACKUPDIR+BACKUPTAR+".checksum", "w")
            mOut.write(m)
            mOut.close()
            del m
            del mOut
Exemplo n.º 3
0
        shutil.rmtree(OBJ)
        return
    else:
        print("REM '"+OBJ+"'")
        os.remove(OBJ)
        return

if __name__ == "__main__":
    WORKDIR = settings.getWorkdir()
    BACKUPDIR = settings.getBackupdir()
    for ARGS in sys.argv:
        if(ARGS == sys.argv[0]):
            continue
        if(os.access("packages.rules"+os.sep+ARGS+".json", os.R_OK) == True):
            INSTDIR=packages.getInstdir(ARGS+".json")
            TARFILE=packages.getTarfile(ARGS+".json")
            if(INSTDIR==None):
                INSTDIR=TARFILE[:-8]
            rm(WORKDIR+INSTDIR)
            rm("packages.checksum"+os.sep+TARFILE+".checksum")
            if( os.path.isfile("packages.installed") == True):
                PACK = open("packages.installed","r").read().split("\n")
                PACK.remove(ARGS)
                PACKOUT = open("packages.installed", "w")
                for LINES in PACK:
                    PACKOUT.write(LINES+"\n")
                PACKOUT.close()
                del PACK
                del PACKOUT
            continue
        if(ARGS.lower() == "all"):